-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Form.php
1124 lines (937 loc) · 29.7 KB
/
Form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Admin\Widgets;
use Admin\Classes\BaseWidget;
use Admin\Classes\FormField;
use Admin\Classes\FormTabs;
use Admin\Classes\Widgets;
use Admin\Facades\AdminAuth;
use Admin\Traits\FormModelWidget;
use Admin\Traits\LocationAwareWidget;
use Exception;
use Model;
class Form extends BaseWidget
{
use FormModelWidget;
use LocationAwareWidget;
//
// Configurable properties
//
/**
* @var array Form field configuration.
*/
public $fields;
/**
* @var array Primary tab configuration.
*/
public $tabs;
/**
* @var array Secondary tab configuration.
*/
public $secondaryTabs;
/**
* @var string The active tab name of this form.
*/
public $activeTab;
/**
* @var Model Form model object.
*/
public $model;
/**
* @var array Dataset containing field values, if none supplied, model is used.
*/
public $data;
/**
* @var string The context of this form, fields that do not belong
* to this context will not be shown.
*/
public $context;
/**
* @var string If the field element names should be contained in an array.
* Eg: <input name="nameArray[fieldName]" />
*/
public $arrayName;
//
// Object properties
//
protected $defaultAlias = 'form';
/**
* @var bool Determines if field definitions have been created.
*/
protected $fieldsDefined = FALSE;
/**
* @var array Collection of all fields used in this form.
* @see \Admin\Classes\FormField
*/
protected $allFields = [];
/**
* @var object Collection of tab sections used in this form.
* @see \Admin\Classes\FormTabs
*/
protected $allTabs = [
'outside' => null,
'primary' => null,
'secondary' => null,
];
/**
* @var array Collection of all form widgets used in this form.
*/
protected $formWidgets = [];
/**
* @var string Active session key, used for editing forms and deferred bindings.
*/
public $sessionKey;
/**
* @var bool Render this form with uneditable preview data.
*/
public $previewMode = FALSE;
/**
* @var \Admin\Classes\Widgets
*/
protected $widgetManager;
protected $optionModelTypes;
public function initialize()
{
$this->fillFromConfig([
'fields',
'tabs',
'secondaryTabs',
'model',
'data',
'arrayName',
'context',
]);
$this->optionModelTypes = [
'select', 'selectlist',
'radio', 'radiolist', 'radiotoggle',
'checkbox', 'checkboxlist', 'checkboxtoggle',
'partial',
];
$this->widgetManager = Widgets::instance();
$this->allTabs = (object)$this->allTabs;
$this->validateModel();
}
/**
* Ensure fields are defined and form widgets are registered so they can
* also be bound to the controller this allows their AJAX features to
* operate.
* @return void
*/
public function bindToController()
{
$this->defineFormFields();
parent::bindToController();
}
public function loadAssets()
{
$this->addJs('vendor/bootstrap-multiselect/bootstrap-multiselect.js', 'bootstrap-multiselect-js');
$this->addCss('vendor/bootstrap-multiselect/bootstrap-multiselect.css', 'bootstrap-multiselect-css');
$this->addJs('vendor/inputmask/jquery.inputmask.min.js', 'inputmask-js');
$this->addJs('js/selectlist.js', 'selectlist-js');
$this->addCss('css/selectlist.css', 'selectlist-css');
$this->addJs('js/form.js', 'form-js');
}
/**
* Renders the widget.
* Options:
* - preview: Render this form as an uneditable preview. Default: false
* - useContainer: Wrap the result in a container, used by AJAX. Default: true
* - section: Which form section to render. Default: null
* - outside: Renders the Outside Fields section.
* - primary: Renders the Primary Tabs section.
* - secondary: Renders the Secondary Tabs section.
* - null: Renders all sections
*
* @param array $options
*
* @return string|bool The rendered partial contents, or false if suppressing an exception
*/
public function render($options = [])
{
if (isset($options['preview'])) {
$this->previewMode = $options['preview'];
}
if (!isset($options['useContainer'])) {
$options['useContainer'] = TRUE;
}
if (!isset($options['section'])) {
$options['section'] = null;
}
$extraVars = [];
$targetPartial = 'form/form';
// Determine the partial to use based on the supplied section option
if ($section = $options['section']) {
$section = strtolower($section);
if (isset($this->allTabs->{$section})) {
$extraVars['tabs'] = $this->allTabs->{$section};
}
$targetPartial = 'form/form_section';
$extraVars['renderSection'] = $section;
}
// Apply a container to the element
if ($useContainer = $options['useContainer']) {
$targetPartial = 'form/form_container';
}
$this->prepareVars();
// Apply preview mode to widgets
foreach ($this->formWidgets as $widget) {
$widget->previewMode = $this->previewMode;
}
return $this->makePartial($targetPartial, $extraVars);
}
/**
* Renders a single form field
* Options:
* - useContainer: Wrap the result in a container, used by AJAX. Default: true
*
* @param string|array $field The field name or definition
* @param array $options
*
* @return bool|string The rendered partial contents, or false if suppressing an exception
* @throws \Exception
*/
public function renderField($field, $options = [])
{
if (is_string($field)) {
if (!isset($this->allFields[$field])) {
throw new Exception(sprintf(
lang('admin::lang.form.missing_definition'),
$field
));
}
$field = $this->allFields[$field];
}
if (!isset($options['useContainer'])) {
$options['useContainer'] = TRUE;
}
$targetPartial = $options['useContainer'] ? 'form/field_container' : 'form/field';
$this->prepareVars();
return $this->makePartial($targetPartial, ['field' => $field]);
}
/**
* Renders the HTML element for a field
*
* @param \Admin\Classes\BaseFormWidget $field
*
* @return string|bool The rendered partial contents, or false if suppressing an exception
*/
public function renderFieldElement($field)
{
return $this->makePartial(
'form/field_'.$field->type,
[
'field' => $field,
'formModel' => $this->model,
]
);
}
/**
* Prepares the form data
* @return void
*/
protected function prepareVars()
{
$this->defineFormFields();
$this->applyFiltersFromModel();
$this->vars['cookieKey'] = $this->getCookieKey();
$this->vars['activeTab'] = $this->getActiveTab();
$this->vars['outsideTabs'] = $this->allTabs->outside;
$this->vars['primaryTabs'] = $this->allTabs->primary;
}
/**
* Sets or resets form field values.
*
* @param array $data
*
* @return array
*/
public function setFormValues($data = null)
{
if ($data === null) {
$data = $this->getSaveData();
}
$this->prepareModelsToSave($this->model, $data);
if ($this->data !== $this->model) {
$this->data = (object)array_merge((array)$this->data, (array)$data);
}
foreach ($this->allFields as $field) {
$field->value = $this->getFieldValue($field);
}
return $data;
}
/**
* Event handler for refreshing the form.
*
* @return array
*/
public function onRefresh()
{
$result = [];
$saveData = $this->getSaveData();
// Extensibility
$dataHolder = (object)['data' => $saveData];
$this->fireSystemEvent('admin.form.beforeRefresh', [$dataHolder]);
$saveData = $dataHolder->data;
$this->setFormValues($saveData);
$this->prepareVars();
// Extensibility
$this->fireSystemEvent('admin.form.refreshFields', [$this->allFields]);
if (($updateFields = post('fields')) && is_array($updateFields)) {
foreach ($updateFields as $field) {
if (!isset($this->allFields[$field])) {
continue;
}
$fieldObject = $this->allFields[$field];
$result['#'.$fieldObject->getId('group')] = $this->makePartial('field', ['field' => $fieldObject]);
}
}
if (empty($result)) {
$result = ['#'.$this->getId() => $this->makePartial('form')];
}
// Extensibility
$eventResults = $this->fireSystemEvent('admin.form.refresh', [$result], FALSE);
foreach ($eventResults as $eventResult) {
$result = $eventResult + $result;
}
return $result;
}
/**
* Programmatically add fields, used internally and for extensibility.
*
* @param array $fields
* @param string $addToArea
*
* @return void
*/
public function addFields(array $fields, $addToArea = null)
{
foreach ($fields as $name => $config) {
// Check if admin has permissions to show this field
$permissions = array_get($config, 'permissions');
if (!empty($permissions) AND !AdminAuth::getUser()->hasPermission($permissions, FALSE)) {
continue;
}
$fieldObj = $this->makeFormField($name, $config);
$fieldTab = is_array($config) ? array_get($config, 'tab') : null;
// Check that the form field matches the active context
if ($fieldObj->context !== null) {
$context = is_array($fieldObj->context) ? $fieldObj->context : [$fieldObj->context];
if (!in_array($this->getContext(), $context)) {
continue;
}
}
$this->allFields[$name] = $fieldObj;
if (strtolower($addToArea) == FormTabs::SECTION_PRIMARY) {
$this->allTabs->primary->addField($name, $fieldObj, $fieldTab);
}
else {
$this->allTabs->outside->addField($name, $fieldObj);
}
}
}
/**
* Add tab fields.
*
* @param array $fields
*
* @return void
*/
public function addTabFields(array $fields)
{
$this->addFields($fields, 'primary');
}
/**
* Programmatically remove a field.
*
* @param string $name
*
* @return bool
*/
public function removeField($name)
{
if (!isset($this->allFields[$name])) {
return FALSE;
}
// Remove from tabs
$this->allTabs->primary->removeField($name);
$this->allTabs->outside->removeField($name);
// Remove from main collection
unset($this->allFields[$name]);
return TRUE;
}
/**
* Programmatically remove all fields belonging to a tab.
*
* @param string $name
*/
public function removeTab($name)
{
foreach ($this->allFields as $fieldName => $field) {
if ($field->tab == $name) {
$this->removeField($fieldName);
}
}
}
/**
* Creates a form field object from name and configuration.
*
* @param string $name
* @param array $config
*
* @return \Admin\Classes\FormField
* @throws \Exception
*/
public function makeFormField($name, $config)
{
$label = $config['label'] ?? null;
[$fieldName, $fieldContext] = $this->getFieldName($name);
$field = new FormField($fieldName, $label);
if ($fieldContext) {
$field->context = $fieldContext;
}
$field->arrayName = $this->arrayName;
$field->idPrefix = $this->getId();
// Simple field type
if (is_string($config)) {
if ($this->isFormWidget($config) !== FALSE) {
$field->displayAs('widget', ['widget' => $config]);
}
else {
$field->displayAs($config);
}
} // Defined field type
else {
$fieldType = $config['type'] ?? null;
if (!is_string($fieldType) AND !is_null($fieldType)) {
throw new Exception(sprintf(
lang('admin::lang.form.field_invalid_type'), gettype($fieldType)
));
}
// Widget with configuration
if ($this->isFormWidget($fieldType) !== FALSE) {
$config['widget'] = $fieldType;
$fieldType = 'widget';
}
$field->displayAs($fieldType, $config);
}
// Set field value
$field->value = $this->getFieldValue($field);
// Check model if field is required
// if (!$field->required AND $this->model AND method_exists($this->model, 'isAttributeRequired')) {
// $field->required = $this->model->isAttributeRequired($field->fieldName);
// }
// Get field options from model
if (in_array($field->type, $this->optionModelTypes, FALSE)) {
// Defer the execution of option data collection
$field->options(function () use ($field, $config) {
$fieldOptions = $config['options'] ?? null;
$fieldOptions = $this->getOptionsFromModel($field, $fieldOptions);
return $fieldOptions;
});
}
return $field;
}
/**
* Makes a widget object from a form field object.
*
* @param FormField $field
*
* @return \Admin\Classes\BaseFormWidget|null
* @throws \Exception
*/
public function makeFormFieldWidget($field)
{
if ($field->type !== 'widget') {
return null;
}
if (isset($this->formWidgets[$field->fieldName])) {
return $this->formWidgets[$field->fieldName];
}
$widgetConfig = $this->makeConfig($field->config);
$widgetConfig['alias'] = $this->alias.studly_case(name_to_id($field->fieldName));
$widgetConfig['sessionKey'] = $this->getSessionKey();
$widgetConfig['previewMode'] = $this->previewMode;
$widgetConfig['model'] = $this->model;
$widgetConfig['data'] = $this->data;
$widgetName = $widgetConfig['widget'];
$widgetClass = $this->widgetManager->resolveFormWidget($widgetName);
if (!class_exists($widgetClass)) {
throw new Exception(sprintf("The Widget class name '%s' has not been registered", $widgetClass));
}
$widget = $this->makeFormWidget($widgetClass, $field, $widgetConfig);
// If options config is defined, request options from the model.
if (isset($field->config['options'])) {
$field->options(function () use ($field) {
$fieldOptions = $field->config['options'];
if ($fieldOptions === TRUE) $fieldOptions = null;
$fieldOptions = $this->getOptionsFromModel($field, $fieldOptions);
return $fieldOptions;
});
}
return $this->formWidgets[$field->fieldName] = $widget;
}
/**
* Get all the loaded form widgets for the instance.
* @return array
*/
public function getFormWidgets()
{
return $this->formWidgets;
}
/**
* Get a specified form widget
*
* @param string $field
*
* @return mixed
*/
public function getFormWidget($field)
{
if (isset($this->formWidgets[$field])) {
return $this->formWidgets[$field];
}
return null;
}
/**
* Get all the registered fields for the instance.
* @return array
*/
public function getFields()
{
return $this->allFields;
}
/**
* Get a specified field object
*
* @param string $field
*
* @return mixed
*/
public function getField($field)
{
if (isset($this->allFields[$field])) {
return $this->allFields[$field];
}
return null;
}
/**
* Get all tab objects for the instance.
* @return object[FormTabs]
*/
public function getTabs()
{
return $this->allTabs;
}
/**
* Get a specified tab object.
* Options: outside, primary, secondary.
*
* @param string $tab
*
* @return mixed
*/
public function getTab($tab)
{
if (isset($this->allTabs->$tab)) {
return $this->allTabs->$tab;
}
return null;
}
/**
* Parses a field's name
*
* @param string $field Field name
*
* @return array [columnName, context]
*/
public function getFieldName($field)
{
if (strpos($field, '@') === FALSE) {
return [$field, null];
}
return explode('@', $field);
}
/**
* Looks up the field value.
*
* @param mixed $field
*
* @return string
* @throws \Exception
*/
public function getFieldValue($field)
{
if (is_string($field)) {
if (!isset($this->allFields[$field])) {
throw new Exception(lang(
'admin::lang.form.missing_definition',
compact('field')
));
}
$field = $this->allFields[$field];
}
$defaultValue = $field->getDefaultFromData($this->data);
if ($value = post($field->getName()))
return $value;
return $field->getValueFromData($this->data, $defaultValue);
}
/**
* Returns a HTML encoded value containing the other fields this
* field depends on
*
* @param \Admin\Classes\FormField $field
*
* @return string
*/
public function getFieldDepends($field)
{
if (!$field->dependsOn) {
return '';
}
$dependsOn = (array)$field->dependsOn;
$dependsOn = htmlspecialchars(json_encode($dependsOn), ENT_QUOTES, 'UTF-8');
return $dependsOn;
}
/**
* Helper method to determine if field should be rendered
* with label and comments.
*
* @param \Admin\Classes\FormField $field
*
* @return bool
*/
public function showFieldLabels($field)
{
if ($field->type == 'section') {
return FALSE;
}
if ($field->type == 'widget') {
return $this->makeFormFieldWidget($field)->showLabels;
}
return TRUE;
}
/**
* Returns post data from a submitted form.
* @return array
*/
public function getSaveData()
{
$this->defineFormFields();
$result = [];
// Source data
$data = $this->getSourceData();
if (!$data)
$data = [];
// Spin over each field and extract the postback value
foreach ($this->allFields as $field) {
// Disabled and hidden should be omitted from data set
if ($field->disabled OR $field->hidden OR starts_with($field->fieldName, '_')) {
continue;
}
// Handle HTML array, eg: item[key][another]
$parts = name_to_array($field->fieldName);
if (($value = $this->dataArrayGet($data, $parts)) !== null) {
// Number fields should be converted to integers
if ($field->type === 'number') {
$value = !strlen(trim($value)) ? null : (float)$value;
}
$this->dataArraySet($result, $parts, $value);
}
}
// Give widgets an opportunity to process the data.
foreach ($this->formWidgets as $field => $widget) {
$parts = name_to_array($field);
$widgetValue = $widget->getSaveValue($this->dataArrayGet($result, $parts));
$this->dataArraySet($result, $parts, $widgetValue);
}
return $result;
}
public function setActiveTab($tab)
{
$this->activeTab = $tab;
}
public function getActiveTab()
{
$activeTabs = @json_decode(array_get($_COOKIE, 'ti_activeFormTabs'), TRUE);
$cookieKey = $this->getCookieKey();
$activeTab = $activeTabs[$cookieKey] ?? null;
$tabs = $this->allTabs->primary;
$type = $tabs->section;
$activeTabIndex = (int)str_after($activeTab, '#'.$type.'tab-');
// In cases where a tab has been removed, the first tab becomes the active tab
$activeTab = ($activeTabIndex <= count($tabs->fields))
? $activeTab : '#'.$type.'tab-1';
return $this->activeTab = $activeTab;
}
public function getCookieKey()
{
return $this->makeSessionKey().'-'.$this->context;
}
/**
* Returns the active session key.
* @return \Illuminate\Routing\Route|mixed|string
*/
public function getSessionKey()
{
if ($this->sessionKey) {
return $this->sessionKey;
}
if (post('_session_key')) {
return $this->sessionKey = post('_session_key');
}
return $this->sessionKey = uniqid();
}
/**
* Returns the active context for displaying the form.
* @return string
*/
public function getContext()
{
return $this->context;
}
/**
* Validate the supplied form model.
* @return mixed
* @throws \Exception
*/
protected function validateModel()
{
if (!$this->model) {
throw new Exception(sprintf(
lang('admin::lang.form.missing_model'), get_class($this->controller)
));
}
$this->data = !is_null($this->data) ? (object)$this->data : $this->model;
return $this->model;
}
/**
* Creates a flat array of form fields from the configuration.
* Also slots fields in to their respective tabs.
* @return void
*/
protected function defineFormFields()
{
if ($this->fieldsDefined) {
return;
}
// Extensibility
$this->fireSystemEvent('admin.form.extendFieldsBefore');
// Outside fields
if (!isset($this->fields) OR !is_array($this->fields)) {
$this->fields = [];
}
$this->allTabs->outside = new FormTabs(FormTabs::SECTION_OUTSIDE, $this->config);
$this->addFields($this->fields);
// Primary Tabs + Fields
if (!isset($this->tabs['fields']) OR !is_array($this->tabs['fields'])) {
$this->tabs['fields'] = [];
}
$this->allTabs->primary = new FormTabs(FormTabs::SECTION_PRIMARY, $this->tabs);
$this->addFields($this->tabs['fields'], FormTabs::SECTION_PRIMARY);
// Extensibility
$this->fireSystemEvent('admin.form.extendFields', [$this->allFields]);
// Check that the form field matches the active location context
foreach ($this->allFields as $field) {
if ($this->isLocationAware($field->config))
$field->disabled = TRUE;
}
// Convert automatic spanned fields
foreach ($this->allTabs->outside->getFields() as $fields) {
$this->processAutoSpan($fields);
}
foreach ($this->allTabs->primary->getFields() as $fields) {
$this->processAutoSpan($fields);
}
// At least one tab section should stretch
if (
$this->allTabs->primary->stretch === null
AND $this->allTabs->outside->stretch === null
) {
if ($this->allTabs->primary->hasFields()) {
$this->allTabs->primary->stretch = TRUE;
}
else {
$this->allTabs->outside->stretch = TRUE;
}
}
// Bind all form widgets to controller
foreach ($this->allFields as $field) {
if ($field->type !== 'widget') {
continue;
}
$widget = $this->makeFormFieldWidget($field);
$widget->bindToController();
}
$this->fieldsDefined = TRUE;
}
/**
* Converts fields with a span set to 'auto' as either
* 'left' or 'right' depending on the previous field.
*
* @param $fields
*
* @return void
*/
protected function processAutoSpan($fields)
{
$prevSpan = null;
foreach ($fields as $field) {
if (strtolower($field->span) === 'auto') {
if ($prevSpan === 'left') {
$field->span = 'right';
}
else {
$field->span = 'left';
}
}
$prevSpan = $field->span;
}
}
/**
* Check if a field type is a widget or not
*
* @param string $fieldType
*
* @return bool
*/
protected function isFormWidget($fieldType)
{
if ($fieldType === null) {
return FALSE;
}
if (strpos($fieldType, '\\')) {
return TRUE;
}
$widgetClass = $this->widgetManager->resolveFormWidget($fieldType);
if (!class_exists($widgetClass)) {
return FALSE;
}
if (is_subclass_of($widgetClass, 'Admin\Classes\BaseFormWidget')) {
return TRUE;
}
return FALSE;
}
/**
* Allow the model to filter fields.
*/
protected function applyFiltersFromModel()
{
if (method_exists($this->model, 'filterFields')) {
$this->model->filterFields($this);
}
}
/**
* Looks at the model for defined options.
*
* @param FormField $field
* @param $fieldOptions
*
* @return mixed
* @throws \Exception
*/
protected function getOptionsFromModel($field, $fieldOptions)
{
// Advanced usage, supplied options are callable