Completed
Push — master ( 72aa1a...163c98 )
by Jean-Christophe
03:34
created

Widget::addButtonInToolbar()   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
11
abstract class Widget extends HtmlDoubleElement {
12
	use FieldAsTrait;
13
14
	/**
15
	 * @var string classname
16
	 */
17
	protected $_model;
18
	protected $_modelInstance;
19
	/**
20
	 * @var InstanceViewer
21
	 */
22
	protected $_instanceViewer;
23
	/**
24
	 * @var boolean
25
	 */
26
	protected $_toolbar;
27
	/**
28
	 * @var PositionInTable
29
	 */
30
	protected $_toolbarPosition;
31
32
33
	public function __construct($identifier,$model,$modelInstance=NULL) {
34
		parent::__construct($identifier);
35
		$this->_template="%wrapContentBefore%%content%%wrapContentAfter%";
36
		$this->setModel($model);
37
		if(isset($modelInstance));
38
			$this->show($modelInstance);
39
	}
40
41
	protected function _getFieldIdentifier($prefix){
42
		return $this->identifier."-{$prefix}-".$this->_instanceViewer->getIdentifier();
43
	}
44
45
	protected abstract function _setToolbarPosition($table,$captions=NULL);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
46
47
	public function show($modelInstance){
48
		$this->_modelInstance=$modelInstance;
49
	}
50
51
	public function getModel() {
52
		return $this->_model;
53
	}
54
55
	public function setModel($_model) {
56
		$this->_model=$_model;
57
		return $this;
58
	}
59
60
	public function getInstanceViewer() {
61
		return $this->_instanceViewer;
62
	}
63
64
	public function setInstanceViewer($_instanceViewer) {
65
		$this->_instanceViewer=$_instanceViewer;
66
		return $this;
67
	}
68
69
	public abstract function getHtmlComponent();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
70
71
	public function setColor($color){
72
		return $this->getHtmlComponent()->setColor($color);
73
	}
74
75
76
	public function setCaptions($captions){
77
		$this->_instanceViewer->setCaptions($captions);
78
		return $this;
79
	}
80
81
	public function setFields($fields){
82
		$this->_instanceViewer->setVisibleProperties($fields);
83
		return $this;
84
	}
85
86
	public function addField($field){
87
		$this->_instanceViewer->addField($field);
88
		return $this;
89
	}
90
91
	public function insertField($index,$field){
92
		$this->_instanceViewer->insertField($index, $field);
93
		return $this;
94
	}
95
96
	public function insertInField($index,$field){
97
		$this->_instanceViewer->insertInField($index, $field);
98
		return $this;
99
	}
100
101
	public function setValueFunction($index,$callback){
102
		$this->_instanceViewer->setValueFunction($index, $callback);
103
		return $this;
104
	}
105
106
	public function setIdentifierFunction($callback){
107
		$this->_instanceViewer->setIdentifierFunction($callback);
108
		return $this;
109
	}
110
111
	/**
112
	 * @return \Ajax\semantic\html\collections\menus\HtmlMenu
113
	 */
114
	public function getToolbar(){
115
		if(isset($this->_toolbar)===false){
116
			$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...
117
			$this->_toolbar->setSecondary();
118
		}
119
		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 119 which is incompatible with the return type documented by Ajax\common\Widget::getToolbar of type Ajax\semantic\html\collections\menus\HtmlMenu.
Loading history...
120
	}
121
122
	/**
123
	 * Adds a new element in toolbar
124
	 * @param mixed $element
125
	 * @param callable $callback function to call on $element
126
	 * @return \Ajax\common\html\HtmlDoubleElement
127
	 */
128
	public function addInToolbar($element,$callback=NULL){
129
		$tb=$this->getToolbar();
130
		if(isset($callback)){
131
			if(\is_callable($callback)){
132
				$callback($element);
133
			}
134
		}
135
		return $tb->addItem($element);
136
	}
137
138
	public function addItemInToolbar($caption,$icon=NULL){
139
		$result=$this->addInToolbar($caption);
140
		$result->addIcon($icon);
141
		return $result;
142
	}
143
144
	public function addButtonInToolbar($caption,$callback=NULL){
145
		$bt=new HtmlButton("",$caption);
146
		return $this->addInToolbar($bt,$callback);
147
	}
148
149
	public function addLabelledIconButtonInToolbar($caption,$icon,$before=true,$labeled=false){
150
		$bt=new HtmlButton("",$caption);
151
		$bt->addIcon($icon,$before,$labeled);
152
		return $this->addInToolbar($bt);
153
	}
154
}