Projet

Général

Profil

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

root / drupal7 / sites / all / modules / views / handlers / views_handler_relationship.inc @ 4003efde

1
<?php
2

    
3
/**
4
 * @file
5
 * Definition of views_handler_relationship.
6
 */
7

    
8
/**
9
 * @defgroup views_relationship_handlers Views relationship handlers
10
 * @{
11
 * Handlers to tell Views how to create alternate relationships.
12
 */
13

    
14
/**
15
 * Relationship handler, allows a new version of the primary table to be linked.
16
 *
17
 * The base relationship handler can only handle a single join. Some
18
 * relationships are more complex and might require chains of joins; for those,
19
 * you must use a custom relationship handler.
20
 *
21
 * Definition items:
22
 * - base: The new base table this relationship will be adding. This does not
23
 *   have to be a declared base table, but if there are no tables that
24
 *   utilize this base table, it won't be very effective.
25
 * - base field: The field to use in the relationship; if left out this will be
26
 *   assumed to be the primary field.
27
 * - relationship table: The actual table this relationship operates against.
28
 *   This is analogous to using a 'table' override.
29
 * - relationship field: The actual field this relationship operates against.
30
 *   This is analogous to using a 'real field' override.
31
 * - label: The default label to provide for this relationship, which is
32
 *   shown in parentheses next to any field/sort/filter/argument that uses
33
 *   the relationship.
34
 *
35
 * @ingroup views_relationship_handlers
36
 */
37
class views_handler_relationship extends views_handler {
38

    
39
  /**
40
   * Let relationships live on tables other than the table they operate on.
41
   */
42
  public function init(&$view, &$options) {
43
    parent::init($view, $options);
44
    if (isset($this->definition['relationship table'])) {
45
      $this->table = $this->definition['relationship table'];
46
    }
47
    if (isset($this->definition['relationship field'])) {
48
      // Set both real_field and field so custom handler can rely on the old
49
      // field value.
50
      $this->real_field = $this->field = $this->definition['relationship field'];
51
    }
52
  }
53

    
54
  /**
55
   * Get this field's label.
56
   */
57
  public function label() {
58
    if (!isset($this->options['label'])) {
59
      return $this->ui_name();
60
    }
61
    return $this->options['label'];
62
  }
63

    
64
  /**
65
   * {@inheritdoc}
66
   */
67
  public function option_definition() {
68
    $options = parent::option_definition();
69

    
70

    
71
    // Relationships definitions should define a default label, but if they
72
    // aren't get another default value.
73
    if (!empty($this->definition['label'])) {
74
      $label = $this->definition['label'];
75
    }
76
    else {
77
      $label = !empty($this->definition['field']) ? $this->definition['field'] : $this->definition['base field'];
78
    }
79

    
80
    $options['label'] = array('default' => $label, 'translatable' => TRUE);
81
    $options['required'] = array('default' => FALSE, 'bool' => TRUE);
82

    
83
    return $options;
84
  }
85

    
86
  /**
87
   * Provide the label widget that all fields should have.
88
   */
89
  public function options_form(&$form, &$form_state) {
90
    parent::options_form($form, $form_state);
91
    $form['label'] = array(
92
      '#type' => 'textfield',
93
      '#title' => t('Identifier'),
94
      '#default_value' => isset($this->options['label']) ? $this->options['label'] : '',
95
      '#description' => t('Edit the administrative label displayed when referencing this relationship from filters, etc.'),
96
      '#required' => TRUE,
97
    );
98

    
99
    $form['required'] = array(
100
      '#type' => 'checkbox',
101
      '#title' => t('Require this relationship'),
102
      '#description' => t('Enable to hide items that do not contain this relationship'),
103
      '#default_value' => !empty($this->options['required']),
104
    );
105
  }
106

    
107
  /**
108
   * Called to implement a relationship in a query.
109
   */
110
  public function query() {
111
    // Figure out what base table this relationship brings to the party.
112
    $table_data = views_fetch_data($this->definition['base']);
113
    $base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field'];
114

    
115
    $this->ensure_my_table();
116

    
117
    $def = $this->definition;
118
    $def['table'] = $this->definition['base'];
119
    $def['field'] = $base_field;
120
    $def['left_table'] = $this->table_alias;
121
    $def['left_field'] = $this->real_field;
122
    if (!empty($this->options['required'])) {
123
      $def['type'] = 'INNER';
124
    }
125

    
126
    if (!empty($this->definition['extra'])) {
127
      $def['extra'] = $this->definition['extra'];
128
    }
129

    
130
    if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
131
      $join = new $def['join_handler']();
132
    }
133
    else {
134
      $join = new views_join();
135
    }
136

    
137
    $join->definition = $def;
138
    $join->options = $this->options;
139
    $join->construct();
140
    $join->adjusted = TRUE;
141

    
142
    // Use a short alias for this.
143
    $alias = $def['table'] . '_' . $this->table;
144

    
145
    $this->alias = $this->query->add_relationship($alias, $join, $this->definition['base'], $this->relationship);
146

    
147
    // Add access tags if the base table provide it.
148
    if (empty($this->query->options['disable_sql_rewrite']) && isset($table_data['table']['base']['access query tag'])) {
149
      $access_tag = $table_data['table']['base']['access query tag'];
150
      $this->query->add_tag($access_tag);
151
    }
152
  }
153

    
154
  /**
155
   * You can't groupby a relationship.
156
   */
157
  public function use_group_by() {
158
    return FALSE;
159
  }
160

    
161
}
162

    
163
/**
164
 * A special handler to take the place of missing or broken handlers.
165
 *
166
 * @ingroup views_relationship_handlers
167
 */
168
class views_handler_relationship_broken extends views_handler_relationship {
169

    
170
  /**
171
   * {@inheritdoc}
172
   */
173
  public function ui_name($short = FALSE) {
174
    return t('Broken/missing handler');
175
  }
176

    
177
  /**
178
   * {@inheritdoc}
179
   */
180
  public function ensure_my_table() {
181
    // No table to ensure!
182
  }
183

    
184
  /**
185
   * {@inheritdoc}
186
   */
187
  public function query() {
188
    // No query to run.
189
  }
190

    
191
  /**
192
   * {@inheritdoc}
193
   */
194
  public function options_form(&$form, &$form_state) {
195
    $form['markup'] = array(
196
      '#markup' => '<div class="form-item description">' . t('The handler for this item is broken or missing and cannot be used. If a module provided the handler and was disabled, re-enabling the module may restore it. Otherwise, you should probably delete this item.') . '</div>',
197
    );
198
  }
199

    
200
  /**
201
   * {@inheritdoc}
202
   */
203
  public function broken() {
204
    return TRUE;
205
  }
206

    
207
}
208

    
209
/**
210
 * @}
211
 */