Completed
Push — master ( 2c7b4a...2bc7fe )
by Jean-Christophe
03:22
created

Widget::insertInField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace Ajax\common;
4
5
use Ajax\common\html\HtmlDoubleElement;
6
use Ajax\semantic\html\elements\HtmlButton;
7
use Ajax\semantic\widgets\datatable\PositionInTable;
8
use Ajax\semantic\html\collections\menus\HtmlMenu;
9
use Ajax\semantic\widgets\base\FieldAsTrait;
10
use Ajax\semantic\html\elements\HtmlButtonGroups;
11
use Ajax\semantic\widgets\base\InstanceViewer;
12
use Ajax\semantic\html\modules\HtmlDropdown;
13
use Ajax\service\JArray;
14
use Ajax\service\Javascript;
15
16
abstract class Widget extends HtmlDoubleElement {
17
	use FieldAsTrait;
18
19
	/**
20
	 * @var string classname
21
	 */
22
	protected $_model;
23
	protected $_modelInstance;
24
	/**
25
	 * @var InstanceViewer
26
	 */
27
	protected $_instanceViewer;
28
	/**
29
	 * @var HtmlMenu
30
	 */
31
	protected $_toolbar;
32
	/**
33
	 * @var PositionInTable
34
	 */
35
	protected $_toolbarPosition;
36
37
	protected $_edition;
38
39
40
	public function __construct($identifier,$model,$modelInstance=NULL) {
41
		parent::__construct($identifier);
42
		$this->_template="%wrapContentBefore%%content%%wrapContentAfter%";
43
		$this->setModel($model);
44
		if(isset($modelInstance));
45
			$this->show($modelInstance);
46
	}
47
48
	protected function _init($instanceViewer,$contentKey,$content,$edition){
49
		$this->_instanceViewer=$instanceViewer;
50
		$this->content=[$contentKey=>$content];
51
		$this->_toolbarPosition=PositionInTable::BEFORETABLE;
52
		$this->_edition=$edition;
53
	}
54
55
	protected function _getFieldIdentifier($prefix,$name=""){
56
		return $this->identifier."-{$prefix}-".$this->_instanceViewer->getIdentifier();
57
	}
58
59
	protected function _getFieldName($index){
60
		return $this->_instanceViewer->getFieldName($index);
61
	}
62
63
	protected function _getFieldCaption($index){
64
		return $this->_instanceViewer->getCaption($index);
65
	}
66
67
	abstract protected  function _setToolbarPosition($table,$captions=NULL);
68
69
	public function show($modelInstance){
70
		$this->_modelInstance=$modelInstance;
71
	}
72
73
	public function getModel() {
74
		return $this->_model;
75
	}
76
77
	public function setModel($_model) {
78
		$this->_model=$_model;
79
		return $this;
80
	}
81
82
	public function getInstanceViewer() {
83
		return $this->_instanceViewer;
84
	}
85
86
	public function setInstanceViewer($_instanceViewer) {
87
		$this->_instanceViewer=$_instanceViewer;
88
		return $this;
89
	}
90
91
	abstract public function getHtmlComponent();
92
93
	public function setColor($color){
94
		return $this->getHtmlComponent()->setColor($color);
95
	}
96
97
98
	public function setCaptions($captions){
99
		$this->_instanceViewer->setCaptions($captions);
100
		return $this;
101
	}
102
103
	public function setFields($fields){
104
		$this->_instanceViewer->setVisibleProperties($fields);
105
		return $this;
106
	}
107
108
	public function addField($field){
109
		$this->_instanceViewer->addField($field);
110
		return $this;
111
	}
112
113
	public function insertField($index,$field){
114
		$this->_instanceViewer->insertField($index, $field);
115
		return $this;
116
	}
117
118
	public function insertInField($index,$field){
119
		$this->_instanceViewer->insertInField($index, $field);
120
		return $this;
121
	}
122
123
	public function setValueFunction($index,$callback){
124
		$this->_instanceViewer->setValueFunction($index, $callback);
125
		return $this;
126
	}
127
128
	public function setIdentifierFunction($callback){
129
		$this->_instanceViewer->setIdentifierFunction($callback);
130
		return $this;
131
	}
132
133
	/**
134
	 * @return \Ajax\semantic\html\collections\menus\HtmlMenu
135
	 */
136
	public function getToolbar(){
137
		if(isset($this->_toolbar)===false){
138
			$this->_toolbar=new HtmlMenu("toolbar-".$this->identifier);
139
		}
140
		return $this->_toolbar;
141
	}
142
143
	/**
144
	 * Adds a new element in toolbar
145
	 * @param mixed $element
146
	 * @param callable $callback function to call on $element
147
	 * @return \Ajax\common\html\HtmlDoubleElement
148
	 */
149
	public function addInToolbar($element,$callback=NULL){
150
		$tb=$this->getToolbar();
151
		if(isset($callback)){
152
			if(\is_callable($callback)){
153
				$callback($element);
154
			}
155
		}
156
		return $tb->addItem($element);
157
	}
158
159
	/**
160
	 * @param string $caption
161
	 * @param string $icon
162
	 * @param callable $callback function($element)
163
	 * @return \Ajax\common\html\HtmlDoubleElement
164
	 */
165
	public function addItemInToolbar($caption,$icon=NULL,$callback=NULL){
166
		$result=$this->addInToolbar($caption,$callback);
167
		if(isset($icon))
168
			$result->addIcon($icon);
169
		return $result;
170
	}
171
172
	/**
173
	 * @param array $items
174
	 * @param callable $callback function($element)
175
	 * @return \Ajax\common\Widget
176
	 */
177
	public function addItemsInToolbar(array $items,$callback=NULL){
178
		if(JArray::isAssociative($items)){
179
			foreach ($items as $icon=>$item){
180
				$this->addItemInToolbar($item,$icon,$callback);
181
			}
182
		}else{
183
			foreach ($items as $item){
184
				$this->addItemInToolbar($item,null,$callback);
185
			}
186
		}
187
		return $this;
188
	}
189
190
	/**
191
	 * @param string $value
192
	 * @param array $items
193
	 * @param callable $callback function($element)
194
	 * @return \Ajax\common\html\HtmlDoubleElement
195
	 */
196 View Code Duplication
	public function addDropdownInToolbar($value,$items,$callback=NULL){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
197
		$dd=$value;
198
		if (\is_string($value)) {
199
			$dd=new HtmlDropdown("dropdown-". $this->identifier."-".$value, $value, $items);
200
		}
201
		return $this->addInToolbar($dd,$callback);
202
	}
203
204
	/**
205
	 * @param unknown $caption
206
	 * @param callable $callback function($element)
207
	 * @return \Ajax\common\html\HtmlDoubleElement
208
	 */
209
	public function addButtonInToolbar($caption,$callback=NULL){
210
		$bt=new HtmlButton("",$caption);
211
		return $this->addInToolbar($bt,$callback);
212
	}
213
214
	/**
215
	 * @param array $captions
216
	 * @param boolean $asIcon
217
	 * @param callable $callback function($element)
218
	 * @return \Ajax\common\html\HtmlDoubleElement
219
	 */
220
	public function addButtonsInToolbar(array $captions,$asIcon=false,$callback=NULL){
221
		$bts=new HtmlButtonGroups("",$captions,$asIcon);
222
		return $this->addInToolbar($bts,$callback);
223
	}
224
225
	/**
226
	 * @param string $caption
227
	 * @param string $icon
228
	 * @param boolean $before
229
	 * @param boolean $labeled
230
	 * @return \Ajax\common\html\HtmlDoubleElement
231
	 */
232
	public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){
233
		$bt=new HtmlButton("",$caption);
234
		$bt->addIcon($icon,$before,$labeled);
235
		return $this->addInToolbar($bt);
236
	}
