|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class Ajde_Crud extends Ajde_Object_Standard |
|
4
|
|
|
{ |
|
5
|
|
|
protected $_model = null; |
|
6
|
|
|
protected $_collection = null; |
|
7
|
|
|
|
|
8
|
|
|
protected $_fields = null; |
|
9
|
|
|
|
|
10
|
|
|
//protected $_operation = null; |
|
11
|
|
|
protected $_operation = 'list'; |
|
12
|
|
|
|
|
13
|
|
|
protected $_templateData = []; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct($model, $options = []) |
|
16
|
|
|
{ |
|
17
|
|
|
if ($model instanceof Ajde_Model) { |
|
18
|
|
|
$this->_model = $model; |
|
19
|
|
|
} else { |
|
20
|
|
|
$modelName = $this->toCamelCase($model, true).'Model'; |
|
21
|
|
|
$this->_model = new $modelName(); |
|
22
|
|
|
} |
|
23
|
|
|
if ($options instanceof Ajde_Crud_Options) { |
|
24
|
|
|
$options = $options->getArray(); |
|
25
|
|
|
} |
|
26
|
|
|
$this->setOptions($options); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function __toString() |
|
30
|
|
|
{ |
|
31
|
|
|
try { |
|
32
|
|
|
$output = $this->output(); |
|
33
|
|
|
} catch (Exception $e) { |
|
34
|
|
|
$output = Ajde_Exception_Handler::handler($e); |
|
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return (string) $output; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @throws Ajde_Exception |
|
42
|
|
|
* |
|
43
|
|
|
* @return mixed |
|
44
|
|
|
*/ |
|
45
|
|
|
public function output() |
|
46
|
|
|
{ |
|
47
|
|
|
$controller = Ajde_Controller::fromRoute(new Ajde_Core_Route('_core/crud:'.$this->getOperation())); |
|
48
|
|
|
$controller->setCrudInstance($this); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
return $controller->invoke(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function export($format = 'excel') |
|
54
|
|
|
{ |
|
55
|
|
|
/* @var $exporter Ajde_Crud_Export_Interface */ |
|
56
|
|
|
|
|
57
|
|
|
$exporterClass = 'Ajde_Crud_Export_'.ucfirst($format); |
|
58
|
|
|
$exporter = new $exporterClass(); |
|
59
|
|
|
|
|
60
|
|
|
$table = []; |
|
61
|
|
|
|
|
62
|
|
|
$fieldsToShow = $this->getFieldNames(); |
|
63
|
|
|
|
|
64
|
|
|
$headers = []; |
|
65
|
|
|
|
|
66
|
|
|
$this->getCollection()->getView()->setPageSize(9999999999); |
|
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
$items = $this->getItems(); |
|
69
|
|
|
|
|
70
|
|
|
foreach ($fieldsToShow as $fieldName) { |
|
71
|
|
|
|
|
72
|
|
|
// JSON |
|
73
|
|
|
if ($items->count() && ($first = current($items->items())) && $first->isFieldJson($fieldName)) { |
|
74
|
|
|
$maxJsonFields = 0; |
|
75
|
|
|
$useJsonFields = []; |
|
76
|
|
|
foreach ($items as $model) { |
|
77
|
|
|
$jsonFields = (array) @json_decode($model->has($fieldName) ? $model->get($fieldName) : ''); |
|
78
|
|
|
if (count($jsonFields) > $maxJsonFields) { |
|
79
|
|
|
$useJsonFields = array_keys($jsonFields); |
|
80
|
|
|
$maxJsonFields = count($jsonFields); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
foreach ($useJsonFields as $key) { |
|
84
|
|
|
$headers[] = $key; |
|
85
|
|
|
} |
|
86
|
|
|
// Normal |
|
87
|
|
|
} else { |
|
88
|
|
|
$field = $this->getField($fieldName); |
|
89
|
|
|
$headers[] = $field->getLabel(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$table[] = $headers; |
|
94
|
|
|
|
|
95
|
|
|
foreach ($items as $model) { |
|
96
|
|
|
/* @var $model Ajde_Model */ |
|
97
|
|
|
$this->fireCrudLoadedOnModel($model); |
|
98
|
|
|
|
|
99
|
|
|
$row = []; |
|
100
|
|
|
|
|
101
|
|
|
foreach ($fieldsToShow as $fieldName) { |
|
102
|
|
|
$field = $this->getField($fieldName); |
|
103
|
|
|
$value = $model->has($fieldName) ? $model->get($fieldName) : false; |
|
104
|
|
|
|
|
105
|
|
|
// JSON |
|
106
|
|
|
if ($model->isFieldJson($fieldName)) { |
|
107
|
|
|
foreach (($jsonFields = (array) @json_decode($value)) as $item) { |
|
108
|
|
|
$row[] = $item; |
|
109
|
|
|
} |
|
110
|
|
|
for ($i = 0; $i < ($maxJsonFields - count($jsonFields)); $i++) { |
|
|
|
|
|
|
111
|
|
|
$row[] = ''; |
|
112
|
|
|
} |
|
113
|
|
|
} else { |
|
114
|
|
|
if ($this->getField($fieldName) instanceof Ajde_Crud_Field_Sort) { |
|
115
|
|
|
$row[] = $value; |
|
116
|
|
|
// Display function |
|
117
|
|
|
// } elseif ($field->hasFunction() && $field->getFunction()) { |
|
118
|
|
|
// $displayFunction = $field->getFunction(); |
|
119
|
|
|
// $displayFunctionArgs = $field->hasFunctionArgs() ? $field->getFunctionArgs() : array(); |
|
120
|
|
|
// $funcValue = call_user_func_array(array($model, $displayFunction), $displayFunctionArgs); |
|
121
|
|
|
// $row[] = $funcValue; |
|
122
|
|
|
// Linked Model (not loaded) |
|
123
|
|
|
} elseif ($value instanceof Ajde_Model && !$value->hasLoaded()) { |
|
124
|
|
|
$row[] = '(not set)'; |
|
125
|
|
|
// Linked Model |
|
126
|
|
|
} elseif ($value instanceof Ajde_Model && $value->hasLoaded()) { |
|
127
|
|
|
$row[] = $value->get($value->getDisplayField()); |
|
128
|
|
|
// Boolean |
|
129
|
|
|
} elseif ($field instanceof Ajde_Crud_Field_Boolean) { |
|
130
|
|
|
$row[] = $value; |
|
131
|
|
|
// File |
|
132
|
|
|
} elseif ($this->getField($fieldName) instanceof Ajde_Crud_Field_File) { |
|
133
|
|
|
$row[] = config('app.rootUrl').$field->getSaveDir().$value; |
|
|
|
|
|
|
134
|
|
|
// Text value |
|
135
|
|
|
} else { |
|
136
|
|
|
$row[] = strip_tags($value); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$table[] = $row; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
$exporter->prepare($this->getSessionName(), $table); |
|
145
|
|
|
|
|
146
|
|
|
return $exporter; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* GETTERS & SETTERS. |
|
151
|
|
|
*/ |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getAction() |
|
157
|
|
|
{ |
|
158
|
|
|
if (!$this->hasAction()) { |
|
|
|
|
|
|
159
|
|
|
$this->setAction('list'); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
return parent::getAction(); |
|
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function setAction($value) |
|
166
|
|
|
{ |
|
167
|
|
|
if (substr($value, 0, 4) === 'edit' || substr($value, 0, 4) === 'list') { |
|
168
|
|
|
$this->setOperation(substr($value, 0, 4)); |
|
169
|
|
|
} |
|
170
|
|
|
parent::setAction($value); |
|
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
public function setOperation($operation) |
|
174
|
|
|
{ |
|
175
|
|
|
$this->_operation = $operation; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
public function getOperation() |
|
179
|
|
|
{ |
|
180
|
|
|
// if (!isset($this->_operation)) { |
|
181
|
|
|
// if (Ajde::app()->getRequest()->has('new')) { |
|
182
|
|
|
// $this->setOperation('new'); |
|
183
|
|
|
// } else if (Ajde::app()->getRequest()->has('edit')) { |
|
184
|
|
|
// $this->setOperation('edit'); |
|
185
|
|
|
// } else { |
|
186
|
|
|
// $this->setOperation('list'); |
|
187
|
|
|
// } |
|
188
|
|
|
// } |
|
189
|
|
|
return $this->_operation; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* OPTIONS. |
|
194
|
|
|
* |
|
195
|
|
|
* @param $name |
|
196
|
|
|
* @param bool|mixed $default |
|
197
|
|
|
* |
|
198
|
|
|
* @return array|bool |
|
199
|
|
|
*/ |
|
200
|
|
|
public function getOption($name, $default = false) |
|
201
|
|
|
{ |
|
202
|
|
|
$path = explode('.', $name); |
|
203
|
|
|
$options = $this->getOptions(); |
|
204
|
|
|
foreach ($path as $key) { |
|
205
|
|
|
if (isset($options[$key])) { |
|
206
|
|
|
$options = $options[$key]; |
|
207
|
|
|
} else { |
|
208
|
|
|
return $default; |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
return $options; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
public function setOption($name, $value) |
|
216
|
|
|
{ |
|
217
|
|
|
$path = explode('.', $name); |
|
218
|
|
|
$options = $this->getOptions(); |
|
219
|
|
|
$wc = &$options; |
|
220
|
|
|
foreach ($path as $key) { |
|
221
|
|
|
if (!isset($wc[$key])) { |
|
222
|
|
|
$wc[$key] = []; |
|
223
|
|
|
} |
|
224
|
|
|
$wc = &$wc[$key]; |
|
225
|
|
|
} |
|
226
|
|
|
$wc = $value; |
|
227
|
|
|
$this->setOptions($options); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @return array |
|
232
|
|
|
*/ |
|
233
|
|
|
public function getOptions($key = null) |
|
234
|
|
|
{ |
|
235
|
|
|
if (isset($key)) { |
|
236
|
|
|
$options = parent::getOptions(); |
|
|
|
|
|
|
237
|
|
|
|
|
238
|
|
|
return issetor($options[$key], []); |
|
239
|
|
|
} else { |
|
240
|
|
|
return parent::getOptions(); |
|
|
|
|
|
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
public function setOptions($value) |
|
245
|
|
|
{ |
|
246
|
|
|
parent::setOptions($value); |
|
|
|
|
|
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* MISC. |
|
251
|
|
|
*/ |
|
252
|
|
|
public function setItem($value) |
|
253
|
|
|
{ |
|
254
|
|
|
parent::setItem($value); |
|
|
|
|
|
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
public function setCustomTemplateModule($value) |
|
258
|
|
|
{ |
|
259
|
|
|
parent::setCustomTemplateModule($value); |
|
|
|
|
|
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
public function getCustomTemplateModule() |
|
263
|
|
|
{ |
|
264
|
|
|
if (parent::hasCustomTemplateModule()) { |
|
|
|
|
|
|
265
|
|
|
return parent::getCustomTemplateModule(); |
|
|
|
|
|
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
return (string) $this->getModel()->getTable(); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
public function setEditAction($value) |
|
272
|
|
|
{ |
|
273
|
|
|
parent::setEditAction($value); |
|
|
|
|
|
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
public function getEditAction() |
|
277
|
|
|
{ |
|
278
|
|
|
if (parent::hasEditAction()) { |
|
|
|
|
|
|
279
|
|
|
return parent::getEditAction(); |
|
|
|
|
|
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
return false; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
public function setNewAction($value) |
|
286
|
|
|
{ |
|
287
|
|
|
parent::setNewAction($value); |
|
|
|
|
|
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
public function getNewAction() |
|
291
|
|
|
{ |
|
292
|
|
|
if (parent::hasNewAction()) { |
|
|
|
|
|
|
293
|
|
|
return parent::getNewAction(); |
|
|
|
|
|
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
return false; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
public function setListAction($value) |
|
300
|
|
|
{ |
|
301
|
|
|
parent::setListAction($value); |
|
|
|
|
|
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
public function getListAction() |
|
305
|
|
|
{ |
|
306
|
|
|
if (parent::hasListAction()) { |
|
|
|
|
|
|
307
|
|
|
return parent::getListAction(); |
|
|
|
|
|
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
return false; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* @return Ajde_Collection |
|
315
|
|
|
*/ |
|
316
|
|
|
public function getCollection() |
|
317
|
|
|
{ |
|
318
|
|
|
if (!isset($this->_collection)) { |
|
319
|
|
|
$collectionName = str_replace('Model', '', get_class($this->getModel())).'Collection'; |
|
320
|
|
|
$this->_collection = new $collectionName(); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
return $this->_collection; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* @return Ajde_Model |
|
328
|
|
|
*/ |
|
329
|
|
|
public function getModel() |
|
330
|
|
|
{ |
|
331
|
|
|
return $this->_model; |
|
332
|
|
|
} |
|
333
|
|
|
|
|
334
|
|
|
/** |
|
335
|
|
|
* @return string |
|
336
|
|
|
*/ |
|
337
|
|
|
public function getHash() |
|
338
|
|
|
{ |
|
339
|
|
|
return spl_object_hash($this); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* HELPERS. |
|
344
|
|
|
*/ |
|
345
|
|
|
public function loadItem($id = null) |
|
346
|
|
|
{ |
|
347
|
|
|
$model = $this->getModel(); |
|
348
|
|
|
if (isset($id)) { |
|
349
|
|
|
$model->loadByPK($id); |
|
350
|
|
|
} |
|
351
|
|
|
$this->setItem($model); |
|
352
|
|
|
|
|
353
|
|
|
return $this->getItem(); |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* @return Ajde_Model |
|
358
|
|
|
*/ |
|
359
|
|
|
public function getItem() |
|
360
|
|
|
{ |
|
361
|
|
|
if ($this->isNew()) { |
|
362
|
|
|
$this->fireCrudLoadedOnModel($this->getModel()); |
|
363
|
|
|
|
|
364
|
|
|
return $this->getModel(); |
|
365
|
|
|
} |
|
366
|
|
|
if (!$this->getModel()->getPK()) { |
|
367
|
|
|
$model = $this->getModel(); |
|
368
|
|
|
if (!$model->loadByPK($this->getId())) { |
|
|
|
|
|
|
369
|
|
|
Ajde_Http_Response::redirectNotFound(); |
|
370
|
|
|
} else { |
|
371
|
|
|
if (!$model->getAutoloadParents()) { |
|
372
|
|
|
$model->loadParents(); |
|
373
|
|
|
} |
|
374
|
|
|
} |
|
375
|
|
|
$this->fireCrudLoadedOnModel($this->getModel()); |
|
376
|
|
|
} |
|
377
|
|
|
|
|
378
|
|
|
return $this->getModel(); |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
public function isNew() |
|
382
|
|
|
{ |
|
383
|
|
|
return !$this->hasId() || $this->getId() === false || is_null($this->getId()); |
|
|
|
|
|
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
/** |
|
387
|
|
|
* @return Ajde_Collection |
|
388
|
|
|
*/ |
|
389
|
|
|
public function getItems() |
|
390
|
|
|
{ |
|
391
|
|
|
$collection = $this->getCollection(); |
|
392
|
|
|
|
|
393
|
|
|
// Collection view |
|
394
|
|
|
if ($collection->hasView()) { |
|
395
|
|
|
$collection->applyView(); |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
$collection->load(); |
|
399
|
|
|
|
|
400
|
|
|
// TODO: this should be done with JOIN |
|
401
|
|
|
if ($this->getModel()->getAutoloadParents()) { |
|
402
|
|
|
$collection->loadParents(); |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
return $collection; |
|
406
|
|
|
} |
|
407
|
|
|
|
|
408
|
|
|
public function fireCrudLoadedOnModel($model) |
|
409
|
|
|
{ |
|
410
|
|
|
Ajde_Event::trigger($model, 'afterCrudLoaded'); |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
|
|
public function getFields() |
|
414
|
|
|
{ |
|
415
|
|
|
if (!isset($this->_fields)) { |
|
416
|
|
|
$this->loadFields(); |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
return $this->_fields; |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
public function setReadOnlyForAllFields() |
|
423
|
|
|
{ |
|
424
|
|
|
foreach ($this->getModel()->getTable()->getFieldNames() as $fieldName) { |
|
425
|
|
|
$this->setOption('fields.'.$fieldName.'.readonly', true); |
|
426
|
|
|
} |
|
427
|
|
|
} |
|
428
|
|
|
|
|
429
|
|
|
public function loadFields() |
|
430
|
|
|
{ |
|
431
|
|
|
$fields = []; |
|
432
|
|
|
$allFields = $this->getDeclaredFieldNames(); |
|
433
|
|
|
|
|
434
|
|
|
$fieldsArray = $this->getModel()->getTable()->getFieldProperties(); |
|
435
|
|
|
// TODO: changed getItem to getModel, any side effects? |
|
436
|
|
|
$parents = $this->getModel()->getTable()->getParents(); |
|
437
|
|
|
|
|
438
|
|
|
foreach ($allFields as $fieldName) { |
|
439
|
|
|
$fieldProperties = []; |
|
440
|
|
|
if (isset($fieldsArray[$fieldName])) { |
|
441
|
|
|
$fieldProperties = $fieldsArray[$fieldName]; |
|
442
|
|
|
} |
|
443
|
|
|
$fieldOptions = $this->getFieldOptions($fieldName, $fieldProperties); |
|
444
|
|
|
$fieldOptions['name'] = $fieldName; |
|
445
|
|
|
if (in_array($fieldOptions['name'], $parents)) { |
|
446
|
|
|
$fieldOptions['type'] = 'fk'; |
|
447
|
|
|
} |
|
448
|
|
|
$field = $this->createField($fieldOptions); |
|
449
|
|
|
$fields[$fieldName] = $field; |
|
450
|
|
|
} |
|
451
|
|
|
|
|
452
|
|
|
return $this->_fields = $fields; |
|
453
|
|
|
} |
|
454
|
|
|
|
|
455
|
|
|
public function createField($fieldOptions) |
|
456
|
|
|
{ |
|
457
|
|
|
if (!isset($fieldOptions['type'])) { |
|
458
|
|
|
$fieldOptions['type'] = 'text'; |
|
459
|
|
|
} |
|
460
|
|
|
$fieldClass = Ajde_Core_ExternalLibs::getClassname('Ajde_Crud_Field_'.ucfirst($fieldOptions['type'])); |
|
461
|
|
|
$field = new $fieldClass($this, $fieldOptions); |
|
462
|
|
|
if ($this->getOperation() === 'edit') { |
|
463
|
|
|
if (!$field->hasValue() || $field->hasEmpty('value')) { |
|
464
|
|
|
if ($this->isNew() && $field->hasNotEmpty('default')) { |
|
465
|
|
|
$field->setValue($field->getDefault()); |
|
466
|
|
|
} elseif (!$this->isNew() && $this->getItem()->has($field->getName())) { |
|
467
|
|
|
$field->setValue($this->getItem()->get($field->getName())); |
|
468
|
|
|
} else { |
|
469
|
|
|
$field->setValue(false); |
|
470
|
|
|
} |
|
471
|
|
|
} |
|
472
|
|
|
} |
|
473
|
|
|
|
|
474
|
|
|
return $field; |
|
475
|
|
|
} |
|
476
|
|
|
|
|
477
|
|
|
/** |
|
478
|
|
|
* @param string $fieldName |
|
479
|
|
|
* |
|
480
|
|
|
* @throws Ajde_Exception |
|
481
|
|
|
* |
|
482
|
|
|
* @return Ajde_Crud_Field |
|
483
|
|
|
*/ |
|
484
|
|
|
public function getField($fieldName, $strict = true) |
|
485
|
|
|
{ |
|
486
|
|
|
if (!isset($this->_fields)) { |
|
487
|
|
|
$this->getFields(); |
|
488
|
|
|
} |
|
489
|
|
|
if (isset($this->_fields[$fieldName])) { |
|
490
|
|
|
return $this->_fields[$fieldName]; |
|
491
|
|
|
} else { |
|
492
|
|
|
if ($strict === true) { |
|
493
|
|
|
// TODO: |
|
494
|
|
|
throw new Ajde_Exception($fieldName.' is not a field in '.(string) $this->getModel()->getTable()); |
|
495
|
|
|
} else { |
|
496
|
|
|
return false; |
|
|
|
|
|
|
497
|
|
|
} |
|
498
|
|
|
} |
|
499
|
|
|
} |
|
500
|
|
|
|
|
501
|
|
|
public function getDeclaredFieldNames() |
|
502
|
|
|
{ |
|
503
|
|
|
$fields = $this->getFieldNames(); |
|
504
|
|
|
foreach ($this->getFieldNamesFromOptions() as $optionField) { |
|
505
|
|
|
if (!in_array($optionField, $fields)) { |
|
506
|
|
|
$fields[] = $optionField; |
|
507
|
|
|
} |
|
508
|
|
|
} |
|
509
|
|
|
|
|
510
|
|
|
return $fields; |
|
511
|
|
|
} |
|
512
|
|
|
|
|
513
|
|
|
public function getFieldNamesFromOptions() |
|
514
|
|
|
{ |
|
515
|
|
|
return array_keys($this->getOptions('fields')); |
|
516
|
|
|
} |
|
517
|
|
|
|
|
518
|
|
|
public function getFieldOptions($fieldName, $fieldProperties = []) |
|
519
|
|
|
{ |
|
520
|
|
|
$fieldsOptions = $this->getOptions('fields'); |
|
521
|
|
|
$fieldOptions = issetor($fieldsOptions[$fieldName], []); |
|
|
|
|
|
|
522
|
|
|
|
|
523
|
|
|
return array_merge($fieldProperties, $fieldOptions); |
|
524
|
|
|
} |
|
525
|
|
|
|
|
526
|
|
|
public function getFieldNames() |
|
527
|
|
|
{ |
|
528
|
|
|
$model = $this->getModel(); |
|
529
|
|
|
|
|
530
|
|
|
return $model->getTable()->getFieldNames(); |
|
531
|
|
|
} |
|
532
|
|
|
|
|
533
|
|
|
public function getFieldLabels() |
|
534
|
|
|
{ |
|
535
|
|
|
$model = $this->getModel(); |
|
536
|
|
|
|
|
537
|
|
|
return $model->getTable()->getFieldLabels(); |
|
538
|
|
|
} |
|
539
|
|
|
|
|
540
|
|
|
public function setSessionName($name) |
|
541
|
|
|
{ |
|
542
|
|
|
parent::setSessionName($name); |
|
|
|
|
|
|
543
|
|
|
} |
|
544
|
|
|
|
|
545
|
|
|
public function getSessionName() |
|
546
|
|
|
{ |
|
547
|
|
|
if (parent::hasSessionName()) { |
|
|
|
|
|
|
548
|
|
|
return parent::getSessionName(); |
|
|
|
|
|
|
549
|
|
|
} else { |
|
550
|
|
|
return (string) $this->getModel()->getTable(); |
|
551
|
|
|
} |
|
552
|
|
|
} |
|
553
|
|
|
|
|
554
|
|
|
/** |
|
555
|
|
|
* @param array $viewParams |
|
556
|
|
|
* @param bool|string $persist |
|
557
|
|
|
* |
|
558
|
|
|
* @return Ajde_Collection_View |
|
559
|
|
|
*/ |
|
560
|
|
|
public function getCollectionView($viewParams = [], $persist = 'auto') |
|
561
|
|
|
{ |
|
562
|
|
|
if (!$this->getCollection()->hasView()) { |
|
563
|
|
|
$viewSession = new Ajde_Session('AC.Crud.View'); |
|
564
|
|
|
$sessionName = $this->getSessionName(); |
|
565
|
|
|
|
|
566
|
|
|
if ($viewSession->has($sessionName)) { |
|
567
|
|
|
$crudView = $viewSession->get($sessionName); |
|
568
|
|
|
} else { |
|
569
|
|
|
$crudView = new Ajde_Collection_View($sessionName, $this->getOption('list.view', [])); |
|
|
|
|
|
|
570
|
|
|
$crudView->setColumns($this->getOption('list.show', $this->getFieldNames())); |
|
571
|
|
|
} |
|
572
|
|
|
|
|
573
|
|
|
// somehow, when altering crudView, the instance in the session gets updated as well, and we don't want that |
|
574
|
|
|
$crudView = clone $crudView; |
|
575
|
|
|
|
|
576
|
|
|
if (empty($viewParams)) { |
|
577
|
|
|
$viewParams = Ajde::app()->getRequest()->getParam('view', []); |
|
578
|
|
|
// if we have params, but no columns, assume a reset |
|
579
|
|
|
if (!empty($viewParams) && !isset($viewParams['columns'])) { |
|
580
|
|
|
$viewParams['columns'] = $this->getOption('list.show', $this->getFieldNames()); |
|
581
|
|
|
} |
|
582
|
|
|
} |
|
583
|
|
|
$crudView->setOptions($viewParams); |
|
584
|
|
|
|
|
585
|
|
|
if (($persist == 'auto' && $this->getOperation() == 'list') || $persist === true) { |
|
586
|
|
|
$viewSession->set($sessionName, $crudView); |
|
587
|
|
|
} |
|
588
|
|
|
|
|
589
|
|
|
$this->getCollection()->setView($crudView); |
|
590
|
|
|
} |
|
591
|
|
|
|
|
592
|
|
|
return $this->getCollection()->getView(); |
|
593
|
|
|
} |
|
594
|
|
|
|
|
595
|
|
|
/** |
|
596
|
|
|
* RENDERING. |
|
597
|
|
|
*/ |
|
598
|
|
|
public function getTemplate() |
|
599
|
|
|
{ |
|
600
|
|
|
$template = new Ajde_Template(MODULE_DIR.'_core/', 'crud/'.$this->getOperation()); |
|
601
|
|
|
Ajde::app()->getDocument()->autoAddResources($template); |
|
|
|
|
|
|
602
|
|
|
if ($this->getOperation() !== $this->getAction()) { |
|
603
|
|
|
$template = new Ajde_Template(MODULE_DIR.'_core/', 'crud/'.$this->getAction()); |
|
604
|
|
|
} |
|
605
|
|
View Code Duplication |
if ($this->_hasCustomTemplate()) { |
|
|
|
|
|
|
606
|
|
|
$base = $this->_getCustomTemplateBase(); |
|
607
|
|
|
$action = $this->_getCustomTemplateAction(); |
|
608
|
|
|
$template = new Ajde_Template($base, $action); |
|
609
|
|
|
} |
|
610
|
|
|
$template->assignArray($this->_templateData); |
|
611
|
|
|
|
|
612
|
|
|
return $template; |
|
613
|
|
|
} |
|
614
|
|
|
|
|
615
|
|
|
public function setTemplateData($array) |
|
616
|
|
|
{ |
|
617
|
|
|
$this->_templateData = (array) $array; |
|
618
|
|
|
} |
|
619
|
|
|
|
|
620
|
|
View Code Duplication |
private function _hasCustomTemplate() |
|
|
|
|
|
|
621
|
|
|
{ |
|
622
|
|
|
$base = $this->_getCustomTemplateBase(); |
|
623
|
|
|
$action = $this->_getCustomTemplateAction(); |
|
624
|
|
|
|
|
625
|
|
|
return Ajde_Template::exist($base, $action) !== false; |
|
626
|
|
|
} |
|
627
|
|
|
|
|
628
|
|
|
private function _getCustomTemplateBase() |
|
629
|
|
|
{ |
|
630
|
|
|
return MODULE_DIR.$this->getCustomTemplateModule().DIRECTORY_SEPARATOR; |
|
631
|
|
|
} |
|
632
|
|
|
|
|
633
|
|
|
private function _getCustomTemplateAction() |
|
634
|
|
|
{ |
|
635
|
|
|
return 'crud/'.(string) $this->getModel()->getTable().DIRECTORY_SEPARATOR.$this->getAction(); |
|
636
|
|
|
} |
|
637
|
|
|
} |
|
638
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.