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

3
namespace App\Controller\Api\V1;
4

Mickaël Pastor's avatar
CRUD  
Mickaël Pastor committed
5
use App\Controller\AppController;
6 7 8
use App\Model\Table\LicensesTable;
use Cake\Network\Exception\NotFoundException;
use Cake\Network\Response;
Mickaël Pastor's avatar
CRUD  
Mickaël Pastor committed
9 10 11 12

/**
 * Licenses Controller
 *
13
 * @property LicensesTable $Licenses
Mickaël Pastor's avatar
CRUD  
Mickaël Pastor committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 */
class LicensesController extends AppController
{

    /**
     * Index method
     *
     * @return void
     */
    public function index()
    {
        $this->paginate = [
            'contain' => ['LicenceTypes']
        ];
        $this->set('licenses', $this->paginate($this->Licenses));
        $this->set('_serialize', ['licenses']);
    }

    /**
     * View method
     *
35
     * @param string|null $id License id.
Mickaël Pastor's avatar
CRUD  
Mickaël Pastor committed
36
     * @return void
37
     * @throws NotFoundException When record not found.
Mickaël Pastor's avatar
CRUD  
Mickaël Pastor committed
38 39 40
     */
    public function view($id = null)
    {
41
        $license = $this->Licenses->get(
42 43
            $id,
            [
44
                'contain' => ['LicenceTypes']
45 46
            ]
        );
Mickaël Pastor's avatar
CRUD  
Mickaël Pastor committed
47 48 49 50 51 52 53 54 55 56 57 58 59
        $this->set('license', $license);
        $this->set('_serialize', ['license']);
    }

    /**
     * Add method
     *
     * @return void Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $license = $this->Licenses->newEntity();
        if ($this->request->is('post')) {
60
            $license = $this->Licenses->patchEntity($license, $this->request->getData());
Mickaël Pastor's avatar
CRUD  
Mickaël Pastor committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
            if ($this->Licenses->save($license)) {
                $this->Flash->success(__('The license has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The license could not be saved. Please, try again.'));
            }
        }
        $licenceTypes = $this->Licenses->LicenceTypes->find('list', ['limit' => 200]);
        $this->set(compact('license', 'licenceTypes'));
        $this->set('_serialize', ['license']);
    }

    /**
     * Edit method
     *
76
     * @param string|null $id License id.
Mickaël Pastor's avatar
CRUD  
Mickaël Pastor committed
77
     * @return void Redirects on successful edit, renders view otherwise.
78
     * @throws NotFoundException When record not found.
Mickaël Pastor's avatar
CRUD  
Mickaël Pastor committed
79 80 81
     */
    public function edit($id = null)
    {
82
        $license = $this->Licenses->get(
83 84
            $id,
            [
85
                'contain' => []
86 87
            ]
        );
Mickaël Pastor's avatar
CRUD  
Mickaël Pastor committed
88
        if ($this->request->is(['patch', 'post', 'put'])) {
89
            $license = $this->Licenses->patchEntity($license, $this->request->getData());
Mickaël Pastor's avatar
CRUD  
Mickaël Pastor committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
            if ($this->Licenses->save($license)) {
                $this->Flash->success(__('The license has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The license could not be saved. Please, try again.'));
            }
        }
        $licenceTypes = $this->Licenses->LicenceTypes->find('list', ['limit' => 200]);
        $this->set(compact('license', 'licenceTypes'));
        $this->set('_serialize', ['license']);
    }

    /**
     * Delete method
     *
105
     * @param string|null $id License id.
106 107
     * @return Response|null Redirects to index.
     * @throws NotFoundException When record not found.
Mickaël Pastor's avatar
CRUD  
Mickaël Pastor committed
108 109 110 111 112 113 114 115 116 117 118 119 120
     */
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $license = $this->Licenses->get($id);
        if ($this->Licenses->delete($license)) {
            $this->Flash->success(__('The license has been deleted.'));
        } else {
            $this->Flash->error(__('The license could not be deleted. Please, try again.'));
        }
        return $this->redirect(['action' => 'index']);
    }
}