237
238
	/**
239
	 * Defines a callback function to call for modifying captions
240
	 * function parameters are $captions: the captions to modify and $instance: the active model instance
241
	 * @param callable $captionCallback
242
	 * @return Widget
243
	 */
244
	public function setCaptionCallback($captionCallback) {
245
		$this->_instanceViewer->setCaptionCallback($captionCallback);
246
		return $this;
247
	}
248
249
	/**
250
	 * Makes the input fields editable
251
	 * @param boolean $_edition
252
	 * @return \Ajax\common\Widget
253
	 */
254
	public function setEdition($_edition=true) {
255
		$this->_edition=$_edition;
256
		return $this;
257
	}
258
259
	/**
260
	 * Defines the default function which displays fields value
261
	 * @param callable $defaultValueFunction function parameters are : $name : the field name, $value : the field value ,$index : the field index, $instance : the active instance of model
262
	 * @return \Ajax\common\Widget
263
	 */
264
	public function setDefaultValueFunction($defaultValueFunction){
265
		$this->_instanceViewer->setDefaultValueFunction($defaultValueFunction);
266
		return $this;
267
	}
268
269
	/**
270
	 * @param string|boolean $disable
271
	 * @return string
272
	 */
273
	public function jsDisabled($disable=true){
274
		return "$('#".$this->identifier." .ui.input,#".$this->identifier." .ui.dropdown,#".$this->identifier." .ui.checkbox').toggleClass('disabled',".$disable.");";
275
	}
276
277
	/**
278
	 * @param string $caption
279
	 * @param callable $callback function($element)
280
	 * @return \Ajax\common\html\HtmlDoubleElement
281
	 */
282
	public function addEditButtonInToolbar($caption,$callback=NULL){
283
		$bt=new HtmlButton($this->identifier."-editBtn",$caption);
284
		$bt->setToggle();
285
		$bt->setActive($this->_edition);
286
		$bt->onClick($this->jsDisabled(Javascript::prep_value("!$(event.target).hasClass('active')")));
287
		return $this->addInToolbar($bt,$callback);
288
	}
289
290
	public function setToolbar(HtmlMenu $_toolbar) {
291
		$this->_toolbar=$_toolbar;
292
		return $this;
293
	}
294
295
	public function setToolbarPosition($_toolbarPosition) {
296
		$this->_toolbarPosition=$_toolbarPosition;
297
		return $this;
298
	}
299
300
}