views_handler_argument.inc 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. <?php
  2. /**
  3. * @file
  4. * @todo.
  5. */
  6. /**
  7. * @defgroup views_argument_handlers Views argument handlers
  8. * Handlers to tell Views how to contextually filter queries.
  9. * @{
  10. */
  11. /**
  12. * Base class for arguments.
  13. *
  14. * The basic argument works for very simple arguments such as nid and uid
  15. *
  16. * Definition terms for this handler:
  17. * - name field: The field to use for the name to use in the summary, which is
  18. * the displayed output. For example, for the node: nid argument,
  19. * the argument itself is the nid, but node.title is displayed.
  20. * - name table: The table to use for the name, should it not be in the same
  21. * table as the argument.
  22. * - empty field name: For arguments that can have no value, such as taxonomy
  23. * which can have "no term", this is the string which
  24. * will be displayed for this lack of value. Be sure to use
  25. * t().
  26. * - validate type: A little used string to allow an argument to restrict
  27. * which validator is available to just one. Use the
  28. * validator ID. This probably should not be used at all,
  29. * and may disappear or change.
  30. * - numeric: If set to TRUE this field is numeric and will use %d instead of
  31. * %s in queries.
  32. *
  33. * @ingroup views_argument_handlers
  34. */
  35. class views_handler_argument extends views_handler {
  36. var $validator = NULL;
  37. var $argument = NULL;
  38. var $value = NULL;
  39. /**
  40. * The table to use for the name, should it not be in the same table as the argument.
  41. * @var string
  42. */
  43. var $name_table;
  44. /**
  45. * The field to use for the name to use in the summary, which is
  46. * the displayed output. For example, for the node: nid argument,
  47. * the argument itself is the nid, but node.title is displayed.
  48. * @var string
  49. */
  50. var $name_field;
  51. /**
  52. * Constructor
  53. */
  54. function construct() {
  55. parent::construct();
  56. if (!empty($this->definition['name field'])) {
  57. $this->name_field = $this->definition['name field'];
  58. }
  59. if (!empty($this->definition['name table'])) {
  60. $this->name_table = $this->definition['name table'];
  61. }
  62. }
  63. function init(&$view, &$options) {
  64. parent::init($view, $options);
  65. // Compatibility: The new UI changed several settings.
  66. if (!empty($options['wildcard']) && !isset($options['exception']['value'])) {
  67. $this->options['exception']['value'] = $options['wildcard'];
  68. }
  69. if (!empty($options['wildcard_substitution']) && !isset($options['exception']['title'])) {
  70. // Enable the checkbox if the title is filled in.
  71. $this->options['exception']['title_enable'] = 1;
  72. $this->options['exception']['title'] = $options['wildcard_substitution'];
  73. }
  74. if (!isset($options['summary']['format']) && !empty($options['style_plugin'])) {
  75. $this->options['summary']['format'] = $options['style_plugin'];
  76. }
  77. // Setup default value.
  78. $options['style_options'] = isset($options['style_options']) ? $options['style_options'] : array();
  79. if (!isset($options['summary']['sort_order']) && !empty($options['default_action']) && $options['default_action'] == 'summary asc') {
  80. $this->options['default_action'] = 'summary';
  81. $this->options['summary']['sort_order'] = 'asc';
  82. $this->options['summary']['number_of_records'] = 0;
  83. $this->options['summary_options'] = $options['style_options'];
  84. }
  85. elseif (!isset($options['summary']['sort_order']) && !empty($options['default_action']) && $options['default_action'] == 'summary desc') {
  86. $this->options['default_action'] = 'summary';
  87. $this->options['summary']['sort_order'] = 'desc';
  88. $this->options['summary']['number_of_records'] = 0;
  89. $this->options['summary_options'] = $options['style_options'];
  90. }
  91. elseif (!isset($options['summary']['sort_order']) && !empty($options['default_action']) && $options['default_action'] == 'summary asc by count') {
  92. $this->options['default_action'] = 'summary';
  93. $this->options['summary']['sort_order'] = 'asc';
  94. $this->options['summary']['number_of_records'] = 1;
  95. $this->options['summary_options'] = $options['style_options'];
  96. }
  97. elseif (!isset($options['summary']['sort_order']) && !empty($options['default_action']) && $options['default_action'] == 'summary desc by count') {
  98. $this->options['default_action'] = 'summary';
  99. $this->options['summary']['sort_order'] = 'desc';
  100. $this->options['summary']['number_of_records'] = 1;
  101. $this->options['summary_options'] = $options['style_options'];
  102. }
  103. if (!empty($options['title']) && !isset($options['title_enable'])) {
  104. $this->options['title_enable'] = 1;
  105. }
  106. if (!empty($options['breadcrumb']) && !isset($options['breadcrumb_enable'])) {
  107. $this->options['breadcrumb_enable'] = 1;
  108. }
  109. if (!empty($options['validate_type']) && !isset($options['validate']['type'])) {
  110. $this->options['validate']['type'] = $options['validate_type'];
  111. $this->options['specify_validation'] = 1;
  112. }
  113. if (!empty($options['validate_fail']) && !isset($options['validate']['fail'])) {
  114. $this->options['validate']['fail'] = $options['validate_fail'];
  115. $this->options['specify_validation'] = 1;
  116. }
  117. }
  118. /**
  119. * Give an argument the opportunity to modify the breadcrumb, if it wants.
  120. * This only gets called on displays where a breadcrumb is actually used.
  121. *
  122. * The breadcrumb will be in the form of an array, with the keys being
  123. * the path and the value being the already sanitized title of the path.
  124. */
  125. function set_breadcrumb(&$breadcrumb) { }
  126. /**
  127. * Determine if the argument can generate a breadcrumb
  128. *
  129. * @return TRUE/FALSE
  130. */
  131. function uses_breadcrumb() {
  132. $info = $this->default_actions($this->options['default_action']);
  133. return !empty($info['breadcrumb']);
  134. }
  135. function is_exception($arg = NULL) {
  136. if (!isset($arg)) {
  137. $arg = isset($this->argument) ? $this->argument : NULL;
  138. }
  139. return !empty($this->options['exception']['value']) && $this->options['exception']['value'] === $arg;
  140. }
  141. function exception_title() {
  142. // If title overriding is off for the exception, return the normal title.
  143. if (empty($this->options['exception']['title_enable'])) {
  144. return $this->get_title();
  145. }
  146. return $this->options['exception']['title'];
  147. }
  148. /**
  149. * Determine if the argument needs a style plugin.
  150. *
  151. * @return TRUE/FALSE
  152. */
  153. function needs_style_plugin() {
  154. $info = $this->default_actions($this->options['default_action']);
  155. $validate_info = $this->default_actions($this->options['validate']['fail']);
  156. return !empty($info['style plugin']) || !empty($validate_info['style plugin']);
  157. }
  158. function option_definition() {
  159. $options = parent::option_definition();
  160. $options['default_action'] = array('default' => 'ignore');
  161. $options['exception'] = array(
  162. 'contains' => array(
  163. 'value' => array('default' => 'all'),
  164. 'title_enable' => array('default' => FALSE, 'bool' => TRUE),
  165. 'title' => array('default' => 'All', 'translatable' => TRUE),
  166. ),
  167. );
  168. $options['title_enable'] = array('default' => FALSE, 'bool' => TRUE);
  169. $options['title'] = array('default' => '', 'translatable' => TRUE);
  170. $options['breadcrumb_enable'] = array('default' => FALSE, 'bool' => TRUE);
  171. $options['breadcrumb'] = array('default' => '', 'translatable' => TRUE);
  172. $options['default_argument_type'] = array('default' => 'fixed', 'export' => 'export_plugin');
  173. $options['default_argument_options'] = array('default' => array(), 'export' => FALSE);
  174. $options['default_argument_skip_url'] = array('default' => FALSE, 'bool' => TRUE);
  175. $options['summary_options'] = array('default' => array(), 'export' => FALSE);
  176. $options['summary'] = array(
  177. 'contains' => array(
  178. 'sort_order' => array('default' => 'asc'),
  179. 'number_of_records' => array('default' => 0),
  180. 'format' => array('default' => 'default_summary', 'export' => 'export_summary'),
  181. ),
  182. );
  183. $options['specify_validation'] = array('default' => FALSE, 'bool' => TRUE);
  184. $options['validate'] = array(
  185. 'contains' => array(
  186. 'type' => array('default' => 'none', 'export' => 'export_validation'),
  187. 'fail' => array('default' => 'not found'),
  188. ),
  189. );
  190. $options['validate_options'] = array('default' => array(), 'export' => FALSE);
  191. return $options;
  192. }
  193. function options_form(&$form, &$form_state) {
  194. parent::options_form($form, $form_state);
  195. $argument_text = $this->view->display_handler->get_argument_text();
  196. $form['#pre_render'][] = 'views_ui_pre_render_move_argument_options';
  197. $form['description'] = array(
  198. '#markup' => $argument_text['description'],
  199. '#theme_wrappers' => array('container'),
  200. '#attributes' => array('class' => array('description')),
  201. );
  202. $form['no_argument'] = array(
  203. '#type' => 'fieldset',
  204. '#title' => $argument_text['filter value not present'],
  205. );
  206. // Everything in the fieldset is floated, so the last element needs to
  207. // clear those floats.
  208. $form['no_argument']['clearfix'] = array(
  209. '#weight' => 1000,
  210. '#markup' => '<div class="clearfix"></div>',
  211. );
  212. $form['default_action'] = array(
  213. '#type' => 'radios',
  214. '#process' => array('views_ui_process_container_radios'),
  215. '#default_value' => $this->options['default_action'],
  216. '#fieldset' => 'no_argument',
  217. );
  218. $form['exception'] = array(
  219. '#type' => 'fieldset',
  220. '#title' => t('Exceptions'),
  221. '#collapsible' => TRUE,
  222. '#collapsed' => TRUE,
  223. '#fieldset' => 'no_argument',
  224. );
  225. $form['exception']['value'] = array(
  226. '#type' => 'textfield',
  227. '#title' => t('Exception value'),
  228. '#size' => 20,
  229. '#default_value' => $this->options['exception']['value'],
  230. '#description' => t('If this value is received, the filter will be ignored; i.e, "all values"'),
  231. );
  232. $form['exception']['title_enable'] = array(
  233. '#type' => 'checkbox',
  234. '#title' => t('Override title'),
  235. '#default_value' => $this->options['exception']['title_enable'],
  236. );
  237. $form['exception']['title'] = array(
  238. '#type' => 'textfield',
  239. '#title' => t('Override title'),
  240. '#title_display' => 'invisible',
  241. '#size' => 20,
  242. '#default_value' => $this->options['exception']['title'],
  243. '#description' => t('Override the view and other argument titles. Use "%1" for the first argument, "%2" for the second, etc.'),
  244. '#dependency' => array(
  245. 'edit-options-exception-title-enable' => array('1'),
  246. ),
  247. );
  248. $options = array();
  249. $defaults = $this->default_actions();
  250. $validate_options = array();
  251. foreach ($defaults as $id => $info) {
  252. $options[$id] = $info['title'];
  253. if (empty($info['default only'])) {
  254. $validate_options[$id] = $info['title'];
  255. }
  256. if (!empty($info['form method'])) {
  257. $this->{$info['form method']}($form, $form_state);
  258. }
  259. }
  260. $form['default_action']['#options'] = $options;
  261. $form['argument_present'] = array(
  262. '#type' => 'fieldset',
  263. '#title' => $argument_text['filter value present'],
  264. );
  265. $form['title_enable'] = array(
  266. '#type' => 'checkbox',
  267. '#title' => t('Override title'),
  268. '#default_value' => $this->options['title_enable'],
  269. '#fieldset' => 'argument_present',
  270. );
  271. $form['title'] = array(
  272. '#type' => 'textfield',
  273. '#title' => t('Provide title'),
  274. '#title_display' => 'invisible',
  275. '#default_value' => $this->options['title'],
  276. '#description' => t('Override the view and other argument titles. Use "%1" for the first argument, "%2" for the second, etc.'),
  277. '#dependency' => array(
  278. 'edit-options-title-enable' => array('1'),
  279. ),
  280. '#fieldset' => 'argument_present',
  281. );
  282. $form['breadcrumb_enable'] = array(
  283. '#type' => 'checkbox',
  284. '#title' => t('Override breadcrumb'),
  285. '#default_value' => $this->options['breadcrumb_enable'],
  286. '#fieldset' => 'argument_present',
  287. );
  288. $form['breadcrumb'] = array(
  289. '#type' => 'textfield',
  290. '#title' => t('Provide breadcrumb'),
  291. '#title_display' => 'invisible',
  292. '#default_value' => $this->options['breadcrumb'],
  293. '#description' => t('Enter a breadcrumb name you would like to use. See "Title" for percent substitutions.'),
  294. '#dependency' => array(
  295. 'edit-options-breadcrumb-enable' => array('1'),
  296. ),
  297. '#fieldset' => 'argument_present',
  298. );
  299. $form['specify_validation'] = array(
  300. '#type' => 'checkbox',
  301. '#title' => t('Specify validation criteria'),
  302. '#default_value' => $this->options['specify_validation'],
  303. '#fieldset' => 'argument_present',
  304. );
  305. $form['validate'] = array(
  306. '#type' => 'container',
  307. '#fieldset' => 'argument_present',
  308. );
  309. // @todo The mockup wanted to use "Validate using" here, but it doesn't
  310. // work well with many options (they'd need to be changed as well)
  311. $form['validate']['type'] = array(
  312. '#type' => 'select',
  313. '#title' => t('Validator'),
  314. '#default_value' => $this->options['validate']['type'],
  315. '#dependency' => array(
  316. 'edit-options-specify-validation' => array('1'),
  317. ),
  318. );
  319. $validate_types = array('none' => t('- Basic validation -'));
  320. $plugins = views_fetch_plugin_data('argument validator');
  321. foreach ($plugins as $id => $info) {
  322. if (!empty($info['no ui'])) {
  323. continue;
  324. }
  325. $valid = TRUE;
  326. if (!empty($info['type'])) {
  327. $valid = FALSE;
  328. if (empty($this->definition['validate type'])) {
  329. continue;
  330. }
  331. foreach ((array) $info['type'] as $type) {
  332. if ($type == $this->definition['validate type']) {
  333. $valid = TRUE;
  334. break;
  335. }
  336. }
  337. }
  338. // If we decide this validator is ok, add it to the list.
  339. if ($valid) {
  340. $plugin = $this->get_plugin('argument validator', $id);
  341. if ($plugin) {
  342. if ($plugin->access() || $this->options['validate']['type'] == $id) {
  343. $form['validate']['options'][$id] = array(
  344. '#prefix' => '<div id="edit-options-validate-options-' . $id . '-wrapper">',
  345. '#suffix' => '</div>',
  346. '#type' => 'item',
  347. // Even if the plugin has no options add the key to the form_state.
  348. '#input' => TRUE, // trick it into checking input to make #process run
  349. '#dependency' => array(
  350. 'edit-options-specify-validation' => array('1'),
  351. 'edit-options-validate-type' => array($id),
  352. ),
  353. '#dependency_count' => 2,
  354. '#id' => 'edit-options-validate-options-' . $id,
  355. );
  356. $plugin->options_form($form['validate']['options'][$id], $form_state);
  357. $validate_types[$id] = $info['title'];
  358. }
  359. }
  360. }
  361. }
  362. asort($validate_types);
  363. $form['validate']['type']['#options'] = $validate_types;
  364. $form['validate']['fail'] = array(
  365. '#type' => 'select',
  366. '#title' => t('Action to take if filter value does not validate'),
  367. '#default_value' => $this->options['validate']['fail'],
  368. '#options' => $validate_options,
  369. '#dependency' => array(
  370. 'edit-options-specify-validation' => array('1'),
  371. ),
  372. '#fieldset' => 'argument_present',
  373. );
  374. }
  375. function options_validate(&$form, &$form_state) {
  376. if (empty($form_state['values']['options'])) {
  377. return;
  378. }
  379. // Let the plugins do validation.
  380. $default_id = $form_state['values']['options']['default_argument_type'];
  381. $plugin = $this->get_plugin('argument default', $default_id);
  382. if ($plugin) {
  383. $plugin->options_validate($form['argument_default'][$default_id], $form_state, $form_state['values']['options']['argument_default'][$default_id]);
  384. }
  385. // summary plugin
  386. $summary_id = $form_state['values']['options']['summary']['format'];
  387. $plugin = $this->get_plugin('style', $summary_id);
  388. if ($plugin) {
  389. $plugin->options_validate($form['summary']['options'][$summary_id], $form_state, $form_state['values']['options']['summary']['options'][$summary_id]);
  390. }
  391. $validate_id = $form_state['values']['options']['validate']['type'];
  392. $plugin = $this->get_plugin('argument validator', $validate_id);
  393. if ($plugin) {
  394. $plugin->options_validate($form['validate']['options'][$default_id], $form_state, $form_state['values']['options']['validate']['options'][$validate_id]);
  395. }
  396. }
  397. function options_submit(&$form, &$form_state) {
  398. if (empty($form_state['values']['options'])) {
  399. return;
  400. }
  401. // Let the plugins make submit modifications if necessary.
  402. $default_id = $form_state['values']['options']['default_argument_type'];
  403. $plugin = $this->get_plugin('argument default', $default_id);
  404. if ($plugin) {
  405. $options = &$form_state['values']['options']['argument_default'][$default_id];
  406. $plugin->options_submit($form['argument_default'][$default_id], $form_state, $options);
  407. // Copy the now submitted options to their final resting place so they get saved.
  408. $form_state['values']['options']['default_argument_options'] = $options;
  409. }
  410. // summary plugin
  411. $summary_id = $form_state['values']['options']['summary']['format'];
  412. $plugin = $this->get_plugin('style', $summary_id);
  413. if ($plugin) {
  414. $options = &$form_state['values']['options']['summary']['options'][$summary_id];
  415. $plugin->options_submit($form['summary']['options'][$summary_id], $form_state, $options);
  416. // Copy the now submitted options to their final resting place so they get saved.
  417. $form_state['values']['options']['summary_options'] = $options;
  418. }
  419. $validate_id = $form_state['values']['options']['validate']['type'];
  420. $plugin = $this->get_plugin('argument validator', $validate_id);
  421. if ($plugin) {
  422. $options = &$form_state['values']['options']['validate']['options'][$validate_id];
  423. $plugin->options_submit($form['validate']['options'][$validate_id], $form_state, $options);
  424. // Copy the now submitted options to their final resting place so they get saved.
  425. $form_state['values']['options']['validate_options'] = $options;
  426. }
  427. // Clear out the content of title if it's not enabled.
  428. $options =& $form_state['values']['options'];
  429. if (empty($options['title_enable'])) {
  430. $options['title'] = '';
  431. }
  432. }
  433. /**
  434. * Provide a list of default behaviors for this argument if the argument
  435. * is not present.
  436. *
  437. * Override this method to provide additional (or fewer) default behaviors.
  438. */
  439. function default_actions($which = NULL) {
  440. $defaults = array(
  441. 'ignore' => array(
  442. 'title' => t('Display all results for the specified field'),
  443. 'method' => 'default_ignore',
  444. 'breadcrumb' => TRUE, // generate a breadcrumb to here
  445. ),
  446. 'default' => array(
  447. 'title' => t('Provide default value'),
  448. 'method' => 'default_default',
  449. 'form method' => 'default_argument_form',
  450. 'has default argument' => TRUE,
  451. 'default only' => TRUE, // this can only be used for missing argument, not validation failure
  452. 'breadcrumb' => TRUE, // generate a breadcrumb to here
  453. ),
  454. 'not found' => array(
  455. 'title' => t('Hide view'),
  456. 'method' => 'default_not_found',
  457. 'hard fail' => TRUE, // This is a hard fail condition
  458. ),
  459. 'summary' => array(
  460. 'title' => t('Display a summary'),
  461. 'method' => 'default_summary',
  462. 'form method' => 'default_summary_form',
  463. 'style plugin' => TRUE,
  464. 'breadcrumb' => TRUE, // generate a breadcrumb to here
  465. ),
  466. 'empty' => array(
  467. 'title' => t('Display contents of "No results found"'),
  468. 'method' => 'default_empty',
  469. 'breadcrumb' => TRUE, // generate a breadcrumb to here
  470. ),
  471. 'access denied' => array(
  472. 'title' => t('Display "Access Denied"'),
  473. 'method' => 'default_access_denied',
  474. 'breadcrumb' => FALSE, // generate a breadcrumb to here
  475. ),
  476. );
  477. if ($this->view->display_handler->has_path()) {
  478. $defaults['not found']['title'] = t('Show "Page not found"');
  479. }
  480. if ($which) {
  481. if (!empty($defaults[$which])) {
  482. return $defaults[$which];
  483. }
  484. }
  485. else {
  486. return $defaults;
  487. }
  488. }
  489. /**
  490. * Provide a form for selecting the default argument when the
  491. * default action is set to provide default argument.
  492. */
  493. function default_argument_form(&$form, &$form_state) {
  494. $plugins = views_fetch_plugin_data('argument default');
  495. $options = array();
  496. $form['default_argument_skip_url'] = array(
  497. '#type' => 'checkbox',
  498. '#title' => t('Skip default argument for view URL'),
  499. '#default_value' => $this->options['default_argument_skip_url'],
  500. '#description' => t('Select whether to include this default argument when constructing the URL for this view. Skipping default arguments is useful e.g. in the case of feeds.')
  501. );
  502. $form['default_argument_type'] = array(
  503. '#prefix' => '<div id="edit-options-default-argument-type-wrapper">',
  504. '#suffix' => '</div>',
  505. '#type' => 'select',
  506. '#id' => 'edit-options-default-argument-type',
  507. '#title' => t('Type'),
  508. '#default_value' => $this->options['default_argument_type'],
  509. '#dependency' => array('radio:options[default_action]' => array('default')),
  510. // Views custom key, moves this element to the appropriate container
  511. // under the radio button.
  512. '#argument_option' => 'default',
  513. );
  514. foreach ($plugins as $id => $info) {
  515. if (!empty($info['no ui'])) {
  516. continue;
  517. }
  518. $plugin = $this->get_plugin('argument default', $id);
  519. if ($plugin) {
  520. if ($plugin->access() || $this->options['default_argument_type'] == $id) {
  521. $form['argument_default']['#argument_option'] = 'default';
  522. $form['argument_default'][$id] = array(
  523. '#prefix' => '<div id="edit-options-argument-default-options-' . $id . '-wrapper">',
  524. '#suffix' => '</div>',
  525. '#id' => 'edit-options-argument-default-options-' . $id,
  526. '#type' => 'item',
  527. // Even if the plugin has no options add the key to the form_state.
  528. '#input' => TRUE,
  529. '#dependency' => array(
  530. 'radio:options[default_action]' => array('default'),
  531. 'edit-options-default-argument-type' => array($id)
  532. ),
  533. '#dependency_count' => 2,
  534. );
  535. $options[$id] = $info['title'];
  536. $plugin->options_form($form['argument_default'][$id], $form_state);
  537. }
  538. }
  539. }
  540. asort($options);
  541. $form['default_argument_type']['#options'] = $options;
  542. }
  543. /**
  544. * Provide a form for selecting further summary options when the
  545. * default action is set to display one.
  546. */
  547. function default_summary_form(&$form, &$form_state) {
  548. $style_plugins = views_fetch_plugin_data('style');
  549. $summary_plugins = array();
  550. $format_options = array();
  551. foreach ($style_plugins as $key => $plugin) {
  552. if (isset($plugin['type']) && $plugin['type'] == 'summary') {
  553. $summary_plugins[$key] = $plugin;
  554. $format_options[$key] = $plugin['title'];
  555. }
  556. }
  557. $form['summary'] = array(
  558. // Views custom key, moves this element to the appropriate container
  559. // under the radio button.
  560. '#argument_option' => 'summary',
  561. );
  562. $form['summary']['sort_order'] = array(
  563. '#type' => 'radios',
  564. '#title' => t('Sort order'),
  565. '#options' => array('asc' => t('Ascending'), 'desc' => t('Descending')),
  566. '#default_value' => $this->options['summary']['sort_order'],
  567. '#dependency' => array('radio:options[default_action]' => array('summary')),
  568. );
  569. $form['summary']['number_of_records'] = array(
  570. '#type' => 'radios',
  571. '#title' => t('Sort by'),
  572. '#default_value' => $this->options['summary']['number_of_records'],
  573. '#options' => array(
  574. 0 => $this->get_sort_name(),
  575. 1 => t('Number of records')
  576. ),
  577. '#dependency' => array('radio:options[default_action]' => array('summary')),
  578. );
  579. $form['summary']['format'] = array(
  580. '#type' => 'radios',
  581. '#title' => t('Format'),
  582. '#options' => $format_options,
  583. '#default_value' => $this->options['summary']['format'],
  584. '#dependency' => array('radio:options[default_action]' => array('summary')),
  585. );
  586. foreach ($summary_plugins as $id => $info) {
  587. if (empty($info['uses options'])) {
  588. continue;
  589. }
  590. $plugin = $this->get_plugin('style', $id);
  591. if ($plugin) {
  592. $form['summary']['options'][$id] = array(
  593. '#prefix' => '<div id="edit-options-summary-options-' . $id . '-wrapper">',
  594. '#suffix' => '</div>',
  595. '#id' => 'edit-options-summary-options-' . $id,
  596. '#type' => 'item',
  597. '#input' => TRUE, // trick it into checking input to make #process run
  598. '#dependency' => array(
  599. 'radio:options[default_action]' => array('summary'),
  600. 'radio:options[summary][format]' => array($id),
  601. ),
  602. '#dependency_count' => 2,
  603. );
  604. $options[$id] = $info['title'];
  605. $plugin->options_form($form['summary']['options'][$id], $form_state);
  606. }
  607. }
  608. }
  609. /**
  610. * Handle the default action, which means our argument wasn't present.
  611. *
  612. * Override this method only with extreme care.
  613. *
  614. * @return
  615. * A boolean value; if TRUE, continue building this view. If FALSE,
  616. * building the view will be aborted here.
  617. */
  618. function default_action($info = NULL) {
  619. if (!isset($info)) {
  620. $info = $this->default_actions($this->options['default_action']);
  621. }
  622. if (!$info) {
  623. return FALSE;
  624. }
  625. if (!empty($info['method args'])) {
  626. return call_user_func_array(array(&$this, $info['method']), $info['method args']);
  627. }
  628. else {
  629. return $this->{$info['method']}();
  630. }
  631. }
  632. /**
  633. * How to act if validation failes
  634. */
  635. function validate_fail() {
  636. $info = $this->default_actions($this->options['validate']['fail']);
  637. return $this->default_action($info);
  638. }
  639. /**
  640. * Default action: ignore.
  641. *
  642. * If an argument was expected and was not given, in this case, simply
  643. * ignore the argument entirely.
  644. */
  645. function default_ignore() {
  646. return TRUE;
  647. }
  648. /**
  649. * Default action: not found.
  650. *
  651. * If an argument was expected and was not given, in this case, report
  652. * the view as 'not found' or hide it.
  653. */
  654. function default_not_found() {
  655. // Set a failure condition and let the display manager handle it.
  656. $this->view->build_info['fail'] = TRUE;
  657. return FALSE;
  658. }
  659. /**
  660. * Default action: access denied.
  661. *
  662. * If an argument was expected and was not given, in this case, report
  663. * the view as 'access denied'.
  664. */
  665. function default_access_denied() {
  666. $this->view->build_info['denied'] = TRUE;
  667. return FALSE;
  668. }
  669. /**
  670. * Default action: empty
  671. *
  672. * If an argument was expected and was not given, in this case, display
  673. * the view's empty text
  674. */
  675. function default_empty() {
  676. // We return with no query; this will force the empty text.
  677. $this->view->built = TRUE;
  678. $this->view->executed = TRUE;
  679. $this->view->result = array();
  680. return FALSE;
  681. }
  682. /**
  683. * This just returns true. The view argument builder will know where
  684. * to find the argument from.
  685. */
  686. function default_default() {
  687. return TRUE;
  688. }
  689. /**
  690. * Determine if the argument is set to provide a default argument.
  691. */
  692. function has_default_argument() {
  693. $info = $this->default_actions($this->options['default_action']);
  694. return !empty($info['has default argument']);
  695. }
  696. /**
  697. * Get a default argument, if available.
  698. */
  699. function get_default_argument() {
  700. $plugin = $this->get_plugin('argument default');
  701. if ($plugin) {
  702. return $plugin->get_argument();
  703. }
  704. }
  705. /**
  706. * Process the summary arguments for display.
  707. *
  708. * For example, the validation plugin may want to alter an argument for use in
  709. * the URL.
  710. */
  711. function process_summary_arguments(&$args) {
  712. if ($this->options['validate']['type'] != 'none') {
  713. if (isset($this->validator) || $this->validator = $this->get_plugin('argument validator')) {
  714. $this->validator->process_summary_arguments($args);
  715. }
  716. }
  717. }
  718. /**
  719. * Default action: summary.
  720. *
  721. * If an argument was expected and was not given, in this case, display
  722. * a summary query.
  723. */
  724. function default_summary() {
  725. $this->view->build_info['summary'] = TRUE;
  726. $this->view->build_info['summary_level'] = $this->options['id'];
  727. // Change the display style to the summary style for this
  728. // argument.
  729. $this->view->plugin_name = $this->options['summary']['format'];
  730. $this->view->style_options = $this->options['summary_options'];
  731. // Clear out the normal primary field and whatever else may have
  732. // been added and let the summary do the work.
  733. $this->query->clear_fields();
  734. $this->summary_query();
  735. $by = $this->options['summary']['number_of_records'] ? 'num_records' : NULL;
  736. $this->summary_sort($this->options['summary']['sort_order'], $by);
  737. // Summaries have their own sorting and fields, so tell the View not
  738. // to build these.
  739. $this->view->build_sort = $this->view->build_fields = FALSE;
  740. return TRUE;
  741. }
  742. /**
  743. * Build the info for the summary query.
  744. *
  745. * This must:
  746. * - add_groupby: group on this field in order to create summaries.
  747. * - add_field: add a 'num_nodes' field for the count. Usually it will
  748. * be a count on $view->base_field
  749. * - set_count_field: Reset the count field so we get the right paging.
  750. *
  751. * @return
  752. * The alias used to get the number of records (count) for this entry.
  753. */
  754. function summary_query() {
  755. $this->ensure_my_table();
  756. // Add the field.
  757. $this->base_alias = $this->query->add_field($this->table_alias, $this->real_field);
  758. $this->summary_name_field();
  759. return $this->summary_basics();
  760. }
  761. /**
  762. * Add the name field, which is the field displayed in summary queries.
  763. * This is often used when the argument is numeric.
  764. */
  765. function summary_name_field() {
  766. // Add the 'name' field. For example, if this is a uid argument, the
  767. // name field would be 'name' (i.e, the username).
  768. if (isset($this->name_table)) {
  769. // if the alias is different then we're probably added, not ensured,
  770. // so look up the join and add it instead.
  771. if ($this->table_alias != $this->name_table) {
  772. $j = views_get_table_join($this->name_table, $this->table);
  773. if ($j) {
  774. $join = clone $j;
  775. $join->left_table = $this->table_alias;
  776. $this->name_table_alias = $this->query->add_table($this->name_table, $this->relationship, $join);
  777. }
  778. }
  779. else {
  780. $this->name_table_alias = $this->query->ensure_table($this->name_table, $this->relationship);
  781. }
  782. }
  783. else {
  784. $this->name_table_alias = $this->table_alias;
  785. }
  786. if (isset($this->name_field)) {
  787. $this->name_alias = $this->query->add_field($this->name_table_alias, $this->name_field);
  788. }
  789. else {
  790. $this->name_alias = $this->base_alias;
  791. }
  792. }
  793. /**
  794. * Some basic summary behavior that doesn't need to be repeated as much as
  795. * code that goes into summary_query()
  796. */
  797. function summary_basics($count_field = TRUE) {
  798. // Add the number of nodes counter
  799. $distinct = ($this->view->display_handler->get_option('distinct') && empty($this->query->no_distinct));
  800. $count_alias = $this->query->add_field($this->query->base_table, $this->query->base_field, 'num_records',
  801. array('count' => TRUE, 'distinct' => $distinct));
  802. $this->query->add_groupby($this->name_alias);
  803. if ($count_field) {
  804. $this->query->set_count_field($this->table_alias, $this->real_field);
  805. }
  806. $this->count_alias = $count_alias;
  807. }
  808. /**
  809. * Sorts the summary based upon the user's selection. The base variant of
  810. * this is usually adequte.
  811. *
  812. * @param $order
  813. * The order selected in the UI.
  814. */
  815. function summary_sort($order, $by = NULL) {
  816. $this->query->add_orderby(NULL, NULL, $order, (!empty($by) ? $by : $this->name_alias));
  817. }
  818. /**
  819. * Provide the argument to use to link from the summary to the next level;
  820. * this will be called once per row of a summary, and used as part of
  821. * $view->get_url().
  822. *
  823. * @param $data
  824. * The query results for the row.
  825. */
  826. function summary_argument($data) {
  827. return $data->{$this->base_alias};
  828. }
  829. /**
  830. * Provides the name to use for the summary. By default this is just
  831. * the name field.
  832. *
  833. * @param $data
  834. * The query results for the row.
  835. */
  836. function summary_name($data) {
  837. $value = $data->{$this->name_alias};
  838. if (empty($value) && !empty($this->definition['empty field name'])) {
  839. $value = $this->definition['empty field name'];
  840. }
  841. return check_plain($value);
  842. }
  843. /**
  844. * Set up the query for this argument.
  845. *
  846. * The argument sent may be found at $this->argument.
  847. */
  848. function query($group_by = FALSE) {
  849. $this->ensure_my_table();
  850. $this->query->add_where(0, "$this->table_alias.$this->real_field", $this->argument);
  851. }
  852. /**
  853. * Get the title this argument will assign the view, given the argument.
  854. *
  855. * This usually needs to be overridden to provide a proper title.
  856. */
  857. function title() {
  858. return check_plain($this->argument);
  859. }
  860. /**
  861. * Called by the view object to get the title. This may be set by a
  862. * validator so we don't necessarily call through to title().
  863. */
  864. function get_title() {
  865. if (isset($this->validated_title)) {
  866. return $this->validated_title;
  867. }
  868. else {
  869. return $this->title();
  870. }
  871. }
  872. /**
  873. * Validate that this argument works. By default, all arguments are valid.
  874. */
  875. function validate_arg($arg) {
  876. // By using % in URLs, arguments could be validated twice; this eases
  877. // that pain.
  878. if (isset($this->argument_validated)) {
  879. return $this->argument_validated;
  880. }
  881. if ($this->is_exception($arg)) {
  882. return $this->argument_validated = TRUE;
  883. }
  884. if ($this->options['validate']['type'] == 'none') {
  885. return $this->argument_validated = $this->validate_argument_basic($arg);
  886. }
  887. $plugin = $this->get_plugin('argument validator');
  888. if ($plugin) {
  889. return $this->argument_validated = $plugin->validate_argument($arg);
  890. }
  891. // If the plugin isn't found, fall back to the basic validation path:
  892. return $this->argument_validated = $this->validate_argument_basic($arg);
  893. }
  894. /**
  895. * Called by the menu system to validate an argument.
  896. *
  897. * This checks to see if this is a 'soft fail', which means that if the
  898. * argument fails to validate, but there is an action to take anyway,
  899. * then validation cannot actually fail.
  900. */
  901. function validate_argument($arg) {
  902. $validate_info = $this->default_actions($this->options['validate']['fail']);
  903. if (empty($validate_info['hard fail'])) {
  904. return TRUE;
  905. }
  906. $rc = $this->validate_arg($arg);
  907. // If the validator has changed the validate fail condition to a
  908. // soft fail, deal with that:
  909. $validate_info = $this->default_actions($this->options['validate']['fail']);
  910. if (empty($validate_info['hard fail'])) {
  911. return TRUE;
  912. }
  913. return $rc;
  914. }
  915. /**
  916. * Provide a basic argument validation.
  917. *
  918. * This can be overridden for more complex types; the basic
  919. * validator only checks to see if the argument is not NULL
  920. * or is numeric if the definition says it's numeric.
  921. */
  922. function validate_argument_basic($arg) {
  923. if (!isset($arg) || $arg === '') {
  924. return FALSE;
  925. }
  926. if (!empty($this->definition['numeric']) && !isset($this->options['break_phrase']) && !is_numeric($arg)) {
  927. return FALSE;
  928. }
  929. return TRUE;
  930. }
  931. /**
  932. * Set the input for this argument
  933. *
  934. * @return TRUE if it successfully validates; FALSE if it does not.
  935. */
  936. function set_argument($arg) {
  937. $this->argument = $arg;
  938. return $this->validate_arg($arg);
  939. }
  940. /**
  941. * Get the value of this argument.
  942. */
  943. function get_value() {
  944. // If we already processed this argument, we're done.
  945. if (isset($this->argument)) {
  946. return $this->argument;
  947. }
  948. // Otherwise, we have to pretend to process ourself to find the value.
  949. $value = NULL;
  950. // Find the position of this argument within the view.
  951. $position = 0;
  952. foreach ($this->view->argument as $id => $argument) {
  953. if ($id == $this->options['id']) {
  954. break;
  955. }
  956. $position++;
  957. }
  958. $arg = isset($this->view->args[$position]) ? $this->view->args[$position] : NULL;
  959. $this->position = $position;
  960. // Clone ourselves so that we don't break things when we're really
  961. // processing the arguments.
  962. $argument = clone $this;
  963. if (!isset($arg) && $argument->has_default_argument()) {
  964. $arg = $argument->get_default_argument();
  965. // remember that this argument was computed, not passed on the URL.
  966. $this->is_default = TRUE;
  967. }
  968. // Set the argument, which will also validate that the argument can be set.
  969. if ($argument->set_argument($arg)) {
  970. $value = $argument->argument;
  971. }
  972. unset($argument);
  973. return $value;
  974. }
  975. /**
  976. * Export handler for summary export.
  977. *
  978. * Arguments can have styles for the summary view. This special export
  979. * handler makes sure this works properly.
  980. */
  981. function export_summary($indent, $prefix, $storage, $option, $definition, $parents) {
  982. $output = '';
  983. $name = $this->options['summary'][$option];
  984. $options = $this->options['summary_options'];
  985. $plugin = views_get_plugin('style', $name);
  986. if ($plugin) {
  987. $plugin->init($this->view, $this->view->display_handler->display, $options);
  988. // Write which plugin to use.
  989. $output .= $indent . $prefix . "['summary']['$option'] = '$name';\n";
  990. // Pass off to the plugin to export itself.
  991. $output .= $plugin->export_options($indent, $prefix . "['summary_options']");
  992. }
  993. return $output;
  994. }
  995. /**
  996. * Export handler for validation export.
  997. *
  998. * Arguments use validation plugins. This special export handler makes sure
  999. * this works properly.
  1000. */
  1001. function export_validation($indent, $prefix, $storage, $option, $definition, $parents) {
  1002. $output = '';
  1003. $name = $this->options['validate'][$option];
  1004. $options = $this->options['validate_options'];
  1005. $plugin = views_get_plugin('argument validator', $name);
  1006. if ($plugin) {
  1007. $plugin->init($this->view, $this->display, $options);
  1008. // Write which plugin to use.
  1009. $output .= $indent . $prefix . "['validate']['$option'] = '$name';\n";
  1010. // Pass off to the plugin to export itself.
  1011. $output .= $plugin->export_options($indent, $prefix . "['validate_options']");
  1012. }
  1013. return $output;
  1014. }
  1015. /**
  1016. * Generic plugin export handler.
  1017. *
  1018. * Since style and validation plugins have their own export handlers, this
  1019. * one is currently only used for default argument plugins.
  1020. */
  1021. function export_plugin($indent, $prefix, $storage, $option, $definition, $parents) {
  1022. $output = '';
  1023. if ($option == 'default_argument_type') {
  1024. $type = 'argument default';
  1025. $option_name = 'default_argument_options';
  1026. }
  1027. $plugin = $this->get_plugin($type);
  1028. $name = $this->options[$option];
  1029. if ($plugin) {
  1030. // Write which plugin to use.
  1031. $output .= $indent . $prefix . "['$option'] = '$name';\n";
  1032. // Pass off to the plugin to export itself.
  1033. $output .= $plugin->export_options($indent, $prefix . "['$option_name']");
  1034. }
  1035. return $output;
  1036. }
  1037. /**
  1038. * Get the display or row plugin, if it exists.
  1039. */
  1040. function get_plugin($type = 'argument default', $name = NULL) {
  1041. $options = array();
  1042. switch ($type) {
  1043. case 'argument default':
  1044. $plugin_name = $this->options['default_argument_type'];
  1045. $options_name = 'default_argument_options';
  1046. break;
  1047. case 'argument validator':
  1048. $plugin_name = $this->options['validate']['type'];
  1049. $options_name = 'validate_options';
  1050. break;
  1051. case 'style':
  1052. $plugin_name = $this->options['summary']['format'];
  1053. $options_name = 'summary_options';
  1054. }
  1055. if (!$name) {
  1056. $name = $plugin_name;
  1057. }
  1058. // we only fetch the options if we're fetching the plugin actually
  1059. // in use.
  1060. if ($name == $plugin_name) {
  1061. $options = $this->options[$options_name];
  1062. }
  1063. $plugin = views_get_plugin($type, $name);
  1064. if ($plugin) {
  1065. // Style plugins expects different parameters as argument related plugins.
  1066. if ($type == 'style') {
  1067. $plugin->init($this->view, $this->view->display_handler->display, $options);
  1068. }
  1069. else {
  1070. $plugin->init($this->view, $this, $options);
  1071. }
  1072. return $plugin;
  1073. }
  1074. }
  1075. /**
  1076. * Return a description of how the argument would normally be sorted.
  1077. *
  1078. * Subclasses should override this to specify what the default sort order of
  1079. * their argument is (e.g. alphabetical, numeric, date).
  1080. */
  1081. function get_sort_name() {
  1082. return t('Default sort', array(), array('context' => 'Sort order'));
  1083. }
  1084. }
  1085. /**
  1086. * A special handler to take the place of missing or broken handlers.
  1087. *
  1088. * @ingroup views_argument_handlers
  1089. */
  1090. class views_handler_argument_broken extends views_handler_argument {
  1091. function ui_name($short = FALSE) {
  1092. return t('Broken/missing handler');
  1093. }
  1094. function ensure_my_table() { /* No table to ensure! */ }
  1095. function query($group_by = FALSE) { /* No query to run */ }
  1096. function options_form(&$form, &$form_state) {
  1097. $form['markup'] = array(
  1098. '#markup' => '<div class="form-item description">' . t('The handler for this item is broken or missing and cannot be used. If a module provided the handler and was disabled, re-enabling the module may restore it. Otherwise, you should probably delete this item.') . '</div>',
  1099. );
  1100. }
  1101. /**
  1102. * Determine if the handler is considered 'broken'
  1103. */
  1104. function broken() { return TRUE; }
  1105. }
  1106. /**
  1107. * @}
  1108. */