views_handler_sort.inc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_sort.
  5. */
  6. /**
  7. * @defgroup views_sort_handlers Views sort handlers
  8. * @{
  9. * Handlers to tell Views how to sort queries.
  10. */
  11. /**
  12. * Base sort handler that has no options and performs a simple sort.
  13. *
  14. * @ingroup views_sort_handlers
  15. */
  16. class views_handler_sort extends views_handler {
  17. /**
  18. * Determine if a sort can be exposed.
  19. */
  20. public function can_expose() {
  21. return TRUE;
  22. }
  23. /**
  24. * Called to add the sort to a query.
  25. */
  26. public function query() {
  27. $this->ensure_my_table();
  28. // Add the field.
  29. $this->query->add_orderby($this->table_alias, $this->real_field, $this->options['order']);
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function option_definition() {
  35. $options = parent::option_definition();
  36. $options['order'] = array('default' => 'ASC');
  37. $options['exposed'] = array('default' => FALSE, 'bool' => TRUE);
  38. $options['expose'] = array(
  39. 'contains' => array(
  40. 'label' => array('default' => '', 'translatable' => TRUE),
  41. ),
  42. );
  43. return $options;
  44. }
  45. /**
  46. * Display whether or not the sort order is ascending or descending
  47. */
  48. public function admin_summary() {
  49. if (!empty($this->options['exposed'])) {
  50. return t('Exposed');
  51. }
  52. switch ($this->options['order']) {
  53. case 'ASC':
  54. case 'asc':
  55. default:
  56. return t('asc');
  57. case 'DESC';
  58. case 'desc';
  59. return t('desc');
  60. }
  61. }
  62. /**
  63. * Basic options for all sort criteria
  64. */
  65. public function options_form(&$form, &$form_state) {
  66. parent::options_form($form, $form_state);
  67. if ($this->can_expose()) {
  68. $this->show_expose_button($form, $form_state);
  69. }
  70. $form['op_val_start'] = array('#value' => '<div class="clearfix">');
  71. $this->show_sort_form($form, $form_state);
  72. $form['op_val_end'] = array('#value' => '</div>');
  73. if ($this->can_expose()) {
  74. $this->show_expose_form($form, $form_state);
  75. }
  76. }
  77. /**
  78. * Shortcut to display the expose/hide button.
  79. */
  80. public function show_expose_button(&$form, &$form_state) {
  81. $form['expose_button'] = array(
  82. '#prefix' => '<div class="views-expose clearfix">',
  83. '#suffix' => '</div>',
  84. // Should always come first.
  85. '#weight' => -1000,
  86. );
  87. // Add a checkbox for JS users, which will have behavior attached to it
  88. // so it can replace the button.
  89. $form['expose_button']['checkbox'] = array(
  90. '#theme_wrappers' => array('container'),
  91. '#attributes' => array('class' => array('js-only')),
  92. );
  93. $form['expose_button']['checkbox']['checkbox'] = array(
  94. '#title' => t('Expose this sort to visitors, to allow them to change it'),
  95. '#type' => 'checkbox',
  96. );
  97. // Then add the button itself.
  98. if (empty($this->options['exposed'])) {
  99. $form['expose_button']['markup'] = array(
  100. '#markup' => '<div class="description exposed-description" style="float: left; margin-right:10px">' . t('This sort is not exposed. Expose it to allow the users to change it.') . '</div>',
  101. );
  102. $form['expose_button']['button'] = array(
  103. '#limit_validation_errors' => array(),
  104. '#type' => 'submit',
  105. '#value' => t('Expose sort'),
  106. '#submit' => array('views_ui_config_item_form_expose'),
  107. );
  108. $form['expose_button']['checkbox']['checkbox']['#default_value'] = 0;
  109. }
  110. else {
  111. $form['expose_button']['markup'] = array(
  112. '#markup' => '<div class="description exposed-description">' . t('This sort is exposed. If you hide it, users will not be able to change it.') . '</div>',
  113. );
  114. $form['expose_button']['button'] = array(
  115. '#limit_validation_errors' => array(),
  116. '#type' => 'submit',
  117. '#value' => t('Hide sort'),
  118. '#submit' => array('views_ui_config_item_form_expose'),
  119. );
  120. $form['expose_button']['checkbox']['checkbox']['#default_value'] = 1;
  121. }
  122. }
  123. /**
  124. * Simple validate handler.
  125. */
  126. public function options_validate(&$form, &$form_state) {
  127. $this->sort_validate($form, $form_state);
  128. if (!empty($this->options['exposed'])) {
  129. $this->expose_validate($form, $form_state);
  130. }
  131. }
  132. /**
  133. * Simple submit handler.
  134. */
  135. public function options_submit(&$form, &$form_state) {
  136. // Don't store this.
  137. unset($form_state['values']['expose_button']);
  138. $this->sort_submit($form, $form_state);
  139. if (!empty($this->options['exposed'])) {
  140. $this->expose_submit($form, $form_state);
  141. }
  142. }
  143. /**
  144. * Shortcut to display the value form.
  145. */
  146. public function show_sort_form(&$form, &$form_state) {
  147. $options = $this->sort_options();
  148. if (!empty($options)) {
  149. $form['order'] = array(
  150. '#type' => 'radios',
  151. '#options' => $options,
  152. '#default_value' => $this->options['order'],
  153. );
  154. }
  155. }
  156. /**
  157. * {@inheritdoc}
  158. */
  159. public function sort_validate(&$form, &$form_state) {
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function sort_submit(&$form, &$form_state) {
  165. }
  166. /**
  167. * Provide a list of options for the default sort form.
  168. *
  169. * Should be overridden by classes that don't override sort_form.
  170. */
  171. public function sort_options() {
  172. return array(
  173. 'ASC' => t('Sort ascending'),
  174. 'DESC' => t('Sort descending'),
  175. );
  176. }
  177. /**
  178. * {@inheritdoc}
  179. */
  180. public function expose_form(&$form, &$form_state) {
  181. // #flatten will move everything from $form['expose'][$key] to $form[$key]
  182. // prior to rendering. That's why the pre_render for it needs to run first,
  183. // so that when the next pre_render (the one for fieldsets) runs, it gets
  184. // the flattened data.
  185. array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
  186. $form['expose']['#flatten'] = TRUE;
  187. $form['expose']['label'] = array(
  188. '#type' => 'textfield',
  189. '#default_value' => $this->options['expose']['label'],
  190. '#title' => t('Label'),
  191. '#required' => TRUE,
  192. '#size' => 40,
  193. '#weight' => -1,
  194. );
  195. }
  196. /**
  197. * Provide default options for exposed sorts.
  198. */
  199. public function expose_options() {
  200. $this->options['expose'] = array(
  201. 'order' => $this->options['order'],
  202. 'label' => $this->definition['title'],
  203. );
  204. }
  205. }
  206. /**
  207. * A special handler to take the place of missing or broken handlers.
  208. *
  209. * @ingroup views_sort_handlers
  210. */
  211. class views_handler_sort_broken extends views_handler_sort {
  212. /**
  213. * {@inheritdoc}
  214. */
  215. public function ui_name($short = FALSE) {
  216. return t('Broken/missing handler');
  217. }
  218. /**
  219. * {@inheritdoc}
  220. */
  221. public function ensure_my_table() {
  222. // No table to ensure!
  223. }
  224. /**
  225. * {@inheritdoc}
  226. */
  227. public function query($group_by = FALSE) {
  228. // No query to run.
  229. }
  230. /**
  231. * {@inheritdoc}
  232. */
  233. public function options_form(&$form, &$form_state) {
  234. $form['markup'] = array(
  235. '#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>',
  236. );
  237. }
  238. /**
  239. * Determine if the handler is considered 'broken'.
  240. */
  241. public function broken() {
  242. return TRUE;
  243. }
  244. }
  245. /**
  246. * @}
  247. */