2 // $Id: views_plugin_style_table.inc,v 1.5 2008/10/15 22:25:36 merlinofchaos Exp $
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 {
14 function option_definition() {
15 $options = parent::option_definition();
17 $options['columns'] = array('default' => array());
18 $options['default'] = array('default' => '');
19 $options['info'] = array('default' => array());
20 $options['override'] = array('default' => TRUE);
21 $options['sticky'] = array('default' => FALSE);
22 $options['order'] = array('default' => 'asc');
28 * Determine if we should provide sorting based upon $_GET inputs.
30 function build_sort() {
31 if (!isset($_GET['order'])) {
32 // check for a 'default' clicksort. If there isn't one, exit gracefully.
33 if (empty($this->options['default'])) {
36 $sort = $this->options['default'];
37 $this->order = !empty($this->options['order']) ? $this->options['order'] : 'asc';
40 $sort = $_GET['order'];
41 // Store the $order for later use.
42 $this->order = !empty($_GET['sort']) ? strtolower($_GET['sort']) : 'asc';
45 // If a sort we don't know anything about gets through, exit gracefully.
46 if (empty($this->view->field[$sort])) {
50 // Ensure $this->order is valid.
51 if ($this->order != 'asc' && $this->order != 'desc') {
55 // Store the $sort for later use.
56 $this->active = $sort;
58 // Tell the field to click sort.
59 $this->view->field[$sort]->click_sort($this->order);
61 // Let the builder know whether or not we're overriding the default sorts.
62 return empty($this->options['override']);
66 * Normalize a list of columns based upon the fields that are
67 * available. This compares the fields stored in the style handler
68 * to the list of fields actually in the view, removing fields that
69 * have been removed and adding new fields in their own column.
71 * - Each field must be in a column.
72 * - Each column must be based upon a field, and that field
73 * is somewhere in the column.
74 * - Any fields not currently represented must be added.
75 * - Columns must be re-ordered to match the fields.
78 * An array of all fields; the key is the id of the field and the
79 * value is the id of the column the field should be in.
81 * The fields to use for the columns. If not provided, they will
82 * be requested from the current display. The running render should
83 * send the fields through, as they may be different than what the
84 * display has listed due to access control or other changes.
86 function sanitize_columns($columns, $fields = NULL) {
88 if ($fields === NULL) {
89 $fields = $this->display->handler->get_option('fields');
92 // Preconfigure the sanitized array so that the order is retained.
93 foreach ($fields as $field => $info) {
94 // Set to itself so that if it isn't touched, it gets column
95 // status automatically.
96 $sanitized[$field] = $field;
99 foreach ($columns as $field => $column) {
100 // first, make sure the field still exists.
101 if (!isset($sanitized[$field])) {
105 // If the field is the column, mark it so, or the column
106 // it's set to is a column, that's ok
107 if ($field == $column || $columns[$column] == $column && !empty($sanitized[$column])) {
108 $sanitized[$field] = $column;
110 // Since we set the field to itself initially, ignoring
111 // the condition is ok; the field will get its column
119 * Render the given style.
121 function options_form(&$form, &$form_state) {
122 parent::options_form($form, $form_state);
123 $handlers = $this->display->handler->get_handlers('field');
124 if (empty($handlers)) {
125 $form['error_markup'] = array(
126 '#value' => t('You need at least one field before you can configure your table settings'),
127 '#prefix' => '<div class="error form-item description">',
128 '#suffix' => '</div>',
133 $form['override'] = array(
134 '#type' => 'checkbox',
135 '#title' => t('Override normal sorting if click sorting is used'),
136 '#default_value' => !empty($this->options['override']),
139 $form['sticky'] = array(
140 '#type' => 'checkbox',
141 '#title' => t('Enable Drupal style "sticky" table headers (Javascript)'),
142 '#default_value' => !empty($this->options['sticky']),
143 '#description' => t('(Sticky header effects will not be active for preview below, only on live output.)'),
146 $form['order'] = array(
148 '#title' => t('Default sort order'),
149 '#options' => array('asc' => t('Ascending'), 'desc' => t('Descending')),
150 '#default_value' => $this->options['order'],
151 '#description' => t('If a default sort order is selected, what order should it use by default.'),
154 // Note: views UI registers this theme handler on our behalf. Your module
155 // will have to register your theme handlers if you do stuff like this.
156 $form['#theme'] = 'views_ui_style_plugin_table';
158 $columns = $this->sanitize_columns($this->options['columns']);
160 // Create an array of allowed columns from the data we know:
161 foreach ($handlers as $field => $handler) {
162 if ($label = $handler->label()) {
163 $field_names[$field] = $label;
166 $field_names[$field] = $handler->ui_name();
170 if (isset($this->options['default'])) {
171 $default = $this->options['default'];
172 if (!isset($columns[$default])) {
180 foreach ($columns as $field => $column) {
181 $safe = str_replace(array('][', '_', ' '), '-', $field);
182 // the $id of the column for dependency checking.
183 $id = 'edit-style-options-columns-' . $safe;
185 $form['columns'][$field] = array(
187 '#options' => $field_names,
188 '#default_value' => $column,
190 if ($handlers[$field]->click_sortable()) {
191 $form['info'][$field]['sortable'] = array(
192 '#type' => 'checkbox',
193 '#default_value' => !empty($this->options['info'][$field]['sortable']),
194 '#process' => array('views_process_dependency'),
195 '#dependency' => array($id => array($field)),
197 // Provide an ID so we can have such things.
198 $radio_id = form_clean_id('edit-default-' . $field);
199 $form['default'][$field] = array(
201 '#return_value' => $field,
202 '#parents' => array('style_options', 'default'),
204 // because 'radio' doesn't fully support '#id' =(
205 '#attributes' => array('id' => $radio_id),
206 '#default_value' => $default,
207 '#process' => array('views_process_dependency'),
208 '#dependency' => array($id => array($field)),
211 $form['info'][$field]['separator'] = array(
212 '#type' => 'textfield',
214 '#default_value' => isset($this->options['info'][$field]['separator']) ? $this->options['info'][$field]['separator'] : '',
215 '#process' => array('views_process_dependency'),
216 '#dependency' => array($id => array($field)),
219 // markup for the field name
220 $form['info'][$field]['name'] = array(
221 '#value' => $field_names[$field],
225 // Provide a radio for no default sort
226 $form['default'][-1] = array(
228 '#return_value' => -1,
229 '#parents' => array('style_options', 'default'),
230 '#id' => 'edit-default-0',
231 '#default_value' => $default,
234 $form['description_markup'] = array(
235 '#prefix' => '<div class="description form-item">',
236 '#suffix' => '</div>',
237 '#value' => 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.'),