Completed
Push — master ( 3c0bc3...b6875a )
by Jean-Christophe
03:24
created

Widget::setEdition()   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 1
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
15
abstract class Widget extends HtmlDoubleElement {
16
	use FieldAsTrait;
17
18
	/**
19
	 * @var string classname
20
	 */
21
	protected $_model;
22
	protected $_modelInstance;
23
	/**
24
	 * @var InstanceViewer
25
	 */
26
	protected $_instanceViewer;
27
	/**
28
	 * @var HtmlMenu
29
	 */
30
	protected $_toolbar;
31
	/**
32
	 * @var PositionInTable
33
	 */
34
	protected $_toolbarPosition;
35
36
	protected $_edition;
37
38
39
	public function __construct($identifier,$model,$modelInstance=NULL) {
40
		parent::__construct($identifier);
41
		$this->_template="%wrapContentBefore%%content%%wrapContentAfter%";
42
		$this->setModel($model);
43
		if(isset($modelInstance));
44
			$this->show($modelInstance);
45
	}
46
47
	protected function _init($instanceViewer,$contentKey,$content,$edition){
48
		$this->_instanceViewer=$instanceViewer;
49
		$this->content=[$contentKey=>$content];
50
		$this->_toolbarPosition=PositionInTable::BEFORETABLE;
51
		$this->_edition=$edition;
52
	}
53
54
	protected function _getFieldIdentifier($prefix){
55
		return $this->identifier."-{$prefix}-".$this->_instanceViewer->getIdentifier();
56
	}
57
58
	abstract protected  function _setToolbarPosition($table,$captions=NULL);
59
60
	public function show($modelInstance){
61
		$this->_modelInstance=$modelInstance;
62
	}
63
64
	public function getModel() {
65
		return $this->_model;
66
	}
67
68
	public function setModel($_model) {
69
		$this->_model=$_model;
70
		return $this;
71
	}
72
73
	public function getInstanceViewer() {
74
		return $this->_instanceViewer;
75
	}
76
77
	public function setInstanceViewer($_instanceViewer) {
78
		$this->_instanceViewer=$_instanceViewer;
79
		return $this;
80
	}
81
82
	abstract public function getHtmlComponent();
83
84
	public function setColor($color){
85
		return $this->getHtmlComponent()->setColor($color);
86
	}
87
88
89
	public function setCaptions($captions){
90
		$this->_instanceViewer->setCaptions($captions);
91
		return $this;
92
	}
93
94
	public function setFields($fields){
95
		$this->_instanceViewer->setVisibleProperties($fields);
96
		return $this;
97
	}
98
99
	public function addField($field){
100
		$this->_instanceViewer->addField($field);
101
		return $this;
102
	}
103
104
	public function insertField($index,$field){
105
		$this->_instanceViewer->insertField($index, $field);
106
		return $this;
107
	}
108
109
	public function insertInField($index,$field){
110
		$this->_instanceViewer->insertInField($index, $field);
111
		return $this;
112
	}
113
114
	public function setValueFunction($index,$callback){
115
		$this->_instanceViewer->setValueFunction($index, $callback);
116
		return $this;
117
	}
118
119
	public function setIdentifierFunction($callback){
120
		$this->_instanceViewer->setIdentifierFunction($callback);
121
		return $this;
122
	}
123
124
	/**
125
	 * @return \Ajax\semantic\html\collections\menus\HtmlMenu
126
	 */
127
	public function getToolbar(){
128
		if(isset($this->_toolbar)===false){
129
			$this->_toolbar=new HtmlMenu("toolbar-".$this->identifier);
130
			//$this->_toolbar->setSecondary();
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
131
		}
132
		return $this->_toolbar;
133
	}
134
135
	/**
136
	 * Adds a new element in toolbar
137
	 * @param mixed $element
138
	 * @param callable $callback function to call on $element
139
	 * @return \Ajax\common\html\HtmlDoubleElement
140
	 */
141
	public function addInToolbar($element,$callback=NULL){
142
		$tb=$this->getToolbar();
143
		if(isset($callback)){
144
			if(\is_callable($callback)){
145
				$callback($element);
146
			}
147
		}
148
		return $tb->addItem($element);
149
	}
150
151
	/**
152
	 * @param string $caption
153
	 * @param string $icon
154
	 * @param callable $callback function($element)
155
	 * @return \Ajax\common\html\HtmlDoubleElement
156
	 */
157
	public function addItemInToolbar($caption,$icon=NULL,$callback=NULL){
158
		$result=$this->addInToolbar($caption,$callback);
159
		if(isset($icon))
160
			$result->addIcon($icon);
161
		return $result;
162
	}
163
164
	/**
165
	 * @param array $items
166
	 * @param callable $callback function($element)
167
	 * @return \Ajax\common\Widget
168
	 */
169
	public function addItemsInToolbar(array $items,$callback=NULL){
170
		if(JArray::isAssociative($items)){
171
			foreach ($items as $icon=>$item){
172
				$this->addItemInToolbar($item,$icon,$callback);
173
			}
174
		}else{
175
			foreach ($items as $item){
176
				$this->addItemInToolbar($item,null,$callback);
177
			}
178
		}
179
		return $this;
180
	}
181
182
	/**
183
	 * @param string $value
184
	 * @param array|NULL $items
185
	 * @param callable $callback function($element)
186
	 * @return \Ajax\common\html\HtmlDoubleElement
187
	 */
188 View Code Duplication
	public function addDropdownInToolbar($value,$items=NULL,$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...
189
		$dd=$value;
190
		if (\is_string($value)) {
191
			$dd=new HtmlDropdown("dropdown-". $this->identifier."-".$value, $value, $items);
0 ignored issues
show
Bug introduced by
It seems like $items defined by parameter $items on line 188 can also be of type null; however, Ajax\semantic\html\modul...Dropdown::__construct() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
192
		}
193
		return $this->addInToolbar($dd,$callback);
194
	}
195
196
	/**
197
	 * @param unknown $caption
198
	 * @param callable $callback function($element)
199
	 * @return \Ajax\common\html\HtmlDoubleElement
200
	 */
201
	public function addButtonInToolbar($caption,$callback=NULL){
202
		$bt=new HtmlButton("",$caption);
203
		return $this->addInToolbar($bt,$callback);
204
	}
205
206
	/**
207
	 * @param array $captions
208
	 * @param boolean $asIcon
209
	 * @param callable $callback function($element)
210
	 * @return \Ajax\common\html\HtmlDoubleElement
211
	 */
212
	public function addButtonsInToolbar(array $captions,$asIcon=false,$callback=NULL){
213
		$bts=new HtmlButtonGroups("",$captions,$asIcon);
214
		return $this->addInToolbar($bts,$callback);
215
	}
216
217
	/**
218
	 * @param string $caption
219
	 * @param string $icon
220
	 * @param boolean $before
221
	 * @param boolean $labeled
222
	 * @return \Ajax\common\html\HtmlDoubleElement
223
	 */
224
	public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){
225
		$bt=new HtmlButton("",$caption);
226
		$bt->addIcon($icon,$before,$labeled);
227
		return $this->addInToolbar($bt);
228
	}
229
230
	/**
231
	 * Defines a callback function to call for modifying captions
232
	 * function parameters are $captions: the captions to modify and $instance: the active model instance
233
	 * @param callable $captionCallback
234
	 * @return Widget
235
	 */
236
	public function setCaptionCallback($captionCallback) {
237
		$this->_instanceViewer->setCaptionCallback($captionCallback);
238
		return $this;
239
	}
240
241
	/**
242
	 * Makes the input fields editable
243
	 * @param boolean $_edition
244
	 * @return \Ajax\common\Widget
245
	 */
246
	public function setEdition($_edition=true) {
247
		$this->_edition=$_edition;
248
		return $this;
249
	}
250
251
	/**
252
	 * Defines the default function which displays fields value
253
	 * @param callable $defaultValueFunction
254
	 * @return \Ajax\common\Widget
255
	 */
256
	public function setDefaultValueFunction($defaultValueFunction){
257
		$this->_instanceViewer->setDefaultValueFunction($defaultValueFunction);
258
		return $this;
259
	}
260
261
}