~spreadubuntu/spreadubuntu/devel-drupal6

« back to all changes in this revision

Viewing changes to sites/all/modules/votingapi/views/votingapi_views_handler_relationship.inc

  • Committer: ruben
  • Date: 2009-06-08 09:38:49 UTC
  • Revision ID: ruben@captive-20090608093849-s1qtsyctv2vwp1x1
SpreadUbuntu moving to Drupal6. Based on ubuntu-drupal theme and adding our modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
// $Id: votingapi_views_handler_relationship.inc,v 1.1.2.3 2008/10/01 15:47:47 eaton Exp $
 
3
 
 
4
/**
 
5
 * @file
 
6
 * Provide views handler for votingapi joins.
 
7
 */
 
8
 
 
9
/**
 
10
 * A custom join handler that connects arbitrary base tables to VotingAPI's data.
 
11
 *
 
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.
 
15
 *
 
16
 * Definition items:
 
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
 
26
 *   the relationship.
 
27
 */
 
28
class votingapi_views_handler_relationship extends views_handler_relationship {
 
29
  function option_definition() {
 
30
    $options = parent::option_definition();
 
31
 
 
32
    $label = !empty($this->definition['label']) ? $this->definition['label'] : $this->definition['title'];
 
33
    $options['label'] = array('default' => $label, 'translatable' => TRUE);
 
34
 
 
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);
 
39
    }
 
40
    elseif ($this->definition['base'] == 'votingapi_vote') {
 
41
      $options['current_user'] = array('default' => FALSE);
 
42
    }
 
43
 
 
44
    return $options;
 
45
  }
 
46
 
 
47
  /**
 
48
   * Default options form that provides the label widget that all fields
 
49
   * should have.
 
50
   */
 
51
  function options_form(&$form, &$form_state) {
 
52
    parent::options_form($form, $form_state);
 
53
 
 
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.'),
 
59
      '#tree' => TRUE,
 
60
    );
 
61
    $form['votingapi']['value_type'] = array(
 
62
      '#title' => t('Value type'),
 
63
      '#type' => 'select',
 
64
      '#options' => _votingapi_views_value_types($this->definition['base']),
 
65
      '#default_value' => $this->options['votingapi']['value_type'],
 
66
    );
 
67
    $form['votingapi']['tag'] = array(
 
68
      '#title' => t('Vote tag'),
 
69
      '#type' => 'select',
 
70
      '#options' => _votingapi_views_tags($this->definition['base']),
 
71
      '#default_value' => $this->options['votingapi']['tag'],
 
72
    );
 
73
 
 
74
    if ($this->definition['base'] == 'votingapi_cache') {
 
75
      $form['votingapi']['function'] = array(
 
76
        '#title' => t('Aggregation function'),
 
77
        '#type' => 'select',
 
78
        '#options' => _votingapi_views_functions(),
 
79
        '#default_value' => $this->options['votingapi']['function'],
 
80
      );
 
81
    }
 
82
    else {
 
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'],
 
88
      );
 
89
    }
 
90
  }
 
91
 
 
92
  /**
 
93
   * Called to implement a relationship in a query.
 
94
   */
 
95
  function query() {
 
96
    // Figure out what base table this relationship brings to the party.
 
97
    $table_data = views_fetch_data($this->definition['base']);
 
98
 
 
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';
 
106
    }
 
107
 
 
108
    if (!empty($def['join_handler']) && class_exists($def['join_handler'])) {
 
109
      $join = new $def['join_handler'];
 
110
    }
 
111
    else {
 
112
      $join = new views_join();
 
113
    }
 
114
 
 
115
    // use a short alias for this:
 
116
    $alias = $def['table'] . '_' . $def['left_table'];
 
117
 
 
118
    if (!empty($this->options['votingapi'])) {
 
119
      foreach ($this->options['votingapi'] as $field => $value) {
 
120
        if (!empty($value)) {
 
121
          $def['extra'][] = array(
 
122
            'field' => $field,
 
123
            'value' => $value,
 
124
            'numeric' => FALSE
 
125
          );
 
126
          $alias .= '_'. $value;
 
127
        }
 
128
      }
 
129
    }
 
130
 
 
131
    if (!empty($this->options['current_user'])) {
 
132
      $def['extra'][] = array(
 
133
        'field' => 'uid',
 
134
        'value' => '***CURRENT_USER***',
 
135
        'numeric' => FALSE
 
136
      );
 
137
      $alias .= '_curuser';
 
138
    }
 
139
 
 
140
    $join->definition = $def;
 
141
    $join->construct();
 
142
 
 
143
    $this->ensure_my_table();
 
144
 
 
145
    $this->alias = $this->query->add_relationship($alias, $join, $this->definition['base'], $this->relationship);
 
146
  }
 
147
}