Passed
Push — master ( 254bb0...6d276a )
by Jean-Christophe
02:18
created

FieldAsTrait::fieldAsDropDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 6
rs 10
1
<?php
2
namespace Ajax\semantic\widgets\base;
3
4
use Ajax\service\JString;
5
use Ajax\semantic\html\elements\HtmlImage;
6
use Ajax\semantic\html\base\constants\Size;
7
use Ajax\semantic\html\elements\HtmlLabel;
8
use Ajax\semantic\html\modules\HtmlProgress;
9
use Ajax\semantic\html\modules\HtmlRating;
10
use Ajax\semantic\html\elements\HtmlHeader;
11
use Ajax\semantic\html\collections\form\HtmlFormCheckbox;
12
use Ajax\semantic\html\collections\form\HtmlFormInput;
13
use Ajax\semantic\html\collections\form\HtmlFormDropdown;
14
use Ajax\semantic\html\collections\form\HtmlFormTextarea;
15
use Ajax\semantic\html\collections\form\HtmlFormFields;
16
use Ajax\semantic\html\collections\HtmlMessage;
17
use Ajax\semantic\html\elements\HtmlButton;
18
use Ajax\service\JArray;
19
use Ajax\semantic\html\elements\html5\HtmlLink;
20
use Ajax\semantic\html\elements\HtmlFlag;
21
use Ajax\common\html\BaseHtml;
22
use Ajax\semantic\html\collections\form\HtmlFormField;
23
use Ajax\semantic\html\collections\form\HtmlFormRadio;
24
use Ajax\semantic\html\base\HtmlSemDoubleElement;
25
use Ajax\semantic\html\elements\HtmlIcon;
26
use Ajax\semantic\html\elements\HtmlList;
27
28
/**
29
 * trait used in Widget
30
 *
31
 * @author jc
32
 * @property InstanceViewer $_instanceViewer
33
 * @property boolean $_edition
34
 * @property mixed $_modelInstance
35
 * @property boolean $_hasRules
36
 */
