|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* AttendeeExtraField.php |
|
4
|
|
|
* |
|
5
|
|
|
* @author Bram de Leeuw |
|
6
|
|
|
* Date: 24/05/17 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Broarm\EventTickets; |
|
10
|
|
|
|
|
11
|
|
|
use CheckboxField; |
|
12
|
|
|
use Convert; |
|
13
|
|
|
use DataObject; |
|
14
|
|
|
use FieldList; |
|
15
|
|
|
use FormField; |
|
16
|
|
|
use ReadonlyField; |
|
17
|
|
|
use Tab; |
|
18
|
|
|
use TabSet; |
|
19
|
|
|
use TextField; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class AttendeeExtraField |
|
23
|
|
|
* todo write migration from AttendeeExtraField -> UserField |
|
24
|
|
|
* |
|
25
|
|
|
* @property string Name |
|
26
|
|
|
* @property string Title |
|
27
|
|
|
* @property string Default |
|
28
|
|
|
* @property string ExtraClass |
|
29
|
|
|
* @property boolean Required |
|
30
|
|
|
* @property boolean Editable |
|
31
|
|
|
* |
|
32
|
|
|
* @method \CalendarEvent Event() |
|
33
|
|
|
*/ |
|
34
|
|
|
class UserField extends DataObject |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @var FormField |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $fieldType = 'FormField'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Field name to be used in the AttendeeField (Composite field) |
|
43
|
|
|
* |
|
44
|
|
|
* @see AttendeeField::__construct() |
|
45
|
|
|
* |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $fieldName; |
|
49
|
|
|
|
|
50
|
|
|
private static $db = array( |
|
|
|
|
|
|
51
|
|
|
'Name' => 'Varchar(255)', // mostly here for default fields lookup |
|
52
|
|
|
'Title' => 'Varchar(255)', |
|
53
|
|
|
'Default' => 'Varchar(255)', |
|
54
|
|
|
'ExtraClass' => 'Varchar(255)', |
|
55
|
|
|
'Required' => 'Boolean', |
|
56
|
|
|
'Editable' => 'Boolean', |
|
57
|
|
|
'Sort' => 'Int' |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
private static $defaults = array( |
|
|
|
|
|
|
61
|
|
|
'Editable' => 1 |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
private static $default_sort = 'Sort ASC'; |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
private static $has_one = array( |
|
|
|
|
|
|
67
|
|
|
'Event' => 'CalendarEvent' |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
private static $belongs_many_many = array( |
|
|
|
|
|
|
71
|
|
|
'Attendees' => 'Broarm\EventTickets\Attendee' |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
private static $summary_fields = array( |
|
|
|
|
|
|
75
|
|
|
'singular_name' => 'Type of field', |
|
76
|
|
|
'Title' => 'Title', |
|
77
|
|
|
'RequiredNice' => 'Required field' |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
private static $translate = array( |
|
|
|
|
|
|
81
|
|
|
'Title', |
|
82
|
|
|
'Default' |
|
83
|
|
|
); |
|
84
|
|
|
|
|
85
|
|
|
public function getCMSFields() |
|
|
|
|
|
|
86
|
|
|
{ |
|
87
|
|
|
$fields = new FieldList(new TabSet('Root', $mainTab = new Tab('Main'))); |
|
88
|
|
|
$fields->addFieldsToTab('Root.Main', array( |
|
89
|
|
|
ReadonlyField::create('PreviewFieldType', 'Field type', $this->ClassName), |
|
90
|
|
|
ReadonlyField::create('Name', _t('AttendeeExtraField.Name', 'Name for this field')), |
|
91
|
|
|
TextField::create('Title', _t('AttendeeExtraField.Title', 'Field label or question')), |
|
92
|
|
|
TextField::create('ExtraClass', _t('AttendeeExtraField.ExtraClass', 'Add an extra class to the field')), |
|
93
|
|
|
TextField::create('Default', _t('AttendeeExtraField.Default', 'Set a default value')) |
|
94
|
|
|
)); |
|
95
|
|
|
|
|
96
|
|
|
$fields->addFieldsToTab('Root.Validation', array( |
|
97
|
|
|
CheckboxField::create('Required', _t( |
|
98
|
|
|
'AttendeeExtraField.Required', |
|
99
|
|
|
'This field is required' |
|
100
|
|
|
))->setDisabled(!(bool)$this->Editable) |
|
101
|
|
|
)); |
|
102
|
|
|
|
|
103
|
|
|
$this->extend('updateCMSFields', $fields); |
|
104
|
|
|
return $fields; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function onBeforeWrite() |
|
108
|
|
|
{ |
|
109
|
|
|
// Set the title to be the name when empty |
|
110
|
|
|
if (empty($this->Name)) { |
|
111
|
|
|
$str = preg_replace('/[^a-z0-9]+/i', ' ', $this->Title); |
|
112
|
|
|
$str = trim($str); |
|
113
|
|
|
$str = ucwords($str); |
|
114
|
|
|
$this->Name = str_replace(" ", "", $str); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
parent::onBeforeWrite(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get the filed type |
|
122
|
|
|
* |
|
123
|
|
|
* @return FormField |
|
124
|
|
|
*/ |
|
125
|
|
|
public function getFieldType() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->fieldType; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Show if the field is required in a nice format |
|
132
|
|
|
* |
|
133
|
|
|
* BUGFIX Using the shorthand Required.Nice in the summary_fields |
|
134
|
|
|
* made the fields, that were required (true), be set to (false) |
|
135
|
|
|
* |
|
136
|
|
|
* @return mixed |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getRequiredNice() |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->dbObject('Required')->Nice(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Get the value |
|
145
|
|
|
* |
|
146
|
|
|
* @return mixed|string |
|
147
|
|
|
*/ |
|
148
|
|
|
public function getValue() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->getField('Value'); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Create a field from given configuration |
|
155
|
|
|
* These fields are created based on the set default fields @see Attendee::$default_fields |
|
156
|
|
|
* |
|
157
|
|
|
* @param $fieldName |
|
158
|
|
|
* @param $fieldConfig |
|
159
|
|
|
* |
|
160
|
|
|
* @return UserField|DataObject |
|
161
|
|
|
*/ |
|
162
|
|
|
public static function createDefaultField($fieldName, $fieldConfig) |
|
163
|
|
|
{ |
|
164
|
|
|
/** @var UserField $fieldType */ |
|
165
|
|
|
$fieldType = $fieldConfig['FieldType']; |
|
166
|
|
|
|
|
167
|
|
|
$field = $fieldType::create(); |
|
168
|
|
|
$field->Name = $fieldName; |
|
169
|
|
|
if (is_array($fieldConfig)) { |
|
170
|
|
|
foreach ($fieldConfig as $property => $value) { |
|
171
|
|
|
$field->setField($property, $value); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return $field; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Create the actual field |
|
180
|
|
|
* Overwrite this on the field subclass |
|
181
|
|
|
* |
|
182
|
|
|
* @param $fieldName string Created by the AttendeeField |
|
183
|
|
|
* @param $defaultValue string Set a default value |
|
184
|
|
|
* |
|
185
|
|
|
* @return FormField |
|
186
|
|
|
*/ |
|
187
|
|
|
public function createField($fieldName, $defaultValue = null) |
|
188
|
|
|
{ |
|
189
|
|
|
$fieldType = $this->fieldType; |
|
190
|
|
|
$field = $fieldType::create($fieldName, $this->Title, $defaultValue); |
|
191
|
|
|
$field->addExtraClass($this->ExtraClass); |
|
192
|
|
|
$this->extend('updateCreateField', $field); |
|
193
|
|
|
return $field; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Returns the singular name without the namespaces |
|
198
|
|
|
* |
|
199
|
|
|
* @return string |
|
200
|
|
|
*/ |
|
201
|
|
|
public function singular_name() |
|
202
|
|
|
{ |
|
203
|
|
|
$name = explode('\\', parent::singular_name()); |
|
204
|
|
|
return trim(str_replace('User', '', end($name))); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
public function canView($member = null) |
|
|
|
|
|
|
208
|
|
|
{ |
|
209
|
|
|
return $this->Event()->canView($member); |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
public function canEdit($member = null) |
|
|
|
|
|
|
213
|
|
|
{ |
|
214
|
|
|
return $this->Event()->canEdit($member); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
public function canDelete($member = null) |
|
218
|
|
|
{ |
|
219
|
|
|
return $this->Editable; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
public function canCreate($member = null) |
|
|
|
|
|
|
223
|
|
|
{ |
|
224
|
|
|
return $this->Event()->canCreate($member); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|