src/Controller/TaskController.php line 821

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Project;
  4. use App\Entity\Task;
  5. use App\Entity\User;
  6. use App\Form\CallPersonToAssignTaskType;
  7. use App\Form\LoggedTimeTypeType;
  8. use App\Form\TaskDelayedRangeType;
  9. use App\Form\TaskFilterType;
  10. use App\Form\TaskType;
  11. use App\Module\Slack\SlackManager;
  12. use App\Repository\OfferPackageTaskRepository;
  13. use App\Repository\ProjectRepository;
  14. use App\Repository\TaskRepository;
  15. use App\Repository\UserRepository;
  16. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\HttpFoundation\JsonResponse;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use Symfony\Contracts\Translation\TranslatorInterface;
  23. /**
  24.  * Class TaskController
  25.  *
  26.  * @package App\Controller
  27.  * @author Lukasz Krakowiak <lukasz94krakowiak@gmail.com>
  28.  * @Route("/zadania")
  29.  */
  30. class TaskController extends AbstractController
  31. {
  32.     /**
  33.      * @var ProjectRepository
  34.      */
  35.     private ProjectRepository $projectRepository;
  36.     /**
  37.      * @var TaskRepository
  38.      */
  39.     private TaskRepository $taskRepository;
  40.     /**
  41.      * @var TranslatorInterface
  42.      */
  43.     private TranslatorInterface $translator;
  44.     /**
  45.      * @var bool
  46.      */
  47.     private bool $isList false;
  48.     /**
  49.      * @param ProjectRepository $projectRepository
  50.      * @param TaskRepository $taskRepository
  51.      * @param TranslatorInterface $translator
  52.      */
  53.     public function __construct(ProjectRepository $projectRepositoryTaskRepository $taskRepositoryTranslatorInterface $translator)
  54.     {
  55.         $this->projectRepository $projectRepository;
  56.         $this->taskRepository $taskRepository;
  57.         $this->translator $translator;
  58.     }
  59.     /**
  60.      * @Route("/", name="task_index")
  61.      */
  62.     public function index(UserRepository $userRepository): Response
  63.     {
  64.         return $this->render('task/index.html.twig', [
  65.             'users' => $userRepository->getUserByRole("ROLE_EMPLOYER"),
  66.         ]);
  67.     }
  68.     /**
  69.      * @Route("/zadania-grafika", name="task_graphic_index")
  70.      */
  71.     public function indexGraphic(UserRepository $userRepository): Response
  72.     {
  73.         return $this->render('task/graphic.html.twig', [
  74.             'users' => $userRepository->getUserByRole("ROLE_EMPLOYER"),
  75.             'tasks' => $this->taskRepository->findBy(["isGraphic" => true], ["plannedOnDay" => "DESC"])
  76.         ]);
  77.     }
  78.     /**
  79.      * @Route("/migracja-zadan-graficznych", name="task_graphic_migrate")
  80.      */
  81.     public function taskGraphicMigrate(): JsonResponse
  82.     {
  83.         foreach ($this->taskRepository->findBy(["project" => $this->projectRepository->find(273)], ["plannedOnDay" => "DESC"]) as $task) {
  84.             $task->setIsGraphic(true);
  85.             $this->taskRepository->saveWithoutFlush($task);
  86.         }
  87.         $this->taskRepository->flush();
  88.         return $this->json([]);
  89.     }
  90.     /**
  91.      * @Route("/zadania-pracownika-dzisiaj/{user}", name="task_user_today")
  92.      */
  93.     public function taskUserToday(Request $requestUser $userUserRepository $userRepository): Response
  94.     {
  95.         $plannedTaskDay $request->get("plannedTaskDay") ? new \DateTimeImmutable($request->get("plannedTaskDay")) : new \DateTimeImmutable();
  96.         $nextDayPlannedTaskDay $plannedTaskDay->add(new \DateInterval("P1D"));
  97.         $previousDayPlannedTaskDay $plannedTaskDay->sub(new \DateInterval("P1D"));
  98.         return $this->render('task/task-today.html.twig', [
  99.             'users' => $userRepository->getUserByRole("ROLE_EMPLOYER"),
  100.             'tasks' => $this->taskRepository->findTasks([
  101.                 "plannedOnDayStart" => $plannedTaskDay->modify("00:00"),
  102.                 "plannedOnDayEnd" => $plannedTaskDay->modify("23:59"),
  103.                 'user' => $user
  104.             ]),
  105.             'user' => $user,
  106.             "currentDate" => $plannedTaskDay,
  107.             "previousDate" => $previousDayPlannedTaskDay->format("Y-m-d"),
  108.             "nextDate" => $nextDayPlannedTaskDay->format("Y-m-d"),
  109.         ]);
  110.     }
  111.     /**
  112.      * @Route("/kalendarz", name="task_calendar")
  113.      * @IsGranted("ROLE_EMPLOYER")
  114.      */
  115.     public function calendar(Request $request): Response
  116.     {
  117.         return $this->render('task/calendar.html.twig', [
  118.         ]);
  119.     }
  120.     /**
  121.      * @Route("/nowy", name="task_new")
  122.      * @IsGranted("ROLE_EMPLOYER")
  123.      */
  124.     public function new(Request $requestOfferPackageTaskRepository $offerPackageTaskRepository): Response
  125.     {
  126.         $task = new Task($this->getUser());
  127.         $offerTask false;
  128.         $countAssignedOfferTask 0;
  129.         if ($request->get('project')) {
  130.             $task->setProject($this->projectRepository->findOneBy(["id" => $request->get('project')]));
  131.         }
  132.         if ($request->get('offerTask')) {
  133.             $offerTask true;
  134.             $offerPackageTask $offerPackageTaskRepository->findOneBy(["id" => $request->get('offerTask')]);
  135.             $task->setOfferPackageTask($offerPackageTaskRepository->findOneBy(["id" => $request->get('offerTask')]));
  136.             $countAssignedOfferTask $this->taskRepository->countAssignedTaskFromOfferPackageTask($task->getProject(), $task->getOfferPackageTask(), new \DateTime())['count'];
  137.         }
  138.         $repeat $offerTask && $offerPackageTask->getRepeatInMonth() - $countAssignedOfferTask $offerPackageTask->getRepeatInMonth() - $countAssignedOfferTask 0;
  139.         $form $this->createForm(TaskType::class, $task, [
  140.             'user' => $this->getUser(),
  141.             "project" => $task->getProject(),
  142.             "repeat" => $repeat,
  143.         ]);
  144.         $form->handleRequest($request);
  145.         if ($form->isSubmitted() && $form->isValid()) {
  146.             $now = new \DateTime();
  147.             $i 0;
  148.             while ($form->has('plannedOnDayTasks-'.$i)) {
  149.                 if (!$form->get('plannedOnDayTasks-'.$i)->isEmpty()) {
  150.                     //if($form->get('plannedOnDayTasks-'.$i)->getData() >= $now->modify("-1 day")) {
  151.                     $tempTask = clone $task;
  152.                     $tempTask->setPlannedOnDay($form->get('plannedOnDayTasks-'.$i)->getData());
  153.                     $this->taskRepository->save($tempTask);
  154.                     //}
  155.                 }
  156.                 $i++;
  157.             }
  158.             $this->taskRepository->save($task);
  159.             return $this->redirectToRoute('project_show', ['project' => $task->getProject()->getId()]);
  160.         }
  161.         return $this->render("task/new.html.twig", [
  162.             "form" => $form->createView(),
  163.             'offerTask' => $offerTask,
  164.             'repeat' => $repeat,
  165.         ]);
  166.     }
  167.     /**
  168.      * @Route("/nowy-js", name="task_new_js")
  169.      * @IsGranted("ROLE_EMPLOYER")
  170.      */
  171.     public function newJs(Request $requestOfferPackageTaskRepository $offerPackageTaskRepositoryUserRepository  $userRepository): Response
  172.     {
  173.         $users = [];
  174.         if($request->get('user')) {
  175.             foreach ($request->get('user') as $queryUser) {
  176.                 $users[] = $userRepository->find($queryUser);
  177.             }
  178.         } else {
  179.             $users[] = $this->getUser();
  180.         }
  181.         $task = new Task($this->getUser());
  182.         $task->setProject($this->projectRepository->findOneBy(["id" => $request->get('project')]));
  183.         $task->setOfferPackageTask($offerPackageTaskRepository->findOneBy(["id" => $request->get('offerTask')]));
  184.         foreach ($users as $user) {
  185.             $task->addUser($user);
  186.         }
  187.         $dates explode(','$request->get('dates'));
  188.         foreach ($dates as $date) {
  189.             $tempTask = clone $task;
  190.             $tempTask->setPlannedOnDay(new \DateTimeImmutable($date));
  191.             $this->taskRepository->save($tempTask);
  192.         }
  193.         return $this->json([]);
  194.     }
  195.     /**
  196.      * @param Request $request
  197.      * @param Project $project
  198.      * @return JsonResponse
  199.      * @Route("/get-list-task/{project}", name="task_offer_list")
  200.      * @IsGranted("ROLE_EMPLOYER")
  201.      */
  202.     public function apiGetListTask(Request $requestProject $project): JsonResponse
  203.     {
  204.         $tasks $project->getOfferPackage()->getOfferPackageTasks();
  205.         $taskJson = [];
  206.         foreach ($tasks as $task) {
  207.             $taskJson[$task->getId()] = $task->getName();
  208.         }
  209.         return $this->json($taskJson);
  210.     }
  211.     /**
  212.      * @Route("/edytuj/{task}", name="task_edit")
  213.      * @IsGranted("ROLE_EMPLOYER")
  214.      */
  215.     public function edit(Request $requestTask $task): Response
  216.     {
  217.         $form $this->createForm(TaskType::class, $task, [
  218.             'user' => $this->getUser(),
  219.             "project" => $task->getProject(),
  220.         ]);
  221.         $form->handleRequest($request);
  222.         if ($form->isSubmitted() && $form->isValid()) {
  223.             $this->taskRepository->save($task);
  224.             return $this->redirectToRoute('task_index');
  225.         }
  226.         return $this->render("task/edit.html.twig", [
  227.             "form" => $form->createView(),
  228.             'offerTask' => null,
  229.             'repeat' => null,
  230.         ]);
  231.     }
  232.     /**
  233.      * @Route("/status/{task}/{status}", name="task_edit_status")
  234.      * @IsGranted("ROLE_EMPLOYER")
  235.      */
  236.     public function status(Request $requestTask $taskint $status): Response
  237.     {
  238.         if ($status == || $status == || $status == 3) {
  239.             $task->setStatus($status);
  240.             if ($task->isCompleted()) {
  241.                 $task->setCompletedAt(new \DateTimeImmutable());
  242.             }
  243.             $this->taskRepository->save($task);
  244.         }
  245.         return $this->redirectToRoute('task_index');
  246.     }
  247.     /**
  248.      * @Route("/usun/{task}", name="task_remove")
  249.      * @IsGranted("ROLE_EMPLOYER")
  250.      */
  251.     public function remove(Task $task): Response
  252.     {
  253.         $this->taskRepository->remove($task);
  254.         return $this->redirectToRoute('task_index');
  255.     }
  256.     /**
  257.      * @Route("/update-event/{task}", name="update_calendar_task")
  258.      * @IsGranted("ROLE_EMPLOYER")
  259.      * @param Request $request
  260.      * @param Task $task
  261.      * @return Response
  262.      * @throws \Exception
  263.      */
  264.     public function updateCalendarEvent(Request $requestTask $task): Response
  265.     {
  266.         $task->setPlannedOnDay((new \DateTime($request->get('date')))->modify("+5 hour"));
  267.         $this->taskRepository->save($task);
  268.         return $this->json(["success"]);
  269.     }
  270.     /**
  271.      * @Route("/feed", name="task_feed_dep")
  272.      */
  273.     public function feed(Request $requestUserRepository $userRepository): Response
  274.     {
  275.         if (!$request->get("project")) {
  276.             $defaultProject null;
  277.         } else {
  278.             $defaultProject $request->get("project")
  279.                 ? $this->projectRepository->findOneBy(["id" => $request->get("project"), 'type' => Project::TYPE_ACTIVE])
  280.                 : null;
  281.         }
  282.         $defaultUser $this->getUser();
  283.         $start = (new \DateTime('@'.strtotime($request->get('start'))))->setTimezone(new \DateTimeZone("Europe/Warsaw"));
  284.         $end = (new \DateTime('@'.strtotime($request->get('end'))))->setTimezone(new \DateTimeZone("Europe/Warsaw"));
  285.         $tasks $this->taskRepository->findTasks([
  286.             "project" => $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_EMPLOYER") ? $this->projectRepository->findOneByClient($this->getUser()) : $defaultProject,
  287.             "plannedOnDayStart" => $start,
  288.             "plannedOnDayEnd" => $end,
  289.             "user" => $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_EMPLOYER") ? null $defaultUser,
  290.             "list" => $request->get("user") == "list",
  291.         ]);
  292.         $t = [];
  293.         /** @var Task $task */
  294.         foreach ($tasks as $task) {
  295.             if ($task->getProject()->isDeactivated()) {
  296.                 continue;
  297.             }
  298.             if ($task->getOfferPackageTask()) {
  299.                 $t[] = $this->generateEventFromOfferTaskCalendar($task$this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
  300.             } elseif ($task->isRepeatable()) {
  301.                 if ($task->repeatDays()) {
  302.                     $startTask $task->getPlannedOnDay();
  303.                     $diff $startTask->diff($end);
  304.                     for ($i 0$i <= $diff->days$i++) {
  305.                         if ($task->getPlannedOnDay()->format("w") == 6) {
  306.                             $task->getPlannedOnDay()->modify("+2 days");
  307.                         }
  308.                         if ($task->getPlannedOnDay()->format("w") == 0) {
  309.                             $task->getPlannedOnDay()->modify("+1 days");
  310.                         }
  311.                         $t[] = $this->generateEventsCalendar($task$this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
  312.                         $task->getPlannedOnDay()->modify("+1 days");
  313.                         $task->getPlannedOnDay()->modify("-1 hour");
  314.                     }
  315.                 } elseif ($task->repeatTwoDays()) {
  316.                     $startTask $task->getPlannedOnDay();
  317.                     $diff $startTask->diff($end);
  318.                     for ($i 0$i <= ceil($diff->days 2); $i++) {
  319.                         if ($task->getPlannedOnDay()->format("w") == 6) {
  320.                             $task->getPlannedOnDay()->modify("+2 days");
  321.                         }
  322.                         if ($task->getPlannedOnDay()->format("w") == 0) {
  323.                             $task->getPlannedOnDay()->modify("+1 days");
  324.                         }
  325.                         $t[] = $this->generateEventsCalendar($task$this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
  326.                         $task->getPlannedOnDay()->modify("+2 days");
  327.                         $task->getPlannedOnDay()->modify("-1 hour");
  328.                     }
  329.                 } elseif ($task->repeatThreeDays()) {
  330.                     $startTask $task->getPlannedOnDay();
  331.                     $diff $startTask->diff($end);
  332.                     for ($i 0$i <= ceil($diff->days 3); $i++) {
  333.                         if ($task->getPlannedOnDay()->format("w") == 6) {
  334.                             $task->getPlannedOnDay()->modify("+2 days");
  335.                         }
  336.                         if ($task->getPlannedOnDay()->format("w") == 0) {
  337.                             $task->getPlannedOnDay()->modify("+1 days");
  338.                         }
  339.                         $t[] = $this->generateEventsCalendar($task$this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
  340.                         $task->getPlannedOnDay()->modify("+3 days");
  341.                         $task->getPlannedOnDay()->modify("-1 hour");
  342.                     }
  343.                 } elseif ($task->repeatFourDays()) {
  344.                     $startTask $task->getPlannedOnDay();
  345.                     $diff $startTask->diff($end);
  346.                     for ($i 0$i <= ceil($diff->days 4); $i++) {
  347.                         if ($task->getPlannedOnDay()->format("w") == 6) {
  348.                             $task->getPlannedOnDay()->modify("+2 days");
  349.                         }
  350.                         if ($task->getPlannedOnDay()->format("w") == 0) {
  351.                             $task->getPlannedOnDay()->modify("+1 days");
  352.                         }
  353.                         $t[] = $this->generateEventsCalendar($task$this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
  354.                         $task->getPlannedOnDay()->modify("+4 days");
  355.                         $task->getPlannedOnDay()->modify("-1 hour");
  356.                     }
  357.                 } elseif ($task->repeatWeekly()) {
  358.                     $startTask $task->getPlannedOnDay();
  359.                     $diff $startTask->diff($end);
  360.                     for ($i 0$i <= ceil($diff->days 5); $i++) {
  361.                         if ($task->getPlannedOnDay()->format("w") == 6) {
  362.                             $task->getPlannedOnDay()->modify("+2 days");
  363.                         }
  364.                         if ($task->getPlannedOnDay()->format("w") == 0) {
  365.                             $task->getPlannedOnDay()->modify("+1 days");
  366.                         }
  367.                         $t[] = $this->generateEventsCalendar($task$this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
  368.                         $task->getPlannedOnDay()->modify("+7 days");
  369.                         $task->getPlannedOnDay()->modify("-1 hour");
  370.                     }
  371.                 } elseif ($task->repeatMonthly()) {
  372.                     $startTask $task->getPlannedOnDay();
  373.                     $diff $startTask->diff($end);
  374.                     for ($i 0$i <= $diff->m$i++) {
  375.                         if ($task->getPlannedOnDay()->format("w") == 6) {
  376.                             $task->getPlannedOnDay()->modify("+2 days");
  377.                         }
  378.                         if ($task->getPlannedOnDay()->format("w") == 0) {
  379.                             $task->getPlannedOnDay()->modify("+1 days");
  380.                         }
  381.                         $t[] = $this->generateEventsCalendar($task$this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
  382.                         $task->getPlannedOnDay()->modify("+1 month");
  383.                         $task->getPlannedOnDay()->modify("-1 hour");
  384.                     }
  385.                 }
  386.             } else {
  387.                 $t[] = $this->generateEventsCalendar($task$this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
  388.             }
  389.         }
  390.         return $this->json($t);
  391.     }
  392.     /**
  393.      * @Route("/my-task", name="task_feed")
  394.      */
  395.     public function myTask(Request $requestUserRepository $userRepository): Response
  396.     {
  397.         if (!$request->get("project")) {
  398.             $defaultProject null;
  399.         } else {
  400.             $defaultProject $request->get("project")
  401.                 ? $this->projectRepository->findOneBy(["id" => $request->get("project"), 'type' => Project::TYPE_ACTIVE])
  402.                 : null;
  403.         }
  404.         if (!$request->get("user") && !$request->get("createdBy")) {
  405.             $defaultUser $this->getUser();
  406.         } else {
  407.             if ($request->get("user") == "list") {
  408.                 $defaultUser null;
  409. //                $this->isList = true;
  410.             } else {
  411.                 $defaultUser $request->get("user")
  412.                     ? $userRepository->findOneBy(["id" => $request->get("user")])
  413.                     : null;
  414.             }
  415.         }
  416.         $createdBy null;
  417.         if ($request->get("createdBy")) {
  418.             $createdBy $this->getUser();
  419.         }
  420. //
  421. //        $start = (new \DateTime('@'.strtotime($request->get('start'))))->setTimezone(new \DateTimeZone("Europe/Warsaw"));
  422.         $end = (new \DateTime())->setTimezone(new \DateTimeZone("Europe/Warsaw"));
  423.         if ($this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_EMPLOYER")) {
  424.             $tasks $this->taskRepository->findTasks([
  425.                 "project" => $this->projectRepository->findOneByClient($this->getUser()),
  426.                 "plannedOnDayStart" => new \DateTimeImmutable("midnight first day of this month"),
  427.                 "plannedOnDayEnd" => new \DateTimeImmutable("last day of this month 23:59"),
  428.                 "user" => $defaultUser,
  429.                 "createdBy" => $createdBy,
  430.                 "list" => $request->get("user") == "list",
  431.             ]);
  432.         } else {
  433.             $tasks $this->taskRepository->findTasks([
  434.                 "project" => $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_EMPLOYER") ? $this->projectRepository->findOneByClient($this->getUser()) : $defaultProject,
  435. //            "plannedOnDayStart" => $start,
  436. //            "plannedOnDayEnd" => $end,
  437.                 "status" => $request->get("status") ? null Task::STATUS_PLANNED,
  438.                 "user" => $defaultUser,
  439.                 "createdBy" => $createdBy,
  440.                 "list" => $request->get("user") == "list",
  441.             ]);
  442.         }
  443.         $t = [];
  444.         /** @var Task $task */
  445.         foreach ($tasks as $task) {
  446.             if ($task->getProject()->isDeactivated()) {
  447.                 continue;
  448.             }
  449.             if ($task->getOfferPackageTask()) {
  450.                 $t[] = $this->generateEventFromOfferTask($task$this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
  451.             } elseif ($task->isRepeatable()) {
  452.                 if ($task->repeatDays()) {
  453.                     $startTask $task->getPlannedOnDay();
  454.                     $diff $startTask->diff($end);
  455.                     for ($i 0$i <= $diff->days$i++) {
  456.                         if ($task->getPlannedOnDay()->format("w") == 6) {
  457.                             $task->getPlannedOnDay()->modify("+2 days");
  458.                         }
  459.                         if ($task->getPlannedOnDay()->format("w") == 0) {
  460.                             $task->getPlannedOnDay()->modify("+1 days");
  461.                         }
  462.                         $t[] = $this->generateEvents($task$this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
  463.                         $task->getPlannedOnDay()->modify("+1 days");
  464.                         $task->getPlannedOnDay()->modify("-1 hour");
  465.                     }
  466.                 } elseif ($task->repeatTwoDays()) {
  467.                     $startTask $task->getPlannedOnDay();
  468.                     $diff $startTask->diff($end);
  469.                     for ($i 0$i <= ceil($diff->days 2); $i++) {
  470.                         if ($task->getPlannedOnDay()->format("w") == 6) {
  471.                             $task->getPlannedOnDay()->modify("+2 days");
  472.                         }
  473.                         if ($task->getPlannedOnDay()->format("w") == 0) {
  474.                             $task->getPlannedOnDay()->modify("+1 days");
  475.                         }
  476.                         $t[] = $this->generateEvents($task$this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
  477.                         $task->getPlannedOnDay()->modify("+2 days");
  478.                         $task->getPlannedOnDay()->modify("-1 hour");
  479.                     }
  480.                 } elseif ($task->repeatThreeDays()) {
  481.                     $startTask $task->getPlannedOnDay();
  482.                     $diff $startTask->diff($end);
  483.                     for ($i 0$i <= ceil($diff->days 3); $i++) {
  484.                         if ($task->getPlannedOnDay()->format("w") == 6) {
  485.                             $task->getPlannedOnDay()->modify("+2 days");
  486.                         }
  487.                         if ($task->getPlannedOnDay()->format("w") == 0) {
  488.                             $task->getPlannedOnDay()->modify("+1 days");
  489.                         }
  490.                         $t[] = $this->generateEvents($task$this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
  491.                         $task->getPlannedOnDay()->modify("+3 days");
  492.                         $task->getPlannedOnDay()->modify("-1 hour");
  493.                     }
  494.                 } elseif ($task->repeatFourDays()) {
  495.                     $startTask $task->getPlannedOnDay();
  496.                     $diff $startTask->diff($end);
  497.                     for ($i 0$i <= ceil($diff->days 4); $i++) {
  498.                         if ($task->getPlannedOnDay()->format("w") == 6) {
  499.                             $task->getPlannedOnDay()->modify("+2 days");
  500.                         }
  501.                         if ($task->getPlannedOnDay()->format("w") == 0) {
  502.                             $task->getPlannedOnDay()->modify("+1 days");
  503.                         }
  504.                         $t[] = $this->generateEvents($task$this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
  505.                         $task->getPlannedOnDay()->modify("+4 days");
  506.                         $task->getPlannedOnDay()->modify("-1 hour");
  507.                     }
  508.                 } elseif ($task->repeatWeekly()) {
  509.                     $startTask $task->getPlannedOnDay();
  510.                     $diff $startTask->diff($end);
  511.                     for ($i 0$i <= ceil($diff->days 5); $i++) {
  512.                         if ($task->getPlannedOnDay()->format("w") == 6) {
  513.                             $task->getPlannedOnDay()->modify("+2 days");
  514.                         }
  515.                         if ($task->getPlannedOnDay()->format("w") == 0) {
  516.                             $task->getPlannedOnDay()->modify("+1 days");
  517.                         }
  518.                         $t[] = $this->generateEvents($task$this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
  519.                         $task->getPlannedOnDay()->modify("+7 days");
  520.                         $task->getPlannedOnDay()->modify("-1 hour");
  521.                     }
  522.                 } elseif ($task->repeatMonthly()) {
  523.                     $startTask $task->getPlannedOnDay();
  524.                     $diff $startTask->diff($end);
  525.                     for ($i 0$i <= $diff->m$i++) {
  526.                         if ($task->getPlannedOnDay()->format("w") == 6) {
  527.                             $task->getPlannedOnDay()->modify("+2 days");
  528.                         }
  529.                         if ($task->getPlannedOnDay()->format("w") == 0) {
  530.                             $task->getPlannedOnDay()->modify("+1 days");
  531.                         }
  532.                         $t[] = $this->generateEvents($task$this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
  533.                         $task->getPlannedOnDay()->modify("+1 month");
  534.                         $task->getPlannedOnDay()->modify("-1 hour");
  535.                     }
  536.                 }
  537.             } else {
  538.                 $t[] = $this->generateEvents($task$this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
  539.             }
  540.         }
  541.         return $this->json($t);
  542.     }
  543.     /**
  544.      * @param Task $task
  545.      * @param bool $client
  546.      * @return array
  547.      */
  548.     private function generateEventFromOfferTask(Task $taskbool $client): array
  549.     {
  550.         $usersName = [];
  551.         foreach ($task->getUsers() as $user) {
  552.             $usersName[] = $user->getName();
  553.         }
  554.         $userName implode(', '$usersName);
  555.         if ($this->isList) {
  556.             return [
  557.                 "title" => $task->getOfferPackageTask()->getName()." - ".$userName." - ".$task->getProject()->getName(),
  558.                 "task_id" => $task->getOfferPackageTask()->getId(),
  559.                 "normal_task_id" => $task->getId(),
  560.                 "project_id" => $task->getProject()->getId(),
  561.                 "url_update" => $this->generateUrl('update_calendar_task', ["task" => $task->getId()]),
  562.                 "start" => $task->getPlannedOnDay()->format("Y-m-d H:i"),
  563.                 "end" => $task->getPlannedOnDay()->modify("+30 minute")->format("Y-m-d H:i"),
  564.                 "editable" => $this->canEditEvent($task->getUsers()->first()),
  565.                 "backgroundColor" => $task->getUsers()->first()->getColor() ?? "blue",
  566.                 "urlEndTask" => $this->generateUrl('task_edit_status', ["status" => Task::STATUS_COMPLETED"task" => $task->getId()]),
  567.                 "urlToTask" => $client "" $this->generateUrl('project_show', ["project" => $task->getProject()->getId()]),
  568.                 "task_completed" => $task->isCompleted(),
  569.                 "task_basic" => (int)$task->getOfferPackageTask()->isBasic(),
  570.                 "client" => $client,
  571.                 "task_status" => $task->getStatus(),
  572.             ];
  573.         }
  574.         $taskContent "<a href='".$this->generateUrl('show_task', ["task" => $task->getId()])."'>".$task->getOfferPackageTask()->getName()." - ".$userName." - ".$task->getProject()->getName()."</a>";
  575.         if ($this->isGranted("ROLE_CLIENT")) {
  576.             $taskContent $task->getOfferPackageTask()->getName()." - ".$userName." - ".$task->getProject()->getName();
  577.         }
  578.         return [
  579.             "task_content" => $taskContent,
  580.             "task_project" => $task->getProject()->getName(),
  581.             "task_deadline" => $task->getPlannedOnDay()->format("Y-m-d H:i"),
  582.             "task_status" => $this->translator->trans("task.status_".$task->getStatus()),
  583.             "task_who_assing" => $task->getCreatedBy()->getName(),
  584.             "task_comment" => $task->getContent(),
  585.         ];
  586.     }
  587.     /**
  588.      * @param Task $task
  589.      * @return array
  590.      */
  591.     private function generateEvents(Task $taskbool $client): array
  592.     {
  593.         $usersName = [];
  594.         foreach ($task->getUsers() as $user) {
  595.             $usersName[] = $user->getName();
  596.         }
  597.         $userName implode(', '$usersName);
  598.         if ($this->isList) {
  599.             return [
  600.                 "title" => $task->getName()." - ".$userName." - ".$task->getProject()->getName(),
  601.                 "url_update" => $this->generateUrl('update_calendar_task', ["task" => $task->getId()]),
  602.                 "start" => $task->getPlannedOnDay()->format("Y-m-d H:i"),
  603.                 "end" => $task->getPlannedOnDay()->modify("+30 minute")->format("Y-m-d H:i"),
  604.                 "editable" => $this->canEditEvent($task->getUsers()->first()),
  605.                 "backgroundColor" => $task->getUsers()->first()->getColor() ?? "blue",
  606.                 "urlEndTask" => $this->generateUrl('task_edit_status', ["status" => Task::STATUS_COMPLETED"task" => $task->getId()]),
  607.                 "urlToTask" => $client "" $this->generateUrl('show_task', ["task" => $task->getId()]),
  608.                 "task_completed" => $task->isCompleted(),
  609.                 "project_id" => $task->getProject()->getId(),
  610.                 "task_id" => $task->getId(),
  611.                 "client" => $client,
  612.                 "task_status" => $task->getStatus(),
  613.             ];
  614.         }
  615.         $taskContent "<a href='".$this->generateUrl('show_task', ["task" => $task->getId()])."'>".$task->getName()." - ".$userName." - ".$task->getProject()->getName()."</a>";
  616.         if ($this->isGranted("ROLE_CLIENT")) {
  617.             $taskContent $task->getName()." - ".$userName." - ".$task->getProject()->getName();
  618.         }
  619.         return [
  620.             "task_content" => $taskContent,
  621.             "task_project" => $task->getProject()->getName(),
  622.             "task_deadline" => $task->getPlannedOnDay()->format("Y-m-d H:i"),
  623.             "task_status" => $this->translator->trans("task.status_".$task->getStatus()),
  624.             "task_who_assing" => $task->getCreatedBy()->getName(),
  625.             "task_comment" => $task->getContent(),
  626. //
  627. //
  628. //            "url_update" => $this->generateUrl('update_calendar_task', ["task" => $task->getId()]),
  629. //            "url" => $client ? "" : $this->generateUrl('show_task', ["task" => $task->getId()]),
  630. //            "start" => $task->getPlannedOnDay()->format("Y-m-d H:i"),
  631. //            "end" => $task->getPlannedOnDay()->modify("+30 minute")->format("Y-m-d H:i"),
  632. //            "editable" => $this->canEditEvent($task->getUsers()->first()),
  633. //            "backgroundColor" => $task->getUsers()->first()->getColor() ?? "blue",
  634. //            "task_completed" => $task->isCompleted(),
  635. //            "client" => $client,
  636. //            "task_status" => $task->getStatus(),
  637.         ];
  638.     }
  639.     /**
  640.      * @Route("/can-task-time", name="task_can_task_time")
  641.      */
  642.     public function canTaskTime(Request $requestUserRepository $userRepository): JsonResponse
  643.     {
  644.         $users json_decode($request->get('users'));
  645.         $return = [];
  646.         $time = new \DateTime($request->get("time"));
  647.         foreach ($users as $userId) {
  648.             $user $userRepository->findOneBy(["id" => $userId]);
  649.             $tasks $this->taskRepository->findTasks([
  650.                 "user" => $user,
  651.                 "plannedOnDayStart" => $time,
  652.                 "plannedOnDayEnd" => $time,
  653.             ]);
  654.             $bothEmptyTime null;
  655.             if ($tasks) {
  656.                 foreach ($tasks as $task) {
  657.                     if ($task->getPlannedOnDay() == $time) {
  658.                         $t = clone $time;
  659.                         $emptyTime null;
  660.                         for ($i 0$i 50$i++) {
  661.                             $t $t->modify("+30 min");
  662.                             $tempTasks $this->taskRepository->findTasks([
  663.                                 "user" => $user,
  664.                                 "plannedOnDayStart" => $t,
  665.                                 "plannedOnDayEnd" => $t,
  666.                                 "hard" => true,
  667.                             ]);
  668.                             $tempTasksCurrentUser $this->taskRepository->findTasks([
  669.                                 "user" => $this->getUser(),
  670.                                 "plannedOnDayStart" => $t,
  671.                                 "plannedOnDayEnd" => $t,
  672.                                 "hard" => true,
  673.                             ]);
  674.                             if (!$tempTasks && !$emptyTime) {
  675.                                 $emptyTime $t;
  676.                             }
  677.                             if (!$tempTasksCurrentUser && !$tempTasks) {
  678.                                 $bothEmptyTime $t;
  679.                                 break;
  680.                             }
  681.                         }
  682.                         $return[] = [
  683.                             "Użytkownik ".$user->getName()." posiada zadanie w tym czasie. Kolejny wolny termin to: ".$emptyTime->format("Y-m-d H:i:s")."<br>".
  684.                             "Wasz najbliższy wolny wspólny termin: ".$bothEmptyTime->format("Y-m-d H:i:s")."<br>"."<br>",
  685.                         ];
  686.                     }
  687.                 }
  688.             }
  689.         }
  690.         if (count($return) == 0) {
  691.             return $this->json(["can" => true]);
  692.         }
  693.         return $this->json(["can" => false"error" => $return]);
  694.     }
  695.     /**
  696.      * @IsGranted("ROLE_ADMIN")
  697.      * @Route("/opoznienia", name="delayed_tasks")
  698.      * @return Response
  699.      */
  700.     public function taskDelayed(Request $requestUserRepository $userRepository): Response
  701.     {
  702.         $defaultDateFrom = new \DateTime("midnight first day of this month");
  703.         $defaultDateTo = new \DateTime("-1 day 23:59");
  704.         $form $this->createForm(TaskDelayedRangeType::class, null, [
  705.             "dateFrom" => $defaultDateFrom,
  706.             "dateTo" => $defaultDateTo,
  707.         ]);
  708.         $form->handleRequest($request);
  709.         if ($form->isSubmitted() && $form->isValid()) {
  710.             $defaultDateFrom $form->get('dateFrom')->getData();
  711.             $defaultDateTo $form->get('dateTo')->getData();
  712.             if ($defaultDateTo > new \DateTime("-1 day")) {
  713.                 $defaultDateTo = new \DateTime("-1 day");
  714.             }
  715.         }
  716.         $data = [];
  717.         foreach ($userRepository->getUserByRole("ROLE_EMPLOYER") as $user) {
  718.             if (!isset($data[$user->getId()])) {
  719.                 $data[$user->getName()] = [];
  720.             }
  721.             foreach ($this->taskRepository->findDelayedTask($user, [
  722.                 "plannedOnDayStart" => $defaultDateFrom,
  723.                 "plannedOnDayEnd" => $defaultDateTo,
  724.             ]) as $task) {
  725.                 $data[$user->getName()][] = $task;
  726.             }
  727.             if (empty($data[$user->getName()])) {
  728.                 unset($data[$user->getName()]);
  729.             }
  730.         }
  731.         return $this->render("task/delayed.html.twig", [
  732.             "data" => $data,
  733.             "form" => $form->createView(),
  734.         ]);
  735.     }
  736.     /**
  737.      * @Route("/{task}", name="show_task")
  738.      * @IsGranted("ROLE_EMPLOYER")
  739.      */
  740.     public function show(Task $task): Response
  741.     {
  742.         return $this->render("task/show.html.twig", [
  743.             "task" => $task,
  744.         ]);
  745.     }
  746.     /**
  747.      * @param User $user
  748.      * @return bool
  749.      */
  750.     private function canEditEvent(User $user): bool
  751.     {
  752.         if ($this->isGranted("ROLE_ADMIN")) {
  753.             return true;
  754.         }
  755.         if ($this->getUser() == $user) {
  756.             return true;
  757.         }
  758.         return false;
  759.     }
  760.     /**
  761.      * @param Task $task
  762.      * @return string
  763.      */
  764.     private function getChannelId(Task $task): string
  765.     {
  766.         return $task->getProject()->getSlackChannelId() ?? "C02B302PFQE";
  767.     }
  768.     private function generateEventFromOfferTaskCalendar(Task $taskbool $client): array
  769.     {
  770.         $usersName = [];
  771.         foreach ($task->getUsers() as $user) {
  772.             $usersName[] = $user->getName();
  773.         }
  774.         $userName implode(', '$usersName);
  775.         return [
  776.             "title" => $task->getOfferPackageTask()->getName()." - ".$userName." - ".$task->getProject()->getName(),
  777.             "start" => $task->getPlannedOnDay()->format("Y-m-d H:i"),
  778.             "end" => $task->getPlannedOnDay()->modify("+30 minute")->format("Y-m-d H:i"),
  779.             "editable" => false,
  780.             "backgroundColor" => $task->getUsers()->first() ? $task->getUsers()->first()->getColor() ?? "blue" "blue",
  781.             "task_completed" => $task->isCompleted(),
  782.             "task_status" => $task->getStatus(),
  783.         ];
  784.     }
  785.     /**
  786.      * @param Task $task
  787.      * @return array
  788.      */
  789.     private function generateEventsCalendar(Task $taskbool $client): array
  790.     {
  791.         $usersName = [];
  792.         foreach ($task->getUsers() as $user) {
  793.             $usersName[] = $user->getName();
  794.         }
  795.         $userName implode(', '$usersName);
  796.         return [
  797.             "title" => $task->getName()." - ".$userName." - ".$task->getProject()->getName(),
  798.             "start" => $task->getPlannedOnDay()->format("Y-m-d H:i"),
  799.             "end" => $task->getPlannedOnDay()->modify("+30 minute")->format("Y-m-d H:i"),
  800.             "editable" => false,
  801.             "backgroundColor" => $task->getUsers()->first()->getColor() ?? "blue",
  802.             "task_completed" => $task->isCompleted(),
  803.             "task_status" => $task->getStatus(),
  804.         ];
  805.     }
  806. }