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