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