Blame view

src/Controller/CategoriesController.php 5.93 KB
6c4edfa3   Alexandre   First Commit LabI...
1
2
3
4
<?php
namespace App\Controller;

use App\Controller\AppController;
8b709355   Alexandre   Affichage du cont...
5
use Cake\ORM\TableRegistry;
6c4edfa3   Alexandre   First Commit LabI...
6

63a22db6   Alexandre   Version: 2.2.5.0
7

6c4edfa3   Alexandre   First Commit LabI...
8
9
10
11
12
13
14
15
/**
 * Categories Controller
 *
 * @property \App\Model\Table\CategoriesTable $Categories
 */
class CategoriesController extends AppController
{

63a22db6   Alexandre   Version: 2.2.5.0
16
17
18
19
	protected function getArticle() {
		return "La";
	}
	
04a6b875   Alexandre   Version: 2.4.2.0
20
21
22
23
24
25
26
27
28
	/**
	 * @param $user
	 *
	 * Give authorization for categories
	 *
	 * @return boolean
	 */
	public function isAuthorized($user)
	{
aaf7558a   Thibaud Ajas   modifications de ...
29
		$configuration = $this->confLabinvent;
04a6b875   Alexandre   Version: 2.4.2.0
30
31
32
33
34
35
		$role = TableRegistry::get('Users')->find()->where(['username' => $user[$configuration->authentificationType_ldap][0]])->first()['role'];
		$action = $this->request->params['action'];
		
		// Super-Admin peut accéder à chaque action
    	if($role == 'Super Administrateur') return true;
		
243c3483   Alexis Proust   mise a jour fichier
36
37
38
		// Administration peut ajouter, supprimer ou modifier une categorie
		if($role == 'Administration' && in_array($action,['add','delete','edit'])) return true;
		
04a6b875   Alexandre   Version: 2.4.2.0
39
40
41
42
		if (in_array($action, ['getBySurCategorie', 'getAll', 'view', 'index'])) {
			return true;
		}
	
08d8dbb0   Alexandre   Version: 2.4.2.14
43
		if($this->userHasRole('Administration Plus')) {
94c77ea4   Alexandre   Version: 2.4.2.10
44
			if($action != 'delete') return true;
04a6b875   Alexandre   Version: 2.4.2.0
45
46
47
48
49
		}
		
		return false;
	}
	
6c4edfa3   Alexandre   First Commit LabI...
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    /**
     * 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']
        ]);
8b709355   Alexandre   Affichage du cont...
78
79
        $sousCategories = TableRegistry::get('SousCategories')->find('all')->where(['categorie_id =' => $id]);
        $this->set('sousCategories', $sousCategories);
19798ef9   Alexandre   Mode_install, maj...
80
        
3e24b686   Alexandre   Version: 2.4.2.20
81
82
83
        $materiels = TableRegistry::get('Materiels')->find('all')->where(['categorie_id =' => $id]);
        $this->set('materiels', $materiels);
        
6c4edfa3   Alexandre   First Commit LabI...
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
        $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...
99
                $this->Flash->success(__('La catégorie a bien été ajouté.'));
d40786f0   Alexandre   Version: 2.4.2.3
100
                return $this->redirect(['action' => 'view', $category->id]);
6c4edfa3   Alexandre   First Commit LabI...
101
            } else {
d6960faf   Alexandre   Migration de plus...
102
                $this->Flash->error(__('La catégorie n\'a pas pu être ajouté.'));
6c4edfa3   Alexandre   First Commit LabI...
103
104
            }
        }
19798ef9   Alexandre   Mode_install, maj...
105
106
        $surCategories = $this->Categories->SurCategories->find('list', [ 'keyField' => 'id', 'valueField' => 'nom']);
        
6c4edfa3   Alexandre   First Commit LabI...
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
        $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...
126
                $this->Flash->success(__('La catégorie a bien été édité.'));
d40786f0   Alexandre   Version: 2.4.2.3
127
                return $this->redirect(['action' => 'view', $id]);
6c4edfa3   Alexandre   First Commit LabI...
128
            } else {
d6960faf   Alexandre   Migration de plus...
129
                $this->Flash->error(__('La catégorie n\'a pas pu être édité.'));
6c4edfa3   Alexandre   First Commit LabI...
130
131
            }
        }
19798ef9   Alexandre   Mode_install, maj...
132
133
        $surCategories = $this->Categories->SurCategories->find('list', [ 'keyField' => 'id', 'valueField' => 'nom']);
       
6c4edfa3   Alexandre   First Commit LabI...
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
        $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...
150
            $this->Flash->success(__('La catégorie a bien été supprimé.'));
6c4edfa3   Alexandre   First Commit LabI...
151
        } else {
d6960faf   Alexandre   Migration de plus...
152
            $this->Flash->error(__('La catégorie n\'a pas pu être supprimé.'));
6c4edfa3   Alexandre   First Commit LabI...
153
154
155
        }
        return $this->redirect(['action' => 'index']);
    }
758a84af   Alexandre   Version: 2.2.4.0
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
183
    
    
    public function getBySurCategorie() {
    	if (isset($this->request->data['s_sur_categorie_id']))
    		$sur_categorie_id = $this->request->data['s_sur_categorie_id'];
    		else
    			$sur_categorie_id = $this->request->data['sur_categorie_id'];
    			$categories = $this->Categories->find('list', [
    					'conditions' => ['Categories.sur_categorie_id' => $sur_categorie_id],
    					'order' => ['Categories.nom'],
    					'recursive' => -1,
    					'keyField' => 'id', 'valueField' => 'nom'
    			]);
    			$this->set('categories',$categories);
    			$this->viewBuilder()->layout = 'ajax';
    }
    
    // called from view ADD/EDIT (scaffold.form.ctp) on domain select change (javascript event)
    public function getAll() {
    	$categories = $this->Categories->find('list', [
    			'order' => ['Categories.nom'],
    			'recursive' => -1,
    			'keyField' => 'id', 'valueField' => 'nom'
    	]);
    	$this->set('categories',$categories);
    	$this->viewBuilder()->layout = 'ajax';
    }
    
6c4edfa3   Alexandre   First Commit LabI...
184
}