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(); |
|
|
|
|
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); |
|
|
|
|
118
|
|
|
//$this->_toolbar->setSecondary(); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
return $this->_toolbar; |
|
|
|
|
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
|
|
|
} |