Blame view

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

use App\Controller\AppController;
6bb4a0f7   Alexandre   Version: 2.2.2
5
6
use Cake\ORM\TableRegistry;
use FPDF;
6c4edfa3   Alexandre   First Commit LabI...
7
8
9
10
11

/**
 * Documents Controller
 *
 * @property \App\Model\Table\DocumentsTable $Documents
9b4da83b   Alexandre   Version: 2.5.0.0
12
 * @property \Cake\ORM\Association\BelongsTo $TypeDocuments
6c4edfa3   Alexandre   First Commit LabI...
13
14
15
16
 */
class DocumentsController extends AppController
{

04a6b875   Alexandre   Version: 2.4.2.0
17
18
19
20
21
22
23
24
25
	/**
	 * @param $user
	 *
	 * Give authorization for documents
	 *
	 * @return boolean
	 */
	public function isAuthorized($user)
	{
2dc3932b   Alexandre   Version: 2.4.2.9
26
27
28
29
		$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'];
		
9b4da83b   Alexandre   Version: 2.5.0.0
30
31
32
		// Super-Admin peut accéder à chaque action
		if($role == 'Super Administrateur') return true;
		
04a6b875   Alexandre   Version: 2.4.2.0
33
		if (in_array($action, ['admission', 'sortie'])) {
8b90af53   Alexandre   Version: 2.4.2.12
34
			if ($this->userHasRole('Administration')) {
04a6b875   Alexandre   Version: 2.4.2.0
35
36
37
				return true;
			}
		}
4dae83a2   Alexandre   Version: 2.5.1.0
38
		
f4e6dc02   Alexandre   Version: 2.5.2.0
39
		if(in_array($action, ['delete', 'edit'])) {
4dae83a2   Alexandre   Version: 2.5.1.0
40
41
42
			if ($this->userHasRole('Administration')) {
				return true;
			}
f4e6dc02   Alexandre   Version: 2.5.2.0
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
			$u = TableRegistry::get('Users')->find()->where(['username' => $user[$configuration->authentificationType_ldap][0]])->first();
			
			$doc = $this->Documents->get((int)$this->request->params['pass'][0]);
			
			$id = $doc->get('materiel_id');
			if(empty($id)) {
				$id = $doc->get('suivi_id');
				$suiviTable = TableRegistry::get('Suivis');
				if ($role == 'Responsable') {
					return ($suiviTable->exists(['id' => $id, 'groupes_metier_id' => $u['groupes_metier_id']]) || $suiviTable->exists(['id' => $id, 'groupes_thematique_id' => $u['groupe_thematique_id']]));
				}
				if ($role == 'Utilisateur') {
					return $suiviTable->exists(['id' => $id, 'nom_createur' => $user['givenname'][0].' '.$user['sn'][0]]);
				}
			} else {
				$materielTable = TableRegistry::get('Materiels');
				if ($role == 'Responsable') {
					return ($materielTable->exists(['id' => $id, 'groupes_metier_id' => $u['groupes_metier_id']]) || $materielTable->exists(['id' => $id, 'groupes_thematique_id' => $u['groupe_thematique_id']]));
				}
				if ($role == 'Utilisateur') {
					return ($materielTable->exists(['id' => $id, 'nom_createur' => $user['givenname'][0].' '.$user['sn'][0]]) || $materielTable->exists(['id' => $id, 'nom_responsable' => $user['givenname'][0].' '.$user['sn'][0]]));
				}		
			}	
4dae83a2   Alexandre   Version: 2.5.1.0
66
		}
9b4da83b   Alexandre   Version: 2.5.0.0
67
68
69

		//Pour tout le monde
		if (in_array($action, ['view', 'add', 'ficheMateriel'])) return true;
4dae83a2   Alexandre   Version: 2.5.1.0
70
71
		
		return false;
04a6b875   Alexandre   Version: 2.4.2.0
72
73
	}
	
6c4edfa3   Alexandre   First Commit LabI...
74
75
76
77
78
79
80
    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index()
    {
9b4da83b   Alexandre   Version: 2.5.0.0
81
82
83
    	$this->paginate = [
    			'contain' => ['TypeDocuments'],
    	];
6c4edfa3   Alexandre   First Commit LabI...
84
85
        $documents = $this->paginate($this->Documents);

4fd23929   Alexandre   Version: 2.5.0
86
87
88
        $materiel = $this->Documents->Materiels;
        
        $this->set(compact('documents', 'materiel'));
6c4edfa3   Alexandre   First Commit LabI...
89
90
91
92
93
94
95
96
97
98
99
100
        $this->set('_serialize', ['documents']);
    }

