src/Controller/TaskController.php line 45

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Job;
  4. use App\Entity\JobCheck;
  5. use App\Entity\Project;
  6. use App\Entity\Task;
  7. use App\Entity\Document;
  8. use App\Form\DocumentType;
  9. use App\Form\TaskType;
  10. use App\Form\TaskSearchType;
  11. use App\Repository\ConfigurationRepository;
  12. use App\Repository\DocumentRepository;
  13. use App\Repository\JobRepository;
  14. use App\Repository\ProjectRepository;
  15. use App\Repository\TaskRepository;
  16. use App\Repository\TierRepository;
  17. use App\Repository\UserRepository;
  18. use App\Service\FilActualite;
  19. use App\Service\ThumbnailService;
  20. use Doctrine\ORM\EntityManager;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  26. use Knp\Component\Pager\PaginatorInterface;
  27. use Symfony\Contracts\Translation\TranslatorInterface;
  28. use Asana;
  29. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  30. use Symfony\Component\Mime\FileinfoMimeTypeGuesser;
  31. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  32. class TaskController extends AbstractController
  33. {
  34.     /**
  35.      * @Route("/admin/task", name="task_index", methods={"GET","POST"})
  36.      * @Route("/api/task", name="api_task_index", methods={"GET","POST"})
  37.      * @Security("is_granted('ROLE_USER')")
  38.      */
  39.     public function index(PaginatorInterface $paginatorRequest $requestConfigurationRepository $configurationRepository,
  40.                           TaskRepository $taskRepositoryTranslatorInterface $translatorProjectRepository $projectRepository,
  41.                           TierRepository $tierRepositoryUserRepository $userRepository): Response
  42.     {
  43.         $config $configurationRepository->findOneById(1);
  44.         if (is_null($config)) {
  45.             $this->addFlash("warning"$translator->trans("Veuillez ajouter les éléments de configuration d'abord!"));
  46.             return $this->redirectToRoute('configuration_edit');
  47.         }
  48.         $client $request->query->get('client') != '' $request->query->get('client') : null;
  49.         $form $this->createForm(TaskSearchType::class, null,
  50.             ['priorties' => $config->getTaskPriortiesValues(), 'status' => $config->getTaskStatusValues(), 'em' => $this->getDoctrine()->getManager(),'client' => $client]
  51.         );
  52.         $form->handleRequest($request);
  53.         if ($form->isSubmitted()) {
  54.             $url $this->buildSearchUrl($request->request->all());
  55.             if (!empty($url)) {
  56.                 return $this->redirectToRoute('task_index'$url);
  57.             }
  58.         }
  59.         $limit = empty($request->query->get('limit')) ? 20 $request->query->get('limit');
  60.         $tasks $paginator->paginate(
  61.             $taskRepository->MyFindAll($request->query->all()), $request->query->getInt('page'1), $limit
  62.         );
  63.         $nombreTask count($taskRepository->MyFindAll($request->query->all())->getQuery()->getResult());
  64.         $nombrePage = ($nombreTask - ($nombreTask $limit)) / $limit;
  65.         if ($nombreTask $limit 0) {
  66.             $nombrePage++;
  67.         }
  68.         if ($this->isApiRoute($request)) {
  69.             $resultat = [
  70.                 'clients' => $this->getJsonClient($tierRepository),
  71.                 'projects' => $this->getJsonProject($projectRepository),
  72.                 'tasks' => $this->getJsonTasks($tasks),
  73.                 'nombrePages' => $nombrePage
  74.             ];
  75.             $response = new Response();
  76.             $response->setContent(json_encode($resultat));
  77.             $response->headers->set('Content-Type''application/json');
  78.             return $response;
  79.         }
  80.         return $this->render('task/index.html.twig', [
  81.             'form' => $form->createView(),
  82.             'tasks' => $tasks,
  83.             'avatar_path' => $this->getParameter('app.path.avatars'),
  84.             'priorities' => $config->getTaskPriortiesValues(),
  85.             'status' => $config->getTaskStatusValues(),
  86.             'users' => $userRepository->findAll(),
  87.             'clients' => $tierRepository->findBy(['archived' => false], ['name' => 'asc']),
  88.             'projects' => $projectRepository->findBy(['archived' => false], ['name' => 'asc']),
  89.         ]);
  90.     }
  91.     /**
  92.      * @Route("admin/task/new", name="task_new", methods={"GET","POST"})
  93.      * @Security("is_granted('ROLE_USER')")
  94.      */
  95.     public function new(Request $requestProjectRepository $projectRepositoryConfigurationRepository $configurationRepositoryTranslatorInterface $translator): Response
  96.     {
  97.         $config $configurationRepository->findOneById(1);
  98.         if (is_null($config)) {
  99.             $this->addFlash("warning"$translator->trans("Veuillez ajouter les éléments de configuration d'abord!"));
  100.             return $this->redirectToRoute('configuration_edit');
  101.         }
  102.         $task = new Task();
  103.         $form $this->createForm(TaskType::class, $task,
  104.             ['priorties' => $config->getTaskPriortiesValues(), 'status' => $config->getTaskStatusValues(), 'em' => $this->getDoctrine()->getManager()]
  105.         );
  106.         $form->handleRequest($request);
  107.         if ($form->isSubmitted() && $form->isValid()) {
  108.             $entityManager $this->getDoctrine()->getManager();
  109.             $project $projectRepository->find($request->request->all()['task']['project']);
  110.             if ($project->getExternalService()) {
  111.                 $asana = new Asana(array('personalAccessToken' => $project->getExternalService()->getKeyApi()));
  112.                 $asana->getWorkspaces();
  113.                 $workspaceId $asana->getData()[0]->gid;
  114.                 $projectId $project->getIdExterne(); // The project where we want to save our task
  115. // First we create the task
  116.                 $result $asana->createTask(array(
  117.                     'workspace' => $workspaceId// Workspace ID
  118.                     'name' => $task->getName(), // Name of task
  119.                     'assignee' => 'racem@nanogramme.fr'// Assign task to...
  120.                 ));
  121.                 $taskId $asana->getData()->gid// Here we have the id of the task that have been created
  122. // Now we do another request to add the task to a project
  123.                 $asana->addProjectToTask($taskId$projectId);
  124.                 $task->setIdExterne($taskId);
  125.             }
  126.             $entityManager->persist($task);
  127.             $entityManager->flush();
  128.             $this->addFlash("warning"$translator->trans("Nouvelle tâche ajoutée avec succès"));
  129.             return $this->redirectToRoute('task_edit', [
  130.                 'id' => $task->getId(),
  131.             ]);
  132.         }
  133.         if (!$request->isXmlHttpRequest()) {
  134.             return $this->redirectToRoute('task_index', [
  135.                 'a' => 'new'
  136.             ]);
  137.         }
  138.         return $this->render('task/new.html.twig', [
  139.             'task' => $task,
  140.             'form' => $form->createView(),
  141.         ]);
  142.     }
  143.     /**
  144.      * @Route("admin/task/new_ajax", name="new_task_ajax", methods={"GET","POST"})
  145.      * @Security("is_granted('ROLE_USER')")
  146.      */
  147.     public function newAjax(Request $requestTaskRepository $taskRepositoryTranslatorInterface $translator): Response
  148.     {
  149.         $sub_task $request->request->get('sub_task');
  150.         $id_parent_task $request->request->get('parent_task');
  151.         $parent_task $taskRepository->find($id_parent_task);
  152.         $task = clone $parent_task;
  153.         $task->setName($sub_task);
  154.         $task->setDescription('');
  155.         $task->setParent($parent_task);
  156.         $entityManager $this->getDoctrine()->getManager();
  157.         $entityManager->persist($task);
  158.         $entityManager->flush();
  159.         $route_task_show $this->get('router')->getRouteCollection()->get('task_show');
  160.         $path_task_show str_replace('{id}'$task->getId(), $route_task_show->getPath());
  161.         $tab['path_task_show'] = $path_task_show;
  162.         $tab['task_id'] = $task->getId();
  163.         $tab['task_name'] = $task->getName();
  164.         return new Response(json_encode($tab));
  165.     }
  166.     /**
  167.      * @Route("admin/task/url_termine_task", name="url_termine_task", methods={"GET","POST"})
  168.      * @Security("is_granted('ROLE_USER')")
  169.      */
  170.     public function urlTermineTask(Request $requestTaskRepository $taskRepositoryFilActualite $filActualiteTranslatorInterface $translator): Response
  171.     {
  172.         $task_id $request->request->get('task_id');
  173.         $task_type $request->request->get('task_type');
  174.         $termine $request->request->get('termine');
  175.         $task $taskRepository->find($task_id);
  176.         $entityManager $this->getDoctrine()->getManager();
  177.         if (($task_type == 'principal_task' || $task_type == 'list_task') && $termine == 'oui') {
  178.             $task->setTermine(true);
  179.             foreach ($task->getChildren() as $child) {
  180.                 $child->setTermine(true);
  181.                 $entityManager->persist($child);
  182.                 $entityManager->flush();
  183.             }
  184.             $entityManager->persist($task);
  185.             $entityManager->flush();
  186.         } elseif ($task_type == 'list_task' && $termine == 'non') {
  187.             $task->setTermine(false);
  188.             if ($task->getParent()) {
  189.                 $task_parent $task->getParent();
  190.                 $task_parent->setTermine(false);
  191.                 $entityManager->persist($task_parent);
  192.                 $entityManager->flush();
  193.             }
  194.             $entityManager->persist($task);
  195.             $entityManager->flush();
  196.         } elseif ($task_type == 'sub_task' && $termine == 'oui') {
  197.             $task->setTermine(true);
  198.             $entityManager->flush();
  199.         } elseif ($task_type == 'sub_task' && $termine == 'non') {
  200.             $task->setTermine(false);
  201.             $parent_task $task->getParent();
  202.             $parent_task->setTermine(false);
  203.             $entityManager->flush();
  204.         } elseif ($termine == 'non') {
  205.             $task->setTermine(false);
  206.             $entityManager->flush();
  207.         } elseif ($termine == 'oui') {
  208.             $task->setTermine(true);
  209.             $entityManager->flush();
  210.         }
  211.         $filActualite->closeReopenTask($task,$this->getUser());
  212.         return new Response('ok');
  213.     }
  214.     /**
  215.      * @Route("/task/start-new-job/{id}", name="task_start_new_job", methods={"GET"})
  216.      * @Security("is_granted('ROLE_USER')")
  217.      */
  218.     public function TaskStartNewJob(Task $taskRequest $requestTaskRepository $taskRepositoryTranslatorInterface $translatorJobRepository $jobRepository): Response
  219.     {
  220.         $em $this->getDoctrine()->getManager();
  221.         $oldJob null;
  222.         if (is_null($task->getUser())) {
  223.             $task->setUser($this->getUser());
  224.             $em->flush();
  225.         }
  226.         $oldJob $this->getLastJobForUser($jobRepository);
  227.         if (!is_null($oldJob)) {
  228.             $now = new \DateTime('now');
  229.             $end = new \DateTime();
  230.             $end->setTimestamp($now->getTimestamp());
  231.             $oldJob->setEnd($end);
  232.             $this->createjobCheck($em$oldJob, []);
  233.         }
  234.         $job $this->createJob($em$task$oldJob, []);
  235.         //Create JobCheck
  236.         $this->createjobCheck($em$job, []);
  237.         $this->addFlash('success''Vous venez de démarrer un nouveau job sur cette tâche');
  238.         return $this->redirectToRoute('task_index', [
  239.             'a' => 'show',
  240.             'e' => $task->getId()
  241.         ]);
  242.     }
  243.     /**
  244.      * @Route("/api/task/select", name="api_task_select", methods={"POST"})
  245.      */
  246.     public function apiSelectTask(Request $requestTaskRepository $taskRepositoryTranslatorInterface $translatorJobRepository $jobRepository): Response
  247.     {
  248.         $data $request->request->all();
  249.         $em $this->getDoctrine()->getManager();
  250.         $oldJob null;
  251.         $task $taskRepository->findOneById($data['task']);
  252.         if (is_null($task)) {
  253.             $response = new Response();
  254.             $response->setContent(json_encode(['Resultat' => 'NO']));
  255.             $response->headers->set('Content-Type''application/json');
  256.             return $response;
  257.         }
  258.         //If check close old Job and open new Job
  259.         if (isset($data['depuis'])) {
  260.             $oldJob $this->getLastJobForUser($jobRepository);
  261.             if (!is_null($oldJob)) {
  262.                 $now = new \DateTime('now');
  263.                 $end = new \DateTime();
  264.                 $end->setTimestamp($now->getTimestamp() - $data['depuis']);
  265.                 $oldJob->setEnd($end);
  266.                 $this->createjobCheck($em$oldJob$data);
  267.             }
  268.         }
  269.         //create new JOB
  270.         $job $this->createJob($em$task$oldJob$data);
  271.         //Create JobCheck
  272.         $this->createjobCheck($em$job$data);
  273.         $response = new Response();
  274.         $response->setContent(json_encode(['task' => $task->getId(), 'job' => $job->getId()]));
  275.         $response->headers->set('Content-Type''application/json');
  276.         return $response;
  277.     }
  278.     /**
  279.      * @Route("/api/closejob", name="api_close_job", methods={"GET"})
  280.      */
  281.     public function apiCloseJob(Request $requestTaskRepository $taskRepositoryTranslatorInterface $translatorJobRepository $jobRepository): Response
  282.     {
  283.         $data $request->request->all();
  284.         $em $this->getDoctrine()->getManager();
  285.         $oldJob null;
  286.         $oldJob $this->getLastJobForUser($jobRepository);
  287.         if (!is_null($oldJob) && is_null($oldJob->getEnd())) {
  288.             $now = new \DateTime('now');
  289.             $end = new \DateTime();
  290.             $end->setTimestamp($now->getTimestamp());
  291.             $oldJob->setEnd($end);
  292.             if (!isset($data['display_delay'])) {
  293.                 $data['display_delay'] = 0;
  294.             }
  295.             if (!isset($data['window_type'])) {
  296.                 $data['window_type'] = 11;
  297.             }
  298.             $this->createjobCheck($em$oldJob$data);
  299.         }
  300.         $response = new Response();
  301.         $response->setContent(json_encode(['result' => 'ok']));
  302.         $response->headers->set('Content-Type''application/json');
  303.         return $response;
  304.     }
  305.     /**
  306.      * @Route("/api/task/pause", name="api_task_pause", methods={"POST"})
  307.      */
  308.     public
  309.     function apiPause(Request $requestJobRepository $jobRepositoryTaskRepository $taskRepositoryTranslatorInterface $translator): Response
  310.     {
  311.         $data $request->request->all();
  312.         $em $this->getDoctrine()->getManager();
  313.         $oldJob null;
  314.         $file __DIR__.'/../../private_documents/task.txt';
  315.         $tab = [];
  316.         if(file_exists($file)){
  317.             $tab json_decode(file_get_contents($file),true);
  318.         }
  319.         $tab[] = $data;
  320.         file_put_contents($file,json_encode($data));
  321.         //Close old JOB
  322.         $oldJob $this->getLastJobForUser($jobRepository);
  323.         if (is_null($oldJob)) {
  324.             $task $taskRepository->findOneById(['user'=>$this->getUser()],['id'=>'DESC']);
  325.             $job $this->createJob($em$tasknull$datafalse);
  326.             //Create JobCheck
  327.             $this->createjobCheck($em$job$data);
  328.             $response = new Response();
  329.             $response->setContent(json_encode(['task' => $task->getId(), 'job' => $job->getId()]));
  330.             $response->headers->set('Content-Type''application/json');
  331.             return $response;
  332.         }elseif($oldJob->getId() != $data['job']){
  333.             $now = new \DateTime('now');
  334.             $time $now->getTimestamp() - $oldJob->getStart()->getTimestamp();
  335.             $current = [
  336.                 'Resultat' => 'OK',
  337.                 'id' => $oldJob->getId(),
  338.                 'jobDescription' => $oldJob->getDescription(),
  339.                 'break' => ($oldJob->getBreak()) ? 'yes' 'no',
  340.                 'time' => $time,
  341.                 'taskId' => $oldJob->getTask()->getId(),
  342.                 'taskName' => $oldJob->getTask()->getName(),
  343.                 'taskDescription' => $oldJob->getTask()->getDescription(),
  344.                 'project' => is_null($oldJob->getTask()->getProject()) ? '' $oldJob->getTask()->getProject()->getName(),
  345.                 'client' => is_null($oldJob->getTask()->getClient()) ? '' $oldJob->getTask()->getClient()->getName(),
  346.             ];
  347.             $response = new Response();
  348.             $response->setContent(json_encode($current));
  349.             $response->headers->set('Content-Type''application/json');
  350.             return $response;
  351.         }
  352.         $now = new \DateTime('now');
  353.         $oldJob->setEnd($now);
  354.         $this->createjobCheck($em$oldJob$data);
  355.         $break = ($data['break'] == 'yes') ? true false;
  356.         //create new JOB
  357.         $task $taskRepository->findOneById($data['task']);
  358.         $job $this->createJob($em$task$oldJob$data$break);
  359.         //Create JobCheck
  360.         $this->createjobCheck($em$job$data);
  361.         $response = new Response();
  362.         $response->setContent(json_encode(['task' => $task->getId(), 'job' => $job->getId()]));
  363.         $response->headers->set('Content-Type''application/json');
  364.         return $response;
  365.     }
  366.     /**
  367.      * @Route("/api/task/new", name="api_task_new", methods={"POST"})
  368.      */
  369.     public
  370.     function apiNew(Request $requestTranslatorInterface $translatorFilActualite $filActualiteJobRepository $jobRepository): Response
  371.     {
  372.         $data $request->request->all();
  373.         $em $this->getDoctrine()->getManager();
  374.         $oldJob null;
  375.         //Created Task
  376.         $task $this->createTask($data$em);
  377.         $filActualite->createTask($task,$this->getUser());
  378.         //If check close old Job and open new Job
  379.         if (isset($data['depuis'])) {
  380.             $oldJob $this->getLastJobForUser($jobRepository);
  381.             if (!is_null($oldJob)) {
  382.                 $now = new \DateTime('now');
  383.                 $end = new \DateTime();
  384.                 $end->setTimestamp($now->getTimestamp() - $data['depuis']);
  385.                 $oldJob->setEnd($end);
  386.                 $this->createjobCheck($em$oldJob$data);
  387.             }
  388.         }
  389.         //create new JOB
  390.         $job $this->createJob($em$task$oldJob$data);
  391.         //Create JobCheck
  392.         $this->createjobCheck($em$job$data);
  393.         $response = new Response();
  394.         $response->setContent(json_encode(['task' => $task->getId(), 'job' => $job->getId()]));
  395.         $response->headers->set('Content-Type''application/json');
  396.         return $response;
  397.     }
  398.     /**
  399.      * @Route("/api/jobcheck", name="api_job_check", methods={"POST"})
  400.      */
  401.     public
  402.     function apiJobCheck(Request $requestJobRepository $jobRepositoryTranslatorInterface $translator): Response
  403.     {
  404.         $data $request->request->all();
  405.         $em $this->getDoctrine()->getManager();
  406.         //$jobClient = $jobRepository->findOneById($data['job']);
  407.         $job $jobRepository->findOneBy(['user' => $this->getUser(),'end'=>null], ['id' => 'desc']);
  408.         if (is_null($job)) {
  409.             $response = new Response();
  410.             $response->setContent(json_encode(['Resultat' => 'NO']));
  411.             $response->headers->set('Content-Type''application/json');
  412.             return $response;
  413.         }
  414.         $now = new \DateTime('now');
  415.         $time $now->getTimestamp() - $job->getStart()->getTimestamp();
  416.         $this->createjobCheck($em$job$data);
  417.         $current = [
  418.             'Resultat' => 'OK',
  419.             'id' => $job->getId(),
  420.             'jobDescription' => $job->getDescription(),
  421.             'break' => ($job->getBreak()) ? 'yes' 'no',
  422.             'time' => $time,
  423.             'taskId' => $job->getTask()->getId(),
  424.             'taskName' => $job->getTask()->getName(),
  425.             'taskDescription' => $job->getTask()->getDescription(),
  426.             'project' => is_null($job->getTask()->getProject()) ? '' $job->getTask()->getProject()->getName(),
  427.             'client' => is_null($job->getTask()->getClient()) ? '' $job->getTask()->getClient()->getName(),
  428.         ];
  429.         $response = new Response();
  430.         $response->setContent(json_encode($current));
  431.         $response->headers->set('Content-Type''application/json');
  432.         return $response;
  433.     }
  434.     /**
  435.      * @Route("/api/currentjob", name="api_job_current", methods={"GET"})
  436.      */
  437.     public
  438.     function apiCurrentJob(JobRepository $jobRepositoryTranslatorInterface $translator): Response
  439.     {
  440.         $job $jobRepository->findOneBy(['user' => $this->getUser()], ['id' => 'desc']);
  441.         if (is_null($job) or !is_null($job->getEnd())) {
  442.             $response = new Response();
  443.             $response->setContent(json_encode(['Resultat' => 'NO']));
  444.             $response->headers->set('Content-Type''application/json');
  445.             return $response;
  446.         }
  447.         $now = new \DateTime('now');
  448.         $time $now->getTimestamp() - $job->getStart()->getTimestamp();
  449.         $current = [
  450.             'Resultat' => 'OK',
  451.             'id' => $job->getId(),
  452.             'jobDescription' => $job->getDescription(),
  453.             'break' => ($job->getBreak()) ? 'yes' 'no',
  454.             'time' => $time,
  455.             'taskId' => $job->getTask()->getId(),
  456.             'taskName' => $job->getTask()->getName(),
  457.             'taskDescription' => $job->getTask()->getDescription(),
  458.             'project' => is_null($job->getTask()->getProject()) ? '' $job->getTask()->getProject()->getName(),
  459.             'client' => is_null($job->getTask()->getClient()) ? '' $job->getTask()->getClient()->getName(),
  460.         ];
  461.         $response = new Response();
  462.         $response->setContent(json_encode($current));
  463.         $response->headers->set('Content-Type''application/json');
  464.         return $response;
  465.     }
  466.     /**
  467.      * @Route("/admin/task/task_search_advenced", name="task_search_advenced", methods={"GET","POST"})
  468.      * @Security("is_granted('ROLE_USER')")
  469.      */
  470.     public function taskSearchAdvenced(Request $requestConfigurationRepository $configurationRepositoryTranslatorInterface $translator): Response
  471.     {
  472.         $config $configurationRepository->findOneById(1);
  473.         $task = new Task();
  474.         if (is_null($config)) {
  475.             $this->addFlash("warning"$translator->trans("Veuillez ajouter les éléments de configuration d'abord!"));
  476.             return $this->redirectToRoute('configuration_edit');
  477.         }
  478.         $form $this->createForm(TaskSearchType::class, $task,
  479.             ['priorties' => $config->getTaskPriortiesValuesSearch(), 'status' => $config->getTaskStatusValuesSearch(), 'em' => $this->getDoctrine()->getManager(), 'data' => $request->query->all()]
  480.         );
  481.         $form->handleRequest($request);
  482.         if ($form->isSubmitted()) {
  483.             $url $this->buildSearchUrl($request->request->all());
  484.             if (!empty($url)) {
  485.                 return $this->redirectToRoute('task_index'$url);
  486.             }
  487.         }
  488.         return $this->render('task/recherche_avancer.html.twig', [
  489.             'form' => $form->createView(),
  490.         ]);
  491.     }
  492.     /**
  493.      * @Route("/admin/task/{id}", name="task_show", methods={"GET","POST"})
  494.      * @Security("is_granted('ROLE_USER')")
  495.      */
  496.     public function show(Task $taskConfigurationRepository $configurationRepositoryUserRepository $userRepositoryRequest $request,
  497.                          DocumentRepository $documentRepositoryThumbnailService $thumbnailServiceFilActualite $filActualite): Response
  498.     {
  499.         if (!$request->isXmlHttpRequest()) {
  500.             return $this->redirectToRoute('task_index', [
  501.                 'a' => 'show',
  502.                 'e' => $task->getId()
  503.             ]);
  504.         }
  505.         $em $this->getDoctrine()->getManager();
  506.         $document = new Document();
  507.         $form $this->createForm(DocumentType::class, $document);
  508.         $form->handleRequest($request);
  509.         $documentPath is_null($task->getProject()) ? 'x/'.$task->getId() : $task->getProject()->getId().'/'.$task->getId().'/';
  510.         if ($form->isSubmitted() && $form->isValid()) {
  511.             $data $request->request->all();
  512.             //dump($data['document']['originalName']);exit;
  513.             $document->setTask($task);
  514.             $document->setOriginalName($data['document']['originalName']);
  515.             $em->persist($document);
  516.             $em->flush();
  517.             $thumbnailService->createThumbnail($document);
  518.             $filActualite->addFile($task,$this->getUser(),$document);
  519.             return $this->render('task/elements/documents.html.twig', [
  520.                 'task' => $task,
  521.                 'documentPath' => $this->getParameter("app.path.documents").$documentPath,
  522.                 'imagePath' => substr($this->getParameter("app.path.documents_image"),1).$documentPath,
  523.             ]);
  524.         }
  525.         $conf $configurationRepository->findOneById(1);
  526.         return $this->render('task/show.html.twig', [
  527.             'task' => $task,
  528.             'priorities' => $conf->getTaskPriortiesValues(),
  529.             'status' => $conf->getTaskStatusValues(),
  530.             'avatar_path' => $this->getParameter('app.path.avatars'),
  531.             'users' => $userRepository->findAll(),
  532.             'cost' => $task->getDisplayDureeAndCostPerUser(),
  533.             'totaux' => $task->getTotaux(),
  534.             'conf' => $conf,
  535.             'form' => $form->createView(),
  536.             "commentaires" => $filActualite->getComments($task,$this->getUser()),
  537.             'documentPath' => $this->getParameter("app.path.documents").$documentPath,
  538.             'imagePath' => substr($this->getParameter("app.path.documents_image"),1).$documentPath,
  539.         ]);
  540.     }
  541.     /**
  542.      * @Route("/admin/task/download/{name}", name="doc_download", methods={"GET"})
  543.      * @Security("is_granted('ROLE_USER')")
  544.      */
  545.     public function downloadDoc(Document $document): Response
  546.     {
  547.         $folder dirname(__FILE__).'/../..' $this->getParameter('app.path.documents');
  548.         $folder .= $document->getTask()->getProject() ? $document->getTask()->getProject()->getId() : 'x';
  549.         $folder .= '/'$document->getTask()->getId().'/';
  550.         $filename $document->getName();
  551.         // This should return the file to the browser as response
  552.         $response = new BinaryFileResponse($folder.$filename);
  553.         // To generate a file download, you need the mimetype of the file
  554.         $mimeTypeGuesser = new FileinfoMimeTypeGuesser();
  555.         // Set the mimetype with the guesser or manually
  556.         if($mimeTypeGuesser->isGuesserSupported()){
  557.             // Guess the mimetype of the file according to the extension of the file
  558.             $response->headers->set('Content-Type'$mimeTypeGuesser->guessMimeType($folder.$filename));
  559.         }else{
  560.             // Set the mimetype of the file manually, in this case for a text file is text/plain
  561.             $response->headers->set('Content-Type''text/plain');
  562.         }
  563.         // Set content disposition inline of the file
  564.         $response->setContentDisposition(
  565.             ResponseHeaderBag::DISPOSITION_ATTACHMENT,
  566.             $filename
  567.         );
  568.         return $response;
  569.     }
  570.     /**
  571.      * @Route("/admin/task/{id}/edit", name="task_edit", methods={"GET","POST"})
  572.      * @Security("is_granted('ROLE_USER')")
  573.      */
  574.     /*public
  575.     function edit(Request $request, Task $task, ConfigurationRepository $configurationRepository, TranslatorInterface $translator): Response
  576.     {
  577.         $config = $configurationRepository->findOneById(1);
  578.         if (is_null($config)) {
  579.             $this->addFlash("warning", $translator->trans("Veuillez ajouter les éléments de configuration d'abord!"));
  580.             return $this->redirectToRoute('configuration_edit');
  581.         }
  582.         $form = $this->createForm(TaskType::class, $task,
  583.             ['priorties' => $config->getTaskPriortiesValues(), 'status' => $config->getTaskStatusValues(), 'em' => $this->getDoctrine()->getManager()]
  584.         );
  585.         $form->handleRequest($request);
  586.         if ($form->isSubmitted() && $form->isValid()) {
  587.             $this->getDoctrine()->getManager()->flush();
  588.             $this->addFlash("warning", $translator->trans("Tâche modifiée avec succès"));
  589.             return $this->redirectToRoute('task_edit', [
  590.                 'id' => $task->getId(),
  591.             ]);
  592.         }
  593.         return $this->render('task/edit.html.twig', [
  594.             'task' => $task,
  595.             'form' => $form->createView(),
  596.         ]);
  597.     }*/
  598.     /**
  599.      * @Route("/admin/task/{id}", name="task_delete", methods={"DELETE"})
  600.      */
  601.     public
  602.     function delete(Request $requestTask $taskTranslatorInterface $translator): Response
  603.     {
  604.         if ($this->isCsrfTokenValid('delete' $task->getId(), $request->request->get('_token'))) {
  605.             $entityManager $this->getDoctrine()->getManager();
  606.             $entityManager->remove($task);
  607.             $entityManager->flush();
  608.             $this->addFlash("success"$translator->trans("Tâche supprimée avec succès"));
  609.         }
  610.         return $this->redirectToRoute('task_index');
  611.     }
  612.     /**
  613.      * @param $data
  614.      * @return array
  615.      */
  616.     private
  617.     function buildSearchUrl($data)
  618.     {
  619.         $tab = [];
  620.         $data $data['task_search'];
  621.         $restriction = [];
  622.         foreach ($data as $key => $value) {
  623.             if ($value === '' or $key == '_token') {
  624.                 continue;
  625.             }
  626.             if (!in_array($value$restriction)) {
  627.                 $tab[$key] = $value;
  628.             }
  629.         }
  630.         return $tab;
  631.     }
  632.     /**
  633.      * @param Request $request
  634.      * @return bool
  635.      */
  636.     private
  637.     function isApiRoute(Request $request)
  638.     {
  639.         $route $request->attributes->get('_route');
  640.         if (substr($route04) === 'api_') {
  641.             return true;
  642.         }
  643.         return false;
  644.     }
  645.     private
  646.     function getJsonTasks($tasks)
  647.     {
  648.         $tab = [];
  649.         foreach ($tasks as $task) {
  650.             $tab[] = [
  651.                 'id' => $task->getId(),
  652.                 'name' => $task->getName(),
  653.                 'priority' => $task->getPriority(),
  654.                 'status' => $task->getStatus(),
  655.                 'project' => is_null($task->getProject()) ? '' $task->getProject()->getId(),
  656.                 'client' => is_null($task->getClient()) ? '' $task->getClient()->getId(),
  657.             ];
  658.         }
  659.         return $tab;
  660.     }
  661.     private
  662.     function getJsonProject(ProjectRepository $projectRepository)
  663.     {
  664.         $tab = [];
  665.         $projects $projectRepository->findAll();
  666.         foreach ($projects as $project) {
  667.             if (!$project->getArchived()) {
  668.                 $tab[] = [
  669.                     'id' => $project->getId(),
  670.                     'name' => $project->getName(),
  671.                     'client' => is_null($project->getClient()) ? '' $project->getClient()->getId(),
  672.                 ];
  673.             }
  674.         }
  675.         return $tab;
  676.     }
  677.     private
  678.     function getJsonClient(TierRepository $tierRepository)
  679.     {
  680.         $tab = [];
  681.         $tiers $tierRepository->findAll();
  682.         foreach ($tiers as $tier) {
  683.             if (!$tier->getArchived()) {
  684.                 $tab[] = [
  685.                     'id' => $tier->getId(),
  686.                     'name' => $tier->getName(),
  687.                 ];
  688.             }
  689.         }
  690.         return $tab;
  691.     }
  692.     /**
  693.      * @param null $id
  694.      * @return |null
  695.      */
  696.     private
  697.     function getProjectById($id null)
  698.     {
  699.         if (empty($id)) {
  700.             return null;
  701.         }
  702.         $em $this->getDoctrine()->getManager();
  703.         return $em->getRepository('App\Entity\Project')->findOneById($id);
  704.     }
  705.     /**
  706.      * @param null $id
  707.      * @return |null
  708.      */
  709.     private
  710.     function getClientById($id null)
  711.     {
  712.         if (empty($id)) {
  713.             return null;
  714.         }
  715.         $em $this->getDoctrine()->getManager();
  716.         return $em->getRepository('App\Entity\Tier')->findOneById($id);
  717.     }
  718.     /**
  719.      * @return object[]
  720.      */
  721.     private
  722.     function getLastJobForUser(JobRepository $jobRepository)
  723.     {
  724.         return $jobRepository->findOneBy(['user' => $this->getUser(),"end"=>null], ['id' => 'DESC']);
  725.     }
  726.     private
  727.     function createTask($dataEntityManager $entityManager)
  728.     {
  729.         $task = new Task();
  730.         $task->setName($data['name']);
  731.         $task->setDescription($data['description']);
  732.         $task->setClient($this->getClientById($data['client']));
  733.         $task->setProject($this->getProjectById($data['project']));
  734.         $task->setUser($this->getUser());
  735.         $entityManager->persist($task);
  736.         $entityManager->flush();
  737.         return $task;
  738.     }
  739.     private
  740.     function createJob(EntityManager $entityManagerTask $taskJob $oldJob null$data$isBreak false)
  741.     {
  742.         $job = new Job();
  743.         $job->setTask($task);
  744.         $job->setUser($this->getUser());
  745.         if (empty($data)) {
  746.             $data['jobDescription'] = '';
  747.         }
  748.         $start = !is_null($oldJob) ? $oldJob->getEnd() : new \DateTime('now');
  749.         $job->setStart($start);
  750.         $job->setDescription($data['jobDescription']);
  751.         $job->setBreak($isBreak);
  752.         $entityManager->persist($job);
  753.         $entityManager->flush();
  754.         return $job;
  755.     }
  756.     private
  757.     function createjobCheck(EntityManager $entityManagerJob $job$data)
  758.     {
  759.         $check = new JobCheck();
  760.         $date = new \DateTime("now");
  761.         //$start = $job->getStart();
  762.         if (empty($data)) {
  763.             $data['display_delay'] = 0;
  764.             $data['window_type'] = 12;
  765.         }
  766.         $check->setUser($this->getUser());
  767.         $check->setJob($job);
  768.         $check->setClick($date);
  769.         $display = new \DateTime('now');
  770.         $display->setTimestamp($date->getTimestamp() - $data['display_delay']);
  771.         $check->setDisplay($display);
  772.         $check->setWindowType($data['window_type']);
  773.         $entityManager->persist($check);
  774.         $entityManager->flush();
  775.         return $check;
  776.     }
  777.     /**
  778.      * @Route("/save_user_task/{id}", name="save_user_task", methods="GET|POST")
  779.      * @Security("is_granted('ROLE_USER')")
  780.      */
  781.     public function saveUserTask(Task $taskUserRepository $userRepositoryRequest $requestFilActualite $filActualite): Response
  782.     {
  783.         $user_id $request->request->all()['user_id'];
  784.         $user $userRepository->find($user_id);
  785.         $task->setUser($user);
  786.         $this->getDoctrine()->getManager()->flush();
  787.         $filActualite->modifyTask($task,$this->getUser(),'User');
  788.         return new Response('ok');
  789.     }
  790.     /**
  791.      * @Route("/update_status_ajax/{id}", name="update_status_ajax", methods="GET|POST")
  792.      * @Security("is_granted('ROLE_USER')")
  793.      */
  794.     public function updateStatusAjax(Task $taskRequest $requestFilActualite $filActualite): Response
  795.     {
  796.         $status $request->request->all()['status'];
  797.         $task->setStatus($status);
  798.         $this->getDoctrine()->getManager()->flush();
  799.         $filActualite->modifyTask($task,$this->getUser(),'Status');
  800.         return new Response('ok');
  801.     }
  802.     /**
  803.      * @Route("/update_priority_ajax/{id}", name="update_priority_ajax", methods="GET|POST")
  804.      * @Security("is_granted('ROLE_USER')")
  805.      */
  806.     public function updatePriorityAjax(Task $taskRequest $requestFilActualite $filActualite): Response
  807.     {
  808.         $priority $request->request->all()['priority'];
  809.         $task->setPriority($priority);
  810.         $this->getDoctrine()->getManager()->flush();
  811.         $filActualite->modifyTask($task,$this->getUser(),'Priority');
  812.         return new Response('ok');
  813.     }
  814.     /**
  815.      * @Route("/url_update_titre/{id}", name="url_update_titre", methods="GET|POST")
  816.      * @Security("is_granted('ROLE_USER')")
  817.      */
  818.     public function urlUpdateTitre(Task $taskRequest $requestFilActualite $filActualite): Response
  819.     {
  820.         $titre $request->request->all()['task_titre'];
  821.         $task->setName($titre);
  822.         $this->getDoctrine()->getManager()->flush();
  823.         $filActualite->modifyTask($task,$this->getUser(),'Name');
  824.         return new Response('ok');
  825.     }
  826.     /**
  827.      * @Route("/url_update_description/{id}", name="url_update_description", methods="GET|POST")
  828.      * @Security("is_granted('ROLE_USER')")
  829.      */
  830.     public function urlUpdateDescription(Task $taskRequest $requestFilActualite $filActualite): Response
  831.     {
  832.         $description $request->request->all()['task_description'];
  833.         $task->setDescription($description);
  834.         $this->getDoctrine()->getManager()->flush();
  835.         $filActualite->modifyTask($task,$this->getUser(),'Description');
  836.         return new Response('ok');
  837.     }
  838.     /**
  839.      * @Route("/url_update_date_echeance/{id}", name="url_update_date_echeance", methods="GET|POST")
  840.      * @Security("is_granted('ROLE_USER')")
  841.      */
  842.     public function urlUpdateDateEcheance(Task $taskRequest $requestFilActualite $filActualite): Response
  843.     {
  844.         $date_ech $request->request->all()['date_ech'];
  845.         if ($date_ech != '') {
  846.             $task->setDate(new \DateTime($date_ech));
  847.         } else {
  848.             $task->setDate(null);
  849.         }
  850.         $this->getDoctrine()->getManager()->flush();
  851.         $filActualite->modifyTask($task,$this->getUser(),'Date');
  852.         return new Response('ok');
  853.     }
  854.     /**
  855.      * @Route("/url_update_client_project/{id}", name="url_update_client_project", methods="GET|POST")
  856.      * @Security("is_granted('ROLE_USER')")
  857.      */
  858.     public function urlUpdateClientProject(Task $taskRequest $requestTierRepository $tierRepositoryProjectRepository $projectRepositoryFilActualite $filActualite): Response
  859.     {
  860.         $oldProject is_null($task->getProject()) ? 'x' $task->getProject()->getId();
  861.         $data $request->request->all();
  862.         if (isset($data['client_id'])) {
  863.             if ($data['client_id'] != 0) {
  864.                 $client $tierRepository->find($data['client_id']);
  865.                 $task->setClient($client);
  866.             } else {
  867.                 $task->setClient(null);
  868.             }
  869.             $task->setProject(null);
  870.         }
  871.         if (isset($data['project_id'])) {
  872.             if ($data['project_id'] != 0) {
  873.                 $project $projectRepository->find($data['project_id']);
  874.                 $task->setProject($project);
  875.             } else {
  876.                 $task->setProject(null);
  877.             }
  878.         }
  879.         $this->getDoctrine()->getManager()->flush();
  880.         $filActualite->modifyTask($task,$this->getUser(),'Project',$oldProject);
  881.         return new Response('ok');
  882.     }
  883.     /**
  884.      * @Route("/url_new_task_ajax", name="url_new_task_ajax", methods="GET|POST")
  885.      * @Security("is_granted('ROLE_USER')")
  886.      */
  887.     public function newTaskAjax(Request $requestConfigurationRepository $configurationRepository,
  888.                                 TierRepository $tierRepositoryProjectRepository $projectRepository,
  889.                                 UserRepository $userRepositoryFilActualite $filActualite): Response
  890.     {
  891.         $config $configurationRepository->find(1);
  892.         $priorities $config->getTaskPriortiesValues();
  893.         $status $config->getTaskStatusValues();
  894.         $task = new Task();
  895.         $data $request->request->all();
  896.         $name_task $data['name_task'];
  897.         $hide_col $data['hide_col'];
  898.         if (isset($data['client']) && !empty($data['client'])) {
  899.             $client $tierRepository->find($data['client']);
  900.             $task->setClient($client);
  901.         }
  902.         if (isset($data['project']) && !empty($data['project'])) {
  903.             $project $projectRepository->find($data['project']);
  904.             $task->setProject($project);
  905.             $task->setClient($project->getClient());
  906.         }
  907.         if (isset($data['user']) && !empty($data['user'])) {
  908.             $user $userRepository->find($data['user']);
  909.             $task->setUser($user);
  910.         }
  911.         $task->setName($name_task);
  912.         $entityManager $this->getDoctrine()->getManager();
  913.         $entityManager->persist($task);
  914.         $entityManager->flush();
  915.         $filActualite->createTask($task,$this->getUser());
  916.         $color_border '';
  917.         $project_name '';
  918.         if ($task->getProject()) {
  919.             if ($task->getProject()->getColor() != '') {
  920.                 $color_border 'border-left: 3px solid ' $task->getProject()->getColor();
  921.             }
  922.             $project_name $task->getProject()->getName();
  923.         }
  924.         if (!$task->getUser()) {
  925.             $tab_jason['user_task'] = 'avatar_1';
  926.         } elseif ($task->getUser()->getAvatar() == '' || !file_exists(substr($this->getParameter('app.path.avatars'), 1) . $task->getUser()->getAvatar())) {
  927.             $tab_jason['user_task'] = 'avatar_2';
  928.             $tab_jason['user_task_color'] = $task->getUser()->getColor();
  929.             $tab_jason['user_task_name'] = $task->getUser()->getPrenomNom();
  930.             $tab_jason['user_task_abrv'] = $task->getUser()->getAbrev();
  931.         } else {
  932.             $tab_jason['user_task'] = 'avatar_3';
  933.             $tab_jason['user_task_avatar'] = $this->getParameter('app.path.avatars') . $task->getUser()->getAvatar();
  934.             $tab_jason['user_task_name'] = $task->getUser()->getPrenomNom();
  935.         }
  936.         if (isset($data['status']) && !empty($data['status'])) {
  937.             $tab_jason['status'] = 'status_1';
  938.             $tab_jason['status_background'] = $status[$data['status']]['background'];
  939.             $tab_jason['status_color'] = $status[$data['status']]['color'];
  940.             $tab_jason['status_value'] = $data['status'];
  941.         }else{
  942.             $tab_jason['status'] = 'status_2';
  943.             $tab_jason['status_value'] = '-';
  944.         }
  945.         if (isset($data['priority']) && !empty($data['priority'])) {
  946.             $tab_jason['status'] = 'priority_1';
  947.             $tab_jason['priority_background'] = $priorities[$data['priority']]['background'];
  948.             $tab_jason['priority_color'] = $priorities[$data['priority']]['color'];
  949.             $tab_jason['priority_value'] = $data['priority'];
  950.         }else{
  951.             $tab_jason['priority'] = 'priority_2';
  952.             $tab_jason['priority_value'] = '';
  953.         }
  954.         $tab_jason['task_id'] = $task->getId();
  955.         $tab_jason['color_border'] = $color_border;
  956.         $tab_jason['name_task'] = $task->getName();
  957.         $tab_jason['name_project'] = $project_name;
  958.         $tab_jason['hide_col'] = $hide_col;
  959.         return new Response(json_encode($tab_jason));
  960.     }
  961.     /**
  962.      * @Route("/save-comment/{id}", name="save_comment", methods="POST")
  963.      * @Security("is_granted('ROLE_USER')")
  964.      */
  965.     public function saveComment(Request $requestTask $taskFilActualite $filActualiteUserRepository $userRepository): Response
  966.     {
  967.         $filActualite->saveComment($task$this->getUser(), $request->request->get('comment'));
  968.         return $this->render('task/elements/commentaires.html.twig', [
  969.             'task' => $task,
  970.             'avatar_path' => $this->getParameter('app.path.avatars'),
  971.             'users' => $userRepository->findAll(),
  972.             "commentaires" => $filActualite->getComments($task,$this->getUser()),
  973.         ]);
  974.     }
  975. }