commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / views / plugins / views_plugin_style_table.inc
1 <?php
2
3 /**
4 * @file
5 * Contains the table style plugin.
6 */
7
8 /**
9 * Style plugin to render each item as a row in a table.
10 *
11 * @ingroup views_style_plugins
12 */
13 class views_plugin_style_table extends views_plugin_style {
14
15 /**
16 * Contains the current active sort column.
17 * @var string
18 */
19 public $active;
20
21 /**
22 * Contains the current active sort order, either desc or asc.
23 * @var string
24 */
25 public $order;
26
27 function option_definition() {
28 $options = parent::option_definition();
29
30 $options['columns'] = array('default' => array());
31 $options['default'] = array('default' => '');
32 $options['info'] = array('default' => array());
33 $options['override'] = array('default' => TRUE, 'bool' => TRUE);
34 $options['sticky'] = array('default' => FALSE, 'bool' => TRUE);
35 $options['order'] = array('default' => 'asc');
36 $options['caption'] = array('default' => '', 'translatable' => TRUE);
37 $options['summary'] = array('default' => '', 'translatable' => TRUE);
38 $options['empty_table'] = array('default' => FALSE, 'bool' => TRUE);
39
40 return $options;
41 }
42
43 /**
44 * Determine if we should provide sorting based upon $_GET inputs
45 *
46 * @return bool
47 */
48 function build_sort() {
49 if (!isset($_GET['order']) && ($this->options['default'] == -1 || empty($this->view->field[$this->options['default']]))) {
50 return TRUE;
51 }
52
53 // If a sort we don't know anything about gets through, exit gracefully.
54 if (isset($_GET['order']) && empty($this->view->field[$_GET['order']])) {
55 return TRUE;
56 }
57
58 // Let the builder know whether or not we're overriding the default sorts.
59 return empty($this->options['override']);
60 }
61
62 /**
63 * Add our actual sort criteria
64 */
65 function build_sort_post() {
66 if (!isset($_GET['order'])) {
67 // check for a 'default' clicksort. If there isn't one, exit gracefully.
68 if (empty($this->options['default'])) {
69 return;
70 }
71 $sort = $this->options['default'];
72 if (!empty($this->options['info'][$sort]['default_sort_order'])) {
73 $this->order = $this->options['info'][$sort]['default_sort_order'];
74 }
75 else {
76 $this->order = !empty($this->options['order']) ? $this->options['order'] : 'asc';
77 }
78 }
79 else {
80 $sort = $_GET['order'];
81 // Store the $order for later use.
82 $this->order = !empty($_GET['sort']) ? strtolower($_GET['sort']) : 'asc';
83 }
84
85 // If a sort we don't know anything about gets through, exit gracefully.
86 if (empty($this->view->field[$sort])) {
87 return;
88 }
89
90 // Ensure $this->order is valid.
91 if ($this->order != 'asc' && $this->order != 'desc') {
92 $this->order = 'asc';
93 }
94
95 // Store the $sort for later use.
96 $this->active = $sort;
97
98 // Tell the field to click sort.
99 $this->view->field[$sort]->click_sort($this->order);
100 }
101
102 /**
103 * Normalize a list of columns based upon the fields that are
104 * available. This compares the fields stored in the style handler
105 * to the list of fields actually in the view, removing fields that
106 * have been removed and adding new fields in their own column.
107 *
108 * - Each field must be in a column.
109 * - Each column must be based upon a field, and that field
110 * is somewhere in the column.
111 * - Any fields not currently represented must be added.
112 * - Columns must be re-ordered to match the fields.
113 *
114 * @param $columns
115 * An array of all fields; the key is the id of the field and the
116 * value is the id of the column the field should be in.
117 * @param $fields
118 * The fields to use for the columns. If not provided, they will
119 * be requested from the current display. The running render should
120 * send the fields through, as they may be different than what the
121 * display has listed due to access control or other changes.
122 *
123 * @return array
124 * An array of all the sanitized columns.
125 */
126 function sanitize_columns($columns, $fields = NULL) {
127 $sanitized = array();
128 if ($fields === NULL) {
129 $fields = $this->display->handler->get_option('fields');
130 }
131 // Preconfigure the sanitized array so that the order is retained.
132 foreach ($fields as $field => $info) {
133 // Set to itself so that if it isn't touched, it gets column
134 // status automatically.
135 $sanitized[$field] = $field;
136 }
137
138 foreach ($columns as $field => $column) {
139 // first, make sure the field still exists.
140 if (!isset($sanitized[$field])) {
141 continue;
142 }
143
144 // If the field is the column, mark it so, or the column
145 // it's set to is a column, that's ok
146 if ($field == $column || $columns[$column] == $column && !empty($sanitized[$column])) {
147 $sanitized[$field] = $column;
148 }
149 // Since we set the field to itself initially, ignoring
150 // the condition is ok; the field will get its column
151 // status back.
152 }
153
154 return $sanitized;
155 }
156
157 /**
158 * Render the given style.
159 */
160 function options_form(&$form, &$form_state) {
161 parent::options_form($form, $form_state);
162 $handlers = $this->display->handler->get_handlers('field');
163 if (empty($handlers)) {
164 $form['error_markup'] = array(
165 '#markup' => '<div class="error messages">' . t('You need at least one field before you can configure your table settings') . '</div>',
166 );
167 return;
168 }
169
170 $form['override'] = array(
171 '#type' => 'checkbox',
172 '#title' => t('Override normal sorting if click sorting is used'),
173 '#default_value' => !empty($this->options['override']),
174 );
175
176 $form['sticky'] = array(
177 '#type' => 'checkbox',
178 '#title' => t('Enable Drupal style "sticky" table headers (Javascript)'),
179 '#default_value' => !empty($this->options['sticky']),
180 '#description' => t('(Sticky header effects will not be active for preview below, only on live output.)'),
181 );
182
183 $form['caption'] = array(
184 '#type' => 'textfield',
185 '#title' => t('Short description of table'),
186 '#description' => t('Include a caption for better accessibility of your table.'),
187 '#default_value' => $this->options['caption'],
188 '#maxlength' => 255,
189 );
190
191 $form['summary'] = array(
192 '#type' => 'textfield',
193 '#title' => t('Table summary'),
194 '#description' => t('This value will be displayed as table-summary attribute in the html. Use this to give a summary of complex tables.'),
195 '#default_value' => $this->options['summary'],
196 '#maxlength' => 255,
197 );
198
199 // Note: views UI registers this theme handler on our behalf. Your module
200 // will have to register your theme handlers if you do stuff like this.
201 $form['#theme'] = 'views_ui_style_plugin_table';
202
203 $columns = $this->sanitize_columns($this->options['columns']);
204
205 // Create an array of allowed columns from the data we know:
206 $field_names = $this->display->handler->get_field_labels();
207
208 if (isset($this->options['default'])) {
209 $default = $this->options['default'];
210 if (!isset($columns[$default])) {
211 $default = -1;
212 }
213 }
214 else {
215 $default = -1;
216 }
217
218 foreach ($columns as $field => $column) {
219 $safe = str_replace(array('][', '_', ' '), '-', $field);
220 // the $id of the column for dependency checking.
221 $id = 'edit-style-options-columns-' . $safe;
222
223 $form['columns'][$field] = array(
224 '#type' => 'select',
225 '#options' => $field_names,
226 '#default_value' => $column,
227 );
228 if ($handlers[$field]->click_sortable()) {
229 $form['info'][$field]['sortable'] = array(
230 '#type' => 'checkbox',
231 '#default_value' => !empty($this->options['info'][$field]['sortable']),
232 '#dependency' => array($id => array($field)),
233 );
234 $form['info'][$field]['default_sort_order'] = array(
235 '#type' => 'select',
236 '#options' => array('asc' => t('Ascending'), 'desc' => t('Descending')),
237 '#default_value' => !empty($this->options['info'][$field]['default_sort_order']) ? $this->options['info'][$field]['default_sort_order'] : 'asc',
238 '#dependency_count' => 2,
239 '#dependency' => array($id => array($field), 'edit-style-options-info-' . $safe . '-sortable' => array(1)),
240 );
241 // Provide an ID so we can have such things.
242 $radio_id = drupal_html_id('edit-default-' . $field);
243 $form['default'][$field] = array(
244 '#type' => 'radio',
245 '#return_value' => $field,
246 '#parents' => array('style_options', 'default'),
247 '#id' => $radio_id,
248 // because 'radio' doesn't fully support '#id' =(
249 '#attributes' => array('id' => $radio_id),
250 '#default_value' => $default,
251 '#dependency' => array($id => array($field)),
252 );
253 }
254 $form['info'][$field]['align'] = array(
255 '#type' => 'select',
256 '#default_value' => !empty($this->options['info'][$field]['align']) ? $this->options['info'][$field]['align'] : '',
257 '#options' => array(
258 '' => t('None'),
259 'views-align-left' => t('Left'),
260 'views-align-center' => t('Center'),
261 'views-align-right' => t('Right'),
262 ),
263 '#dependency' => array($id => array($field)),
264 );
265 $form['info'][$field]['separator'] = array(
266 '#type' => 'textfield',
267 '#size' => 10,
268 '#default_value' => isset($this->options['info'][$field]['separator']) ? $this->options['info'][$field]['separator'] : '',
269 '#dependency' => array($id => array($field)),
270 );
271 $form['info'][$field]['empty_column'] = array(
272 '#type' => 'checkbox',
273 '#default_value' => isset($this->options['info'][$field]['empty_column']) ? $this->options['info'][$field]['empty_column'] : FALSE,
274 '#dependency' => array($id => array($field)),
275 );
276
277 // markup for the field name
278 $form['info'][$field]['name'] = array(
279 '#markup' => $field_names[$field],
280 );
281 }
282
283 // Provide a radio for no default sort
284 $form['default'][-1] = array(
285 '#type' => 'radio',
286 '#return_value' => -1,
287 '#parents' => array('style_options', 'default'),
288 '#id' => 'edit-default-0',
289 '#default_value' => $default,
290 );
291
292 $form['empty_table'] = array(
293 '#type' => 'checkbox',
294 '#title' => t('Show the empty text in the table'),
295 '#default_value' => $this->options['empty_table'],
296 '#description' => t('Per default the table is hidden for an empty view. With this option it is posible to show an empty table with the text in it.'),
297 );
298
299 $form['description_markup'] = array(
300 '#markup' => '<div class="description form-item">' . t('Place fields into columns; you may combine multiple fields into the same column. If you do, the separator in the column specified will be used to separate the fields. Check the sortable box to make that column click sortable, and check the default sort radio to determine which column will be sorted by default, if any. You may control column order and field labels in the fields section.') . '</div>',
301 );
302 }
303
304 function even_empty() {
305 return parent::even_empty() || !empty($this->options['empty_table']);
306 }
307 }