views_handler_filter.inc 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  1. <?php
  2. /**
  3. * @file
  4. * Definitions of views_handler_filter and views_handler_filter_broken.
  5. */
  6. /**
  7. * @defgroup views_filter_handlers Views filter handlers
  8. * @{
  9. * Handlers to tell Views how to filter queries.
  10. *
  11. * Definition items:
  12. * - allow empty: If true, the 'IS NULL' and 'IS NOT NULL' operators become
  13. * available as standard operators.
  14. *
  15. * Object flags:
  16. * You can set some specific behavior by setting up the following flags on
  17. * your custom class.
  18. *
  19. * - always_multiple:
  20. * Disable the possibility to force a single value.
  21. * - no_operator:
  22. * Disable the possibility to use operators.
  23. * - always_required:
  24. * Disable the possibility to allow a exposed input to be optional.
  25. */
  26. /**
  27. * Base class for filters.
  28. *
  29. * @ingroup views_filter_handlers
  30. */
  31. class views_handler_filter extends views_handler {
  32. /**
  33. * Contains the actual value of the field,either configured in the views ui
  34. * or entered in the exposed filters.
  35. *
  36. * @var mixed
  37. */
  38. public $value = NULL;
  39. /**
  40. * Contains the operator which is used on the query.
  41. *
  42. * @var string
  43. */
  44. public $operator = '=';
  45. /**
  46. * Contains the information of the selected item in a gruped filter.
  47. *
  48. * @var array|null
  49. */
  50. public $group_info = NULL;
  51. /**
  52. * Disable the possibility to force a single value.
  53. *
  54. * @var bool
  55. */
  56. public $always_multiple = FALSE;
  57. /**
  58. * Disable the possibility to use operators.
  59. *
  60. * @var bool
  61. */
  62. public $no_operator = FALSE;
  63. /**
  64. * Disable the possibility to allow a exposed input to be optional.
  65. *
  66. * @var bool
  67. */
  68. public $always_required = FALSE;
  69. /**
  70. * Provide some extra help to get the operator/value easier to use.
  71. *
  72. * This likely has to be overridden by filters which are more complex
  73. * than simple operator/value.
  74. */
  75. public function init(&$view, &$options) {
  76. parent::init($view, $options);
  77. $this->operator = $this->options['operator'];
  78. $this->value = $this->options['value'];
  79. $this->group_info = $this->options['group_info']['default_group'];
  80. // Compatibility: The new UI changed several settings.
  81. if (!empty($options['exposed']) && !empty($options['expose']['optional']) && !isset($options['expose']['required'])) {
  82. $this->options['expose']['required'] = !$options['expose']['optional'];
  83. }
  84. if (!empty($options['exposed']) && !empty($options['expose']['single']) && !isset($options['expose']['multiple'])) {
  85. $this->options['expose']['multiple'] = !$options['expose']['single'];
  86. }
  87. if (!empty($options['exposed']) && !empty($options['expose']['operator']) && !isset($options['expose']['operator_id'])) {
  88. $this->options['expose']['operator_id'] = $options['expose']['operator_id'] = $options['expose']['operator'];
  89. }
  90. if ($this->multiple_exposed_input()) {
  91. $this->group_info = NULL;
  92. if (!empty($options['group_info']['default_group_multiple'])) {
  93. $this->group_info = array_filter($options['group_info']['default_group_multiple']);
  94. }
  95. $this->options['expose']['multiple'] = TRUE;
  96. }
  97. // If there are relationships in the view, allow empty should be true
  98. // so that we can do IS NULL checks on items. Not all filters respect
  99. // allow empty, but string and numeric do and that covers enough.
  100. if ($this->view->display_handler->get_option('relationships')) {
  101. $this->definition['allow empty'] = TRUE;
  102. }
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function option_definition() {
  108. $options = parent::option_definition();
  109. $options['operator'] = array('default' => '=');
  110. $options['value'] = array('default' => '');
  111. $options['group'] = array('default' => '1');
  112. $options['exposed'] = array('default' => FALSE, 'bool' => TRUE);
  113. $options['expose'] = array(
  114. 'contains' => array(
  115. 'operator_id' => array('default' => FALSE),
  116. 'label' => array('default' => '', 'translatable' => TRUE),
  117. 'description' => array('default' => '', 'translatable' => TRUE),
  118. 'use_operator' => array('default' => FALSE, 'bool' => TRUE),
  119. 'operator_label' => array('default' => '', 'translatable' => TRUE),
  120. 'operator' => array('default' => ''),
  121. 'identifier' => array('default' => ''),
  122. 'required' => array('default' => FALSE, 'bool' => TRUE),
  123. 'remember' => array('default' => FALSE, 'bool' => TRUE),
  124. 'multiple' => array('default' => FALSE, 'bool' => TRUE),
  125. 'remember_roles' => array('default' => array(
  126. DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
  127. )),
  128. ),
  129. );
  130. // A group is a combination of a filter, an operator and a value
  131. // operating like a single filter.
  132. // Users can choose from a select box which group they want to apply.
  133. // Views will filter the view according to the defined values.
  134. // Because it acts as a standard filter, we have to define
  135. // an identifier and other settings like the widget and the label.
  136. // This settings are saved in another array to allow users to switch
  137. // between a normal filter and a group of filters with a single click.
  138. $options['is_grouped'] = array('default' => FALSE, 'bool' => TRUE);
  139. $options['group_info'] = array(
  140. 'contains' => array(
  141. 'label' => array('default' => '', 'translatable' => TRUE),
  142. 'description' => array('default' => '', 'translatable' => TRUE),
  143. 'identifier' => array('default' => ''),
  144. 'optional' => array('default' => TRUE, 'bool' => TRUE),
  145. 'widget' => array('default' => 'select'),
  146. 'multiple' => array('default' => FALSE, 'bool' => TRUE),
  147. 'remember' => array('default' => 0),
  148. 'default_group' => array('default' => 'All'),
  149. 'default_group_multiple' => array('default' => array()),
  150. 'group_items' => array('default' => array()),
  151. ),
  152. );
  153. return $options;
  154. }
  155. /**
  156. * Display the filter on the administrative summary.
  157. */
  158. public function admin_summary() {
  159. return check_plain((string) $this->operator) . ' ' . check_plain((string) $this->value);
  160. }
  161. /**
  162. * Determine if a filter can be exposed.
  163. */
  164. public function can_expose() {
  165. return TRUE;
  166. }
  167. /**
  168. * Determine if a filter can be converted into a group.
  169. *
  170. * Only exposed filters with operators available can be converted into groups.
  171. */
  172. public function can_build_group() {
  173. return $this->is_exposed() && (count($this->operator_options()) > 0);
  174. }
  175. /**
  176. * Returns TRUE if the exposed filter works like a grouped filter.
  177. */
  178. public function is_a_group() {
  179. return $this->is_exposed() && !empty($this->options['is_grouped']);
  180. }
  181. /**
  182. * Provide the basic form which calls through to subforms.
  183. *
  184. * If overridden, it is best to call through to the parent,
  185. * or to at least make sure all of the functions in this form
  186. * are called.
  187. */
  188. public function options_form(&$form, &$form_state) {
  189. parent::options_form($form, $form_state);
  190. if ($this->can_expose()) {
  191. $this->show_expose_button($form, $form_state);
  192. }
  193. if ($this->can_build_group()) {
  194. $this->show_build_group_button($form, $form_state);
  195. }
  196. $form['clear_markup_start'] = array(
  197. '#markup' => '<div class="clearfix">',
  198. );
  199. if ($this->is_a_group()) {
  200. if ($this->can_build_group()) {
  201. $form['clear_markup_start'] = array(
  202. '#markup' => '<div class="clearfix">',
  203. );
  204. // Render the build group form.
  205. $this->show_build_group_form($form, $form_state);
  206. $form['clear_markup_end'] = array(
  207. '#markup' => '</div>',
  208. );
  209. }
  210. }
  211. else {
  212. // Add the subform from operator_form().
  213. $this->show_operator_form($form, $form_state);
  214. // Add the subform from value_form().
  215. $this->show_value_form($form, $form_state);
  216. $form['clear_markup_end'] = array(
  217. '#markup' => '</div>',
  218. );
  219. if ($this->can_expose()) {
  220. // Add the subform from expose_form().
  221. $this->show_expose_form($form, $form_state);
  222. }
  223. }
  224. }
  225. /**
  226. * Simple validate handler
  227. */
  228. public function options_validate(&$form, &$form_state) {
  229. $this->operator_validate($form, $form_state);
  230. $this->value_validate($form, $form_state);
  231. if (!empty($this->options['exposed']) && !$this->is_a_group()) {
  232. $this->expose_validate($form, $form_state);
  233. }
  234. if ($this->is_a_group()) {
  235. $this->build_group_validate($form, $form_state);
  236. }
  237. }
  238. /**
  239. * Simple submit handler.
  240. */
  241. public function options_submit(&$form, &$form_state) {
  242. // Don't store these.
  243. unset($form_state['values']['expose_button']);
  244. unset($form_state['values']['group_button']);
  245. if (!$this->is_a_group()) {
  246. $this->operator_submit($form, $form_state);
  247. $this->value_submit($form, $form_state);
  248. }
  249. if (!empty($this->options['exposed'])) {
  250. $this->expose_submit($form, $form_state);
  251. }
  252. if ($this->is_a_group()) {
  253. $this->build_group_submit($form, $form_state);
  254. }
  255. }
  256. /**
  257. * Shortcut to display the operator form.
  258. */
  259. public function show_operator_form(&$form, &$form_state) {
  260. $this->operator_form($form, $form_state);
  261. $form['operator']['#prefix'] = '<div class="views-group-box views-left-30">';
  262. $form['operator']['#suffix'] = '</div>';
  263. }
  264. /**
  265. * Options form subform for setting the operator.
  266. *
  267. * This may be overridden by child classes, and it must
  268. * define $form['operator'];
  269. *
  270. * @see options_form()
  271. */
  272. public function operator_form(&$form, &$form_state) {
  273. $options = $this->operator_options();
  274. if (!empty($options)) {
  275. $form['operator'] = array(
  276. '#type' => count($options) < 10 ? 'radios' : 'select',
  277. '#title' => t('Operator'),
  278. '#default_value' => $this->operator,
  279. '#options' => $options,
  280. );
  281. }
  282. }
  283. /**
  284. * Provide a list of options for the default operator form.
  285. *
  286. * Should be overridden by classes that don't override operator_form
  287. */
  288. public function operator_options() {
  289. return array();
  290. }
  291. /**
  292. * Validate the operator form.
  293. */
  294. public function operator_validate($form, &$form_state) {
  295. }
  296. /**
  297. * Perform any necessary changes to the form values prior to storage.
  298. *
  299. * There is no need for this function to actually store the data.
  300. */
  301. public function operator_submit($form, &$form_state) {
  302. }
  303. /**
  304. * Shortcut to display the value form.
  305. */
  306. public function show_value_form(&$form, &$form_state) {
  307. $this->value_form($form, $form_state);
  308. if (empty($this->no_operator)) {
  309. $form['value']['#prefix'] = '<div class="views-group-box views-right-70">' . (isset($form['value']['#prefix']) ? $form['value']['#prefix'] : '');
  310. $form['value']['#suffix'] = (isset($form['value']['#suffix']) ? $form['value']['#suffix'] : '') . '</div>';
  311. }
  312. }
  313. /**
  314. * Options form subform for setting options.
  315. *
  316. * This should be overridden by all child classes and it must define
  317. * $form['value'].
  318. *
  319. * @see options_form()
  320. */
  321. public function value_form(&$form, &$form_state) {
  322. $form['value'] = array();
  323. }
  324. /**
  325. * Validate the options form.
  326. */
  327. public function value_validate($form, &$form_state) {
  328. }
  329. /**
  330. * Perform any necessary changes to the form values prior to storage.
  331. *
  332. * There is no need for this function to actually store the data.
  333. */
  334. public function value_submit($form, &$form_state) {
  335. }
  336. /**
  337. * Shortcut to display the exposed options form.
  338. */
  339. public function show_build_group_form(&$form, &$form_state) {
  340. if (empty($this->options['is_grouped'])) {
  341. return;
  342. }
  343. $this->build_group_form($form, $form_state);
  344. // When we click the expose button, we add new gadgets to the form but they
  345. // have no data in $_POST so their defaults get wiped out. This prevents
  346. // these defaults from getting wiped out. This setting will only be TRUE
  347. // during a 2nd pass rerender.
  348. if (!empty($form_state['force_build_group_options'])) {
  349. foreach (element_children($form['group_info']) as $id) {
  350. if (isset($form['group_info'][$id]['#default_value']) && !isset($form['group_info'][$id]['#value'])) {
  351. $form['group_info'][$id]['#value'] = $form['group_info'][$id]['#default_value'];
  352. }
  353. }
  354. }
  355. }
  356. /**
  357. * Shortcut to display the build_group/hide button.
  358. */
  359. public function show_build_group_button(&$form, &$form_state) {
  360. $form['group_button'] = array(
  361. '#prefix' => '<div class="views-grouped clearfix">',
  362. '#suffix' => '</div>',
  363. // Should always come after the description and the relationship.
  364. '#weight' => -190,
  365. );
  366. $grouped_description = t('Grouped filters allow a choice between predefined operator|value pairs.');
  367. $form['group_button']['radios'] = array(
  368. '#theme_wrappers' => array('container'),
  369. '#attributes' => array('class' => array('js-only')),
  370. );
  371. $form['group_button']['radios']['radios'] = array(
  372. '#title' => t('Filter type to expose'),
  373. '#description' => $grouped_description,
  374. '#type' => 'radios',
  375. '#options' => array(
  376. t('Single filter'),
  377. t('Grouped filters'),
  378. ),
  379. );
  380. if (empty($this->options['is_grouped'])) {
  381. $form['group_button']['markup'] = array(
  382. '#markup' => '<div class="description grouped-description">' . $grouped_description . '</div>',
  383. );
  384. $form['group_button']['button'] = array(
  385. '#limit_validation_errors' => array(),
  386. '#type' => 'submit',
  387. '#value' => t('Grouped filters'),
  388. '#submit' => array('views_ui_config_item_form_build_group'),
  389. );
  390. $form['group_button']['radios']['radios']['#default_value'] = 0;
  391. }
  392. else {
  393. $form['group_button']['button'] = array(
  394. '#limit_validation_errors' => array(),
  395. '#type' => 'submit',
  396. '#value' => t('Single filter'),
  397. '#submit' => array('views_ui_config_item_form_build_group'),
  398. );
  399. $form['group_button']['radios']['radios']['#default_value'] = 1;
  400. }
  401. }
  402. /**
  403. * Shortcut to display the expose/hide button.
  404. */
  405. public function show_expose_button(&$form, &$form_state) {
  406. $form['expose_button'] = array(
  407. '#prefix' => '<div class="views-expose clearfix">',
  408. '#suffix' => '</div>',
  409. // Should always come after the description and the relationship.
  410. '#weight' => -200,
  411. );
  412. // Add a checkbox for JS users, which will have behavior attached to it so
  413. // it can replace the button.
  414. $form['expose_button']['checkbox'] = array(
  415. '#theme_wrappers' => array('container'),
  416. '#attributes' => array('class' => array('js-only')),
  417. );
  418. $form['expose_button']['checkbox']['checkbox'] = array(
  419. '#title' => t('Expose this filter to visitors, to allow them to change it'),
  420. '#type' => 'checkbox',
  421. );
  422. // Then add the button itself.
  423. if (empty($this->options['exposed'])) {
  424. $form['expose_button']['markup'] = array(
  425. '#markup' => '<div class="description exposed-description">' . t('This filter is not exposed. Expose it to allow the users to change it.') . '</div>',
  426. );
  427. $form['expose_button']['button'] = array(
  428. '#limit_validation_errors' => array(),
  429. '#type' => 'submit',
  430. '#value' => t('Expose filter'),
  431. '#submit' => array('views_ui_config_item_form_expose'),
  432. );
  433. $form['expose_button']['checkbox']['checkbox']['#default_value'] = 0;
  434. }
  435. else {
  436. $form['expose_button']['markup'] = array(
  437. '#markup' => '<div class="description exposed-description">' . t('This filter is exposed. If you hide it, users will not be able to change it.') . '</div>',
  438. );
  439. $form['expose_button']['button'] = array(
  440. '#limit_validation_errors' => array(),
  441. '#type' => 'submit',
  442. '#value' => t('Hide filter'),
  443. '#submit' => array('views_ui_config_item_form_expose'),
  444. );
  445. $form['expose_button']['checkbox']['checkbox']['#default_value'] = 1;
  446. }
  447. }
  448. /**
  449. * Options form subform for exposed filter options.
  450. *
  451. * @see options_form()
  452. */
  453. public function expose_form(&$form, &$form_state) {
  454. $form['#theme'] = 'views_ui_expose_filter_form';
  455. // #flatten will move everything from $form['expose'][$key] to $form[$key]
  456. // prior to rendering. That's why the pre_render for it needs to run first,
  457. // so that when the next pre_render (the one for fieldsets) runs, it gets
  458. // the flattened data.
  459. array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
  460. $form['expose']['#flatten'] = TRUE;
  461. if (empty($this->always_required)) {
  462. $form['expose']['required'] = array(
  463. '#type' => 'checkbox',
  464. '#title' => t('Required'),
  465. '#default_value' => $this->options['expose']['required'],
  466. );
  467. }
  468. else {
  469. $form['expose']['required'] = array(
  470. '#type' => 'value',
  471. '#value' => TRUE,
  472. );
  473. }
  474. $form['expose']['label'] = array(
  475. '#type' => 'textfield',
  476. '#default_value' => $this->options['expose']['label'],
  477. '#title' => t('Label'),
  478. '#size' => 40,
  479. );
  480. $form['expose']['description'] = array(
  481. '#type' => 'textfield',
  482. '#default_value' => $this->options['expose']['description'],
  483. '#title' => t('Description'),
  484. '#size' => 60,
  485. );
  486. if (!empty($form['operator']['#type'])) {
  487. // Increase the width of the left (operator) column.
  488. $form['operator']['#prefix'] = '<div class="views-group-box views-left-40">';
  489. $form['operator']['#suffix'] = '</div>';
  490. $form['value']['#prefix'] = '<div class="views-group-box views-right-60">';
  491. $form['value']['#suffix'] = '</div>';
  492. $form['expose']['use_operator'] = array(
  493. '#type' => 'checkbox',
  494. '#title' => t('Expose operator'),
  495. '#description' => t('Allow the user to choose the operator.'),
  496. '#default_value' => !empty($this->options['expose']['use_operator']),
  497. );
  498. $form['expose']['operator_label'] = array(
  499. '#type' => 'textfield',
  500. '#default_value' => $this->options['expose']['operator_label'],
  501. '#title' => t('Operator label'),
  502. '#size' => 40,
  503. '#description' => t('This will appear before your operator select field.'),
  504. '#dependency' => array(
  505. 'edit-options-expose-use-operator' => array(1)
  506. ),
  507. );
  508. $form['expose']['operator_id'] = array(
  509. '#type' => 'textfield',
  510. '#default_value' => $this->options['expose']['operator_id'],
  511. '#title' => t('Operator identifier'),
  512. '#size' => 40,
  513. '#description' => t('This will appear in the URL after the ? to identify this operator.'),
  514. '#dependency' => array(
  515. 'edit-options-expose-use-operator' => array(1)
  516. ),
  517. '#fieldset' => 'more',
  518. );
  519. }
  520. else {
  521. $form['expose']['operator_id'] = array(
  522. '#type' => 'value',
  523. '#value' => '',
  524. );
  525. }
  526. if (empty($this->always_multiple)) {
  527. $form['expose']['multiple'] = array(
  528. '#type' => 'checkbox',
  529. '#title' => t('Allow multiple selections'),
  530. '#description' => t('Enable to allow users to select multiple items.'),
  531. '#default_value' => $this->options['expose']['multiple'],
  532. );
  533. }
  534. $form['expose']['remember'] = array(
  535. '#type' => 'checkbox',
  536. '#title' => t('Remember the last selection'),
  537. '#description' => t('Enable to remember the last selection made by the user.'),
  538. '#default_value' => $this->options['expose']['remember'],
  539. );
  540. $role_options = array_map('check_plain', user_roles());
  541. $form['expose']['remember_roles'] = array(
  542. '#type' => 'checkboxes',
  543. '#title' => t('User roles'),
  544. '#description' => t('Remember exposed selection only for the selected user role(s). If you select no roles, the exposed data will never be stored.'),
  545. '#default_value' => $this->options['expose']['remember_roles'],
  546. '#options' => $role_options,
  547. '#dependency' => array(
  548. 'edit-options-expose-remember' => array(1),
  549. ),
  550. );
  551. $form['expose']['identifier'] = array(
  552. '#type' => 'textfield',
  553. '#default_value' => $this->options['expose']['identifier'],
  554. '#title' => t('Filter identifier'),
  555. '#size' => 40,
  556. '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
  557. '#fieldset' => 'more',
  558. );
  559. }
  560. /**
  561. * Validate the options form.
  562. */
  563. public function expose_validate($form, &$form_state) {
  564. if (empty($form_state['values']['options']['expose']['identifier'])) {
  565. form_error($form['expose']['identifier'], t('The identifier is required if the filter is exposed.'));
  566. }
  567. if (!empty($form_state['values']['options']['expose']['identifier'])) {
  568. $illegal_identifiers = array('value', 'q');
  569. if (in_array($form_state['values']['options']['expose']['identifier'], $illegal_identifiers)) {
  570. form_error($form['expose']['identifier'], t('This identifier is not allowed.'));
  571. }
  572. }
  573. if (!$this->view->display_handler->is_identifier_unique($form_state['id'], $form_state['values']['options']['expose']['identifier'])) {
  574. form_error($form['expose']['identifier'], t('This identifier is used by another handler.'));
  575. }
  576. }
  577. /**
  578. * Validate the build group options form.
  579. */
  580. public function build_group_validate($form, &$form_state) {
  581. if (!empty($form_state['values']['options']['group_info'])) {
  582. if (empty($form_state['values']['options']['group_info']['identifier'])) {
  583. form_error($form['group_info']['identifier'], t('The identifier is required if the filter is exposed.'));
  584. }
  585. if (!empty($form_state['values']['options']['group_info']['identifier'])) {
  586. $illegal_identifiers = array('value', 'q');
  587. if (in_array($form_state['values']['options']['group_info']['identifier'], $illegal_identifiers)) {
  588. form_error($form['group_info']['identifier'], t('This identifier is not allowed.'));
  589. }
  590. }
  591. if (!$this->view->display_handler->is_identifier_unique($form_state['id'], $form_state['values']['options']['group_info']['identifier'])) {
  592. form_error($form['group_info']['identifier'], t('This identifier is used by another handler.'));
  593. }
  594. }
  595. if (!empty($form_state['values']['options']['group_info']['group_items'])) {
  596. foreach ($form_state['values']['options']['group_info']['group_items'] as $id => $group) {
  597. if (empty($group['remove'])) {
  598. // Check if the title is defined but value wasn't defined.
  599. if (!empty($group['title'])) {
  600. if ((!is_array($group['value']) && trim($group['value']) == "") ||
  601. (is_array($group['value']) && count(array_filter($group['value'], '_views_array_filter_zero')) == 0)) {
  602. form_error($form['group_info']['group_items'][$id]['value'],
  603. t('The value is required if title for this item is defined.'));
  604. }
  605. }
  606. // Check if the value is defined but title wasn't defined.
  607. if ((!is_array($group['value']) && trim($group['value']) != "") ||
  608. (is_array($group['value']) && count(array_filter($group['value'], '_views_array_filter_zero')) > 0)) {
  609. if (empty($group['title'])) {
  610. form_error($form['group_info']['group_items'][$id]['title'],
  611. t('The title is required if value for this item is defined.'));
  612. }
  613. }
  614. }
  615. }
  616. }
  617. }
  618. /**
  619. * Save new group items, re-enumerates and remove groups marked to delete.
  620. */
  621. public function build_group_submit($form, &$form_state) {
  622. $groups = array();
  623. uasort($form_state['values']['options']['group_info']['group_items'], 'drupal_sort_weight');
  624. // Filter out removed items.
  625. // Start from 1 to avoid problems with #default_value in the widget.
  626. $new_id = 1;
  627. $new_default = 'All';
  628. foreach ($form_state['values']['options']['group_info']['group_items'] as $id => $group) {
  629. if (empty($group['remove'])) {
  630. // Don't store this.
  631. unset($group['remove']);
  632. unset($group['weight']);
  633. $groups[$new_id] = $group;
  634. if ($form_state['values']['options']['group_info']['default_group'] === $id) {
  635. $new_default = $new_id;
  636. }
  637. }
  638. $new_id++;
  639. }
  640. if ($new_default != 'All') {
  641. $form_state['values']['options']['group_info']['default_group'] = $new_default;
  642. }
  643. $filter_default_multiple = array_filter($form_state['values']['options']['group_info']['default_group_multiple']);
  644. $form_state['values']['options']['group_info']['default_group_multiple'] = $filter_default_multiple;
  645. $form_state['values']['options']['group_info']['group_items'] = $groups;
  646. }
  647. /**
  648. * Provide default options for exposed filters.
  649. */
  650. public function expose_options() {
  651. $this->options['expose'] = array(
  652. 'use_operator' => FALSE,
  653. 'operator' => $this->options['id'] . '_op',
  654. 'identifier' => $this->options['id'],
  655. 'label' => $this->definition['title'],
  656. 'description' => NULL,
  657. 'remember' => FALSE,
  658. 'multiple' => FALSE,
  659. 'required' => FALSE,
  660. );
  661. }
  662. /**
  663. * Provide default options for exposed filters.
  664. */
  665. public function build_group_options() {
  666. $this->options['group_info'] = array(
  667. 'label' => $this->definition['title'],
  668. 'description' => NULL,
  669. 'identifier' => $this->options['id'],
  670. 'optional' => TRUE,
  671. 'widget' => 'select',
  672. 'multiple' => FALSE,
  673. 'remember' => FALSE,
  674. 'default_group' => 'All',
  675. 'default_group_multiple' => array(),
  676. 'group_items' => array(),
  677. );
  678. }
  679. /**
  680. * Build a form with a group of operator | values to apply as a single filter.
  681. */
  682. public function group_form(&$form, &$form_state) {
  683. if (!empty($this->options['group_info']['optional']) && !$this->multiple_exposed_input()) {
  684. $old_any = $this->options['group_info']['widget'] == 'select' ? '<Any>' : '&lt;Any&gt;';
  685. $any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? $old_any : t('- Any -');
  686. $groups = array('All' => $any_label);
  687. }
  688. foreach ($this->options['group_info']['group_items'] as $id => $group) {
  689. if (!empty($group['title'])) {
  690. $groups[$id] = $id != 'All' ? t($group['title']) : $group['title'];
  691. }
  692. }
  693. if (count($groups)) {
  694. $value = $this->options['group_info']['identifier'];
  695. $form[$value] = array(
  696. '#type' => $this->options['group_info']['widget'],
  697. '#default_value' => $this->group_info,
  698. '#options' => $groups,
  699. );
  700. if (!empty($this->options['group_info']['multiple'])) {
  701. if (count($groups) < 5) {
  702. $form[$value]['#type'] = 'checkboxes';
  703. }
  704. else {
  705. $form[$value]['#type'] = 'select';
  706. $form[$value]['#size'] = 5;
  707. $form[$value]['#multiple'] = TRUE;
  708. }
  709. unset($form[$value]['#default_value']);
  710. if (empty($form_state['input'][$value])) {
  711. $form_state['input'][$value] = $this->group_info;
  712. }
  713. }
  714. $this->options['expose']['label'] = '';
  715. }
  716. }
  717. /**
  718. * Render our chunk of the exposed filter form when selecting
  719. *
  720. * You can override this if it doesn't do what you expect.
  721. */
  722. public function exposed_form(&$form, &$form_state) {
  723. if (empty($this->options['exposed'])) {
  724. return;
  725. }
  726. // Build the exposed form, when its based on an operator.
  727. if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) {
  728. $operator = $this->options['expose']['operator_id'];
  729. $this->operator_form($form, $form_state);
  730. $form[$operator] = $form['operator'];
  731. $form[$operator]['#title'] = $this->options['expose']['operator_label'];
  732. $form[$operator]['#title_display'] = 'invisible';
  733. $this->exposed_translate($form[$operator], 'operator');
  734. unset($form['operator']);
  735. }
  736. // Build the form and set the value based on the identifier.
  737. if (!empty($this->options['expose']['identifier'])) {
  738. $value = $this->options['expose']['identifier'];
  739. $this->value_form($form, $form_state);
  740. $form[$value] = $form['value'];
  741. if (isset($form[$value]['#title']) && !empty($form[$value]['#type']) && $form[$value]['#type'] != 'checkbox') {
  742. unset($form[$value]['#title']);
  743. }
  744. $this->exposed_translate($form[$value], 'value');
  745. if (!empty($form['#type']) && ($form['#type'] == 'checkboxes' || ($form['#type'] == 'select' && !empty($form['#multiple'])))) {
  746. unset($form[$value]['#default_value']);
  747. }
  748. if (!empty($form['#type']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
  749. $form[$value]['#default_value'] = 'All';
  750. }
  751. if ($value != 'value') {
  752. unset($form['value']);
  753. }
  754. }
  755. }
  756. /**
  757. * Build the form to let users create the group of exposed filters.
  758. *
  759. * This form is displayed when users click on button 'Build group'
  760. */
  761. public function build_group_form(&$form, &$form_state) {
  762. if (empty($this->options['exposed']) || empty($this->options['is_grouped'])) {
  763. return;
  764. }
  765. $form['#theme'] = 'views_ui_build_group_filter_form';
  766. // #flatten will move everything from $form['group_info'][$key] to
  767. // $form[$key] prior to rendering. That's why the pre_render for it needs
  768. // to run first, so that when the next pre_render (the one for fieldsets)
  769. // runs, it gets the flattened data.
  770. array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
  771. $form['group_info']['#flatten'] = TRUE;
  772. if (!empty($this->options['group_info']['identifier'])) {
  773. $identifier = $this->options['group_info']['identifier'];
  774. }
  775. else {
  776. $identifier = 'group_' . $this->options['expose']['identifier'];
  777. }
  778. $form['group_info']['identifier'] = array(
  779. '#type' => 'textfield',
  780. '#default_value' => $identifier,
  781. '#title' => t('Filter identifier'),
  782. '#size' => 40,
  783. '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
  784. '#fieldset' => 'more',
  785. );
  786. $form['group_info']['label'] = array(
  787. '#type' => 'textfield',
  788. '#default_value' => $this->options['group_info']['label'],
  789. '#title' => t('Label'),
  790. '#size' => 40,
  791. );
  792. $form['group_info']['optional'] = array(
  793. '#type' => 'checkbox',
  794. '#title' => t('Optional'),
  795. '#description' => t('This exposed filter is optional and will have added options to allow it not to be set.'),
  796. '#default_value' => $this->options['group_info']['optional'],
  797. );
  798. $form['group_info']['multiple'] = array(
  799. '#type' => 'checkbox',
  800. '#title' => t('Allow multiple selections'),
  801. '#description' => t('Enable to allow users to select multiple items.'),
  802. '#default_value' => $this->options['group_info']['multiple'],
  803. );
  804. $form['group_info']['widget'] = array(
  805. '#type' => 'radios',
  806. '#default_value' => $this->options['group_info']['widget'],
  807. '#title' => t('Widget type'),
  808. '#options' => array(
  809. 'radios' => t('Radios'),
  810. 'select' => t('Select'),
  811. ),
  812. '#description' => t('Select which kind of widget will be used to render the group of filters'),
  813. );
  814. $form['group_info']['remember'] = array(
  815. '#type' => 'checkbox',
  816. '#title' => t('Remember'),
  817. '#description' => t('Remember the last setting the user gave this filter.'),
  818. '#default_value' => $this->options['group_info']['remember'],
  819. );
  820. if (!empty($this->options['group_info']['identifier'])) {
  821. $identifier = $this->options['group_info']['identifier'];
  822. }
  823. else {
  824. $identifier = 'group_' . $this->options['expose']['identifier'];
  825. }
  826. $form['group_info']['identifier'] = array(
  827. '#type' => 'textfield',
  828. '#default_value' => $identifier,
  829. '#title' => t('Filter identifier'),
  830. '#size' => 40,
  831. '#description' => t('This will appear in the URL after the ? to identify this filter. Cannot be blank.'),
  832. '#fieldset' => 'more',
  833. );
  834. $form['group_info']['label'] = array(
  835. '#type' => 'textfield',
  836. '#default_value' => $this->options['group_info']['label'],
  837. '#title' => t('Label'),
  838. '#size' => 40,
  839. );
  840. $form['group_info']['description'] = array(
  841. '#type' => 'textfield',
  842. '#default_value' => $this->options['group_info']['description'],
  843. '#title' => t('Description'),
  844. '#size' => 60,
  845. );
  846. $form['group_info']['optional'] = array(
  847. '#type' => 'checkbox',
  848. '#title' => t('Optional'),
  849. '#description' => t('This exposed filter is optional and will have added options to allow it not to be set.'),
  850. '#default_value' => $this->options['group_info']['optional'],
  851. );
  852. $form['group_info']['widget'] = array(
  853. '#type' => 'radios',
  854. '#default_value' => $this->options['group_info']['widget'],
  855. '#title' => t('Widget type'),
  856. '#options' => array(
  857. 'radios' => t('Radios'),
  858. 'select' => t('Select'),
  859. ),
  860. '#description' => t('Select which kind of widget will be used to render the group of filters'),
  861. );
  862. $form['group_info']['remember'] = array(
  863. '#type' => 'checkbox',
  864. '#title' => t('Remember'),
  865. '#description' => t('Remember the last setting the user gave this filter.'),
  866. '#default_value' => $this->options['group_info']['remember'],
  867. );
  868. // The string '- Any -' will not be rendered.
  869. // @see theme_views_ui_build_group_filter_form()
  870. $groups = array('All' => '- Any -');
  871. // Provide 3 options to start when we are in a new group.
  872. if (count($this->options['group_info']['group_items']) == 0) {
  873. $this->options['group_info']['group_items'] = array_fill(1, 3, array());
  874. }
  875. // After the general settings, comes a table with all the existent groups.
  876. $default_weight = 0;
  877. foreach ($this->options['group_info']['group_items'] as $item_id => $item) {
  878. if (!empty($form_state['values']['options']['group_info']['group_items'][$item_id]['remove'])) {
  879. continue;
  880. }
  881. // Each rows contains three widgets:
  882. // a) The title, where users define how they identify a pair of operator
  883. // | value.
  884. // b) The operator.
  885. // c) The value (or values) to use in the filter with the selected
  886. // operator.
  887. // In each row, we have to display the operator form and the value from
  888. // $row acts as a fake form to render each widget in a row.
  889. $row = array();
  890. $groups[$item_id] = '';
  891. $this->operator_form($row, $form_state);
  892. // Force the operator form to be a select box. Some handlers uses
  893. // radios and they occupy a lot of space in a table row.
  894. $row['operator']['#type'] = 'select';
  895. $row['operator']['#title'] = '';
  896. $this->value_form($row, $form_state);
  897. // Fix the dependencies to update value forms when operators changes.
  898. // This is needed because forms are inside a new form and their ids
  899. // changes. Dependencies are used when operator changes from to
  900. // 'Between', 'Not Between', etc, and two or more widgets are displayed.
  901. $without_children = TRUE;
  902. foreach (element_children($row['value']) as $children) {
  903. if (isset($row['value'][$children]['#dependency']['edit-options-operator'])) {
  904. $row['value'][$children]['#dependency']["edit-options-group-info-group-items-$item_id-operator"] = $row['value'][$children]['#dependency']['edit-options-operator'];
  905. unset($row['value'][$children]['#dependency']['edit-options-operator']);
  906. $row['value'][$children]['#title'] = '';
  907. if (!empty($this->options['group_info']['group_items'][$item_id]['value'][$children])) {
  908. $row['value'][$children]['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'][$children];
  909. }
  910. }
  911. $without_children = FALSE;
  912. }
  913. if ($without_children) {
  914. if (!empty($this->options['group_info']['group_items'][$item_id]['value'])) {
  915. $row['value']['#default_value'] = $this->options['group_info']['group_items'][$item_id]['value'];
  916. }
  917. }
  918. if (!empty($this->options['group_info']['group_items'][$item_id]['operator'])) {
  919. $row['operator']['#default_value'] = $this->options['group_info']['group_items'][$item_id]['operator'];
  920. }
  921. $default_title = '';
  922. if (!empty($this->options['group_info']['group_items'][$item_id]['title'])) {
  923. $default_title = $this->options['group_info']['group_items'][$item_id]['title'];
  924. }
  925. // Per item group, we have a title that identifies it.
  926. $form['group_info']['group_items'][$item_id] = array(
  927. 'title' => array(
  928. '#type' => 'textfield',
  929. '#size' => 20,
  930. '#default_value' => $default_title,
  931. ),
  932. 'operator' => $row['operator'],
  933. 'value' => $row['value'],
  934. 'remove' => array(
  935. '#type' => 'checkbox',
  936. '#id' => 'views-removed-' . $item_id,
  937. '#attributes' => array('class' => array('views-remove-checkbox')),
  938. '#default_value' => 0,
  939. ),
  940. 'weight' => array(
  941. '#type' => 'weight',
  942. '#delta' => count($this->options['group_info']['group_items']),
  943. '#default_value' => $default_weight++,
  944. '#attributes' => array('class' => array('weight')),
  945. ),
  946. );
  947. }
  948. // From all groups, let chose which is the default.
  949. $form['group_info']['default_group'] = array(
  950. '#type' => 'radios',
  951. '#options' => $groups,
  952. '#default_value' => $this->options['group_info']['default_group'],
  953. '#required' => TRUE,
  954. '#attributes' => array(
  955. 'class' => array('default-radios'),
  956. )
  957. );
  958. // From all groups, let chose which is the default.
  959. $form['group_info']['default_group_multiple'] = array(
  960. '#type' => 'checkboxes',
  961. '#options' => $groups,
  962. '#default_value' => $this->options['group_info']['default_group_multiple'],
  963. '#attributes' => array(
  964. 'class' => array('default-checkboxes'),
  965. )
  966. );
  967. $form['group_info']['add_group'] = array(
  968. '#prefix' => '<div class="views-build-group clear-block">',
  969. '#suffix' => '</div>',
  970. '#type' => 'submit',
  971. '#value' => t('Add another item'),
  972. '#submit' => array('views_ui_config_item_form_add_group'),
  973. );
  974. $js = array();
  975. $js['tableDrag']['views-filter-groups']['weight'][0] = array(
  976. 'target' => 'weight',
  977. 'source' => NULL,
  978. 'relationship' => 'sibling',
  979. 'action' => 'order',
  980. 'hidden' => TRUE,
  981. 'limit' => 0,
  982. );
  983. if (!empty($form_state['js settings']) && is_array($js)) {
  984. $form_state['js settings'] = array_merge($form_state['js settings'], $js);
  985. }
  986. else {
  987. $form_state['js settings'] = $js;
  988. }
  989. }
  990. /**
  991. * Make some translations to a form item to make it more suitable to exposing.
  992. */
  993. public function exposed_translate(&$form, $type) {
  994. if (!isset($form['#type'])) {
  995. return;
  996. }
  997. if ($form['#type'] == 'radios') {
  998. $form['#type'] = 'select';
  999. }
  1000. // Checkboxes don't work so well in exposed forms due to GET conversions.
  1001. if ($form['#type'] == 'checkboxes') {
  1002. if (empty($form['#no_convert']) || empty($this->options['expose']['multiple'])) {
  1003. $form['#type'] = 'select';
  1004. }
  1005. if (!empty($this->options['expose']['multiple'])) {
  1006. $form['#multiple'] = TRUE;
  1007. }
  1008. }
  1009. if (empty($this->options['expose']['multiple']) && isset($form['#multiple'])) {
  1010. unset($form['#multiple']);
  1011. $form['#size'] = NULL;
  1012. }
  1013. // Cleanup in case the translated element's (radios or checkboxes) display
  1014. // value contains html.
  1015. if ($form['#type'] == 'select') {
  1016. $this->prepare_filter_select_options($form['#options']);
  1017. }
  1018. if ($type == 'value' && empty($this->always_required) && empty($this->options['expose']['required']) && $form['#type'] == 'select' && empty($form['#multiple'])) {
  1019. $any_label = variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? t('<Any>') : t('- Any -');
  1020. $form['#options'] = array('All' => $any_label) + $form['#options'];
  1021. $form['#default_value'] = 'All';
  1022. }
  1023. if (!empty($this->options['expose']['required'])) {
  1024. $form['#required'] = TRUE;
  1025. }
  1026. }
  1027. /**
  1028. * Sanitizes the HTML select element's options.
  1029. *
  1030. * The function is recursive to support optgroups.
  1031. */
  1032. public function prepare_filter_select_options(&$options) {
  1033. foreach ($options as $value => $label) {
  1034. // Recurse for optgroups.
  1035. if (is_array($label)) {
  1036. $this->prepare_filter_select_options($options[$value]);
  1037. }
  1038. // FAPI has some special value to allow hierarchy.
  1039. // @see _form_options_flatten()
  1040. elseif (is_object($label)) {
  1041. $this->prepare_filter_select_options($options[$value]->option);
  1042. }
  1043. else {
  1044. $options[$value] = strip_tags(decode_entities($label));
  1045. }
  1046. }
  1047. }
  1048. /**
  1049. * Tell the renderer about our exposed form.
  1050. *
  1051. * This only needs to be overridden for particularly complex forms. And maybe
  1052. * not even then.
  1053. *
  1054. * @return array|null
  1055. * For standard exposed filters. An array with the following keys:
  1056. * - operator: The $form key of the operator. Set to NULL if no operator.
  1057. * - value: The $form key of the value. Set to NULL if no value.
  1058. * - label: The label to use for this piece.
  1059. * For grouped exposed filters. An array with the following keys:
  1060. * - value: The $form key of the value. Set to NULL if no value.
  1061. * - label: The label to use for this piece.
  1062. */
  1063. public function exposed_info() {
  1064. if (empty($this->options['exposed'])) {
  1065. return;
  1066. }
  1067. if ($this->is_a_group()) {
  1068. return array(
  1069. 'value' => $this->options['group_info']['identifier'],
  1070. 'label' => $this->options['group_info']['label'],
  1071. 'description' => $this->options['group_info']['description'],
  1072. );
  1073. }
  1074. return array(
  1075. 'operator' => $this->options['expose']['operator_id'],
  1076. 'value' => $this->options['expose']['identifier'],
  1077. 'label' => $this->options['expose']['label'],
  1078. 'description' => $this->options['expose']['description'],
  1079. );
  1080. }
  1081. /**
  1082. * Transform the input from a grouped filter into a standard filter.
  1083. *
  1084. * When a filter is a group, find the set of operator and values
  1085. * that the choosed item represents, and inform views that a normal
  1086. * filter was submitted by telling the operator and the value selected.
  1087. *
  1088. * The param $selected_group_id is only passed when the filter uses the
  1089. * checkboxes widget, and this function will be called for each item
  1090. * choosed in the checkboxes.
  1091. */
  1092. public function convert_exposed_input(&$input, $selected_group_id = NULL) {
  1093. if ($this->is_a_group()) {
  1094. // If it is already defined the selected group, use it. Only valid when
  1095. // the filter uses checkboxes for widget.
  1096. if (!empty($selected_group_id)) {
  1097. $selected_group = $selected_group_id;
  1098. }
  1099. else {
  1100. $selected_group = $input[$this->options['group_info']['identifier']];
  1101. }
  1102. if ($selected_group == 'All' && !empty($this->options['group_info']['optional'])) {
  1103. return NULL;
  1104. }
  1105. if ($selected_group != 'All' && empty($this->options['group_info']['group_items'][$selected_group])) {
  1106. return FALSE;
  1107. }
  1108. if (isset($selected_group) && isset($this->options['group_info']['group_items'][$selected_group])) {
  1109. $input[$this->options['expose']['operator']] = $this->options['group_info']['group_items'][$selected_group]['operator'];
  1110. // Value can be optional, For example for 'empty' and 'not empty'
  1111. // filters.
  1112. if (!empty($this->options['group_info']['group_items'][$selected_group]['value'])) {
  1113. $input[$this->options['expose']['identifier']] = $this->options['group_info']['group_items'][$selected_group]['value'];
  1114. }
  1115. $this->options['expose']['use_operator'] = TRUE;
  1116. $this->group_info = $input[$this->options['group_info']['identifier']];
  1117. return TRUE;
  1118. }
  1119. else {
  1120. return FALSE;
  1121. }
  1122. }
  1123. }
  1124. /**
  1125. * Returns the options available for a grouped filter that users checkboxes
  1126. * as widget, and therefore has to be applied several times, one per
  1127. * item selected.
  1128. */
  1129. public function group_multiple_exposed_input(&$input) {
  1130. if (!empty($input[$this->options['group_info']['identifier']])) {
  1131. return array_filter($input[$this->options['group_info']['identifier']]);
  1132. }
  1133. return array();
  1134. }
  1135. /**
  1136. *
  1137. *
  1138. * @return bool
  1139. * TRUE if users can select multiple groups items of a grouped exposed
  1140. * filter.
  1141. */
  1142. public function multiple_exposed_input() {
  1143. return $this->is_a_group() && !empty($this->options['group_info']['multiple']);
  1144. }
  1145. /**
  1146. * If set to remember exposed input in the session, store it there.
  1147. * This function is similar to store_exposed_input but modified to
  1148. * work properly when the filter is a group.
  1149. */
  1150. public function store_group_input($input, $status) {
  1151. if (!$this->is_a_group() || empty($this->options['group_info']['identifier'])) {
  1152. return TRUE;
  1153. }
  1154. if (empty($this->options['group_info']['remember'])) {
  1155. return;
  1156. }
  1157. // Figure out which display id is responsible for the filters, so we know
  1158. // where to look for session stored values.
  1159. $display_id = ($this->view->display_handler->is_defaulted('filters')) ? 'default' : $this->view->current_display;
  1160. // False means that we got a setting that means to recuse ourselves, so we
  1161. // should erase whatever happened to be there.
  1162. if ($status === FALSE && isset($_SESSION['views'][$this->view->name][$display_id])) {
  1163. $session = &$_SESSION['views'][$this->view->name][$display_id];
  1164. if (isset($session[$this->options['group_info']['identifier']])) {
  1165. unset($session[$this->options['group_info']['identifier']]);
  1166. }
  1167. }
  1168. if ($status !== FALSE) {
  1169. if (!isset($_SESSION['views'][$this->view->name][$display_id])) {
  1170. $_SESSION['views'][$this->view->name][$display_id] = array();
  1171. }
  1172. $session = &$_SESSION['views'][$this->view->name][$display_id];
  1173. $session[$this->options['group_info']['identifier']] = $input[$this->options['group_info']['identifier']];
  1174. }
  1175. }
  1176. /**
  1177. * Check to see if input from the exposed filters should change the behavior.
  1178. */
  1179. public function accept_exposed_input($input) {
  1180. if (empty($this->options['exposed'])) {
  1181. return TRUE;
  1182. }
  1183. if (!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']) && isset($input[$this->options['expose']['operator_id']])) {
  1184. $this->operator = $input[$this->options['expose']['operator_id']];
  1185. }
  1186. if (!empty($this->options['expose']['identifier'])) {
  1187. $value = $input[$this->options['expose']['identifier']];
  1188. // Various ways to check for the absence of non-required input.
  1189. if (empty($this->options['expose']['required'])) {
  1190. if (($this->operator == 'empty' || $this->operator == 'not empty') && $value === '') {
  1191. $value = ' ';
  1192. }
  1193. if ($this->operator != 'empty' && $this->operator != 'not empty') {
  1194. if ($value == 'All' || $value === array()) {
  1195. return FALSE;
  1196. }
  1197. }
  1198. if (!empty($this->always_multiple) && $value === '') {
  1199. return FALSE;
  1200. }
  1201. }
  1202. if (isset($value)) {
  1203. $this->value = $value;
  1204. if (empty($this->always_multiple) && empty($this->options['expose']['multiple'])) {
  1205. $this->value = array($value);
  1206. }
  1207. }
  1208. else {
  1209. return FALSE;
  1210. }
  1211. }
  1212. return TRUE;
  1213. }
  1214. /**
  1215. *
  1216. */
  1217. public function store_exposed_input($input, $status) {
  1218. if (empty($this->options['exposed']) || empty($this->options['expose']['identifier'])) {
  1219. return TRUE;
  1220. }
  1221. if (empty($this->options['expose']['remember'])) {
  1222. return;
  1223. }
  1224. // Check if we store exposed value for current user.
  1225. global $user;
  1226. $allowed_rids = empty($this->options['expose']['remember_roles']) ? array() : array_filter($this->options['expose']['remember_roles']);
  1227. $intersect_rids = array_intersect_key($allowed_rids, $user->roles);
  1228. if (empty($intersect_rids)) {
  1229. return;
  1230. }
  1231. // Figure out which display id is responsible for the filters, so we know
  1232. // where to look for session stored values.
  1233. $display_id = ($this->view->display_handler->is_defaulted('filters')) ? 'default' : $this->view->current_display;
  1234. // Shortcut test.
  1235. $operator = !empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id']);
  1236. // False means that we got a setting that means to recuse ourselves, so we
  1237. // should erase whatever happened to be there.
  1238. if (!$status && isset($_SESSION['views'][$this->view->name][$display_id])) {
  1239. $session = &$_SESSION['views'][$this->view->name][$display_id];
  1240. if ($operator && isset($session[$this->options['expose']['operator_id']])) {
  1241. unset($session[$this->options['expose']['operator_id']]);
  1242. }
  1243. if (isset($session[$this->options['expose']['identifier']])) {
  1244. unset($session[$this->options['expose']['identifier']]);
  1245. }
  1246. }
  1247. if ($status) {
  1248. if (!isset($_SESSION['views'][$this->view->name][$display_id])) {
  1249. $_SESSION['views'][$this->view->name][$display_id] = array();
  1250. }
  1251. $session = &$_SESSION['views'][$this->view->name][$display_id];
  1252. if ($operator && isset($input[$this->options['expose']['operator_id']])) {
  1253. $session[$this->options['expose']['operator_id']] = $input[$this->options['expose']['operator_id']];
  1254. }
  1255. $session[$this->options['expose']['identifier']] = $input[$this->options['expose']['identifier']];
  1256. }
  1257. }
  1258. /**
  1259. * Add this filter to the query.
  1260. *
  1261. * Due to the nature of fapi, the value and the operator have an unintended
  1262. * level of indirection. You will find them in $this->operator and
  1263. * $this->value respectively.
  1264. */
  1265. public function query() {
  1266. $this->ensure_my_table();
  1267. $this->query->add_where($this->options['group'], "$this->table_alias.$this->real_field", $this->value, $this->operator);
  1268. }
  1269. /**
  1270. * Can this filter be used in OR groups?
  1271. *
  1272. * Some filters have complicated where clauses that cannot be easily used with
  1273. * OR groups. Some filters must also use HAVING which also makes them not
  1274. * groupable. These filters will end up in a special group if OR grouping is
  1275. * in use.
  1276. *
  1277. * @return bool
  1278. * Whether the filter can be used in OR groups.
  1279. */
  1280. public function can_group() {
  1281. return TRUE;
  1282. }
  1283. }
  1284. /**
  1285. * A special handler to take the place of missing or broken handlers.
  1286. *
  1287. * @ingroup views_filter_handlers
  1288. */
  1289. class views_handler_filter_broken extends views_handler_filter {
  1290. /**
  1291. * {@inheritdoc}
  1292. */
  1293. public function ui_name($short = FALSE) {
  1294. return t('Broken/missing handler');
  1295. }
  1296. /**
  1297. * {@inheritdoc}
  1298. */
  1299. public function ensure_my_table() {
  1300. // No table to ensure!
  1301. }
  1302. /**
  1303. * {@inheritdoc}
  1304. */
  1305. public function query($group_by = FALSE) {
  1306. // No query to run.
  1307. }
  1308. /**
  1309. * {@inheritdoc}
  1310. */
  1311. public function options_form(&$form, &$form_state) {
  1312. $form['markup'] = array(
  1313. '#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>',
  1314. );
  1315. }
  1316. /**
  1317. * {@inheritdoc}
  1318. */
  1319. public function broken() {
  1320. return TRUE;
  1321. }
  1322. }
  1323. /**
  1324. * Filter by no empty values, though allow to use "0".
  1325. *
  1326. * @param string $var
  1327. *
  1328. * @return bool
  1329. */
  1330. function _views_array_filter_zero($var) {
  1331. return trim($var) != "";
  1332. }
  1333. /**
  1334. * @}
  1335. */