• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • File List
  • File Members

plugins/views_plugin_style_table.inc

Go to the documentation of this file.
00001 <?php
00012 class views_plugin_style_table extends views_plugin_style {
00013 
00018   public $active;
00019 
00024   public $order;
00025 
00026   function option_definition() {
00027     $options = parent::option_definition();
00028 
00029     $options['columns'] = array('default' => array());
00030     $options['default'] = array('default' => '');
00031     $options['info'] = array('default' => array());
00032     $options['override'] = array('default' => TRUE);
00033     $options['sticky'] = array('default' => FALSE);
00034     $options['order'] = array('default' => 'asc');
00035     $options['summary'] = array('default' => '', 'translatable' => TRUE);
00036     $options['empty_table'] = array('default' => FALSE);
00037 
00038     return $options;
00039   }
00040 
00046   function build_sort() {
00047     if (!isset($_GET['order']) && ($this->options['default'] == -1 || empty($this->view->field[$this->options['default']]))) {
00048       return TRUE;
00049     }
00050 
00051     // If a sort we don't know anything about gets through, exit gracefully.
00052     if (isset($_GET['order']) && empty($this->view->field[$_GET['order']])) {
00053       return TRUE;
00054     }
00055 
00056     // Let the builder know whether or not we're overriding the default sorts.
00057     return empty($this->options['override']);
00058   }
00059 
00063   function build_sort_post() {
00064     if (!isset($_GET['order'])) {
00065       // check for a 'default' clicksort. If there isn't one, exit gracefully.
00066       if (empty($this->options['default'])) {
00067         return;
00068       }
00069       $sort = $this->options['default'];
00070       if (!empty($this->options['info'][$sort]['default_sort_order'])) {
00071         $this->order = $this->options['info'][$sort]['default_sort_order'];
00072       }
00073       else {
00074         $this->order = !empty($this->options['order']) ? $this->options['order'] : 'asc';
00075       }
00076     }
00077     else {
00078       $sort = $_GET['order'];
00079       // Store the $order for later use.
00080       $this->order = !empty($_GET['sort']) ? strtolower($_GET['sort']) : 'asc';
00081     }
00082 
00083     // If a sort we don't know anything about gets through, exit gracefully.
00084     if (empty($this->view->field[$sort])) {
00085       return;
00086     }
00087 
00088     // Ensure $this->order is valid.
00089     if ($this->order != 'asc' && $this->order != 'desc') {
00090       $this->order = 'asc';
00091     }
00092 
00093     // Store the $sort for later use.
00094     $this->active = $sort;
00095 
00096     // Tell the field to click sort.
00097     $this->view->field[$sort]->click_sort($this->order);
00098   }
00099 
00124   function sanitize_columns($columns, $fields = NULL) {
00125     $sanitized = array();
00126     if ($fields === NULL) {
00127       $fields = $this->display->handler->get_option('fields');
00128     }
00129     // Preconfigure the sanitized array so that the order is retained.
00130     foreach ($fields as $field => $info) {
00131       // Set to itself so that if it isn't touched, it gets column
00132       // status automatically.
00133       $sanitized[$field] = $field;
00134     }
00135 
00136     foreach ($columns as $field => $column) {
00137       // first, make sure the field still exists.
00138       if (!isset($sanitized[$field])) {
00139         continue;
00140       }
00141 
00142       // If the field is the column, mark it so, or the column
00143       // it's set to is a column, that's ok
00144       if ($field == $column || $columns[$column] == $column && !empty($sanitized[$column])) {
00145         $sanitized[$field] = $column;
00146       }
00147       // Since we set the field to itself initially, ignoring
00148       // the condition is ok; the field will get its column
00149       // status back.
00150     }
00151 
00152     return $sanitized;
00153   }
00154 
00158   function options_form(&$form, &$form_state) {
00159     parent::options_form($form, $form_state);
00160     $handlers = $this->display->handler->get_handlers('field');
00161     if (empty($handlers)) {
00162       $form['error_markup'] = array(
00163         '#markup' => '<div class="error messages">' . t('You need at least one field before you can configure your table settings') . '</div>',
00164       );
00165       return;
00166     }
00167 
00168     $form['override'] = array(
00169       '#type' => 'checkbox',
00170       '#title' => t('Override normal sorting if click sorting is used'),
00171       '#default_value' => !empty($this->options['override']),
00172     );
00173 
00174     $form['sticky'] = array(
00175       '#type' => 'checkbox',
00176       '#title' => t('Enable Drupal style "sticky" table headers (Javascript)'),
00177       '#default_value' => !empty($this->options['sticky']),
00178       '#description' => t('(Sticky header effects will not be active for preview below, only on live output.)'),
00179     );
00180 
00181     $form['summary'] = array(
00182       '#type' => 'textfield',
00183       '#title' => t('Table summary'),
00184       '#description' => t('This value will be displayed as table-summary attribute in the html. Set this for better accessiblity of your site.'),
00185       '#default_value' => $this->options['summary'],
00186       '#maxlength' => 255,
00187     );
00188 
00189     // Note: views UI registers this theme handler on our behalf. Your module
00190     // will have to register your theme handlers if you do stuff like this.
00191     $form['#theme'] = 'views_ui_style_plugin_table';
00192 
00193     $columns = $this->sanitize_columns($this->options['columns']);
00194 
00195     // Create an array of allowed columns from the data we know:
00196     $field_names = $this->display->handler->get_field_labels();
00197 
00198     if (isset($this->options['default'])) {
00199       $default = $this->options['default'];
00200       if (!isset($columns[$default])) {
00201         $default = -1;
00202       }
00203     }
00204     else {
00205       $default = -1;
00206     }
00207 
00208     foreach ($columns as $field => $column) {
00209       $safe = str_replace(array('][', '_', ' '), '-', $field);
00210       // the $id of the column for dependency checking.
00211       $id = 'edit-style-options-columns-' . $safe;
00212 
00213       $form['columns'][$field] = array(
00214         '#type' => 'select',
00215         '#options' => $field_names,
00216         '#default_value' => $column,
00217       );
00218       if ($handlers[$field]->click_sortable()) {
00219         $form['info'][$field]['sortable'] = array(
00220           '#type' => 'checkbox',
00221           '#default_value' => !empty($this->options['info'][$field]['sortable']),
00222           '#dependency' => array($id => array($field)),
00223         );
00224         $form['info'][$field]['default_sort_order'] = array(
00225           '#type' => 'select',
00226           '#options' => array('asc' => t('Ascending'), 'desc' => t('Descending')),
00227           '#default_value' => !empty($this->options['info'][$field]['default_sort_order']) ? $this->options['info'][$field]['default_sort_order'] : 'asc',
00228           '#dependency_count' => 2,
00229           '#dependency' => array($id => array($field), 'edit-style-options-info-' . $safe . '-sortable' => array(1)),
00230         );
00231         // Provide an ID so we can have such things.
00232         $radio_id = drupal_html_id('edit-default-' . $field);
00233         $form['default'][$field] = array(
00234           '#type' => 'radio',
00235           '#return_value' => $field,
00236           '#parents' => array('style_options', 'default'),
00237           '#id' => $radio_id,
00238           // because 'radio' doesn't fully support '#id' =(
00239           '#attributes' => array('id' => $radio_id),
00240           '#default_value' => $default,
00241           '#dependency' => array($id => array($field)),
00242         );
00243       }
00244       $form['info'][$field]['align'] = array(
00245         '#type' => 'select',
00246         '#default_value' => !empty($this->options['info'][$field]['align']) ? $this->options['info'][$field]['align'] : '',
00247         '#options' => array(
00248           '' => t('None'),
00249           'views-align-left' => t('Left'),
00250           'views-align-center' => t('Center'),
00251           'views-align-right' => t('Right'),
00252           ),
00253         '#dependency' => array($id => array($field)),
00254       );
00255       $form['info'][$field]['separator'] = array(
00256         '#type' => 'textfield',
00257         '#size' => 10,
00258         '#default_value' => isset($this->options['info'][$field]['separator']) ? $this->options['info'][$field]['separator'] : '',
00259         '#dependency' => array($id => array($field)),
00260       );
00261       $form['info'][$field]['empty_column'] = array(
00262         '#type' => 'checkbox',
00263         '#default_value' => isset($this->options['info'][$field]['empty_column']) ? $this->options['info'][$field]['empty_column'] : FALSE,
00264         '#dependency' => array($id => array($field)),
00265       );
00266 
00267       // markup for the field name
00268       $form['info'][$field]['name'] = array(
00269         '#markup' => $field_names[$field],
00270       );
00271     }
00272 
00273     // Provide a radio for no default sort
00274     $form['default'][-1] = array(
00275       '#type' => 'radio',
00276       '#return_value' => -1,
00277       '#parents' => array('style_options', 'default'),
00278       '#id' => 'edit-default-0',
00279       '#default_value' => $default,
00280     );
00281 
00282     $form['empty_table'] = array(
00283       '#type' => 'checkbox',
00284       '#title' => t('Show the empty text in the table'),
00285       '#default_value' => $this->options['empty_table'],
00286       '#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.'),
00287     );
00288 
00289     $form['description_markup'] = array(
00290       '#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>',
00291     );
00292   }
00293 
00294   function even_empty() {
00295     return parent::even_empty() || !empty($this->options['empty_table']);
00296   }
00297 }

Generated on Sun Feb 26 2012 12:52:52 for Views by  doxygen 1.7.1