Completed
Push — master ( 2b4546...faabd5 )
by Fèvre
10:23 queued 08:18
created

src/Controller/Admin/ArticlesController.php (2 issues)

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 ArticlesController extends AppController
8
{
9
    /**
10
     * Helpers.
11
     *
12
     * @var array
13
     */
14
    public $helpers = ['I18n'];
15
16
    /**
17
     * Display all articles.
18
     *
19
     * @return void
20
     */
21
    public function index()
22
    {
23
        $this->loadModel('BlogArticles');
24
25
        $this->paginate = [
26
            'maxLimit' => 15
27
        ];
28
29
        $articles = $this->BlogArticles
30
            ->find()
31
            ->contain([
32
                'BlogCategories' => function ($q) {
33
                    return $q
34
                        ->select([
35
                            'id',
36
                            'title'
37
                        ]);
38
                },
39
                'Users' => function ($q) {
40
                    return $q->find('short');
41
                }
42
            ])
43
            ->order([
44
                'BlogArticles.created' => 'desc'
45
            ]);
46
47
        $articles = $this->paginate($articles);
48
        $this->set(compact('articles'));
49
    }
50
51
    /**
52
     * Add an article.
53
     *
54
     * @return \Cake\Network\Response|void
55
     */
56
    public function add()
57
    {
58
        $this->loadModel('BlogArticles');
59
60
        $this->BlogArticles->locale(I18n::defaultLocale());
61
        $article = $this->BlogArticles->newEntity($this->request->data);
62
63 View Code Duplication
        if ($this->request->is('post')) {
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...
64
            $article->user_id = $this->Auth->user('id');
65
            $article->setTranslations($this->request->data);
66
67
            if ($this->BlogArticles->save($article)) {
68
                $this->Flash->success(__d('admin', 'Your article has been created successfully !'));
69
70
                return $this->redirect(['action' => 'index']);
71
            }
72
        }
73
74
        $categories = $this->BlogArticles->BlogCategories->find('list');
75
76
        $this->set(compact('article', 'categories'));
77
    }
78
79
    /**
80
     * Edit an Article.
81
     *
82
     * @return \Cake\Network\Response|void
83
     */
84
    public function edit()
85
    {
86
        $this->loadModel('BlogArticles');
87
88
        $this->BlogArticles->locale(I18n::defaultLocale());
89
        $article = $this->BlogArticles
90
            ->find('translations')
91
            ->where([
92
                'BlogArticles.id' => $this->request->id
93
            ])
94
            ->contain([
95
                'BlogAttachments',
96
                'BlogCategories',
97
                'Users' => function ($q) {
98
                        return $q->find('short');
99
                }
100
            ])
101
            ->first();
102
103
        //Check if the article is found.
104
        if (empty($article)) {
105
            $this->Flash->error(__d('admin', 'This article doesn\'t exist or has been deleted.'));
106
107
            return $this->redirect(['action' => 'index']);
108
        }
109
110 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...
111
            $this->BlogArticles->patchEntity($article, $this->request->data());
112
            $article->setTranslations($this->request->data);
113
114
            if ($this->BlogArticles->save($article)) {
115
                $this->Flash->success(__d('admin', 'This article has been updated successfully !'));
116
117
                return $this->redirect(['action' => 'index']);
118
            }
119
        }
120
121
        $categories = $this->BlogArticles->BlogCategories->find('list');
122
        $this->set(compact('article', 'categories'));
123
    }
124
125
    /**
126
     * Delete an Article and all his comments and likes.
127
     *
128
     * @return \Cake\Network\Response
129
     */
130 View Code Duplication
    public function delete()
131
    {
132
        $this->loadModel('BlogArticles');
133
134
        $article = $this->BlogArticles
135
            ->find()
136
            ->where([
137
                'BlogArticles.id' => $this->request->id
138
            ])
139
            ->first();
140
141
        //Check if the article is found.
142
        if (empty($article)) {
143
            $this->Flash->error(__d('admin', 'This article doesn\'t exist or has been deleted.'));
144
145
            return $this->redirect(['action' => 'index']);
146
        }
147
148
        if ($this->BlogArticles->delete($article)) {
149
            $this->Flash->success(__d('admin', 'This article has been deleted successfully !'));
150
151
            return $this->redirect(['action' => 'index']);
152
        }
153
154
        $this->Flash->error(__d('admin', 'Unable to delete this article.'));
155
156
        return $this->redirect(['action' => 'index']);
157
    }
158
}
159