You are here

class votingapi_views_handler_sort_nullable in Voting API 6.2

Same name and namespace in other branches
  1. 7.3 views/votingapi_views_handler_sort_nullable.inc \votingapi_views_handler_sort_nullable
  2. 7.2 views/votingapi_views_handler_sort_nullable.inc \votingapi_views_handler_sort_nullable

@file Provide a views handlers for votingapi data fields.

Hierarchy

Expanded class hierarchy of votingapi_views_handler_sort_nullable

1 string reference to 'votingapi_views_handler_sort_nullable'
votingapi_views_data in views/votingapi.views.inc
Implementation of hook_views_data().

File

views/votingapi_views_handler_sort_nullable.inc, line 8
Provide a views handlers for votingapi data fields.

View source
class votingapi_views_handler_sort_nullable extends views_handler_sort {
  function option_definition() {
    $options = parent::option_definition();
    $options['coalesce'] = array(
      'default' => TRUE,
    );
    $options['null_value'] = array(
      'default' => 0,
    );
    return $options;
  }
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $form['coalesce'] = array(
      '#type' => 'checkbox',
      '#title' => t('Treat missing votes as zeros'),
      '#default_value' => $this->options['coalesce'],
    );
  }

  /**
   * Called to add the sort to a query.
   */
  function query() {
    $this
      ->ensure_my_table();

    // Add the field.
    if ($this->options['coalesce']) {
      $this->query
        ->add_orderby(NULL, "COALESCE({$this->table_alias}.{$this->field}, 0)", $this->options['order'], $this->table_alias . '_' . $this->field . '_coalesced');
    }
    else {
      $this->query
        ->add_orderby($this->table_alias, $this->real_field, $this->options['order']);
    }
  }

}

Members