ReviewsController.php 9.84 KB
Newer Older
Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
1
<?php
2

3
namespace App\Controller\Api\V1;
4

Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
5
use App\Controller\AppController;
6
7
8
9
10
11
12
use App\Model\Table\ReviewsTable;
use App\Network\Exception\SoftwareNotFoundException;
use App\Network\Exception\UserNotFoundException;
use Cake\Event\Event;
use Cake\Network\Exception\NotFoundException;
use Cake\Network\Response;
use Exception;
Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
13
14
15
16

/**
 * Reviews Controller
 *
17
 * @property ReviewsTable $Reviews
Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
18
19
20
21
 */
class ReviewsController extends AppController
{

22
23
24
    /**
     * Manage all rights for the controllers' actions.
     * notice: $forceDeny parameter is not used here,
Matthieu FAURE's avatar
Matthieu FAURE committed
25
     *                     but is mandatory to be compatible with parent::isAuthorized()
26
27
28
29
30
31
     *
     * @param Array $user User informations
     * @param  boolean $forceDeny by default FALSE, set TRUE to force the deny on parent::isAuthorized()
     * @return boolean
     */
    public function isAuthorized($user, $forceDeny = false)
Pastor Mickaël's avatar
Pastor Mickaël committed
32
    {
33
34
        if ($this->Auth->user()) {
            if ($this->request->action === 'add') {
Pastor Mickaël's avatar
Pastor Mickaël committed
35
36
37
38
39
40
                $this->loadModel("Users");
                return $this->Users->isAdministration($this->Auth->user('id'));
            }
        }
    }

41
    public function beforeFilter(Event $event)
Pastor Mickaël's avatar
Pastor Mickaël committed
42
    {
43
        //        $this->Auth->allow('add');
Pastor Mickaël's avatar
Pastor Mickaël committed
44
45
46
47
        $this->Auth->deny('delete');
        parent::beforeFilter($event);
    }

Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
48
49
50
51
52
53
54
    /**
     * Index method
     *
     * @return void
     */
    public function index()
    {
55
56
        if (isset($this->request->params['software_id'])) {
            $this->viewBuilder()->template("reviews_software");
57
58
59
60
            $software = $this->Reviews->Softwares->find("all")->select([
                "id",
                "softwarename"
            ])->where(["id" => $this->request->params['software_id']])->firstOrFail();
61
            $this->set('software', $software);
62
63
64
            $this->set('_serialize', ['software']);
        }

Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
65
        $this->paginate = [
66
            'conditions' => isset($this->request->params['software_id']) ? [
Pastor Mickaël's avatar
Pastor Mickaël committed
67
                'Softwares.id ' => $this->request->params['software_id']
68
            ] : [],
Pastor Mickaël's avatar
Pastor Mickaël committed
69
            'contain' => [
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
                'Users' => [
                    'UserTypes',
                    "fields" => [
                        "id",
                        "username",
                        'logo_directory',
                        'photo',
                        ]
                ],
                'Softwares' => [
                    "fields" => [
                        "id",
                        "softwarename"
                    ]
                ]
Pastor Mickaël's avatar
Pastor Mickaël committed
85
            ]
Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
86
        ];
87

88
89
90
91
92
93
94
95
        // Check that the current URL is correct
        $softwareId = $software->id;
        $lang = $this->selectedLanguage;
        $allowedUrl = "/$lang/softwares/$softwareId/reviews";
        if ($allowedUrl !== $this->request->here(false)) {
            return $this->redirect("$allowedUrl", 301);
        }

Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
96
97
        $this->set('reviews', $this->paginate($this->Reviews));
        $this->set('_serialize', ['reviews']);
Fabrice Gangler's avatar
Fabrice Gangler committed
98
99
100
101
102
103
104
105
106
107
108
109
110


        // Breadcrumbs
        $links = array();
        $links[] = [
            'name' => $software->softwarename,
            'url' => "softwares/$softwareId"
        ];
        $links[] = [
            'name' => __d('Breadcrumbs', 'Software.Reviews'),
            'url' => "softwares/$softwareId/reviews"
        ];
        $this->setBreadcrumbsSoftware($links);
Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
111
112
113
114
115
    }

    /**
     * View method
     *
116
     * @param string|null $id Review id.
Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
117
     * @return void
118
     * @throws NotFoundException When record not found.
Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
119
120
121
     */
    public function view($id = null)
    {
Pastor Mickaël's avatar
Pastor Mickaël committed
122

123
        $review = $this->Reviews->get(
124
125
            $id,
            [
126
127
                'contain' => [
                    'Users' => [
128
                        'UserTypes',
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
                        'fields' => [
                            'id',
                            'username',
                            'logo_directory',
                            'photo',
                            'description'
                        ]
                    ],
                    'Softwares' => [
                        "Reviews",
                        'fields' => [
                            'id',
                            'softwarename',
                            'url_website',
                            'url_repository',
                            'logo_directory',
                            'photo',
                            'description',
                        ]
148
                    ]
149
150
                ]
            ]
151
        );
152
153
154
155
156
157
158
159
160
161
        $software   = $review->software;
        $softwareId = $software->id;

        // Check that the current URL is correct
        $lang = $this->selectedLanguage;
        $allowedUrl = "/$lang/softwares/$softwareId/reviews/". (int) $id;
        if ($allowedUrl !== $this->request->here(false)) {
            return $this->redirect("$allowedUrl", 301);
        }

Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
162
163
        $this->set('review', $review);
        $this->set('_serialize', ['review']);
Fabrice Gangler's avatar
Fabrice Gangler committed
164
165
166
167
168
169
170
171
172
173
174
175

        // Breadcrumbs
        $links = array();
        $links[] = [
            'name' => $software->softwarename,
            'url' => "softwares/$softwareId"
        ];
        $links[] = [
            'name' => __d('Breadcrumbs', 'Software.Reviews'),
            'url' => "softwares/$softwareId/reviews"
        ];
        $this->setBreadcrumbsSoftware($links);
Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
176
177
178
179
180
181
182
183
184
    }

