|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Firesphere\PartialUserforms\Models; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Control\Controller; |
|
6
|
|
|
use SilverStripe\Forms\FieldList; |
|
7
|
|
|
use SilverStripe\Forms\GridField\GridField; |
|
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\ORM\DataList; |
|
13
|
|
|
use SilverStripe\ORM\DataObject; |
|
14
|
|
|
use SilverStripe\Security\Member; |
|
15
|
|
|
use SilverStripe\UserForms\Model\Submission\SubmittedForm; |
|
16
|
|
|
use SilverStripe\UserForms\Model\UserDefinedForm; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class \Firesphere\PartialUserforms\Models\PartialFormSubmission |
|
20
|
|
|
* |
|
21
|
|
|
* @property boolean $IsSend |
|
22
|
|
|
* @property string $Password |
|
23
|
|
|
* @property int $UserDefinedFormID |
|
24
|
|
|
* @method DataObject UserDefinedForm() |
|
25
|
|
|
* @method DataList|PartialFieldSubmission[] PartialFields() |
|
26
|
|
|
*/ |
|
27
|
|
|
class PartialFormSubmission extends SubmittedForm |
|
28
|
|
|
{ |
|
29
|
|
|
private static $table_name = 'PartialFormSubmission'; |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private static $db = [ |
|
|
|
|
|
|
35
|
|
|
'IsSend' => 'Boolean(false)', |
|
36
|
|
|
'Password' => 'Varchar(64)', |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
private static $has_one = [ |
|
|
|
|
|
|
43
|
|
|
'UserDefinedForm' => DataObject::class |
|
44
|
|
|
]; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
private static $has_many = [ |
|
|
|
|
|
|
50
|
|
|
'PartialFields' => PartialFieldSubmission::class |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
private static $special_characters = [ |
|
54
|
|
|
'!', |
|
55
|
|
|
'@', |
|
56
|
|
|
'#', |
|
57
|
|
|
'$', |
|
58
|
|
|
'%', |
|
59
|
|
|
'^', |
|
60
|
|
|
'&', |
|
61
|
|
|
'*', |
|
62
|
|
|
'(', |
|
63
|
|
|
')', |
|
64
|
|
|
'_', |
|
65
|
|
|
'-', |
|
66
|
|
|
'=', |
|
67
|
|
|
'+', |
|
68
|
|
|
';', |
|
69
|
|
|
':', |
|
70
|
|
|
',', |
|
71
|
|
|
'.', |
|
72
|
|
|
'?' |
|
73
|
|
|
]; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return FieldList |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getCMSFields() |
|
79
|
|
|
{ |
|
80
|
|
|
/** @var FieldList $fields */ |
|
81
|
|
|
$fields = parent::getCMSFields(); |
|
82
|
|
|
$fields->removeByName(['Values', 'IsSend', 'PartialFields']); |
|
83
|
|
|
|
|
84
|
|
|
$values = GridField::create( |
|
85
|
|
|
'PartialFields', |
|
86
|
|
|
_t(static::class . '.PARTIALFIELDS', 'Partial fields'), |
|
87
|
|
|
$this->PartialFields()->sort('Created', 'ASC') |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
$exportColumns = array( |
|
91
|
|
|
'Title' => 'Title', |
|
92
|
|
|
'ExportValue' => 'Value' |
|
93
|
|
|
); |
|
94
|
|
|
|
|
95
|
|
|
$config = new GridFieldConfig(); |
|
96
|
|
|
$config->addComponent(new GridFieldDataColumns()); |
|
97
|
|
|
$config->addComponent(new GridFieldExportButton('after', $exportColumns)); |
|
98
|
|
|
$config->addComponent(new GridFieldPrintButton()); |
|
99
|
|
|
|
|
100
|
|
|
$values->setConfig($config); |
|
101
|
|
|
|
|
102
|
|
|
$fields->addFieldToTab('Root.Main', $values); |
|
103
|
|
|
|
|
104
|
|
|
return $fields; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return DataObject|UserDefinedForm |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getParent() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->UserDefinedForm(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* If the submission is password protected, generate a password. |
|
117
|
|
|
*/ |
|
118
|
|
|
public function onBeforeWrite() |
|
119
|
|
|
{ |
|
120
|
|
|
parent::onBeforeWrite(); |
|
121
|
|
|
if (!$this->Password && |
|
122
|
|
|
$this->UserDefinedFormID && |
|
123
|
|
|
$this->getParent()->PasswordProtected |
|
124
|
|
|
) { |
|
125
|
|
|
$chars = range('A', 'Z'); |
|
126
|
|
|
$chars = array_merge($chars, range('a', 'z')); |
|
127
|
|
|
$chars = array_merge($chars, range(0, 9)); |
|
128
|
|
|
$chars = array_merge($chars, static::$special_characters); |
|
|
|
|
|
|
129
|
|
|
shuffle($chars); |
|
130
|
|
|
$pwd = implode(array_slice($chars, 0, 10)); |
|
131
|
|
|
$this->Password = hash('SHA256', $pwd); |
|
132
|
|
|
Controller::curr()->getRequest()->getSession()->set('FormPassword', $pwd); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param Member |
|
138
|
|
|
* |
|
139
|
|
|
* @return boolean|string |
|
140
|
|
|
*/ |
|
141
|
|
|
public function canCreate($member = null, $context = []) |
|
142
|
|
|
{ |
|
143
|
|
|
if ($this->UserDefinedForm()) { |
|
144
|
|
|
return $this->UserDefinedForm()->canCreate($member, $context); |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return parent::canCreate($member); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param Member |
|
152
|
|
|
* |
|
153
|
|
|
* @return boolean|string |
|
154
|
|
|
*/ |
|
155
|
|
|
public function canView($member = null) |
|
156
|
|
|
{ |
|
157
|
|
|
if ($this->UserDefinedForm()) { |
|
158
|
|
|
return $this->UserDefinedForm()->canView($member); |
|
|
|
|
|
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return parent::canView($member); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param Member |
|
166
|
|
|
* |
|
167
|
|
|
* @return boolean|string |
|
168
|
|
|
*/ |
|
169
|
|
|
public function canEdit($member = null) |
|
170
|
|
|
{ |
|
171
|
|
|
if ($this->UserDefinedForm()) { |
|
172
|
|
|
return $this->UserDefinedForm()->canEdit($member); |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return parent::canEdit($member); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @param Member |
|
180
|
|
|
* |
|
181
|
|
|
* @return boolean|string |
|
182
|
|
|
*/ |
|
183
|
|
|
public function canDelete($member = null) |
|
184
|
|
|
{ |
|
185
|
|
|
if ($this->UserDefinedForm()) { |
|
186
|
|
|
return $this->UserDefinedForm()->canDelete($member); |
|
|
|
|
|
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
return parent::canDelete($member); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|