Newer
Older
activity-manager / src / Controller / ActivityController.php
<?php
namespace App\Controller;

use App\Entity\ActivityEntity;
use App\Entity\ActorEntity;
use App\Entity\TaskEntity;
use App\Form\Activity\ActivityAddForm;
use App\Form\Activity\ActivityDeleteForm;
use App\Form\Activity\ActivityEditForm;
use App\Form\Activity\ActivityFilterForm;
use App\Form\Task\TaskAddForm;
use App\Repository\ActivityRepository;
use App\Repository\ActorRepository;
use App\Repository\TaskRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGenerator;

/**
 * Controller for activity related pages
 */
class ActivityController extends AbstractExtendedController
{

    /**
     * Page for the list of activities
     *
     * @param Request $request
     * @return Response
     */
    public function activities(Request $request): Response
    {
        // Get repositories
        $entityManager = $this->getDoctrine()->getManager();
        /** @var ActivityRepository $activityRepository */
        $activityRepository = $entityManager->getRepository(ActivityEntity::class);
        /** @var ActorRepository $actorRepository */
        $actorRepository = $entityManager->getRepository(ActorEntity::class);

        // Get actors
        $actors = $actorRepository->findAll();

        // Activity add form
        /** @var ActivityAddForm $activityAddForm */
        $activityAddForm = $this->createNamedCustomForm('activityAdd', ActivityAddForm::class, [
            'actors' => $actors,
        ]);
        $activityAddForm->handleRequest($request);
        if ($activityAddForm->isSubmitted() && $activityAddForm->isValid()) {
            $errors = $activityAddForm->validate();
            if (empty($errors)) {
                $activity = $activityAddForm->getActivity();
                $entityManager->persist($activity);
                $entityManager->flush();
                return $this->ajaxFormAnswer([
                    'redirect' => $this->generateUrl('activity_activity', [
                        'id' => $activity->getId()
                    ], UrlGenerator::ABSOLUTE_PATH),
                ]);
            } else {
                return $this->ajaxFormAnswer([
                    'error' => join('<br/>', $errors),
                    'code' => Response::HTTP_BAD_REQUEST,
                ]);
            }
        }

        // Activity delete form
        /** @var ActivityDeleteForm $activityDeleteForm */
        $activityDeleteForm = $this->createNamedCustomForm('activityDelete', ActivityDeleteForm::class);
        $activityDeleteForm->handleRequest($request);
        if ($activityDeleteForm->isSubmitted() && $activityDeleteForm->isValid()) {
            $errors = $activityDeleteForm->validate();
            if (empty($errors)) {
                $activity = $activityDeleteForm->getActivity($activityRepository);
                if (!is_null($activity)) {
                    $entityManager->remove($activity);
                    $entityManager->flush();
                    return $this->ajaxFormAnswer([
                        'refresh' => true,
                    ]);
                } else {
                    return $this->ajaxFormAnswer([
                        'error' => 'Activité non trouvé',
                        'code' => Response::HTTP_BAD_REQUEST,
                    ]);
                }
            } else {
                return $this->ajaxFormAnswer([
                    'error' => join('<br/>', $errors),
                    'code' => Response::HTTP_BAD_REQUEST,
                ]);
            }
        }

        // Filter
        /** @var ActivityFilterForm $activityFilterForm */
        $activityFilterForm = $this->createNamedGetCustomForm('activityFilter', ActivityFilterForm::class, [
            'actors' => $actors,
        ]);
        $activityFilterForm->handleRequest($request);
        $activities = $activityFilterForm->getActivities($activityRepository);

        return $this->render('activity/activities.html.twig', [
            'activities' => $activities,
            'activityFilterForm' => $activityFilterForm,
            'activityDeleteForm' => $activityDeleteForm,
            'activityAddForm' => $activityAddForm,
        ]);
    }

