|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ajax\semantic\widgets\base; |
|
3
|
|
|
|
|
4
|
|
|
use Ajax\service\JString; |
|
5
|
|
|
use Ajax\service\JArray; |
|
6
|
|
|
use Ajax\service\JReflection; |
|
7
|
|
|
|
|
8
|
|
|
class InstanceViewer { |
|
9
|
|
|
|
|
10
|
|
|
protected $widgetIdentifier; |
|
11
|
|
|
|
|
12
|
|
|
protected $instance; |
|
13
|
|
|
|
|
14
|
|
|
protected $reflect; |
|
15
|
|
|
|
|
16
|
|
|
protected $properties; |
|
17
|
|
|
|
|
18
|
|
|
protected $visibleProperties; |
|
19
|
|
|
|
|
20
|
|
|
protected $values; |
|
21
|
|
|
|
|
22
|
|
|
protected $afterCompile; |
|
23
|
|
|
|
|
24
|
|
|
protected $captions; |
|
25
|
|
|
|
|
26
|
|
|
protected $captionCallback; |
|
27
|
|
|
|
|
28
|
|
|
protected $defaultValueFunction; |
|
29
|
|
|
|
|
30
|
|
|
protected $groupByFields; |
|
31
|
|
|
|
|
32
|
|
|
public static $index = 0; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct($identifier, $instance = NULL, $captions = NULL) { |
|
35
|
|
|
$this->widgetIdentifier = $identifier; |
|
36
|
|
|
$this->values = []; |
|
37
|
|
|
$this->afterCompile = []; |
|
38
|
|
|
if (isset($instance)) |
|
39
|
|
|
$this->setInstance($instance); |
|
40
|
|
|
$this->setCaptions($captions); |
|
41
|
|
|
$this->captionCallback = NULL; |
|
42
|
|
|
$this->defaultValueFunction = function ($name, $value) { |
|
43
|
|
|
return $value; |
|
44
|
|
|
}; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function moveFieldTo($from, $to) { |
|
48
|
|
|
if (JArray::moveElementTo($this->visibleProperties, $from, $to)) { |
|
49
|
|
|
return JArray::moveElementTo($this->values, $from, $to); |
|
50
|
|
|
} |
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function swapFields($index1, $index2) { |
|
55
|
|
|
if (JArray::swapElements($this->visibleProperties, $index1, $index2)) { |
|
56
|
|
|
return JArray::swapElements($this->values, $index1, $index2); |
|
57
|
|
|
} |
|
58
|
|
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function removeField($index) { |
|
62
|
|
|
\array_splice($this->visibleProperties, $index, 1); |
|
63
|
|
|
\array_splice($this->values, $index, 1); |
|
64
|
|
|
\array_splice($this->captions, $index, 1); |
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getValues() { |
|
69
|
|
|
$values = []; |
|
70
|
|
|
$index = 0; |
|
71
|
|
|
$count = $this->count(); |
|
72
|
|
|
$hasGroupby = is_array($this->groupByFields); |
|
73
|
|
|
if (! $hasGroupby) { |
|
74
|
|
|
while ($index < $count) { |
|
75
|
|
|
$values[] = $this->getValue($index ++); |
|
76
|
|
|
} |
|
77
|
|
|
} else { |
|
78
|
|
|
while ($index < $count) { |
|
79
|
|
|
if (array_search($index, $this->groupByFields) === false) { |
|
80
|
|
|
$values[] = $this->getValue($index); |
|
81
|
|
|
} |
|
82
|
|
|
$index ++; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
return $values; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function getIdentifier($index = NULL) { |
|
89
|
|
|
if (! isset($index)) |
|
90
|
|
|
$index = self::$index; |
|
91
|
|
|
$value = $index; |
|
92
|
|
|
if (isset($this->values["identifier"])) { |
|
93
|
|
|
if (\is_string($this->values["identifier"])) |
|
94
|
|
|
$value = JReflection::callMethod($this->instance, $this->values["identifier"], []); |
|
95
|
|
|
else |
|
96
|
|
|
$value = $this->values["identifier"]($index, $this->instance); |
|
97
|
|
|
} |
|
98
|
|
|
return $value; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function getValue($index) { |
|
102
|
|
|
$property = $this->properties[$index]; |
|
103
|
|
|
return $this->_getValue($property, $index); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
protected function _beforeAddProperty($index, &$field) {} |
|
107
|
|
|
|
|
108
|
|
|
protected function _getDefaultValue($name, $value, $index) { |
|
109
|
|
|
$func = $this->defaultValueFunction; |
|
110
|
|
|
return $func($name, $value, $index, $this->instance); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
protected function _getPropertyValue(\ReflectionProperty $property) { |
|
114
|
|
|
$property->setAccessible(true); |
|
115
|
|
|
return $property->getValue($this->instance); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
protected function _getValue($property, $index) { |
|
119
|
|
|
$value = null; |
|
120
|
|
|
$propertyName = $property; |
|
121
|
|
|
if ($property instanceof \ReflectionProperty) { |
|
122
|
|
|
$value = $this->_getPropertyValue($property); |
|
123
|
|
|
$propertyName = $property->getName(); |
|
124
|
|
|
} elseif (\is_callable($property) && \array_search($property, ['system','date']) === false){ |
|
125
|
|
|
try{ |
|
126
|
|
|
$value = $property($this->instance); |
|
127
|
|
|
}catch(\Error $e){} |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
elseif (\is_array($property)) { |
|
130
|
|
|
$values = \array_map(function ($v) use ($index) { |
|
131
|
|
|
return $this->_getValue($v, $index); |
|
132
|
|
|
}, $property); |
|
133
|
|
|
$value = \implode('', $values); |
|
134
|
|
|
} elseif (\is_string($property)) { |
|
135
|
|
|
$value = ''; |
|
136
|
|
|
if (isset($this->instance->{$property})) { |
|
137
|
|
|
$value = $this->instance->{$property}; |
|
138
|
|
|
} elseif (\method_exists($this->instance, $getter = JReflection::getterName($property))) { |
|
139
|
|
|
$value = JReflection::callMethod($this->instance, $getter, []); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $this->_postGetValue($index, $propertyName, $value); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
protected function _postGetValue($index, $propertyName, $value) { |
|
147
|
|
|
if (isset($this->values[$index])) { |
|
148
|
|
|
$value = $this->values[$index]($value, $this->instance, $index, self::$index); |
|
149
|
|
|
} else { |
|
150
|
|
|
$value = $this->_getDefaultValue($propertyName, $value, $index); |
|
151
|
|
|
} |
|
152
|
|
|
if (isset($this->afterCompile[$index])) { |
|
153
|
|
|
if (\is_callable($this->afterCompile[$index])) { |
|
154
|
|
|
$this->afterCompile[$index]($value, $this->instance, self::$index); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
return $value; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function insertField($index, $field, $key = null) { |
|
161
|
|
|
if (isset($key)) { |
|
162
|
|
|
array_splice($this->visibleProperties, $index, 0, [ |
|
163
|
|
|
$key => $field |
|
164
|
|
|
]); |
|
165
|
|
|
} else { |
|
166
|
|
|
array_splice($this->visibleProperties, $index, 0, $field); |
|
167
|
|
|
} |
|
168
|
|
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
public function sortColumnContent($index, $array) { |
|
172
|
|
|
if (isset($this->visibleProperties[$index])) { |
|
173
|
|
|
if (is_array($this->visibleProperties[$index])) { |
|
174
|
|
|
$this->visibleProperties[$index] = JArray::sortAssociative($this->visibleProperties[$index], $array); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function insertInField($index, $field, $key = null) { |
|
181
|
|
|
$vb = $this->visibleProperties; |
|
182
|
|
|
if (isset($vb[$index])) { |
|
183
|
|
|
if (isset($key)) { |
|
184
|
|
|
if (\is_array($vb[$index])) { |
|
185
|
|
|
$this->visibleProperties[$index][$key] = $field; |
|
186
|
|
|
} else { |
|
187
|
|
|
$this->visibleProperties[$index] = [ |
|
188
|
|
|
$vb[$index], |
|
189
|
|
|
$key => $field |
|
190
|
|
|
]; |
|
191
|
|
|
} |
|
192
|
|
|
} else { |
|
193
|
|
|
if (\is_array($vb[$index])) { |
|
194
|
|
|
$this->visibleProperties[$index][] = $field; |
|
195
|
|
|
} else { |
|
196
|
|
|
$this->visibleProperties[$index] = [ |
|
197
|
|
|
$vb[$index], |
|
198
|
|
|
$field |
|
199
|
|
|
]; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
} else { |
|
203
|
|
|
return $this->insertField($index, $field); |
|
204
|
|
|
} |
|
205
|
|
|
return $this; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
public function addField($field, $key = null) { |
|
209
|
|
|
if (isset($key)) { |
|
210
|
|
|
$this->visibleProperties[] = [ |
|
211
|
|
|
$key => $field |
|
212
|
|
|
]; |
|
213
|
|
|
} else { |
|
214
|
|
|
$this->visibleProperties[] = $field; |
|
215
|
|
|
} |
|
216
|
|
|
return $this; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
public function addFields($fields) { |
|
220
|
|
|
$this->visibleProperties = \array_merge($this->visibleProperties, $fields); |
|
221
|
|
|
return $this; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function count() { |
|
225
|
|
|
return \sizeof($this->properties); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
public function visiblePropertiesCount() { |
|
229
|
|
|
return \sizeof($this->visibleProperties); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
public function getProperty($index) { |
|
233
|
|
|
return $this->properties[$index]; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
public function getFieldName($index) { |
|
237
|
|
|
$property = $this->getProperty($index); |
|
238
|
|
|
if ($property instanceof \ReflectionProperty) { |
|
239
|
|
|
$result = $property->getName(); |
|
240
|
|
|
} elseif (\is_callable($property)) { |
|
241
|
|
|
$result = $this->visibleProperties[$index]; |
|
242
|
|
|
} else { |
|
243
|
|
|
$result = $property; |
|
244
|
|
|
} |
|
245
|
|
|
return $result; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
protected function showableProperty(\ReflectionProperty $rProperty) { |
|
249
|
|
|
return JString::startswith($rProperty->getName(), "_") === false; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
public function setInstance($instance) { |
|
253
|
|
|
if (\is_string($instance)) { |
|
254
|
|
|
$instance = new $instance(); |
|
255
|
|
|
} |
|
256
|
|
|
$this->instance = $instance; |
|
257
|
|
|
$this->properties = []; |
|
258
|
|
|
$this->reflect = new \ReflectionClass($instance); |
|
259
|
|
|
if (JArray::count($this->visibleProperties) === 0) { |
|
260
|
|
|
$this->properties = $this->getDefaultProperties(); |
|
261
|
|
|
} else { |
|
262
|
|
|
foreach ($this->visibleProperties as $property) { |
|
263
|
|
|
$this->setInstanceProperty($property); |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
return $this; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
private function setInstanceProperty($property) { |
|
270
|
|
|
if (\is_callable($property)) { |
|
271
|
|
|
$this->properties[] = $property; |
|
272
|
|
|
} elseif (\is_string($property)) { |
|
273
|
|
|
try { |
|
274
|
|
|
$this->_beforeAddProperty(\sizeof($this->properties), $property); |
|
275
|
|
|
$rProperty = $this->reflect->getProperty($property); |
|
276
|
|
|
$this->properties[] = $rProperty; |
|
277
|
|
|
} catch (\Exception $e) { |
|
278
|
|
|
$this->_beforeAddProperty(\sizeof($this->properties), $property); |
|
279
|
|
|
$this->properties[] = $property; |
|
280
|
|
|
} |
|
281
|
|
|
} elseif (\is_int($property)) { |
|
282
|
|
|
$props = $this->getDefaultProperties(); |
|
283
|
|
|
if (isset($props[$property])) |
|
284
|
|
|
$this->properties[] = $props[$property]; |
|
285
|
|
|
else |
|
286
|
|
|
$this->properties[] = $property; |
|
287
|
|
|
} else { |
|
288
|
|
|
$this->properties[] = $property; |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
protected function getDefaultProperties() { |
|
293
|
|
|
$result = []; |
|
294
|
|
|
$properties = $this->reflect->getProperties(); |
|
295
|
|
|
foreach ($properties as $property) { |
|
296
|
|
|
$showable = $this->showableProperty($property); |
|
297
|
|
|
if ($showable !== false) { |
|
298
|
|
|
$result[] = $property; |
|
299
|
|
|
} |
|
300
|
|
|
} |
|
301
|
|
|
return $result; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
public function setVisibleProperties($visibleProperties) { |
|
305
|
|
|
$this->visibleProperties = $visibleProperties; |
|
306
|
|
|
return $this; |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
public function setValueFunction($index, $callback) { |
|
310
|
|
|
$this->values[$index] = $callback; |
|
311
|
|
|
return $this; |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
public function setIdentifierFunction($callback) { |
|
315
|
|
|
$this->values["identifier"] = $callback; |
|
316
|
|
|
return $this; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
public static function setIndex($index) { |
|
320
|
|
|
self::$index = $index; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
public function getProperties() { |
|
324
|
|
|
return $this->properties; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
public function getCaption($index) { |
|
328
|
|
|
if (isset($this->captions[$index])) { |
|
329
|
|
|
return $this->captions[$index]; |
|
330
|
|
|
} |
|
331
|
|
|
if ($this->properties[$index] instanceof \ReflectionProperty) |
|
332
|
|
|
return $this->properties[$index]->getName(); |
|
333
|
|
|
elseif (\is_callable($this->properties[$index])) |
|
334
|
|
|
return ""; |
|
335
|
|
|
else |
|
336
|
|
|
return $this->properties[$index]; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
public function getCaptions() { |
|
340
|
|
|
$hasGroupby = is_array($this->groupByFields); |
|
341
|
|
|
$count = $this->count(); |
|
342
|
|
|
$moreAdded = false; |
|
343
|
|
|
if (isset($this->captions)) { |
|
344
|
|
|
$captions = \array_values($this->captions); |
|
345
|
|
|
$gbSize = $hasGroupby ? sizeof($this->groupByFields) : 0; |
|
346
|
|
|
$captionsSize = \sizeof($captions); |
|
347
|
|
|
for ($i = $captionsSize; $i < $count - $gbSize; $i ++) { |
|
348
|
|
|
$captions[] = ""; |
|
349
|
|
|
$moreAdded = true; |
|
350
|
|
|
} |
|
351
|
|
|
} else { |
|
352
|
|
|
$captions = []; |
|
353
|
|
|
$index = 0; |
|
354
|
|
|
while ($index < $count) { |
|
355
|
|
|
$captions[] = $this->getCaption($index ++); |
|
356
|
|
|
} |
|
357
|
|
|
} |
|
358
|
|
|
if ($hasGroupby && sizeof($captions) >= $count && ! $moreAdded) { |
|
359
|
|
|
$captions = JArray::removeByKeys($captions, $this->groupByFields); |
|
360
|
|
|
} |
|
361
|
|
|
if (isset($this->captionCallback) && \is_callable($this->captionCallback)) { |
|
362
|
|
|
$callback = $this->captionCallback; |
|
363
|
|
|
$callback($captions, $this->instance); |
|
364
|
|
|
} |
|
365
|
|
|
return $captions; |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
public function setCaption($index, $caption) { |
|
369
|
|
|
if (isset($this->captions) === false) |
|
370
|
|
|
$this->captions = []; |
|
371
|
|
|
$this->captions[$index] = $caption; |
|
372
|
|
|
return $this; |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
public function setCaptions($captions) { |
|
376
|
|
|
$this->captions = $captions; |
|
377
|
|
|
return $this; |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
/** |
|
381
|
|
|
* Associates a $callback function after the compilation of the field at $index position |
|
382
|
|
|
* The $callback function can take the following arguments : $field=>the compiled field, $instance : the active instance of the object, $index: the field position |
|
383
|
|
|
* |
|
384
|
|
|
* @param int $index |
|
385
|
|
|
* postion of the compiled field |
|
386
|
|
|
* @param callable $callback |
|
387
|
|
|
* function called after the field compilation |
|
388
|
|
|
* @return InstanceViewer |
|
389
|
|
|
*/ |
|
390
|
|
|
public function afterCompile($index, $callback) { |
|
391
|
|
|
$this->afterCompile[$index] = $callback; |
|
392
|
|
|
return $this; |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
/** |
|
396
|
|
|
* Defines a callback function to call for modifying captions |
|
397
|
|
|
* function parameters are $captions: the captions to modify and $instance: the active model instance |
|
398
|
|
|
* |
|
399
|
|
|
* @param callable $captionCallback |
|
400
|
|
|
* @return \Ajax\semantic\widgets\base\InstanceViewer |
|
401
|
|
|
*/ |
|
402
|
|
|
public function setCaptionCallback($captionCallback) { |
|
403
|
|
|
$this->captionCallback = $captionCallback; |
|
404
|
|
|
return $this; |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* Defines the default function which displays fields value |
|
409
|
|
|
* |
|
410
|
|
|
* @param callable $defaultValueFunction |
|
411
|
|
|
* function parameters are : $name : the field name, $value : the field value ,$index : the field index, $instance : the active instance of model |
|
412
|
|
|
* @return \Ajax\semantic\widgets\base\InstanceViewer |
|
413
|
|
|
*/ |
|
414
|
|
|
public function setDefaultValueFunction($defaultValueFunction) { |
|
415
|
|
|
$this->defaultValueFunction = $defaultValueFunction; |
|
416
|
|
|
return $this; |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
public function getVisibleProperties() { |
|
420
|
|
|
return $this->visibleProperties; |
|
421
|
|
|
} |
|
422
|
|
|
|
|
423
|
|
|
public function getSimpleProperties() { |
|
424
|
|
|
return array_filter($this->visibleProperties, function ($item) { |
|
425
|
|
|
return ! (is_array($item) || is_object($item)); |
|
426
|
|
|
}); |
|
427
|
|
|
} |
|
428
|
|
|
|
|
429
|
|
|
/** |
|
430
|
|
|
* |
|
431
|
|
|
* @return callable |
|
432
|
|
|
*/ |
|
433
|
|
|
public function getDefaultValueFunction() { |
|
434
|
|
|
return $this->defaultValueFunction; |
|
435
|
|
|
} |
|
436
|
|
|
|
|
437
|
|
|
/** |
|
438
|
|
|
* |
|
439
|
|
|
* @return mixed |
|
440
|
|
|
*/ |
|
441
|
|
|
public function getGroupByFields() { |
|
442
|
|
|
return $this->groupByFields; |
|
443
|
|
|
} |
|
444
|
|
|
|
|
445
|
|
|
/** |
|
446
|
|
|
* |
|
447
|
|
|
* @param mixed $groupByFields |
|
448
|
|
|
*/ |
|
449
|
|
|
public function setGroupByFields($groupByFields) { |
|
450
|
|
|
$this->groupByFields = $groupByFields; |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
public function addGroupBy($groupByField){ |
|
454
|
|
|
$this->groupByFields[]=$groupByField; |
|
455
|
|
|
} |
|
456
|
|
|
|
|
457
|
|
|
public function getGroupByFieldsCount() { |
|
458
|
|
|
if (is_array($this->groupByFields)) { |
|
459
|
|
|
return sizeof($this->groupByFields); |
|
460
|
|
|
} |
|
461
|
|
|
return 0; |
|
462
|
|
|
} |
|
463
|
|
|
} |
|
464
|
|
|
|