Blame view

src/Controller/CategoriesController.php 5.83 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
29
30
31
32
33
34
35
36
37
38
39
	/**
	 * @param $user
	 *
	 * Give authorization for categories
	 *
	 * @return boolean
	 */
	public function isAuthorized($user)
	{
		$configuration = TableRegistry::get('Configurations')->find()->where(['id =' => 1])->first();
		$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;
		
		if (in_array($action, ['getBySurCategorie', 'getAll', 'view', 'index'])) {
			return true;
		}
	
08d8dbb0   Alexandre   Version: 2.4.2.14
40
		if($this->userHasRole('Administration Plus')) {
94c77ea4   Alexandre   Version: 2.4.2.10
41
			if($action != 'delete') return true;
04a6b875   Alexandre   Version: 2.4.2.0
42
43
44
45
46
		}
		
		return false;
	}
	
6c4edfa3   Alexandre   First Commit LabI...
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
    /**
     * 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...
75
76
        $sousCategories = TableRegistry::get('SousCategories')->find('all')->where(['categorie_id =' => $id]);
        $this->set('sousCategories', $sousCategories);
19798ef9   Alexandre   Mode_install, maj...
77
        
3e24b686   Alexandre   Version: 2.4.2.20
78
79
80
        $materiels = TableRegistry::get('Materiels')->find('all')->where(['categorie_id =' => $id]);
        $this->set('materiels', $materiels);
        
6c4edfa3   Alexandre   First Commit LabI...
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
        $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...
96
                $this->Flash->success(__('La catégorie a bien été ajouté.'));
d40786f0   Alexandre   Version: 2.4.2.3
97
                return $this->redirect(['action' => 'view', $category->id]);
6c4edfa3   Alexandre   First Commit LabI...
98
            } else {
d6960faf   Alexandre   Migration de plus...
99
                $this->Flash->error(__('La catégorie n\'a pas pu être ajouté.'));
6c4edfa3   Alexandre   First Commit LabI...
100
101
            }
        }
19798ef9   Alexandre   Mode_install, maj...
102
103
        $surCategories = $this->Categories->SurCategories->find('list', [ 'keyField' => 'id', 'valueField' => 'nom']);
        
6c4edfa3   Alexandre   First Commit LabI...
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
        $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...
123
                $this->Flash->success(__('La catégorie a bien été édité.'));
d40786f0   Alexandre   Version: 2.4.2.3
124
                return $this->redirect(['action' => 'view', $id]);
6c4edfa3   Alexandre   First Commit LabI...
125
            } else {
d6960faf   Alexandre   Migration de plus...
126
                $this->Flash->error(__('La catégorie n\'a pas pu être édité.'));
6c4edfa3   Alexandre   First Commit LabI...
127
128
            }
        }
19798ef9   Alexandre   Mode_install, maj...
129
130
        $surCategories = $this->Categories->SurCategories->find('list', [ 'keyField' => 'id', 'valueField' => 'nom']);
       
6c4edfa3   Alexandre   First Commit LabI...
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
        $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...
147
            $this->Flash->success(__('La catégorie a bien été supprimé.'));
6c4edfa3   Alexandre   First Commit LabI...
148
        } else {
d6960faf   Alexandre   Migration de plus...
149
            $this->Flash->error(__('La catégorie n\'a pas pu être supprimé.'));
6c4edfa3   Alexandre   First Commit LabI...
150
151
152
        }
        return $this->redirect(['action' => 'index']);
    }
758a84af   Alexandre   Version: 2.2.4.0
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
    
    
    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...
181
}