View source
<?php
class webform_handler_field_submission_data extends views_handler_field {
public function construct() {
$this->webform_expand = $this->definition['webform_expand'];
parent::construct();
}
public function option_definition() {
$options = parent::option_definition();
$options['format'] = array(
'default' => 'html',
);
$options['custom_label'] = array(
'default' => 'default',
);
$options['webform_nid'] = array(
'default' => NULL,
);
$options['webform_cid'] = array(
'default' => NULL,
);
$options['webform_datatype'] = array(
'default' => 'string',
);
return $options;
}
public function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
form_load_include($form_state, 'inc', 'webform', 'views/webform.views');
$form['custom_label']['#type'] = 'radios';
$form['custom_label']['#options'] = array(
'default' => t('Use component label'),
'custom' => t('Custom label'),
'none' => t('No label'),
);
$form['custom_label']['#default_value'] = $this->options['custom_label'];
$form['label']['#dependency'] = array(
'radio:options[custom_label]' => array(
'custom',
),
);
if (!$this->webform_expand) {
$nid = (int) $this->options['webform_nid'];
$cid = (int) $this->options['webform_cid'];
_webform_views_options_form($form, $form_state, $nid, $cid);
}
$form['webform_datatype'] = array(
'#type' => 'select',
'#title' => t('Data type'),
'#options' => array(
'string' => t('String'),
'number' => t('Number'),
),
'#default_value' => $this->options['webform_datatype'],
);
$form['format'] = array(
'#type' => 'select',
'#title' => t('Display format'),
'#options' => array(
'html' => t('HTML'),
'text' => t('Plain text'),
),
'#default_value' => $this->options['format'],
);
}
public function options_validate(&$form, &$form_state) {
parent::options_validate($form, $form_state);
if (!$this->webform_expand) {
_webform_views_options_validate($form, $form_state);
}
}
public function options_submit(&$form, &$form_state) {
parent::options_submit($form, $form_state);
if (!$this->webform_expand) {
_webform_views_options_submit($form, $form_state);
}
}
public function click_sort($order) {
if (isset($this->field_alias)) {
$params = $this->options['group_type'] != 'group' ? array(
'function' => $this->options['group_type'],
) : array();
$join = new views_join();
$extra = array(
array(
'field' => 'cid',
'value' => $this->options['webform_cid'],
'numeric' => TRUE,
),
array(
'field' => 'no',
'value' => '0',
'numeric' => TRUE,
),
);
$join
->construct('webform_submitted_data', 'webform_submissions', 'sid', 'sid', $extra);
$this->query
->add_relationship('webform_submitted_data_click_sort', $join, 'webform_submissions');
switch ($this->options['webform_datatype']) {
case 'number':
$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);
break;
default:
$this->query
->add_orderby('webform_submitted_data_click_sort', 'data', $order, $this->field_alias . '_click_sort', $params);
break;
}
}
}
public function pre_render(&$values) {
$nid = $this->options['webform_nid'];
$this->webform_node = node_load($nid);
if (!isset($this->view->_webform_submissions[$nid])) {
module_load_include('inc', 'webform', 'includes/webform.submissions');
$this->view->_webform_submissions[$nid] = array();
$sids = array();
foreach ($values as $value) {
$sids[] = $value->{$this->field_alias};
}
if ($sids) {
$this->view->_webform_submissions[$nid] = webform_get_submissions(array(
'sid' => $sids,
));
}
}
}
public function label() {
if ($this->options['custom_label'] === 'default' && isset($this->options['webform_cid'])) {
if (isset($this->webform_node)) {
$node = $this->webform_node;
}
else {
$node = node_load($this->options['webform_nid']);
}
if ($node && isset($node->webform['components'][$this->options['webform_cid']])) {
$component = $node->webform['components'][$this->options['webform_cid']];
return $component['name'];
}
}
elseif ($this->options['custom_label'] === 'custom' && isset($this->options['label'])) {
return $this->options['label'];
}
return '';
}
public function render($row) {
$sid = $this
->get_value($row);
$nid = $this->options['webform_nid'];
$cid = $this->options['webform_cid'];
$webform = $this->webform_node;
if (isset($sid) && isset($webform->webform['components'][$cid])) {
$component = $webform->webform['components'][$cid];
$submission = $this->view->_webform_submissions[$nid][$sid];
if ($submission->nid != $nid) {
if (!isset($this->view->_webform_components[$nid][$submission->nid][$cid])) {
if (!isset($this->view->_webform_components[$nid][$submission->nid]['webform'])) {
$this->view->_webform_components[$nid][$submission->nid]['webform'] = $webform;
}
$this->view->_webform_components[$nid][$submission->nid][$cid] = $component;
$submission_node = node_load($submission->nid);
foreach ($submission_node->webform['components'] as $sub_cid => $sub_component) {
if ((string) $sub_component['form_key'] === (string) $component['form_key'] && $sub_component['type'] == $component['type']) {
$this->view->_webform_components[$nid][$submission->nid]['webform'] = $submission_node;
$this->view->_webform_components[$nid][$submission->nid][$cid] = $sub_component;
break;
}
}
}
$webform = $this->view->_webform_components[$nid][$submission->nid]['webform'];
$component = $this->view->_webform_components[$nid][$submission->nid][$cid];
}
if ($this->options['format'] == 'html') {
$render = array(
'#submission' => $submission,
);
_webform_client_form_add_component($webform, $component, NULL, $render, $render, $submission->data, 'html');
$render = $render[$component['form_key']];
$render['#theme_wrappers'] = array();
}
else {
$data = isset($submission->data[$component['cid']]) ? $submission->data[$component['cid']] : NULL;
$render = webform_component_invoke($component['type'], 'table', $component, $data);
}
$render = render($render);
if ($render === ' ' && strlen($this->options['empty'])) {
$render = '';
}
return $render;
}
}
}