Completed
Push — master ( bda9b8...aa1d0c )
by Jean-Christophe
03:12
created

Widget::setCaptionCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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