views_handler_argument.inc 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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. // Validate summary plugin options if one is present.
  386. if (isset($form_state['values']['options']['summary']['format'])) {
  387. $summary_id = $form_state['values']['options']['summary']['format'];
  388. $plugin = $this->get_plugin('style', $summary_id);
  389. if ($plugin) {
  390. $plugin->options_validate($form['summary']['options'][$summary_id], $form_state, $form_state['values']['options']['summary']['options'][$summary_id]);
  391. }
  392. }
  393. $validate_id = $form_state['values']['options']['validate']['type'];
  394. $plugin = $this->get_plugin('argument validator', $validate_id);
  395. if ($plugin) {
  396. $plugin->options_validate($form['validate']['options'][$default_id], $form_state, $form_state['values']['options']['validate']['options'][$validate_id]);
  397. }
  398. }
  399. function options_submit(&$form, &$form_state) {
  400. if (empty($form_state['values']['options'])) {
  401. return;
  402. }
  403. // Let the plugins make submit modifications if necessary.
  404. $default_id = $form_state['values']['options']['default_argument_type'];
  405. $plugin = $this->get_plugin('argument default', $default_id);
  406. if ($plugin) {
  407. $options = &$form_state['values']['options']['argument_default'][$default_id];
  408. $plugin->options_submit($form['argument_default'][$default_id], $form_state, $options);
  409. // Copy the now submitted options to their final resting place so they get saved.
  410. $form_state['values']['options']['default_argument_options'] = $options;
  411. }
  412. // Handle summary plugin options if one is present.
  413. if (isset($form_state['values']['options']['summary']['format'])) {
  414. $summary_id = $form_state['values']['options']['summary']['format'];
  415. $plugin = $this->get_plugin('style', $summary_id);
  416. if ($plugin) {
  417. $options = &$form_state['values']['options']['summary']['options'][$summary_id];
  418. $plugin->options_submit($form['summary']['options'][$summary_id], $form_state, $options);
  419. // Copy the now submitted options to their final resting place so they get saved.
  420. $form_state['values']['options']['summary_options'] = $options;
  421. }
  422. }
  423. $validate_id = $form_state['values']['options']['validate']['type'];
  424. $plugin = $this->get_plugin('argument validator', $validate_id);
  425. if ($plugin) {
  426. $options = &$form_state['values']['options']['validate']['options'][$validate_id];
  427. $plugin->options_submit($form['validate']['options'][$validate_id], $form_state, $options);
  428. // Copy the now submitted options to their final resting place so they get saved.
  429. $form_state['values']['options']['validate_options'] = $options;
  430. }
  431. // Clear out the content of title if it's not enabled.
  432. $options =& $form_state['values']['options'];
  433. if (empty($options['title_enable'])) {
  434. $options['title'] = '';
  435. }
  436. }
  437. /**
  438. * Provide a list of default behaviors for this argument if the argument
  439. * is not present.
  440. *
  441. * Override this method to provide additional (or fewer) default behaviors.
  442. */
  443. function default_actions($which = NULL) {
  444. $defaults = array(
  445. 'ignore' => array(
  446. 'title' => t('Display all results for the specified field'),
  447. 'method' => 'default_ignore',
  448. 'breadcrumb' => TRUE, // generate a breadcrumb to here
  449. ),
  450. 'default' => array(
  451. 'title' => t('Provide default value'),
  452. 'method' => 'default_default',
  453. 'form method' => 'default_argument_form',
  454. 'has default argument' => TRUE,
  455. 'default only' => TRUE, // this can only be used for missing argument, not validation failure
  456. 'breadcrumb' => TRUE, // generate a breadcrumb to here
  457. ),
  458. 'not found' => array(
  459. 'title' => t('Hide view'),
  460. 'method' => 'default_not_found',
  461. 'hard fail' => TRUE, // This is a hard fail condition
  462. ),
  463. 'summary' => array(
  464. 'title' => t('Display a summary'),
  465. 'method' => 'default_summary',
  466. 'form method' => 'default_summary_form',
  467. 'style plugin' => TRUE,
  468. 'breadcrumb' => TRUE, // generate a breadcrumb to here
  469. ),
  470. 'empty' => array(
  471. 'title' => t('Display contents of "No results found"'),
  472. 'method' => 'default_empty',
  473. 'breadcrumb' => TRUE, // generate a breadcrumb to here
  474. ),
  475. 'access denied' => array(
  476. 'title' => t('Display "Access Denied"'),
  477. 'method' => 'default_access_denied',
  478. 'breadcrumb' => FALSE, // generate a breadcrumb to here
  479. ),
  480. );
  481. if ($this->view->display_handler->has_path()) {
  482. $defaults['not found']['title'] = t('Show "Page not found"');
  483. }
  484. if ($which) {
  485. if (!empty($defaults[$which])) {
  486. return $defaults[$which];
  487. }
  488. }
  489. else {
  490. return $defaults;
  491. }
  492. }
  493. /**
  494. * Provide a form for selecting the default argument when the
  495. * default action is set to provide default argument.
  496. */
  497. function default_argument_form(&$form, &$form_state) {
  498. $plugins = views_fetch_plugin_data('argument default');
  499. $options = array();
  500. $form['default_argument_skip_url'] = array(
  501. '#type' => 'checkbox',
  502. '#title' => t('Skip default argument for view URL'),
  503. '#default_value' => $this->options['default_argument_skip_url'],
  504. '#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.')
  505. );
  506. $form['default_argument_type'] = array(
  507. '#prefix' => '<div id="edit-options-default-argument-type-wrapper">',
  508. '#suffix' => '</div>',
  509. '#type' => 'select',
  510. '#id' => 'edit-options-default-argument-type',
  511. '#title' => t('Type'),
  512. '#default_value' => $this->options['default_argument_type'],
  513. '#dependency' => array('radio:options[default_action]' => array('default')),
  514. // Views custom key, moves this element to the appropriate container
  515. // under the radio button.
  516. '#argument_option' => 'default',
  517. );
  518. foreach ($plugins as $id => $info) {
  519. if (!empty($info['no ui'])) {
  520. continue;
  521. }
  522. $plugin = $this->get_plugin('argument default', $id);
  523. if ($plugin) {
  524. if ($plugin->access() || $this->options['default_argument_type'] == $id) {
  525. $form['argument_default']['#argument_option'] = 'default';
  526. $form['argument_default'][$id] = array(
  527. '#prefix' => '<div id="edit-options-argument-default-options-' . $id . '-wrapper">',
  528. '#suffix' => '</div>',
  529. '#id' => 'edit-options-argument-default-options-' . $id,
  530. '#type' => 'item',
  531. // Even if the plugin has no options add the key to the form_state.
  532. '#input' => TRUE,
  533. '#dependency' => array(
  534. 'radio:options[default_action]' => array('default'),
  535. 'edit-options-default-argument-type' => array($id)
  536. ),
  537. '#dependency_count' => 2,
  538. );
  539. $options[$id] = $info['title'];
  540. $plugin->options_form($form['argument_default'][$id], $form_state);
  541. }
  542. }
  543. }
  544. asort($options);
  545. $form['default_argument_type']['#options'] = $options;
  546. }
  547. /**
  548. * Provide a form for selecting further summary options when the
  549. * default action is set to display one.
  550. */
  551. function default_summary_form(&$form, &$form_state) {
  552. $style_plugins = views_fetch_plugin_data('style');
  553. $summary_plugins = array();
  554. $format_options = array();
  555. foreach ($style_plugins as $key => $plugin) {
  556. if (isset($plugin['type']) && $plugin['type'] == 'summary') {
  557. $summary_plugins[$key] = $plugin;
  558. $format_options[$key] = $plugin['title'];
  559. }
  560. }
  561. $form['summary'] = array(
  562. // Views custom key, moves this element to the appropriate container
  563. // under the radio button.
  564. '#argument_option' => 'summary',
  565. );
  566. $form['summary']['sort_order'] = array(
  567. '#type' => 'radios',
  568. '#title' => t('Sort order'),
  569. '#options' => array('asc' => t('Ascending'), 'desc' => t('Descending')),
  570. '#default_value' => $this->options['summary']['sort_order'],
  571. '#dependency' => array('radio:options[default_action]' => array('summary')),
  572. );
  573. $form['summary']['number_of_records'] = array(
  574. '#type' => 'radios',
  575. '#title' => t('Sort by'),
  576. '#default_value' => $this->options['summary']['number_of_records'],
  577. '#options' => array(
  578. 0 => $this->get_sort_name(),
  579. 1 => t('Number of records')
  580. ),
  581. '#dependency' => array('radio:options[default_action]' => array('summary')),
  582. );
  583. $form['summary']['format'] = array(
  584. '#type' => 'radios',
  585. '#title' => t('Format'),
  586. '#options' => $format_options,
  587. '#default_value' => $this->options['summary']['format'],
  588. '#dependency' => array('radio:options[default_action]' => array('summary')),
  589. );
  590. foreach ($summary_plugins as $id => $info) {
  591. if (empty($info['uses options'])) {
  592. continue;
  593. }
  594. $plugin = $this->get_plugin('style', $id);
  595. if ($plugin) {
  596. $form['summary']['options'][$id] = array(
  597. '#prefix' => '<div id="edit-options-summary-options-' . $id . '-wrapper">',
  598. '#suffix' => '</div>',
  599. '#id' => 'edit-options-summary-options-' . $id,
  600. '#type' => 'item',
  601. '#input' => TRUE, // trick it into checking input to make #process run
  602. '#dependency' => array(
  603. 'radio:options[default_action]' => array('summary'),
  604. 'radio:options[summary][format]' => array($id),
  605. ),
  606. '#dependency_count' => 2,
  607. );
  608. $options[$id] = $info['title'];
  609. $plugin->options_form($form['summary']['options'][$id], $form_state);
  610. }
  611. }
  612. }
  613. /**
  614. * Handle the default action, which means our argument wasn't present.
  615. *
  616. * Override this method only with extreme care.
  617. *
  618. * @return
  619. * A boolean value; if TRUE, continue building this view. If FALSE,
  620. * building the view will be aborted here.
  621. */
  622. function default_action($info = NULL) {
  623. if (!isset($info)) {
  624. $info = $this->default_actions($this->options['default_action']);
  625. }
  626. if (!$info) {
  627. return FALSE;
  628. }
  629. if (!empty($info['method args'])) {
  630. return call_user_func_array(array(&$this, $info['method']), $info['method args']);
  631. }
  632. else {
  633. return $this->{$info['method']}();
  634. }
  635. }
  636. /**
  637. * How to act if validation failes
  638. */
  639. function validate_fail() {
  640. $info = $this->default_actions($this->options['validate']['fail']);
  641. return $this->default_action($info);
  642. }
  643. /**
  644. * Default action: ignore.
  645. *
  646. * If an argument was expected and was not given, in this case, simply
  647. * ignore the argument entirely.
  648. */
  649. function default_ignore() {
  650. return TRUE;
  651. }
  652. /**
  653. * Default action: not found.
  654. *
  655. * If an argument was expected and was not given, in this case, report
  656. * the view as 'not found' or hide it.
  657. */
  658. function default_not_found() {
  659. // Set a failure condition and let the display manager handle it.
  660. $this->view->build_info['fail'] = TRUE;
  661. return FALSE;
  662. }
  663. /**
  664. * Default action: access denied.
  665. *
  666. * If an argument was expected and was not given, in this case, report
  667. * the view as 'access denied'.
  668. */
  669. function default_access_denied() {
  670. $this->view->build_info['denied'] = TRUE;
  671. return FALSE;
  672. }
  673. /**
  674. * Default action: empty
  675. *
  676. * If an argument was expected and was not given, in this case, display
  677. * the view's empty text
  678. */
  679. function default_empty() {
  680. // We return with no query; this will force the empty text.
  681. $this->view->built = TRUE;
  682. $this->view->executed = TRUE;
  683. $this->view->result = array();
  684. return FALSE;
  685. }
  686. /**
  687. * This just returns true. The view argument builder will know where
  688. * to find the argument from.
  689. */
  690. function default_default() {
  691. return TRUE;
  692. }
  693. /**
  694. * Determine if the argument is set to provide a default argument.
  695. */
  696. function has_default_argument() {
  697. $info = $this->default_actions($this->options['default_action']);
  698. return !empty($info['has default argument']);
  699. }
  700. /**
  701. * Get a default argument, if available.
  702. */
  703. function get_default_argument() {
  704. $plugin = $this->get_plugin('argument default');
  705. if ($plugin) {
  706. return $plugin->get_argument();
  707. }
  708. }
  709. /**
  710. * Process the summary arguments for display.
  711. *
  712. * For example, the validation plugin may want to alter an argument for use in
  713. * the URL.
  714. */
  715. function process_summary_arguments(&$args) {
  716. if ($this->options['validate']['type'] != 'none') {
  717. if (isset($this->validator) || $this->validator = $this->get_plugin('argument validator')) {
  718. $this->validator->process_summary_arguments($args);
  719. }
  720. }
  721. }
  722. /**
  723. * Default action: summary.
  724. *
  725. * If an argument was expected and was not given, in this case, display
  726. * a summary query.
  727. */
  728. function default_summary() {
  729. $this->view->build_info['summary'] = TRUE;
  730. $this->view->build_info['summary_level'] = $this->options['id'];
  731. // Change the display style to the summary style for this
  732. // argument.
  733. $this->view->plugin_name = $this->options['summary']['format'];
  734. $this->view->style_options = $this->options['summary_options'];
  735. // Clear out the normal primary field and whatever else may have
  736. // been added and let the summary do the work.
  737. $this->query->clear_fields();
  738. $this->summary_query();
  739. $by = $this->options['summary']['number_of_records'] ? 'num_records' : NULL;
  740. $this->summary_sort($this->options['summary']['sort_order'], $by);
  741. // Summaries have their own sorting and fields, so tell the View not
  742. // to build these.
  743. $this->view->build_sort = $this->view->build_fields = FALSE;
  744. return TRUE;
  745. }
  746. /**
  747. * Build the info for the summary query.
  748. *
  749. * This must:
  750. * - add_groupby: group on this field in order to create summaries.
  751. * - add_field: add a 'num_nodes' field for the count. Usually it will
  752. * be a count on $view->base_field
  753. * - set_count_field: Reset the count field so we get the right paging.
  754. *
  755. * @return
  756. * The alias used to get the number of records (count) for this entry.
  757. */
  758. function summary_query() {
  759. $this->ensure_my_table();
  760. // Add the field.
  761. $this->base_alias = $this->query->add_field($this->table_alias, $this->real_field);
  762. $this->summary_name_field();
  763. return $this->summary_basics();
  764. }
  765. /**
  766. * Add the name field, which is the field displayed in summary queries.
  767. * This is often used when the argument is numeric.
  768. */
  769. function summary_name_field() {
  770. // Add the 'name' field. For example, if this is a uid argument, the
  771. // name field would be 'name' (i.e, the username).
  772. if (isset($this->name_table)) {
  773. // if the alias is different then we're probably added, not ensured,
  774. // so look up the join and add it instead.
  775. if ($this->table_alias != $this->name_table) {
  776. $j = views_get_table_join($this->name_table, $this->table);
  777. if ($j) {
  778. $join = clone $j;
  779. $join->left_table = $this->table_alias;
  780. $this->name_table_alias = $this->query->add_table($this->name_table, $this->relationship, $join);
  781. }
  782. }
  783. else {
  784. $this->name_table_alias = $this->query->ensure_table($this->name_table, $this->relationship);
  785. }
  786. }
  787. else {
  788. $this->name_table_alias = $this->table_alias;
  789. }
  790. if (isset($this->name_field)) {
  791. $this->name_alias = $this->query->add_field($this->name_table_alias, $this->name_field);
  792. }
  793. else {
  794. $this->name_alias = $this->base_alias;
  795. }
  796. }
  797. /**
  798. * Some basic summary behavior that doesn't need to be repeated as much as
  799. * code that goes into summary_query()
  800. */
  801. function summary_basics($count_field = TRUE) {
  802. // Add the number of nodes counter
  803. $distinct = ($this->view->display_handler->get_option('distinct') && empty($this->query->no_distinct));
  804. $count_alias = $this->query->add_field($this->query->base_table, $this->query->base_field, 'num_records',
  805. array('count' => TRUE, 'distinct' => $distinct));
  806. $this->query->add_groupby($this->name_alias);
  807. if ($count_field) {
  808. $this->query->set_count_field($this->table_alias, $this->real_field);
  809. }
  810. $this->count_alias = $count_alias;
  811. }
  812. /**
  813. * Sorts the summary based upon the user's selection. The base variant of
  814. * this is usually adequte.
  815. *
  816. * @param $order
  817. * The order selected in the UI.
  818. */
  819. function summary_sort($order, $by = NULL) {
  820. $this->query->add_orderby(NULL, NULL, $order, (!empty($by) ? $by : $this->name_alias));
  821. }
  822. /**
  823. * Provide the argument to use to link from the summary to the next level;
  824. * this will be called once per row of a summary, and used as part of
  825. * $view->get_url().
  826. *
  827. * @param $data
  828. * The query results for the row.
  829. */
  830. function summary_argument($data) {
  831. return $data->{$this->base_alias};
  832. }
  833. /**
  834. * Provides the name to use for the summary. By default this is just
  835. * the name field.
  836. *
  837. * @param $data
  838. * The query results for the row.
  839. */
  840. function summary_name($data) {
  841. $value = $data->{$this->name_alias};
  842. if (empty($value) && !empty($this->definition['empty field name'])) {
  843. $value = $this->definition['empty field name'];
  844. }
  845. return check_plain($value);
  846. }
  847. /**
  848. * Set up the query for this argument.
  849. *
  850. * The argument sent may be found at $this->argument.
  851. */
  852. function query($group_by = FALSE) {
  853. $this->ensure_my_table();
  854. $this->query->add_where(0, "$this->table_alias.$this->real_field", $this->argument);
  855. }
  856. /**
  857. * Get the title this argument will assign the view, given the argument.
  858. *
  859. * This usually needs to be overridden to provide a proper title.
  860. */
  861. function title() {
  862. return check_plain($this->argument);
  863. }
  864. /**
  865. * Called by the view object to get the title. This may be set by a
  866. * validator so we don't necessarily call through to title().
  867. */
  868. function get_title() {
  869. if (isset($this->validated_title)) {
  870. return $this->validated_title;
  871. }
  872. else {
  873. return $this->title();
  874. }
  875. }
  876. /**
  877. * Validate that this argument works. By default, all arguments are valid.
  878. */
  879. function validate_arg($arg) {
  880. // By using % in URLs, arguments could be validated twice; this eases
  881. // that pain.
  882. if (isset($this->argument_validated)) {
  883. return $this->argument_validated;
  884. }
  885. if ($this->is_exception($arg)) {
  886. return $this->argument_validated = TRUE;
  887. }
  888. if ($this->options['validate']['type'] == 'none') {
  889. return $this->argument_validated = $this->validate_argument_basic($arg);
  890. }
  891. $plugin = $this->get_plugin('argument validator');
  892. if ($plugin) {
  893. return $this->argument_validated = $plugin->validate_argument($arg);
  894. }
  895. // If the plugin isn't found, fall back to the basic validation path:
  896. return $this->argument_validated = $this->validate_argument_basic($arg);
  897. }
  898. /**
  899. * Called by the menu system to validate an argument.
  900. *
  901. * This checks to see if this is a 'soft fail', which means that if the
  902. * argument fails to validate, but there is an action to take anyway,
  903. * then validation cannot actually fail.
  904. */
  905. function validate_argument($arg) {
  906. $validate_info = $this->default_actions($this->options['validate']['fail']);
  907. if (empty($validate_info['hard fail'])) {
  908. return TRUE;
  909. }
  910. $rc = $this->validate_arg($arg);
  911. // If the validator has changed the validate fail condition to a
  912. // soft fail, deal with that:
  913. $validate_info = $this->default_actions($this->options['validate']['fail']);
  914. if (empty($validate_info['hard fail'])) {
  915. return TRUE;
  916. }
  917. return $rc;
  918. }
  919. /**
  920. * Provide a basic argument validation.
  921. *
  922. * This can be overridden for more complex types; the basic
  923. * validator only checks to see if the argument is not NULL
  924. * or is numeric if the definition says it's numeric.
  925. */
  926. function validate_argument_basic($arg) {
  927. if (!isset($arg) || $arg === '') {
  928. return FALSE;
  929. }
  930. if (!empty($this->definition['numeric']) && !isset($this->options['break_phrase']) && !is_numeric($arg)) {
  931. return FALSE;
  932. }
  933. return TRUE;
  934. }
  935. /**
  936. * Set the input for this argument
  937. *
  938. * @return TRUE if it successfully validates; FALSE if it does not.
  939. */
  940. function set_argument($arg) {
  941. $this->argument = $arg;
  942. return $this->validate_arg($arg);
  943. }
  944. /**
  945. * Get the value of this argument.
  946. */
  947. function get_value() {
  948. // If we already processed this argument, we're done.
  949. if (isset($this->argument)) {
  950. return $this->argument;
  951. }
  952. // Otherwise, we have to pretend to process ourself to find the value.
  953. $value = NULL;
  954. // Find the position of this argument within the view.
  955. $position = 0;
  956. foreach ($this->view->argument as $id => $argument) {
  957. if ($id == $this->options['id']) {
  958. break;
  959. }
  960. $position++;
  961. }
  962. $arg = isset($this->view->args[$position]) ? $this->view->args[$position] : NULL;
  963. $this->position = $position;
  964. // Clone ourselves so that we don't break things when we're really
  965. // processing the arguments.
  966. $argument = clone $this;
  967. if (!isset($arg) && $argument->has_default_argument()) {
  968. $arg = $argument->get_default_argument();
  969. // remember that this argument was computed, not passed on the URL.
  970. $this->is_default = TRUE;
  971. }
  972. // Set the argument, which will also validate that the argument can be set.
  973. if ($argument->set_argument($arg)) {
  974. $value = $argument->argument;
  975. }
  976. unset($argument);
  977. return $value;
  978. }
  979. /**
  980. * Export handler for summary export.
  981. *
  982. * Arguments can have styles for the summary view. This special export
  983. * handler makes sure this works properly.
  984. */
  985. function export_summary($indent, $prefix, $storage, $option, $definition, $parents) {
  986. $output = '';
  987. $name = $this->options['summary'][$option];
  988. $options = $this->options['summary_options'];
  989. $plugin = views_get_plugin('style', $name);
  990. if ($plugin) {
  991. $plugin->init($this->view, $this->view->display_handler->display, $options);
  992. // Write which plugin to use.
  993. $output .= $indent . $prefix . "['summary']['$option'] = '$name';\n";
  994. // Pass off to the plugin to export itself.
  995. $output .= $plugin->export_options($indent, $prefix . "['summary_options']");
  996. }
  997. return $output;
  998. }
  999. /**
  1000. * Export handler for validation export.
  1001. *
  1002. * Arguments use validation plugins. This special export handler makes sure
  1003. * this works properly.
  1004. */
  1005. function export_validation($indent, $prefix, $storage, $option, $definition, $parents) {
  1006. $output = '';
  1007. $name = $this->options['validate'][$option];
  1008. $options = $this->options['validate_options'];
  1009. $plugin = views_get_plugin('argument validator', $name);
  1010. if ($plugin) {
  1011. $plugin->init($this->view, $this->display, $options);
  1012. // Write which plugin to use.
  1013. $output .= $indent . $prefix . "['validate']['$option'] = '$name';\n";
  1014. // Pass off to the plugin to export itself.
  1015. $output .= $plugin->export_options($indent, $prefix . "['validate_options']");
  1016. }
  1017. return $output;
  1018. }
  1019. /**
  1020. * Generic plugin export handler.
  1021. *
  1022. * Since style and validation plugins have their own export handlers, this
  1023. * one is currently only used for default argument plugins.
  1024. */
  1025. function export_plugin($indent, $prefix, $storage, $option, $definition, $parents) {
  1026. $output = '';
  1027. if ($option == 'default_argument_type') {
  1028. $type = 'argument default';
  1029. $option_name = 'default_argument_options';
  1030. }
  1031. $plugin = $this->get_plugin($type);
  1032. $name = $this->options[$option];
  1033. if ($plugin) {
  1034. // Write which plugin to use.
  1035. $output .= $indent . $prefix . "['$option'] = '$name';\n";
  1036. // Pass off to the plugin to export itself.
  1037. $output .= $plugin->export_options($indent, $prefix . "['$option_name']");
  1038. }
  1039. return $output;
  1040. }
  1041. /**
  1042. * Get the display or row plugin, if it exists.
  1043. */
  1044. function get_plugin($type = 'argument default', $name = NULL) {
  1045. $options = array();
  1046. switch ($type) {
  1047. case 'argument default':
  1048. $plugin_name = $this->options['default_argument_type'];
  1049. $options_name = 'default_argument_options';
  1050. break;
  1051. case 'argument validator':
  1052. $plugin_name = $this->options['validate']['type'];
  1053. $options_name = 'validate_options';
  1054. break;
  1055. case 'style':
  1056. $plugin_name = $this->options['summary']['format'];
  1057. $options_name = 'summary_options';
  1058. }
  1059. if (!$name) {
  1060. $name = $plugin_name;
  1061. }
  1062. // we only fetch the options if we're fetching the plugin actually
  1063. // in use.
  1064. if ($name == $plugin_name) {
  1065. $options = $this->options[$options_name];
  1066. }
  1067. $plugin = views_get_plugin($type, $name);
  1068. if ($plugin) {
  1069. // Style plugins expects different parameters as argument related plugins.
  1070. if ($type == 'style') {
  1071. $plugin->init($this->view, $this->view->display_handler->display, $options);
  1072. }
  1073. else {
  1074. $plugin->init($this->view, $this, $options);
  1075. }
  1076. return $plugin;
  1077. }
  1078. }
  1079. /**
  1080. * Return a description of how the argument would normally be sorted.
  1081. *
  1082. * Subclasses should override this to specify what the default sort order of
  1083. * their argument is (e.g. alphabetical, numeric, date).
  1084. */
  1085. function get_sort_name() {
  1086. return t('Default sort', array(), array('context' => 'Sort order'));
  1087. }
  1088. }
  1089. /**
  1090. * A special handler to take the place of missing or broken handlers.
  1091. *
  1092. * @ingroup views_argument_handlers
  1093. */
  1094. class views_handler_argument_broken extends views_handler_argument {
  1095. function ui_name($short = FALSE) {
  1096. return t('Broken/missing handler');
  1097. }
  1098. function ensure_my_table() { /* No table to ensure! */ }
  1099. function query($group_by = FALSE) { /* No query to run */ }
  1100. function options_form(&$form, &$form_state) {
  1101. $form['markup'] = array(
  1102. '#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>',
  1103. );
  1104. }
  1105. /**
  1106. * Determine if the handler is considered 'broken'
  1107. */
  1108. function broken() { return TRUE; }
  1109. }
  1110. /**
  1111. * @}
  1112. */