    /**
     * View method
     *
     * @param string|null $id Document id.
     * @return \Cake\Network\Response|null
     * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
     */
    public function view($id = null)
    {
9b4da83b   Alexandre   Version: 2.5.0.0
101
102
103
        $document = $this->Documents->get($id, [
            'contain' => ['TypeDocuments']
        ]);
6c4edfa3   Alexandre   First Commit LabI...
104

4fd23929   Alexandre   Version: 2.5.0
105
106
107
108
109
110
111
112
113
114
        $materiel = $this->Documents->Materiels->find()->where(['id =' => $document->materiel_id])->first();

        if($materiel != null) {
        	$this->set('materiel', $materiel);
        }
        else {
        	$suivi = $this->Documents->Suivis->find()->where(['id =' => $document->suivi_id])->first();
        	$this->set('suivi', $suivi);
        }
       
6c4edfa3   Alexandre   First Commit LabI...
115
116
117
118
119
120
121
122
123
124
125
126
        $this->set('document', $document);
        $this->set('_serialize', ['document']);
    }

    /**
     * Add method
     *
     * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
     */
    public function add()
    {
        $document = $this->Documents->newEntity();
f4e6dc02   Alexandre   Version: 2.5.2.0
127
        
6c4edfa3   Alexandre   First Commit LabI...
128
129
        if ($this->request->is('post')) {
            $document = $this->Documents->patchEntity($document, $this->request->data);
9b4da83b   Alexandre   Version: 2.5.0.0
130

f4e6dc02   Alexandre   Version: 2.5.2.0
131
132
            $nomType = $this->Documents->TypeDocuments->find()->where(['id =' => $document->type_document_id])->first()['nom'];      

4dae83a2   Alexandre   Version: 2.5.1.0
133
134
135
            if(isset($this->passedArgs[2]) && $this->passedArgs[2] == 'photo') {
            	$document->set('photo', 1);
            }
f4e6dc02   Alexandre   Version: 2.5.2.0
136

6c4edfa3   Alexandre   First Commit LabI...
137
            if ($this->Documents->save($document)) {
3601f4f5   Alexandre   Traduction messag...
138
                $this->Flash->success(__('Le fichier a bien été ajouté.'));
9b4da83b   Alexandre   Version: 2.5.0.0
139
                
f4e6dc02   Alexandre   Version: 2.5.2.0
140
141
142
143
144
145
                $id = $document->materiel_id;
                if(!empty($id)) {
					$materielTable = TableRegistry::get('Materiels');
                	$materiel = $materielTable->get($id);
                }
                
4dae83a2   Alexandre   Version: 2.5.1.0
146
                if(isset($this->passedArgs[2]) && $this->passedArgs[2] == 'photo') {
f4e6dc02   Alexandre   Version: 2.5.2.0
147

4dae83a2   Alexandre   Version: 2.5.1.0
148
149
150
151
152
153
154
155
156
157
                	$photoIdOld = $materiel->get('photo_id');
                	if(isset($photoIdOld)) {
                		$docOld = TableRegistry::get('Documents')->get($photoIdOld);
                		$this->Documents->delete($docOld);
                	}
                
                	$materiel->set('photo_id', $document->id);
                	$materielTable->save($materiel);
                }
                
3f179b66   Alexandre   Version: 2.5.4.2
158
                $userName = $this->LdapAuth->user('sn')[0].' '.$this->LdapAuth->user('givenname')[0];
f4e6dc02   Alexandre   Version: 2.5.2.0
159
160
                $userEmail = $this->LdapAuth->user('mail')[0];

9b4da83b   Alexandre   Version: 2.5.0.0
161
162
163
                $id = $document->materiel_id;
                if(empty($id)) {
                	$id = $document->suivi_id;
f4e6dc02   Alexandre   Version: 2.5.2.0
164
                	$this->sendEmailToManagementWith('[LabInvent] Ajout d\'un document', $userName.' (email = '.$userEmail.') a ajouté un document de type "'.$nomType.'" au suivi "Suivi '.$document->suivi_id.'".');
9b4da83b   Alexandre   Version: 2.5.0.0
165
166
                	return $this->redirect(['controller' => 'suivis', 'action' => 'view', $id]);
                } else {
f4e6dc02   Alexandre   Version: 2.5.2.0
167
                	$this->sendEmailToManagementWith('[LabInvent] Ajout d\'un document', $userName.' (email = '.$userEmail.') a ajouté un document de type "'.$nomType.'" au matériel "'.$materiel->designation.'" (id = '.$materiel->id.').');
9b4da83b   Alexandre   Version: 2.5.0.0
168
169
170
                	return $this->redirect(['controller' => 'materiels', 'action' => 'view', $id]);
                }
                
6c4edfa3   Alexandre   First Commit LabI...
171
            } else {
3601f4f5   Alexandre   Traduction messag...
172
                $this->Flash->error(__('Le fichier n\'a pas pu être ajouté.'));
6c4edfa3   Alexandre   First Commit LabI...
173
174
            }
        }
4fd23929   Alexandre   Version: 2.5.0
175
176
177
178
179
180
181
182
183
        
        if(isset($this->passedArgs[1]) && $this->passedArgs[1] == 'mat') {
        	$materiel = $this->Documents->Materiels->find('list', [ 'keyField' => 'id', 'valueField' => 'numero_laboratoire'])->where(['id =' => $this->passedArgs[0]]);
        	$this->set('materiel', $materiel);
        }
        else {
        	$suivi = $this->Documents->Suivis->find('list', [ 'keyField' => 'id', 'valueField' => 'id'])->where(['id =' => $this->passedArgs[0]]);
        	$this->set('suivi', $suivi);
        }
4dae83a2   Alexandre   Version: 2.5.1.0
184
185
186
        
        if(isset($this->passedArgs[2]) && $this->passedArgs[2] == 'photo') {
        	$this->set('photo', 1);
aca4ed9b   Alexandre   Version: 2.5.4.0
187
188
189
190
191
192
        	$typesD = $this->Documents->TypeDocuments->find('list', [ 'keyField' => 'id', 'valueField' => 'nom'])->where(['nom =' => 'Photo']);
        	$idType = $this->Documents->TypeDocuments->find()->where(['nom =' => 'Photo'])->first()['id'];
        	$this->set('idType', $idType);
        } else {
        	$typesD = $this->Documents->TypeDocuments->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'TypeDocuments.nom']);
        }  
