|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SunnySideUp\BuildDataObject; |
|
4
|
|
|
|
|
5
|
|
|
class API extends \Object |
|
6
|
|
|
{ |
|
7
|
|
|
private static $excluded_data_objects = [ |
|
|
|
|
|
|
8
|
|
|
'Image_Cached', |
|
9
|
|
|
'PermissionRoleCode', |
|
10
|
|
|
'LoginAttempt', |
|
11
|
|
|
'MemberPassword', |
|
12
|
|
|
'MemberPassword', |
|
13
|
|
|
'SiteConfig' |
|
14
|
|
|
]; |
|
15
|
|
|
|
|
16
|
|
|
private static $excluded_db_fields_types = [ |
|
|
|
|
|
|
17
|
|
|
'DBField', |
|
18
|
|
|
'Field', |
|
19
|
|
|
'DBLocale', |
|
20
|
|
|
'Locale', |
|
21
|
|
|
'StringField', |
|
22
|
|
|
'CompositeField', |
|
23
|
|
|
'PrimaryKey', |
|
24
|
|
|
'ForeignKey' |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
private static $additional_db_fields = [ |
|
|
|
|
|
|
28
|
|
|
'ID', |
|
29
|
|
|
'Created', |
|
30
|
|
|
'LastEdited', |
|
31
|
|
|
'ClassName' |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
protected $rootBaseClass = 'DataObject'; |
|
35
|
|
|
|
|
36
|
|
|
protected $myBaseClass = ''; |
|
37
|
|
|
|
|
38
|
|
|
protected $_data = null; |
|
39
|
|
|
|
|
40
|
|
|
private static $_my_singleton = []; |
|
41
|
|
|
|
|
42
|
|
|
public static function inst($myBaseClass = 'DataObject', $data) |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
if (! isset(self::$_my_singleton[$myBaseClass])) { |
|
45
|
|
|
self::$_my_singleton[$myBaseClass] = \Injector::inst()->get('SunnySideUp\BuildDataObject\API'); |
|
46
|
|
|
} |
|
47
|
|
|
self::$_my_singleton[$myBaseClass]->_data = $data; |
|
48
|
|
|
self::$_my_singleton[$myBaseClass]->setBaseClass($myBaseClass); |
|
49
|
|
|
|
|
50
|
|
|
return self::$_my_singleton[$myBaseClass]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function setBaseClass($myBaseClass) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->myBaseClass = $myBaseClass; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
protected function retrieveDBFields($name) |
|
60
|
|
|
{ |
|
61
|
|
|
$data = $this->_data; |
|
62
|
|
|
$ar = []; |
|
63
|
|
|
if (isset($data->$name)) { |
|
64
|
|
|
foreach ($data->$name as $data) { |
|
65
|
|
|
if ($data->Key && $data->Value) { |
|
66
|
|
|
$ar[$data->Key] = $data->Key; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $ar; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected $_dbfieldCache = []; |
|
75
|
|
|
|
|
76
|
|
|
public function DbFields() |
|
77
|
|
|
{ |
|
78
|
|
|
if (count($this->_dbfieldCache) === 0) { |
|
79
|
|
|
$list = \ClassInfo::subclassesFor('DbField'); |
|
80
|
|
|
$newList = []; |
|
81
|
|
|
foreach ($list as $class) { |
|
|
|
|
|
|
82
|
|
View Code Duplication |
if (substr($class, 0, 2) == 'DB') { |
|
|
|
|
|
|
83
|
|
|
$class = substr($class, 2, strlen($class)); |
|
84
|
|
|
} |
|
85
|
|
View Code Duplication |
if (substr($class, 0, 3) == 'SS_') { |
|
|
|
|
|
|
86
|
|
|
$class = substr($class, 3, strlen($class)); |
|
87
|
|
|
} |
|
88
|
|
|
if ('Varchar' === $class) { |
|
89
|
|
|
$class = 'Varchar(n)'; |
|
90
|
|
|
} |
|
91
|
|
|
if ('HTMLVarchar' === $class) { |
|
92
|
|
|
$class = 'HTMLVarchar(n)'; |
|
93
|
|
|
} |
|
94
|
|
|
if ('Enum' === $class) { |
|
95
|
|
|
$class = 'Enum(\\\'Foo,Bar\\\', \\\'FOO\\\')'; |
|
96
|
|
|
} |
|
97
|
|
|
if ('MultiEnum' === $class) { |
|
98
|
|
|
$class = 'MultiEnum(\\\'Foo,Bar\\\', \\\'FOO\\\')'; |
|
99
|
|
|
} |
|
100
|
|
|
if ( |
|
|
|
|
|
|
101
|
|
|
$class == 'DbField' || |
|
102
|
|
|
is_subclass_of($class, 'TestOnly') || |
|
103
|
|
|
in_array($class, $this->Config()->get('excluded_db_fields_types')) |
|
104
|
|
|
) { |
|
105
|
|
|
//do nothing |
|
106
|
|
|
} else { |
|
107
|
|
|
$newList[$class] = $class; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
ksort($newList); |
|
111
|
|
|
$this->_dbfieldCache = $newList; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $this->_dbfieldCache; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function MyDbFields() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->retrieveDBFields('db'); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function MyDbFieldsWithDefaults() |
|
123
|
|
|
{ |
|
124
|
|
|
$list = $this->retrieveDBFields('db'); |
|
125
|
|
|
$toAdd = $this->Config()->get('additional_db_fields'); |
|
126
|
|
|
$toAdd = array_combine($toAdd, $toAdd); |
|
127
|
|
|
|
|
128
|
|
|
return $toAdd + $list; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function mydbfieldsfancywithbelongswithbasicfields() |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->MyDbFieldsFancyWithBelongs(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function MyDbFieldsFancyWithBelongs() |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->myDbFieldsFancyWithoutBelongs(true); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function MyDbFieldsFancyWithoutBelongs($includeBelongs = false) |
|
142
|
|
|
{ |
|
143
|
|
|
$ar = []; |
|
144
|
|
|
$list = $this->retrieveDBFields('db'); |
|
145
|
|
|
foreach ($list as $key => $value) { |
|
146
|
|
|
$ar[$key] = $key; |
|
147
|
|
|
$shortValue = explode('(', $value); |
|
148
|
|
|
$shortValue = $shortValue[0]; |
|
149
|
|
|
switch ($shortValue) { |
|
150
|
|
|
case 'Varchar': |
|
151
|
|
|
case 'HTMLTextField': |
|
152
|
|
|
case 'HTMLVarchar': |
|
153
|
|
|
case 'Text': |
|
154
|
|
|
$ar[$key.'.LimitCharacters'] = $key.'.LimitCharacters'; |
|
155
|
|
|
break; |
|
156
|
|
|
default: |
|
157
|
|
|
$ar[$key.'.Nice'] = $key.'.Nice'; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
$list = []; |
|
161
|
|
|
if ($includeBelongs) { |
|
162
|
|
|
$list += $this->retrieveDBFields('belongs_to'); |
|
163
|
|
|
} |
|
164
|
|
|
$list += $this->retrieveDBFields('has_one'); |
|
165
|
|
|
foreach ($list as $key => $value) { |
|
166
|
|
|
if ($value === 'Image' || is_subclass_of($value, 'Image')) { |
|
167
|
|
|
$ar[$key.'.Thumbnail'] = $key.'.Thumbnail'; |
|
168
|
|
|
} else { |
|
169
|
|
|
$ar[$key.'.Title'] = $key.'.Title'; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
$list = |
|
173
|
|
|
$this->retrieveDBFields('has_many') + |
|
174
|
|
|
$this->retrieveDBFields('many_many'); |
|
175
|
|
|
if ($includeBelongs) { |
|
176
|
|
|
$list += $this->retrieveDBFields('belongs_many_many'); |
|
177
|
|
|
} |
|
178
|
|
|
foreach ($list as $key => $value) { |
|
179
|
|
|
$ar[$key.'.Count'] = $key.'.Count'; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $ar; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function MyDbFieldsAndHasOnes() |
|
186
|
|
|
{ |
|
187
|
|
|
return |
|
188
|
|
|
$this->retrieveDBFields('db') + |
|
189
|
|
|
$this->retrieveDBFields('has_one'); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function MyDbFieldsAndHasOnesWithIDs() |
|
193
|
|
|
{ |
|
194
|
|
|
$list = $this->retrieveDBFields('db'); |
|
195
|
|
|
$hasOnes = $this->retrieveDBFields('has_one'); |
|
196
|
|
|
foreach ($hasOnes as $field => $type) { |
|
197
|
|
|
$fieldWithID = $field . 'ID'; |
|
198
|
|
|
$list[$fieldWithID] = $fieldWithID; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
return $list; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public function MyDbFieldsAndIndexes() |
|
205
|
|
|
{ |
|
206
|
|
|
return |
|
207
|
|
|
$this->MyDbFieldsWithDefaults() + |
|
208
|
|
|
['index1' => 'index1'] + |
|
209
|
|
|
['index2' => 'index2'] + |
|
210
|
|
|
['index3' => 'index3'] + |
|
211
|
|
|
['index4' => 'index4'] + |
|
212
|
|
|
['index5' => 'index5']; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
public function MyAllFieldsWithBelongs() |
|
216
|
|
|
{ |
|
217
|
|
|
return $this->myAllFieldsWithoutBelongs(true); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
public function MyAllFieldsWithoutBelongs($includeBelongs = false) |
|
221
|
|
|
{ |
|
222
|
|
|
$list = $this->MyDbFieldsWithDefaults(); |
|
223
|
|
|
if ($includeBelongs) { |
|
224
|
|
|
$list += $this->retrieveDBFields('belongs_to'); |
|
225
|
|
|
} |
|
226
|
|
|
$list += |
|
227
|
|
|
$this->retrieveDBFields('has_one') + |
|
228
|
|
|
$this->retrieveDBFields('has_many') + |
|
229
|
|
|
$this->retrieveDBFields('many_many'); |
|
230
|
|
|
if ($includeBelongs) { |
|
231
|
|
|
$list += $this->retrieveDBFields('belongs_many_many'); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
return $list; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
|
|
238
|
|
|
public function IndexOptions() |
|
239
|
|
|
{ |
|
240
|
|
|
return [ |
|
241
|
|
|
'true' => 'true', |
|
242
|
|
|
'unique("<column-name>")' => 'unique', |
|
243
|
|
|
'[\'type\' => \'<type>\', \'value\' => \'"<column-name>"\']' => 'other' |
|
244
|
|
|
]; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
public function RequiredOptions() |
|
248
|
|
|
{ |
|
249
|
|
|
return [ |
|
250
|
|
|
'true' => 'true', |
|
251
|
|
|
'unique' => 'unique' |
|
252
|
|
|
]; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
|
|
256
|
|
|
public function PossibleRelationsWithBaseClass($rootClass = '') |
|
257
|
|
|
{ |
|
258
|
|
|
if($rootClass) { |
|
|
|
|
|
|
259
|
|
|
// |
|
260
|
|
|
} else { |
|
261
|
|
|
$rootClass = $this->rootBaseClass; |
|
262
|
|
|
} |
|
263
|
|
|
return |
|
264
|
|
|
[$rootClass => $rootClass] + |
|
265
|
|
|
$this->possibleRelations(); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
protected $_classesCache = []; |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* |
|
272
|
|
|
* @return array |
|
273
|
|
|
*/ |
|
274
|
|
|
public function PossibleRelations($rootClass = '') |
|
275
|
|
|
{ |
|
276
|
|
|
if($rootClass) { |
|
|
|
|
|
|
277
|
|
|
// |
|
278
|
|
|
} else { |
|
279
|
|
|
$rootClass = $this->rootBaseClass; |
|
280
|
|
|
} |
|
281
|
|
|
if (!isset($this->_classesCache[$rootClass])) { |
|
282
|
|
|
$list = \ClassInfo::subclassesFor($rootClass); |
|
283
|
|
|
$newList = []; |
|
284
|
|
View Code Duplication |
foreach ($list as $class) { |
|
|
|
|
|
|
285
|
|
|
if ( |
|
|
|
|
|
|
286
|
|
|
$class == $rootClass || |
|
287
|
|
|
is_subclass_of($class, 'TestOnly') || |
|
288
|
|
|
in_array($class, $this->Config()->get('excluded_data_objects')) |
|
289
|
|
|
) { |
|
290
|
|
|
//do nothing |
|
291
|
|
|
} else { |
|
292
|
|
|
$newList[$class] = $class; |
|
293
|
|
|
$name = \Injector::inst()->get($class)->singular_name(); |
|
294
|
|
|
if ($name !== $class) { |
|
295
|
|
|
$newList[$class] .= ' ('.$name.')'; |
|
296
|
|
|
} |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
$this->_classesCache[$rootClass] = $newList; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
return $this->_classesCache[$rootClass]; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
protected $_filtersCache = []; |
|
306
|
|
|
|
|
307
|
|
|
public function PossibleSearchFilters() |
|
308
|
|
|
{ |
|
309
|
|
|
if (count($this->_filtersCache) === 0) { |
|
310
|
|
|
$list = \ClassInfo::subclassesFor('SearchFilter'); |
|
311
|
|
|
$newList = []; |
|
312
|
|
|
foreach ($list as $class) { |
|
|
|
|
|
|
313
|
|
|
if ($class !== 'SearchFilter') { |
|
314
|
|
|
$newList[$class] = $class; |
|
315
|
|
|
} |
|
316
|
|
|
} |
|
317
|
|
|
$this->_filtersCache = $newList; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
return $this->_filtersCache; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
protected $_modelAdmins = []; |
|
324
|
|
|
|
|
325
|
|
|
public function ModelAdminOptions() |
|
326
|
|
|
{ |
|
327
|
|
|
if (count($this->_modelAdmins) === 0) { |
|
328
|
|
|
$list = \ClassInfo::subclassesFor('ModelAdmin'); |
|
329
|
|
|
$newList = []; |
|
330
|
|
|
foreach ($list as $class) { |
|
|
|
|
|
|
331
|
|
|
if ( |
|
|
|
|
|
|
332
|
|
|
$class == 'ModelAdmin' || |
|
333
|
|
|
is_subclass_of($class, 'TestOnly') |
|
334
|
|
|
) { |
|
335
|
|
|
//do nothing |
|
336
|
|
|
} else { |
|
337
|
|
|
$newList[$class] = $class; |
|
338
|
|
|
} |
|
339
|
|
|
} |
|
340
|
|
|
$newList['tba'] = 'tba'; |
|
341
|
|
|
$this->_modelAdmins = $newList; |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
return $this->_modelAdmins; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
|
|
348
|
|
|
public function SortOptions() |
|
349
|
|
|
{ |
|
350
|
|
|
return [ |
|
351
|
|
|
'ASC' => 'ASC', |
|
352
|
|
|
'DESC' => 'DESC' |
|
353
|
|
|
]; |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
protected $_canOptions = null; |
|
357
|
|
|
|
|
358
|
|
|
public function CanOptions() |
|
|
|
|
|
|
359
|
|
|
{ |
|
360
|
|
|
if (! $this->_canOptions) { |
|
361
|
|
|
$ar = [ |
|
362
|
|
|
'true' => 'always', |
|
363
|
|
|
'false' => 'never', |
|
364
|
|
|
'parent' => 'use parent class', |
|
365
|
|
|
]; |
|
366
|
|
|
$permissions = \Permission::get()->column('Code'); |
|
367
|
|
|
$ar = $ar + array_combine($permissions, $permissions); |
|
368
|
|
|
|
|
369
|
|
|
$this->_canOptions = $ar; |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
return $this->_canOptions; |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
|
|
376
|
|
|
|
|
377
|
|
|
/** |
|
378
|
|
|
* |
|
379
|
|
|
* @return array |
|
380
|
|
|
*/ |
|
381
|
|
|
public function SiteTreeList($rootClass = 'SiteTree') |
|
382
|
|
|
{ |
|
383
|
|
|
$list = \ClassInfo::subclassesFor($rootClass); |
|
384
|
|
View Code Duplication |
foreach ($list as $class) { |
|
|
|
|
|
|
385
|
|
|
if ( |
|
|
|
|
|
|
386
|
|
|
$class == $rootClass || |
|
387
|
|
|
is_subclass_of($class, 'TestOnly') || |
|
388
|
|
|
in_array($class, $this->Config()->get('excluded_data_objects')) |
|
389
|
|
|
) { |
|
390
|
|
|
//do nothing |
|
391
|
|
|
} else { |
|
392
|
|
|
$newList[$class] = $class; |
|
|
|
|
|
|
393
|
|
|
$name = \Injector::inst()->get($class)->singular_name(); |
|
394
|
|
|
if ($name !== $class) { |
|
395
|
|
|
$newList[$class] .= ' ('.$name.')'; |
|
396
|
|
|
} |
|
397
|
|
|
} |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
return $newList; |
|
|
|
|
|
|
401
|
|
|
} |
|
402
|
|
|
|
|
403
|
|
|
|
|
404
|
|
|
/** |
|
405
|
|
|
* |
|
406
|
|
|
* @return array |
|
407
|
|
|
*/ |
|
408
|
|
|
public function AllowedChildrenOptions($rootClass = 'SiteTree') |
|
409
|
|
|
{ |
|
410
|
|
|
return ['none' => 'none'] + $this->SiteTreeList($rootClass); |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
|
|
/** |
|
414
|
|
|
* |
|
415
|
|
|
* @return array |
|
416
|
|
|
*/ |
|
417
|
|
|
public function TrueOrFalseListWithIgnore() |
|
418
|
|
|
{ |
|
419
|
|
|
return [ |
|
420
|
|
|
'' => '-- ignore --' |
|
421
|
|
|
] + |
|
422
|
|
|
$this->TrueOrFalseList(); |
|
423
|
|
|
} |
|
424
|
|
|
|
|
425
|
|
|
/** |
|
426
|
|
|
* |
|
427
|
|
|
* @return array |
|
428
|
|
|
*/ |
|
429
|
|
|
public function TrueOrFalseList() |
|
430
|
|
|
{ |
|
431
|
|
|
return [ |
|
432
|
|
|
'true' => 'YES', |
|
433
|
|
|
'false' => 'NO' |
|
434
|
|
|
]; |
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
|
|
} |
|
438
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.