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

handlers/views_handler_sort.inc

00001 <?php
00011 class views_handler_sort extends views_handler {
00012 
00016   function can_expose() { return TRUE; }
00017 
00021   function query() {
00022     $this->ensure_my_table();
00023     // Add the field.
00024     $this->query->add_orderby($this->table_alias, $this->real_field, $this->options['order']);
00025   }
00026 
00027   function option_definition() {
00028     $options = parent::option_definition();
00029 
00030     $options['order'] = array('default' => 'ASC');
00031     $options['exposed'] = array('default' => FALSE);
00032     $options['expose'] = array(
00033       'contains' => array(
00034         'label' => array('default' => '', 'translatable' => TRUE),
00035       ),
00036     );
00037     return $options;
00038   }
00039 
00043   function admin_summary() {
00044     if (!empty($this->options['exposed'])) {
00045       return t('Exposed');
00046     }
00047     switch ($this->options['order']) {
00048       case 'ASC':
00049       case 'asc':
00050       default:
00051         return t('asc');
00052         break;
00053       case 'DESC';
00054       case 'desc';
00055         return t('desc');
00056         break;
00057     }
00058   }
00059 
00063   function options_form(&$form, &$form_state) {
00064     parent::options_form($form, $form_state);
00065     if ($this->can_expose()) {
00066       $this->show_expose_button($form, $form_state);
00067     }
00068     $form['op_val_start'] = array('#value' => '<div class="clearfix">');
00069     $this->show_sort_form($form, $form_state);
00070     $form['op_val_end'] = array('#value' => '</div>');
00071     if ($this->can_expose()) {
00072       $this->show_expose_form($form, $form_state);
00073     }
00074   }
00075 
00079   function show_expose_button(&$form, &$form_state) {
00080     $form['expose_button'] = array(
00081       '#prefix' => '<div class="views-expose clearfix">',
00082       '#suffix' => '</div>',
00083       // Should always come first
00084       '#weight' => -1000,
00085     );
00086 
00087     // Add a checkbox for JS users, which will have behavior attached to it
00088     // so it can replace the button.
00089     $form['expose_button']['checkbox'] = array(
00090       '#theme_wrappers' => array('container'),
00091       '#attributes' => array('class' => array('js-only')),
00092     );
00093     $form['expose_button']['checkbox']['checkbox'] = array(
00094       '#title' => t('Expose this sort to visitors, to allow them to change it'),
00095       '#type' => 'checkbox',
00096     );
00097 
00098     // Then add the button itself.
00099     if (empty($this->options['exposed'])) {
00100       $form['expose_button']['markup'] = array(
00101         '#markup' => '<div class="description exposed-description" style="float: left; margin-right:10px">' . t('This sort is not exposed. Expose it to allow the users to change it.') . '</div>',
00102       );
00103       $form['expose_button']['button'] = array(
00104         '#limit_validation_errors' => array(),
00105         '#type' => 'submit',
00106         '#value' => t('Expose sort'),
00107         '#submit' => array('views_ui_config_item_form_expose'),
00108       );
00109       $form['expose_button']['checkbox']['checkbox']['#default_value'] = 0;
00110     }
00111     else {
00112       $form['expose_button']['markup'] = array(
00113         '#markup' => '<div class="description exposed-description">' . t('This sort is exposed. If you hide it, users will not be able to change it.') . '</div>',
00114       );
00115       $form['expose_button']['button'] = array(
00116         '#limit_validation_errors' => array(),
00117         '#type' => 'submit',
00118         '#value' => t('Hide sort'),
00119         '#submit' => array('views_ui_config_item_form_expose'),
00120       );
00121       $form['expose_button']['checkbox']['checkbox']['#default_value'] = 1;
00122     }
00123   }
00124 
00128   function options_validate(&$form, &$form_state) {
00129     $this->sort_validate($form, $form_state);
00130     if (!empty($this->options['exposed'])) {
00131       $this->expose_validate($form, $form_state);
00132     }
00133 
00134   }
00135 
00139   function options_submit(&$form, &$form_state) {
00140     unset($form_state['values']['expose_button']); // don't store this.
00141     $this->sort_submit($form, $form_state);
00142     if (!empty($this->options['exposed'])) {
00143       $this->expose_submit($form, $form_state);
00144     }
00145   }
00146 
00150   function show_sort_form(&$form, &$form_state) {
00151     $options = $this->sort_options();
00152     if (!empty($options)) {
00153       $form['order'] = array(
00154         '#type' => 'radios',
00155         '#options' => $options,
00156         '#default_value' => $this->options['order'],
00157       );
00158     }
00159   }
00160 
00161   function sort_validate(&$form, &$form_state) { }
00162 
00163   function sort_submit(&$form, &$form_state) { }
00164 
00169   function sort_options() {
00170     return array(
00171       'ASC' => t('Sort ascending'),
00172       'DESC' => t('Sort descending'),
00173     );
00174   }
00175 
00176   function expose_form(&$form, &$form_state) {
00177     // #flatten will move everything from $form['expose'][$key] to $form[$key]
00178     // prior to rendering. That's why the pre_render for it needs to run first,
00179     // so that when the next pre_render (the one for fieldsets) runs, it gets
00180     // the flattened data.
00181     array_unshift($form['#pre_render'], 'views_ui_pre_render_flatten_data');
00182     $form['expose']['#flatten'] = TRUE;
00183 
00184     $form['expose']['label'] = array(
00185       '#type' => 'textfield',
00186       '#default_value' => $this->options['expose']['label'],
00187       '#title' => t('Label'),
00188       '#required' => TRUE,
00189       '#size' => 40,
00190       '#weight' => -1,
00191    );
00192   }
00193 
00197   function expose_options() {
00198     $this->options['expose'] = array(
00199       'order' => $this->options['order'],
00200       'label' => $this->definition['title'],
00201     );
00202   }
00203 }
00204 
00210 class views_handler_sort_broken extends views_handler_sort {
00211   function ui_name($short = FALSE) {
00212     return t('Broken/missing handler');
00213   }
00214 
00215   function ensure_my_table() { /* No table to ensure! */ }
00216   function query($group_by = FALSE) { /* No query to run */ }
00217   function options_form(&$form, &$form_state) {
00218     $form['markup'] = array(
00219       '#markup' => '<div class="form-item description">' . t('The handler for this item is broken or missing and cannot be used. If a module provided the handler and was disabled, re-enabling the module may restore it. Otherwise, you should probably delete this item.') . '</div>',
00220     );
00221   }
00222 
00226   function broken() { return TRUE; }
00227 }
00228 
00229 

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