1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FFCMS\Controllers\Admin; |
4
|
|
|
|
5
|
|
|
use FFMVC\Helpers; |
6
|
|
|
use FFCMS\{Traits, Controllers, Models, Mappers}; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Admin Assets CMS Controller Class. |
10
|
|
|
* |
11
|
|
|
* @author Vijay Mahrra <[email protected]> |
12
|
|
|
* @copyright 2016 Vijay Mahrra |
13
|
|
|
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html) |
14
|
|
|
*/ |
15
|
|
View Code Duplication |
class Assets extends Admin |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* For admin listing and search results |
19
|
|
|
*/ |
20
|
|
|
use Traits\SearchController; |
21
|
|
|
|
22
|
|
|
protected $template_path = 'cms/admin/assets/'; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* |
27
|
|
|
* |
28
|
|
|
* @param \Base $f3 |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function listing(\Base $f3) |
32
|
|
|
{ |
33
|
|
|
$view = strtolower(trim(strip_tags($f3->get('REQUEST.view')))); |
34
|
|
|
$view = empty($view) ? 'list.phtml' : $view . '.phtml'; |
35
|
|
|
$f3->set('REQUEST.view', $view); |
36
|
|
|
|
37
|
|
|
$f3->set('results', $this->getListingResults($f3, new Mappers\Assets)); |
38
|
|
|
|
39
|
|
|
$f3->set('breadcrumbs', [ |
40
|
|
|
_('Admin') => 'admin', |
41
|
|
|
_('Assets') => 'admin_assets_list', |
42
|
|
|
]); |
43
|
|
|
|
44
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
45
|
|
|
echo \View::instance()->render($this->template_path . $view); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* |
51
|
|
|
* |
52
|
|
|
* @param \Base $f3 |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
public function search(\Base $f3) |
56
|
|
|
{ |
57
|
|
|
$view = strtolower(trim(strip_tags($f3->get('REQUEST.view')))); |
58
|
|
|
$view = empty($view) ? 'list.phtml' : $view . '.phtml'; |
59
|
|
|
$f3->set('REQUEST.view', $view); |
60
|
|
|
|
61
|
|
|
$f3->set('results', $this->getSearchResults($f3, new Mappers\Assets)); |
62
|
|
|
|
63
|
|
|
$f3->set('breadcrumbs', [ |
64
|
|
|
_('Admin') => 'admin', |
65
|
|
|
_('Assets') => 'admin_assets_list', |
66
|
|
|
_('Search') => '', |
67
|
|
|
]); |
68
|
|
|
|
69
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
70
|
|
|
echo \View::instance()->render($this->template_path . $view); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* |
76
|
|
|
* |
77
|
|
|
* @param \Base $f3 |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function edit(\Base $f3) |
81
|
|
|
{ |
82
|
|
|
$this->redirectLoggedOutUser(); |
83
|
|
|
$this->csrf(); |
84
|
|
|
|
85
|
|
|
if (false == $f3->get('isRoot')) { |
86
|
|
|
$this->notify(_('You do not have (root) permission!'), 'error'); |
87
|
|
|
return $f3->reroute('@admin'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$uuid = $f3->get('REQUEST.uuid'); |
91
|
|
|
|
92
|
|
|
$mapper = new Mappers\Assets; |
93
|
|
|
$mapper->load(['uuid = ?', $uuid]); |
94
|
|
|
|
95
|
|
|
if (null == $mapper->id) { |
96
|
|
|
$this->notify(_('The entry no longer exists!'), 'error'); |
97
|
|
|
return $f3->reroute('@admin_assets_lists'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$f3->set('breadcrumbs', [ |
101
|
|
|
_('Admin') => 'admin', |
102
|
|
|
_('Users') => $this->url('@admin_assets_search', [ |
103
|
|
|
'search' => $mapper->users_uuid, |
104
|
|
|
'search_fields' => 'uuid', |
105
|
|
|
'type' => 'exact', |
106
|
|
|
]), |
107
|
|
|
_('Assets') => $this->url('@admin_assets_search', [ |
108
|
|
|
'search' => $mapper->users_uuid, |
109
|
|
|
'search_fields' => 'users_uuid', |
110
|
|
|
'order' => 'key', |
111
|
|
|
'type' => 'exact', |
112
|
|
|
]), |
113
|
|
|
_('Edit') => '', |
114
|
|
|
]); |
115
|
|
|
|
116
|
|
|
$f3->set('form', $mapper->cast()); |
117
|
|
|
echo \View::instance()->render($this->template_path . 'edit.phtml'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* |
123
|
|
|
* |
124
|
|
|
* @param \Base $f3 |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
public function editPost(\Base $f3) |
128
|
|
|
{ |
129
|
|
|
$this->csrf('@admin_assets_list'); |
130
|
|
|
$this->redirectLoggedOutUser(); |
131
|
|
|
|
132
|
|
|
if (false == $f3->get('isRoot')) { |
133
|
|
|
$this->notify(_('You do not have (root) permission!'), 'error'); |
134
|
|
|
return $f3->reroute('@admin'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$view = $this->template_path . 'edit.phtml'; |
138
|
|
|
|
139
|
|
|
// get current user details |
140
|
|
|
$uuid = $f3->get('REQUEST.uuid'); |
141
|
|
|
|
142
|
|
|
$mapper = new Mappers\Assets; |
143
|
|
|
$mapper->load(['uuid = ?', $uuid]); |
144
|
|
|
|
145
|
|
|
if (null == $mapper->id) { |
146
|
|
|
$this->notify(_('The entry no longer exists!'), 'error'); |
147
|
|
|
return $f3->reroute('@admin_assets_list'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$f3->set('breadcrumbs', [ |
151
|
|
|
_('Admin') => 'admin', |
152
|
|
|
_('Users') => $this->url('@admin_assets_search', [ |
153
|
|
|
'search' => $mapper->users_uuid, |
154
|
|
|
'search_fields' => 'uuid', |
155
|
|
|
'type' => 'exact', |
156
|
|
|
]), |
157
|
|
|
_('Assets') => $this->url('@admin_assets_search', [ |
158
|
|
|
'search' => $mapper->users_uuid, |
159
|
|
|
'search_fields' => 'users_uuid', |
160
|
|
|
'order' => 'key', |
161
|
|
|
'type' => 'exact', |
162
|
|
|
]), |
163
|
|
|
_('Edit') => '', |
164
|
|
|
]); |
165
|
|
|
|
166
|
|
|
// only allow updating of these fields |
167
|
|
|
$data = $f3->get('REQUEST'); |
168
|
|
|
$fields = [ |
169
|
|
|
'users_uuid', |
170
|
|
|
'scopes', |
171
|
|
|
'key', |
172
|
|
|
'name', |
173
|
|
|
'description', |
174
|
|
|
'query', |
175
|
|
|
]; |
176
|
|
|
|
177
|
|
|
// check input data has values set for the above fields |
178
|
|
|
foreach ($fields as $k => $field) { |
179
|
|
|
if (!array_key_exists($field, $data)) { |
180
|
|
|
$data[$field] = null; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
// then remove any input data fields that aren't in the above fields |
184
|
|
|
foreach ($data as $field => $v) { |
185
|
|
|
if (!in_array($field, $fields)) { |
186
|
|
|
unset($data[$field]); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
// update required fields to check from ones which changed |
191
|
|
|
// validate the entered data |
192
|
|
|
$data['uuid'] = $f3->get('REQUEST.uuid'); |
193
|
|
|
$data['users_uuid'] = $f3->get('uuid'); |
194
|
|
|
$mapper->copyfrom($data); |
195
|
|
|
$mapper->validationRequired($fields); |
196
|
|
|
$errors = $mapper->validate(false); |
197
|
|
|
if (is_array($errors)) { |
198
|
|
|
$this->notify(['warning' => $mapper->validationErrors($errors)]); |
199
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
200
|
|
|
echo \View::instance()->render($view); |
201
|
|
|
return; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
// no change, do nothing |
205
|
|
|
if (!$mapper->changed()) { |
206
|
|
|
$this->notify(_('There was nothing to change!'), 'info'); |
207
|
|
|
return $f3->reroute('@admin_assets_list'); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// reset usermapper and copy in valid data |
211
|
|
|
$mapper->load(['uuid = ?', $data['uuid']]); |
212
|
|
|
$mapper->copyfrom($data); |
213
|
|
|
if ($mapper->save()) { |
214
|
|
|
$this->notify(_('The asset data was updated!'), 'success'); |
215
|
|
|
} else { |
216
|
|
|
$this->notify(_('Unable to update asset data!'), 'error'); |
217
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
218
|
|
|
echo \View::instance()->render($view); |
219
|
|
|
return; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$f3->reroute('@admin_assets_search' . '?search=' . $mapper->uuid); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* |
228
|
|
|
* |
229
|
|
|
* @param \Base $f3 |
230
|
|
|
* @return void |
231
|
|
|
*/ |
232
|
|
|
public function add(\Base $f3) |
233
|
|
|
{ |
234
|
|
|
$this->redirectLoggedOutUser(); |
235
|
|
|
$this->csrf(); |
236
|
|
|
|
237
|
|
|
if (false == $f3->get('isRoot')) { |
238
|
|
|
$this->notify(_('You do not have (root) permission!'), 'error'); |
239
|
|
|
return $f3->reroute('@admin'); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$uuid = $f3->get('REQUEST.uuid'); |
243
|
|
|
|
244
|
|
|
$mapper = new Mappers\Assets; |
245
|
|
|
|
246
|
|
|
$data = $mapper->cast(); |
247
|
|
|
$data['uuid'] = $uuid; |
248
|
|
|
|
249
|
|
|
$f3->set('breadcrumbs', [ |
250
|
|
|
_('Admin') => 'admin', |
251
|
|
|
_('Assets') => $this->url('@admin_assets_search', [ |
252
|
|
|
'search' => $uuid, |
253
|
|
|
'search_fields' => 'uuid', |
254
|
|
|
'order' => 'key', |
255
|
|
|
'type' => 'exact', |
256
|
|
|
]), |
257
|
|
|
_('Add') => '', |
258
|
|
|
]); |
259
|
|
|
|
260
|
|
|
$f3->set('form', $data); |
261
|
|
|
echo \View::instance()->render($this->template_path . 'add.phtml'); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* |
267
|
|
|
* |
268
|
|
|
* @param \Base $f3 |
269
|
|
|
* @return void |
270
|
|
|
*/ |
271
|
|
|
public function addPost(\Base $f3) |
272
|
|
|
{ |
273
|
|
|
$this->csrf('@admin_assets_list'); |
274
|
|
|
$this->redirectLoggedOutUser(); |
275
|
|
|
|
276
|
|
|
if (false == $f3->get('isRoot')) { |
277
|
|
|
$this->notify(_('You do not have (root) permission!'), 'error'); |
278
|
|
|
return $f3->reroute('@admin'); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
$view = $this->template_path . 'add.phtml'; |
282
|
|
|
|
283
|
|
|
$uuid = $f3->get('REQUEST.uuid'); |
284
|
|
|
|
285
|
|
|
$mapper = new Mappers\Assets; |
286
|
|
|
|
287
|
|
|
$f3->set('breadcrumbs', [ |
288
|
|
|
_('Admin') => 'admin', |
289
|
|
|
_('Users') => $this->url('@admin_assets_search', [ |
290
|
|
|
'search' => $uuid, |
291
|
|
|
'search_fields' => 'uuid', |
292
|
|
|
'type' => 'exact', |
293
|
|
|
]), |
294
|
|
|
_('Assets') => $this->url('@admin_assets_search', [ |
295
|
|
|
'search' => $uuid, |
296
|
|
|
'search_fields' => 'users_uuid', |
297
|
|
|
'order' => 'key', |
298
|
|
|
'type' => 'exact', |
299
|
|
|
]), |
300
|
|
|
_('Add') => '', |
301
|
|
|
]); |
302
|
|
|
|
303
|
|
|
// only allow updating of these fields |
304
|
|
|
$data = $f3->get('REQUEST'); |
305
|
|
|
$fields = [ |
306
|
|
|
'scopes', |
307
|
|
|
'key', |
308
|
|
|
'name', |
309
|
|
|
'description', |
310
|
|
|
'query', |
311
|
|
|
]; |
312
|
|
|
|
313
|
|
|
// check input data has values set for the above fields |
314
|
|
|
foreach ($fields as $k => $field) { |
315
|
|
|
if (!array_key_exists($field, $data) || empty($data[$field])) { |
316
|
|
|
$data[$field] = null; |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
// then remove any input data fields that aren't in the above fields |
320
|
|
|
foreach ($data as $field => $v) { |
321
|
|
|
if (!in_array($field, $fields)) { |
322
|
|
|
unset($data[$field]); |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
// update required fields to check from ones which changed |
327
|
|
|
// validate the entered data |
328
|
|
|
$data['users_uuid'] = $f3->get('uuid'); |
329
|
|
|
$mapper->copyfrom($data); |
330
|
|
|
$mapper->validationRequired($fields); |
331
|
|
|
$errors = $mapper->validate(false); |
332
|
|
|
if (is_array($errors)) { |
333
|
|
|
$this->notify(['warning' => $mapper->validationErrors($errors)]); |
334
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
335
|
|
|
echo \View::instance()->render($view); |
336
|
|
|
return; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
// no change, do nothing |
340
|
|
|
if (!$mapper->changed()) { |
341
|
|
|
$this->notify(_('There was nothing to change!'), 'info'); |
342
|
|
|
return $f3->reroute('@admin_assets_list'); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
// reset usermapper and copy in valid data |
346
|
|
|
$mapper->load(['uuid = ?', $mapper->uuid]); |
347
|
|
|
$mapper->copyfrom($data); |
348
|
|
|
if ($mapper->save()) { |
349
|
|
|
$this->notify(_('The asset data was updated!'), 'success'); |
350
|
|
|
} else { |
351
|
|
|
$this->notify(_('Unable to update asset data!'), 'error'); |
352
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
353
|
|
|
echo \View::instance()->render($view); |
354
|
|
|
return; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
$f3->reroute('@admin_assets_search' . '?search=' . $mapper->uuid); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* |
363
|
|
|
* |
364
|
|
|
* @param \Base $f3 |
365
|
|
|
* @return void |
366
|
|
|
*/ |
367
|
|
|
public function view(\Base $f3) |
368
|
|
|
{ |
369
|
|
|
$this->redirectLoggedOutUser(); |
370
|
|
|
|
371
|
|
|
$uuid = $f3->get('REQUEST.uuid'); |
372
|
|
|
$view = strtolower(trim(strip_tags($f3->get('REQUEST.view')))); |
373
|
|
|
|
374
|
|
|
$mapper = new Mappers\Assets; |
375
|
|
|
$mapper->load(['uuid = ?', $uuid]); |
376
|
|
|
|
377
|
|
|
if (null == $mapper->id) { |
378
|
|
|
$this->notify(_('The entry no longer exists!'), 'error'); |
379
|
|
|
return $f3->reroute('@admin_assets_lists'); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
$f3->set('breadcrumbs', [ |
383
|
|
|
_('Admin') => 'admin', |
384
|
|
|
_('Users') => $this->url('@admin_assets_search', [ |
385
|
|
|
'search' => $mapper->users_uuid, |
386
|
|
|
'search_fields' => 'uuid', |
387
|
|
|
'type' => 'exact', |
388
|
|
|
]), |
389
|
|
|
_('Assets') => $this->url('@admin_assets_search', [ |
390
|
|
|
'search' => $mapper->users_uuid, |
391
|
|
|
'search_fields' => 'users_uuid', |
392
|
|
|
'order' => 'key', |
393
|
|
|
'type' => 'exact', |
394
|
|
|
]), |
395
|
|
|
_('View') => '', |
396
|
|
|
]); |
397
|
|
|
|
398
|
|
|
$db = \Registry::get('db'); |
399
|
|
|
$results = $db->exec($mapper->query); |
|
|
|
|
400
|
|
|
$f3->set('results', $results); |
401
|
|
|
|
402
|
|
|
if ('csv' !== $view) { |
403
|
|
|
$view = empty($view) ? 'view.phtml' : $view . '.phtml'; |
404
|
|
|
$f3->set('REQUEST.view', $view); |
405
|
|
|
$f3->set('form', $mapper->cast()); |
406
|
|
|
echo \View::instance()->render($this->template_path . $view); |
407
|
|
|
} else { |
408
|
|
|
// write the csv file |
409
|
|
|
$file = realpath($f3->get('TEMP')) . '/' . date('Y-m-d') . '-' . $mapper->key . '.csv'; |
410
|
|
|
if (!empty($results) && count($results) > 0) { |
411
|
|
|
$fp = fopen($file, 'w'); |
412
|
|
|
fputcsv($fp, array_keys($results[0])); |
413
|
|
|
foreach ($results as $k => $fields) { |
414
|
|
|
$values = array_values($fields); |
415
|
|
|
fputcsv($fp, $values); |
416
|
|
|
} |
417
|
|
|
fclose($fp); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
header('Content-Description: File Transfer'); |
421
|
|
|
header('Content-type: application/csv; charset=' . $f3->get('ENCODING')); |
422
|
|
|
header("Content-Disposition: attachment; filename=" . basename($file)); |
423
|
|
|
header('Content-Length: ' . filesize($file)); |
424
|
|
|
header('Expires: 0'); |
425
|
|
|
header('Cache-Control: must-revalidate'); |
426
|
|
|
header('Pragma: public'); |
427
|
|
|
readfile($file); |
428
|
|
|
} |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* |
434
|
|
|
* |
435
|
|
|
* @param \Base $f3 |
436
|
|
|
* @return void |
437
|
|
|
*/ |
438
|
|
|
public function delete(\Base $f3) |
439
|
|
|
{ |
440
|
|
|
$this->redirectLoggedOutUser(); |
441
|
|
|
$this->csrf(); |
442
|
|
|
|
443
|
|
|
if (false == $f3->get('isRoot')) { |
444
|
|
|
$this->notify(_('You do not have (root) permission!'), 'error'); |
445
|
|
|
return $f3->reroute('@admin_assets_list'); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
$uuid = $f3->get('REQUEST.uuid'); |
449
|
|
|
|
450
|
|
|
$mapper = new Mappers\Assets; |
451
|
|
|
$mapper->load(['uuid = ?', $uuid]); |
452
|
|
|
|
453
|
|
|
if (null == $mapper->id) { |
454
|
|
|
$this->notify(_('The asset no longer exists!'), 'error'); |
455
|
|
|
return $f3->reroute('@admin_assets_list'); |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
$mapper->erase(); |
459
|
|
|
$this->notify('Asset deleted!', 'success'); |
460
|
|
|
$this->notify(_('Unable to update asset data!'), 'error'); |
461
|
|
|
return $f3->reroute('@admin_assets_list'); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
|
465
|
|
|
} |
466
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.