4fd23929   Alexandre   Version: 2.5.0
193
        
4fd23929   Alexandre   Version: 2.5.0
194
        
9b4da83b   Alexandre   Version: 2.5.0.0
195
        $this->set(compact('document', 'typesD'));
6c4edfa3   Alexandre   First Commit LabI...
196
197
198
        $this->set('_serialize', ['document']);
    }

f4e6dc02   Alexandre   Version: 2.5.2.0
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
    /**
     * EDit method
     *
     * @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise.
     */
    public function edit($id = null)
    {
    	$document = $this->Documents->get($id, [
    			'contain' => []
    	]);
    
    	if ($this->request->is(['patch', 'post', 'put'])) {
    		$document = $this->Documents->patchEntity($document, $this->request->data);
    
    		if ($this->Documents->save($document)) {
    			$this->Flash->success(__('Le fichier a bien été edité.'));
        
    			$id = $document->materiel_id;
    			if(empty($id)) {
    				$id = $document->suivi_id;
    				return $this->redirect(['controller' => 'suivis', 'action' => 'view', $id]);
    			} else {
    				return $this->redirect(['controller' => 'materiels', 'action' => 'view', $id]);
    			}
    
    		} else {
    			$this->Flash->error(__('Le fichier n\'a pas pu être edité.'));
    		}
    	}
    	
    	$id = $document->materiel_id;
    	if(empty($id)) {
    		$id = $document->suivi_id;
    		$suivi = $this->Documents->Suivis->find('list', [ 'keyField' => 'id', 'valueField' => 'id'])->where(['id =' => $id]);
    		$this->set('suivi', $suivi);
    	} else {
    		$materiel = $this->Documents->Materiels->find('list', [ 'keyField' => 'id', 'valueField' => 'numero_laboratoire'])->where(['id =' => $id]);
    		$this->set('materiel', $materiel);
    	}
    
    	if($document->photo) {
    		$this->set('photo', 1);
    	}
    	 
    
    	$typesD = $this->Documents->TypeDocuments->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'TypeDocuments.nom']);
    
    	$this->set(compact('document', 'typesD'));
    	$this->set('_serialize', ['document']);
    }
