Project

General

Profile

Paste
Download (4.87 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / webform / views / webform_handler_relationship_submission_data.inc @ 389fb945

1
<?php
2

    
3
/**
4
 * Views' relationship handlers.
5
 */
6
class webform_handler_relationship_submission_data extends views_handler_relationship {
7

    
8
  /**
9
   * {@inheritdoc}
10
   */
11
  public function option_definition() {
12
    $options = parent::option_definition();
13
    $options['webform_nid'] = array('default' => NULL);
14
    $options['webform_cid'] = array('default' => NULL);
15
    $options['webform_form_key'] = array('default' => NULL);
16
    $options['webform_join_by'] = array('default' => 'nid_cid');
17
    return $options;
18
  }
19

    
20
  /**
21
   * {@inheritdoc}
22
   */
23
  public function options_form(&$form, &$form_state) {
24
    parent::options_form($form, $form_state);
25
    form_load_include($form_state, 'inc', 'webform', 'views/webform.views');
26

    
27
    $nid = (int) $this->options['webform_nid'];
28
    $cid = (int) $this->options['webform_cid'];
29

    
30
    // Helper function provides webform_nid and webform_cid options.
31
    _webform_views_options_form($form, $form_state, $nid, $cid);
32

    
33
    $form['webform_join_by'] = array(
34
      '#type' => 'select',
35
      '#title' => t('Relate using'),
36
      '#default_value' => $this->options['webform_join_by'],
37
      '#options' => array(
38
        'nid_cid' => t('Node and Component ID'),
39
        'cid' => t('Component ID'),
40
        'form_key' => t('Component Form Key'),
41
      ),
42
      '#description' => t('Choose <em>Node and Component ID</em> when this view will display data from only this webform.</br>Choose <em>Component ID</em> when this view will display data from other webforms and where the Component ID is identical.</br>Choose <em>Component Form Key</em> when this view will display data from other webforms with varying Component IDs.'),
43
    );
44

    
45
  }
46

    
47
  /**
48
   * {@inheritdoc}
49
   */
50
  public function options_validate(&$form, &$form_state) {
51
    parent::options_validate($form, $form_state);
52
    _webform_views_options_validate($form, $form_state);
53
  }
54

    
55
  /**
56
   * {@inheritdoc}
57
   */
58
  public function options_submit(&$form, &$form_state) {
59
    parent::options_submit($form, $form_state);
60
    _webform_views_options_submit($form, $form_state);
61
    $options =& $form_state['values']['options'];
62
    $options['webform_form_key'] = $options['webform_join_by'] == 'form_key' && ($node = node_load($options['webform_nid']))
63
                                        ? $node->webform['components'][$options['webform_cid']]['form_key']
64
                                        : NULL;
65
    // Drop PHP reference.
66
    unset($options);
67
  }
68

    
69
  /**
70
   * Called to implement a relationship in a query.
71
   *
72
   * It respects the given component ids, provided via options form.
73
   */
74
  public function query() {
75
    // When defining extra clauses, the 'table' key can be used to specify the
76
    // alias of another table. If NULL is specified, then the field is not
77
    // qualified with a table. Therefore, do NOT specify "'table' => NULL".
78
    switch ($this->options['webform_join_by']) {
79
      case 'form_key':
80
        $form_key = $this->options['webform_form_key'];
81
        $join_type = $this->options['required'] ? 'INNER' : 'LEFT';
82

    
83
        $this->ensure_my_table();
84
        $join = new views_join();
85

    
86
        $join->construct(
87
          // The table to be joined.
88
          'webform_component',
89
          // The left table (i.e. this table, webform_submission).
90
          $this->table,
91
          // The left field (i.e. the webform node id).
92
          'nid',
93
          // The field (i.e. webform_components.nid).
94
          'nid',
95
          // Extra array of additional conditions.
96
          array(
97
            array(
98
              // Extra join of form_key.
99
              'field' => 'form_key',
100
              // ...    = $form_key (operator '=' is default)
101
              'value' => $form_key,
102
            ),
103
          ),
104
          // Join type is the same as this relationship's join type.
105
          $join_type);
106

    
107
        $alias = $this->query->add_relationship(
108
          // Requested alias for new reliationship.
109
          'webform_component_' . $form_key,
110
          // Addition join to be added to this relatinship.
111
          $join,
112
          // Base table (i.e. drivingevals_submission)
113
          $this->table_alias,
114
          // Add the view to this relationship.
115
          $this->relationship);
116

    
117
        // The actual alias for this relationship's join is not known yet. Becasue
118
        // of name conflicts, it may have a number appended to the end. %alias is
119
        // substitued when the query is build with the actual alias name.
120
        $this->definition['extra'][] = "%alias.cid = {$alias}.cid";
121
        break;
122

    
123
      case 'nid_cid':
124
        $this->definition['extra'][] = array(
125
          'field' => "nid",
126
          'value' => $this->options['webform_nid'],
127
        );
128
        // FALL THROUGH.
129
      case 'cid':
130
        $this->definition['extra'][] = array(
131
          'field' => "cid",
132
          'value' => $this->options['webform_cid'],
133
        );
134
        break;
135
    }
136

    
137
    // The rest of building the join is performed by the parent.
138
    parent::query();
139
  }
140

    
141
}