2
// $Id: votingapi_views_handler_relationship.inc,v 1.1.2.3 2008/10/01 15:47:47 eaton Exp $
6
* Provide views handler for votingapi joins.
10
* A custom join handler that connects arbitrary base tables to VotingAPI's data.
12
* The base relationship handler can only handle a single join. Some relationships
13
* are more complex and might require chains of joins; for those, you must
14
* utilize a custom relationship handler.
17
* - base: The new base table this relationship will be adding. This does not
18
* have to be a declared base table, but if there are no tables that
19
* utilize this base table, it won't be very effective.
20
* - relationship table: The actual table this relationship operates against.
21
* This is analogous to using a 'table' override.
22
* - relationship field: The actual field this relationsihp operates against.
23
* This is analogous to using a 'real field' override.
24
* - label: The default label to provide for this relationship, which is
25
* shown in parentheses next to any field/sort/filter/argument that uses
28
class votingapi_views_handler_relationship extends views_handler_relationship {
29
function option_definition() {
30
$options = parent::option_definition();
32
$label = !empty($this->definition['label']) ? $this->definition['label'] : $this->definition['title'];
33
$options['label'] = array('default' => $label, 'translatable' => TRUE);
35
$options['votingapi']['value_type'] = array('default' => NULL);
36
$options['votingapi']['tag'] = array('default' => NULL);
37
if ($this->definition['base'] == 'votingapi_cache') {
38
$options['votingapi']['function'] = array('default' => NULL);
40
elseif ($this->definition['base'] == 'votingapi_vote') {
41
$options['current_user'] = array('default' => FALSE);
48
* Default options form that provides the label widget that all fields
51
function options_form(&$form, &$form_state) {
52
parent::options_form($form, $form_state);
54
$form['votingapi'] = array(
55
'#type' => 'fieldset',
56
'#collapsible' => FALSE,
57
'#title' => t('Data filters'),
58
'#description' => t('For each piece of content, many pieces of voting data may be saved. Use these options to specify exactly which types should be available via this relationship. <strong>Warning!</strong> Leaving any of these filters empty may result in multiple copies of each piece of content being displayed in listings.'),
61
$form['votingapi']['value_type'] = array(
62
'#title' => t('Value type'),
64
'#options' => _votingapi_views_value_types($this->definition['base']),
65
'#default_value' => $this->options['votingapi']['value_type'],
67
$form['votingapi']['tag'] = array(
68
'#title' => t('Vote tag'),
70
'#options' => _votingapi_views_tags($this->definition['base']),
71
'#default_value' => $this->options['votingapi']['tag'],
74
if ($this->definition['base'] == 'votingapi_cache') {
75
$form['votingapi']['function'] = array(
76
'#title' => t('Aggregation function'),
78
'#options' => _votingapi_views_functions(),
79
'#default_value' => $this->options['votingapi']['function'],
83
$form['current_user'] = array(
84
'#title' => t('Restrict to current user'),
85
'#type' => 'checkbox',
86
'#return_value' => TRUE,
87
'#default_value' => $this->options['current_user'],
93
* Called to implement a relationship in a query.
96
// Figure out what base table this relationship brings to the party.
97
$table_data = views_fetch_data($this->definition['base']);
99
$def = $this->definition;
100
$def['table'] = $this->definition['base'];
101
$def['field'] = 'content_id';
102
$def['left_table'] = $this->table;
103
$def['left_field'] = $this->field;
104
if (!empty($this->options['required'])) {
105
$def['type'] = 'INNER';
108
if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
109
$join = new $def['join_handler'];
112
$join = new views_join();
115
// use a short alias for this:
116
$alias = $def['table'] . '_' . $def['left_table'];
118
if (!empty($this->options['votingapi'])) {
119
foreach ($this->options['votingapi'] as $field => $value) {
120
if (!empty($value)) {
121
$def['extra'][] = array(
126
$alias .= '_'. $value;
131
if (!empty($this->options['current_user'])) {
132
$def['extra'][] = array(
134
'value' => '***CURRENT_USER***',
137
$alias .= '_curuser';
140
$join->definition = $def;
143
$this->ensure_my_table();
145
$this->alias = $this->query->add_relationship($alias, $join, $this->definition['base'], $this->relationship);