Blame view

src/Controller/CategoriesController.php 6.38 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
	/**
	 * @param $user
f084c88b   Etienne Pallier   Gros bugfix + sim...
22
	 * @return boolean
04a6b875   Alexandre   Version: 2.4.2.0
23
24
25
	 *
	 * Give authorization for categories
	 *
04a6b875   Alexandre   Version: 2.4.2.0
26
27
28
	 */
	public function isAuthorized($user)
	{
f084c88b   Etienne Pallier   Gros bugfix + sim...
29
30
31
32
33
		//$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);
04a6b875   Alexandre   Version: 2.4.2.0
34
		
f084c88b   Etienne Pallier   Gros bugfix + sim...
35
36
37
38
39
40
41
		// Actions autorisées pour tous les profils
		//if (in_array($action, ['getBySurCategorie', 'getAll', 'view', 'index'])) {
		if (in_array($action, ['getBySurCategorie', 'getAll'])) {
		        return true;
		}

		/*
04a6b875   Alexandre   Version: 2.4.2.0
42
		// Super-Admin peut accéder à chaque action
f084c88b   Etienne Pallier   Gros bugfix + sim...
43
44
        	if($role == 'Super Administrateur') return true;
        	*/
04a6b875   Alexandre   Version: 2.4.2.0
45
		
243c3483   Alexis Proust   mise a jour fichier
46
		
f084c88b   Etienne Pallier   Gros bugfix + sim...
47
48
49
50
51
52
53
54
		/*
		// 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;
04a6b875   Alexandre   Version: 2.4.2.0
55
		}
f084c88b   Etienne Pallier   Gros bugfix + sim...
56
57
58
59
60
		*/
		
		// Par défaut
		//return false;
		//return parent::isAuthorized($user);
04a6b875   Alexandre   Version: 2.4.2.0
61
		
b7f0fc55   Etienne Pallier   bugfix du bugfix...
62
		return $this->isAuthorizedCommons($user);
04a6b875   Alexandre   Version: 2.4.2.0
63
64
	}
	
6c4edfa3   Alexandre   First Commit LabI...
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
    /**
     * 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...
93
94
        $sousCategories = TableRegistry::get('SousCategories')->find('all')->where(['categorie_id =' => $id]);
        $this->set('sousCategories', $sousCategories);
19798ef9   Alexandre   Mode_install, maj...
95
        
3e24b686   Alexandre   Version: 2.4.2.20
96
97
98
        $materiels = TableRegistry::get('Materiels')->find('all')->where(['categorie_id =' => $id]);
        $this->set('materiels', $materiels);
        
6c4edfa3   Alexandre   First Commit LabI...
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
        $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...
114
                $this->Flash->success(__('La catégorie a bien été ajouté.'));
d40786f0   Alexandre   Version: 2.4.2.3
115
                return $this->redirect(['action' => 'view', $category->id]);
6c4edfa3   Alexandre   First Commit LabI...
116
            } else {
d6960faf   Alexandre   Migration de plus...
117
                $this->Flash->error(__('La catégorie n\'a pas pu être ajouté.'));
6c4edfa3   Alexandre   First Commit LabI...
118
119
            }
        }
19798ef9   Alexandre   Mode_install, maj...
120
121
        $surCategories = $this->Categories->SurCategories->find('list', [ 'keyField' => 'id', 'valueField' => 'nom']);
        
6c4edfa3   Alexandre   First Commit LabI...
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
        $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...
141
                $this->Flash->success(__('La catégorie a bien été édité.'));
d40786f0   Alexandre   Version: 2.4.2.3
142
                return $this->redirect(['action' => 'view', $id]);
6c4edfa3   Alexandre   First Commit LabI...
143
            } else {
d6960faf   Alexandre   Migration de plus...
144
                $this->Flash->error(__('La catégorie n\'a pas pu être édité.'));
6c4edfa3   Alexandre   First Commit LabI...
145
146
            }
        }
19798ef9   Alexandre   Mode_install, maj...
147
148
        $surCategories = $this->Categories->SurCategories->find('list', [ 'keyField' => 'id', 'valueField' => 'nom']);
       
6c4edfa3   Alexandre   First Commit LabI...
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
        $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...
165
            $this->Flash->success(__('La catégorie a bien été supprimé.'));
6c4edfa3   Alexandre   First Commit LabI...
166
        } else {
d6960faf   Alexandre   Migration de plus...
167
            $this->Flash->error(__('La catégorie n\'a pas pu être supprimé.'));
6c4edfa3   Alexandre   First Commit LabI...
168
169
170
        }
        return $this->redirect(['action' => 'index']);
    }
758a84af   Alexandre   Version: 2.2.4.0
171
172
173
    
    
    public function getBySurCategorie() {
a0fefb3d   Thibaud Ajas   bugfixes suite au...
174
175
176
177
178
179
180
181
182
183
184
185
        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';
758a84af   Alexandre   Version: 2.2.4.0
186
187
188
189
190
191
192
193
194
195
196
197
198
    }
    
    // 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...
199
}