confLabinvent; $role = TableRegistry::get('Users')->find() ->where(['username' => $user[$configuration->authentificationType_ldap][0]]) ->first()['role']; $action = $this->request->getAttribute('params')['action']; // Super-Admin peut accéder à chaque action if ($role == 'Super Administrateur') return true; if (in_array($action, [ 'admission', 'sortie' ])) { if ($this->userHasRoleAtLeast('Administration')) { return true; } } if (in_array($action, [ 'delete', 'edit' ])) { if ($this->userHasRoleAtLeast('Administration')) { return true; } $u = TableRegistry::get('Users')->find()->where([ 'username' => $user[$configuration->authentificationType_ldap][0] ])->first(); $doc = $this->Documents->get((int) $this->request->getAttribute('params')['pass'][0]); $id = $doc->get('materiel_id'); if (empty($id)) { $id = $doc->get('suivi_id'); $suiviTable = TableRegistry::get('Suivis'); if ($role == 'Responsable') { if ($u['groupes_metier_id'] !== null && $u['groupes_metier_id'] != TableRegistry::get('GroupesMetiers')->find()->where([ 'nom =' => 'N/A' ])->first()['id']) { return ($suiviTable->exists([ 'id' => $id, 'groupes_metier_id' => $u['groupes_metier_id'] ])); } else if ($u['groupe_thematique_id'] !== null && $u['groupe_thematique_id'] != TableRegistry::get('GroupesThematiques')->find()->where([ 'nom =' => 'N/A' ])->first()['id']) { return ($suiviTable->exists([ 'id' => $id, 'groupes_thematique_id' => $u['groupe_thematique_id'] ])); } else { return false; } } if ($role == 'Utilisateur') { return $suiviTable->exists([ 'id' => $id, 'nom_createur' => $user['sn'][0] . ' ' . $user['givenname'][0] ]); } } else { $materielTable = TableRegistry::get('Materiels'); if ($role == 'Responsable') { if ($u['groupes_metier_id'] !== null && $u['groupes_metier_id'] != TableRegistry::get('GroupesMetiers')->find()->where([ 'nom =' => 'N/A' ])->first()['id']) { return ($materielTable->exists([ 'id' => $id, 'groupes_metier_id' => $u['groupes_metier_id'] ])); } else if ($u['groupe_thematique_id'] !== null && $u['groupe_thematique_id'] != TableRegistry::get('GroupesThematiques')->find()->where([ 'nom =' => 'N/A' ])->first()['id']) { return ($materielTable->exists([ 'id' => $id, 'groupes_thematique_id' => $u['groupe_thematique_id'] ])); } else { return false; } } if ($role == 'Utilisateur') { return ($materielTable->exists([ 'id' => $id, 'nom_createur' => $user['sn'][0] . ' ' . $user['givenname'][0] ]) || $materielTable->exists([ 'id' => $id, 'nom_responsable' => $user['sn'][0] . ' ' . $user['givenname'][0] ])); } } } // Pour tout le monde if (in_array($action, [ 'view', 'add', 'ficheMateriel' ])) return true; return false; } /** * Index method * * @return \Cake\Network\Response|null */ public function index() { $this->paginate = [ 'contain' => [ 'TypeDocuments' ] ]; $documents = $this->paginate($this->Documents); $materiel = $this->Documents->Materiels; $this->set(compact('documents', 'materiel')); $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) { $document = $this->Documents->get($id, [ 'contain' => [ 'TypeDocuments' ] ]); $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); } $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); $nomType = $this->Documents->TypeDocuments->find()->where([ 'id =' => $document->type_document_id ])->first()['nom']; if (isset($this->request->getAttribute('params')['pass'][2]) && $this->request->getAttribute('params')['pass'][2] == 'photo') { $document->set('photo', 1); } if ($this->Documents->save($document)) { $this->Flash->success(__('Le fichier a bien été ajouté.')); $id = $document->materiel_id; if (! empty($id)) { $materielTable = TableRegistry::get('Materiels'); $materiel = $materielTable->get($id); } if (isset($this->request->getAttribute('params')['pass'][2]) && $this->request->getAttribute('params')['pass'][2] == 'photo') { $photoIdOld = $materiel->get('photo_id'); if ($photoIdOld !== null) { $docOld = TableRegistry::get('Documents')->get($photoIdOld); $this->Documents->delete($docOld); } $materiel->set('photo_id', $document->id); $materielTable->save($materiel); } $userName = $this->LdapAuth->user('sn')[0] . ' ' . $this->LdapAuth->user('givenname')[0]; $userEmail = $this->LdapAuth->user('mail')[0]; $id = $document->materiel_id; if (empty($id)) { // Ajout de doc au suivi $id = $document->suivi_id; // $this->sendEmailToManagementWith('[LabInvent] Ajout d\'un document', $userName.' (email = '.$userEmail.') a ajouté un document de type "'.$nomType.'" au suivi "Suivi '.$document->suivi_id.'".'); $this->sendEmail(null, '[LabInvent] Ajout d\'un document', $userName . ' a ajouté un document de type "' . $nomType . '" au suivi "Suivi ' . $document->suivi_id . '".'); return $this->redirect([ 'controller' => 'suivis', 'action' => 'view', $id ]); } else { // Ajout de doc au matériel // $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.').'); $this->sendEmail(null, '[LabInvent] Ajout d\'un document', $userName . ' a ajouté un document de type "' . $nomType . '" au matériel "' . $materiel->designation . '" (id = ' . $materiel->id . ').'); return $this->redirect([ 'controller' => 'materiels', 'action' => 'view', $id ]); } } else { $this->Flash->error(__('Le fichier n\'a pas pu être ajouté.')); } } if (isset($this->request->getAttribute('params')['pass'][1]) && $this->request->getAttribute('params')['pass'][1] == 'mat') { $materiel = $this->Documents->Materiels->find('list', [ 'keyField' => 'id', 'valueField' => 'numero_laboratoire' ])->where([ 'id =' => $this->request->getAttribute('params')['pass'][0] ]); $this->set('materiel', $materiel); } else { $suivi = $this->Documents->Suivis->find('list', [ 'keyField' => 'id', 'valueField' => 'id' ])->where([ 'id =' => $this->request->getAttribute('params')['pass'][0] ]); $this->set('suivi', $suivi); } if (isset($this->request->getAttribute('params')['pass'][2]) && $this->request->getAttribute('params')['pass'][2] == 'photo') { $this->set('photo', 1); $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' ]); } $this->set(compact('document', 'typesD')); $this->set('_serialize', [ 'document' ]); } /** * 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']); } /** * 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 ($document->photo) { $materielTable = TableRegistry::get('Materiels'); $materiel = $materielTable->get($document->materiel_id); $materiel->set('photo_id', null); $materielTable->save($materiel); } if ($this->Documents->delete($document)) $this->Flash->success(__('Le fichier a bien été supprimé.')); else $this->Flash->error(__('Le fichier n\'a pas pu être supprimé.')); $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]); } 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 $userName = $this->LdapAuth->user('username'); $numeroLab = $materiel->numero_laboratoire; $dateAcquisition = $materiel->date_acquisition; $dateAcquisition = $dateAcquisition; $numeroCommande = $materiel->numero_commande; $designation = $materiel->designation; if ($materiel->organisme_id !== null && ! 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 public function ficheMateriel($labNumber) { // Find the concerned materiel $materiel = TableRegistry::get('Materiels')->find('all', ['conditions' => ['numero_laboratoire' => $labNumber]])->first(); // End find if ($materiel->sur_categorie_id !== null) $surCategorie = TableRegistry::get('SurCategories')->find()->where(['id =' => $materiel->sur_categorie_id])->first()->nom; else $surCategorie = ' '; if ($materiel->categorie_id !== null) $categorie = TableRegistry::get('Categories')->find()->where(['id =' => $materiel->categorie_id])->first()->nom; else $categorie = ' '; if ($materiel->sous_categorie_id !== null) $sousCategorie = TableRegistry::get('SousCategories')->find() ->where(['id =' => $materiel->sous_categorie_id]) ->first()->nom; else $sousCategorie = ' '; if ($materiel->groupes_thematique_id !== null) $groupesThematique = TableRegistry::get('GroupesThematiques')->find() ->where(['id =' => $materiel->groupes_thematique_id]) ->first()->nom; else $groupesThematique = ' '; if ($materiel->groupes_metier_id !== null) $groupesMetier = TableRegistry::get('GroupesMetiers')->find()->where(['id =' => $materiel->groupes_metier_id])->first()->nom; else $groupesMetier = ' '; if ($materiel->organisme_id !== null) $organisme = TableRegistry::get('Organismes')->find()->where(['id =' => $materiel->organisme_id])->first()->nom; else $organisme = ' '; if ($materiel->site_id !== null) $site = TableRegistry::get('Sites')->find()->where(['id =' => $materiel->site_id])->first()->nom; else $site = ' '; $configuration = $this->confLabinvent; $nom_groupe_thematique = $configuration->nom_groupe_thematique; $nom_groupe_metier = $configuration->nom_groupe_metier; // set the data materiel for the document (accessible par $materiel dans le document) $this->set(compact('materiel', 'surCategorie', 'categorie', 'sousCategorie', 'groupesThematique', 'groupesMetier', 'organisme', 'site', 'nom_groupe_metier', 'nom_groupe_thematique')); $this->set('fpdf', new FPDF('P', 'mm', 'A4')); } public function ficheMetrologique($id) { // Find the concerned suivi $fiche = TableRegistry::get('Fichemetrologiques')->find('all', ['conditions' => ['id' => $id]])->first(); $suivi = TableRegistry::get('Suivis')->find('all', ['conditions' => ['id' => $fiche->suivi_id]])->first(); $mesures = TableRegistry::get('Mesures')->find('all', ['conditions' => ['fichemetrologique_id' => $fiche->id]]); if ($suivi->unite_id !== null) $unite = TableRegistry::get('Unites')->find()->where(['id =' => $suivi->unite_id])->first()->nom; else $unite = ' '; if ($suivi->unite_id !== null) $symbole = TableRegistry::get('Unites')->find()->where(['id =' => $suivi->unite_id])->first()->symbole; else $symbole = ' '; // set the data materiel for the document (accessible par $materiel dans le document) $this->set(compact('suivi', 'unite', 'fiche', 'symbole', 'mesures')); $this->set('fpdf', new FPDF('P', 'mm', 'A4')); } }