Index: views_handler_sort.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/handlers/views_handler_sort.inc,v
retrieving revision 1.1
diff -u -p -r1.1 views_handler_sort.inc
--- views_handler_sort.inc 3 Sep 2008 19:21:28 -0000 1.1
+++ views_handler_sort.inc 9 May 2009 11:58:09 -0000
@@ -11,51 +11,247 @@
*/
class views_handler_sort extends views_handler {
/**
+ * Provide some extra help to get the sort/value easier to use.
+ *
+ * This likely has to be overridden by sorts which are more complex
+ * than simple sort/value.
+ */
+ function init(&$view, $options) {
+ parent::init($view, $options);
+
+ $this->get_sort();
+ }
+
+ /**
+ * Determine if a sort can be exposed.
+ */
+ function can_expose() { return TRUE; }
+
+ /**
+ * Provide the basic form which calls through to subforms.
+ * If overridden, it is best to call through to the parent,
+ * or to at least make sure all of the functions in this form
+ * are called.
+ */
+ function options_form(&$form, &$form_state) {
+ $form['start'] = array('#value' => '
');
+ $this->sort_form($form, $form_state);
+ $form['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);
+ }
+ }
+
+ /**
+ * Provide a form for setting the sort.
+ *
+ * This may be overridden by child classes, and it must
+ * define $form['sort'];
+ */
+ function sort_form(&$form, &$form_state) {
+ $options = $this->sort_options();
+ if (!empty($options)) {
+ $form['sort'] = array(
+ '#type' => 'radios',
+ '#options' => $options,
+ '#default_value' => $this->options['sort'],
+ );
+ }
+ }
+
+ /**
+ * 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(
+ '' => t('Unsorted'),
+ 'ASC' => t('Sort ascending'),
+ 'DESC' => t('Sort descending'),
+ );
+ }
+
+ function expose_form_left(&$form, &$form_state) {
+ $form['expose']['label'] = array(
+ '#type' => 'textfield',
+ '#default_value' => $this->options['expose']['label'],
+ '#title' => t('Label'),
+ '#size' => 40,
+ );
+ }
+
+ /**
+ * Handle the 'left' side fo the exposed options form.
+ */
+ function expose_form_right(&$form, &$form_state) {
+
+ $form['expose']['sort'] = array(
+ '#type' => 'value',
+ '#value' => '',
+ );
+
+ $form['expose']['label'] = array(
+ '#type' => 'textfield',
+ '#default_value' => $this->options['expose']['label'],
+ '#title' => t('Label'),
+ '#size' => 40,
+ );
+ }
+
+ /**
+ * Provide default options for exposed sorts.
+ */
+ function expose_options() {
+ $this->options['expose'] = array(
+ 'sort' => $this->get_sort(),
+ 'label' => $this->ui_name(),
+ );
+ }
+
+ /**
+ * Render our chunk of the exposed sort form when selecting
+ *
+ * You can override this if it doesn't do what you expect.
+ */
+ function exposed_form(&$form, &$form_state) {
+ if (empty($this->options['exposed'])) {
+ return;
+ }
+
+ if (isset($this->options['expose']['sort'])) {
+ $sort = $this->options['expose']['sort'];
+ $this->sort_form($form, $form_state);
+ $form[$sort] = $form['sort'];
+
+ $this->exposed_translate($form[$sort], 'sort');
+
+ unset($form['sort']);
+ }
+ }
+
+ /**
+ * Make some translations to a form item to make it more suitable to
+ * exposing.
+ */
+ function exposed_translate(&$form, $type) {
+ if (!isset($form['#type'])) {
+ return;
+ }
+
+ if ($form['#type'] == 'radios') {
+ $form['#type'] = 'select';
+ }
+ }
+
+ /**
+ * Tell the renderer about our exposed form. This only needs to be
+ * overridden for particularly complex forms. And maybe not even then.
+ */
+ function exposed_info() {
+ if (empty($this->options['exposed'])) {
+ return;
+ }
+
+ return array(
+ 'sort' => $this->options['expose']['sort'],
+ 'label' => $this->options['expose']['label'],
+ );
+ }
+
+ /**
+ * Check to see if input from the exposed sorts should change
+ * the behavior of this sort.
+ */
+ function accept_exposed_input($input) {
+ return TRUE;
+ }
+
+ function store_exposed_input($input, $status) {
+ return TRUE;
+ }
+
+ /**
+ * Determine if we should provide sorting based upon $_GET inputs.
+ */
+ function get_sort() {
+ if (!isset($_GET['sort'])) {
+ $sort = $this->options['sort'];
+ $this->sort = $this->options['sort'];
+ }
+ else {
+ $sort = $_GET['sort'];
+ // Store the $sort for later use.
+ $this->sort = strtolower($sort);
+ }
+
+ // Ensure $this->sort is valid.
+ if ($this->sort != 'asc' && $this->sort != 'desc') {
+ $this->sort = '';
+ }
+ }
+
+ /**
* Called to add the sort to a query.
*/
function query() {
+ if (empty($this->sort)) {
+ return;
+ }
$this->ensure_my_table();
// Add the field.
- $this->query->add_orderby($this->table_alias, $this->real_field, $this->options['order']);
+ $this->query->add_orderby($this->table_alias, $this->real_field, $this->sort);
}
function option_definition() {
$options = parent::option_definition();
-
- $options['order'] = array('default' => 'ASC');
+ $options['exposed'] = array('default' => FALSE);
+ $options['sort'] = array('default' => '');
return $options;
}
/**
- * Display whether or not the sort order is ascending or descending
+ * Display whether or not the sort sort is ascending or descending
*/
function admin_summary() {
- switch ($this->options['order']) {
+ switch ($this->options['sort']) {
case 'ASC':
case 'asc':
- default:
$type = t('asc');
break;
case 'DESC';
case 'desc';
$type = t('desc');
break;
+ default:
+ $type == t('unsorted');
+ 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'],
- );
- }
+
}
/**