1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Apps\Controller\Admin; |
4
|
|
|
|
5
|
|
|
use Apps\ActiveRecord\ContentCategory; |
6
|
|
|
use Apps\Model\Admin\Content\FormCategoryDelete; |
7
|
|
|
use Apps\Model\Admin\Content\FormCategoryUpdate; |
8
|
|
|
use Apps\Model\Admin\Content\FormContentClear; |
9
|
|
|
use Apps\Model\Admin\Content\FormContentDelete; |
10
|
|
|
use Apps\Model\Admin\Content\FormContentGlobDelete; |
11
|
|
|
use Apps\Model\Admin\Content\FormContentRestore; |
12
|
|
|
use Apps\Model\Admin\Content\FormContentUpdate; |
13
|
|
|
use Apps\Model\Admin\Content\FormSettings; |
14
|
|
|
use Extend\Core\Arch\AdminAppController; |
15
|
|
|
use Ffcms\Core\App; |
16
|
|
|
use Apps\ActiveRecord\Content as ContentEntity; |
17
|
|
|
use Ffcms\Core\Exception\ForbiddenException; |
18
|
|
|
use Ffcms\Core\Exception\NotFoundException; |
19
|
|
|
use Ffcms\Core\Exception\SyntaxException; |
20
|
|
|
use Ffcms\Core\Helper\FileSystem\Directory; |
21
|
|
|
use Ffcms\Core\Helper\HTML\SimplePagination; |
22
|
|
|
use Ffcms\Core\Helper\Type\Arr; |
23
|
|
|
use Ffcms\Core\Helper\Type\Obj; |
24
|
|
|
|
25
|
|
|
class Content extends AdminAppController |
26
|
|
|
{ |
27
|
|
|
const VERSION = 0.1; |
28
|
|
|
|
29
|
|
|
const ITEM_PER_PAGE = 10; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* List content items |
33
|
|
|
* @return string |
34
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
35
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
36
|
|
|
*/ |
37
|
|
|
public function actionIndex() |
38
|
|
|
{ |
39
|
|
|
// set current page and offset |
40
|
|
|
$page = (int)App::$Request->query->get('page'); |
41
|
|
|
$offset = $page * self::ITEM_PER_PAGE; |
42
|
|
|
|
43
|
|
|
$query = null; |
|
|
|
|
44
|
|
|
// get query type (trash, category, all) |
45
|
|
|
$type = App::$Request->query->get('type'); |
46
|
|
|
if ($type === 'trash') { |
47
|
|
|
$query = ContentEntity::onlyTrashed(); |
48
|
|
|
} elseif (Obj::isLikeInt($type)) { // sounds like category id ;) |
49
|
|
|
$query = ContentEntity::where('category_id', '=', (int)$type); |
50
|
|
|
} else { |
51
|
|
|
$query = new ContentEntity(); |
52
|
|
|
$type = 'all'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// build pagination |
56
|
|
|
$pagination = new SimplePagination([ |
57
|
|
|
'url' => ['content/index', null, null, ['type' => $type]], |
58
|
|
|
'page' => $page, |
59
|
|
|
'step' => self::ITEM_PER_PAGE, |
60
|
|
|
'total' => $query->count() |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
// build listing objects |
64
|
|
|
$records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get(); |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
return App::$View->render('index', [ |
68
|
|
|
'records' => $records, |
69
|
|
|
'pagination' => $pagination, |
70
|
|
|
'type' => $type |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Edit and add content items |
76
|
|
|
* @param $id |
77
|
|
|
* @return string |
78
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
79
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function actionUpdate($id) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
// get item with trashed objects |
84
|
|
|
$record = ContentEntity::withTrashed()->find($id); |
|
|
|
|
85
|
|
|
$isNew = $record->id === null; |
86
|
|
|
|
87
|
|
|
// create empty object if its new |
88
|
|
|
if ($isNew === true) { |
89
|
|
|
$record = new ContentEntity(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// init model |
93
|
|
|
$model = new FormContentUpdate($record); |
94
|
|
|
|
95
|
|
|
// check if model is submit |
96
|
|
|
if ($model->send() && $model->validate()) { |
97
|
|
|
$model->save(); |
98
|
|
|
if ($isNew === true) { |
99
|
|
|
App::$Response->redirect('content/index'); |
100
|
|
|
} |
101
|
|
|
App::$Session->getFlashBag()->add('success', __('Content is successful updated')); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// draw response |
105
|
|
|
return App::$View->render('content_update', [ |
106
|
|
|
'model' => $model |
107
|
|
|
]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Delete content by id |
112
|
|
|
* @param int $id |
113
|
|
|
* @return string |
114
|
|
|
* @throws NotFoundException |
115
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
116
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
117
|
|
|
*/ |
118
|
|
View Code Duplication |
public function actionDelete($id) |
|
|
|
|
119
|
|
|
{ |
120
|
|
|
if (!Obj::isLikeInt($id) || $id < 1) { |
121
|
|
|
throw new NotFoundException(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// get content record and check availability |
125
|
|
|
$record = ContentEntity::find($id); |
126
|
|
|
if ($record === null || $record === false) { |
127
|
|
|
throw new NotFoundException(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// init delete model |
131
|
|
|
$model = new FormContentDelete($record); |
132
|
|
|
if ($model->send() && $model->validate()) { |
133
|
|
|
$model->make(); |
134
|
|
|
App::$Session->getFlashBag()->add('success', __('Content is successful moved to trash')); |
135
|
|
|
App::$Response->redirect('content/index'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return App::$View->render('content_delete', [ |
139
|
|
|
'model' => $model->export() |
140
|
|
|
]); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Restore deleted content |
145
|
|
|
* @param $id |
146
|
|
|
* @return string |
147
|
|
|
* @throws NotFoundException |
148
|
|
|
* @throws SyntaxException |
149
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
150
|
|
|
*/ |
151
|
|
View Code Duplication |
public function actionRestore($id) |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
if (!Obj::isLikeInt($id) || $id < 1) { |
154
|
|
|
throw new NotFoundException(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// get removed object |
158
|
|
|
$record = ContentEntity::onlyTrashed()->find($id); |
|
|
|
|
159
|
|
|
if ($record === null || $record === false) { |
160
|
|
|
throw new NotFoundException(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// init model |
164
|
|
|
$model = new FormContentRestore($record); |
165
|
|
|
// check if action is send |
166
|
|
|
if ($model->send() && $model->validate()) { |
167
|
|
|
$model->make(); |
168
|
|
|
App::$Session->getFlashBag()->add('success', __('Content are successful recovered')); |
169
|
|
|
App::$Response->redirect('content/index'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// draw response |
173
|
|
|
return App::$View->render('content_restore', [ |
174
|
|
|
'model' => $model->export() |
175
|
|
|
]); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Clear the trashed items |
180
|
|
|
* @return string |
181
|
|
|
* @throws SyntaxException |
182
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
183
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
184
|
|
|
*/ |
185
|
|
|
public function actionClear() |
186
|
|
|
{ |
187
|
|
|
// find trashed rows |
188
|
|
|
$records = ContentEntity::onlyTrashed(); |
189
|
|
|
|
190
|
|
|
// init model |
191
|
|
|
$model = new FormContentClear($records->count()); |
192
|
|
|
if ($model->send() && $model->validate()) { |
193
|
|
|
// remove all trashed items |
194
|
|
|
foreach ($records->get() as $item) { |
|
|
|
|
195
|
|
|
$galleryPath = '/upload/gallery/' . (int)$item->id; |
196
|
|
|
if (Directory::exist($galleryPath)) { |
197
|
|
|
Directory::remove($galleryPath); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
// totally remove rows from db |
201
|
|
|
$records->forceDelete(); |
202
|
|
|
App::$Session->getFlashBag()->add('success', __('Trashed content is cleanup')); |
203
|
|
|
App::$Response->redirect('content/index'); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
// draw response |
207
|
|
|
return App::$View->render('content_clear', [ |
208
|
|
|
'model' => $model->export() |
209
|
|
|
]); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Display category list |
214
|
|
|
*/ |
215
|
|
|
public function actionCategories() |
216
|
|
|
{ |
217
|
|
|
return App::$View->render('category_list'); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Delete category action |
222
|
|
|
* @param int $id |
223
|
|
|
* @return string |
224
|
|
|
* @throws ForbiddenException |
225
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
226
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
227
|
|
|
*/ |
228
|
|
View Code Duplication |
public function actionCategorydelete($id) |
|
|
|
|
229
|
|
|
{ |
230
|
|
|
// check id |
231
|
|
|
if (!Obj::isLikeInt($id) || $id < 2) { |
232
|
|
|
throw new ForbiddenException(); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
// get object relation |
236
|
|
|
$record = ContentCategory::find($id); |
237
|
|
|
if ($record === null || $record === false) { |
238
|
|
|
throw new ForbiddenException(); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
// init model with object relation |
242
|
|
|
$model = new FormCategoryDelete($record); |
243
|
|
|
|
244
|
|
|
// check if delete is submited |
245
|
|
|
if ($model->send() && $model->validate()) { |
246
|
|
|
$model->make(); |
247
|
|
|
App::$Session->getFlashBag()->add('success', __('Category is successful removed')); |
248
|
|
|
App::$Response->redirect('content/categories'); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
// draw view |
252
|
|
|
return App::$View->render('category_delete', [ |
253
|
|
|
'model' => $model->export() |
254
|
|
|
]); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Show category edit and create |
259
|
|
|
* @param int $id |
260
|
|
|
* @return string |
261
|
|
|
* @throws \Ffcms\Core\Exception\SyntaxException |
262
|
|
|
* @throws \Ffcms\Core\Exception\NativeException |
263
|
|
|
*/ |
264
|
|
|
public function actionCategoryupdate($id = null) |
265
|
|
|
{ |
266
|
|
|
// get owner id for new rows |
267
|
|
|
$parentId = (int)App::$Request->query->get('parent'); |
268
|
|
|
|
269
|
|
|
// get relation and pass to model |
270
|
|
|
$record = ContentCategory::findOrNew($id); |
271
|
|
|
$isNew = $record->id === null; |
272
|
|
|
$model = new FormCategoryUpdate($record, $parentId); |
|
|
|
|
273
|
|
|
|
274
|
|
|
// if model is submited |
275
|
|
|
if ($model->send() && $model->validate()) { |
276
|
|
|
$model->save(); |
277
|
|
|
// if is new - redirect to list after submit |
278
|
|
|
if ($isNew) { |
279
|
|
|
App::$Response->redirect('content/categories'); |
280
|
|
|
} |
281
|
|
|
// show notify message |
282
|
|
|
App::$Session->getFlashBag()->add('success', __('Category is successful updated')); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
// draw response view and pass model properties |
286
|
|
|
return App::$View->render('category_update', [ |
287
|
|
|
'model' => $model->export() |
288
|
|
|
]); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Show content global delete |
293
|
|
|
* @return string |
294
|
|
|
* @throws NotFoundException |
295
|
|
|
* @throws SyntaxException |
296
|
|
|
*/ |
297
|
|
|
public function actionGlobdelete() |
298
|
|
|
{ |
299
|
|
|
// get content ids from request |
300
|
|
|
$ids = App::$Request->query->get('selectRemove'); |
301
|
|
|
|
302
|
|
|
// check if input is array |
303
|
|
|
if (!Obj::isArray($ids) || count($ids) < 1) { |
304
|
|
|
throw new NotFoundException(__('Nothing to delete is founded')); |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
// get all records as object from db |
308
|
|
|
$records = ContentEntity::find($ids); |
309
|
|
|
|
310
|
|
|
if ($records->count() < 1) { |
311
|
|
|
throw new NotFoundException(__('Nothing to delete is founded')); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
// init model and pass objects |
315
|
|
|
$model = new FormContentGlobDelete($records); |
316
|
|
|
|
317
|
|
|
// check if delete is submited |
318
|
|
|
if ($model->send() && $model->validate()) { |
319
|
|
|
$model->make(); |
320
|
|
|
App::$Session->getFlashBag()->add('success', __('Content are successful removed')); |
321
|
|
|
App::$Response->redirect('content/index'); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
// return response |
325
|
|
|
return App::$View->render('content_glob_delete', [ |
326
|
|
|
'model' => $model |
327
|
|
|
]); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Show settings form with prepared model |
332
|
|
|
* @return string |
333
|
|
|
* @throws SyntaxException |
334
|
|
|
*/ |
335
|
|
View Code Duplication |
public function actionSettings() |
|
|
|
|
336
|
|
|
{ |
337
|
|
|
// init model with config array data |
338
|
|
|
$model = new FormSettings($this->getConfigs()); |
339
|
|
|
|
340
|
|
|
// check if form is send |
341
|
|
|
if ($model->send()) { |
342
|
|
|
if ($model->validate()) { |
343
|
|
|
$this->setConfigs($model->getAllProperties()); |
344
|
|
|
App::$Response->redirect('content/index'); |
345
|
|
|
} else { |
346
|
|
|
App::$Session->getFlashBag()->add('error', __('Form validation is failed')); |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
// draw response |
351
|
|
|
return App::$View->render('settings', [ |
352
|
|
|
'model' => $model->export() |
353
|
|
|
]); |
354
|
|
|
} |
355
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.