1 | <?php |
---|
2 | /** |
---|
3 | * @file |
---|
4 | * Views' relationship handlers. |
---|
5 | */ |
---|
6 | |
---|
7 | /** |
---|
8 | * @defgroup views_relationship_handlers Views' relationship handlers |
---|
9 | * @{ |
---|
10 | * Handlers to tell Views how to create alternate relationships. |
---|
11 | */ |
---|
12 | |
---|
13 | /** |
---|
14 | * Simple relationship handler that allows a new version of the primary table |
---|
15 | * to be linked in. |
---|
16 | * |
---|
17 | * The base relationship handler can only handle a single join. Some relationships |
---|
18 | * are more complex and might require chains of joins; for those, you must |
---|
19 | * utilize 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 | class views_handler_relationship extends views_handler { |
---|
36 | /** |
---|
37 | * Init handler to let relationships live on tables other than |
---|
38 | * the table they operate on. |
---|
39 | */ |
---|
40 | function init(&$view, $options) { |
---|
41 | parent::init($view, $options); |
---|
42 | if (isset($this->definition['relationship table'])) { |
---|
43 | $this->table = $this->definition['relationship table']; |
---|
44 | } |
---|
45 | if (isset($this->definition['relationship field'])) { |
---|
46 | // Set both real_field and field so custom handler |
---|
47 | // can rely on the old field value. |
---|
48 | $this->real_field = $this->field = $this->definition['relationship field']; |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * Get this field's label. |
---|
54 | */ |
---|
55 | function label() { |
---|
56 | if (!isset($this->options['label'])) { |
---|
57 | return $this->ui_name(); |
---|
58 | } |
---|
59 | return $this->options['label']; |
---|
60 | } |
---|
61 | |
---|
62 | function option_definition() { |
---|
63 | $options = parent::option_definition(); |
---|
64 | |
---|
65 | |
---|
66 | // Relationships definitions should define a default label, but if they aren't get another default value. |
---|
67 | if (!empty($this->definition['label'])) { |
---|
68 | $label = $this->definition['label']; |
---|
69 | } |
---|
70 | else { |
---|
71 | $label = !empty($this->definition['field']) ? $this->definition['field'] : $this->definition['base field']; |
---|
72 | } |
---|
73 | |
---|
74 | $options['label'] = array('default' => $label, 'translatable' => TRUE); |
---|
75 | $options['required'] = array('default' => FALSE); |
---|
76 | |
---|
77 | return $options; |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * Default options form that provides the label widget that all fields |
---|
82 | * should have. |
---|
83 | */ |
---|
84 | function options_form(&$form, &$form_state) { |
---|
85 | parent::options_form($form, $form_state); |
---|
86 | $form['label'] = array( |
---|
87 | '#type' => 'textfield', |
---|
88 | '#title' => t('Label'), |
---|
89 | '#default_value' => isset($this->options['label']) ? $this->options['label'] : '', |
---|
90 | '#description' => t('The label for this relationship that will be displayed only administratively.'), |
---|
91 | '#required' => TRUE, |
---|
92 | ); |
---|
93 | |
---|
94 | $form['required'] = array( |
---|
95 | '#type' => 'checkbox', |
---|
96 | '#title' => t('Require this relationship'), |
---|
97 | '#description' => t('If required, items that do not contain this relationship will not appear.'), |
---|
98 | '#default_value' => !empty($this->options['required']), |
---|
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 | $base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field']; |
---|
109 | |
---|
110 | $this->ensure_my_table(); |
---|
111 | |
---|
112 | $def = $this->definition; |
---|
113 | $def['table'] = $this->definition['base']; |
---|
114 | $def['field'] = $base_field; |
---|
115 | $def['left_table'] = $this->table_alias; |
---|
116 | $def['left_field'] = $this->real_field; |
---|
117 | if (!empty($this->options['required'])) { |
---|
118 | $def['type'] = 'INNER'; |
---|
119 | } |
---|
120 | |
---|
121 | if (!empty($this->definition['extra'])) { |
---|
122 | $def['extra'] = $this->definition['extra']; |
---|
123 | } |
---|
124 | |
---|
125 | if (!empty($def['join_handler']) && class_exists($def['join_handler'], FALSE)) { |
---|
126 | $join = new $def['join_handler']; |
---|
127 | } |
---|
128 | else { |
---|
129 | $join = new views_join(); |
---|
130 | } |
---|
131 | |
---|
132 | $join->definition = $def; |
---|
133 | $join->options = $this->options; |
---|
134 | $join->construct(); |
---|
135 | $join->adjusted = TRUE; |
---|
136 | |
---|
137 | // use a short alias for this: |
---|
138 | $alias = $def['table'] . '_' . $this->table; |
---|
139 | |
---|
140 | $this->alias = $this->query->add_relationship($alias, $join, $this->definition['base'], $this->relationship); |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * You can't groupby a relationship. |
---|
145 | */ |
---|
146 | function use_group_by() { |
---|
147 | return FALSE; |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * A special handler to take the place of missing or broken handlers. |
---|
153 | * |
---|
154 | * @ingroup views_relationship_handlers |
---|
155 | */ |
---|
156 | class views_handler_relationship_broken extends views_handler_relationship { |
---|
157 | function ui_name($short = FALSE) { |
---|
158 | return t('Broken/missing handler'); |
---|
159 | } |
---|
160 | |
---|
161 | function ensure_my_table() { /* No table to ensure! */ } |
---|
162 | function query() { /* No query to run */ } |
---|
163 | function options_form(&$form, &$form_state) { |
---|
164 | $form['markup'] = array( |
---|
165 | '#prefix' => '<div class="form-item description">', |
---|
166 | '#value' => 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.'), |
---|
167 | ); |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * Determine if the handler is considered 'broken' |
---|
172 | */ |
---|
173 | function broken() { return TRUE; } |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * @} |
---|
178 | */ |
---|