Completed
Push — master ( d27f99...f4e97d )
by Jean-Christophe
03:35
created

Widget::addDropdownInToolbar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 3
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 boolean
29
	 */
30
	protected $_toolbar;
31
	/**
32
	 * @var PositionInTable
33
	 */
34
	protected $_toolbarPosition;
35
36
37
	public function __construct($identifier,$model,$modelInstance=NULL) {
38
		parent::__construct($identifier);
39
		$this->_template="%wrapContentBefore%%content%%wrapContentAfter%";
40
		$this->setModel($model);
41
		if(isset($modelInstance));
42
			$this->show($modelInstance);
43
	}
44
45
	protected function _getFieldIdentifier($prefix){
46
		return $this->identifier."-{$prefix}-".$this->_instanceViewer->getIdentifier();
47
	}
48
49
	abstract protected  function _setToolbarPosition($table,$captions=NULL);
50
51
	public function show($modelInstance){
52
		$this->_modelInstance=$modelInstance;
53
	}
54
55
	public function getModel() {
56
		return $this->_model;
57
	}
58
59
	public function setModel($_model) {
60
		$this->_model=$_model;
61
		return $this;
62
	}
63
64
	public function getInstanceViewer() {
65
		return $this->_instanceViewer;
66
	}
67
68
	public function setInstanceViewer($_instanceViewer) {
69
		$this->_instanceViewer=$_instanceViewer;
70
		return $this;
71
	}
72
73
	public abstract function getHtmlComponent();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
74
75
	public function setColor($color){
76
		return $this->getHtmlComponent()->setColor($color);
77
	}
78
79
80
	public function setCaptions($captions){
81
		$this->_instanceViewer->setCaptions($captions);
82
		return $this;
83
	}
84
85
	public function setFields($fields){
86
		$this->_instanceViewer->setVisibleProperties($fields);
87
		return $this;
88
	}
89
90
	public function addField($field){
91
		$this->_instanceViewer->addField($field);
92
		return $this;
93
	}
94
95
	public function insertField($index,$field){
96
		$this->_instanceViewer->insertField($index, $field);
97
		return $this;
98
	}
99
100
	public function insertInField($index,$field){
101
		$this->_instanceViewer->insertInField($index, $field);
102
		return $this;
103
	}
104
105
	public function setValueFunction($index,$callback){
106
		$this->_instanceViewer->setValueFunction($index, $callback);
107
		return $this;
108
	}
109
110
	public function setIdentifierFunction($callback){
111
		$this->_instanceViewer->setIdentifierFunction($callback);
112
		return $this;
113
	}
114
115
	/**
116
	 * @return \Ajax\semantic\html\collections\menus\HtmlMenu
117
	 */
118
	public function getToolbar(){
119
		if(isset($this->_toolbar)===false){
120
			$this->_toolbar=new HtmlMenu("toolbar-".$this->identifier);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Ajax\semantic\html\...-' . $this->identifier) of type object<Ajax\semantic\htm...ections\menus\HtmlMenu> is incompatible with the declared type boolean of property $_toolbar.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
121
			//$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...
122
		}
123
		return $this->_toolbar;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->_toolbar; of type Ajax\semantic\html\colle...\menus\HtmlMenu|boolean adds the type boolean to the return on line 123 which is incompatible with the return type documented by Ajax\common\Widget::getToolbar of type Ajax\semantic\html\collections\menus\HtmlMenu.
Loading history...
124
	}
125
126
	/**
127
	 * Adds a new element in toolbar
128
	 * @param mixed $element
129
	 * @param callable $callback function to call on $element
130
	 * @return \Ajax\common\html\HtmlDoubleElement
131
	 */
132
	public function addInToolbar($element,$callback=NULL){
133
		$tb=$this->getToolbar();
134
		if(isset($callback)){
135
			if(\is_callable($callback)){
136
				$callback($element);
137
			}
138
		}
139
		return $tb->addItem($element);
140
	}
141
142
	public function addItemInToolbar($caption,$icon=NULL,$callback=NULL){
143
		$result=$this->addInToolbar($caption,$callback);
144
		if(isset($icon))
145
			$result->addIcon($icon);
146
		return $result;
147
	}
148
149
	public function addItemsInToolbar(array $items,$callback=NULL){
150
		if(JArray::isAssociative($items)){
151
			foreach ($items as $icon=>$item){
152
				$this->addItemInToolbar($item,$icon,$callback);
153
			}
154
		}else{
155
			foreach ($items as $item){
156
				$this->addItemInToolbar($item,null,$callback);
157
			}
158
		}
159
		return $this;
160
	}
161
162 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...
163
		$dd=$value;
164
		if (\is_string($value)) {
165
			$dd=new HtmlDropdown("dropdown-". $this->identifier."-".$value, $value, $items);
166
		}
167
		return $this->addInToolbar($dd,$callback);
168
	}
169
170
	public function addButtonInToolbar($caption,$callback=NULL){
171
		$bt=new HtmlButton("",$caption);
172
		return $this->addInToolbar($bt,$callback);
173
	}
174
175
	public function addButtonsInToolbar(array $captions,$asIcon=false,$callback=NULL){
176
		$bts=new HtmlButtonGroups("",$captions,$asIcon);
177
		return $this->addInToolbar($bts,$callback);
178
	}
179
180
	public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){
181
		$bt=new HtmlButton("",$caption);
182
		$bt->addIcon($icon,$before,$labeled);
183
		return $this->addInToolbar($bt);
184
	}
185
186
	/**
187
	 * Defines a callback function to call for modifying captions
188
	 * function parameters are $captions: the captions to modify and $instance: the active model instance
189
	 * @param callable $captionCallback
190
	 * @return Widget
191
	 */
192
	public function setCaptionCallback($captionCallback) {
193
		$this->_instanceViewer->setCaptionCallback($captionCallback);
194
		return $this;
195
	}
196
}