    /**
     * Add method
     *
     * @return void Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
185
        $this->request->allowMethod(['post', 'get']);
Pastor Mickaël's avatar
Pastor Mickaël committed
186

Fabrice Gangler's avatar
Fabrice Gangler committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
        try {
            $software = $this->Reviews->Softwares->get($this->request->params['software_id']);
            $this->request->data['software_id'] = $software->id;
        } catch (Exception $e) {
            throw new SoftwareNotFoundException(
                "The software with the id " . $this->request->params['software_id'] . " does not exist"
            );
        }

        try {
            $user = $this->Reviews->Users->get($this->Auth->user('id'));
            $this->request->data['user_id'] = $user->id;
        } catch (Exception $e) {
            throw new UserNotFoundException("The user with the id " . $this->Auth->user('id') . " does not exist");
        }

203
204
205
206
        $review = $this->Reviews->newEntity();
        $message = "";
        if ($this->request->is('post')) {
            $review = $this->Reviews->patchEntity($review, $this->request->data);
Pastor Mickaël's avatar
Pastor Mickaël committed
207

208
209
            if ($this->Reviews->save($review)) {
                $message = "Success";
210
211
212
                if (!$this->request->is('json')) {
                    $this->Flash->success(__d("Forms", "Your review has been posted"));
                    $this->redirect("softwares/" . $this->request->data['software_id']);
213
214
                }
            } else {
215
216
217
218
                if (!$this->request->is('json')) {
                    $this->Flash->error(__d("Forms", "You can not post more than one review for a software."));
                    $this->redirect("softwares/" . $this->request->data['software_id']);
                } else {
219
                    $message = "Error";
220
221
                    $review = $review->errors();
                }
222
            }
Pastor Mickaël's avatar
Pastor Mickaël committed
223
        }
224
225
        $this->set(
            [
226
227
228
                'message' => $message,
                'review' => $review,
                '_serialize' => ['message', 'review']
229
230
            ]
        );
Fabrice Gangler's avatar
Fabrice Gangler committed
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259

        // Breadcrumbs
        $softwareId = $software->id;
        $links = array();
        $links[] = [
            'name' => $software->softwarename,
            'url' => "softwares/$softwareId"
        ];
        $links[] = [
            'name' => __d('Breadcrumbs', 'Software.AddReview'),
            'url' => "softwares/add-review/$softwareId"
        ];
        $this->setBreadcrumbsSoftware($links);
    }

    /**
     * $links = [ 0 => [ 'name' => '…', 'url' => '/dir/file', 'title' => '…'],
     *            1 => [ 'name' => '…', 'url' => '/dir/file', 'title' => '…'], ]
     *
     * @param array $links
     */
    protected function setBreadcrumbsSoftware(array $links = [])
    {
        $firstLink = [
            'name' => __d('Breadcrumbs', 'Software.ListOfSoftware'),
            'url' => 'softwares'
        ];
        array_unshift($links, $firstLink);
        parent::setBreadcrumbs($links);
Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
260
261
262
263
264
    }

    /**
     * Edit method
     *
265
     * @param string|null $id Review id.
Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
266
     * @return void Redirects on successful edit, renders view otherwise.
267
     * @throws NotFoundException When record not found.
Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
268
269
270
     */
    public function edit($id = null)
    {
271
        $review = $this->Reviews->get(
272
273
            $id,
            [
274
                'contain' => []
275
276
            ]
        );
Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
277
278
279
280
281
282
283
284
285
286
287
288
289
290
        if ($this->request->is(['patch', 'post', 'put'])) {
            $review = $this->Reviews->patchEntity($review, $this->request->data);
            if ($this->Reviews->save($review)) {
                $this->Flash->success(__('The review has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The review could not be saved. Please, try again.'));
            }
        }
        $users = $this->Reviews->Users->find('list', ['limit' => 200]);
        $softwares = $this->Reviews->Softwares->find('list', ['limit' => 200]);
        $this->set(compact('review', 'users', 'softwares'));
        $this->set('_serialize', ['review']);
    }
Pastor Mickaël's avatar
Pastor Mickaël committed
291

Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
292
293
294
    /**
     * Delete method
     *
295
     * @param string|null $id Review id.
296
297
     * @return Response|null Redirects to index.
     * @throws NotFoundException When record not found.
Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
298
299
300
301
302
303
304
305
306
307
308
309
310
     */
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $review = $this->Reviews->get($id);
        if ($this->Reviews->delete($review)) {
            $this->Flash->success(__('The review has been deleted.'));
        } else {
            $this->Flash->error(__('The review could not be deleted. Please, try again.'));
        }
        return $this->redirect(['action' => 'index']);
    }
}