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
|
/**
|
16
|
*
|
17
|
*/
|
18
|
public function construct() {
|
19
|
// We need to set this property before calling the construct() chain
|
20
|
// as we use it in the option_definintion() call.
|
21
|
$this->webform_expand = $this->definition['webform_expand'];
|
22
|
parent::construct();
|
23
|
}
|
24
|
|
25
|
/**
|
26
|
*
|
27
|
*/
|
28
|
public function option_definition() {
|
29
|
$options = parent::option_definition();
|
30
|
$options['format'] = array('default' => 'html');
|
31
|
$options['custom_label'] = array('default' => 'default');
|
32
|
$options['webform_nid'] = array('default' => NULL);
|
33
|
$options['webform_cid'] = array('default' => NULL);
|
34
|
$options['webform_datatype'] = array('default' => 'string');
|
35
|
return $options;
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
*
|
40
|
*/
|
41
|
public function options_form(&$form, &$form_state) {
|
42
|
parent::options_form($form, $form_state);
|
43
|
form_load_include($form_state, 'inc', 'webform', 'views/webform.views');
|
44
|
|
45
|
$form['custom_label']['#type'] = 'radios';
|
46
|
$form['custom_label']['#options'] = array(
|
47
|
'default' => t('Use component label'),
|
48
|
'custom' => t('Custom label'),
|
49
|
'none' => t('No label'),
|
50
|
);
|
51
|
$form['custom_label']['#default_value'] = $this->options['custom_label'];
|
52
|
$form['label']['#dependency'] = array('radio:options[custom_label]' => array('custom'));
|
53
|
|
54
|
if (!$this->webform_expand) {
|
55
|
$nid = (int) $this->options['webform_nid'];
|
56
|
$cid = (int) $this->options['webform_cid'];
|
57
|
|
58
|
// Helper function provides webform_nid and webform_cid options.
|
59
|
_webform_views_options_form($form, $form_state, $nid, $cid);
|
60
|
}
|
61
|
|
62
|
// Modify behavior for the type of data in the component.
|
63
|
$form['webform_datatype'] = array(
|
64
|
'#type' => 'select',
|
65
|
'#title' => t('Data type'),
|
66
|
'#options' => array(
|
67
|
'string' => t('String'),
|
68
|
'number' => t('Number'),
|
69
|
),
|
70
|
'#default_value' => $this->options['webform_datatype'],
|
71
|
);
|
72
|
|
73
|
// Provide the selection for the display format.
|
74
|
$form['format'] = array(
|
75
|
'#type' => 'select',
|
76
|
'#title' => t('Display format'),
|
77
|
'#options' => array(
|
78
|
'html' => t('HTML'),
|
79
|
'text' => t('Plain text'),
|
80
|
),
|
81
|
'#default_value' => $this->options['format'],
|
82
|
);
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
*
|
87
|
*/
|
88
|
public function options_validate(&$form, &$form_state) {
|
89
|
parent::options_validate($form, $form_state);
|
90
|
if (!$this->webform_expand) {
|
91
|
_webform_views_options_validate($form, $form_state);
|
92
|
}
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
*
|
97
|
*/
|
98
|
public function options_submit(&$form, &$form_state) {
|
99
|
parent::options_submit($form, $form_state);
|
100
|
if (!$this->webform_expand) {
|
101
|
_webform_views_options_submit($form, $form_state);
|
102
|
}
|
103
|
}
|
104
|
|
105
|
/**
|
106
|
* Called to determine what to tell the clicksorter.
|
107
|
*/
|
108
|
public function click_sort($order) {
|
109
|
if (isset($this->field_alias)) {
|
110
|
// Since fields should always have themselves already added, just
|
111
|
// add a sort on the field.
|
112
|
$params = $this->options['group_type'] != 'group' ? array('function' => $this->options['group_type']) : array();
|
113
|
|
114
|
$join = new views_join();
|
115
|
$extra = array(
|
116
|
array(
|
117
|
'field' => 'cid',
|
118
|
'value' => $this->options['webform_cid'],
|
119
|
'numeric' => TRUE,
|
120
|
),
|
121
|
array(
|
122
|
'field' => 'no',
|
123
|
'value' => '0',
|
124
|
'numeric' => TRUE,
|
125
|
),
|
126
|
);
|
127
|
$join->construct('webform_submitted_data', 'webform_submissions', 'sid', 'sid', $extra);
|
128
|
$this->query->add_relationship('webform_submitted_data_click_sort', $join, 'webform_submissions');
|
129
|
switch ($this->options['webform_datatype']) {
|
130
|
case 'number':
|
131
|
$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);
|
132
|
break;
|
133
|
|
134
|
default:
|
135
|
$this->query->add_orderby('webform_submitted_data_click_sort', 'data', $order, $this->field_alias . '_click_sort', $params);
|
136
|
break;
|
137
|
}
|
138
|
}
|
139
|
}
|
140
|
|
141
|
/**
|
142
|
* Load the node and submissions needed for this components values.
|
143
|
*/
|
144
|
public function pre_render(&$values) {
|
145
|
$nid = $this->options['webform_nid'];
|
146
|
$this->webform_node = node_load($nid);
|
147
|
// Load all the submissions needed for this page. This is stored at the
|
148
|
// view level to ensure it's available between fields so we don't load
|
149
|
// them twice.
|
150
|
if (!isset($this->view->_webform_submissions[$nid])) {
|
151
|
module_load_include('inc', 'webform', 'includes/webform.submissions');
|
152
|
$this->view->_webform_submissions[$nid] = array();
|
153
|
$sids = array();
|
154
|
foreach ($values as $value) {
|
155
|
$sids[] = $value->{$this->field_alias};
|
156
|
}
|
157
|
if ($sids) {
|
158
|
$this->view->_webform_submissions[$nid] = webform_get_submissions(array('sid' => $sids));
|
159
|
}
|
160
|
}
|
161
|
}
|
162
|
|
163
|
/**
|
164
|
* Get this field's label based on the selected component.
|
165
|
*/
|
166
|
public function label() {
|
167
|
if ($this->options['custom_label'] === 'default' && isset($this->options['webform_cid'])) {
|
168
|
if (isset($this->webform_node)) {
|
169
|
$node = $this->webform_node;
|
170
|
}
|
171
|
else {
|
172
|
$node = node_load($this->options['webform_nid']);
|
173
|
}
|
174
|
if ($node && isset($node->webform['components'][$this->options['webform_cid']])) {
|
175
|
$component = $node->webform['components'][$this->options['webform_cid']];
|
176
|
return $component['name'];
|
177
|
}
|
178
|
}
|
179
|
elseif ($this->options['custom_label'] === 'custom' && isset($this->options['label'])) {
|
180
|
return $this->options['label'];
|
181
|
}
|
182
|
return '';
|
183
|
}
|
184
|
|
185
|
/**
|
186
|
* Render the field using the loaded submissions from pre_render().
|
187
|
*/
|
188
|
public function render($row) {
|
189
|
$sid = $this->get_value($row);
|
190
|
$nid = $this->options['webform_nid'];
|
191
|
$cid = $this->options['webform_cid'];
|
192
|
$webform = $this->webform_node;
|
193
|
if (isset($sid) && isset($webform->webform['components'][$cid])) {
|
194
|
|
195
|
$component = $webform->webform['components'][$cid];
|
196
|
$submission = $this->view->_webform_submissions[$nid][$sid];
|
197
|
if ($submission->nid != $nid) {
|
198
|
// The actual submission is from a different webform than the one used to define the view.
|
199
|
// Rather than using the component with the same cid, try to match the form_key.
|
200
|
if (!isset($this->view->_webform_components[$nid][$submission->nid][$cid])) {
|
201
|
if (!isset($this->view->_webform_components[$nid][$submission->nid]['webform'])) {
|
202
|
$this->view->_webform_components[$nid][$submission->nid]['webform'] = $webform;
|
203
|
}
|
204
|
$this->view->_webform_components[$nid][$submission->nid][$cid] = $component;
|
205
|
$submission_node = node_load($submission->nid);
|
206
|
foreach ($submission_node->webform['components'] as $sub_cid => $sub_component) {
|
207
|
if ((string) $sub_component['form_key'] === (string) $component['form_key'] && $sub_component['type'] == $component['type']) {
|
208
|
$this->view->_webform_components[$nid][$submission->nid]['webform'] = $submission_node;
|
209
|
$this->view->_webform_components[$nid][$submission->nid][$cid] = $sub_component;
|
210
|
break;
|
211
|
}
|
212
|
}
|
213
|
}
|
214
|
$webform = $this->view->_webform_components[$nid][$submission->nid]['webform'];
|
215
|
$component = $this->view->_webform_components[$nid][$submission->nid][$cid];
|
216
|
// Note: $nid and $cid refer to the definition webform, not the submission webform
|
217
|
// whereas $component refers to the submission component.
|
218
|
}
|
219
|
|
220
|
if ($this->options['format'] == 'html') {
|
221
|
$render = array('#submission' => $submission);
|
222
|
_webform_client_form_add_component($webform, $component, NULL, $render, $render, $submission->data, 'html');
|
223
|
$render = $render[$component['form_key']];
|
224
|
// Remove display label.
|
225
|
$render['#theme_wrappers'] = array();
|
226
|
}
|
227
|
else {
|
228
|
// Plain text format is generated via invoking the table output to ensure output is sanitised.
|
229
|
$data = isset($submission->data[$component['cid']]) ? $submission->data[$component['cid']] : NULL;
|
230
|
$render = webform_component_invoke($component['type'], 'table', $component, $data);
|
231
|
}
|
232
|
// Webform renders empty values as a space, which prevents views empty
|
233
|
// rewriting from being used. If empty is in use, change result to an
|
234
|
// actual empty string.
|
235
|
$render = render($render);
|
236
|
if ($render === ' ' && strlen($this->options['empty'])) {
|
237
|
$render = '';
|
238
|
}
|
239
|
return $render;
|
240
|
}
|
241
|
}
|
242
|
|
243
|
}
|