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

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Repository\ActivityRepository;
use App\Repository\ActorRepository;
use App\Entity\ActivityEntity;
use App\Entity\ActorEntity;
use App\Form\Activity\ActivityAddForm;
use App\Form\Activity\ActivityDeleteForm;
use App\Form\Activity\ActivityEditForm;
use App\Form\Activity\ActivityFilterForm;

/**
 * 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([
                    'refresh' => true,
                    'reset' => 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)) {
                $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);

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

        // Activity edit form
        /** @var ActivityEditForm $activityEditForm */
        $activityEditForm = $this->createNamedCustomForm('edit', ActivityEditForm::class, [
            'actors' => $actorRepository->findAll()
        ]);
        $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,
                ]);
            }
        }

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

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