1 | <?php |
---|
2 | /** |
---|
3 | * @defgroup views_sort_handlers Views' sort handlers |
---|
4 | * @{ |
---|
5 | * Handlers to tell Views how to sort queries |
---|
6 | */ |
---|
7 | |
---|
8 | /** |
---|
9 | * Base sort handler that has no options and performs a simple sort |
---|
10 | */ |
---|
11 | class views_handler_sort extends views_handler { |
---|
12 | |
---|
13 | /** |
---|
14 | * Determine if a sort can be exposed. |
---|
15 | */ |
---|
16 | function can_expose() { return TRUE; } |
---|
17 | |
---|
18 | /** |
---|
19 | * Called to add the sort to a query. |
---|
20 | */ |
---|
21 | function query() { |
---|
22 | $this->ensure_my_table(); |
---|
23 | // Add the field. |
---|
24 | $this->query->add_orderby($this->table_alias, $this->real_field, $this->options['order']); |
---|
25 | } |
---|
26 | |
---|
27 | function option_definition() { |
---|
28 | $options = parent::option_definition(); |
---|
29 | |
---|
30 | $options['order'] = array('default' => 'ASC'); |
---|
31 | $options['exposed'] = array('default' => FALSE); |
---|
32 | $options['expose'] = array( |
---|
33 | 'contains' => array( |
---|
34 | 'label' => array('default' => '', 'translatable' => TRUE), |
---|
35 | ), |
---|
36 | ); |
---|
37 | return $options; |
---|
38 | } |
---|
39 | |
---|
40 | /** |
---|
41 | * Display whether or not the sort order is ascending or descending |
---|
42 | */ |
---|
43 | function admin_summary() { |
---|
44 | if (!empty($this->options['exposed'])) { |
---|
45 | return t('Exposed'); |
---|
46 | } |
---|
47 | switch ($this->options['order']) { |
---|
48 | case 'ASC': |
---|
49 | case 'asc': |
---|
50 | default: |
---|
51 | return t('asc'); |
---|
52 | break; |
---|
53 | case 'DESC'; |
---|
54 | case 'desc'; |
---|
55 | return t('desc'); |
---|
56 | break; |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Basic options for all sort criteria |
---|
62 | */ |
---|
63 | function options_form(&$form, &$form_state) { |
---|
64 | parent::options_form($form, $form_state); |
---|
65 | if ($this->can_expose()) { |
---|
66 | $this->show_expose_button($form, $form_state); |
---|
67 | } |
---|
68 | $form['op_val_start'] = array('#value' => '<div class="clear-block">'); |
---|
69 | $this->show_sort_form($form, $form_state); |
---|
70 | $form['op_val_end'] = array('#value' => '</div>'); |
---|
71 | if ($this->can_expose()) { |
---|
72 | $this->show_expose_form($form, $form_state); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * Simple validate handler |
---|
78 | */ |
---|
79 | function options_validate(&$form, &$form_state) { |
---|
80 | $this->sort_validate($form, $form_state); |
---|
81 | if (!empty($this->options['exposed'])) { |
---|
82 | $this->expose_validate($form, $form_state); |
---|
83 | } |
---|
84 | |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * Simple submit handler |
---|
89 | */ |
---|
90 | function options_submit(&$form, &$form_state) { |
---|
91 | unset($form_state['values']['expose_button']); // don't store this. |
---|
92 | $this->sort_submit($form, $form_state); |
---|
93 | if (!empty($this->options['exposed'])) { |
---|
94 | $this->expose_submit($form, $form_state); |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Shortcut to display the value form. |
---|
100 | */ |
---|
101 | function show_sort_form(&$form, &$form_state) { |
---|
102 | $options = $this->sort_options(); |
---|
103 | if (!empty($options)) { |
---|
104 | $form['order'] = array( |
---|
105 | '#type' => 'radios', |
---|
106 | '#options' => $options, |
---|
107 | '#default_value' => $this->options['order'], |
---|
108 | ); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | function sort_validate(&$form, &$form_state) { } |
---|
113 | |
---|
114 | function sort_submit(&$form, &$form_state) { } |
---|
115 | |
---|
116 | /** |
---|
117 | * Provide a list of options for the default sort form. |
---|
118 | * Should be overridden by classes that don't override sort_form |
---|
119 | */ |
---|
120 | function sort_options() { |
---|
121 | return array( |
---|
122 | 'ASC' => t('Sort ascending'), |
---|
123 | 'DESC' => t('Sort descending'), |
---|
124 | ); |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * Since all exposed sorts are grouped into one select box. |
---|
129 | * We don't return nothing when views call to exposed_form() |
---|
130 | */ |
---|
131 | function exposed_form(&$form, &$form_state) { } |
---|
132 | |
---|
133 | /** |
---|
134 | * Handle the 'left' side fo the exposed options form. |
---|
135 | */ |
---|
136 | function expose_form_left(&$form, &$form_state) { |
---|
137 | $form['expose']['label'] = array( |
---|
138 | '#type' => 'textfield', |
---|
139 | '#default_value' => $this->options['expose']['label'], |
---|
140 | '#title' => t('Label'), |
---|
141 | '#required' => TRUE, |
---|
142 | '#size' => 40, |
---|
143 | ); |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * Handle the 'right' side fo the exposed options form. |
---|
148 | */ |
---|
149 | function expose_form_right(&$form, &$form_state) { |
---|
150 | $form['expose']['order'] = array( |
---|
151 | '#type' => 'value', |
---|
152 | '#value' => 'ASC', |
---|
153 | ); |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * Provide default options for exposed sorts. |
---|
158 | */ |
---|
159 | function expose_options() { |
---|
160 | $this->options['expose'] = array( |
---|
161 | 'order' => $this->options['order'], |
---|
162 | 'label' => $this->ui_name(), |
---|
163 | ); |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * A special handler to take the place of missing or broken handlers. |
---|
169 | * |
---|
170 | * @ingroup views_sort_handlers |
---|
171 | */ |
---|
172 | class views_handler_sort_broken extends views_handler_sort { |
---|
173 | function ui_name($short = FALSE) { |
---|
174 | return t('Broken/missing handler'); |
---|
175 | } |
---|
176 | |
---|
177 | function ensure_my_table() { /* No table to ensure! */ } |
---|
178 | function query() { /* No query to run */ } |
---|
179 | function options_form(&$form, &$form_state) { |
---|
180 | $form['markup'] = array( |
---|
181 | '#prefix' => '<div class="form-item description">', |
---|
182 | '#value' => 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.'), |
---|
183 | ); |
---|
184 | } |
---|
185 | |
---|
186 | /** |
---|
187 | * Determine if the handler is considered 'broken' |
---|
188 | */ |
---|
189 | function broken() { return TRUE; } |
---|
190 | } |
---|
191 | |
---|
192 | |
---|
193 | /** |
---|
194 | * @} |
---|
195 | */ |
---|