37
trait FieldAsTrait {
38
39
	abstract protected function _getFieldIdentifier($prefix, $name = "");
40
41
	abstract public function setValueFunction($index, $callback);
42
43
	abstract protected function _getFieldName($index);
44
45
	abstract protected function _getFieldCaption($index);
46
47
	abstract protected function _buttonAsSubmit(BaseHtml &$button, $event, $url, $responseElement = NULL, $parameters = NULL);
48
49
	/**
50
	 *
51
	 * @param HtmlFormField $element
52
	 * @param array $attributes
53
	 */
54
	protected function _applyAttributes(BaseHtml $element, &$attributes, $index, $instance = null) {
55
		if (isset($attributes["jsCallback"])) {
56
			$callback = $attributes["jsCallback"];
57
			if (\is_callable($callback)) {
58
				$callback($element, $instance, $index, InstanceViewer::$index);
59
			}
60
		}
61
		unset($attributes["rules"]);
62
		unset($attributes["ajax"]);
63
		unset($attributes["visibleHover"]);
64
		$element->fromArray($attributes);
65
	}
66
67
	private function _getLabelField($caption, $icon = NULL) {
68
		$label = new HtmlLabel($this->_getFieldIdentifier("lbl"), $caption, $icon);
69
		return $label;
70
	}
71
72
	protected function _addRules(HtmlFormField $element, &$attributes) {
73
		if (isset($attributes["rules"])) {
74
			$this->_hasRules = true;
75
			$rules = $attributes["rules"];
76
			if (\is_array($rules)) {
77
				$element->addRules($rules);
78
			} else {
79
				$element->addRule($rules);
80
			}
81
			unset($attributes["rules"]);
82
		}
83
	}
84
85
	protected function _prepareFormFields(HtmlFormField &$field, $name, &$attributes) {
86
		$field->setName($name);
87
		$this->_addRules($field, $attributes);
88
		return $field;
89
	}
90
91
	protected function _fieldAs($elementCallback, &$index, $attributes = NULL, $prefix = null) {
92
		$this->setValueFunction($index, function ($value, $instance, $index, $rowIndex) use (&$attributes, $elementCallback, $prefix) {
93
			$caption = $this->_getFieldCaption($index);
94
			$name = $this->_getFieldName($index);
95
			$id = $this->_getFieldIdentifier($prefix, $name);
96
			if (isset($attributes["name"])) {
97
				$name = $attributes["name"];
98
				unset($attributes["name"]);
99
			}
100
			$element = $elementCallback($id, $name, $value, $caption);
101
			if (\is_array($attributes)) {
102
				$this->_applyAttributes($element, $attributes, $index, $instance);
103
			}
104
			$element->setDisabled(! $this->_edition);
105
			return $element;
106
		});
107
		return $this;
108
	}
109
110
	public function fieldAsProgress($index, $label = NULL, $attributes = array()) {
111
		$this->setValueFunction($index, function ($value) use ($label, $attributes) {
112
			$pb = new HtmlProgress($this->_getFieldIdentifier("pb"), $value, $label, $attributes);
113
			return $pb;
114
		});
115
		return $this;
116
	}
117
118
	public function fieldAsRating($index, $max = 5, $icon = "") {
119
		$this->setValueFunction($index, function ($value) use ($max, $icon) {
120
			$rating = new HtmlRating($this->_getFieldIdentifier("rat"), $value, $max, $icon);
121
			return $rating;
122
		});
123
		return $this;
124
	}
125
126
	public function fieldAsLabel($index, $icon = NULL, $attributes = NULL) {
127
		return $this->_fieldAs(function ($id, $name, $value) use ($icon) {
128
			$lbl = new HtmlLabel($id, $value);
129
			if (isset($icon))
130
				$lbl->addIcon($icon);
131
			return $lbl;
132
		}, $index, $attributes, "label");
133
	}
134
135
	public function fieldAsHeader($index, $niveau = 1, $icon = NULL, $attributes = NULL) {
136
		return $this->_fieldAs(function ($id, $name, $value) use ($niveau, $icon) {
137
			$header = new HtmlHeader($id, $niveau, $value);
138
			if (isset($icon))
139
				$header->asIcon($icon, $value);
140
			return $header;
141
		}, $index, $attributes, "header");
142
	}
143
144
	public function fieldAsImage($index, $size = Size::MINI, $circular = false) {
145
		$this->setValueFunction($index, function ($img) use ($size, $circular) {
146
			$image = new HtmlImage($this->_getFieldIdentifier("image"), $img);
147
			$image->setSize($size);
148
			if ($circular)
149
				$image->setCircular();
150
			return $image;
151
		});
152
		return $this;
153
	}
154
155
	public function fieldAsFlag($index) {
156
		$this->setValueFunction($index, function ($flag) {
157
			$flag = new HtmlFlag($this->_getFieldIdentifier("flag"), $flag);
158
			return $flag;
159
		});
160
		return $this;
161
	}
162
163
	public function fieldAsIcon($index) {
164
		$this->setValueFunction($index, function ($icon) {
165
			$icon = new HtmlIcon($this->_getFieldIdentifier("icon"), $icon);
166
			return $icon;
167
		});
168
		return $this;
169
	}
170
171
	public function fieldAsAvatar($index, $attributes = NULL) {
172
		return $this->_fieldAs(function ($id, $name, $value) {
173
			$img = new HtmlImage($id, $value);
174
			$img->asAvatar();
175
			return $img;
176
		}, $index, $attributes, "avatar");
177
	}
178
179
	public function fieldAsRadio($index, $attributes = NULL) {
180
		return $this->_fieldAs(function ($id, $name, $value) use ($attributes) {
181
			$input = new HtmlFormRadio($id, $name, $value, $value);
182
			return $this->_prepareFormFields($input, $name, $attributes);
183
		}, $index, $attributes, "radio");
184
	}
185
186
	public function fieldAsRadios($index, $elements = [], $attributes = NULL) {
187
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($elements) {
188
			return HtmlFormFields::radios($name, $elements, $caption, $value);
189
		}, $index, $attributes, "radios");
190
	}
191
192
	public function fieldAsList($index, $classNames = "", $attributes = NULL) {
193
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($classNames) {
194
			$result = new HtmlList($name, $value);
195
			$result->addClass($classNames);
196
			return $result;
197
		}, $index, $attributes, "list");
198
	}
199
200
	public function fieldAsInput($index, $attributes = NULL) {
201
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) {
202
			$input = new HtmlFormInput($id, $caption, "text", $value);
203
			return $this->_prepareFormFields($input, $name, $attributes);
204
		}, $index, $attributes, "input");
205
	}
206
207
	public function fieldAsLabeledInput($index, $attributes = NULL) {
208
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) {
209
			$input = new HtmlFormInput($id, '', 'text', $value, $caption);
210
			$required = '';
211
			if (isset($attributes['rules'])) {
212
				$rules = json_encode($attributes['rules']);
213
				if (strpos($rules, 'empty') !== false) {
214
					$required = 'required';
215
				}
216
			}
217
			$input->getField()
218
				->labeled($caption)
219
				->setTagName('label')
220
				->addClass($required);
221
			return $this->_prepareFormFields($input, $name, $attributes);
222
		}, $index, $attributes, 'input');
223
	}
224
225
	public function fieldAsDataList($index, ?array $items = [], $attributes = NULL) {
226
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes, $items) {
227
			$input = new HtmlFormInput($id, $caption, "text", $value);
228
			$input->getField()
229
				->addDataList($items);
230
			return $this->_prepareFormFields($input, $name, $attributes);
231
		}, $index, $attributes, "input");
232
	}
