Passed
Push — master ( cc7747...d2190f )
by Mihail
10:36
created

Content::actionClear()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 0
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
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\FormContentPublish;
12
use Apps\Model\Admin\Content\FormContentRestore;
13
use Apps\Model\Admin\Content\FormContentUpdate;
14
use Apps\Model\Admin\Content\FormSettings;
15
use Extend\Core\Arch\AdminController;
16
use Ffcms\Core\App;
17
use Apps\ActiveRecord\Content as ContentEntity;
18
use Ffcms\Core\Exception\ForbiddenException;
19
use Ffcms\Core\Exception\NativeException;
20
use Ffcms\Core\Exception\NotFoundException;
21
use Ffcms\Core\Exception\SyntaxException;
22
use Ffcms\Core\Helper\FileSystem\Directory;
23
use Ffcms\Core\Helper\HTML\SimplePagination;
24
use Ffcms\Core\Helper\Type\Obj;
25
26
/**
27
 * Class Content. Admin controller to manage & control contents
28
 * @package Apps\Controller\Admin
29
 */
30
class Content extends AdminController
31
{
32
    const VERSION = 0.1;
33
    const ITEM_PER_PAGE = 10;
34
35
    public $type = 'app';
36
37
    /**
38
     * List content items
39
     * @return string
40
     * @throws \Ffcms\Core\Exception\SyntaxException
41
     * @throws \Ffcms\Core\Exception\NativeException
42
     */
43
    public function actionIndex()
44
    {
45
        // set current page and offset
46
        $page = (int)$this->request->query->get('page');
47
        $offset = $page * self::ITEM_PER_PAGE;
48
49
        $query = null;
0 ignored issues
show
Unused Code introduced by
$query is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

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.

Loading history...
50
        // get query type (trash, category, all)
51
        $type = $this->request->query->get('type');
52
        if ($type === 'trash') {
53
            $query = ContentEntity::onlyTrashed();
0 ignored issues
show
Bug introduced by
The method onlyTrashed() does not exist on Apps\ActiveRecord\Content. Did you maybe mean trashed()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
54
        } elseif ($type === 'moderate') { // only items on moderate
55
            $query = ContentEntity::where('display', '=', 0);
56
        } elseif (Obj::isLikeInt($type)) { // sounds like category id ;)
57
            $query = ContentEntity::where('category_id', '=', (int)$type);
58
        } else {
59
            $query = new ContentEntity();
60
            $type = 'all';
61
        }
62
63
        // build pagination
64
        $pagination = new SimplePagination([
65
            'url' => ['content/index', null, null, ['type' => $type]],
66
            'page' => $page,
67
            'step' => self::ITEM_PER_PAGE,
68
            'total' => $query->count()
69
        ]);
70
71
        // build listing objects
72
        $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get();
73
74
        return $this->view->render('index', [
75
            'records' => $records,
76
            'pagination' => $pagination,
77
            'type' => $type
78
        ]);
79
    }
80
81
    /**
82
     * Edit and add content items
83
     * @param int|null $id
84
     * @return string
85
     * @throws \Ffcms\Core\Exception\SyntaxException
86
     * @throws \Ffcms\Core\Exception\NativeException
87
     */
88 View Code Duplication
    public function actionUpdate($id = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90
        // get item with trashed objects
91
        $record = ContentEntity::withTrashed()->findOrNew($id);
0 ignored issues
show
Bug introduced by
The method withTrashed() does not exist on Apps\ActiveRecord\Content. Did you maybe mean trashed()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
92
        $isNew = $record->id === null;
93
94
        // create empty object if its new
95
        if ($isNew === true) {
96
            $record = new ContentEntity();
97
        }
98
99
        // init model
100
        $model = new FormContentUpdate($record);
101
102
        // check if model is submit
103
        if ($model->send() && $model->validate()) {
104
            $model->save();
105
            if ($isNew === true) {
106
                $this->response->redirect('content/index');
107
            }
108
            App::$Session->getFlashBag()->add('success', __('Content is successful updated'));
109
        }
110
111
        // draw response
112
        return $this->view->render('content_update', [
113
            'model' => $model
114
        ]);
115
    }
116
117
    /**
118
     * Delete content by id
119
     * @param int $id
120
     * @return string
121
     * @throws NotFoundException
122
     * @throws \Ffcms\Core\Exception\SyntaxException
123
     * @throws \Ffcms\Core\Exception\NativeException
124
     */
125 View Code Duplication
    public function actionDelete($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127
        if (!Obj::isLikeInt($id) || $id < 1) {
128
            throw new NotFoundException();
129
        }
130
131
        // get content record and check availability
132
        $record = ContentEntity::find($id);
133
        if ($record === null || $record === false) {
134
            throw new NotFoundException();
135
        }
136
137
        // init delete model
138
        $model = new FormContentDelete($record);
0 ignored issues
show
Compatibility introduced by
$record of type object<Ffcms\Core\Arch\ActiveModel> is not a sub-type of object<Apps\ActiveRecord\Content>. It seems like you assume a child class of the class Ffcms\Core\Arch\ActiveModel to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
139
        if ($model->send() && $model->validate()) {
140
            $model->make();
141
            App::$Session->getFlashBag()->add('success', __('Content is successful moved to trash'));
142
            $this->response->redirect('content/index');
143
        }
144
145
        return $this->view->render('content_delete', [
146
            'model' => $model
147
        ]);
148
    }
149
150
    /**
151
     * Restore deleted content
152
     * @param $id
153
     * @return string
154
     * @throws NotFoundException
155
     * @throws SyntaxException
156
     * @throws \Ffcms\Core\Exception\NativeException
157
     */
158 View Code Duplication
    public function actionRestore($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
159
    {
160
        if (!Obj::isLikeInt($id) || $id < 1) {
161
            throw new NotFoundException();
162
        }
163
164
        // get removed object
165
        $record = ContentEntity::onlyTrashed()->find($id);
0 ignored issues
show
Bug introduced by
The method onlyTrashed() does not exist on Apps\ActiveRecord\Content. Did you maybe mean trashed()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
166
        if ($record === null || $record === false) {
167
            throw new NotFoundException();
168
        }
169
170
        // init model
171
        $model = new FormContentRestore($record);
172
        // check if action is send
173
        if ($model->send() && $model->validate()) {
174
            $model->make();
175
            App::$Session->getFlashBag()->add('success', __('Content are successful recovered'));
176
            $this->response->redirect('content/index');
177
        }
178
179
        // draw response
180
        return $this->view->render('content_restore', [
181
            'model' => $model
182
        ]);
183
    }
184
185
    /**
186
     * Clear all trashed items
187
     * @return string
188
     * @throws SyntaxException
189
     * @throws \Ffcms\Core\Exception\SyntaxException
190
     * @throws \Ffcms\Core\Exception\NativeException
191
     */
192
    public function actionClear()
193
    {
194
        // find trashed rows
195
        $records = ContentEntity::onlyTrashed();
0 ignored issues
show
Bug introduced by
The method onlyTrashed() does not exist on Apps\ActiveRecord\Content. Did you maybe mean trashed()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
196
197
        // init model
198
        $model = new FormContentClear($records);
199
        if ($model->send() && $model->validate()) {
200
            $model->make();
201
            App::$Session->getFlashBag()->add('success', __('Trash content is cleaned'));
202
            $this->response->redirect('content/index');
203
        }
204
205
        // draw response
206
        return $this->view->render('content_clear', [
207
            'model' => $model
208
        ]);
209
    }
210
211
    /**
212
     * Display category list
213
     * @return string
214
     * @throws \Ffcms\Core\Exception\SyntaxException
215
     * @throws \Ffcms\Core\Exception\NativeException
216
     */
217
    public function actionCategories()
218
    {
219
        return $this->view->render('category_list', [
220
            'categories' => ContentCategory::getSortedAll()
221
        ]);
222
    }
223
224
    /**
225
     * Delete category action
226
     * @param int $id
227
     * @return string
228
     * @throws ForbiddenException
229
     * @throws \Ffcms\Core\Exception\SyntaxException
230
     * @throws \Ffcms\Core\Exception\NativeException
231
     */
232 View Code Duplication
    public function actionCategorydelete($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
233
    {
234
        // check id
235
        if (!Obj::isLikeInt($id) || $id < 2) {
236
            throw new ForbiddenException();
237
        }
238
239
        // get object relation
240
        $record = ContentCategory::find($id);
241
        if ($record === null || $record === false) {
242
            throw new ForbiddenException();
243
        }
244
245
        // init model with object relation
246
        $model = new FormCategoryDelete($record);
0 ignored issues
show
Compatibility introduced by
$record of type object<Ffcms\Core\Arch\ActiveModel> is not a sub-type of object<Apps\ActiveRecord\ContentCategory>. It seems like you assume a child class of the class Ffcms\Core\Arch\ActiveModel to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
247
248
        // check if delete is submited
249
        if ($model->send() && $model->validate()) {
250
            $model->make();
251
            App::$Session->getFlashBag()->add('success', __('Category is successful removed'));
252
            $this->response->redirect('content/categories');
253
        }
254
255
        // draw view
256
        return $this->view->render('category_delete', [
257
            'model' => $model
258
        ]);
259
    }
260
261
    /**
262
     * Show category edit and create
263
     * @param int|null $id
264
     * @return string
265
     * @throws \Ffcms\Core\Exception\SyntaxException
266
     * @throws \Ffcms\Core\Exception\NativeException
267
     */
268
    public function actionCategoryupdate($id = null)
269
    {
270
        // get owner id for new rows
271
        $parentId = (int)$this->request->query->get('parent');
272
273
        // get relation and pass to model
274
        $record = ContentCategory::findOrNew($id);
275
        $isNew = $record->id === null;
276
        $model = new FormCategoryUpdate($record, $parentId);
277
278
        // if model is submited
279
        if ($model->send() && $model->validate()) {
280
            $model->save();
281
            // if is new - redirect to list after submit
282
            if ($isNew) {
283
                $this->response->redirect('content/categories');
284
            }
285
            // show notify message
286
            App::$Session->getFlashBag()->add('success', __('Category is successful updated'));
287
        }
288
289
        // draw response view and pass model properties
290
        return $this->view->render('category_update', [
291
            'model' => $model
292
        ]);
293
    }
294
295
    /**
296
     * Show content global delete
297
     * @return string
298
     * @throws NotFoundException
299
     * @throws SyntaxException
300
     * @throws NativeException
301
     */
302
    public function actionGlobdelete()
303
    {
304
        // get content ids from request
305
        $ids = $this->request->query->get('selected');
306
307
        // check if input is array
308 View Code Duplication
        if (!Obj::isArray($ids) || count($ids) < 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
309
            throw new NotFoundException(__('Nothing to delete is founded'));
310
        }
311
312
        // get all records as object from db
313
        $records = ContentEntity::find($ids);
314
315
        if ($records->count() < 1) {
316
            throw new NotFoundException(__('Nothing to delete is founded'));
317
        }
318
319
        // init model and pass objects
320
        $model = new FormContentGlobDelete($records);
0 ignored issues
show
Documentation introduced by
$records is of type object<Ffcms\Core\Arch\ActiveModel>|null, but the function expects a array<integer,object<App...se\Eloquent\Collection>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
321
322
        // check if delete is submited
323
        if ($model->send() && $model->validate()) {
324
            $model->make();
325
            App::$Session->getFlashBag()->add('success', __('Content are successful removed'));
326
            $this->response->redirect('content/index');
327
        }
328
329
        // return response
330
        return $this->view->render('content_glob_delete', [
331
            'model' => $model
332
        ]);
333
    }
334
335
    /**
336
     * Publish content on moderate stage
337
     * @return string
338
     * @throws NotFoundException
339
     * @throws SyntaxException
340
     * @throws NativeException
341
     */
342
    public function actionPublish()
343
    {
344
        // get ids as array from GET
345
        $ids = $this->request->query->get('selected');
346 View Code Duplication
        if (!Obj::isArray($ids) || count($ids) < 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
347
            throw new NotFoundException(__('Items to publish is not found'));
348
        }
349
350
        // try to find items in db
351
        $records = ContentEntity::whereIn('id', $ids)->where('display', '=', 0);
352
        if ($records->count() < 1) {
353
            throw new NotFoundException(__('Items to publish is not found'));
354
        }
355
356
        // initialize model and operate submit
357
        $model = new FormContentPublish($records);
358
        if ($model->send() && $model->validate()) {
359
            $model->make();
360
            App::$Session->getFlashBag()->add('success', __('Content is successful published'));
361
            $this->response->redirect('content/index');
362
        }
363
364
        // draw view output
365
        return $this->view->render('publish', [
366
            'records' => $records->get(),
367
            'model' => $model
368
        ]);
369
    }
370
371
    /**
372
     * Show settings form with prepared model
373
     * @return string
374
     * @throws SyntaxException
375
     * @throws NativeException
376
     */
377 View Code Duplication
    public function actionSettings()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
378
    {
379
        // init model with config array data
380
        $model = new FormSettings($this->getConfigs());
381
382
        // check if form is send
383
        if ($model->send()) {
384
            if ($model->validate()) {
385
                $this->setConfigs($model->getAllProperties());
386
                App::$Session->getFlashBag()->add('success', __('Settings is successful updated'));
387
                $this->response->redirect('content/index');
388
            } else {
389
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
390
            }
391
        }
392
393
        // draw response
394
        return $this->view->render('settings', [
395
            'model' => $model
396
        ]);
397
    }
398
}