Projet

Général

Profil

Paste
Télécharger (5,22 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / votingapi / views / votingapi_views_handler_relationship.inc @ 1f142f4f

1
<?php
2

    
3
/**
4
 * @file
5
 * Provide views handler for votingapi joins.
6
 */
7

    
8
/**
9
 * A custom join handler that connects arbitrary base tables to VotingAPI's data.
10
 *
11
 * The base relationship handler can only handle a single join. Some relationships
12
 * are more complex and might require chains of joins; for those, you must
13
 * utilize a custom relationship handler.
14
 *
15
 * Definition items:
16
 * - base: The new base table this relationship will be adding. This does not
17
 *   have to be a declared base table, but if there are no tables that
18
 *   utilize this base table, it won't be very effective.
19
 * - relationship table: The actual table this relationship operates against.
20
 *   This is analogous to using a 'table' override.
21
 * - relationship field: The actual field this relationsihp operates against.
22
 *   This is analogous to using a 'real field' override.
23
 * - label: The default label to provide for this relationship, which is
24
 *   shown in parentheses next to any field/sort/filter/argument that uses
25
 *   the relationship.
26
 */
27
class votingapi_views_handler_relationship extends views_handler_relationship {
28
  function option_definition() {
29
    $options = parent::option_definition();
30

    
31
    $label = !empty($this->definition['label']) ? $this->definition['label'] : $this->definition['title'];
32
    $options['label'] = array('default' => $label, 'translatable' => TRUE);
33

    
34
    $options['votingapi']['value_type'] = array('default' => NULL);
35
    $options['votingapi']['tag'] = array('default' => NULL);
36
    if ($this->definition['base'] == 'votingapi_cache') {
37
      $options['votingapi']['function'] = array('default' => NULL);
38
    }
39
    elseif ($this->definition['base'] == 'votingapi_vote') {
40
      $options['current_user'] = array('default' => FALSE);
41
    }
42

    
43
    return $options;
44
  }
45

    
46
  /**
47
   * Default options form that provides the label widget that all fields
48
   * should have.
49
   */
50
  function options_form(&$form, &$form_state) {
51
    parent::options_form($form, $form_state);
52

    
53
    $options['value_types'][''] = t('No filtering');
54
    $options['tags'][''] = t('No filtering');
55
    $options['functions'][''] = t('No filtering');
56
    foreach (votingapi_metadata() as $bin => $bin_data) {
57
      foreach ($bin_data as $key => $data) {
58
        $options[$bin][$key] = $data['name'];
59
      }
60
    }
61

    
62
    $form['votingapi'] = array(
63
      '#type' => 'fieldset',
64
      '#collapsible' => FALSE,
65
      '#title' => t('Data filters'),
66
      '#description' => t('For each piece of content, many pieces of voting data may be saved. Use these options to specify exactly which types should be available via this relationship. <strong>Warning!</strong> Leaving any of these filters empty may result in multiple copies of each piece of content being displayed in listings.'),
67
      '#tree' => TRUE,
68
    );
69
    $form['votingapi']['value_type'] = array(
70
      '#title' => t('Value type'),
71
      '#type' => 'select',
72
      '#options' => $options['value_types'],
73
      '#default_value' => $this->options['votingapi']['value_type'],
74
    );
75
    $form['votingapi']['tag'] = array(
76
      '#title' => t('Vote tag'),
77
      '#type' => 'select',
78
      '#options' => $options['tags'],
79
      '#default_value' => $this->options['votingapi']['tag'],
80
    );
81

    
82
    if ($this->definition['base'] == 'votingapi_cache') {
83
      $form['votingapi']['function'] = array(
84
        '#title' => t('Aggregation function'),
85
        '#type' => 'select',
86
        '#options' => $options['functions'],
87
        '#default_value' => $this->options['votingapi']['function'],
88
      );
89
    }
90
    else {
91
      $form['current_user'] = array(
92
        '#title' => t('Restrict to current user'),
93
        '#type' => 'checkbox',
94
        '#return_value' => TRUE,
95
        '#default_value' => $this->options['current_user'],
96
      );
97
    }
98
  }
99

    
100
  /**
101
   * Called to implement a relationship in a query.
102
   */
103
  function query() {
104
    // Figure out what base table this relationship brings to the party.
105
    $table_data = views_fetch_data($this->definition['base']);
106

    
107
    $def = $this->definition;
108
    $def['table'] = $this->definition['base'];
109
    $def['field'] = 'entity_id';
110
    $def['left_table'] = $this->table;
111
    $def['left_field'] = $this->field;
112
    if (!empty($this->options['required'])) {
113
      $def['type'] = 'INNER';
114
    }
115

    
116
    if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
117
      $join = new $def['join_handler'];
118
    }
119
    else {
120
      $join = new views_join();
121
    }
122

    
123
    // use a short alias for this:
124
    $alias = $def['table'] . '_' . $def['left_table'];
125

    
126
    if (!empty($this->options['votingapi'])) {
127
      foreach ($this->options['votingapi'] as $field => $value) {
128
        if (!empty($value)) {
129
          $def['extra'][] = array(
130
            'field' => $field,
131
            'value' => $value,
132
            'numeric' => FALSE
133
          );
134
          $alias .= '_' . str_replace(array(' ', '-', '.'), '_', $value);
135
        }
136
      }
137
    }
138

    
139
    if (!empty($this->options['current_user'])) {
140
      $def['extra'][] = array(
141
        'field' => 'uid',
142
        'value' => '***CURRENT_USER***',
143
        'numeric' => FALSE
144
      );
145
      $alias .= '_curuser';
146
    }
147

    
148
    $join->definition = $def;
149
    $join->construct();
150

    
151
    $this->ensure_my_table();
152

    
153
    $this->alias = $this->query->add_relationship($alias, $join, $this->definition['base'], $this->relationship);
154
  }
155
}