webform_plugin_row_submission_view.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Contains the submission view row style plugin.
  4. *
  5. * Plugin which performs a webform_submission_render on the resulting object.
  6. *
  7. * Most of the code on this object is in the theme function.
  8. *
  9. * @ingroup views_row_plugins
  10. */
  11. class webform_views_plugin_row_submission_view extends views_plugin_row {
  12. /**
  13. * Basic properties that let the row style follow relationships.
  14. *
  15. * @var string
  16. */
  17. public $base_table = 'webform_submissions';
  18. public $base_field = 'sid';
  19. /**
  20. * Stores the nodes loaded with pre_render.
  21. *
  22. * @var array
  23. */
  24. private $submissions = array();
  25. private $nodes = array();
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function option_definition() {
  30. $options = parent::option_definition();
  31. $options['format'] = array('default' => 'html');
  32. return $options;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function options_form(&$form, &$form_state) {
  38. parent::options_form($form, $form_state);
  39. $options = $this->options_form_summary_options();
  40. $form['format'] = array(
  41. '#type' => 'radios',
  42. '#options' => $options,
  43. '#title' => t('Display mode'),
  44. '#default_value' => $this->options['format'],
  45. );
  46. }
  47. /**
  48. * Return the main options, which are shown in the summary title.
  49. */
  50. public function options_form_summary_options() {
  51. return array(
  52. 'html' => t('HTML'),
  53. 'text' => t('Plain text'),
  54. );
  55. }
  56. /**
  57. *
  58. */
  59. public function summary_title() {
  60. $options = $this->options_form_summary_options();
  61. return check_plain($options[$this->options['format']]);
  62. }
  63. /**
  64. *
  65. */
  66. public function pre_render($values) {
  67. $sids = array();
  68. foreach ($values as $row) {
  69. $sids[] = $row->{$this->field_alias};
  70. }
  71. module_load_include('inc', 'webform', 'includes/webform.submissions');
  72. $this->submissions = $sids ? webform_get_submissions(array('sid' => $sids)) : array();
  73. $nids = array();
  74. foreach ($this->submissions as $sid => $submission) {
  75. $nids[] = $submission->nid;
  76. }
  77. $nids = array_unique($nids);
  78. $this->nodes = $nids ? node_load_multiple($nids) : array();
  79. }
  80. /**
  81. *
  82. */
  83. public function render($row) {
  84. if (isset($this->submissions[$row->{$this->field_alias}])) {
  85. $submission = $this->submissions[$row->{$this->field_alias}];
  86. $node = $this->nodes[$submission->nid];
  87. $submission->view = $this->view;
  88. $format = $this->options['format'];
  89. $build = webform_submission_render($node, $submission, NULL, $format);
  90. // Add extra theme functions:
  91. $themes = array();
  92. foreach ($build['#theme'] as $hook) {
  93. $themes = array_merge($themes, _views_theme_functions($hook, $this->view, $this->view->display[$this->view->current_display]));
  94. }
  95. $build['#theme'] = $themes;
  96. // Render built submission, and if unsanitized plain text is used, make
  97. // it safe for display.
  98. $render = drupal_render($build);
  99. return $format == 'html' ? $render : nl2br(check_plain($render));
  100. }
  101. }
  102. }