    /**
     * Page for editing activity
     *
     * @param String $id
     * @param Request $request
     * @return Response
     */
    public function activity(string $id, Request $request): Response
    {
        // Get repositories
        $entityManager = $this->getDoctrine()->getManager();
        /** @var ActivityRepository $actorRepository */
        $activityRepository = $entityManager->getRepository(ActivityEntity::class);
        /** @var ActorRepository $actorRepository */
        $actorRepository = $entityManager->getRepository(ActorEntity::class);
        /** @var TaskRepository $taskRepository */
        $taskRepository = $entityManager->getRepository(TaskEntity::class);

        // Get activity
        $activity = $activityRepository->find($id);

        // Get actors
        $actors = $actorRepository->findAll();

        // Activity edit form
        /** @var ActivityEditForm $activityEditForm */
        $activityEditForm = $this->createNamedCustomForm('edit', ActivityEditForm::class, [
            'actors' => $actors,
        ]);
        $activityEditForm->handleRequest($request);
        if ($activityEditForm->isSubmitted() && $activityEditForm->isValid()) {
            $errors = $activityEditForm->validate();
            $errors = $activityEditForm->validate();
            if (empty($errors)) {
                $activityEditForm->updateActivity($activity);
                $entityManager->persist($activity);
                $entityManager->flush();
                return $this->ajaxFormAnswer([
                    'message' => 'Modification enregistrées',
                    'refresh' => true,
                ]);
            } else {
                return $this->ajaxFormAnswer([
                    'error' => join('<br/>', $errors),
                    'code' => Response::HTTP_BAD_REQUEST,
                ]);
            }
        }

        // Activity delete form
        /** @var ActivityDeleteForm $activityDeleteForm */
        $activityDeleteForm = $this->createNamedCustomForm('activityDelete', ActivityDeleteForm::class);
        $activityDeleteForm->handleRequest($request);
        if ($activityDeleteForm->isSubmitted() && $activityDeleteForm->isValid()) {
            $errors = $activityDeleteForm->validate();
            if (empty($errors)) {
                if (!is_null($activity)) {
                    $entityManager->remove($activity);
                    $entityManager->flush();
                    return $this->ajaxFormAnswer([
                        'redirect' => $this->generateUrl('activity_activities', [], UrlGenerator::ABSOLUTE_PATH),
                    ]);
                } else {
                    return $this->ajaxFormAnswer([
                        'error' => 'Activité non trouvé',
                        'code' => Response::HTTP_BAD_REQUEST,
                    ]);
                }
            } else {
                return $this->ajaxFormAnswer([
                    'error' => join('<br/>', $errors),
                    'code' => Response::HTTP_BAD_REQUEST,
                ]);
            }
        }

        // Task add form
        /** @var TaskAddForm $taskAddForm */
        $taskAddForm = $this->createNamedCustomForm('taskAdd', TaskAddForm::class, [
            'actors' => $actors,
        ]);
        $taskAddForm->handleRequest($request);
        if ($taskAddForm->isSubmitted() && $taskAddForm->isValid()) {
            $errors = $taskAddForm->validate();
            if (empty($errors)) {
                $task = $taskAddForm->getTask();
                $task->setActivity($activity);
                $entityManager->persist($task);
                $entityManager->flush();
                return $this->ajaxFormAnswer([
                    'refresh' => true,
                    'reset' => true,
                ]);
            } else {
                return $this->ajaxFormAnswer([
                    'error' => join('<br/>', $errors),
                    'code' => Response::HTTP_BAD_REQUEST,
                ]);
            }
        }

        // Update fields
        $activityEditForm->updateFields($activity);

        // Get tasks
        $tasks = $taskRepository->findBy([
            'activity' => $activity,
        ]);

        return $this->render('activity/activity.html.twig', [
            'activity' => $activity,
            'tasks' => $tasks,
            'activityEditForm' => $activityEditForm,
            'activityDeleteForm' => $activityDeleteForm,
            'taskAddForm' => $taskAddForm,
        ]);
    }
}