233
234
	public function fieldAsFile($index, $attributes = NULL) {
235
		if (isset($this->_form)) {
236
			$this->_form->setProperty('enctype', 'multipart/form-data');
237
		}
238
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) {
239
			$input = new HtmlFormInput($id, $caption);
240
			$input->asFile();
241
			return $this->_prepareFormFields($input, $name, $attributes);
242
		}, $index, $attributes, "input");
243
	}
244
245
	public function fieldAsTextarea($index, $attributes = NULL) {
246
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) {
247
			$textarea = new HtmlFormTextarea($id, $caption, $value);
248
			return $this->_prepareFormFields($textarea, $name, $attributes);
249
		}, $index, $attributes, "textarea");
250
	}
251
252
	public function fieldAsElement($index, $tagName = "div", $baseClass = "", $attributes = NULL) {
253
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes, $tagName, $baseClass) {
254
			$div = new HtmlSemDoubleElement($id, $tagName, $baseClass);
255
			$div->setContent(\htmlentities($value));
256
			$textarea = new HtmlFormField("field-" . $id, $div, $caption);
257
			return $this->_prepareFormFields($textarea, $name, $attributes);
258
		}, $index, $attributes, "element");
259
	}
260
261
	public function fieldAsHidden($index, $attributes = NULL) {
262
		if (! \is_array($attributes)) {
263
			$attributes = [];
264
		}
265
		$attributes["inputType"] = "hidden";
266
		$attributes["style"] = "display:none;";
267
		return $this->fieldAsInput($index, $attributes);
268
	}
269
270
	public function fieldAsCheckbox($index, $attributes = NULL) {
271
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) {
272
			if ($caption === null || $caption === "")
273
				$caption = "";
274
			$input = new HtmlFormCheckbox($id, $caption, $this->_instanceViewer->getIdentifier());
275
			$input->setChecked(JString::isBooleanTrue($value));
276
			return $this->_prepareFormFields($input, $name, $attributes);
277
		}, $index, $attributes, "ck");
278
	}
279
280
	public function fieldAsDropDown($index, $elements = [], $multiple = false, $attributes = NULL) {
281
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($elements, $multiple, $attributes) {
282
			$dd = new HtmlFormDropdown($id, $elements, $caption, $value ?? '');
283
			$dd->asSelect($name, $multiple);
284
			return $this->_prepareFormFields($dd, $name, $attributes);
285
		}, $index, $attributes, "dd");
286
	}
287
288
	public function fieldAsMessage($index, $attributes = NULL) {
289
		return $this->_fieldAs(function ($id, $name, $value, $caption) {
290
			$mess = new HtmlMessage("message-" . $id, $caption);
291
			$mess->addHeader($value);
292
			return $mess;
293
		}, $index, $attributes, "message");
294
	}
295
296
	public function fieldAsLink($index, $attributes = NULL) {
297
		return $this->_fieldAs(function ($id, $name, $value, $caption) {
298
			$lnk = new HtmlLink("message-" . $id, "#", $caption);
299
			return $lnk;
300
		}, $index, $attributes, "link");
301
	}
302
303
	/**
304
	 * Change fields type
305
	 *
306
	 * @param array $types
307
	 *        	an array or associative array $type=>$attributes
308
	 */
309
	public function fieldsAs(array $types) {
310
		$i = 0;
311
		if (JArray::isAssociative($types)) {
312
			foreach ($types as $type => $attributes) {
313
				if (\is_int($type))
314
					$this->fieldAs($i ++, $attributes, []);
315
				else {
316
					$type = preg_replace('/\d/', '', $type);
317
					$this->fieldAs($i ++, $type, $attributes);
318
				}
319
			}
320
		} else {
321
			foreach ($types as $type) {
322
				$this->fieldAs($i ++, $type);
323
			}
324
		}
325
	}
326
327
	public function fieldAs($index, $type, $attributes = NULL) {
328
		$method = "fieldAs" . \ucfirst($type);
329
		if (\method_exists($this, $method)) {
330
			if (! \is_array($attributes)) {
331
				$attributes = [
332
					$index
333
				];
334
			} else {
335
				\array_unshift($attributes, $index);
336
			}
337
			\call_user_func_array([
338
				$this,
339
				$method
340
			], $attributes);
341
		}
342
	}
343
344
	public function fieldAsSubmit($index, $cssStyle = NULL, $url = NULL, $responseElement = NULL, $attributes = NULL) {
345
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($url, $responseElement, $cssStyle, $attributes) {
346
			$button = new HtmlButton($id, $caption, $cssStyle);
347
			$this->_buttonAsSubmit($button, "click", $url, $responseElement, @$attributes["ajax"]);
348
			return $button;
349
		}, $index, $attributes, "submit");
350
	}
351
352
	public function fieldAsButton($index, $cssStyle = NULL, $attributes = NULL) {
353
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($cssStyle) {
354
			$button = new HtmlButton($id, $value, $cssStyle);
355
			return $button;
356
		}, $index, $attributes, "button");
357
	}
358
}
359