コード例 #1
0
ファイル: TacheController.php プロジェクト: PhilippeGeek/PHPM
 /**
  * Edits an existing Tache entity.
  *
  * @Route("/{id}/update", name="tache_update")
  * @Method("post")
  * @Template("AssoMakerPHPMBundle:Tache:edit.html.twig")
  */
 public function updateAction($id)
 {
     if (false === $this->get('security.context')->isGranted('ROLE_USER')) {
         throw new AccessDeniedException();
     }
     $admin = $this->get('security.context')->isGranted('ROLE_HUMAIN');
     $em = $this->getDoctrine()->getEntityManager();
     $config = $this->get('config.extension');
     $request = $this->getRequest();
     $param = $request->request->all();
     $entity = $em->getRepository('AssoMakerPHPMBundle:Tache')->find($id);
     $prevStatut = $entity->getStatut();
     $user = $this->get('security.context')->getToken()->getUser();
     $rOnly = $entity->getStatut() >= 1 && !$admin || $entity->getStatut() == 3;
     if ($config->getValue('phpm_tache_heure_limite_validation')) {
         $heureLimite = $config->getValue('phpm_tache_heure_limite_validation');
         $deadlinePassed = new \DateTime($heureLimite) < new \DateTime();
     } else {
         $deadlinePassed = false;
     }
     if (!$entity) {
         throw $this->createNotFoundException('Unable to find Tache entity.');
     }
     $defaultValues = array('entity' => $entity, 'Materiel' => $entity->getMateriel());
     $editForm = $this->createForm(new TacheType($admin, $em, $config, $rOnly), $defaultValues);
     $editForm->handleRequest($request);
     $data = $editForm->getData();
     $typeCommentaire = 0;
     $valid = $editForm->isValid();
     if ($valid) {
         $tmpm = $data['Materiel'];
         if ($param['action'] == 'submit_validation') {
             $entity->setStatut(1);
             $typeCommentaire = 1;
         }
         if ($param['action'] == 'validate') {
             $entity->setStatut(2);
             $typeCommentaire = 2;
         }
         if ($param['action'] == 'reject') {
             $entity->setStatut(0);
             $typeCommentaire = 3;
         }
         if ($param['action'] == 'delete') {
             $entity->setStatut(-1);
             $entity->removeAllCreneaux();
             $typeCommentaire = -1;
         }
         if ($param['action'] == 'restore') {
             $entity->setStatut(0);
             $typeCommentaire = 4;
         }
         if ($param['action'] == 'devalidate') {
             $entity->setStatut(0);
             $typeCommentaire = 11;
         }
         if ($tmpm) {
             foreach ($tmpm as $group) {
                 foreach ($group as $key => $value) {
                     $bms = $em->createQuery("SELECT b FROM AssoMakerPHPMBundle:BesoinMateriel b JOIN b.materiel m JOIN b.tache t WHERE t.id = :tid AND m.id=:mid")->setParameter('tid', $id)->setParameter('mid', $key)->getResult();
                     if ($value * 1 != 0) {
                         if (!array_key_exists(0, $bms)) {
                             //Le bm liant m et t n'existe pas
                             $bm = new BesoinMateriel();
                             $bm->setTache($entity);
                             $m = $em->createQuery("SELECT m FROM AssoMakerPHPMBundle:Materiel  m  WHERE  m.id=:mid")->setParameter('mid', $key)->getSingleResult();
                             $bm->setMateriel($m);
                             $em->persist($bm);
                         } else {
                             $bm = $bms[0];
                         }
                         $bm->setQuantite($value * 1);
                         $entity->addBesoinMateriel($bm);
                     } else {
                         if (array_key_exists(0, $bms)) {
                             //Le bm liant m et t n'existe pas
                             $bm = $bms[0];
                             $entity->getBesoinsMateriel()->removeElement($bm);
                             $m = $em->createQuery("SELECT m FROM AssoMakerPHPMBundle:Materiel  m  WHERE  m.id=:mid")->setParameter('mid', $key)->getSingleResult();
                             $m->getBesoinsMateriel()->removeElement($bm);
                             $em->remove($bm);
                         }
                     }
                 }
             }
         }
         if ($data['commentaire'] != "" || $typeCommentaire != 0) {
             $commentaire = new Commentaire();
             $commentaire->setAuteur($user);
             $commentaire->setHeure(new \DateTime());
             $commentaire->setTache($entity);
             $commentaire->setTexte($data['commentaire']);
             $commentaire->setType($typeCommentaire);
             $em->persist($commentaire);
         }
         $em->persist($entity);
         $em->flush();
         $defaultValues = array('entity' => $entity, 'Materiel' => $entity->getMateriel());
         $rOnly = $entity->getStatut() >= 1 && !$admin || $entity->getStatut() == 3;
         $editForm = $this->createForm(new TacheType($admin, $em, $config, $rOnly), $defaultValues);
         //             return $this->redirect($this->generateUrl('tache_edit', array('id' => $id)));
         if ($param['action'] == 'save_return') {
             return $this->redirect($this->generateUrl('groupetache_edit', array('id' => $entity->getGroupeTache()->getId())));
         }
         if ($param['action'] == 'creneaumaker') {
             return $this->redirect($this->generateUrl('creneaumaker_tache', array('id' => $entity->getId())));
         }
         if ($param['action'] == 'affectation') {
             return $this->redirect($this->generateUrl('tache_okaffectation', array('id' => $entity->getId())));
         }
     }
     if ($param['action'] == 'add_plage') {
         return $this->redirect($this->generateUrl('plagehoraire_new', array('id' => $entity->getId())));
     }
     return array('entity' => $entity, 'form' => $editForm->createView(), 'valid' => $valid, 'rOnly' => $rOnly, 'deadlinePassed' => $deadlinePassed);
 }