Projet

Général

Profil

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

root / drupal7 / sites / all / modules / webform / views / webform_handler_field_submission_data.inc @ 01f36513

1
<?php
2

    
3
/**
4
 * Views handler to display data value of a webform submission component.
5
 *
6
 * Field handler to show submission data.
7
 *
8
 * @ingroup views_field_handlers
9
 */
10
class webform_handler_field_submission_data extends views_handler_field {
11

    
12
  /**
13
   *
14
   */
15
  public 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
  /**
23
   *
24
   */
25
  public function option_definition() {
26
    $options = parent::option_definition();
27
    $options['format'] = array('default' => 'html');
28
    $options['custom_label'] = array('default' => 'default');
29
    $options['webform_nid'] = array('default' => NULL);
30
    $options['webform_cid'] = array('default' => NULL);
31
    $options['webform_datatype'] = array('default' => 'string');
32
    return $options;
33
  }
34

    
35
  /**
36
   *
37
   */
38
  public function options_form(&$form, &$form_state) {
39
    parent::options_form($form, $form_state);
40
    form_load_include($form_state, 'inc', 'webform', 'views/webform.views');
41

    
42
    $form['custom_label']['#type'] = 'radios';
43
    $form['custom_label']['#options'] = array(
44
      'default' => t('Use component label'),
45
      'custom' => t('Custom label'),
46
      'none' => t('No label'),
47
    );
48
    $form['custom_label']['#default_value'] = $this->options['custom_label'];
49
    $form['label']['#dependency'] = array('radio:options[custom_label]' => array('custom'));
50

    
51
    if (!$this->webform_expand) {
52
      $nid = (int) $this->options['webform_nid'];
53
      $cid = (int) $this->options['webform_cid'];
54

    
55
      // Helper function provides webform_nid and webform_cid options.
56
      _webform_views_options_form($form, $form_state, $nid, $cid);
57
    }
58

    
59
    // Modify behavior for the type of data in the component.
60
    $form['webform_datatype'] = array(
61
      '#type' => 'select',
62
      '#title' => t('Data type'),
63
      '#options' => array(
64
        'string' => t('String'),
65
        'number' => t('Number'),
66
      ),
67
      '#default_value' => $this->options['webform_datatype'],
68
    );
69

    
70
    // Provide the selection for the display format.
71
    $form['format'] = array(
72
      '#type' => 'select',
73
      '#title' => t('Display format'),
74
      '#options' => array(
75
        'html' => t('HTML'),
76
        'text' => t('Plain text'),
77
      ),
78
      '#default_value' => $this->options['format'],
79
    );
80
  }
81

    
82
  /**
83
   *
84
   */
85
  public function options_validate(&$form, &$form_state) {
86
    parent::options_validate($form, $form_state);
87
    if (!$this->webform_expand) {
88
      _webform_views_options_validate($form, $form_state);
89
    }
90
  }
91

    
92
  /**
93
   *
94
   */
95
  public function options_submit(&$form, &$form_state) {
96
    parent::options_submit($form, $form_state);
97
    if (!$this->webform_expand) {
98
      _webform_views_options_submit($form, $form_state);
99
    }
100
  }
101

    
102
  /**
103
   * Called to determine what to tell the clicksorter.
104
   */
105
  public function click_sort($order) {
106
    if (isset($this->field_alias)) {
107
      // Since fields should always have themselves already added, just
108
      // add a sort on the field.
109
      $params = $this->options['group_type'] != 'group' ? array('function' => $this->options['group_type']) : array();
110

    
111
      $join = new views_join();
112
      $extra = array(
113
        array(
114
          'field' => 'cid',
115
          'value' => $this->options['webform_cid'],
116
          'numeric' => TRUE,
117
        ),
118
        array(
119
          'field' => 'no',
120
          'value' => '0',
121
          'numeric' => TRUE,
122
        ),
123
      );
124
      $join->construct('webform_submitted_data', 'webform_submissions', 'sid', 'sid', $extra);
125
      $this->query->add_relationship('webform_submitted_data_click_sort', $join, 'webform_submissions');
126
      switch ($this->options['webform_datatype']) {
127
        case 'number':
128
          $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);
129
          break;
130

    
131
        default:
132
          $this->query->add_orderby('webform_submitted_data_click_sort', 'data', $order, $this->field_alias . '_click_sort', $params);
133
          break;
134
      }
135
    }
136
  }
137

    
138
  /**
139
   * Load the node and submissions needed for this components values.
140
   */
