|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\FormBuilder\Application\Modules\Admin\Forms\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\FormBuilder\Application\Models\Fields\Traits\FormFieldTrait; |
|
6
|
|
|
use Nip\Records\Record; |
|
7
|
|
|
use Nip\Records\RecordManager; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Trait FieldFormTrait |
|
11
|
|
|
* @package ByTIC\FormBuilder\Application\Modules\Admin\Forms\Traits |
|
12
|
|
|
* |
|
13
|
|
|
* @method Record|FormFieldTrait getModel() |
|
14
|
|
|
* @method \Nip_Form_Element_Abstract getElement() |
|
15
|
|
|
*/ |
|
16
|
|
|
trait FieldFormTrait |
|
17
|
|
|
{ |
|
18
|
|
|
public function init() |
|
19
|
|
|
{ |
|
20
|
|
|
parent::init(); |
|
21
|
|
|
|
|
22
|
|
|
$this->addSelect('type', translator()->translate('type'), true); |
|
|
|
|
|
|
23
|
|
|
$this->getElement('type')->setAttrib('disabled', 'disabled'); |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
$this->addInput('label', translator()->translate('name'), true); |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
$this->addInput('label_intern', translator()->translate('internal_name')); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
$this->addInput('help', 'help'); |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
$this->initVisibleElement(); |
|
32
|
|
|
$this->initMandatoryElement(); |
|
33
|
|
|
|
|
34
|
|
|
if ($this->hasListingFlags()) { |
|
35
|
|
|
$this->initListingElement(); |
|
36
|
|
|
} |
|
37
|
|
|
if ($this->hasFilterFlags()) { |
|
38
|
|
|
$this->initFilterElement(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$this->addButton('save', translator()->translate('submit')); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
View Code Duplication |
protected function initVisibleElement() |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
$this->addBsRadioGroup('visible', translator()->translate('visible'), true); |
|
|
|
|
|
|
47
|
|
|
$this->visible->addOption('yes', translator()->translate('yes')) |
|
|
|
|
|
|
48
|
|
|
->addOption('no', translator()->translate('no')) |
|
49
|
|
|
->getRenderer()->setSeparator(''); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
View Code Duplication |
protected function initMandatoryElement() |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
$this->addBsRadioGroup('mandatory', translator()->translate('mandatory'), true); |
|
|
|
|
|
|
55
|
|
|
$this->mandatory->addOption('yes', translator()->translate('yes')) |
|
|
|
|
|
|
56
|
|
|
->addOption('no', translator()->translate('no')) |
|
57
|
|
|
->getRenderer()->setSeparator(''); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function hasListingFlags() |
|
64
|
|
|
{ |
|
65
|
|
|
return count($this->getListingFlags()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function getListingFlags() |
|
72
|
|
|
{ |
|
73
|
|
|
return []; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
View Code Duplication |
protected function initListingElement() |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
$this->addCheckboxGroup('listing', $this->getModelManager()->getLabel('form.listing'), false); |
|
|
|
|
|
|
79
|
|
|
$this->listing->addOption('public', $this->getModelManager()->getLabel('form.listing.public')) |
|
|
|
|
|
|
80
|
|
|
->addOption('admin', $this->getModelManager()->getLabel('form.listing.admin')) |
|
81
|
|
|
->getRenderer()->setSeparator(''); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return RecordManager |
|
86
|
|
|
*/ |
|
87
|
|
|
abstract public function getModelManager(); |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function hasFilterFlags() |
|
93
|
|
|
{ |
|
94
|
|
|
return count($this->getFilterFlags()); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function getFilterFlags() |
|
101
|
|
|
{ |
|
102
|
|
|
return []; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
View Code Duplication |
protected function initFilterElement() |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
|
$this->addCheckboxGroup('filter', $this->getModelManager()->getLabel('form.filter'), false); |
|
|
|
|
|
|
108
|
|
|
$this->filter->addOption('public', $this->getModelManager()->getLabel('form.filter.public')) |
|
|
|
|
|
|
109
|
|
|
->addOption('admin', $this->getModelManager()->getLabel('form.filter.admin')) |
|
110
|
|
|
->getRenderer()->setSeparator(''); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function getDataFromModel() |
|
114
|
|
|
{ |
|
115
|
|
|
$fields = $this->getModel()->getManager()->getTypes(); |
|
|
|
|
|
|
116
|
|
|
foreach ($fields as $field) { |
|
117
|
|
|
$this->getElement('type')->addOption($field->getName(), $field->getLabel()); |
|
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
parent::getDataFromModel(); |
|
121
|
|
|
|
|
122
|
|
|
if ($this->hasListingFlags()) { |
|
123
|
|
|
$this->getElement('listing')->setValue($this->getModel()->getListingArray()); |
|
|
|
|
|
|
124
|
|
|
} |
|
125
|
|
|
if ($this->hasFilterFlags()) { |
|
126
|
|
|
$this->getElement('filter')->setValue($this->getModel()->getFilterArray()); |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
$this->getModel()->getType()->adminGetDataFromModel($this); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function processValidation() |
|
132
|
|
|
{ |
|
133
|
|
|
parent::processValidation(); |
|
134
|
|
|
$this->getModel()->getType()->adminProcessValidation($this); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function saveToModel() |
|
138
|
|
|
{ |
|
139
|
|
|
parent::saveToModel(); |
|
140
|
|
|
|
|
141
|
|
|
if ($this->hasFilterFlags()) { |
|
142
|
|
|
$this->getModel()->listing = implode(',', $this->getElement('listing')->getValue()); |
|
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
if ($this->hasFilterFlags()) { |
|
145
|
|
|
$this->getModel()->filter = implode(',', $this->getElement('filter')->getValue()); |
|
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
$this->getModel()->getType()->adminSaveToModel($this); |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.