src/Controller/Api/Prive/ApiContactController.php line 76

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api\Prive;
  3. use App\Entity\Centreinteret;
  4. use App\Entity\Contact;
  5. use App\Entity\Prospect;
  6. use App\Entity\Rdv;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. /**
  11.  * @Route("/contact")
  12.  */
  13. class ApiContactController extends ApiController
  14. {
  15.     /**
  16.      * @Route("/{id}", name="api_Contact_getById", methods={"GET"})
  17.      * @param int $id
  18.      * @return JsonResponse
  19.      */
  20.     public function getById(int $id)
  21.     {
  22.         if ( $id 0) {
  23.             $contact $this->em->getRepository(Contact::class)->find($id);
  24.             if ($contact) {
  25.                 return $this->apiOk($contact);
  26.             }
  27.         }
  28.         return $this->apiKo("Contact non trouvé !!");
  29.     }
  30.     /**
  31.      * @Route("/{id}", name="api_Contact_delete", methods={"DELETE"})
  32.      * @param int $id
  33.      * @return JsonResponse
  34.      */
  35.     public function delete(int $id)
  36.     {
  37.         if ($id 0) {
  38.             $contact $this->em->getRepository(Contact::class)->find($id);
  39.             // 1 - Voir Régles de suppression de contacts avec malika
  40.             //$rdvs = $this->em->getRepository(Rdv::class)->findBy(["contact" => $contact]);
  41.            // if ($rdvs) {
  42.                 // Suppression Logique
  43.                 $contact->setActive(2);
  44.                 $this->em->persist($contact);
  45.                 $this->em->flush();
  46.                 return $this->apiOk($id);
  47.            /* } else {
  48.                 // Suppression Physique
  49.                 try {
  50.                     return $this->removeElement($contact);
  51.                 } catch (\Exception $e) {
  52.                     return $this->apiKo($e->getMessage());
  53.                 }
  54.             }*/
  55.         }
  56.         return $this->apiKo("Contact non trouvé !!");
  57.     }
  58.     /**
  59.      * @Route("/", name="api_Contact_save", methods={"POST"})
  60.      * @param Request $request
  61.      * @return JsonResponse
  62.      */
  63.     public function save(Request $request)
  64.     {
  65.         $data json_decode($request->getContent(), true);
  66.         if (isset($data['prospect']) && isset($data['prospect']['id'])) {
  67.             $prospect $this->getObject(Prospect::class,$data['prospect']['id']);
  68.         }
  69.         if (!isset($data['civilite']) || trim($data['civilite'] == "")) {
  70.             return $this->apiKo("Civilité ne peut pas etre vide !");
  71.         }
  72.             if (!isset($data['prenom']) || trim($data['prenom'] == "")) {
  73.                 return $this->apiKo("Prenom du contact ne peut pas etre vide !");
  74.             }
  75.         if (!isset($data['nom']) || trim($data['nom'] == "")) {
  76.                 return $this->apiKo("Nom du contact ne peut pas etre vide !");
  77.         }
  78.         if (!isset($data['fonction']) || trim($data['fonction'] == "")) {
  79.             return $this->apiKo("Fonction du contact ne peut pas etre vide !");
  80.         }
  81.         $contact $this->saveContact($prospect,$data);
  82.         if($contact instanceof Contact){
  83.             return $this->apiOk($contact);
  84.         }else{
  85.             return $this->apiKo($contact);
  86.         }
  87.     }
  88.     /**
  89.      * @Route("/find", name="api_Contact_find", methods={"POST"})
  90.      * @param Request $request
  91.      * @return JsonResponse
  92.      */
  93.     public function find(Request $request)
  94.     {
  95.         $data json_decode($request->getContent(), true);
  96.         $data['filter']['and']['contact.active'] = ' <> 2 '// ne pas affichier les elements supprimé logiquement
  97.         $sql "
  98.             SELECT contact 
  99.             FROM " Contact::class . " contact
  100.             LEFT JOIN contact.centreinterets centreinteret
  101.         ";
  102.         return $this->findElementsWithJoin($sql$data);
  103.     }
  104. }