Blame view

src/Controller/DocumentsController.php 6.98 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
12
13
14
15

/**
 * Documents Controller
 *
 * @property \App\Model\Table\DocumentsTable $Documents
 */
class DocumentsController extends AppController
{

04a6b875   Alexandre   Version: 2.4.2.0
16
17
18
19
20
21
22
23
24
	/**
	 * @param $user
	 *
	 * Give authorization for documents
	 *
	 * @return boolean
	 */
	public function isAuthorized($user)
	{
2dc3932b   Alexandre   Version: 2.4.2.9
25
26
27
28
		$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'];
		
04a6b875   Alexandre   Version: 2.4.2.0
29
		if (in_array($action, ['admission', 'sortie'])) {
8b90af53   Alexandre   Version: 2.4.2.12
30
			if ($this->userHasRole('Administration')) {
04a6b875   Alexandre   Version: 2.4.2.0
31
32
33
34
35
36
37
				return true;
			}
		}
	
		return parent::isAuthorized($user);
	}
	
6c4edfa3   Alexandre   First Commit LabI...
38
39
40
41
42
43
44
    /**
     * Index method
     *
     * @return \Cake\Network\Response|null
     */
    public function index()
    {
6c4edfa3   Alexandre   First Commit LabI...
45
46
        $documents = $this->paginate($this->Documents);

4fd23929   Alexandre   Version: 2.5.0
47
48
49
        $materiel = $this->Documents->Materiels;
        
        $this->set(compact('documents', 'materiel'));
6c4edfa3   Alexandre   First Commit LabI...
50
51
52
53
54
55
56
57
58
59
60
61
        $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)
    {
4fd23929   Alexandre   Version: 2.5.0
62
        $document = $this->Documents->get($id);
6c4edfa3   Alexandre   First Commit LabI...
63

4fd23929   Alexandre   Version: 2.5.0
64
65
66
67
68
69
70
71
72
73
        $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...
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        $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();
        if ($this->request->is('post')) {
            $document = $this->Documents->patchEntity($document, $this->request->data);
            if ($this->Documents->save($document)) {
3601f4f5   Alexandre   Traduction messag...
89
                $this->Flash->success(__('Le fichier a bien été ajouté.'));
6c4edfa3   Alexandre   First Commit LabI...
90
91
                return $this->redirect(['action' => 'index']);
            } else {
3601f4f5   Alexandre   Traduction messag...
92
                $this->Flash->error(__('Le fichier n\'a pas pu être ajouté.'));
6c4edfa3   Alexandre   First Commit LabI...
93
94
            }
        }
4fd23929   Alexandre   Version: 2.5.0
95
96
97
98
99
100
101
102
103
104
105
106
        
        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);
        }
         
        
        $this->set(compact('document'));
6c4edfa3   Alexandre   First Commit LabI...
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
        $this->set('_serialize', ['document']);
    }

    /**
     * Edit method
     *
     * @param string|null $id Document 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)
    {
        $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)) {
3601f4f5   Alexandre   Traduction messag...
125
                $this->Flash->success(__('Le fichier a bien été édité.'));
6c4edfa3   Alexandre   First Commit LabI...
126
127
                return $this->redirect(['action' => 'index']);
            } else {
3601f4f5   Alexandre   Traduction messag...
128
                $this->Flash->error(__('Le fichier n\'a pas pu être édité.'));
6c4edfa3   Alexandre   First Commit LabI...
129
130
            }
        }
4fd23929   Alexandre   Version: 2.5.0
131
132
133
134
135
136
137
138
139
140
141
142
        
        $materiel = $this->Documents->Materiels->find('list', [ 'keyField' => 'id', 'valueField' => 'numero_laboratoire'])->where(['id =' => $document->materiel_id]);
        if($materiel->count() > 0) {
        	$this->set('materiel', $materiel);
        }
        else {
        	$suivi = $this->Documents->Suivis->find('list', [ 'keyField' => 'id', 'valueField' => 'id'])->where(['id =' => $document->suivi_id]);
        	$this->set('suivi', $suivi);
        }
        
        
        $this->set(compact('document'));
6c4edfa3   Alexandre   First Commit LabI...
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
        $this->set('_serialize', ['document']);
    }

    /**
     * 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);
        if ($this->Documents->delete($document)) {
3601f4f5   Alexandre   Traduction messag...
158
            $this->Flash->success(__('Le fichier a bien été supprimé.'));
6c4edfa3   Alexandre   First Commit LabI...
159
        } else {
3601f4f5   Alexandre   Traduction messag...
160
            $this->Flash->error(__('Le fichier n\'a pas pu être supprimé.'));
6c4edfa3   Alexandre   First Commit LabI...
161
162
163
        }
        return $this->redirect(['action' => 'index']);
    }
6bb4a0f7   Alexandre   Version: 2.2.2
164
165
166
167
168
169
170
171
172
173
174
175
176
    
    
    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
177
    	$userName = $this->LdapAuth->user('username');
6bb4a0f7   Alexandre   Version: 2.2.2
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
    	$numeroLab = $materiel->numero_laboratoire;
    	$dateAcquisition = $materiel->date_acquisition;
    	// convert to French format dd-mm-yyyy :
    	$dateAcquisition = date ( "d-m-Y", strtotime ( $dateAcquisition ) );
    	$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
    
    
6c4edfa3   Alexandre   First Commit LabI...
215
}