ReviewsController.php 6.47 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
6
7
8
9
10
11
12
13
14
use App\Controller\AppController;

/**
 * Reviews Controller
 *
 * @property \App\Model\Table\ReviewsTable $Reviews
 */
class ReviewsController extends AppController
{

Pastor Mickaël's avatar
Pastor Mickaël committed
15
16
    public function isAuthorized($user)
    {
17
18
        if ($this->Auth->user()) {
            if ($this->request->action === 'add') {
Pastor Mickaël's avatar
Pastor Mickaël committed
19
20
21
22
23
24
25
26
27
28
29
30
31
                $this->loadModel("Users");
                return $this->Users->isAdministration($this->Auth->user('id'));
            }
        }
    }

    public function beforeFilter(\Cake\Event\Event $event)
    {
//        $this->Auth->allow('add');
        $this->Auth->deny('delete');
        parent::beforeFilter($event);
    }

Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
32
33
34
35
36
37
38
    /**
     * Index method
     *
     * @return void
     */
    public function index()
    {
Pastor Mickaël's avatar
Pastor Mickaël committed
39

40
41
        if (isset($this->request->params['software_id'])) {
            $this->viewBuilder()->template("reviews_software");
42
43
            $software = $this->Reviews->Softwares->find("all")->select(["id", "softwarename"])->where(["id" => $this->request->params['software_id']])->firstOrFail();
            $this->set('software', $software);
44
45
46
47
            $this->set('_serialize', ['software']);

        }

Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
48
        $this->paginate = [
49
            'conditions' => isset($this->request->params['software_id']) ? [
Pastor Mickaël's avatar
Pastor Mickaël committed
50
                'Softwares.id ' => $this->request->params['software_id']
51
            ] : [],
Pastor Mickaël's avatar
Pastor Mickaël committed
52
            'contain' => [
53
54
                'Users' => ["fields" => ["id", "username"]],
                'Softwares' => ["fields" => ["id", "softwarename"]]
Pastor Mickaël's avatar
Pastor Mickaël committed
55
            ]
Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
56
        ];
57

Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
58
59
60
61
62
63
64
65
66
67
68
69
70
        $this->set('reviews', $this->paginate($this->Reviews));
        $this->set('_serialize', ['reviews']);
    }

    /**
     * View method
     *
     * @param string|null $id Review id.
     * @return void
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function view($id = null)
    {
Pastor Mickaël's avatar
Pastor Mickaël committed
71

Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
72
        $review = $this->Reviews->get($id, [
73
74
75
76
77
78
79
80
81
82
83
            'contain' => [
                'Users' => [
                    'fields' => [
                        'id',
                        'username',
                        'logo_directory',
                        'photo',
                        'description'
                    ]
                ],
                'Softwares' => [
84
                    "Reviews",
85
86
87
                    'fields' => [
                        'id',
                        'softwarename',
88
89
                        'url_website',
                        'url_repository',
90
91
92
93
                        'logo_directory',
                        'photo',
                        'description',
                    ]
94
95
                ]
            ]
Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
96
97
98
99
100
101
102
103
104
105
106
107
        ]);
        $this->set('review', $review);
        $this->set('_serialize', ['review']);
    }

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

110
111
112
113
114
115
116
117
118
119
        $review = $this->Reviews->newEntity();
        $message = "";
        if ($this->request->is('post')) {

            try {
                $software = $this->Reviews->Softwares->get($this->request->params['software_id']);
                $this->request->data['software_id'] = $software->id;
            } catch (\Exception $e) {
                throw new \App\Network\Exception\SoftwareNotFoundException("The software with the id " . $this->request->params['software_id'] . " does not exist");
            }
Pastor Mickaël's avatar
Pastor Mickaël committed
120

121
122
123
124
125
126
            try {
                $user = $this->Reviews->Users->get($this->Auth->user('id'));
                $this->request->data['user_id'] = $user->id;
            } catch (\Exception $e) {
                throw new \App\Network\Exception\UserNotFoundException("The user with the id " . $this->Auth->user('id') . " does not exist");
            }
Pastor Mickaël's avatar
Pastor Mickaël committed
127
128


129
            $review = $this->Reviews->patchEntity($review, $this->request->data);
Pastor Mickaël's avatar
Pastor Mickaël committed
130

131
132
            if ($this->Reviews->save($review)) {
                $message = "Success";
133
134
135
                if (!$this->request->is('json')) {
                    $this->Flash->success(__d("Forms", "Your review has been posted"));
                    $this->redirect("softwares/" . $this->request->data['software_id']);
136
137
                }
            } else {
138
139
140
141
                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 {
142
                    $message = "Error";
143
144
145
146
                    $review = $review->errors();
                }


147
            }
Pastor Mickaël's avatar
Pastor Mickaël committed
148
149
        }
        $this->set([
150
151
152
153
            'message' => $message,
            'review' => $review,
            '_serialize' => ['message', 'review']
        ]);
Pastor Mickaël's avatar
Pastor Mickaël committed
154

Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
    }

    /**
     * Edit method
     *
     * @param string|null $id Review id.
     * @return void Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function edit($id = null)
    {
        $review = $this->Reviews->get($id, [
            'contain' => []
        ]);
        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
183

Pastor Mickaël's avatar
CRUD  
Pastor Mickaël committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
    /**
     * Delete method
     *
     * @param string|null $id Review id.
     * @return \Cake\Network\Response|null Redirects to index.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    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']);
    }
}