1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\UserForms\Model\Submission; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\FieldList; |
6
|
|
|
use SilverStripe\Forms\GridField\GridField; |
7
|
|
|
use SilverStripe\Forms\GridField\GridFieldButtonRow; |
8
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig; |
9
|
|
|
use SilverStripe\Forms\GridField\GridFieldDataColumns; |
10
|
|
|
use SilverStripe\Forms\GridField\GridFieldExportButton; |
11
|
|
|
use SilverStripe\Forms\GridField\GridFieldPrintButton; |
12
|
|
|
use SilverStripe\Forms\GridField\GridFieldToolbarHeader; |
13
|
|
|
use SilverStripe\Forms\ReadonlyField; |
14
|
|
|
use SilverStripe\ORM\DataObject; |
15
|
|
|
use SilverStripe\ORM\DB; |
16
|
|
|
use SilverStripe\Security\Member; |
17
|
|
|
|
18
|
|
|
class SubmittedForm extends DataObject |
19
|
|
|
{ |
20
|
|
|
private static $has_one = [ |
|
|
|
|
21
|
|
|
'SubmittedBy' => Member::class, |
22
|
|
|
'Parent' => DataObject::class, |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
private static $has_many = [ |
|
|
|
|
26
|
|
|
'Values' => SubmittedFormField::class |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
private static $cascade_deletes = [ |
|
|
|
|
30
|
|
|
'Values', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
private static $summary_fields = [ |
|
|
|
|
34
|
|
|
'ID', |
35
|
|
|
'Created' |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
private static $table_name = 'SubmittedForm'; |
|
|
|
|
39
|
|
|
|
40
|
|
|
public function requireDefaultRecords() |
41
|
|
|
{ |
42
|
|
|
parent::requireDefaultRecords(); |
43
|
|
|
|
44
|
|
|
// make sure to migrate the class across (prior to v5.x) |
45
|
|
|
DB::query("UPDATE \"SubmittedForm\" SET \"ParentClass\" = 'Page' WHERE \"ParentClass\" IS NULL"); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns the value of a relation or, in the case of this form, the value |
50
|
|
|
* of a given child {@link SubmittedFormField} |
51
|
|
|
* |
52
|
|
|
* @param string |
53
|
|
|
* |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
|
|
public function relField($fieldName) |
57
|
|
|
{ |
58
|
|
|
// default case |
59
|
|
|
if ($value = parent::relField($fieldName)) { |
60
|
|
|
return $value; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// check values for a form field with the matching name. |
64
|
|
|
$formField = SubmittedFormField::get()->filter(array( |
65
|
|
|
'ParentID' => $this->ID, |
66
|
|
|
'Name' => $fieldName |
67
|
|
|
))->first(); |
68
|
|
|
|
69
|
|
|
if ($formField) { |
|
|
|
|
70
|
|
|
return $formField->getFormattedValue(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return FieldList |
76
|
|
|
*/ |
77
|
|
|
public function getCMSFields() |
78
|
|
|
{ |
79
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) { |
80
|
|
|
$fields->removeByName('Values'); |
81
|
|
|
|
82
|
|
|
//check to ensure there is a Member to extract an Email from else null value |
83
|
|
|
if ($this->SubmittedBy() && $this->SubmittedBy()->exists()) { |
|
|
|
|
84
|
|
|
$submitter = $this->SubmittedBy()->Email; |
85
|
|
|
} else { |
86
|
|
|
$submitter = null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
//replace scaffolded field with readonly submitter |
90
|
|
|
$fields->replaceField( |
91
|
|
|
'SubmittedByID', |
92
|
|
|
ReadonlyField::create('Submitter', _t(__CLASS__ . '.SUBMITTER', 'Submitter'), $submitter) |
93
|
|
|
->addExtraClass('form-field--no-divider') |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$values = GridField::create( |
97
|
|
|
'Values', |
98
|
|
|
SubmittedFormField::class, |
99
|
|
|
$this->Values()->sort('Created', 'ASC') |
|
|
|
|
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
$exportColumns = array( |
103
|
|
|
'Title' => 'Title', |
104
|
|
|
'ExportValue' => 'Value' |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$config = GridFieldConfig::create(); |
108
|
|
|
$config->addComponent(new GridFieldDataColumns()); |
109
|
|
|
$config->addComponent(new GridFieldButtonRow('after')); |
110
|
|
|
$config->addComponent(new GridFieldExportButton('buttons-after-left', $exportColumns)); |
111
|
|
|
$config->addComponent(new GridFieldPrintButton('buttons-after-left')); |
112
|
|
|
$values->setConfig($config); |
113
|
|
|
|
114
|
|
|
$fields->addFieldToTab('Root.Main', $values); |
115
|
|
|
}); |
116
|
|
|
|
117
|
|
|
$fields = parent::getCMSFields(); |
118
|
|
|
|
119
|
|
|
return $fields; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param Member |
124
|
|
|
* |
125
|
|
|
* @return boolean |
126
|
|
|
*/ |
127
|
|
|
public function canCreate($member = null, $context = []) |
128
|
|
|
{ |
129
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
130
|
|
|
if ($extended !== null) { |
131
|
|
|
return $extended; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($this->Parent()) { |
|
|
|
|
135
|
|
|
return $this->Parent()->canCreate($member); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return parent::canCreate($member); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param Member |
143
|
|
|
* |
144
|
|
|
* @return boolean |
145
|
|
|
*/ |
146
|
|
|
public function canView($member = null) |
147
|
|
|
{ |
148
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
149
|
|
|
|
150
|
|
|
if ($extended !== null) { |
151
|
|
|
return $extended; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if ($this->Parent()) { |
155
|
|
|
return $this->Parent()->canView($member); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return parent::canView($member); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param Member |
163
|
|
|
* |
164
|
|
|
* @return boolean |
165
|
|
|
*/ |
166
|
|
|
public function canEdit($member = null) |
167
|
|
|
{ |
168
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
169
|
|
|
|
170
|
|
|
if ($extended !== null) { |
171
|
|
|
return $extended; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if ($this->Parent()) { |
175
|
|
|
return $this->Parent()->canEdit($member); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return parent::canEdit($member); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param Member |
183
|
|
|
* |
184
|
|
|
* @return boolean |
185
|
|
|
*/ |
186
|
|
|
public function canDelete($member = null) |
187
|
|
|
{ |
188
|
|
|
$extended = $this->extendedCan(__FUNCTION__, $member); |
189
|
|
|
|
190
|
|
|
if ($extended !== null) { |
191
|
|
|
return $extended; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if ($this->Parent()) { |
195
|
|
|
return $this->Parent()->canDelete($member); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return parent::canDelete($member); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Before we delete this form make sure we delete all the field values so |
203
|
|
|
* that we don't leave old data round. |
204
|
|
|
* |
205
|
|
|
* @return void |
206
|
|
|
*/ |
207
|
|
|
protected function onBeforeDelete() |
208
|
|
|
{ |
209
|
|
|
if ($this->Values()) { |
210
|
|
|
foreach ($this->Values() as $value) { |
211
|
|
|
$value->delete(); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
parent::onBeforeDelete(); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|