Stripping CVS keywords
[project/views.git] / handlers / views_handler_relationship.inc
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 iwll 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 relationsihp 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 $this->field = $this->definition['relationship field'];
47 }
48 }
49
50 /**
51 * Get this field's label.
52 */
53 function label() {
54 if (!isset($this->options['label'])) {
55 return $this->ui_name();
56 }
57 return $this->options['label'];
58 }
59
60 function option_definition() {
61 $options = parent::option_definition();
62
63 $label = !empty($this->definition['label']) ? $this->definition['label'] : $this->definition['field'];
64 $options['label'] = array('default' => $label, 'translatable' => TRUE);
65 $options['required'] = array('default' => FALSE);
66
67 return $options;
68 }
69
70 /**
71 * Default options form that provides the label widget that all fields
72 * should have.
73 */
74 function options_form(&$form, &$form_state) {
75 $form['label'] = array(
76 '#type' => 'textfield',
77 '#title' => t('Label'),
78 '#default_value' => isset($this->options['label']) ? $this->options['label'] : '',
79 '#description' => t('The label for this relationship that will be displayed only administratively.'),
80 );
81
82 $form['required'] = array(
83 '#type' => 'checkbox',
84 '#title' => t('Require this relationship'),
85 '#description' => t('If required, items that do not contain this relationship will not appear.'),
86 '#default_value' => !empty($this->options['required']),
87 );
88 }
89
90 /**
91 * Called to implement a relationship in a query.
92 */
93 function query() {
94 // Figure out what base table this relationship brings to the party.
95 $table_data = views_fetch_data($this->definition['base']);
96 $base_field = empty($this->definition['base field']) ? $table_data['table']['base']['field'] : $this->definition['base field'];
97
98 $this->ensure_my_table();
99
100 $def = $this->definition;
101 $def['table'] = $this->definition['base'];
102 $def['field'] = $base_field;
103 $def['left_table'] = $this->table_alias;
104 $def['left_field'] = $this->field;
105 if (!empty($this->options['required'])) {
106 $def['type'] = 'INNER';
107 }
108
109 if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
110 $join = new $def['join_handler'];
111 }
112 else {
113 $join = new views_join();
114 }
115
116 $join->definition = $def;
117 $join->construct();
118 $join->adjusted = TRUE;
119
120 // use a short alias for this:
121 $alias = $def['table'] . '_' . $this->table;
122
123 $this->alias = $this->query->add_relationship($alias, $join, $this->definition['base'], $this->relationship);
124 }
125 }
126
127 /**
128 * A special handler to take the place of missing or broken handlers.
129 */
130 class views_handler_relationship_broken extends views_handler_relationship {
131 function ui_name($short = FALSE) {
132 return t('Broken/missing handler');
133 }
134
135 function ensure_my_table() { /* No table to ensure! */ }
136 function query() { /* No query to run */ }
137 function options_form(&$form, &$form_state) {
138 $form['markup'] = array(
139 '#prefix' => '<div class="form-item description">',
140 '#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.'),
141 );
142 }
143
144 /**
145 * Determine if the handler is considered 'broken'
146 */
147 function broken() { return TRUE; }
148 }
149
150 /**
151 * @}
152 */