webform_handler_field_submission_data.inc 8.7 KB

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