<?php
namespace App\Controller;
use App\Entity\Project;
use App\Entity\Task;
use App\Entity\User;
use App\Form\CallPersonToAssignTaskType;
use App\Form\LoggedTimeTypeType;
use App\Form\TaskDelayedRangeType;
use App\Form\TaskFilterType;
use App\Form\TaskType;
use App\Module\Slack\SlackManager;
use App\Repository\OfferPackageTaskRepository;
use App\Repository\ProjectRepository;
use App\Repository\TaskRepository;
use App\Repository\UserRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Class TaskController
*
* @package App\Controller
* @author Lukasz Krakowiak <lukasz94krakowiak@gmail.com>
* @Route("/zadania")
*/
class TaskController extends AbstractController
{
/**
* @var ProjectRepository
*/
private ProjectRepository $projectRepository;
/**
* @var TaskRepository
*/
private TaskRepository $taskRepository;
/**
* @var TranslatorInterface
*/
private TranslatorInterface $translator;
/**
* @var bool
*/
private bool $isList = false;
/**
* @param ProjectRepository $projectRepository
* @param TaskRepository $taskRepository
* @param TranslatorInterface $translator
*/
public function __construct(ProjectRepository $projectRepository, TaskRepository $taskRepository, TranslatorInterface $translator)
{
$this->projectRepository = $projectRepository;
$this->taskRepository = $taskRepository;
$this->translator = $translator;
}
/**
* @Route("/", name="task_index")
*/
public function index(UserRepository $userRepository): Response
{
return $this->render('task/index.html.twig', [
'users' => $userRepository->getUserByRole("ROLE_EMPLOYER"),
]);
}
/**
* @Route("/zadania-grafika", name="task_graphic_index")
*/
public function indexGraphic(UserRepository $userRepository): Response
{
return $this->render('task/graphic.html.twig', [
'users' => $userRepository->getUserByRole("ROLE_EMPLOYER"),
'tasks' => $this->taskRepository->findBy(["isGraphic" => true], ["plannedOnDay" => "DESC"])
]);
}
/**
* @Route("/migracja-zadan-graficznych", name="task_graphic_migrate")
*/
public function taskGraphicMigrate(): JsonResponse
{
foreach ($this->taskRepository->findBy(["project" => $this->projectRepository->find(273)], ["plannedOnDay" => "DESC"]) as $task) {
$task->setIsGraphic(true);
$this->taskRepository->saveWithoutFlush($task);
}
$this->taskRepository->flush();
return $this->json([]);
}
/**
* @Route("/zadania-pracownika-dzisiaj/{user}", name="task_user_today")
*/
public function taskUserToday(Request $request, User $user, UserRepository $userRepository): Response
{
$plannedTaskDay = $request->get("plannedTaskDay") ? new \DateTimeImmutable($request->get("plannedTaskDay")) : new \DateTimeImmutable();
$nextDayPlannedTaskDay = $plannedTaskDay->add(new \DateInterval("P1D"));
$previousDayPlannedTaskDay = $plannedTaskDay->sub(new \DateInterval("P1D"));
return $this->render('task/task-today.html.twig', [
'users' => $userRepository->getUserByRole("ROLE_EMPLOYER"),
'tasks' => $this->taskRepository->findTasks([
"plannedOnDayStart" => $plannedTaskDay->modify("00:00"),
"plannedOnDayEnd" => $plannedTaskDay->modify("23:59"),
'user' => $user
]),
'user' => $user,
"currentDate" => $plannedTaskDay,
"previousDate" => $previousDayPlannedTaskDay->format("Y-m-d"),
"nextDate" => $nextDayPlannedTaskDay->format("Y-m-d"),
]);
}
/**
* @Route("/kalendarz", name="task_calendar")
* @IsGranted("ROLE_EMPLOYER")
*/
public function calendar(Request $request): Response
{
return $this->render('task/calendar.html.twig', [
]);
}
/**
* @Route("/nowy", name="task_new")
* @IsGranted("ROLE_EMPLOYER")
*/
public function new(Request $request, OfferPackageTaskRepository $offerPackageTaskRepository): Response
{
$task = new Task($this->getUser());
$offerTask = false;
$countAssignedOfferTask = 0;
if ($request->get('project')) {
$task->setProject($this->projectRepository->findOneBy(["id" => $request->get('project')]));
}
if ($request->get('offerTask')) {
$offerTask = true;
$offerPackageTask = $offerPackageTaskRepository->findOneBy(["id" => $request->get('offerTask')]);
$task->setOfferPackageTask($offerPackageTaskRepository->findOneBy(["id" => $request->get('offerTask')]));
$countAssignedOfferTask = $this->taskRepository->countAssignedTaskFromOfferPackageTask($task->getProject(), $task->getOfferPackageTask(), new \DateTime())['count'];
}
$repeat = $offerTask && $offerPackageTask->getRepeatInMonth() - $countAssignedOfferTask > 1 ? $offerPackageTask->getRepeatInMonth() - $countAssignedOfferTask - 1 : 0;
$form = $this->createForm(TaskType::class, $task, [
'user' => $this->getUser(),
"project" => $task->getProject(),
"repeat" => $repeat,
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$now = new \DateTime();
$i = 0;
while ($form->has('plannedOnDayTasks-'.$i)) {
if (!$form->get('plannedOnDayTasks-'.$i)->isEmpty()) {
//if($form->get('plannedOnDayTasks-'.$i)->getData() >= $now->modify("-1 day")) {
$tempTask = clone $task;
$tempTask->setPlannedOnDay($form->get('plannedOnDayTasks-'.$i)->getData());
$this->taskRepository->save($tempTask);
//}
}
$i++;
}
$this->taskRepository->save($task);
return $this->redirectToRoute('project_show', ['project' => $task->getProject()->getId()]);
}
return $this->render("task/new.html.twig", [
"form" => $form->createView(),
'offerTask' => $offerTask,
'repeat' => $repeat,
]);
}
/**
* @Route("/nowy-js", name="task_new_js")
* @IsGranted("ROLE_EMPLOYER")
*/
public function newJs(Request $request, OfferPackageTaskRepository $offerPackageTaskRepository, UserRepository $userRepository): Response
{
$users = [];
if($request->get('user')) {
foreach ($request->get('user') as $queryUser) {
$users[] = $userRepository->find($queryUser);
}
} else {
$users[] = $this->getUser();
}
$task = new Task($this->getUser());
$task->setProject($this->projectRepository->findOneBy(["id" => $request->get('project')]));
$task->setOfferPackageTask($offerPackageTaskRepository->findOneBy(["id" => $request->get('offerTask')]));
foreach ($users as $user) {
$task->addUser($user);
}
$dates = explode(',', $request->get('dates'));
foreach ($dates as $date) {
$tempTask = clone $task;
$tempTask->setPlannedOnDay(new \DateTimeImmutable($date));
$this->taskRepository->save($tempTask);
}
return $this->json([]);
}
/**
* @param Request $request
* @param Project $project
* @return JsonResponse
* @Route("/get-list-task/{project}", name="task_offer_list")
* @IsGranted("ROLE_EMPLOYER")
*/
public function apiGetListTask(Request $request, Project $project): JsonResponse
{
$tasks = $project->getOfferPackage()->getOfferPackageTasks();
$taskJson = [];
foreach ($tasks as $task) {
$taskJson[$task->getId()] = $task->getName();
}
return $this->json($taskJson);
}
/**
* @Route("/edytuj/{task}", name="task_edit")
* @IsGranted("ROLE_EMPLOYER")
*/
public function edit(Request $request, Task $task): Response
{
$form = $this->createForm(TaskType::class, $task, [
'user' => $this->getUser(),
"project" => $task->getProject(),
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->taskRepository->save($task);
return $this->redirectToRoute('task_index');
}
return $this->render("task/edit.html.twig", [
"form" => $form->createView(),
'offerTask' => null,
'repeat' => null,
]);
}
/**
* @Route("/status/{task}/{status}", name="task_edit_status")
* @IsGranted("ROLE_EMPLOYER")
*/
public function status(Request $request, Task $task, int $status): Response
{
if ($status == 1 || $status == 2 || $status == 3) {
$task->setStatus($status);
if ($task->isCompleted()) {
$task->setCompletedAt(new \DateTimeImmutable());
}
$this->taskRepository->save($task);
}
return $this->redirectToRoute('task_index');
}
/**
* @Route("/usun/{task}", name="task_remove")
* @IsGranted("ROLE_EMPLOYER")
*/
public function remove(Task $task): Response
{
$this->taskRepository->remove($task);
return $this->redirectToRoute('task_index');
}
/**
* @Route("/update-event/{task}", name="update_calendar_task")
* @IsGranted("ROLE_EMPLOYER")
* @param Request $request
* @param Task $task
* @return Response
* @throws \Exception
*/
public function updateCalendarEvent(Request $request, Task $task): Response
{
$task->setPlannedOnDay((new \DateTime($request->get('date')))->modify("+5 hour"));
$this->taskRepository->save($task);
return $this->json(["success"]);
}
/**
* @Route("/feed", name="task_feed_dep")
*/
public function feed(Request $request, UserRepository $userRepository): Response
{
if (!$request->get("project")) {
$defaultProject = null;
} else {
$defaultProject = $request->get("project")
? $this->projectRepository->findOneBy(["id" => $request->get("project"), 'type' => Project::TYPE_ACTIVE])
: null;
}
$defaultUser = $this->getUser();
$start = (new \DateTime('@'.strtotime($request->get('start'))))->setTimezone(new \DateTimeZone("Europe/Warsaw"));
$end = (new \DateTime('@'.strtotime($request->get('end'))))->setTimezone(new \DateTimeZone("Europe/Warsaw"));
$tasks = $this->taskRepository->findTasks([
"project" => $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_EMPLOYER") ? $this->projectRepository->findOneByClient($this->getUser()) : $defaultProject,
"plannedOnDayStart" => $start,
"plannedOnDayEnd" => $end,
"user" => $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_EMPLOYER") ? null : $defaultUser,
"list" => $request->get("user") == "list",
]);
$t = [];
/** @var Task $task */
foreach ($tasks as $task) {
if ($task->getProject()->isDeactivated()) {
continue;
}
if ($task->getOfferPackageTask()) {
$t[] = $this->generateEventFromOfferTaskCalendar($task, $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
} elseif ($task->isRepeatable()) {
if ($task->repeatDays()) {
$startTask = $task->getPlannedOnDay();
$diff = $startTask->diff($end);
for ($i = 0; $i <= $diff->days; $i++) {
if ($task->getPlannedOnDay()->format("w") == 6) {
$task->getPlannedOnDay()->modify("+2 days");
}
if ($task->getPlannedOnDay()->format("w") == 0) {
$task->getPlannedOnDay()->modify("+1 days");
}
$t[] = $this->generateEventsCalendar($task, $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
$task->getPlannedOnDay()->modify("+1 days");
$task->getPlannedOnDay()->modify("-1 hour");
}
} elseif ($task->repeatTwoDays()) {
$startTask = $task->getPlannedOnDay();
$diff = $startTask->diff($end);
for ($i = 0; $i <= ceil($diff->days / 2); $i++) {
if ($task->getPlannedOnDay()->format("w") == 6) {
$task->getPlannedOnDay()->modify("+2 days");
}
if ($task->getPlannedOnDay()->format("w") == 0) {
$task->getPlannedOnDay()->modify("+1 days");
}
$t[] = $this->generateEventsCalendar($task, $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
$task->getPlannedOnDay()->modify("+2 days");
$task->getPlannedOnDay()->modify("-1 hour");
}
} elseif ($task->repeatThreeDays()) {
$startTask = $task->getPlannedOnDay();
$diff = $startTask->diff($end);
for ($i = 0; $i <= ceil($diff->days / 3); $i++) {
if ($task->getPlannedOnDay()->format("w") == 6) {
$task->getPlannedOnDay()->modify("+2 days");
}
if ($task->getPlannedOnDay()->format("w") == 0) {
$task->getPlannedOnDay()->modify("+1 days");
}
$t[] = $this->generateEventsCalendar($task, $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
$task->getPlannedOnDay()->modify("+3 days");
$task->getPlannedOnDay()->modify("-1 hour");
}
} elseif ($task->repeatFourDays()) {
$startTask = $task->getPlannedOnDay();
$diff = $startTask->diff($end);
for ($i = 0; $i <= ceil($diff->days / 4); $i++) {
if ($task->getPlannedOnDay()->format("w") == 6) {
$task->getPlannedOnDay()->modify("+2 days");
}
if ($task->getPlannedOnDay()->format("w") == 0) {
$task->getPlannedOnDay()->modify("+1 days");
}
$t[] = $this->generateEventsCalendar($task, $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
$task->getPlannedOnDay()->modify("+4 days");
$task->getPlannedOnDay()->modify("-1 hour");
}
} elseif ($task->repeatWeekly()) {
$startTask = $task->getPlannedOnDay();
$diff = $startTask->diff($end);
for ($i = 0; $i <= ceil($diff->days / 5); $i++) {
if ($task->getPlannedOnDay()->format("w") == 6) {
$task->getPlannedOnDay()->modify("+2 days");
}
if ($task->getPlannedOnDay()->format("w") == 0) {
$task->getPlannedOnDay()->modify("+1 days");
}
$t[] = $this->generateEventsCalendar($task, $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
$task->getPlannedOnDay()->modify("+7 days");
$task->getPlannedOnDay()->modify("-1 hour");
}
} elseif ($task->repeatMonthly()) {
$startTask = $task->getPlannedOnDay();
$diff = $startTask->diff($end);
for ($i = 0; $i <= $diff->m; $i++) {
if ($task->getPlannedOnDay()->format("w") == 6) {
$task->getPlannedOnDay()->modify("+2 days");
}
if ($task->getPlannedOnDay()->format("w") == 0) {
$task->getPlannedOnDay()->modify("+1 days");
}
$t[] = $this->generateEventsCalendar($task, $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
$task->getPlannedOnDay()->modify("+1 month");
$task->getPlannedOnDay()->modify("-1 hour");
}
}
} else {
$t[] = $this->generateEventsCalendar($task, $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
}
}
return $this->json($t);
}
/**
* @Route("/my-task", name="task_feed")
*/
public function myTask(Request $request, UserRepository $userRepository): Response
{
if (!$request->get("project")) {
$defaultProject = null;
} else {
$defaultProject = $request->get("project")
? $this->projectRepository->findOneBy(["id" => $request->get("project"), 'type' => Project::TYPE_ACTIVE])
: null;
}
if (!$request->get("user") && !$request->get("createdBy")) {
$defaultUser = $this->getUser();
} else {
if ($request->get("user") == "list") {
$defaultUser = null;
// $this->isList = true;
} else {
$defaultUser = $request->get("user")
? $userRepository->findOneBy(["id" => $request->get("user")])
: null;
}
}
$createdBy = null;
if ($request->get("createdBy")) {
$createdBy = $this->getUser();
}
//
// $start = (new \DateTime('@'.strtotime($request->get('start'))))->setTimezone(new \DateTimeZone("Europe/Warsaw"));
$end = (new \DateTime())->setTimezone(new \DateTimeZone("Europe/Warsaw"));
if ($this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_EMPLOYER")) {
$tasks = $this->taskRepository->findTasks([
"project" => $this->projectRepository->findOneByClient($this->getUser()),
"plannedOnDayStart" => new \DateTimeImmutable("midnight first day of this month"),
"plannedOnDayEnd" => new \DateTimeImmutable("last day of this month 23:59"),
"user" => $defaultUser,
"createdBy" => $createdBy,
"list" => $request->get("user") == "list",
]);
} else {
$tasks = $this->taskRepository->findTasks([
"project" => $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_EMPLOYER") ? $this->projectRepository->findOneByClient($this->getUser()) : $defaultProject,
// "plannedOnDayStart" => $start,
// "plannedOnDayEnd" => $end,
"status" => $request->get("status") ? null : Task::STATUS_PLANNED,
"user" => $defaultUser,
"createdBy" => $createdBy,
"list" => $request->get("user") == "list",
]);
}
$t = [];
/** @var Task $task */
foreach ($tasks as $task) {
if ($task->getProject()->isDeactivated()) {
continue;
}
if ($task->getOfferPackageTask()) {
$t[] = $this->generateEventFromOfferTask($task, $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
} elseif ($task->isRepeatable()) {
if ($task->repeatDays()) {
$startTask = $task->getPlannedOnDay();
$diff = $startTask->diff($end);
for ($i = 0; $i <= $diff->days; $i++) {
if ($task->getPlannedOnDay()->format("w") == 6) {
$task->getPlannedOnDay()->modify("+2 days");
}
if ($task->getPlannedOnDay()->format("w") == 0) {
$task->getPlannedOnDay()->modify("+1 days");
}
$t[] = $this->generateEvents($task, $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
$task->getPlannedOnDay()->modify("+1 days");
$task->getPlannedOnDay()->modify("-1 hour");
}
} elseif ($task->repeatTwoDays()) {
$startTask = $task->getPlannedOnDay();
$diff = $startTask->diff($end);
for ($i = 0; $i <= ceil($diff->days / 2); $i++) {
if ($task->getPlannedOnDay()->format("w") == 6) {
$task->getPlannedOnDay()->modify("+2 days");
}
if ($task->getPlannedOnDay()->format("w") == 0) {
$task->getPlannedOnDay()->modify("+1 days");
}
$t[] = $this->generateEvents($task, $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
$task->getPlannedOnDay()->modify("+2 days");
$task->getPlannedOnDay()->modify("-1 hour");
}
} elseif ($task->repeatThreeDays()) {
$startTask = $task->getPlannedOnDay();
$diff = $startTask->diff($end);
for ($i = 0; $i <= ceil($diff->days / 3); $i++) {
if ($task->getPlannedOnDay()->format("w") == 6) {
$task->getPlannedOnDay()->modify("+2 days");
}
if ($task->getPlannedOnDay()->format("w") == 0) {
$task->getPlannedOnDay()->modify("+1 days");
}
$t[] = $this->generateEvents($task, $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
$task->getPlannedOnDay()->modify("+3 days");
$task->getPlannedOnDay()->modify("-1 hour");
}
} elseif ($task->repeatFourDays()) {
$startTask = $task->getPlannedOnDay();
$diff = $startTask->diff($end);
for ($i = 0; $i <= ceil($diff->days / 4); $i++) {
if ($task->getPlannedOnDay()->format("w") == 6) {
$task->getPlannedOnDay()->modify("+2 days");
}
if ($task->getPlannedOnDay()->format("w") == 0) {
$task->getPlannedOnDay()->modify("+1 days");
}
$t[] = $this->generateEvents($task, $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
$task->getPlannedOnDay()->modify("+4 days");
$task->getPlannedOnDay()->modify("-1 hour");
}
} elseif ($task->repeatWeekly()) {
$startTask = $task->getPlannedOnDay();
$diff = $startTask->diff($end);
for ($i = 0; $i <= ceil($diff->days / 5); $i++) {
if ($task->getPlannedOnDay()->format("w") == 6) {
$task->getPlannedOnDay()->modify("+2 days");
}
if ($task->getPlannedOnDay()->format("w") == 0) {
$task->getPlannedOnDay()->modify("+1 days");
}
$t[] = $this->generateEvents($task, $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
$task->getPlannedOnDay()->modify("+7 days");
$task->getPlannedOnDay()->modify("-1 hour");
}
} elseif ($task->repeatMonthly()) {
$startTask = $task->getPlannedOnDay();
$diff = $startTask->diff($end);
for ($i = 0; $i <= $diff->m; $i++) {
if ($task->getPlannedOnDay()->format("w") == 6) {
$task->getPlannedOnDay()->modify("+2 days");
}
if ($task->getPlannedOnDay()->format("w") == 0) {
$task->getPlannedOnDay()->modify("+1 days");
}
$t[] = $this->generateEvents($task, $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
$task->getPlannedOnDay()->modify("+1 month");
$task->getPlannedOnDay()->modify("-1 hour");
}
}
} else {
$t[] = $this->generateEvents($task, $this->isGranted("ROLE_CLIENT") && !$this->isGranted("ROLE_ADMIN") && !$this->isGranted("ROLE_EMPLOYER"));
}
}
return $this->json($t);
}
/**
* @param Task $task
* @param bool $client
* @return array
*/
private function generateEventFromOfferTask(Task $task, bool $client): array
{
$usersName = [];
foreach ($task->getUsers() as $user) {
$usersName[] = $user->getName();
}
$userName = implode(', ', $usersName);
if ($this->isList) {
return [
"title" => $task->getOfferPackageTask()->getName()." - ".$userName." - ".$task->getProject()->getName(),
"task_id" => $task->getOfferPackageTask()->getId(),
"normal_task_id" => $task->getId(),
"project_id" => $task->getProject()->getId(),
"url_update" => $this->generateUrl('update_calendar_task', ["task" => $task->getId()]),
"start" => $task->getPlannedOnDay()->format("Y-m-d H:i"),
"end" => $task->getPlannedOnDay()->modify("+30 minute")->format("Y-m-d H:i"),
"editable" => $this->canEditEvent($task->getUsers()->first()),
"backgroundColor" => $task->getUsers()->first()->getColor() ?? "blue",
"urlEndTask" => $this->generateUrl('task_edit_status', ["status" => Task::STATUS_COMPLETED, "task" => $task->getId()]),
"urlToTask" => $client ? "" : $this->generateUrl('project_show', ["project" => $task->getProject()->getId()]),
"task_completed" => $task->isCompleted(),
"task_basic" => (int)$task->getOfferPackageTask()->isBasic(),
"client" => $client,
"task_status" => $task->getStatus(),
];
}
$taskContent = "<a href='".$this->generateUrl('show_task', ["task" => $task->getId()])."'>".$task->getOfferPackageTask()->getName()." - ".$userName." - ".$task->getProject()->getName()."</a>";
if ($this->isGranted("ROLE_CLIENT")) {
$taskContent = $task->getOfferPackageTask()->getName()." - ".$userName." - ".$task->getProject()->getName();
}
return [
"task_content" => $taskContent,
"task_project" => $task->getProject()->getName(),
"task_deadline" => $task->getPlannedOnDay()->format("Y-m-d H:i"),
"task_status" => $this->translator->trans("task.status_".$task->getStatus()),
"task_who_assing" => $task->getCreatedBy()->getName(),
"task_comment" => $task->getContent(),
];
}
/**
* @param Task $task
* @return array
*/
private function generateEvents(Task $task, bool $client): array
{
$usersName = [];
foreach ($task->getUsers() as $user) {
$usersName[] = $user->getName();
}
$userName = implode(', ', $usersName);
if ($this->isList) {
return [
"title" => $task->getName()." - ".$userName." - ".$task->getProject()->getName(),
"url_update" => $this->generateUrl('update_calendar_task', ["task" => $task->getId()]),
"start" => $task->getPlannedOnDay()->format("Y-m-d H:i"),
"end" => $task->getPlannedOnDay()->modify("+30 minute")->format("Y-m-d H:i"),
"editable" => $this->canEditEvent($task->getUsers()->first()),
"backgroundColor" => $task->getUsers()->first()->getColor() ?? "blue",
"urlEndTask" => $this->generateUrl('task_edit_status', ["status" => Task::STATUS_COMPLETED, "task" => $task->getId()]),
"urlToTask" => $client ? "" : $this->generateUrl('show_task', ["task" => $task->getId()]),
"task_completed" => $task->isCompleted(),
"project_id" => $task->getProject()->getId(),
"task_id" => $task->getId(),
"client" => $client,
"task_status" => $task->getStatus(),
];
}
$taskContent = "<a href='".$this->generateUrl('show_task', ["task" => $task->getId()])."'>".$task->getName()." - ".$userName." - ".$task->getProject()->getName()."</a>";
if ($this->isGranted("ROLE_CLIENT")) {
$taskContent = $task->getName()." - ".$userName." - ".$task->getProject()->getName();
}
return [
"task_content" => $taskContent,
"task_project" => $task->getProject()->getName(),
"task_deadline" => $task->getPlannedOnDay()->format("Y-m-d H:i"),
"task_status" => $this->translator->trans("task.status_".$task->getStatus()),
"task_who_assing" => $task->getCreatedBy()->getName(),
"task_comment" => $task->getContent(),
//
//
// "url_update" => $this->generateUrl('update_calendar_task', ["task" => $task->getId()]),
// "url" => $client ? "" : $this->generateUrl('show_task', ["task" => $task->getId()]),
// "start" => $task->getPlannedOnDay()->format("Y-m-d H:i"),
// "end" => $task->getPlannedOnDay()->modify("+30 minute")->format("Y-m-d H:i"),
// "editable" => $this->canEditEvent($task->getUsers()->first()),
// "backgroundColor" => $task->getUsers()->first()->getColor() ?? "blue",
// "task_completed" => $task->isCompleted(),
// "client" => $client,
// "task_status" => $task->getStatus(),
];
}
/**
* @Route("/can-task-time", name="task_can_task_time")
*/
public function canTaskTime(Request $request, UserRepository $userRepository): JsonResponse
{
$users = json_decode($request->get('users'));
$return = [];
$time = new \DateTime($request->get("time"));
foreach ($users as $userId) {
$user = $userRepository->findOneBy(["id" => $userId]);
$tasks = $this->taskRepository->findTasks([
"user" => $user,
"plannedOnDayStart" => $time,
"plannedOnDayEnd" => $time,
]);
$bothEmptyTime = null;
if ($tasks) {
foreach ($tasks as $task) {
if ($task->getPlannedOnDay() == $time) {
$t = clone $time;
$emptyTime = null;
for ($i = 0; $i < 50; $i++) {
$t = $t->modify("+30 min");
$tempTasks = $this->taskRepository->findTasks([
"user" => $user,
"plannedOnDayStart" => $t,
"plannedOnDayEnd" => $t,
"hard" => true,
]);
$tempTasksCurrentUser = $this->taskRepository->findTasks([
"user" => $this->getUser(),
"plannedOnDayStart" => $t,
"plannedOnDayEnd" => $t,
"hard" => true,
]);
if (!$tempTasks && !$emptyTime) {
$emptyTime = $t;
}
if (!$tempTasksCurrentUser && !$tempTasks) {
$bothEmptyTime = $t;
break;
}
}
$return[] = [
"Użytkownik ".$user->getName()." posiada zadanie w tym czasie. Kolejny wolny termin to: ".$emptyTime->format("Y-m-d H:i:s")."<br>".
"Wasz najbliższy wolny wspólny termin: ".$bothEmptyTime->format("Y-m-d H:i:s")."<br>"."<br>",
];
}
}
}
}
if (count($return) == 0) {
return $this->json(["can" => true]);
}
return $this->json(["can" => false, "error" => $return]);
}
/**
* @IsGranted("ROLE_ADMIN")
* @Route("/opoznienia", name="delayed_tasks")
* @return Response
*/
public function taskDelayed(Request $request, UserRepository $userRepository): Response
{
$defaultDateFrom = new \DateTime("midnight first day of this month");
$defaultDateTo = new \DateTime("-1 day 23:59");
$form = $this->createForm(TaskDelayedRangeType::class, null, [
"dateFrom" => $defaultDateFrom,
"dateTo" => $defaultDateTo,
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$defaultDateFrom = $form->get('dateFrom')->getData();
$defaultDateTo = $form->get('dateTo')->getData();
if ($defaultDateTo > new \DateTime("-1 day")) {
$defaultDateTo = new \DateTime("-1 day");
}
}
$data = [];
foreach ($userRepository->getUserByRole("ROLE_EMPLOYER") as $user) {
if (!isset($data[$user->getId()])) {
$data[$user->getName()] = [];
}
foreach ($this->taskRepository->findDelayedTask($user, [
"plannedOnDayStart" => $defaultDateFrom,
"plannedOnDayEnd" => $defaultDateTo,
]) as $task) {
$data[$user->getName()][] = $task;
}
if (empty($data[$user->getName()])) {
unset($data[$user->getName()]);
}
}
return $this->render("task/delayed.html.twig", [
"data" => $data,
"form" => $form->createView(),
]);
}
/**
* @Route("/{task}", name="show_task")
* @IsGranted("ROLE_EMPLOYER")
*/
public function show(Task $task): Response
{
return $this->render("task/show.html.twig", [
"task" => $task,
]);
}
/**
* @param User $user
* @return bool
*/
private function canEditEvent(User $user): bool
{
if ($this->isGranted("ROLE_ADMIN")) {
return true;
}
if ($this->getUser() == $user) {
return true;
}
return false;
}
/**
* @param Task $task
* @return string
*/
private function getChannelId(Task $task): string
{
return $task->getProject()->getSlackChannelId() ?? "C02B302PFQE";
}
private function generateEventFromOfferTaskCalendar(Task $task, bool $client): array
{
$usersName = [];
foreach ($task->getUsers() as $user) {
$usersName[] = $user->getName();
}
$userName = implode(', ', $usersName);
return [
"title" => $task->getOfferPackageTask()->getName()." - ".$userName." - ".$task->getProject()->getName(),
"start" => $task->getPlannedOnDay()->format("Y-m-d H:i"),
"end" => $task->getPlannedOnDay()->modify("+30 minute")->format("Y-m-d H:i"),
"editable" => false,
"backgroundColor" => $task->getUsers()->first() ? $task->getUsers()->first()->getColor() ?? "blue" : "blue",
"task_completed" => $task->isCompleted(),
"task_status" => $task->getStatus(),
];
}
/**
* @param Task $task
* @return array
*/
private function generateEventsCalendar(Task $task, bool $client): array
{
$usersName = [];
foreach ($task->getUsers() as $user) {
$usersName[] = $user->getName();
}
$userName = implode(', ', $usersName);
return [
"title" => $task->getName()." - ".$userName." - ".$task->getProject()->getName(),
"start" => $task->getPlannedOnDay()->format("Y-m-d H:i"),
"end" => $task->getPlannedOnDay()->modify("+30 minute")->format("Y-m-d H:i"),
"editable" => false,
"backgroundColor" => $task->getUsers()->first()->getColor() ?? "blue",
"task_completed" => $task->isCompleted(),
"task_status" => $task->getStatus(),
];
}
}