Blame view

src/Controller/CategoriesController.php 5.67 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
40
41
42
43
44
45
46
	/**
	 * @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;
		}
	
		if(in_array($role, ['Administration', 'Administration Plus'])) {
			if(action != 'delete') return true;
		}
		
		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
        
6c4edfa3   Alexandre   First Commit LabI...
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
        $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...
93
                $this->Flash->success(__('La catégorie a bien été ajouté.'));
6c4edfa3   Alexandre   First Commit LabI...
94
95
                return $this->redirect(['action' => 'index']);
            } else {
d6960faf   Alexandre   Migration de plus...
96
                $this->Flash->error(__('La catégorie n\'a pas pu être ajouté.'));
6c4edfa3   Alexandre   First Commit LabI...
97
98
            }
        }
19798ef9   Alexandre   Mode_install, maj...
99
100
        $surCategories = $this->Categories->SurCategories->find('list', [ 'keyField' => 'id', 'valueField' => 'nom']);
        
6c4edfa3   Alexandre   First Commit LabI...
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
        $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...
120
                $this->Flash->success(__('La catégorie a bien été édité.'));
6c4edfa3   Alexandre   First Commit LabI...
121
122
                return $this->redirect(['action' => 'index']);
            } else {
d6960faf   Alexandre   Migration de plus...
123
                $this->Flash->error(__('La catégorie n\'a pas pu être édité.'));
6c4edfa3   Alexandre   First Commit LabI...
124
125
            }
        }
19798ef9   Alexandre   Mode_install, maj...
126
127
        $surCategories = $this->Categories->SurCategories->find('list', [ 'keyField' => 'id', 'valueField' => 'nom']);
       
6c4edfa3   Alexandre   First Commit LabI...
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
        $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...
144
            $this->Flash->success(__('La catégorie a bien été supprimé.'));
6c4edfa3   Alexandre   First Commit LabI...
145
        } else {
d6960faf   Alexandre   Migration de plus...
146
            $this->Flash->error(__('La catégorie n\'a pas pu être supprimé.'));
6c4edfa3   Alexandre   First Commit LabI...
147
148
149
        }
        return $this->redirect(['action' => 'index']);
    }
758a84af   Alexandre   Version: 2.2.4.0
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
    
    
    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...
178
}