views_handler_filter.inc 55 KB

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