views_handler_argument.inc 43 KB

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