|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
abstract class Ajde_Crud_Field extends Ajde_Object_Standard |
|
4
|
|
|
{ |
|
5
|
|
|
/** |
|
6
|
|
|
* @var Ajde_Crud |
|
7
|
|
|
*/ |
|
8
|
|
|
protected $_crud; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $_type; |
|
14
|
|
|
|
|
15
|
|
|
protected $_useSpan = 12; |
|
16
|
|
|
protected $_attributes = []; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(Ajde_Crud $crud, $fieldOptions) |
|
19
|
|
|
{ |
|
20
|
|
|
$explode = explode('_', get_class($this)); |
|
21
|
|
|
end($explode); |
|
22
|
|
|
$this->_type = strtolower(current($explode)); |
|
23
|
|
|
$this->_crud = $crud; |
|
24
|
|
|
|
|
25
|
|
|
/* defaults */ |
|
26
|
|
|
$this->_data = [ |
|
27
|
|
|
'name' => isset($fieldOptions['name']) ? $fieldOptions['name'] : false, |
|
28
|
|
|
'type' => 'text', |
|
29
|
|
|
'length' => 255, |
|
30
|
|
|
'default' => '', |
|
31
|
|
|
'label' => isset($fieldOptions['name']) ? ucfirst($fieldOptions['name']) : false, |
|
32
|
|
|
'isRequired' => false, |
|
33
|
|
|
'isPK' => false, |
|
34
|
|
|
'isAutoIncrement' => false, |
|
35
|
|
|
'isAutoUpdate' => false, |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/* options */ |
|
39
|
|
|
foreach ($fieldOptions as $key => $value) { |
|
40
|
|
|
$this->set($key, $value); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$this->prepare(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
protected function prepare() |
|
47
|
|
|
{ |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Getters and setters. |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getName() |
|
54
|
|
|
{ |
|
55
|
|
|
return parent::getName(); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getDbType() |
|
59
|
|
|
{ |
|
60
|
|
|
return parent::getDbType(); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function getLabel() |
|
64
|
|
|
{ |
|
65
|
|
|
return parent::getLabel(); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getLength() |
|
69
|
|
|
{ |
|
70
|
|
|
return parent::getLength(); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function getIsRequired() |
|
74
|
|
|
{ |
|
75
|
|
|
return parent::getIsRequired(); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getDefault() |
|
79
|
|
|
{ |
|
80
|
|
|
return parent::getDefault(); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function getIsAutoIncrement() |
|
84
|
|
|
{ |
|
85
|
|
|
return parent::getIsAutoIncrement(); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function getIsAutoUpdate() |
|
89
|
|
|
{ |
|
90
|
|
|
return parent::getIsAutoUpdate(); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function getIsUnique() |
|
94
|
|
|
{ |
|
95
|
|
|
return parent::getIsUnique(); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function getValue() |
|
99
|
|
|
{ |
|
100
|
|
|
if (parent::hasValue()) { |
|
|
|
|
|
|
101
|
|
|
return parent::getValue(); |
|
|
|
|
|
|
102
|
|
|
} else { |
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function getType() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->_type; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Template functions. |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getHtml() |
|
116
|
|
|
{ |
|
117
|
|
|
$template = $this->_getFieldTemplate(); |
|
118
|
|
|
$template->assign('field', $this); |
|
119
|
|
|
|
|
120
|
|
|
return $template->render(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function getInput($id = null) |
|
124
|
|
|
{ |
|
125
|
|
|
$template = $this->_getInputTemplate(); |
|
126
|
|
|
$template->assign('field', $this); |
|
127
|
|
|
$template->assign('id', $id); |
|
128
|
|
|
|
|
129
|
|
|
return $template->render(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
protected function _getFieldTemplate() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->_getTemplate('field'); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
protected function _getInputTemplate() |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->_getTemplate('field/'.$this->_type); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
protected function _getTemplate($action) |
|
143
|
|
|
{ |
|
144
|
|
|
$template = null; |
|
145
|
|
|
if (Ajde_Template::exist(MODULE_DIR.'_core/', 'crud/'.$action) !== false) { |
|
146
|
|
|
$template = new Ajde_Template(MODULE_DIR.'_core/', 'crud/'.$action); |
|
147
|
|
|
Ajde::app()->getDocument()->autoAddResources($template); |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
View Code Duplication |
if ($this->_hasCustomTemplate($action)) { |
|
|
|
|
|
|
150
|
|
|
$base = $this->_getCustomTemplateBase(); |
|
151
|
|
|
$action = $this->_getCustomTemplateAction($action); |
|
152
|
|
|
$template = new Ajde_Template($base, $action); |
|
153
|
|
|
} |
|
154
|
|
|
if ($template instanceof Ajde_Template) { |
|
155
|
|
|
return $template; |
|
156
|
|
|
} else { |
|
157
|
|
|
// TODO: |
|
158
|
|
|
throw new Ajde_Exception('No crud template found for field '.$action); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
View Code Duplication |
protected function _hasCustomTemplate($action) |
|
|
|
|
|
|
163
|
|
|
{ |
|
164
|
|
|
$base = $this->_getCustomTemplateBase(); |
|
165
|
|
|
$action = $this->_getCustomTemplateAction($action); |
|
166
|
|
|
|
|
167
|
|
|
return Ajde_Template::exist($base, $action) !== false; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
protected function _getCustomTemplateBase() |
|
171
|
|
|
{ |
|
172
|
|
|
return MODULE_DIR.$this->_crud->getCustomTemplateModule().'/'; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
protected function _getCustomTemplateAction($action) |
|
176
|
|
|
{ |
|
177
|
|
|
return 'crud/'.(string) $this->_crud->getModel()->getTable().'/'.$action; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* HTML functions. |
|
182
|
|
|
*/ |
|
183
|
|
|
public function getHtmlRequired() |
|
184
|
|
|
{ |
|
185
|
|
|
//return '<span class="required">*</span>'; |
|
186
|
|
|
return ''; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
public function getHtmlPK() |
|
190
|
|
|
{ |
|
191
|
|
|
return " <img src='".MEDIA_DIR."icons/16/key_login.png' style='vertical-align: middle;' title='Primary key' />"; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
public function getHtmlAttributesAsArray() |
|
195
|
|
|
{ |
|
196
|
|
|
if (empty($this->_attributes)) { |
|
197
|
|
|
$attributes = []; |
|
198
|
|
|
if (method_exists($this, '_getHtmlAttributes')) { |
|
199
|
|
|
$attributes = $this->_getHtmlAttributes(); |
|
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
$attributes['name'] = $this->getName(); |
|
202
|
|
|
if (!array_key_exists('id', $attributes)) { |
|
203
|
|
|
$attributes['id'] = 'in_'.$this->getName(); |
|
204
|
|
|
} |
|
205
|
|
|
if ($this->hasNotEmpty('class')) { |
|
206
|
|
|
if (array_key_exists('class', $attributes)) { |
|
207
|
|
|
$attributes['class'] .= ' '.$this->getClass(); |
|
|
|
|
|
|
208
|
|
|
} else { |
|
209
|
|
|
$attributes['class'] = $this->getClass(); |
|
|
|
|
|
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
if ($this->_useSpan !== false) { |
|
213
|
|
|
if (array_key_exists('class', $attributes)) { |
|
214
|
|
|
$attributes['class'] .= ' span'.$this->_useSpan; |
|
215
|
|
|
} else { |
|
216
|
|
|
$attributes['class'] = 'span'.$this->_useSpan; |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
$this->_attributes = $attributes; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
return $this->_attributes; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
public function getHtmlAttribute($name) |
|
226
|
|
|
{ |
|
227
|
|
|
$attributes = $this->getHtmlAttributesAsArray(); |
|
228
|
|
|
if (isset($attributes[$name])) { |
|
229
|
|
|
return $attributes[$name]; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
return false; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
public function getHtmlAttributes() |
|
236
|
|
|
{ |
|
237
|
|
|
$attributes = $this->getHtmlAttributesAsArray(); |
|
238
|
|
|
|
|
239
|
|
|
$text = ''; |
|
240
|
|
|
foreach ($attributes as $k => $v) { |
|
241
|
|
|
if (strlen($v)) { |
|
242
|
|
|
$text .= $k.'="'.$v.'" '; |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
return $text; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @return Ajde_Crud |
|
251
|
|
|
*/ |
|
252
|
|
|
public function getCrud() |
|
253
|
|
|
{ |
|
254
|
|
|
return $this->_crud; |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: