5 * Contains the table style plugin.
9 * Style plugin to render each item as a row in a table.
11 * @ingroup views_style_plugins
13 class views_plugin_style_table extends views_plugin_style {
16 * Contains the current active sort column.
22 * Contains the current active sort order, either desc or asc.
27 function option_definition() {
28 $options = parent::option_definition();
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);
44 * Determine if we should provide sorting based upon $_GET inputs
48 function build_sort() {
49 if (!isset($_GET['order']) && ($this->options['default'] == -1 || empty($this->view->field[$this->options['default']]))) {
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']])) {
58 // Let the builder know whether or not we're overriding the default sorts.
59 return empty($this->options['override']);
63 * Add our actual sort criteria
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'])) {
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'];
76 $this->order = !empty($this->options['order']) ? $this->options['order'] : 'asc';
80 $sort = $_GET['order'];
81 // Store the $order for later use.
82 $this->order = !empty($_GET['sort']) ? strtolower($_GET['sort']) : 'asc';
85 // If a sort we don't know anything about gets through, exit gracefully.
86 if (empty($this->view->field[$sort])) {
90 // Ensure $this->order is valid.
91 if ($this->order != 'asc' && $this->order != 'desc') {
95 // Store the $sort for later use.
96 $this->active = $sort;
98 // Tell the field to click sort.
99 $this->view->field[$sort]->click_sort($this->order);
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.
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.
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.
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.
124 * An array of all the sanitized columns.
126 function sanitize_columns($columns, $fields = NULL) {
127 $sanitized = array();
128 if ($fields === NULL) {
129 $fields = $this->display->handler->get_option('fields');
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;
138 foreach ($columns as $field => $column) {
139 // first, make sure the field still exists.
140 if (!isset($sanitized[$field])) {
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;
149 // Since we set the field to itself initially, ignoring
150 // the condition is ok; the field will get its column
158 * Render the given style.
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>',
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']),
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.)'),
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'],
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'],
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';
203 $columns = $this->sanitize_columns($this->options['columns']);
205 // Create an array of allowed columns from the data we know:
206 $field_names = $this->display->handler->get_field_labels();
208 if (isset($this->options['default'])) {
209 $default = $this->options['default'];
210 if (!isset($columns[$default])) {
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;
223 $form['columns'][$field] = array(
225 '#options' => $field_names,
226 '#default_value' => $column,
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)),
234 $form['info'][$field]['default_sort_order'] = array(
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)),
241 // Provide an ID so we can have such things.
242 $radio_id = drupal_html_id('edit-default-' . $field);
243 $form['default'][$field] = array(
245 '#return_value' => $field,
246 '#parents' => array('style_options', 'default'),
248 // because 'radio' doesn't fully support '#id' =(
249 '#attributes' => array('id' => $radio_id),
250 '#default_value' => $default,
251 '#dependency' => array($id => array($field)),
254 $form['info'][$field]['align'] = array(
256 '#default_value' => !empty($this->options['info'][$field]['align']) ? $this->options['info'][$field]['align'] : '',
259 'views-align-left' => t('Left'),
260 'views-align-center' => t('Center'),
261 'views-align-right' => t('Right'),
263 '#dependency' => array($id => array($field)),
265 $form['info'][$field]['separator'] = array(
266 '#type' => 'textfield',
268 '#default_value' => isset($this->options['info'][$field]['separator']) ? $this->options['info'][$field]['separator'] : '',
269 '#dependency' => array($id => array($field)),
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)),
277 // markup for the field name
278 $form['info'][$field]['name'] = array(
279 '#markup' => $field_names[$field],
283 // Provide a radio for no default sort
284 $form['default'][-1] = array(
286 '#return_value' => -1,
287 '#parents' => array('style_options', 'default'),
288 '#id' => 'edit-default-0',
289 '#default_value' => $default,
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.'),
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>',
304 function even_empty() {
305 return parent::even_empty() || !empty($this->options['empty_table']);