This repository has been archived by the owner on Mar 29, 2018. It is now read-only.
/
Submission.php
123 lines (108 loc) · 3.5 KB
/
Submission.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace Drupal\little_helpers;
class Submission {
protected $node;
protected $submission;
public $webform;
public $remote_addr;
public $submitted;
protected $data;
public static function load($nid, $sid) {
$node = \Drupal::entityManager()->getStorage('node')->load($nid);
$submission = webform_get_submission($nid, $sid);
if ($node && $submission) {
return new static($node, $submission);
}
}
public function __construct($node, $submission) {
$this->submission = $submission;
$this->node = $node;
$this->webform = new Webform($node);
$this->submitted = $submission->submitted;
$this->remote_addr = $submission->remote_addr;
$this->data = array();
if (!isset($submission->tracking)) {
$submission->tracking = (object) array();
if (\Drupal::moduleHandler()->moduleExists('webform_tracking') && isset($submission->sid)) {
webform_tracking_load($submission);
}
}
// Some components like checkboxes and fieldsets may have no values
// We want to return NULL in that case instead of throwing a notice.
$webform4 = Webform::is_webform4();
foreach (array_keys($this->node->webform['components']) as $cid) {
if (isset($this->submission->data[$cid])) {
$this->data[$cid] = $webform4 ?
$this->submission->data[$cid] :
$this->submission->data[$cid]['value'];
}
else {
$this->data[$cid] = array(NULL);
}
}
}
public function getNode() {
return $this->node;
}
public function valueByKey($form_key) {
if ($component = &$this->webform->componentByKey($form_key)) {
return $this->valueByCid($component['cid']);
}
elseif (isset($this->submission->tracking->$form_key)) {
return $this->submission->tracking->$form_key;
}
}
public function valuesByKey($form_key) {
if ($component = &$this->webform->componentByKey($form_key)) {
return $this->valuesByCid($component['cid']);
}
elseif (isset($this->submission->tracking->$form_key)) {
return $this->submission->tracking->$form_key;
}
}
public function valuesByType($type) {
$values = array();
foreach (array_keys($this->componentsByType($type)) as $cid) {
$values[$cid] = $this->valueByCid($cid);
}
return $values;
}
public function valueByCid($cid) {
reset($this->data[$cid]);
return current($this->data[$cid]);
}
public function valuesByCid($cid) {
return $this->data[$cid];
}
public function unwrap() {
return $this->submission;
}
public function ids() {
return array(
'nid' => $this->node->nid,
'sid' => $this->submission->sid,
);
}
/**
* @deprecated Serializing submission objects is not a good idea especially
* for long term storage.
*/
public function __sleep() {
$this->nid = $this->node->nid;
$this->sid = $this->submission->sid;
return array('nid', 'sid');
}
/**
* @deprecated Serializing submission objects is not a good idea especially
* for long term storage.
*/
public function __wakeup() {
if (!($node = \Drupal::entityManager()->getStorage('node')->load($this->nid))) {
throw new \UnexpectedValueException('Tried to __wakeup with non-existing node.');
}
if (!($submission = webform_get_submission($this->nid, $this->sid))) {
throw new \UnexpectedValueException('Tried to __wakeup with non-existing submission.');
}
$this->__construct($node, $submission);
}
}