RelationshipsSoftwaresUsersController.php 8.85 KB
Newer Older
1
<?php
2

3
namespace App\Controller\Api\V1;
4

5
use App\Controller\AppController;
6 7 8
use App\Model\Table\RelationshipsSoftwaresUsersTable;
use Cake\Network\Exception\NotFoundException;
use Cake\Network\Response;
9 10 11 12

/**
 * RelationshipsSoftwaresUsers Controller
 *
13
 * @property RelationshipsSoftwaresUsersTable $RelationshipsSoftwaresUsers
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 */
class RelationshipsSoftwaresUsersController extends AppController
{

    /**
     * Index method
     *
     * @return void
     */
    public function index()
    {
        $this->paginate = [
            'contain' => ['Softwares', 'Users', 'Relationships']
        ];
        $this->set('relationshipsSoftwaresUsers', $this->paginate($this->RelationshipsSoftwaresUsers));
        $this->set('_serialize', ['relationshipsSoftwaresUsers']);
    }
31

32
    /**
33
     * Returns all services providers in Comptoir du libre.
34
     */
35 36
    public function getServicesProviders()
    {
37

38 39 40
        if ($this->request->is('get') && $this->response->type('json')) {
            $Relationships = $this->RelationshipsSoftwaresUsers->find(
                'all',
41
                [
42
                    'contain' => ["Users", "Relationships"],
43
                    'conditions' => ["Relationships.cd = " => $this->request->getQuery('Relationships')],
44 45 46 47
                ]
            )
                ->distinct("user_id");
            $message = "Success";
48

49 50
            $this->set('relationshipsSoftwaresUsers', ['message' => $message, 'Relationships' => $Relationships]);
            $this->set('_serialize', ['message', 'relationshipsSoftwaresUsers']);
51 52
        } else {
            $message = "Error";
53 54
            $this->set(
                [
55 56 57
                    'message' => $message,
                    'relationshipsSoftwaresUser' => $relationshipsSoftwaresUser,
                    '_serialize' => ['message', 'relationshipsSoftwaresUser']
58 59
                ]
            );
60
        }
61
    }
62 63 64 65

    /**
     * View method
     *
66
     * @param string|null $id Relationships Softwares User id.
67
     * @return void
68
     * @throws NotFoundException When record not found.
69 70 71
     */
    public function view($id = null)
    {
72
        $relationshipsSoftwaresUser = $this->RelationshipsSoftwaresUsers->get(
73 74
            $id,
            [
75
                'contain' => ['Softwares', 'Users', 'Relationships']
76 77
            ]
        );
78 79 80 81 82 83 84 85 86 87 88 89
        $this->set('relationshipsSoftwaresUser', $relationshipsSoftwaresUser);
        $this->set('_serialize', ['relationshipsSoftwaresUser']);
    }

