Projet

Général

Profil

Paste
Télécharger (8,47 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / views / webform_handler_field_submission_data.inc @ 8c72e82a

1
<?php
2

    
3
/**
4
 * @file
5
 * Views handler to display data value of a webform submission component.
6
 */
7

    
8
/**
9
 * Field handler to show submission data.
10
 *
11
 * @ingroup views_field_handlers
12
 */
13
class webform_handler_field_submission_data extends views_handler_field {
14

    
15
  function construct() {
16
    // We need to set this property before calling the construct() chain
17
    // as we use it in the option_definintion() call.
18
    $this->webform_expand = $this->definition['webform_expand'];
19
    parent::construct();
20
  }
21

    
22
  function option_definition() {
23
    $options = parent::option_definition();
24
    $options['format'] = array('default' => 'html');
25
    $options['custom_label'] = array('default' => 'default');
26
    $options['webform_nid'] = array('default' => NULL);
27
    $options['webform_cid'] = array('default' => NULL);
28
    $options['webform_datatype'] = array('default' => 'string');
29
    return $options;
30
  }
31

    
32
  function options_form(&$form, &$form_state) {
33
    parent::options_form($form, $form_state);
34
    form_load_include($form_state, 'inc', 'webform', 'views/webform.views');
35

    
36
    $form['custom_label']['#type'] = 'radios';
37
    $form['custom_label']['#options'] = array(
38
      'default' => t('Use component label'),
39
      'custom' => t('Custom label'),
40
      'none' => t('No label'),
41
    );
42
    $form['custom_label']['#default_value'] = $this->options['custom_label'];
43
    $form['label']['#dependency'] = array('radio:options[custom_label]' => array('custom'));
44

    
45
    if (!$this->webform_expand) {
46
      $nid = (int) $this->options['webform_nid'];
47
      $cid = (int) $this->options['webform_cid'];
48

    
49
      // Helper function provides webform_nid and webform_cid options.
50
      _webform_views_options_form($form, $form_state, $nid, $cid);
51
    }
52

    
53
    // Modify behavior for the type of data in the component.
54
    $form['webform_datatype'] = array(
55
      '#type' => 'select',
56
      '#title' => t('Data type'),
57
      '#options' => array(
58
        'string' => t('String'),
59
        'number' => t('Number'),
60
      ),
61
      '#default_value' => $this->options['webform_datatype'],
62
    );
63

    
64
    // Provide the selection for the display format.
65
    $form['format'] = array(
66
      '#type' => 'select',
67
      '#title' => t('Display format'),
68
      '#options' => array(
69
        'html' => t('HTML'),
70
        'text' => t('Plain text'),
71
      ),
72
      '#default_value' => $this->options['format'],
73
    );
74
  }
75

    
76
  function options_validate(&$form, &$form_state) {
77
    parent::options_validate($form, $form_state);
78
    if (!$this->webform_expand) {
79
      _webform_views_options_validate($form, $form_state);
80
    }
81
  }
82

    
83
  function options_submit(&$form, &$form_state) {
84
    parent::options_submit($form, $form_state);
85
    if (!$this->webform_expand) {
86
      _webform_views_options_submit($form, $form_state);
87
    }
88
  }
89

    
90
  /**
91
   * Called to determine what to tell the clicksorter.
92
   */
93
  function click_sort($order) {
94
    if (isset($this->field_alias)) {
95
      // Since fields should always have themselves already added, just
96
      // add a sort on the field.
97
      $params = $this->options['group_type'] != 'group' ? array('function' => $this->options['group_type']) : array();
98

    
99
      $join = new views_join;
100
      $extra = array(
101
        array(
102
          'field' => 'cid',
103
          'value' => $this->options['webform_cid'],
104
          'numeric' => TRUE,
105
        ),
106
        array(
107
          'field' => 'no',
108
          'value' => '0',
109
          'numeric' => TRUE,
110
        ),
111
      );
112
      $join->construct('webform_submitted_data', 'webform_submissions', 'sid', 'sid', $extra);
113
      $this->query->add_relationship('webform_submitted_data_click_sort', $join, 'webform_submissions');
114
      switch ($this->options['webform_datatype']) {
115
        case 'number':
116
          $this->query->add_orderby(NULL, "IF(webform_submitted_data_click_sort.data REGEXP '^-?[0-9]+(\\\\.[0-9]*)?$', webform_submitted_data_click_sort.data + 0, NULL)", $order, $this->field_alias . '_click_sort', $params);
117
          break;
118
        default:
119
          $this->query->add_orderby('webform_submitted_data_click_sort', 'data', $order, $this->field_alias . '_click_sort', $params);
120
          break;
121
      }
122
    }
123
  }
124

    
125
  /**
126
   * Load the node and submissions needed for this components values.
127
   */
128
  function pre_render(&$values) {
129
    $nid = $this->options['webform_nid'];
130
    $this->webform_node = node_load($nid);
131
    // Load all the submissions needed for this page. This is stored at the
132
    // view level to ensure it's available between fields so we don't load
133
    // them twice.
134
    if (!isset($this->view->_webform_submissions[$nid])) {
135
      module_load_include('inc', 'webform', 'includes/webform.submissions');
136
      $this->view->_webform_submissions[$nid] = array();
137
      $sids = array();
138
      foreach ($values as $value) {
139
        $sids[] = $value->{$this->field_alias};
140
      }
141
      if ($sids) {
142
        $this->view->_webform_submissions[$nid] = webform_get_submissions(array('sid' => $sids));
143
      }
144
    }
145
  }
146

    
147
  /**
148
   * Get this field's label based on the selected component.
149
   */
150
  function label() {
151
    if ($this->options['custom_label'] === 'default' && isset($this->options['webform_cid'])) {
152
      if (isset($this->webform_node)) {
153
        $node = $this->webform_node;
154
      }
155
      else {
156
        $node = node_load($this->options['webform_nid']);
157
      }
158
      if ($node && isset($node->webform['components'][$this->options['webform_cid']])) {
159
        $component = $node->webform['components'][$this->options['webform_cid']];
160
        return $component['name'];
161
      }
162
    }
163
    elseif ($this->options['custom_label'] === 'custom' && isset($this->options['label'])) {
164
      return $this->options['label'];
165
    }
166
    return '';
167
  }
168

    
169
  /**
170
   * Render the field using the loaded submissions from pre_render().
171
   */
172
  function render($row) {
173
    $sid = $this->get_value($row);
174
    $nid = $this->options['webform_nid'];
175
    $cid = $this->options['webform_cid'];
176
    $webform = $this->webform_node;
177
    if (isset($sid) && isset($webform->webform['components'][$cid])) {
178

    
179
      $component = $webform->webform['components'][$cid];
180
      $submission = $this->view->_webform_submissions[$nid][$sid];
181
      if ($submission->nid != $nid) {
182
        // The actual submission is from a different webform than the one used to define the view.
183
        // Rather than using the component with the same cid, try to match the form_key.
184
        if (!isset($this->view->_webform_components[$nid][$submission->nid][$cid])) {
185
          if (!isset($this->view->_webform_components[$nid][$submission->nid]['webform'])) {
186
            $this->view->_webform_components[$nid][$submission->nid]['webform'] = $webform;
187
          }
188
          $this->view->_webform_components[$nid][$submission->nid][$cid] = $component;
189
          $submission_node = node_load($submission->nid);
190
          foreach ($submission_node->webform['components'] as $sub_cid => $sub_component) {
191
            if ((string) $sub_component['form_key'] === (string) $component['form_key'] && $sub_component['type'] == $component['type']) {
192
              $this->view->_webform_components[$nid][$submission->nid]['webform'] = $submission_node;
193
              $this->view->_webform_components[$nid][$submission->nid][$cid] = $sub_component;
194
              break;
195
            }
196
          }
197
        }
198
        $webform = $this->view->_webform_components[$nid][$submission->nid]['webform'];
199
        $component = $this->view->_webform_components[$nid][$submission->nid][$cid];
200
        // Note: $nid and $cid refer to the definition webform, not the submission webform
201
        // whereas $component refers to the submission component.
202
      }
203

    
204
      if ($this->options['format'] == 'html') {
205
        $render = array('#submission' => $submission);
206
        _webform_client_form_add_component($webform, $component, NULL, $render, $render, $submission->data, 'html');
207
        $render = $render[$component['form_key']];
208
        // Remove display label.
209
        $render['#theme_wrappers'] = array();
210
      }
211
      else {
212
        // Plain text format is generated via invoking the table output to ensure output is sanitised.
213
        $data = isset($submission->data[$component['cid']]) ? $submission->data[$component['cid']] : NULL;
214
        $render = webform_component_invoke($component['type'], 'table', $component, $data);
215
      }
216
      // Webform renders empty values as a space, which prevents views empty
217
      // rewriting from being used. If empty is in use, change result to an
218
      // actual empty string.
219
      $render = render($render);
220
      if ($render === ' ' && strlen($this->options['empty'])) {
221
        $render = '';
222
      }
223
      return $render;
224
    }
225
  }
226
}