Projet

Général

Profil

Paste
Télécharger (1,07 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / votingapi / views / votingapi_views_handler_sort_nullable.inc @ 2c8c2b87

1
<?php
2

    
3
/**
4
 * @file
5
 * Provide a views handlers for votingapi data fields.
6
 */
7

    
8
class votingapi_views_handler_sort_nullable extends views_handler_sort {
9
  function option_definition() {
10
    $options = parent::option_definition();
11
    $options['coalesce'] = array('default' => FALSE);
12
    $options['null_value'] = array('default' => 0);
13
    return $options;
14
  }
15

    
16
  function options_form(&$form, &$form_state) {
17
    parent::options_form($form, $form_state);
18

    
19
    $form['coalesce'] = array(
20
      '#type' => 'checkbox',
21
      '#title' => t('Treat missing votes as zeros'),
22
      '#default_value' => $this->options['coalesce'],
23
    );
24
  }
25

    
26
  /**
27
   * Called to add the sort to a query.
28
   */
29
  function query() {
30
    $this->ensure_my_table();
31
    // Add the field.
32

    
33
    if ($this->options['coalesce']) {
34
      $this->query->add_orderby(NULL, "COALESCE($this->table_alias.$this->field, 0)", $this->options['order'], $this->table_alias . '_' . $this->field . '_coalesced');
35
    }
36
    else {
37
      $this->query->add_orderby($this->table_alias, $this->real_field, $this->options['order']);
38
    }
39
  }
40
}