Blame view

src/Controller/CategoriesController.php 3.82 KB
6c4edfa3   Alexandre   First Commit LabI...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
namespace App\Controller;

use App\Controller\AppController;

/**
 * Categories Controller
 *
 * @property \App\Model\Table\CategoriesTable $Categories
 */
class CategoriesController extends AppController
{

    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index()
    {
        $this->paginate = [
            'contain' => ['SurCategories']
        ];
        $categories = $this->paginate($this->Categories);

        $this->set(compact('categories'));
        $this->set('_serialize', ['categories']);
    }

    /**
     * View method
     *
     * @param string|null $id Category id.
     * @return \Cake\Network\Response|null
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
        $category = $this->Categories->get($id, [
            'contain' => ['SurCategories']
        ]);
19798ef9   Alexandre   Mode_install, maj...
42
43
44
        //$sousCategories = $this->Categories->SousCategories->find('list');
        //$this->set('sousCategories', $sousCategories);
        
6c4edfa3   Alexandre   First Commit LabI...
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
        $this->set('category', $category);
        $this->set('_serialize', ['category']);
    }

    /**
     * Add method
     *
     * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $category = $this->Categories->newEntity();
        if ($this->request->is('post')) {
            $category = $this->Categories->patchEntity($category, $this->request->data);
            if ($this->Categories->save($category)) {
d6960faf   Alexandre   Migration de plus...
60
                $this->Flash->success(__('La catégorie a bien été ajouté.'));
6c4edfa3   Alexandre   First Commit LabI...
61
62
                return $this->redirect(['action' => 'index']);
            } else {
d6960faf   Alexandre   Migration de plus...
63
                $this->Flash->error(__('La catégorie n\'a pas pu être ajouté.'));
6c4edfa3   Alexandre   First Commit LabI...
64
65
            }
        }
19798ef9   Alexandre   Mode_install, maj...
66
67
        $surCategories = $this->Categories->SurCategories->find('list', [ 'keyField' => 'id', 'valueField' => 'nom']);
        
6c4edfa3   Alexandre   First Commit LabI...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        $this->set(compact('category', 'surCategories'));
        $this->set('_serialize', ['category']);
    }

    /**
     * Edit method
     *
     * @param string|null $id Category id.
     * @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise.
     * @throws \Cake\Network\Exception\NotFoundException When record not found.
     */
    public function edit($id = null)
    {
        $category = $this->Categories->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $category = $this->Categories->patchEntity($category, $this->request->data);
            if ($this->Categories->save($category)) {
d6960faf   Alexandre   Migration de plus...
87
                $this->Flash->success(__('La catégorie a bien été édité.'));
6c4edfa3   Alexandre   First Commit LabI...
88
89
                return $this->redirect(['action' => 'index']);
            } else {
d6960faf   Alexandre   Migration de plus...
90
                $this->Flash->error(__('La catégorie n\'a pas pu être édité.'));
6c4edfa3   Alexandre   First Commit LabI...
91
92
            }
        }
19798ef9   Alexandre   Mode_install, maj...
93
94
        $surCategories = $this->Categories->SurCategories->find('list', [ 'keyField' => 'id', 'valueField' => 'nom']);
       
6c4edfa3   Alexandre   First Commit LabI...
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
        $this->set(compact('category', 'surCategories'));
        $this->set('_serialize', ['category']);
    }

    /**
     * Delete method
     *
     * @param string|null $id Category id.
     * @return \Cake\Network\Response|null Redirects to index.
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function delete($id = null)
    {
        $this->request->allowMethod(['post', 'delete']);
        $category = $this->Categories->get($id);
        if ($this->Categories->delete($category)) {
d6960faf   Alexandre   Migration de plus...
111
            $this->Flash->success(__('La catégorie a bien été supprimé.'));
6c4edfa3   Alexandre   First Commit LabI...
112
        } else {
d6960faf   Alexandre   Migration de plus...
113
            $this->Flash->error(__('La catégorie n\'a pas pu être supprimé.'));
6c4edfa3   Alexandre   First Commit LabI...
114
115
116
117
        }
        return $this->redirect(['action' => 'index']);
    }
}