141
  public function pre_render(&$values) {
142
    $nid = $this->options['webform_nid'];
143
    $this->webform_node = node_load($nid);
144
    // Load all the submissions needed for this page. This is stored at the
145
    // view level to ensure it's available between fields so we don't load
146
    // them twice.
147
    if (!isset($this->view->_webform_submissions[$nid])) {
148
      module_load_include('inc', 'webform', 'includes/webform.submissions');
149
      $this->view->_webform_submissions[$nid] = array();
150
      $sids = array();
151
      foreach ($values as $value) {
152
        $sids[] = $value->{$this->field_alias};
153
      }
154
      if ($sids) {
155
        $this->view->_webform_submissions[$nid] = webform_get_submissions(array('sid' => $sids));
156
      }
157
    }
158
  }
159

    
160
  /**
161
   * Get this field's label based on the selected component.
162
   */
163
  public function label() {
164
    if ($this->options['custom_label'] === 'default' && isset($this->options['webform_cid'])) {
165
      if (isset($this->webform_node)) {
166
        $node = $this->webform_node;
167
      }
168
      else {
169
        $node = node_load($this->options['webform_nid']);
170
      }
171
      if ($node && isset($node->webform['components'][$this->options['webform_cid']])) {
172
        $component = $node->webform['components'][$this->options['webform_cid']];
173
        return $component['name'];
174
      }
175
    }
176
    elseif ($this->options['custom_label'] === 'custom' && isset($this->options['label'])) {
177
      return $this->options['label'];
178
    }
179
    return '';
180
  }
181

    
182
  /**
183
   * Render the field using the loaded submissions from pre_render().
184
   */
185
  public function render($row) {
186
    $sid = $this->get_value($row);
187
    $nid = $this->options['webform_nid'];
188
    $cid = $this->options['webform_cid'];
189
    $webform = $this->webform_node;
190
    if (isset($sid) && isset($webform->webform['components'][$cid])) {
191

    
192
      $component = $webform->webform['components'][$cid];
193
      $submission = $this->view->_webform_submissions[$nid][$sid];
194
      if ($submission->nid != $nid) {
195
        // The actual submission is from a different webform than the one used
196
        // to define the view. Rather than using the component with the same
197
        // cid, try to match the form_key.
198
        if (!isset($this->view->_webform_components[$nid][$submission->nid][$cid])) {
199
          if (!isset($this->view->_webform_components[$nid][$submission->nid]['webform'])) {
200
            $this->view->_webform_components[$nid][$submission->nid]['webform'] = $webform;
201
          }
202
          $this->view->_webform_components[$nid][$submission->nid][$cid] = $component;
203
          $submission_node = node_load($submission->nid);
204
          foreach ($submission_node->webform['components'] as $sub_cid => $sub_component) {
205
            if ((string) $sub_component['form_key'] === (string) $component['form_key'] && $sub_component['type'] == $component['type']) {
206
              $this->view->_webform_components[$nid][$submission->nid]['webform'] = $submission_node;
207
              $this->view->_webform_components[$nid][$submission->nid][$cid] = $sub_component;
208
              break;
209
            }
210
          }
211
        }
212
        $webform = $this->view->_webform_components[$nid][$submission->nid]['webform'];
213
        $component = $this->view->_webform_components[$nid][$submission->nid][$cid];
214
        // Note: $nid and $cid refer to the definition webform, not the
215
        // submission webform whereas $component refers to the submission
216
        // component.
217
      }
218

    
219
      if ($this->options['format'] == 'html') {
220
        $render = array('#submission' => $submission);
221
        _webform_client_form_add_component($webform, $component, NULL, $render, $render, $submission->data, 'html');
222
        $render = $render[$component['form_key']];
223
        // Remove display label.
224
        $render['#theme_wrappers'] = array();
225
      }
226
      else {
227
        // Plain text format is generated via invoking the table output to ensure output is sanitised.
228
        $data = isset($submission->data[$component['cid']]) ? $submission->data[$component['cid']] : NULL;
229
        $render = webform_component_invoke($component['type'], 'table', $component, $data);
230
      }
231
      // Webform renders empty values as a space, which prevents views empty
232
      // rewriting from being used. If empty is in use, change result to an
233
      // actual empty string.
234
      $render = render($render);
235
      if ($render === ' ' && strlen($this->options['empty'])) {
236
        $render = '';
237
      }
238
      return $render;
239
    }
240
  }
241

    
242
}