    /**
     * Add method
     *
     * @return void Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $relationshipsSoftwaresUser = $this->RelationshipsSoftwaresUsers->newEntity();
90
        if ($this->request->is('post') && !$this->request->is('json')) {
91 92
            $relationshipsSoftwaresUser = $this->RelationshipsSoftwaresUsers->patchEntity(
                $relationshipsSoftwaresUser,
93
                $this->request->getData()
94
            );
95 96 97 98 99 100
            if ($this->RelationshipsSoftwaresUsers->save($relationshipsSoftwaresUser)) {
                $this->Flash->success(__('The relationships softwares user has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The relationships softwares user could not be saved. Please, try again.'));
            }
101 102 103 104 105
            $softwares = $this->RelationshipsSoftwaresUsers->Softwares->find('list', ['limit' => 200]);
            $users = $this->RelationshipsSoftwaresUsers->Users->find('list', ['limit' => 200]);
            $relationships = $this->RelationshipsSoftwaresUsers->Relationships->find('list', ['limit' => 200]);
            $this->set(compact('relationshipsSoftwaresUser', 'softwares', 'users', 'relationships'));
            $this->set('_serialize', ['relationshipsSoftwaresUser']);
106
        } elseif ($this->request->is('post') && $this->request->is('json')) {
107 108
            $relationshipsSoftwaresUser = $this->RelationshipsSoftwaresUsers->patchEntity(
                $relationshipsSoftwaresUser,
109
                $this->request->getData()
110
            );
111 112 113 114 115
            if ($this->RelationshipsSoftwaresUsers->save($relationshipsSoftwaresUser)) {
                $message = "Success";
            } else {
                $message = 'Error';
            }
116 117
            $this->set(
                [
118 119 120
                    'message' => $message,
                    'relationshipsSoftwaresUser' => $relationshipsSoftwaresUser,
                    '_serialize' => ['message', 'relationshipsSoftwaresUser']
121 122
                ]
            );
123 124 125 126 127
        } else {//Pour le template avant d'avoir poste les données.
            $softwares = $this->RelationshipsSoftwaresUsers->Softwares->find(
                'list',
                ['limit' => 200]
            )->orderAsc('softwarename');
128
            $users = $this->RelationshipsSoftwaresUsers->Users->find('list', ['limit' => 200])->orderAsc('username');
129 130 131 132
            $relationships = $this->RelationshipsSoftwaresUsers->Relationships->find(
                'list',
                ['limit' => 200]
            )->orderAsc('name');
133 134 135
            $this->set(compact('relationshipsSoftwaresUser', 'softwares', 'users', 'relationships'));
            $this->set('_serialize', ['relationshipsSoftwaresUser']);
        }
136 137 138 139 140
    }

    /**
     * Edit method
     *
141
     * @param string|null $id Relationships Softwares User id.
142
     * @return void Redirects on successful edit, renders view otherwise.
143
     * @throws NotFoundException When record not found.
144 145 146
     */
    public function edit($id = null)
    {
147
        $relationshipsSoftwaresUser = $this->RelationshipsSoftwaresUsers->get(
148 149
            $id,
            [
150
                'contain' => []
151 152
            ]
        );
153
        if ($this->request->is(['patch', 'post', 'put']) && !$this->request->is('json')) {
154 155
            $relationshipsSoftwaresUser = $this->RelationshipsSoftwaresUsers->patchEntity(
                $relationshipsSoftwaresUser,
156
                $this->request->getData()
157
            );
158 159 160 161 162 163
            if ($this->RelationshipsSoftwaresUsers->save($relationshipsSoftwaresUser)) {
                $this->Flash->success(__('The relationships softwares user has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The relationships softwares user could not be saved. Please, try again.'));
            }
164

165 166 167 168
            $softwares = $this->RelationshipsSoftwaresUsers->Softwares->find('list', ['limit' => 200]);
            $users = $this->RelationshipsSoftwaresUsers->Users->find('list', ['limit' => 200]);
            $relationships = $this->RelationshipsSoftwaresUsers->Relationships->find('list', ['limit' => 200]);
            $this->set(compact('relationshipsSoftwaresUser', 'softwares', 'users', 'relationships'));
169
            $this->set('_serialize', ['relationshipsSoftwaresUser']);
170
        } elseif ($this->request->is(['patch', 'post', 'put']) && $this->request->is('json')) {
171 172
            $relationshipsSoftwaresUser = $this->RelationshipsSoftwaresUsers->patchEntity(
                $relationshipsSoftwaresUser,
173
                $this->request->getData()
174
            );
175
            if ($this->RelationshipsSoftwaresUsers->save($relationshipsSoftwaresUser)) {
176
                $message = "Success";
177 178 179
            } else {
                $message = "Error";
            }
180 181
            $this->set(
                [
182 183 184
                    'message' => $message,
                    'relationshipsSoftwaresUser' => $relationshipsSoftwaresUser,
                    '_serialize' => ['message', 'relationshipsSoftwaresUser']
185 186
                ]
            );
187
        } else {//Pour le template avant le post des données.
188 189 190 191
            $softwares = $this->RelationshipsSoftwaresUsers->Softwares->find('list', ['limit' => 200]);
            $users = $this->RelationshipsSoftwaresUsers->Users->find('list', ['limit' => 200]);
            $relationships = $this->RelationshipsSoftwaresUsers->Relationships->find('list', ['limit' => 200]);
            $this->set(compact('relationshipsSoftwaresUser', 'softwares', 'users', 'relationships'));
192
            $this->set('_serialize', ['relationshipsSoftwaresUser']);
193 194 195 196 197 198
        }
    }

    /**
     * Delete method
     *
199
     * @param string|null $id Relationships Softwares User id.
200 201
     * @return Response|null Redirects to index.
     * @throws NotFoundException When record not found.
202 203 204 205 206 207 208 209 210 211 212 213 214
     */
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $relationshipsSoftwaresUser = $this->RelationshipsSoftwaresUsers->get($id);
        if ($this->RelationshipsSoftwaresUsers->delete($relationshipsSoftwaresUser)) {
            $this->Flash->success(__('The relationships softwares user has been deleted.'));
        } else {
            $this->Flash->error(__('The relationships softwares user could not be deleted. Please, try again.'));
        }
        return $this->redirect(['action' => 'index']);
    }
}