RelationshipsController.php 4.19 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\RelationshipsTable;
use Cake\Network\Exception\NotFoundException;
use Cake\Network\Response;
9

10 11 12
/**
 * Relationships Controller
 *
13
 * @property RelationshipsTable $Relationships
14 15 16 17 18 19 20 21 22 23 24
 */
class RelationshipsController extends AppController
{

    /**
     * Index method
     *
     * @return void
     */
    public function index()
    {
25

26 27 28
        $this->paginate = [
            'contain' => ['RelationshipTypes']
        ];
29

30 31 32 33 34 35 36
        $this->set('relationships', $this->paginate($this->Relationships));
        $this->set('_serialize', ['relationships']);
    }

    /**
     * View method
     *
37
     * @param string|null $id Relationship id.
38
     * @return void
39
     * @throws NotFoundException When record not found.
40 41 42
     */
    public function view($id = null)
    {
43
        $relationship = $this->Relationships->get(
44 45
            $id,
            [
46
                'contain' => ['RelationshipTypes', 'Softwares', 'Users']
47 48
            ]
        );
49 50 51 52 53 54 55 56 57 58 59 60 61
        $this->set('relationship', $relationship);
        $this->set('_serialize', ['relationship']);
    }

    /**
     * Add method
     *
     * @return void Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $relationship = $this->Relationships->newEntity();
        if ($this->request->is('post')) {
62
            $relationship = $this->Relationships->patchEntity($relationship, $this->request->getData());
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
            if ($this->Relationships->save($relationship)) {
                $this->Flash->success(__('The relationship has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The relationship could not be saved. Please, try again.'));
            }
        }
        $relationshipTypes = $this->Relationships->RelationshipTypes->find('list', ['limit' => 200]);
        $softwares = $this->Relationships->Softwares->find('list', ['limit' => 200]);
        $users = $this->Relationships->Users->find('list', ['limit' => 200]);
        $this->set(compact('relationship', 'relationshipTypes', 'softwares', 'users'));
        $this->set('_serialize', ['relationship']);
    }

    /**
     * Edit method
     *
80
     * @param string|null $id Relationship id.
81
     * @return void Redirects on successful edit, renders view otherwise.
82
     * @throws NotFoundException When record not found.
83 84 85
     */
    public function edit($id = null)
    {
86
        $relationship = $this->Relationships->get(
87 88
            $id,
            [
89
                'contain' => ['Softwares', 'Users']
90 91
            ]
        );
92
        if ($this->request->is(['patch', 'post', 'put'])) {
93
            $relationship = $this->Relationships->patchEntity($relationship, $this->request->getData());
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
            if ($this->Relationships->save($relationship)) {
                $this->Flash->success(__('The relationship has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The relationship could not be saved. Please, try again.'));
            }
        }
        $relationshipTypes = $this->Relationships->RelationshipTypes->find('list', ['limit' => 200]);
        $softwares = $this->Relationships->Softwares->find('list', ['limit' => 200]);
        $users = $this->Relationships->Users->find('list', ['limit' => 200]);
        $this->set(compact('relationship', 'relationshipTypes', 'softwares', 'users'));
        $this->set('_serialize', ['relationship']);
    }

    /**
     * Delete method
     *
111
     * @param string|null $id Relationship id.
112 113
     * @return Response|null Redirects to index.
     * @throws NotFoundException When record not found.
114 115 116 117 118 119 120 121 122 123 124 125 126
     */
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $relationship = $this->Relationships->get($id);
        if ($this->Relationships->delete($relationship)) {
            $this->Flash->success(__('The relationship has been deleted.'));
        } else {
            $this->Flash->error(__('The relationship could not be deleted. Please, try again.'));
        }
        return $this->redirect(['action' => 'index']);
    }
}