Completed
Push — master ( 8b49e3...3c0bc3 )
by Jean-Christophe
03:22
created

Widget::_init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
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
	public function addItemInToolbar($caption,$icon=NULL,$callback=NULL){
152
		$result=$this->addInToolbar($caption,$callback);
153
		if(isset($icon))
154
			$result->addIcon($icon);
155
		return $result;
156
	}
157
158
	public function addItemsInToolbar(array $items,$callback=NULL){
159
		if(JArray::isAssociative($items)){
160
			foreach ($items as $icon=>$item){
161
				$this->addItemInToolbar($item,$icon,$callback);
162
			}
163
		}else{
164
			foreach ($items as $item){
165
				$this->addItemInToolbar($item,null,$callback);
166
			}
167
		}
168
		return $this;
169
	}
170
171 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...
172
		$dd=$value;
173
		if (\is_string($value)) {
174
			$dd=new HtmlDropdown("dropdown-". $this->identifier."-".$value, $value, $items);
175
		}
176
		return $this->addInToolbar($dd,$callback);
177
	}
178
179
	public function addButtonInToolbar($caption,$callback=NULL){
180
		$bt=new HtmlButton("",$caption);
181
		return $this->addInToolbar($bt,$callback);
182
	}
183
184
	public function addButtonsInToolbar(array $captions,$asIcon=false,$callback=NULL){
185
		$bts=new HtmlButtonGroups("",$captions,$asIcon);
186
		return $this->addInToolbar($bts,$callback);
187
	}
188
189
	public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){
190
		$bt=new HtmlButton("",$caption);
191
		$bt->addIcon($icon,$before,$labeled);
192
		return $this->addInToolbar($bt);
193
	}
194
195
	/**
196
	 * Defines a callback function to call for modifying captions
197
	 * function parameters are $captions: the captions to modify and $instance: the active model instance
198
	 * @param callable $captionCallback
199
	 * @return Widget
200
	 */
201
	public function setCaptionCallback($captionCallback) {
202
		$this->_instanceViewer->setCaptionCallback($captionCallback);
203
		return $this;
204
	}
205
206
	public function setEdition($_edition) {
207
		$this->_edition=$_edition;
208
		return $this;
209
	}
210
211
}