Completed
Push — master ( d93525...41af6a )
by Fèvre
10s
created

src/Controller/Admin/CategoriesController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace App\Controller\Admin;
3
4
use App\Controller\AppController;
5
use Cake\I18n\I18n;
6
7
class CategoriesController extends AppController
8
{
9
10
    /**
11
     * Display all categories.
12
     *
13
     * @return void
14
     */
15
    public function index()
16
    {
17
        $this->loadModel('BlogCategories');
18
19
        $this->paginate = [
20
            'maxLimit' => 15
21
        ];
22
23
        $categories = $this->BlogCategories
24
            ->find()
25
            ->order([
26
                'created' => 'desc'
27
            ]);
28
29
        $categories = $this->paginate($categories);
30
        $this->set(compact('categories'));
31
    }
32
33
    /**
34
     * Add a category.
35
     *
36
     * @return \Cake\Network\Response|void
37
     */
38
    public function add()
39
    {
40
        $this->loadModel('BlogCategories');
41
42
        $this->BlogCategories->locale(I18n::defaultLocale());
43
        $category = $this->BlogCategories->newEntity($this->request->getParsedBody());
44
45
        if ($this->request->is('post')) {
46
            $category->setTranslations($this->request->getParsedBody());
47
48
            if ($this->BlogCategories->save($category)) {
49
                $this->Flash->success(__d('admin', 'The category has been created successfully !'));
50
51
                return $this->redirect(['action' => 'index']);
52
            }
53
        }
54
55
        $this->set(compact('category'));
56
    }
57
58
    /**
59
     * Edit a category.
60
     *
61
     * @return \Cake\Network\Response|void
62
     */
63
    public function edit()
64
    {
65
        $this->loadModel('BlogCategories');
66
67
        $this->BlogCategories->locale(I18n::defaultLocale());
68
        $category = $this->BlogCategories
69
            ->find('translations')
70
            ->where([
71
                'BlogCategories.id' => $this->request->id
72
            ])
73
            ->first();
74
75
        //Check if the category is found.
76
        if (empty($category)) {
77
            $this->Flash->error(__d('admin', 'This category doesn\'t exist or has been deleted.'));
78
79
            return $this->redirect(['action' => 'index']);
80
        }
81
82 View Code Duplication
        if ($this->request->is('put')) {
0 ignored issues
show
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...
83
            $this->BlogCategories->patchEntity($category, $this->request->getParsedBody());
84
            $category->setTranslations($this->request->getParsedBody());
85
86
            if ($this->BlogCategories->save($category)) {
87
                $this->Flash->success(__d('admin', 'This category has been updated successfully !'));
88
89
                return $this->redirect(['action' => 'index']);
90
            }
91
        }
92
93
        $this->set(compact('category'));
94
    }
95
96
    /**
97
     * Delete a category and all his comments and likes.
98
     *
99
     * @return \Cake\Network\Response
100
     */
101
    public function delete()
102
    {
103
        $this->loadModel('BlogCategories');
104
105
        $category = $this->BlogCategories
106
            ->find()
107
            ->where([
108
                'BlogCategories.id' => $this->request->id
109
            ])
110
            ->first();
111
112
        //Check if the category is found.
113
        if (empty($category)) {
114
            $this->Flash->error(__d('admin', 'This category doesn\'t exist or has been deleted.'));
115
116
            return $this->redirect(['action' => 'index']);
117
        }
118
119
        //Check if the category has one article or more
120
        if ($category->article_count >= 1) {
121
            $this->Flash->error(__d('admin', 'You can not deleted this category because one article or more is assigned to this category.'));
122
123
            return $this->redirect(['action' => 'index']);
124
        }
125
126
        if ($this->BlogCategories->delete($category)) {
127
            $this->Flash->success(__d('admin', 'This category has been deleted successfully !'));
128
129
            return $this->redirect(['action' => 'index']);
130
        }
131
132
        $this->Flash->error(__d('admin', 'Unable to delete this category.'));
133
134
        return $this->redirect(['action' => 'index']);
135
    }
136
}
137