? .git Index: handlers/views_handler_sort.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/views/handlers/views_handler_sort.inc,v retrieving revision 1.1.2.1 diff -u -p -r1.1.2.1 views_handler_sort.inc --- handlers/views_handler_sort.inc 26 Jun 2009 00:23:37 -0000 1.1.2.1 +++ handlers/views_handler_sort.inc 2 Dec 2009 04:02:49 -0000 @@ -10,6 +10,12 @@ * Base sort handler that has no options and performs a simple sort */ class views_handler_sort extends views_handler { + + /** + * Determine if a sort can be exposed. + */ + function can_expose() { return TRUE; } + /** * Called to add the sort to a query. */ @@ -23,7 +29,12 @@ class views_handler_sort extends views_h $options = parent::option_definition(); $options['order'] = array('default' => 'ASC'); - + $options['exposed'] = array('default' => FALSE); + $options['expose'] = array( + 'contains' => array( + 'label' => array('default' => '', 'translatable' => TRUE), + ), + ); return $options; } @@ -31,31 +42,127 @@ class views_handler_sort extends views_h * Display whether or not the sort order is ascending or descending */ function admin_summary() { + if (!empty($this->options['exposed'])) { + return t('Exposed'); + } switch ($this->options['order']) { case 'ASC': case 'asc': default: - $type = t('asc'); + return t('asc'); break; case 'DESC'; case 'desc'; - $type = t('desc'); + return t('desc'); break; } - return '' . $type . ''; } /** * Basic options for all sort criteria */ function options_form(&$form, &$form_state) { - $form['order'] = array( - '#type' => 'radios', - '#title' => t('Sort order'), - '#options' => array('ASC' => t('Ascending'), 'DESC' => t('Descending')), - '#default_value' => $this->options['order'], + if ($this->can_expose()) { + $this->show_expose_button($form, $form_state); + } + $form['op_val_start'] = array('#value' => '
'); + $this->show_sort_form($form, $form_state); + $form['op_val_end'] = array('#value' => '
'); + if ($this->can_expose()) { + $this->show_expose_form($form, $form_state); + } + } + + /** + * Simple validate handler + */ + function options_validate(&$form, &$form_state) { + $this->sort_validate($form, $form_state); + if (!empty($this->options['exposed'])) { + $this->expose_validate($form, $form_state); + } + + } + + /** + * Simple submit handler + */ + function options_submit(&$form, &$form_state) { + unset($form_state['values']['expose_button']); // don't store this. + $this->sort_submit($form, $form_state); + if (!empty($this->options['exposed'])) { + $this->expose_submit($form, $form_state); + } + } + + /** + * Shortcut to display the value form. + */ + function show_sort_form(&$form, &$form_state) { + $options = $this->sort_options(); + if (!empty($options)) { + $form['order'] = array( + '#type' => 'radios', + '#options' => $options, + '#default_value' => $this->options['order'], + ); + } + } + + function sort_validate(&$form, &$form_state) { } + + function sort_submit(&$form, &$form_state) { } + + /** + * Provide a list of options for the default sort form. + * Should be overridden by classes that don't override sort_form + */ + function sort_options() { + return array( + 'ASC' => t('Sort ascending'), + 'DESC' => t('Sort descending'), + ); + } + + /** + * Since all exposed sorts are grouped into one select box. + * We don't return nothing when views call to exposed_form() + */ + function exposed_form(&$form, &$form_state) { } + + /** + * Handle the 'left' side fo the exposed options form. + */ + function expose_form_left(&$form, &$form_state) { + $form['expose']['label'] = array( + '#type' => 'textfield', + '#default_value' => $this->options['expose']['label'], + '#title' => t('Label'), + '#required' => TRUE, + '#size' => 40, + ); + } + + /** + * Handle the 'right' side fo the exposed options form. + */ + function expose_form_right(&$form, &$form_state) { + $form['expose']['order'] = array( + '#type' => 'value', + '#value' => 'ASC', + ); + } + + /** + * Provide default options for exposed sorts. + */ + function expose_options() { + $this->options['expose'] = array( + 'order' => $this->options['order'], + 'label' => $this->ui_name(), ); } } /** Index: plugins/views_plugin_exposed_form.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/views/plugins/Attic/views_plugin_exposed_form.inc,v retrieving revision 1.1.2.3 diff -u -p -r1.1.2.3 views_plugin_exposed_form.inc --- plugins/views_plugin_exposed_form.inc 30 Nov 2009 21:54:19 -0000 1.1.2.3 +++ plugins/views_plugin_exposed_form.inc 2 Dec 2009 04:03:04 -0000 @@ -32,7 +32,10 @@ class views_plugin_exposed_form extends function option_definition() { $options = parent::option_definition(); - $options['submit_button'] = array('default' => t('Apply'), 'translatable' => TRUE); + $options['submit_button'] = array('default' => 'Apply', 'translatable' => TRUE); + $options['exposed_sorts_label'] = array('default' => 'Sort By', 'translatable' => TRUE); + $options['sort_asc_label'] = array('default' => 'Asc', 'translatable' => TRUE); + $options['sort_desc_label'] = array('default' => 'Desc', 'translatable' => TRUE); return $options; } @@ -42,7 +45,33 @@ class views_plugin_exposed_form extends '#title' => t('Submit button text'), '#description' => t('Text to display in the submit button of the exposed form.'), '#default_value' => $this->options['submit_button'], + '#required' => TRUE, ); + + $form['exposed_sorts_label'] = array( + '#type' => 'textfield', + '#title' => t('Exposed sorts label'), + '#description' => t('Text to display as the label of the exposed sort select box.'), + '#default_value' => $this->options['exposed_sorts_label'], + '#required' => TRUE, + ); + + $form['sort_asc_label'] = array( + '#type' => 'textfield', + '#title' => t('Ascending'), + '#description' => t('Text to use when exposed sort is ordered ascending.'), + '#default_value' => $this->options['sort_asc_label'], + '#required' => TRUE, + ); + + $form['sort_desc_label'] = array( + '#type' => 'textfield', + '#title' => t('Descending'), + '#description' => t('Text to use when exposed sort is ordered descending.'), + '#default_value' => $this->options['sort_desc_label'], + '#required' => TRUE, + ); + } /** @@ -83,7 +112,27 @@ class views_plugin_exposed_form extends return $output; } - function query() { } + function query() { + $view = $this->view; + $exposed_data = $view->exposed_data; + $sort_by = $exposed_data['sort_by']; + if (!empty($sort_by)) { + $handler = $view->sort[$sort_by]; + if (isset($handler)) { + + $view->query->orderby = array(); + if (isset($exposed_data['sort_order']) && in_array($exposed_data['sort_order'], array('asc', 'desc'))) { + $handler->options['order'] = $exposed_data['sort_order']; + } + $handler->query(); + foreach ($view->sort as $sort) { + if (!$sort->is_exposed()) { + $sort->query(); + } + } + } + } + } function pre_render() { } @@ -91,7 +140,33 @@ class views_plugin_exposed_form extends function pre_execute() { } - function exposed_form_alter(&$form, &$form_state) { } + function exposed_form_alter(&$form, &$form_state) { + // Check if there is exposed sorts for this view + $exposed_sorts = array(); + foreach ($this->view->sort as $id => $handler) { + if ($handler->can_expose() && $handler->is_exposed()) { + $exposed_sorts[$id] = check_plain($handler->options['expose']['label']); + } + } + + if (count($exposed_sorts)) { + $form['sort_by'] = array( + '#type' => 'select', + '#options' => $exposed_sorts, + '#title' => t($this->options['exposed_sorts_label']), + ); + $sort_order = array( + 'asc' => check_plain($this->options['sort_asc_label']), + 'desc' => check_plain($this->options['sort_desc_label']), + ); + $form['sort_order'] = array( + '#type' => 'select', + '#options' => $sort_order, + '#title' => t('Order'), + ); + $form['submit']['#weight'] = 10; + } + } function exposed_form_validate(&$form, &$form_state) { }