src/Controller/NoticeController.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Feedback;
  4. use App\Entity\Import;
  5. use App\Entity\Notice;
  6. use App\Entity\Project;
  7. use App\Entity\User;
  8. use App\Form\Type\FeedbackLoggedInType;
  9. use App\Form\Type\FeedbackLoggedOutType;
  10. use App\Form\Type\NoticeType;
  11. use App\Service\MessageService;
  12. use Psr\Log\LoggerInterface;
  13. use Swift_Mailer;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use DateTime;
  20. use Exception;
  21. use Swift_Message;
  22. class NoticeController extends AbstractController
  23. {
  24.     /**
  25.      * @Route("/notice", name="notice")
  26.      * @param Request $request
  27.      * @param MessageService $messageService
  28.      * @return Response
  29.      * @throws Exception
  30.      */
  31.     public function index(Request $requestMessageService $messageService)
  32.     {
  33.         $user_id $this->getUser()->getId();
  34.         $entityManager $this->getDoctrine()->getManager();
  35.         $context "global scope";
  36.         // load project id and import id from given source_uri string
  37.         $project_id $request->get("project_id"0);
  38.         $import_id $request->get("import_id"0);
  39.         // load notice if one exists for context global
  40.         $notice null;
  41.         if ($project_id == && $import_id == 0) {
  42.             $notice $entityManager->getRepository(Notice::class)->findOneBy([
  43.                 "user_id" => $user_id,
  44.                 "project_id" => $project_id,
  45.                 "import_id" => $import_id,
  46.             ]);
  47.         }
  48.         // if we have an project id load project notice
  49.         else if ($project_id && $import_id == 0) {
  50.             $project $entityManager->getRepository(Project::class)->find($project_id);
  51.             $context " project <b>" $project->getName() . "</b>";
  52.             $notice $entityManager->getRepository(Notice::class)->findOneBy([
  53.                 "project_id" => $project_id,
  54.                 "import_id" => $import_id,
  55.             ]);
  56.         }
  57.         // if we even have an import id load import notice
  58.         else if ($project_id && $import_id 0) {
  59.             $project $entityManager->getRepository(Project::class)->find($project_id);
  60.             $context " project <b>" $project->getName() . "</b>";
  61.             $import $entityManager->getRepository(Import::class)->find($import_id);
  62.             $context " import <b>" $import->getDescription() . "</b> @ " $context;
  63.             $notice $entityManager->getRepository(Notice::class)->findOneBy([
  64.                 "project_id" => $project_id,
  65.                 "import_id" => $import_id,
  66.             ]);
  67.         }
  68.         // if no notice exists create an empty one
  69.         if(!$notice) {
  70.             $notice = new Notice();
  71.         }
  72.         $createdBy "";
  73.         $modifiedBy "";
  74.         if ($notice->getCreatedBy() > 0){
  75.             $user $entityManager->getRepository(User::class)->find($notice->getCreatedBy());
  76.             if ($user) {
  77.                 $createdBy $user->getEmail();
  78.             }
  79.         }
  80.         if ($notice->getModifiedBy() > 0){
  81.             $user $entityManager->getRepository(User::class)->find($notice->getModifiedBy());
  82.             if ($user) {
  83.                 $modifiedBy $user->getEmail();
  84.             }
  85.         }
  86.         $form $this->createForm(NoticeType::class, $notice);
  87.         $form->handleRequest($request);
  88.         if ($form->isSubmitted() && $form->isValid()) {
  89.             $notice $form->getData();
  90.             $notice->setUserId($user_id);
  91.             $notice->setProjectId($project_id);
  92.             $notice->setImportId($import_id);
  93.             $notice->setModified(new DateTime());
  94.             $notice->setModifiedBy($user_id);
  95.             // set created only once
  96.             if (!$notice->getCreatedBy()) {
  97.                 $notice->setCreated(new DateTime());
  98.                 $notice->setCreatedBy($user_id);
  99.             }
  100.             $entityManager->persist($notice);
  101.             $entityManager->flush();
  102.             $messageService->setMessage("Your notice was saved successfully.");
  103.             return $this->render("notice.sent.html.twig");
  104.         }
  105.         return $this->render("notice.html.twig", array(
  106.             "form" => $form->createView(),
  107.             "focus" => $context,
  108.             "notice" => $notice,
  109.             "created_by" => $createdBy,
  110.             "modified_by" => $modifiedBy,
  111.         ));
  112.     }
  113.     /**
  114.      * @Route("/get-notice-count", name="get_notice_count")
  115.      * @param Request $request
  116.      * @param MessageService $messageService
  117.      * @return Response
  118.      * @throws Exception
  119.      */
  120.     public function getNoticeCount(Request $requestMessageService $messageService)
  121.     {
  122.         if (!$this->getUser()) {
  123.             return new JsonResponse([
  124.                 "result" => false,
  125.                 "data" => null,
  126.             ]);
  127.         }
  128.         $user_id $this->getUser()->getId();
  129.         $entityManager $this->getDoctrine()->getManager();
  130.         $project_id $request->get("project_id"0);
  131.         $import_id $request->get("import_id"0);
  132.         // load notice if one exists for context global
  133.         $notice null;
  134.         if ($project_id == && $import_id == 0) {
  135.             $notice $entityManager->getRepository(Notice::class)->findOneBy([
  136.                 "user_id" => $user_id,
  137.                 "project_id" => $project_id,
  138.                 "import_id" => $import_id,
  139.             ]);
  140.         }
  141.         // if we have an project id load project notice
  142.         else if ($project_id && $import_id == 0) {
  143.             $notice $entityManager->getRepository(Notice::class)->findOneBy([
  144.                 "project_id" => $project_id,
  145.                 "import_id" => $import_id,
  146.             ]);
  147.         }
  148.         // if we even have an import id load import notice
  149.         else if ($project_id && $import_id 0) {
  150.             $notice $entityManager->getRepository(Notice::class)->findOneBy([
  151.                 "project_id" => $project_id,
  152.                 "import_id" => $import_id,
  153.             ]);
  154.         }
  155.         $count 0;
  156.         if ($notice) {
  157.             $count++;
  158.         }
  159.         return new JsonResponse([
  160.             "result" => true,
  161.             "data" => $count,
  162.         ]);
  163.     }
  164. }