CategoriesController.php
8.99 KB
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
42
43
44
45
46
47
48
49
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\ORM\TableRegistry;
/**
* Categories Controller
*
* @property \App\Model\Table\CategoriesTable $Categories
*/
class CategoriesController extends AppController
{
// Jolis labels pour chaque champ de l'entité
//@Override
protected $nice_field_labels = [
'sur_categorie_id' => 'Domaine',
];
// Genre
public $is_masculin = false;
// Nom pluriel affichable pour cette entité
//@Override
public function getTypeNamePlural($alias_controller_name=null) { return 'catégories'; }
//@deprecated
public function getArticle()
{
return "La";
}
/*
* @Override
*
* Initialisation des autorisations pour les actions spécifiques à ce controleur
*
*/
protected function setAuthorizations() {
// Action 'getFromCategorie'
$this->setAuthorizationsForAction('getBySurCategorie', 0);
// Action 'getAll'
$this->setAuthorizationsForAction('getAll', 0);
}
/**
*
* @param
* $user
* @return boolean Give authorization for categories
*
*/
//public function isAuthorized($user)
/*
public function isAuthorized($user,
$action = null, $id=null, $role=null, $userCname=null) {
// $configuration = $this->confLabinvent;
// $role = TableRegistry::get('Users')->find()->where(['username' => $user[$configuration->authentificationType_ldap][0]])->first()['role'];
// $action = $this->request->getAttribute('params')['action'];
$action = $this->getActionPassed();
$role = $this->getUserRole($user);
// Actions autorisées pour tous les profils
// if (in_array($action, ['getBySurCategorie', 'getAll', 'view', 'index'])) {
if (in_array($action, [
'getBySurCategorie',
'getAll'
])) {
return true;
}
/S
* // Super-Admin peut accéder à chaque action
* if($role == 'Super Administrateur') return true;
S/
/S
* // Administration peut ajouter, supprimer ou modifier une categorie
* //if($role == 'Administration' && in_array($action,['add','delete','edit'])) return true;
* if( in_array($action,['add','delete','edit'])) {
* //if ($role == 'Administration') return true;
* if ($this->USER_IS_ADMIN_AT_LEAST()) return true;
* // Les autres n'y ont pas accès
* return false;
* }
S/
// Par défaut
// return false;
// return parent::isAuthorized($user);
return $this->isAuthorizedCommons($user);
}
*/
/**
* Index method
*
* @return \Cake\Network\Response|null
*/
public function index()
{
$this->index_generic(
'catégories',
[
'nom'=>'Nom',
'sur_categorie_id' => 'Domaine',
]
);
/*
$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) {
$child_entity_types = [
'SousCategories',
'Materiels'
];
//return parent::view($id, $associated_entity_types);
//return $this->view_generic($id, $associated_entity_types, 'SurCategories');
//return $this->view_generic($id, $child_entity_types, ['SurCategories']);
return $this->view_generic($id, $child_entity_types, 'sous-categories', ' une sous-catégorie');
/*
$category = $this->Categories->get($id, [
'contain' => [
'SurCategories'
]
]);
$sousCategories = TableRegistry::get('SousCategories')->find('all')->where([
'categorie_id =' => $id
]);
$this->set('sousCategories', $sousCategories);
$materiels = TableRegistry::get('Materiels')->find('all')->where([
'categorie_id =' => $id
]);
$this->set('materiels', $materiels);
$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()
public function add($parent_id=null)
{
//debug($parent_id);
$category = $this->Categories->newEntity();
if ($this->request->is('post')) {
$category = $this->Categories->patchEntity($category, $this->request->getData());
if ($this->Categories->save($category)) {
$this->Flash->success(__('La catégorie a bien été ajouté.'));
return $this->redirect([
'action' => 'view',
$category->id
]);
} else {
$this->Flash->error(__('La catégorie n\'a pas pu être ajouté.'));
}
}
$surCategories = $this->Categories->SurCategories->find('list', [
'keyField' => 'id',
'valueField' => 'nom'
]);
$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->getData());
if ($this->Categories->save($category)) {
$this->Flash->success(__('La catégorie a bien été édité.'));
return $this->redirect([
'action' => 'view',
$id
]);
} else {
$this->Flash->error(__('La catégorie n\'a pas pu être édité.'));
}
}
$surCategories = $this->Categories->SurCategories->find('list', [
'keyField' => 'id',
'valueField' => 'nom'
]);
$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)) {
$this->Flash->success(__('La catégorie a bien été supprimé.'));
} else {
$this->Flash->error(__('La catégorie n\'a pas pu être supprimé.'));
}
return $this->redirect([
'action' => 'index'
]);
}
public function getBySurCategorie()
{
if ($this->request->getData('s_sur_categorie_id') !== null)
$sur_categorie_id = $this->request->getData('s_sur_categorie_id');
else
$sur_categorie_id = $this->request->getData('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';
}
}