Passed
Push — master ( e370f7...d93737 )
by Jean-Christophe
03:07
created

FieldAsTrait::fieldAsIcon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
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 fieldAsDataList($index, ?array $items = [], $attributes = NULL) {
208
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes, $items) {
209
			$input = new HtmlFormInput($id, $caption, "text", $value);
210
			$input->getField()
211
				->addDataList($items);
212
			return $this->_prepareFormFields($input, $name, $attributes);
213
		}, $index, $attributes, "input");
214
	}
215
216
	public function fieldAsFile($index, $attributes = NULL) {
217
		if (isset($this->_form)) {
218
			$this->_form->setProperty('enctype', 'multipart/form-data');
219
		}
220
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) {
221
			$input = new HtmlFormInput($id, $caption);
222
			$input->asFile();
223
			return $this->_prepareFormFields($input, $name, $attributes);
224
		}, $index, $attributes, "input");
225
	}
226
227
	public function fieldAsTextarea($index, $attributes = NULL) {
228
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) {
229
			$textarea = new HtmlFormTextarea($id, $caption, $value);
230
			return $this->_prepareFormFields($textarea, $name, $attributes);
231
		}, $index, $attributes, "textarea");
232
	}
233
234
	public function fieldAsElement($index, $tagName = "div", $baseClass = "", $attributes = NULL) {
235
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes, $tagName, $baseClass) {
236
			$div = new HtmlSemDoubleElement($id, $tagName, $baseClass);
237
			$div->setContent(\htmlentities($value));
238
			$textarea = new HtmlFormField("field-" . $id, $div, $caption);
239
			return $this->_prepareFormFields($textarea, $name, $attributes);
240
		}, $index, $attributes, "element");
241
	}
242
243
	public function fieldAsHidden($index, $attributes = NULL) {
244
		if (! \is_array($attributes)) {
245
			$attributes = [];
246
		}
247
		$attributes["inputType"] = "hidden";
248
		$attributes["style"] = "display:none;";
249
		return $this->fieldAsInput($index, $attributes);
250
	}
251
252
	public function fieldAsCheckbox($index, $attributes = NULL) {
253
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($attributes) {
254
			if ($caption === null || $caption === "")
255
				$caption = "";
256
			$input = new HtmlFormCheckbox($id, $caption, $this->_instanceViewer->getIdentifier());
257
			$input->setChecked(JString::isBooleanTrue($value));
258
			return $this->_prepareFormFields($input, $name, $attributes);
259
		}, $index, $attributes, "ck");
260
	}
261
262
	public function fieldAsDropDown($index, $elements = [], $multiple = false, $attributes = NULL) {
263
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($elements, $multiple, $attributes) {
264
			$dd = new HtmlFormDropdown($id, $elements, $caption, $value);
265
			$dd->asSelect($name, $multiple);
266
			return $this->_prepareFormFields($dd, $name, $attributes);
267
		}, $index, $attributes, "dd");
268
	}
269
270
	public function fieldAsMessage($index, $attributes = NULL) {
271
		return $this->_fieldAs(function ($id, $name, $value, $caption) {
272
			$mess = new HtmlMessage("message-" . $id, $caption);
273
			$mess->addHeader($value);
274
			return $mess;
275
		}, $index, $attributes, "message");
276
	}
277
278
	public function fieldAsLink($index, $attributes = NULL) {
279
		return $this->_fieldAs(function ($id, $name, $value, $caption) {
280
			$lnk = new HtmlLink("message-" . $id, "#", $caption);
281
			return $lnk;
282
		}, $index, $attributes, "link");
283
	}
284
285
	/**
286
	 * Change fields type
287
	 *
288
	 * @param array $types
289
	 *        	an array or associative array $type=>$attributes
290
	 */
291
	public function fieldsAs(array $types) {
292
		$i = 0;
293
		if (JArray::isAssociative($types)) {
294
			foreach ($types as $type => $attributes) {
295
				if (\is_int($type))
296
					$this->fieldAs($i ++, $attributes, []);
297
				else {
298
					$type = preg_replace('/\d/', '', $type);
299
					$this->fieldAs($i ++, $type, $attributes);
300
				}
301
			}
302
		} else {
303
			foreach ($types as $type) {
304
				$this->fieldAs($i ++, $type);
305
			}
306
		}
307
	}
308
309
	public function fieldAs($index, $type, $attributes = NULL) {
310
		$method = "fieldAs" . \ucfirst($type);
311
		if (\method_exists($this, $method)) {
312
			if (! \is_array($attributes)) {
313
				$attributes = [
314
					$index
315
				];
316
			} else {
317
				\array_unshift($attributes, $index);
318
			}
319
			\call_user_func_array([
320
				$this,
321
				$method
322
			], $attributes);
323
		}
324
	}
325
326
	public function fieldAsSubmit($index, $cssStyle = NULL, $url = NULL, $responseElement = NULL, $attributes = NULL) {
327
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($url, $responseElement, $cssStyle, $attributes) {
328
			$button = new HtmlButton($id, $caption, $cssStyle);
329
			$this->_buttonAsSubmit($button, "click", $url, $responseElement, @$attributes["ajax"]);
330
			return $button;
331
		}, $index, $attributes, "submit");
332
	}
333
334
	public function fieldAsButton($index, $cssStyle = NULL, $attributes = NULL) {
335
		return $this->_fieldAs(function ($id, $name, $value, $caption) use ($cssStyle) {
336
			$button = new HtmlButton($id, $value, $cssStyle);
337
			return $button;
338
		}, $index, $attributes, "button");
339
	}
340
}
341