find()->where(['id =' => 1])->first(); $role = TableRegistry::get('Users')->find()->where(['username' => $user[$configuration->authentificationType_ldap][0]])->first()['role']; $action = $this->request->params['action']; if($this->userHasRole('Administration')) return true; //Pour un "utilisateur" if (in_array($action, ['edit', 'delete'])) { $id = (int)$this->request->params['pass'][0]; if($this->isOwnedBy($id, $user['givenname'][0].' '.$user['sn'][0])) return true; if($role == 'Responsable' && $this->isRespGroup($id, $user[$configuration->authentificationType_ldap][0])) return true; } return parent::isAuthorized($user); } public function isOwnedBy($id, $nomCreateur) { return $this->Suivis->exists(['id' => $id, 'nom_createur' => $nomCreateur]); } public function isRespGroup($id, $loginResponsable) { $u = TableRegistry::get('Users')->find()->where(['username' => $loginResponsable])->first(); return ($this->Suivis->exists(['id' => $id, 'groupes_metier_id' => $u['groupes_metier_id']]) || $this->Suivis->exists(['id' => $id, 'groupes_thematique_id' => $u['groupe_thematique_id']])); } /** * Index method * * @return \Cake\Network\Response|null */ public function index() { $condition = ''; $GM = $this->request->query('GM'); if(isset($GM)) { $condition = ['Suivis.groupes_metier_id =' => $this->request->query('GM')]; } $this->paginate = [ 'contain' => ['Materiels', 'TypeSuivis'] ]; $suivis = $this->paginate($this->Suivis->find('all', ['conditions' => $condition])); $this->set('nbSuivis', $this->Suivis->find('all', ['conditions' => $condition])->count()); $this->set(compact('suivis')); $this->set('_serialize', ['suivis']); } /** * View method * * @param string|null $id Suivi id. * @return \Cake\Network\Response|null * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. */ public function view($id = null) { $suivi = $this->Suivis->get($id, [ 'contain' => ['Materiels', 'Documents', 'TypeSuivis', 'GroupesThematiques', 'GroupesMetiers'] ]); $this->set('suivi', $suivi); $this->set('_serialize', ['suivi']); } /** * Add method * * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise. */ public function add() { $suivi = $this->Suivis->newEntity(); if ($this->request->is('post')) { $suivi = $this->Suivis->patchEntity($suivi, $this->request->data); $suivi->panne_resolu = false; if ($this->Suivis->save($suivi)) { $this->Flash->success(__('Le suivi a bien été ajouté.')); return $this->redirect(['controller' => 'Materiels', 'action' => 'view', $this->passedArgs[0]]); } else { $this->Flash->error(__('Le suivi n\'a pas pu être ajouté.')); } } $materiels = $this->Suivis->Materiels->find('list'); $materiel = $this->Suivis->Materiels->find()->where(['id =' => $this->passedArgs[0]])->first(); $typeSuivis = $this->Suivis->TypeSuivis->find('list', [ 'keyField' => 'id', 'valueField' => 'nom']); $groupesThematiques = $this->Suivis->GroupesThematiques->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'GroupesThematiques.nom']); $groupesMetiers = $this->Suivis->GroupesMetiers->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'GroupesMetiers.nom']); $this->set(compact('suivi', 'materiels', 'typeSuivis', 'materiel', 'groupesThematiques', 'groupesMetiers')); $this->set('_serialize', ['suivi']); } /** * Edit method * * @param string|null $id Suivi 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) { $suivi = $this->Suivis->get($id, [ 'contain' => [] ]); if ($this->request->is(['patch', 'post', 'put'])) { $suivi = $this->Suivis->patchEntity($suivi, $this->request->data); if ($this->Suivis->save($suivi)) { $this->Flash->success(__('Le suivi a bien été édité.')); return $this->redirect(['action' => 'view', $id]); } else { $this->Flash->error(__('Le suivi n\'a pas pu être édité.')); } } $materiels = $this->Suivis->Materiels->find('list'); $numMateriel = $this->Suivis->Materiels->find()->select('numero_laboratoire')->where(['id =' => $suivi->get('materiel_id')])->first()['numero_laboratoire']; $typeSuivis = $this->Suivis->TypeSuivis->find('list', [ 'keyField' => 'id', 'valueField' => 'nom']); $groupesThematiques = $this->Suivis->GroupesThematiques->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'GroupesThematiques.nom']); $groupesMetiers = $this->Suivis->GroupesMetiers->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'GroupesMetiers.nom']); $this->set(compact('suivi', 'materiels', 'typeSuivis', 'numMateriel', 'groupesThematiques', 'groupesMetiers')); $this->set('_serialize', ['suivi']); } /** * Delete method * * @param string|null $id Suivi 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']); $suivi = $this->Suivis->get($id); if ($this->Suivis->delete($suivi)) { $this->Flash->success(__('Le suivi a bien été supprimé.')); } else { $this->Flash->error(__('Le suivi n\'a pas pu être supprimé.')); } return $this->redirect(['action' => 'index']); } /** * GetConditionForField method * * @param unknown $fieldName * @return string[]|NULL */ private function getConditionForField($fieldName) { $searchFieldName = 's_' . $fieldName; if ( isset($this->request->data[$searchFieldName]) && ($this->request->data[$searchFieldName] != '')) return ["Suivis.$fieldName LIKE" => '%'.$this->request->data[$searchFieldName].'%']; return NULL; } /** * GetConditionForFieldNumber method * * @param unknown $fieldName * @return $string[]|NULL */ private function getConditionForFieldNumber($fieldName) { $searchFieldName = 's_' . $fieldName; if ( isset($this->request->data[$searchFieldName]) && ($this->request->data[$searchFieldName] != '')) return ["Suivis.$fieldName =" => $this->request->data[$searchFieldName]]; return NULL; } /** * Find method */ public function find() { $s_groupes_thematiques = $this->Suivis->GroupesThematiques->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'GroupesThematiques.nom']); $s_groupes_metiers = $this->Suivis->GroupesMetiers->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'GroupesMetiers.nom']); $s_type_suivis = $this->Suivis->TypeSuivis; $materiels = $this->Suivis->Materiels; $this->set(compact('s_groupes_thematiques', 's_groupes_metiers', 's_type_suivis', 'materiels')); $resultTri = $this->request->session()->read("resultTri"); if ($this->request->is('post')) { $specificFieldsConditions = NULL; $periode_interventionRequest = NULL; $date_intervention = NULL; if ($this->request->data['s_periode_controle1'] != '') $periode_interventionRequest['Suivis.date_controle >='] = date('Y-m-d', strtotime(str_replace('/', '-', $this->request->data['s_periode_controle1']))); if ($this->request->data['s_periode_controle2'] != '') $periode_interventionRequest['Suivis.date_controle <='] = date('Y-m-d', strtotime(str_replace('/', '-', $this->request->data['s_periode_controle2']))); if ($this->request->data['s_date_controle'] != '') $date_intervention['Suivis.date_controle ='] = date('Y-m-d', strtotime(str_replace('/', '-', $this->request->data['s_date_controle']))); $specificFieldsConditions = [ $date_intervention, $periode_interventionRequest, $this->getConditionForFieldNumber('type_suivi_id'), $this->getConditionForField('organisme'), $this->getConditionForFieldNumber('groupes_metier_id'), $this->getConditionForFieldNumber('groupes_thematique_id'), ]; // CONSTRUCTION DE LA REQUETE SQL COMPLETE = $specificFieldsConditions // by default, no sort $lastResults = $this->Suivis->find('all', ['conditions' => $specificFieldsConditions]); $this->paginate = ['limit' => 1000]; $_results = $this->paginate($lastResults); $this->set(compact('_results')); } // end if() else if (isset($resultTri) && strstr($this->request->here(), 'sort') != false && strstr($this->request->here(), 'direction') != false) { $findedSuivis = []; foreach($resultTri as $r) { array_push($findedSuivis, $r->id); } $res = $this->Suivis->find('all', ['limit' => 1000]); for($i = 0; $i < sizeof($findedSuivis); $i++) { $res->orWhere(['id =' => $findedSuivis[$i]]); } $this->paginate = ['limit' => 1000]; $_results = $this->paginate($res); $this->set(compact('_results')); } } // called from Javascript (Ajax) public function getNextDate($dateORjour, $frequenceORmois, $typeFrequenceORannee, $frequence = null, $typeFrequence = null) { if($frequence != null && $typeFrequence != null) { $date = $dateORjour.'-'.$frequenceORmois.'-'.$typeFrequenceORannee; } else { $date = $dateORjour; $frequence = $frequenceORmois; $typeFrequence = $typeFrequenceORannee; } $date_next = date_create_from_format('d-m-Y', $date); switch($typeFrequence) { case "Jours": date_add($date_next, date_interval_create_from_date_string($frequence.' days')); break; case "Semaines": date_add($date_next, date_interval_create_from_date_string((7*$frequence).' days')); break; case "Mois": date_add($date_next, date_interval_create_from_date_string($frequence.' months')); break; case "Ans": date_add($date_next, date_interval_create_from_date_string($frequence.' years')); break; } $this->set ('date', date_format($date_next, 'd-m-Y')); $this->viewBuilder()->layout = 'ajax'; } }