1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace SunnySideUp\BuildDataObject; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
|
8
|
|
|
abstract class BuildController extends \Controller { |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
private static $form_data_session_variable = 'SunnySideUp\BuildDataObject\DataObjectBuildController'; |
|
|
|
|
12
|
|
|
|
13
|
|
|
private static $url_segment = 'build'; |
|
|
|
|
14
|
|
|
|
15
|
|
|
private static $allowed_actions = [ |
|
|
|
|
16
|
|
|
'primaryformstart' => true, |
17
|
|
|
'PrimaryForm' => true, |
18
|
|
|
'doprimaryform' => true, |
19
|
|
|
'secondaryformstart' => true, |
20
|
|
|
'SecondaryForm' => true, |
21
|
|
|
'dosecondaryform' => true, |
22
|
|
|
'results' => true, |
23
|
|
|
'startover' => true, |
24
|
|
|
'debug' => true |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
protected $myBaseClass = 'DataObject'; |
28
|
|
|
|
29
|
|
|
protected $apiProvider = 'SunnySideUp\BuildDataObject\API'; |
30
|
|
|
|
31
|
|
|
abstract protected function primaryThingsToBuild(); |
|
|
|
|
32
|
|
|
|
33
|
|
|
abstract protected function secondaryThingsToBuild(); |
|
|
|
|
34
|
|
|
|
35
|
|
|
public function Link($action = null) |
36
|
|
|
{ |
37
|
|
|
if($action) { |
38
|
|
|
$action .= '/'; |
39
|
|
|
} |
40
|
|
|
return |
41
|
|
|
'/'.$this->Config()->get('url_segment'). |
42
|
|
|
'/'.strtolower($this->myBaseClass). |
43
|
|
|
'/'.$action; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function Title() |
47
|
|
|
{ |
48
|
|
|
return 'Build a '.$this->myBaseClass.' - Step '.$this->step.' of 2'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
public function jQueryLink() |
53
|
|
|
{ |
54
|
|
|
return \Director::absoluteURL('/framework/thirdparty/jquery/jquery.js'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function startover() |
58
|
|
|
{ |
59
|
|
|
$this->saveData('_PrimaryForm', null); |
60
|
|
|
$this->saveData('_SecondaryForm', null); |
61
|
|
|
return $this->redirect($this->link('primaryformstart')); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* |
66
|
|
|
* @var Form |
67
|
|
|
*/ |
68
|
|
|
protected $step = 1; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* |
72
|
|
|
* @var Form |
73
|
|
|
*/ |
74
|
|
|
protected $form = null; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* |
78
|
|
|
* @var Form |
79
|
|
|
*/ |
80
|
|
|
protected $prevLink = null; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* |
84
|
|
|
* @var ArrayList |
85
|
|
|
*/ |
86
|
|
|
protected $finalData = null; |
87
|
|
|
|
88
|
|
|
function index() |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
return $this->redirect($this->Link('primaryformstart')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
function primaryformstart() |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$this->PrimaryForm(); |
96
|
|
|
$this->prevLink = $this->Link('startover'); |
|
|
|
|
97
|
|
|
|
98
|
|
|
return $this->renderWith('BuildControllerForm'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
function PrimaryForm() |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$this->form = $this->createForm('PrimaryForm', 'Build Model'); |
|
|
|
|
104
|
|
|
|
105
|
|
|
return $this->form; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
function doprimaryform($data, $form) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
$this->saveData('_PrimaryForm', $data); |
111
|
|
|
|
112
|
|
|
return $this->redirect($this->Link('secondaryformstart')); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
function secondaryformstart() |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$this->step = 2; |
|
|
|
|
119
|
|
|
$this->SecondaryForm(); |
120
|
|
|
$this->prevLink = $this->Link('primaryformstart'); |
|
|
|
|
121
|
|
|
|
122
|
|
|
return $this->renderWith('BuildControllerForm'); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
function SecondaryForm() |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$this->form = $this->createForm('SecondaryForm', 'Download Example Class'); |
|
|
|
|
128
|
|
|
|
129
|
|
|
return $this->form; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
function dosecondaryform($data, $form) |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
$this->saveData('_SecondaryForm', $data); |
136
|
|
|
|
137
|
|
|
return $this->redirect($this->Link('results')); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
function results() |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
$this->finalData = $this->processedFormData($this->retrieveData()); |
143
|
|
|
return \SS_HTTPRequest::send_file( |
144
|
|
|
$this->renderWith($this->resultsTemplateForBuilder()), |
145
|
|
|
$this->finalData->Name.'.php' |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
function debug() |
|
|
|
|
150
|
|
|
{ |
151
|
|
|
$this->finalData = $this->processedFormData($this->retrieveData()); |
152
|
|
|
print_r($this->CanMethodBuilder('canEdit')); |
|
|
|
|
153
|
|
|
print_r($this->finalData); |
154
|
|
|
die('-----------------------------'); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
function Form() |
|
|
|
|
158
|
|
|
{ |
159
|
|
|
return $this->form; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
function FinalData() |
|
|
|
|
163
|
|
|
{ |
164
|
|
|
return $this->finalData; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
function PrevLink() |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
return $this->prevLink; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
function MyCanMethodBuilder($type, $value) { |
|
|
|
|
173
|
|
|
if($value === 'parent') { |
174
|
|
|
return null; |
175
|
|
|
} elseif($value === 'true') { |
176
|
|
|
$str = 'true;'; |
177
|
|
|
} elseif($value === 'false') { |
178
|
|
|
$str = 'false;'; |
179
|
|
|
} else { |
180
|
|
|
$str = 'Permission::check(\''.$value.'\', \'any\', $member);'; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return \DBField::create_field('Varchar', $str); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
protected function createForm($formName, $actionTitle) |
188
|
|
|
{ |
189
|
|
|
if($formName === 'PrimaryForm') { |
190
|
|
|
$isPrimary = true; |
191
|
|
|
$isSecond = false; |
|
|
|
|
192
|
|
|
} elseif($formName === 'SecondaryForm') { |
193
|
|
|
$isPrimary = false; |
194
|
|
|
$isSecond = true; |
|
|
|
|
195
|
|
|
} else { |
196
|
|
|
user_error('Set right form type: '.$formName.' is not valid'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$finalFields = \FieldList::create(); |
200
|
|
|
|
201
|
|
|
if($isPrimary) { |
|
|
|
|
202
|
|
|
$toBuild = $this->primaryThingsToBuild(); |
203
|
|
|
$finalFields->push(\HeaderField::create('Name your '.$this->myBaseClass)); |
204
|
|
|
$finalFields->push(\TextField::create('Name', '')); |
205
|
|
|
$finalFields->push(\HeaderField::create('Extends')); |
206
|
|
|
$finalFields->push(\DropdownField::create( |
207
|
|
|
'Extends', |
208
|
|
|
'', |
209
|
|
|
$this->myAPI()->PossibleRelationsWithBaseClass($this->myBaseClass) |
210
|
|
|
|
211
|
|
|
)); |
212
|
|
|
$additionalFields = $this->additionalPrimaryFields(); |
213
|
|
|
foreach($additionalFields as $additionalField) { |
214
|
|
|
$finalFields->push($additionalField); |
215
|
|
|
} |
216
|
|
|
} else { |
217
|
|
|
$toBuild = $this->secondaryThingsToBuild(); |
218
|
|
|
} |
219
|
|
|
$formFields = []; |
220
|
|
|
|
221
|
|
|
$count = 0; |
222
|
|
|
//build fields ... |
223
|
|
|
foreach($toBuild as $item) { |
224
|
|
|
$name = $item[0]; |
225
|
|
|
$sourceMethod1 = $item[1]; |
226
|
|
|
$sourceMethod2 = $item[2]; |
227
|
|
|
$isMultiple = $item[3]; |
228
|
|
|
|
229
|
|
|
//work out style |
230
|
|
|
$hasKeyAndValue = false; |
231
|
|
|
if($sourceMethod1 && $sourceMethod2) { |
232
|
|
|
$hasKeyAndValue = true; |
233
|
|
|
} |
234
|
|
|
$formFields[$count] = []; |
235
|
|
|
$formFields[$count][0] = [ |
236
|
|
|
$name.'_HEADER', |
237
|
|
|
'HeaderField', |
238
|
|
|
$name |
239
|
|
|
]; |
240
|
|
|
if($isMultiple) { |
241
|
|
|
$max = 12; |
242
|
|
|
} else { |
243
|
|
|
$max = 1; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
//work out sources |
247
|
|
|
if($sourceMethod1 && $this->myAPI()->hasMethod($sourceMethod1)) { |
248
|
|
|
$source1 = $this->myAPI()->$sourceMethod1(); |
249
|
|
|
} else { |
250
|
|
|
$source1 = null; |
251
|
|
|
} |
252
|
|
|
if($sourceMethod2 && $this->myAPI()->hasMethod($sourceMethod2)) { |
253
|
|
|
$source2 = $this->myAPI()->$sourceMethod2(); |
254
|
|
|
} elseif($sourceMethod2) { |
255
|
|
|
$source2 = null; |
256
|
|
|
} else { |
257
|
|
|
$source2 = 'ignore'; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
//work out field names |
261
|
|
|
|
262
|
|
|
for($i = 1; $i <= $max; $i++) { |
263
|
|
|
if($hasKeyAndValue) { |
264
|
|
|
$nameKey = $name.'__KEY__'.$i; |
265
|
|
|
$nameValue = $name.'__VALUE__'.$i; |
266
|
|
|
} else { |
267
|
|
|
$nameKey = $name; |
268
|
|
|
$nameValue = ''; |
269
|
|
|
} |
270
|
|
|
if($hasKeyAndValue) { |
271
|
|
|
//key field |
272
|
|
View Code Duplication |
if($source1) { |
|
|
|
|
273
|
|
|
$formFields[$count][$i]['KEY'] = [ |
274
|
|
|
$nameKey, |
275
|
|
|
'DropdownField', |
276
|
|
|
$source1 |
277
|
|
|
]; |
278
|
|
|
} else { |
279
|
|
|
$formFields[$count][$i]['KEY'] = [ |
280
|
|
|
$nameKey, |
281
|
|
|
'TextField' |
282
|
|
|
]; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
//value field |
286
|
|
View Code Duplication |
if($source2) { |
|
|
|
|
287
|
|
|
$formFields[$count][$i]['VALUE'] = [ |
288
|
|
|
$nameValue, |
289
|
|
|
'DropdownField', |
290
|
|
|
$source2 |
291
|
|
|
]; |
292
|
|
|
} else { |
293
|
|
|
$formFields[$count][$i]['VALUE'] = [ |
294
|
|
|
$nameValue, |
295
|
|
|
'TextField' |
296
|
|
|
]; |
297
|
|
|
} |
298
|
|
|
} else { |
299
|
|
|
//keys only! |
300
|
|
|
if($source1) { |
301
|
|
|
$formFields[$count][1] = [ |
302
|
|
|
$nameKey, |
303
|
|
|
'DropdownField', |
304
|
|
|
$source1 |
305
|
|
|
]; |
306
|
|
|
} else { |
307
|
|
|
$formFields[$count][1] = [ |
308
|
|
|
$nameKey, |
309
|
|
|
'TextField' |
310
|
|
|
]; |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
if($i > 2) { |
315
|
|
|
$formFields[$count][$i + 1] = [ |
316
|
|
|
$name.'_ADD_'.$i, |
317
|
|
|
'LiteralField', |
318
|
|
|
' |
319
|
|
|
<div class="CompositeField add-and-remove"> |
320
|
|
|
<a href="#" class="add first-add"><i class="material-icons">add_circle_outline</i></a> |
321
|
|
|
<a href="#" class="remove"><i class="material-icons">remove_circle_outline</i></a> |
322
|
|
|
</div> |
323
|
|
|
' |
324
|
|
|
]; |
325
|
|
|
} |
326
|
|
|
$count++; |
327
|
|
|
} |
328
|
|
|
//create fields ... |
329
|
|
|
$count = 0; |
330
|
|
|
foreach($formFields as $outerCount => $subFieldList) { |
331
|
|
|
$count++; |
332
|
|
|
$compositeField = \CompositeField::create(); |
333
|
|
|
$compositeField->addExtraClass('OuterComposite pos'.$count); |
334
|
|
|
$innerCount = 0; |
335
|
|
|
foreach($subFieldList as $fieldDetails) { |
336
|
|
|
$innerCount++; |
337
|
|
|
if(isset($fieldDetails['KEY']) && isset($fieldDetails['VALUE'])) { |
338
|
|
|
$subCompositeField = \CompositeField::create(); |
339
|
|
|
$subCompositeField->addExtraClass('InnerComposite pos'.$innerCount); |
340
|
|
|
$innerInnerCount = 0; |
|
|
|
|
341
|
|
|
foreach($fieldDetails as $fieldDetailsInner) { |
342
|
|
|
$fieldName = $fieldDetailsInner[0]; |
343
|
|
|
$fieldType = $fieldDetailsInner[1]; |
344
|
|
|
$additionalClasses = []; |
345
|
|
|
if(strpos($fieldName, '__KEY__')) { |
346
|
|
|
$additionalClasses[] = 'mykey'; |
347
|
|
|
} |
348
|
|
|
if(strpos($fieldName, '__VALUE__')) { |
349
|
|
|
$additionalClasses[] = 'myvalue'; |
350
|
|
|
} |
351
|
|
|
if(isset($fieldDetailsInner[2])) { |
352
|
|
|
$source = $fieldDetailsInner[2]; |
353
|
|
|
$source = $this->prependNullOption( $source ); |
354
|
|
|
$tempField = $fieldType::create($fieldName, '', $source); |
355
|
|
|
} else { |
356
|
|
|
$tempField = $fieldType::create($fieldName, ''); |
357
|
|
|
} |
358
|
|
|
if(count($additionalClasses)) { |
359
|
|
|
$classes = implode(' ', $additionalClasses); |
360
|
|
|
$tempField->addExtraClass($classes); |
361
|
|
|
} |
362
|
|
|
$subCompositeField->push($tempField); |
363
|
|
|
} |
364
|
|
|
$compositeField->push($subCompositeField); |
365
|
|
|
} else { |
366
|
|
|
$fieldName = $fieldDetails[0]; |
367
|
|
|
$fieldType = $fieldDetails[1]; |
368
|
|
|
if($fieldType === 'DropdownField') { |
369
|
|
|
$source = $fieldDetails[2]; |
370
|
|
|
$source = $this->prependNullOption($source); |
371
|
|
|
$compositeField->push($fieldType::create($fieldName, '', $source)); |
372
|
|
|
} elseif($fieldType === 'HeaderField') { |
373
|
|
|
$title = str_replace('_', ' ', $fieldDetails[2]); |
374
|
|
|
$compositeField->push($fieldType::create($fieldName, $title)); |
375
|
|
|
} elseif($fieldType === 'LiteralField') { |
376
|
|
|
$title = $fieldDetails[2]; |
377
|
|
|
$compositeField->push($fieldType::create($fieldName, $title)); |
378
|
|
|
} else { |
379
|
|
|
$compositeField->push($fieldType::create($fieldName, '')); |
380
|
|
|
} |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
$finalFields->push($compositeField); |
384
|
|
|
} |
385
|
|
|
$actions = \FieldList::create( |
386
|
|
|
[\FormAction::create('do'.strtolower($formName), $actionTitle)] |
387
|
|
|
); |
388
|
|
|
|
389
|
|
|
$form = \Form::create($this, $formName, $finalFields, $actions); |
390
|
|
|
$form->setFormAction($this->Link($formName)); |
391
|
|
|
$form->loadDataFrom($this->retrieveData()); |
392
|
|
|
|
393
|
|
|
return $form; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* returns an array of fields |
398
|
|
|
* @return array |
399
|
|
|
*/ |
400
|
|
|
protected function additionalPrimaryFields() |
401
|
|
|
{ |
402
|
|
|
return []; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
protected function saveData($name, $data) |
406
|
|
|
{ |
407
|
|
|
$var = $this->Config()->get('form_data_session_variable'); |
408
|
|
|
\Session::set($var.$name, $data); |
409
|
|
|
\Session::save(); |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
private $_data = null; |
413
|
|
|
|
414
|
|
|
protected function retrieveData() |
|
|
|
|
415
|
|
|
{ |
416
|
|
|
if(! $this->_data) { |
417
|
|
|
$var = $this->Config()->get('form_data_session_variable'); |
418
|
|
|
$retrieveDataPrimary = \Session::get($var.'_PrimaryForm'); |
419
|
|
|
if ($retrieveDataPrimary && (is_array($retrieveDataPrimary) || is_object($retrieveDataPrimary))) { |
|
|
|
|
420
|
|
|
//do nothing |
421
|
|
|
} else { |
422
|
|
|
$retrieveDataPrimary = []; |
423
|
|
|
} |
424
|
|
|
$retrieveDataSecondary = \Session::get($var.'_SecondaryForm'); |
425
|
|
|
if ($retrieveDataSecondary && (is_array($retrieveDataSecondary) || is_object($retrieveDataSecondary))) { |
|
|
|
|
426
|
|
|
//do nothing |
427
|
|
|
} else { |
428
|
|
|
$retrieveDataSecondary = []; |
429
|
|
|
} |
430
|
|
|
$this->_data = $retrieveDataPrimary + $retrieveDataSecondary; |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
return $this->_data; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
private $_processed_data = null; |
437
|
|
|
|
438
|
|
|
protected function processedFormData($data = null) { |
|
|
|
|
439
|
|
|
if(! $this->_processed_data) { |
440
|
|
|
if(! $data) { |
441
|
|
|
$data = $this->retrieveData(); |
442
|
|
|
} |
443
|
|
|
$array = []; |
444
|
|
|
foreach($data as $key => $value) { |
445
|
|
|
if($key && $value) { |
446
|
|
|
if( |
447
|
|
|
strpos($key, '__KEY__') || |
448
|
|
|
strpos($key, '__VALUE__') |
449
|
|
|
) { |
450
|
|
|
$parts = explode('__', $key); |
451
|
|
|
if(!isset($array[$parts[0]])) { |
452
|
|
|
$array[$parts[0]] = []; |
453
|
|
|
} |
454
|
|
|
if(! isset($array[$parts[0]][$parts[2]])) { |
455
|
|
|
$array[$parts[0]][$parts[2]] = []; |
456
|
|
|
} |
457
|
|
|
$array[$parts[0]][$parts[2]][$parts[1]] = $value; |
458
|
|
|
} elseif(substr($key, 0, 3) === 'can') { |
459
|
|
|
$array[$key] = $this->MyCanMethodBuilder($key, $value); |
460
|
|
|
} else { |
461
|
|
|
$array[$key] = $value; |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
} |
465
|
|
|
foreach($array as $field => $values) { |
466
|
|
|
$alInner = \ArrayList::create(); |
467
|
|
|
if(is_array($values)) { |
468
|
|
|
foreach($values as $key => $valuePairs) { |
469
|
|
|
if(isset($valuePairs['KEY']) && isset($valuePairs['VALUE'])) { |
470
|
|
|
if($valuePairs['VALUE'] == 'true') { |
471
|
|
|
$valuePairArray = [ |
472
|
|
|
'Key' => $valuePairs['KEY'], |
473
|
|
|
'UnquotedValue' => $valuePairs['VALUE'], |
474
|
|
|
]; |
475
|
|
|
} else { |
476
|
|
|
$valuePairArray = [ |
477
|
|
|
'Key' => $valuePairs['KEY'], |
478
|
|
|
'Value' => $valuePairs['VALUE'], |
479
|
|
|
]; |
480
|
|
|
} |
481
|
|
|
$alInner->push(\ArrayData::create($valuePairArray)); |
482
|
|
|
} |
483
|
|
|
} |
484
|
|
|
$array[$field] = $alInner; |
485
|
|
|
} else { |
|
|
|
|
486
|
|
|
//do nothing |
487
|
|
|
} |
488
|
|
|
} |
489
|
|
|
$this->_processed_data = \ArrayData::create($array); |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
return $this->_processed_data; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
|
496
|
|
|
protected function resultsTemplateForBuilder() |
497
|
|
|
{ |
498
|
|
|
return str_replace(__NAMESPACE__ .'\\', '', $this->class).'Results'; |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
|
502
|
|
|
protected function prependNullOption($source) |
503
|
|
|
{ |
504
|
|
|
$source = ['' => '--- Please Select ---'] + $source; |
505
|
|
|
|
506
|
|
|
return $source; |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
|
510
|
|
|
protected function myAPI() |
|
|
|
|
511
|
|
|
{ |
512
|
|
|
$class = $this->apiProvider; |
513
|
|
|
|
514
|
|
|
return $class::inst($this->myBaseClass, $this->processedFormData()); |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
|
518
|
|
|
} |
519
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.