|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers\crud\viewers\traits; |
|
4
|
|
|
|
|
5
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormField; |
|
6
|
|
|
use Ajax\semantic\html\collections\form\HtmlFormInput; |
|
7
|
|
|
use Ajax\semantic\html\elements\HtmlButton; |
|
8
|
|
|
use Ajax\semantic\html\elements\HtmlIconGroups; |
|
9
|
|
|
use Ajax\semantic\widgets\dataform\DataForm; |
|
10
|
|
|
use Ubiquity\orm\OrmUtils; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Associated with a CRUDController class (part of ModelViewer) |
|
14
|
|
|
* Responsible of the display of the form |
|
15
|
|
|
* Ubiquity\controllers\crud\viewers\traits$FormModelViewerTrait |
|
16
|
|
|
* This class is part of Ubiquity |
|
17
|
|
|
* |
|
18
|
|
|
* @author jcheron <[email protected]> |
|
19
|
|
|
* @version 1.0.0 |
|
20
|
|
|
* @property \Ajax\JsUtils $jquery |
|
21
|
|
|
*/ |
|
22
|
|
|
trait FormModelViewerTrait { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Returns the form for adding or modifying an object |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $identifier |
|
28
|
|
|
* @param object $instance the object to add or modify |
|
29
|
|
|
* @return \Ajax\semantic\widgets\dataform\DataForm |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getForm($identifier, $instance) { |
|
32
|
|
|
$form = $this->jquery->semantic ()->dataForm ( $identifier, $instance ); |
|
33
|
|
|
$form->setLibraryId ( "frmEdit" ); |
|
34
|
|
|
$className = \get_class ( $instance ); |
|
35
|
|
|
$fields = $this->controller->_getAdminData ()->getFormFieldNames ( $className, $instance ); |
|
36
|
|
|
$relFields = OrmUtils::getFieldsInRelations_ ( $className ); |
|
37
|
|
|
|
|
38
|
|
|
$this->setFormFields_ ( $fields, $relFields ); |
|
39
|
|
|
array_unshift ( $fields, "_message" ); |
|
40
|
|
|
$form->setFields ( $fields ); |
|
41
|
|
|
|
|
42
|
|
|
$fieldTypes = OrmUtils::getFieldTypes ( $className ); |
|
43
|
|
|
$this->setFormFieldsComponent ( $form, $fieldTypes ); |
|
44
|
|
|
$this->relationMembersInForm ( $form, $instance, $className, $fields, $relFields ); |
|
|
|
|
|
|
45
|
|
|
OrmUtils::setFieldToMemberNames ( $fields, $relFields ); |
|
46
|
|
|
$form->setCaptions ( $this->getFormCaptions ( $fields, $className, $instance ) ); |
|
47
|
|
|
$message = $this->getFormTitle ( $form, $instance ); |
|
48
|
|
|
$form->setCaption ( "_message", $message ["subMessage"] ); |
|
49
|
|
|
$form->fieldAsMessage ( "_message", [ "icon" => $message ["icon"] ] ); |
|
50
|
|
|
$instance->_message = $message ["message"]; |
|
51
|
|
|
$form->setSubmitParams ( $this->controller->_getBaseRoute () . "/update", "#frm-add-update" ); |
|
52
|
|
|
$form->onGenerateField ( [ $this,'onGenerateFormField' ] ); |
|
53
|
|
|
return $form; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Returns a form for member editing |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $identifier |
|
60
|
|
|
* @param object $instance |
|
61
|
|
|
* @param string $member |
|
62
|
|
|
* @param string $td |
|
63
|
|
|
* @param string $part |
|
64
|
|
|
* @return \Ajax\semantic\widgets\dataform\DataForm |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getMemberForm($identifier, $instance, $member, $td, $part) { |
|
67
|
|
|
$editMemberParams = $this->getEditMemberParams_ ( $part ); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
$form = $this->jquery->semantic ()->dataForm ( $identifier, $instance ); |
|
70
|
|
|
$form->on ( "dblclick", "", true, true ); |
|
71
|
|
|
$form->setProperty ( "onsubmit", "return false;" ); |
|
72
|
|
|
$form->addClass ( "_memberForm" ); |
|
73
|
|
|
$className = \get_class ( $instance ); |
|
74
|
|
|
$fields = [ "id",$member ]; |
|
75
|
|
|
$relFields = OrmUtils::getFieldsInRelations_ ( $className ); |
|
76
|
|
|
$hasRelations = $this->setFormFields_ ( $fields, $relFields ); |
|
77
|
|
|
$form->setFields ( $fields ); |
|
78
|
|
|
$fieldTypes = OrmUtils::getFieldTypes ( $className ); |
|
79
|
|
|
$form->fieldAsHidden ( 0 ); |
|
80
|
|
|
$this->setMemberFormFieldsComponent ( $form, $fieldTypes ); |
|
81
|
|
|
if ($hasRelations) { |
|
82
|
|
|
$this->relationMembersInForm ( $form, $instance, $className, $fields, $relFields ); |
|
83
|
|
|
} |
|
84
|
|
|
$form->setCaptions ( [ "","" ] ); |
|
85
|
|
|
$form->onGenerateField ( function (HtmlFormField $f, $nb) use ($identifier, $editMemberParams) { |
|
86
|
|
|
if ($nb == 1) { |
|
87
|
|
|
$f->setSize ( "mini" ); |
|
88
|
|
|
if ($editMemberParams->getHasButtons ()) { |
|
89
|
|
|
$btO = HtmlButton::icon ( "btO", "check" )->addClass ( "green mini compact" )->onClick ( "\$('#" . $identifier . "').trigger('validate');", true, true ); |
|
90
|
|
|
$btC = HtmlButton::icon ( "btC", "close" )->addClass ( "mini compact" )->onClick ( "\$('#" . $identifier . "').trigger('endEdit');" ); |
|
91
|
|
|
$f->wrap ( "<div class='fields' style='margin:0;'>", [ $btO,$btC,"</div>" ] ); |
|
92
|
|
|
if (! $editMemberParams->getHasPopup ()) { |
|
93
|
|
|
$f->setWidth ( 16 )->setProperty ( "style", "padding-left:0;" ); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
$f->on ( "keydown", "if(event.which == 13) {\$('#" . $identifier . "').trigger('validate');}if(event.keyCode===27) {\$('#" . $identifier . "').trigger('endEdit');}" ); |
|
97
|
|
|
$f->onClick ( "return false;", true, true ); |
|
98
|
|
|
} else { |
|
99
|
|
|
$f->setProperty ( "style", "display: none;" ); |
|
100
|
|
|
} |
|
101
|
|
|
} ); |
|
102
|
|
|
$form->setSubmitParams ( $this->controller->_getBaseRoute () . "/updateMember/" . $member . "/" . $editMemberParams->getUpdateCallback (), "#" . $td, [ "attr" => "","hasLoader" => false,"jsCallback" => "$(self).remove();","jqueryDone" => "html" ] ); |
|
103
|
|
|
if ($editMemberParams->getHasPopup ()) { |
|
104
|
|
|
$endEdit = "\$('#" . $identifier . "').html();\$('.popup').hide();\$('#" . $td . "').popup('destroy');"; |
|
105
|
|
|
$validate = $endEdit; |
|
106
|
|
|
} else { |
|
107
|
|
|
$endEdit = "let td=\$('#" . $td . "');td.html(td.data('originalText'));"; |
|
108
|
|
|
$validate = ""; |
|
109
|
|
|
} |
|
110
|
|
|
$form->on ( "endEdit", $endEdit ); |
|
111
|
|
|
$form->on ( "validate", "\$('#" . $identifier . "').form('submit');" . $validate ); |
|
112
|
|
|
$this->jquery->execAtLast ( "$('form').find('input[type=text],textarea,select').filter(':visible:first').focus();" ); |
|
113
|
|
|
return $form; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
private function setFormFields_(&$fields, $relFields) { |
|
117
|
|
|
$hasRelations = false; |
|
118
|
|
|
$relFields = array_flip ( $relFields ); |
|
119
|
|
|
foreach ( $fields as $index => $field ) { |
|
120
|
|
|
if (isset ( $relFields [$field] )) { |
|
121
|
|
|
$fields [$index] = $relFields [$field]; |
|
122
|
|
|
$hasRelations = true; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
return $hasRelations; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Returns an associative array defining form message title with keys "icon","message","subMessage" |
|
130
|
|
|
* |
|
131
|
|
|
* @param DataForm $form |
|
132
|
|
|
* @param object $instance |
|
133
|
|
|
* @return array the message title |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function getFormTitle($form, $instance) { |
|
|
|
|
|
|
136
|
|
|
$type = ($instance->_new) ? "new" : "edit"; |
|
137
|
|
|
$messageInfos = [ "new" => [ "icon" => HtmlIconGroups::corner ( "table", "plus", "big" ),"subMessage" => "New object creation" ],"edit" => [ "icon" => HtmlIconGroups::corner ( "table", "edit", "big" ),"subMessage" => "Editing an existing object" ] ]; |
|
138
|
|
|
$message = $messageInfos [$type]; |
|
139
|
|
|
$message ["message"] = \get_class ( $instance ); |
|
140
|
|
|
return $message; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Sets the components for each field |
|
145
|
|
|
* |
|
146
|
|
|
* @param DataForm $form |
|
147
|
|
|
* @param array $fieldTypes associative array of field names (keys) and types (values) |
|
148
|
|
|
*/ |
|
149
|
|
|
public function setFormFieldsComponent(DataForm $form, $fieldTypes) { |
|
150
|
|
|
$this->setFormFieldsComponent_ ( $form, $fieldTypes ); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Sets the components for each field |
|
155
|
|
|
* |
|
156
|
|
|
* @param DataForm $form |
|
157
|
|
|
* @param array $fieldTypes associative array of field names (keys) and types (values) |
|
158
|
|
|
*/ |
|
159
|
|
|
public function setMemberFormFieldsComponent(DataForm $form, $fieldTypes) { |
|
160
|
|
|
$this->setFormFieldsComponent_ ( $form, $fieldTypes ); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
protected function setFormFieldsComponent_(DataForm $form, $fieldTypes) { |
|
164
|
|
|
foreach ( $fieldTypes as $property => $type ) { |
|
165
|
|
|
switch ($property) { |
|
166
|
|
|
case "password" : |
|
167
|
|
|
$form->fieldAsInput ( $property, [ "inputType" => "password" ] ); |
|
168
|
|
|
$form->setValidationParams ( [ "inline" => true ] ); |
|
169
|
|
|
break; |
|
170
|
|
|
case "email" : |
|
171
|
|
|
case "mail" : |
|
172
|
|
|
$form->fieldAsInput ( $property, [ "inputType" => "email","rules" => [ [ "email" ] ] ] ); |
|
173
|
|
|
break; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
switch ($type) { |
|
177
|
|
|
case "tinyint(1)" : |
|
178
|
|
|
$form->fieldAsCheckbox ( $property ); |
|
179
|
|
|
break; |
|
180
|
|
|
case "int" : |
|
181
|
|
|
case "integer" : |
|
182
|
|
|
$form->fieldAsInput ( $property, [ "inputType" => "number" ] ); |
|
183
|
|
|
break; |
|
184
|
|
|
case "date" : |
|
185
|
|
|
$form->fieldAsInput ( $property, [ "inputType" => "date" ] ); |
|
186
|
|
|
break; |
|
187
|
|
|
case "datetime" : |
|
188
|
|
|
$form->fieldAsInput ( $property, [ "inputType" => "datetime-local" ] ); |
|
189
|
|
|
break; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* For doing something when $field is generated in form |
|
196
|
|
|
* |
|
197
|
|
|
* @param mixed $field |
|
198
|
|
|
*/ |
|
199
|
|
|
public function onGenerateFormField($field, $nb) { |
|
|
|
|
|
|
200
|
|
|
if ($field instanceof HtmlFormInput) { |
|
201
|
|
|
if ($field->getDataField ()->getProperty ( 'type' ) == "datetime-local") { |
|
202
|
|
|
$v = $field->getDataField ()->getProperty ( 'value' ); |
|
203
|
|
|
$field->getDataField ()->setValue ( date ( "Y-m-d\TH:i:s", strtotime ( $v ) ) ); |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
return; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* Condition to determine if the edit or add form is modal for $model objects |
|
211
|
|
|
* |
|
212
|
|
|
* @param array $objects |
|
213
|
|
|
* @param string $model |
|
214
|
|
|
* @return boolean |
|
215
|
|
|
*/ |
|
216
|
|
|
public function isModal($objects, $model) { |
|
|
|
|
|
|
217
|
|
|
return \count ( $objects ) > 5; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Returns the captions for form fields |
|
222
|
|
|
* |
|
223
|
|
|
* @param array $captions |
|
224
|
|
|
* @param string $className |
|
225
|
|
|
*/ |
|
226
|
|
|
public function getFormCaptions($captions, $className, $instance) { |
|
|
|
|
|
|
227
|
|
|
return \array_map ( "ucfirst", $captions ); |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
|