views_handler_filter.inc 50 KB

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