Projet

Général

Profil

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

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

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

    
30
  function option_definition() {
31
    $options = parent::option_definition();
32

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

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

    
45
    return $options;
46
  }
47

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

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

    
64
    $form['votingapi'] = array(
65
      '#type' => 'fieldset',
66
      '#collapsible' => FALSE,
67
      '#title' => t('Data filters'),
68
      '#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.'),
69
      '#tree' => TRUE,
70
    );
71
    $form['votingapi']['value_type'] = array(
72
      '#title' => t('Value type'),
73
      '#type' => 'select',
74
      '#options' => $options['value_types'],
75
      '#default_value' => $this->options['votingapi']['value_type'],
76
    );
77
    $form['votingapi']['tag'] = array(
78
      '#title' => t('Vote tag'),
79
      '#type' => 'select',
80
      '#options' => $options['tags'],
81
      '#default_value' => $this->options['votingapi']['tag'],
82
    );
83

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

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

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

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

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

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

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

    
150
    $join->definition = $def;
151
    $join->construct();
152

    
153
    $this->ensure_my_table();
154

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