9b4da83b   Alexandre   Version: 2.5.0.0
249

6c4edfa3   Alexandre   First Commit LabI...
250
251
252
253
254
255
256
257
258
259
260
    /**
     * Delete method
     *
     * @param string|null $id Document 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']);
        $document = $this->Documents->get($id);
9b4da83b   Alexandre   Version: 2.5.0.0
261
        
4dae83a2   Alexandre   Version: 2.5.1.0
262
263
264
265
266
267
268
269
        if($document->photo) {
        	$materielTable = TableRegistry::get('Materiels');
        	$materiel = $materielTable->get($document->materiel_id);
        	
        	$materiel->set('photo_id', null);
        	$materielTable->save($materiel);
        }
        
6c4edfa3   Alexandre   First Commit LabI...
270
        if ($this->Documents->delete($document)) {
3601f4f5   Alexandre   Traduction messag...
271
            $this->Flash->success(__('Le fichier a bien été supprimé.'));
6c4edfa3   Alexandre   First Commit LabI...
272
        } else {
3601f4f5   Alexandre   Traduction messag...
273
            $this->Flash->error(__('Le fichier n\'a pas pu être supprimé.'));
6c4edfa3   Alexandre   First Commit LabI...
274
        }
9b4da83b   Alexandre   Version: 2.5.0.0
275
276
277
278
279
280
281
282
        
        $id = $document->materiel_id;
        if(empty($id)) {
        	$id = $document->suivi_id;
        	return $this->redirect(['controller' => 'suivis', 'action' => 'view', $id]);
        } else {
        	return $this->redirect(['controller' => 'materiels', 'action' => 'view', $id]);
        }
6c4edfa3   Alexandre   First Commit LabI...
283
    }
6bb4a0f7   Alexandre   Version: 2.2.2
284
285
286
287
288
289
290
291
292
293
294
295
296
    
    
    public function sortie($labNumber) {   
    	$this->set('fpdf', new FPDF ( 'P', 'mm', 'A4' ));
    }
    
    public function admission($labNumber) {
    	
    	$this->set ( 'fpdf', new FPDF ( 'P', 'mm', 'A4' ) );
    	// Find the concerned materiel
    	$materiel = TableRegistry::get('Materiels')->find('all', ['conditions' => ['numero_laboratoire' => $labNumber]])->first(); // End find
    	
    	// Get the administration user name
d40786f0   Alexandre   Version: 2.4.2.3
297
    	$userName = $this->LdapAuth->user('username');
6bb4a0f7   Alexandre   Version: 2.2.2
298
299
    	$numeroLab = $materiel->numero_laboratoire;
    	$dateAcquisition = $materiel->date_acquisition;
e9a0cc56   Alexandre   Version: 2.4.6.0
300
    	$dateAcquisition = $dateAcquisition;
6bb4a0f7   Alexandre   Version: 2.2.2
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
    	$numeroCommande = $materiel->numero_commande;
    	$designation = $materiel->designation;
    	if(isset($materiel->organisme_id) && !empty($materiel->organisme_id)) {
    		$organisme = TableRegistry::get('Organismes')->find('all')->where(['id =' => $materiel->organisme_id])->first()->nom;
    	}
    	else {
    		$organisme = "";
    	}

    	$fournisseur = $materiel->fournisseur;
    	$numeroOrganisme = $materiel->numero_inventaire_organisme;
    	$eotp = $materiel->eotp;
    	$prix = $materiel->prix_ht;
    
    	// Build the data array
    	$TDoc = [
    			'organisme' => $organisme,
    			'numlab' => $numeroLab,
    			'designation' => $designation,
    			'dateAcquis' => $dateAcquisition,
    			'numCde' => $numeroCommande,
    			'fournisseur' => $fournisseur,
    			'eotp' => $eotp,
    			'prix' => $prix,
    			'numOrg' => $numeroOrganisme
    	];
    
    	// set the data for the document (accessible par $data dans le document)
    	$this->set ( 'data', $TDoc );
    
    } // End fct admission
    
d58b8953   Alexandre   Version: 2.4.4.0
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
    public function ficheMateriel($labNumber) {
    	
    	// Find the concerned materiel
    	$materiel = TableRegistry::get('Materiels')->find('all', ['conditions' => ['numero_laboratoire' => $labNumber]])->first(); // End find
    	
    	if(isset($materiel->sur_categorie_id)) {
    		$surCategorie = TableRegistry::get('SurCategories')->find()->where(['id =' => $materiel->sur_categorie_id])->first()->nom;
    	}
    	else {
    		$surCategorie = ' ';
    	}
    	
    	if(isset($materiel->categorie_id)) {
    		$categorie = TableRegistry::get('Categories')->find()->where(['id =' => $materiel->categorie_id])->first()->nom;
    	}else {
    		$categorie = ' ';
    	}
    	
    	if(isset($materiel->sous_categorie_id)) {
    		$sousCategorie = TableRegistry::get('SousCategories')->find()->where(['id =' => $materiel->sous_categorie_id])->first()->nom;
    	}else {
    		$sousCategorie = ' ';
    	}
    	
    	if(isset($materiel->groupes_thematique_id)) {
    		$groupesThematique = TableRegistry::get('GroupesThematiques')->find()->where(['id =' => $materiel->groupes_thematique_id])->first()->nom;
    	}else {
    		$groupesThematique = ' ';
    	}
    	
    	if(isset($materiel->groupes_metier_id)) {
    		$groupesMetier = TableRegistry::get('GroupesMetiers')->find()->where(['id =' => $materiel->groupes_metier_id])->first()->nom;
    	}else {
    		$groupesMetier = ' ';
    	}
    	
    	if(isset($materiel->organisme_id)) {
    		$organisme = TableRegistry::get('Organismes')->find()->where(['id =' => $materiel->organisme_id])->first()->nom;
    	}else {
    		$organisme = ' ';
    	}
    	
    	if(isset($materiel->site_id)) {
    		$site = TableRegistry::get('Sites')->find()->where(['id =' => $materiel->site_id])->first()->nom;
    	}else {
    		$site = ' ';
    	}

    	 
    	
    	// set the data materiel for the document (accessible par $materiel dans le document)
    	$this->set(compact('materiel', 'surCategorie', 'categorie', 'sousCategorie', 'groupesThematique', 'groupesMetier', 'organisme', 'site'));
    	$this->set ( 'fpdf', new FPDF ( 'P', 'mm', 'A4' ) );
    } 
6bb4a0f7   Alexandre   Version: 2.2.2
387
    
6c4edfa3   Alexandre   First Commit LabI...
388
}