Completed
Push — master ( 9c742a...1858b1 )
by Fèvre
14s
created

src/Controller/Admin/ArticlesController.php (3 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->eventManager()->attach(new Statistics());
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Event\EventManager::attach() has been deprecated with message: 3.0.0 Use on() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
69
                $event = new Event('Model.BlogArticles.new');
70
                $this->eventManager()->dispatch($event);
71
72
                $this->Flash->success(__d('admin', 'Your article has been created successfully !'));
73
74
                return $this->redirect(['action' => 'index']);
75
            }
76
        }
77
78
        $categories = $this->BlogArticles->BlogCategories->find('list');
79
80
        $this->set(compact('article', 'categories'));
81
    }
82
83
    /**
84
     * Edit an Article.
85
     *
86
     * @return \Cake\Network\Response|void
87
     */
88
    public function edit()
89
    {
90
        $this->loadModel('BlogArticles');
91
92
        $this->BlogArticles->locale(I18n::defaultLocale());
93
        $article = $this->BlogArticles
94
            ->find('translations')
95
            ->where([
96
                'BlogArticles.id' => $this->request->id
97
            ])
98
            ->contain([
99
                'BlogAttachments',
100
                'BlogCategories',
101
                'Users' => function ($q) {
102
                        return $q->find('short');
103
                }
104
            ])
105
            ->first();
106
107
        //Check if the article is found.
108
        if (empty($article)) {
109
            $this->Flash->error(__d('admin', 'This article doesn\'t exist or has been deleted.'));
110
111
            return $this->redirect(['action' => 'index']);
112
        }
113
114
        if ($this->request->is('put')) {
115
            $this->BlogArticles->patchEntity($article, $this->request->data());
116
            $article->setTranslations($this->request->data);
117
118
            if ($this->BlogArticles->save($article)) {
119
                $this->Flash->success(__d('admin', 'This article has been updated successfully !'));
120
121
                return $this->redirect(['action' => 'index']);
122
            }
123
        }
124
125
        $categories = $this->BlogArticles->BlogCategories->find('list');
126
        $this->set(compact('article', 'categories'));
127
    }
128
129
    /**
130
     * Delete an Article and all his comments and likes.
131
     *
132
     * @return \Cake\Network\Response
133
     */
134
    public function delete()
135
    {
136
        $this->loadModel('BlogArticles');
137
138
        $article = $this->BlogArticles
139
            ->find()
140
            ->where([
141
                'BlogArticles.id' => $this->request->id
142
            ])
143
            ->first();
144
145
        //Check if the article is found.
146
        if (empty($article)) {
147
            $this->Flash->error(__d('admin', 'This article doesn\'t exist or has been deleted.'));
148
149
            return $this->redirect(['action' => 'index']);
150
        }
151
152
        if ($this->BlogArticles->delete($article)) {
153
            $this->eventManager()->attach(new Statistics());
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Event\EventManager::attach() has been deprecated with message: 3.0.0 Use on() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
154
            $event = new Event('Model.BlogArticles.new');
155
            $this->eventManager()->dispatch($event);
156
157
            $this->Flash->success(__d('admin', 'This article has been deleted successfully !'));
158
159
            return $this->redirect(['action' => 'index']);
160
        }
161
162
        $this->Flash->error(__d('admin', 'Unable to delete this article.'));
163
164
        return $this->redirect(['action' => 'index']);
165
    }
166
}
167