views_plugin_exposed_form.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_plugin_exposed_form.
  5. */
  6. /**
  7. * @defgroup views_exposed_form_plugins Views exposed form plugins
  8. * @{
  9. * Plugins that handle the validation/submission and rendering of exposed forms.
  10. *
  11. * If needed, it is possible to use them to add additional form elements.
  12. *
  13. * @see hook_views_plugins()
  14. */
  15. /**
  16. * The base plugin to handle exposed filter forms.
  17. */
  18. class views_plugin_exposed_form extends views_plugin {
  19. /**
  20. * Initialize the plugin.
  21. *
  22. * @param object $view
  23. * The view object.
  24. * @param object $display
  25. * The display handler.
  26. * @param array $options
  27. * Any additional options that are being added.
  28. */
  29. public function init(&$view, &$display, $options = array()) {
  30. $this->view = &$view;
  31. $this->display = &$display;
  32. $this->unpack_options($this->options, $options);
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function option_definition() {
  38. $options = parent::option_definition();
  39. $options['submit_button'] = array('default' => 'Apply', 'translatable' => TRUE);
  40. $options['reset_button'] = array('default' => FALSE, 'bool' => TRUE);
  41. $options['reset_button_label'] = array('default' => 'Reset', 'translatable' => TRUE);
  42. $options['exposed_sorts_label'] = array('default' => 'Sort by', 'translatable' => TRUE);
  43. $options['expose_sort_order'] = array('default' => TRUE, 'bool' => TRUE);
  44. $options['sort_asc_label'] = array('default' => 'Asc', 'translatable' => TRUE);
  45. $options['sort_desc_label'] = array('default' => 'Desc', 'translatable' => TRUE);
  46. $options['autosubmit'] = array('default' => FALSE, 'bool' => TRUE);
  47. $options['autosubmit_hide'] = array('default' => TRUE, 'bool' => TRUE);
  48. return $options;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function options_form(&$form, &$form_state) {
  54. parent::options_form($form, $form_state);
  55. $form['submit_button'] = array(
  56. '#type' => 'textfield',
  57. '#title' => t('Submit button text'),
  58. '#description' => t('Text to display in the submit button of the exposed form.'),
  59. '#default_value' => $this->options['submit_button'],
  60. '#required' => TRUE,
  61. );
  62. $form['reset_button'] = array (
  63. '#type' => 'checkbox',
  64. '#title' => t('Include reset button'),
  65. '#description' => t('If checked the exposed form will provide a button to reset all the applied exposed filters'),
  66. '#default_value' => $this->options['reset_button'],
  67. );
  68. $form['reset_button_label'] = array(
  69. '#type' => 'textfield',
  70. '#title' => t('Reset button label'),
  71. '#description' => t('Text to display in the reset button of the exposed form.'),
  72. '#default_value' => $this->options['reset_button_label'],
  73. '#required' => TRUE,
  74. '#dependency' => array(
  75. 'edit-exposed-form-options-reset-button' => array(1)
  76. ),
  77. );
  78. $form['exposed_sorts_label'] = array(
  79. '#type' => 'textfield',
  80. '#title' => t('Exposed sorts label'),
  81. '#description' => t('Text to display as the label of the exposed sort select box.'),
  82. '#default_value' => $this->options['exposed_sorts_label'],
  83. '#required' => TRUE,
  84. );
  85. $form['expose_sort_order'] = array(
  86. '#type' => 'checkbox',
  87. '#title' => t('Expose sort order'),
  88. '#description' => t('Allow the user to choose the sort order. If sort order is not exposed, the sort criteria settings for each sort will determine its order.'),
  89. '#default_value' => $this->options['expose_sort_order'],
  90. );
  91. $form['sort_asc_label'] = array(
  92. '#type' => 'textfield',
  93. '#title' => t('Ascending'),
  94. '#description' => t('Text to use when exposed sort is ordered ascending.'),
  95. '#default_value' => $this->options['sort_asc_label'],
  96. '#required' => TRUE,
  97. '#dependency' => array('edit-exposed-form-options-expose-sort-order' => array(TRUE)),
  98. );
  99. $form['sort_desc_label'] = array(
  100. '#type' => 'textfield',
  101. '#title' => t('Descending'),
  102. '#description' => t('Text to use when exposed sort is ordered descending.'),
  103. '#default_value' => $this->options['sort_desc_label'],
  104. '#required' => TRUE,
  105. '#dependency' => array('edit-exposed-form-options-expose-sort-order' => array(TRUE)),
  106. );
  107. $form['autosubmit'] = array(
  108. '#type' => 'checkbox',
  109. '#title' => t('Autosubmit'),
  110. '#description' => t('Automatically submit the form once an element is changed.'),
  111. '#default_value' => $this->options['autosubmit'],
  112. );
  113. $form['autosubmit_hide'] = array(
  114. '#type' => 'checkbox',
  115. '#title' => t('Hide submit button'),
  116. '#description' => t('Hide submit button if javascript is enabled.'),
  117. '#default_value' => $this->options['autosubmit_hide'],
  118. '#dependency' => array(
  119. 'edit-exposed-form-options-autosubmit' => array(1),
  120. ),
  121. );
  122. }
  123. /**
  124. * Render the exposed filter form.
  125. *
  126. * This actually does more than that; because it's using FAPI, the form will
  127. * also assign data to the appropriate handlers for use in building the
  128. * query.
  129. */
  130. public function render_exposed_form($block = FALSE) {
  131. // Deal with any exposed filters we may have, before building.
  132. $form_state = array(
  133. 'view' => &$this->view,
  134. 'display' => &$this->display,
  135. 'method' => 'get',
  136. 'rerender' => TRUE,
  137. 'no_redirect' => TRUE,
  138. 'always_process' => TRUE,
  139. );
  140. // Some types of displays (eg. attachments) may wish to use the exposed
  141. // filters of their parent displays instead of showing an additional
  142. // exposed filter form for the attachment as well as that for the parent.
  143. if (!$this->view->display_handler->displays_exposed() || (!$block && $this->view->display_handler->get_option('exposed_block'))) {
  144. unset($form_state['rerender']);
  145. }
  146. if (!empty($this->ajax)) {
  147. $form_state['ajax'] = TRUE;
  148. }
  149. $form_state['exposed_form_plugin'] = $this;
  150. $form = drupal_build_form('views_exposed_form', $form_state);
  151. $output = drupal_render($form);
  152. if (!$this->view->display_handler->displays_exposed() || (!$block && $this->view->display_handler->get_option('exposed_block'))) {
  153. return "";
  154. }
  155. else {
  156. return $output;
  157. }
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function query() {
  163. $view = $this->view;
  164. $exposed_data = isset($view->exposed_data) ? $view->exposed_data : array();
  165. $sort_by = isset($exposed_data['sort_by']) ? $exposed_data['sort_by'] : NULL;
  166. if (!empty($sort_by) && $this->view->style_plugin->build_sort()) {
  167. // Make sure the original order of sorts is preserved
  168. // (e.g. a sticky sort is often first)
  169. if (isset($view->sort[$sort_by])) {
  170. $view->query->orderby = array();
  171. foreach ($view->sort as $key => $sort) {
  172. if (!$sort->is_exposed()) {
  173. $sort->query();
  174. }
  175. elseif ($key == $sort_by) {
  176. if (isset($exposed_data['sort_order']) && in_array($exposed_data['sort_order'], array('ASC', 'DESC'))) {
  177. $sort->options['order'] = $exposed_data['sort_order'];
  178. }
  179. $sort->set_relationship();
  180. $sort->query();
  181. }
  182. }
  183. }
  184. }
  185. }
  186. /**
  187. * {@inheritdoc}
  188. */
  189. public function pre_render($values) {
  190. }
  191. /**
  192. * {@inheritdoc}
  193. */
  194. public function post_render(&$output) {
  195. }
  196. /**
  197. * {@inheritdoc}
  198. */
  199. public function pre_execute() {
  200. }
  201. /**
  202. * {@inheritdoc}
  203. */
  204. public function post_execute() {
  205. }
  206. /**
  207. * {@inheritdoc}
  208. */
  209. public function exposed_form_alter(&$form, &$form_state) {
  210. if (!empty($this->options['reset_button'])) {
  211. $form['reset'] = array(
  212. '#value' => $this->options['reset_button_label'],
  213. '#type' => 'submit',
  214. );
  215. }
  216. $form['submit']['#value'] = $this->options['submit_button'];
  217. // Check if there is exposed sorts for this view
  218. $exposed_sorts = array();
  219. foreach ($this->view->sort as $id => $handler) {
  220. if ($handler->can_expose() && $handler->is_exposed()) {
  221. $exposed_sorts[$id] = check_plain($handler->options['expose']['label']);
  222. }
  223. }
  224. if (count($exposed_sorts)) {
  225. $form['sort_by'] = array(
  226. '#type' => 'select',
  227. '#options' => $exposed_sorts,
  228. '#title' => $this->options['exposed_sorts_label'],
  229. );
  230. $sort_order = array(
  231. 'ASC' => $this->options['sort_asc_label'],
  232. 'DESC' => $this->options['sort_desc_label'],
  233. );
  234. if (isset($form_state['input']['sort_by']) && isset($this->view->sort[$form_state['input']['sort_by']])) {
  235. $default_sort_order = $this->view->sort[$form_state['input']['sort_by']]->options['order'];
  236. }
  237. else {
  238. $first_sort = reset($this->view->sort);
  239. $default_sort_order = $first_sort->options['order'];
  240. }
  241. if (!isset($form_state['input']['sort_by'])) {
  242. $keys = array_keys($exposed_sorts);
  243. $form_state['input']['sort_by'] = array_shift($keys);
  244. }
  245. if ($this->options['expose_sort_order']) {
  246. $form['sort_order'] = array(
  247. '#type' => 'select',
  248. '#options' => $sort_order,
  249. '#title' => t('Order'),
  250. '#default_value' => $default_sort_order,
  251. );
  252. }
  253. $form['submit']['#weight'] = 10;
  254. if (isset($form['reset'])) {
  255. $form['reset']['#weight'] = 10;
  256. }
  257. }
  258. $pager = $this->view->display_handler->get_plugin('pager');
  259. if ($pager) {
  260. $pager->exposed_form_alter($form, $form_state);
  261. $form_state['pager_plugin'] = $pager;
  262. }
  263. // Apply autosubmit values.
  264. if (!empty($this->options['autosubmit'])) {
  265. $form = array_merge_recursive($form, array('#attributes' => array('class' => array('ctools-auto-submit-full-form'))));
  266. $form['submit']['#attributes']['class'][] = 'ctools-use-ajax';
  267. $form['submit']['#attributes']['class'][] = 'ctools-auto-submit-click';
  268. $form['#attached']['js'][] = drupal_get_path('module', 'ctools') . '/js/auto-submit.js';
  269. if (!empty($this->options['autosubmit_hide'])) {
  270. $form['submit']['#attributes']['class'][] = 'js-hide';
  271. }
  272. }
  273. }
  274. /**
  275. * {@inheritdoc}
  276. */
  277. public function exposed_form_validate(&$form, &$form_state) {
  278. if (isset($form_state['pager_plugin'])) {
  279. $form_state['pager_plugin']->exposed_form_validate($form, $form_state);
  280. }
  281. }
  282. /**
  283. * This function is executed when exposed form is submited.
  284. *
  285. * @param array $form
  286. * Nested array of form elements that comprise the form.
  287. * @param array $form_state
  288. * A keyed array containing the current state of the form.
  289. * @param array $exclude
  290. * Nested array of keys to exclude of insert into $view->exposed_raw_input.
  291. */
  292. public function exposed_form_submit(&$form, &$form_state, &$exclude) {
  293. if (!empty($form_state['values']['op']) && $form_state['values']['op'] == $this->options['reset_button_label']) {
  294. $this->reset_form($form, $form_state);
  295. }
  296. if (isset($form_state['pager_plugin'])) {
  297. $form_state['pager_plugin']->exposed_form_submit($form, $form_state, $exclude);
  298. $exclude[] = 'pager_plugin';
  299. }
  300. }
  301. /**
  302. * {@inheritdoc}
  303. */
  304. public function reset_form(&$form, &$form_state) {
  305. // _SESSION is not defined for users who are not logged in.
  306. // If filters are not overridden, store the 'remember' settings on the
  307. // default display. If they are, store them on this display. This way,
  308. // multiple displays in the same view can share the same filters and
  309. // remember settings.
  310. $display_id = ($this->view->display_handler->is_defaulted('filters')) ? 'default' : $this->view->current_display;
  311. if (isset($_SESSION['views'][$this->view->name][$display_id])) {
  312. unset($_SESSION['views'][$this->view->name][$display_id]);
  313. }
  314. // Set the form to allow redirect.
  315. if (empty($this->view->live_preview)) {
  316. $form_state['no_redirect'] = FALSE;
  317. }
  318. else {
  319. $form_state['rebuild'] = TRUE;
  320. $this->view->exposed_data = array();
  321. }
  322. $form_state['redirect'] = current_path();
  323. $form_state['values'] = array();
  324. }
  325. }
  326. /**
  327. * @}
  328. */