webform_handler_field_submission_link.inc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Views handler to display links to a submission.
  4. *
  5. * Field handler to present a link to the user.
  6. */
  7. class webform_handler_field_submission_link extends views_handler_field {
  8. public $link_type;
  9. /**
  10. * {@inheritdoc}
  11. */
  12. public function construct() {
  13. // We need to set this property before calling the construct() chain
  14. // as we use it in the option_definintion() call.
  15. $this->link_type = $this->definition['link_type'];
  16. parent::construct();
  17. $this->additional_fields['sid'] = 'sid';
  18. $this->additional_fields['nid'] = 'nid';
  19. $this->additional_fields['serial'] = 'serial';
  20. $this->additional_fields['is_draft'] = 'is_draft';
  21. }
  22. /**
  23. *
  24. */
  25. public function allow_advanced_render() {
  26. return FALSE;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function option_definition() {
  32. $options = parent::option_definition();
  33. $options['label'] = array('default' => '', 'translatable' => TRUE);
  34. $options['text'] = array('default' => $this->link_type, 'translatable' => TRUE);
  35. $options['access_check'] = array('default' => TRUE);
  36. return $options;
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function options_form(&$form, &$form_state) {
  42. parent::options_form($form, $form_state);
  43. $form['text'] = array(
  44. '#type' => 'textfield',
  45. '#title' => t('Text to display'),
  46. '#default_value' => $this->options['text'],
  47. '#description' => t('The token [serial] will be replaced with the serial number and draft indication.'),
  48. );
  49. $form['access_check'] = array(
  50. '#type' => 'checkbox',
  51. '#title' => t('Verify submission access for each link'),
  52. '#default_value' => $this->options['access_check'],
  53. );
  54. }
  55. /**
  56. *
  57. */
  58. public function render($values) {
  59. $sid = $values->{$this->aliases['sid']};
  60. $nid = $values->{$this->aliases['nid']};
  61. $serial = $values->{$this->aliases['serial']};
  62. $is_draft = $values->{$this->aliases['is_draft']};
  63. $text = str_ireplace('[serial]',
  64. $serial . ($is_draft ? ' (' . t('draft') . ')' : ''),
  65. $this->options['text']);
  66. switch ($this->link_type) {
  67. case 'view':
  68. $text = !empty($text) ? $text : t('view');
  69. $link = l($text, "node/$nid/submission/$sid");
  70. break;
  71. case 'edit':
  72. $text = !empty($text) ? $text : t('edit');
  73. $link = l($text, "node/$nid/submission/$sid/edit");
  74. break;
  75. case 'delete':
  76. $text = !empty($text) ? $text : t('delete');
  77. $path = drupal_get_path_alias($_GET['q']);
  78. $link = l($text, "node/$nid/submission/$sid/delete", array('query' => array('destination' => $path)));
  79. break;
  80. default:
  81. return;
  82. }
  83. if ($this->options['access_check']) {
  84. module_load_include('inc', 'webform', 'includes/webform.submissions');
  85. $node = node_load($nid);
  86. $submission = webform_get_submission($nid, $sid);
  87. if (!webform_submission_access($node, $submission, $this->link_type)) {
  88. return;
  89. }
  90. }
  91. return $link;
  92. }
  93. }