danielebarchiesi@0
|
1 <?php
|
danielebarchiesi@0
|
2
|
danielebarchiesi@0
|
3 /**
|
danielebarchiesi@0
|
4 * @file
|
danielebarchiesi@0
|
5 * Views' relationship handlers.
|
danielebarchiesi@0
|
6 */
|
danielebarchiesi@0
|
7
|
danielebarchiesi@0
|
8 /**
|
danielebarchiesi@0
|
9 * @defgroup views_relationship_handlers Views relationship handlers
|
danielebarchiesi@0
|
10 * @{
|
danielebarchiesi@0
|
11 * Handlers to tell Views how to create alternate relationships.
|
danielebarchiesi@0
|
12 */
|
danielebarchiesi@0
|
13
|
danielebarchiesi@0
|
14 /**
|
danielebarchiesi@0
|
15 * Simple relationship handler that allows a new version of the primary table
|
danielebarchiesi@0
|
16 * to be linked in.
|
danielebarchiesi@0
|
17 *
|
danielebarchiesi@0
|
18 * The base relationship handler can only handle a single join. Some relationships
|
danielebarchiesi@0
|
19 * are more complex and might require chains of joins; for those, you must
|
danielebarchiesi@0
|
20 * utilize a custom relationship handler.
|
danielebarchiesi@0
|
21 *
|
danielebarchiesi@0
|
22 * Definition items:
|
danielebarchiesi@0
|
23 * - base: The new base table this relationship will be adding. This does not
|
danielebarchiesi@0
|
24 * have to be a declared base table, but if there are no tables that
|
danielebarchiesi@0
|
25 * utilize this base table, it won't be very effective.
|
danielebarchiesi@0
|
26 * - base field: The field to use in the relationship; if left out this will be
|
danielebarchiesi@0
|
27 * assumed to be the primary field.
|
danielebarchiesi@0
|
28 * - relationship table: The actual table this relationship operates against.
|
danielebarchiesi@0
|
29 * This is analogous to using a 'table' override.
|
danielebarchiesi@0
|
30 * - relationship field: The actual field this relationship operates against.
|
danielebarchiesi@0
|
31 * This is analogous to using a 'real field' override.
|
danielebarchiesi@0
|
32 * - label: The default label to provide for this relationship, which is
|
danielebarchiesi@0
|
33 * shown in parentheses next to any field/sort/filter/argument that uses
|
danielebarchiesi@0
|
34 * the relationship.
|
danielebarchiesi@0
|
35 *
|
danielebarchiesi@0
|
36 * @ingroup views_relationship_handlers
|
danielebarchiesi@0
|
37 */
|
danielebarchiesi@0
|
38 class views_handler_relationship extends views_handler {
|
danielebarchiesi@0
|
39 /**
|
danielebarchiesi@0
|
40 * Init handler to let relationships live on tables other than
|
danielebarchiesi@0
|
41 * the table they operate on.
|
danielebarchiesi@0
|
42 */
|
danielebarchiesi@0
|
43 function init(&$view, &$options) {
|
danielebarchiesi@0
|
44 parent::init($view, $options);
|
danielebarchiesi@0
|
45 if (isset($this->definition['relationship table'])) {
|
danielebarchiesi@0
|
46 $this->table = $this->definition['relationship table'];
|
danielebarchiesi@0
|
47 }
|
danielebarchiesi@0
|
48 if (isset($this->definition['relationship field'])) {
|
danielebarchiesi@0
|
49 // Set both real_field and field so custom handler
|
danielebarchiesi@0
|
50 // can rely on the old field value.
|
danielebarchiesi@0
|
51 $this->real_field = $this->field = $this->definition['relationship field'];
|
danielebarchiesi@0
|
52 }
|
danielebarchiesi@0
|
53 }
|
danielebarchiesi@0
|
54
|
danielebarchiesi@0
|
55 /**
|
danielebarchiesi@0
|
56 * Get this field's label.
|
danielebarchiesi@0
|
57 */
|
danielebarchiesi@0
|
58 function label() {
|
danielebarchiesi@0
|
59 if (!isset($this->options['label'])) {
|
danielebarchiesi@0
|
60 return $this->ui_name();
|
danielebarchiesi@0
|
61 }
|
danielebarchiesi@0
|
62 return $this->options['label'];
|
danielebarchiesi@0
|
63 }
|
danielebarchiesi@0
|
64
|
danielebarchiesi@0
|
65 function option_definition() {
|
danielebarchiesi@0
|
66 $options = parent::option_definition();
|
danielebarchiesi@0
|
67
|
danielebarchiesi@0
|
68
|
danielebarchiesi@0
|
69 // Relationships definitions should define a default label, but if they aren't get another default value.
|
danielebarchiesi@0
|
70 if (!empty($this->definition['label'])) {
|
danielebarchiesi@0
|
71 $label = $this->definition['label'];
|
danielebarchiesi@0
|
72 }
|
danielebarchiesi@0
|
73 else {
|
danielebarchiesi@0
|
74 $label = !empty($this->definition['field']) ? $this->definition['field'] : $this->definition['base field'];
|
danielebarchiesi@0
|
75 }
|
danielebarchiesi@0
|
76
|
danielebarchiesi@0
|
77 $options['label'] = array('default' => $label, 'translatable' => TRUE);
|
danielebarchiesi@0
|
78 $options['required'] = array('default' => FALSE, 'bool' => TRUE);
|
danielebarchiesi@0
|
79
|
danielebarchiesi@0
|
80 return $options;
|
danielebarchiesi@0
|
81 }
|
danielebarchiesi@0
|
82
|
danielebarchiesi@0
|
83 /**
|
danielebarchiesi@0
|
84 * Default options form that provides the label widget that all fields
|
danielebarchiesi@0
|
85 * should have.
|
danielebarchiesi@0
|
86 */
|
danielebarchiesi@0
|
87 function options_form(&$form, &$form_state) {
|
danielebarchiesi@0
|
88 parent::options_form($form, $form_state);
|
danielebarchiesi@0
|
89 $form['label'] = array(
|
danielebarchiesi@0
|
90 '#type' => 'textfield',
|
danielebarchiesi@0
|
91 '#title' => t('Identifier'),
|
danielebarchiesi@0
|
92 '#default_value' => isset($this->options['label']) ? $this->options['label'] : '',
|
danielebarchiesi@0
|
93 '#description' => t('Edit the administrative label displayed when referencing this relationship from filters, etc.'),
|
danielebarchiesi@0
|
94 '#required' => TRUE,
|
danielebarchiesi@0
|
95 );
|
danielebarchiesi@0
|
96
|
danielebarchiesi@0
|
97 $form['required'] = array(
|
danielebarchiesi@0
|
98 '#type' => 'checkbox',
|
danielebarchiesi@0
|
99 '#title' => t('Require this relationship'),
|
danielebarchiesi@0
|
100 '#description' => t('Enable to hide items that do not contain this relationship'),
|
danielebarchiesi@0
|
101 '#default_value' => !empty($this->options['required']),
|
danielebarchiesi@0
|
102 );
|
danielebarchiesi@0
|
103 }
|
danielebarchiesi@0
|
104
|
danielebarchiesi@0
|
105 /**
|
danielebarchiesi@0
|
106 * Called to implement a relationship in a query.
|
danielebarchiesi@0
|
107 */
|
danielebarchiesi@0
|
108 function query() {
|
danielebarchiesi@0
|
109 // Figure out what base table this relationship brings to the party.
|
danielebarchiesi@0
|
110 $table_data = views_fetch_data($this->definition['base']);
|
danielebarchiesi@0
|
111 $base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field'];
|
danielebarchiesi@0
|
112
|
danielebarchiesi@0
|
113 $this->ensure_my_table();
|
danielebarchiesi@0
|
114
|
danielebarchiesi@0
|
115 $def = $this->definition;
|
danielebarchiesi@0
|
116 $def['table'] = $this->definition['base'];
|
danielebarchiesi@0
|
117 $def['field'] = $base_field;
|
danielebarchiesi@0
|
118 $def['left_table'] = $this->table_alias;
|
danielebarchiesi@0
|
119 $def['left_field'] = $this->real_field;
|
danielebarchiesi@0
|
120 if (!empty($this->options['required'])) {
|
danielebarchiesi@0
|
121 $def['type'] = 'INNER';
|
danielebarchiesi@0
|
122 }
|
danielebarchiesi@0
|
123
|
danielebarchiesi@0
|
124 if (!empty($this->definition['extra'])) {
|
danielebarchiesi@0
|
125 $def['extra'] = $this->definition['extra'];
|
danielebarchiesi@0
|
126 }
|
danielebarchiesi@0
|
127
|
danielebarchiesi@0
|
128 if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
|
danielebarchiesi@0
|
129 $join = new $def['join_handler'];
|
danielebarchiesi@0
|
130 }
|
danielebarchiesi@0
|
131 else {
|
danielebarchiesi@0
|
132 $join = new views_join();
|
danielebarchiesi@0
|
133 }
|
danielebarchiesi@0
|
134
|
danielebarchiesi@0
|
135 $join->definition = $def;
|
danielebarchiesi@0
|
136 $join->options = $this->options;
|
danielebarchiesi@0
|
137 $join->construct();
|
danielebarchiesi@0
|
138 $join->adjusted = TRUE;
|
danielebarchiesi@0
|
139
|
danielebarchiesi@0
|
140 // use a short alias for this:
|
danielebarchiesi@0
|
141 $alias = $def['table'] . '_' . $this->table;
|
danielebarchiesi@0
|
142
|
danielebarchiesi@0
|
143 $this->alias = $this->query->add_relationship($alias, $join, $this->definition['base'], $this->relationship);
|
danielebarchiesi@0
|
144
|
danielebarchiesi@0
|
145 // Add access tags if the base table provide it.
|
danielebarchiesi@0
|
146 if (empty($this->query->options['disable_sql_rewrite']) && isset($table_data['table']['base']['access query tag'])) {
|
danielebarchiesi@0
|
147 $access_tag = $table_data['table']['base']['access query tag'];
|
danielebarchiesi@0
|
148 $this->query->add_tag($access_tag);
|
danielebarchiesi@0
|
149 }
|
danielebarchiesi@0
|
150 }
|
danielebarchiesi@0
|
151
|
danielebarchiesi@0
|
152 /**
|
danielebarchiesi@0
|
153 * You can't groupby a relationship.
|
danielebarchiesi@0
|
154 */
|
danielebarchiesi@0
|
155 function use_group_by() {
|
danielebarchiesi@0
|
156 return FALSE;
|
danielebarchiesi@0
|
157 }
|
danielebarchiesi@0
|
158 }
|
danielebarchiesi@0
|
159
|
danielebarchiesi@0
|
160 /**
|
danielebarchiesi@0
|
161 * A special handler to take the place of missing or broken handlers.
|
danielebarchiesi@0
|
162 *
|
danielebarchiesi@0
|
163 * @ingroup views_relationship_handlers
|
danielebarchiesi@0
|
164 */
|
danielebarchiesi@0
|
165 class views_handler_relationship_broken extends views_handler_relationship {
|
danielebarchiesi@0
|
166 function ui_name($short = FALSE) {
|
danielebarchiesi@0
|
167 return t('Broken/missing handler');
|
danielebarchiesi@0
|
168 }
|
danielebarchiesi@0
|
169
|
danielebarchiesi@0
|
170 function ensure_my_table() { /* No table to ensure! */ }
|
danielebarchiesi@0
|
171 function query() { /* No query to run */ }
|
danielebarchiesi@0
|
172 function options_form(&$form, &$form_state) {
|
danielebarchiesi@0
|
173 $form['markup'] = array(
|
danielebarchiesi@0
|
174 '#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>',
|
danielebarchiesi@0
|
175 );
|
danielebarchiesi@0
|
176 }
|
danielebarchiesi@0
|
177
|
danielebarchiesi@0
|
178 /**
|
danielebarchiesi@0
|
179 * Determine if the handler is considered 'broken'
|
danielebarchiesi@0
|
180 */
|
danielebarchiesi@0
|
181 function broken() { return TRUE; }
|
danielebarchiesi@0
|
182 }
|
danielebarchiesi@0
|
183
|
danielebarchiesi@0
|
184 /**
|
danielebarchiesi@0
|
185 * @}
|
danielebarchiesi@0
|
186 */
|