| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: views_plugin_style_table.inc,v 1.7.2.3 2010/03/11 00:06:18 merlinofchaos Exp $ 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 function option_definition() { 15 $options = parent::option_definition(); 16 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'); 23 24 return $options; 25 } 26 27 /** 28 * Determine if we should provide sorting based upon $_GET inputs. 29 */ 30 function build_sort() { 31 if (!isset($_GET['order']) && ($this->options['default'] == -1 || empty($this->view->field[$this->options['default']]))) { 32 return TRUE; 33 } 34 35 // If a sort we don't know anything about gets through, exit gracefully. 36 if (isset($_GET['order']) && empty($this->view->field[$_GET['order']])) { 37 return TRUE; 38 } 39 40 // Let the builder know whether or not we're overriding the default sorts. 41 return empty($this->options['override']); 42 } 43 44 /** 45 * Add our actual sort criteria 46 */ 47 function build_sort_post() { 48 if (!isset($_GET['order'])) { 49 // check for a 'default' clicksort. If there isn't one, exit gracefully. 50 if (empty($this->options['default'])) { 51 return; 52 } 53 $sort = $this->options['default']; 54 $this->order = !empty($this->options['order']) ? $this->options['order'] : 'asc'; 55 } 56 else { 57 $sort = $_GET['order']; 58 // Store the $order for later use. 59 $this->order = !empty($_GET['sort']) ? strtolower($_GET['sort']) : 'asc'; 60 } 61 62 // If a sort we don't know anything about gets through, exit gracefully. 63 if (empty($this->view->field[$sort])) { 64 return; 65 } 66 67 // Ensure $this->order is valid. 68 if ($this->order != 'asc' && $this->order != 'desc') { 69 $this->order = 'asc'; 70 } 71 72 // Store the $sort for later use. 73 $this->active = $sort; 74 75 // Tell the field to click sort. 76 $this->view->field[$sort]->click_sort($this->order); 77 } 78 79 /** 80 * Normalize a list of columns based upon the fields that are 81 * available. This compares the fields stored in the style handler 82 * to the list of fields actually in the view, removing fields that 83 * have been removed and adding new fields in their own column. 84 * 85 * - Each field must be in a column. 86 * - Each column must be based upon a field, and that field 87 * is somewhere in the column. 88 * - Any fields not currently represented must be added. 89 * - Columns must be re-ordered to match the fields. 90 * 91 * @param $columns 92 * An array of all fields; the key is the id of the field and the 93 * value is the id of the column the field should be in. 94 * @param $fields 95 * The fields to use for the columns. If not provided, they will 96 * be requested from the current display. The running render should 97 * send the fields through, as they may be different than what the 98 * display has listed due to access control or other changes. 99 */ 100 function sanitize_columns($columns, $fields = NULL) { 101 $sanitized = array(); 102 if ($fields === NULL) { 103 $fields = $this->display->handler->get_option('fields'); 104 } 105 106 // Preconfigure the sanitized array so that the order is retained. 107 foreach ($fields as $field => $info) { 108 // Set to itself so that if it isn't touched, it gets column 109 // status automatically. 110 $sanitized[$field] = $field; 111 } 112 113 foreach ($columns as $field => $column) { 114 // first, make sure the field still exists. 115 if (!isset($sanitized[$field])) { 116 continue; 117 } 118 119 // If the field is the column, mark it so, or the column 120 // it's set to is a column, that's ok 121 if ($field == $column || $columns[$column] == $column && !empty($sanitized[$column])) { 122 $sanitized[$field] = $column; 123 } 124 // Since we set the field to itself initially, ignoring 125 // the condition is ok; the field will get its column 126 // status back. 127 } 128 129 return $sanitized; 130 } 131 132 /** 133 * Render the given style. 134 */ 135 function options_form(&$form, &$form_state) { 136 parent::options_form($form, $form_state); 137 $handlers = $this->display->handler->get_handlers('field'); 138 if (empty($handlers)) { 139 $form['error_markup'] = array( 140 '#value' => t('You need at least one field before you can configure your table settings'), 141 '#prefix' => '<div class="error form-item description">', 142 '#suffix' => '</div>', 143 ); 144 return; 145 } 146 147 $form['override'] = array( 148 '#type' => 'checkbox', 149 '#title' => t('Override normal sorting if click sorting is used'), 150 '#default_value' => !empty($this->options['override']), 151 ); 152 153 $form['sticky'] = array( 154 '#type' => 'checkbox', 155 '#title' => t('Enable Drupal style "sticky" table headers (Javascript)'), 156 '#default_value' => !empty($this->options['sticky']), 157 '#description' => t('(Sticky header effects will not be active for preview below, only on live output.)'), 158 ); 159 160 $form['order'] = array( 161 '#type' => 'select', 162 '#title' => t('Default sort order'), 163 '#options' => array('asc' => t('Ascending'), 'desc' => t('Descending')), 164 '#default_value' => $this->options['order'], 165 '#description' => t('If a default sort order is selected, what order should it use by default.'), 166 ); 167 168 // Note: views UI registers this theme handler on our behalf. Your module 169 // will have to register your theme handlers if you do stuff like this. 170 $form['#theme'] = 'views_ui_style_plugin_table'; 171 172 $columns = $this->sanitize_columns($this->options['columns']); 173 174 // Create an array of allowed columns from the data we know: 175 $field_names = $this->display->handler->get_field_labels(); 176 177 if (isset($this->options['default'])) { 178 $default = $this->options['default']; 179 if (!isset($columns[$default])) { 180 $default = -1; 181 } 182 } 183 else { 184 $default = -1; 185 } 186 187 foreach ($columns as $field => $column) { 188 $safe = str_replace(array('][', '_', ' '), '-', $field); 189 // the $id of the column for dependency checking. 190 $id = 'edit-style-options-columns-' . $safe; 191 192 $form['columns'][$field] = array( 193 '#type' => 'select', 194 '#options' => $field_names, 195 '#default_value' => $column, 196 ); 197 if ($handlers[$field]->click_sortable()) { 198 $form['info'][$field]['sortable'] = array( 199 '#type' => 'checkbox', 200 '#default_value' => !empty($this->options['info'][$field]['sortable']), 201 '#process' => array('views_process_dependency'), 202 '#dependency' => array($id => array($field)), 203 ); 204 // Provide an ID so we can have such things. 205 $radio_id = form_clean_id('edit-default-' . $field); 206 $form['default'][$field] = array( 207 '#type' => 'radio', 208 '#return_value' => $field, 209 '#parents' => array('style_options', 'default'), 210 '#id' => $radio_id, 211 // because 'radio' doesn't fully support '#id' =( 212 '#attributes' => array('id' => $radio_id), 213 '#default_value' => $default, 214 '#process' => array('views_process_dependency'), 215 '#dependency' => array($id => array($field)), 216 ); 217 } 218 $form['info'][$field]['separator'] = array( 219 '#type' => 'textfield', 220 '#size' => 10, 221 '#default_value' => isset($this->options['info'][$field]['separator']) ? $this->options['info'][$field]['separator'] : '', 222 '#process' => array('views_process_dependency'), 223 '#dependency' => array($id => array($field)), 224 ); 225 226 // markup for the field name 227 $form['info'][$field]['name'] = array( 228 '#value' => $field_names[$field], 229 ); 230 } 231 232 // Provide a radio for no default sort 233 $form['default'][-1] = array( 234 '#type' => 'radio', 235 '#return_value' => -1, 236 '#parents' => array('style_options', 'default'), 237 '#id' => 'edit-default-0', 238 '#default_value' => $default, 239 ); 240 241 $form['description_markup'] = array( 242 '#prefix' => '<div class="description form-item">', 243 '#suffix' => '</div>', 244 '#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.'), 245 ); 246 } 247 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |