diff --git a/src/Bundle/ChillActivityBundle/Controller/ActivityController.php b/src/Bundle/ChillActivityBundle/Controller/ActivityController.php index 9b1f3f48a..543aa1f6b 100644 --- a/src/Bundle/ChillActivityBundle/Controller/ActivityController.php +++ b/src/Bundle/ChillActivityBundle/Controller/ActivityController.php @@ -4,15 +4,20 @@ declare(strict_types=1); namespace Chill\ActivityBundle\Controller; +use Chill\ActivityBundle\Entity\ActivityReason; +use Chill\ActivityBundle\Entity\ActivityTypeCategory; use Chill\ActivityBundle\Repository\ActivityACLAwareRepositoryInterface; use Chill\ActivityBundle\Security\Authorization\ActivityVoter; +use Chill\MainBundle\Entity\Location; use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\PersonBundle\Entity\AccompanyingPeriod; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Privacy\PrivacyEvent; +use Chill\ThirdPartyBundle\Entity\ThirdParty; use Psr\Log\LoggerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Form\Form; +use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Form\Extension\Core\Type\SubmitType; @@ -65,10 +70,10 @@ final class ActivityController extends AbstractController $activities = $this->activityACLAwareRepository ->findByPerson($person, ActivityVoter::SEE, 0, null); - $event = new PrivacyEvent($person, array( + $event = new PrivacyEvent($person, [ 'element_class' => Activity::class, 'action' => 'list' - )); + ]); $this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event); $view = 'ChillActivityBundle:Activity:listPerson.html.twig'; @@ -106,11 +111,11 @@ final class ActivityController extends AbstractController $data = []; - $activityTypeCategories = $em->getRepository(\Chill\ActivityBundle\Entity\ActivityTypeCategory::class) + $activityTypeCategories = $em->getRepository(ActivityTypeCategory::class) ->findBy(['active' => true], ['ordering' => 'ASC']); foreach ($activityTypeCategories as $activityTypeCategory) { - $activityTypes = $em->getRepository(\Chill\ActivityBundle\Entity\ActivityType::class) + $activityTypes = $em->getRepository(ActivityType::class) ->findBy(['active' => true, 'category' => $activityTypeCategory], ['ordering' => 'ASC']); $data[] = [ @@ -119,12 +124,6 @@ final class ActivityController extends AbstractController ]; } - if ($request->query->has('activityData')) { - $activityData = $request->query->get('activityData'); - } else { - $activityData = []; - } - if ($view === null) { throw $this->createNotFoundException('Template not found'); } @@ -133,7 +132,7 @@ final class ActivityController extends AbstractController 'person' => $person, 'accompanyingCourse' => $accompanyingPeriod, 'data' => $data, - 'activityData' => $activityData + 'activityData' => $request->query->get('activityData', []), ]); } @@ -151,7 +150,7 @@ final class ActivityController extends AbstractController } $activityType_id = $request->get('activityType_id', 0); - $activityType = $em->getRepository(\Chill\ActivityBundle\Entity\ActivityType::class) + $activityType = $em->getRepository(ActivityType::class) ->find($activityType_id); if (isset($activityType) && !$activityType->isActive()) { @@ -210,20 +209,20 @@ final class ActivityController extends AbstractController if (array_key_exists('personsId', $activityData)) { foreach($activityData['personsId'] as $personId){ - $concernedPerson = $em->getRepository(\Chill\PersonBundle\Entity\Person::class)->find($personId); + $concernedPerson = $em->getRepository(Person::class)->find($personId); $entity->addPerson($concernedPerson); } } if (array_key_exists('professionalsId', $activityData)) { foreach($activityData['professionalsId'] as $professionalsId){ - $professional = $em->getRepository(\Chill\ThirdPartyBundle\Entity\ThirdParty::class)->find($professionalsId); + $professional = $em->getRepository(ThirdParty::class)->find($professionalsId); $entity->addThirdParty($professional); } } if (array_key_exists('location', $activityData)) { - $location = $em->getRepository(\Chill\MainBundle\Entity\Location::class)->find($activityData['location']); + $location = $em->getRepository(Location::class)->find($activityData['location']); $entity->setLocation($location); } @@ -288,13 +287,15 @@ final class ActivityController extends AbstractController $view = 'ChillActivityBundle:Activity:showPerson.html.twig'; } - $entity = $em->getRepository('ChillActivityBundle:Activity')->find($id); + /** @var Activity $entity */ + $entity = $em->getRepository(Activity::class)->find($id); - if (!$entity) { + if (null === $entity) { throw $this->createNotFoundException('Unable to find Activity entity.'); } if (null !== $accompanyingPeriod) { + // @TODO: Properties created dynamically. $entity->personsAssociated = $entity->getPersonsAssociated(); $entity->personsNotAssociated = $entity->getPersonsNotAssociated(); } @@ -302,7 +303,7 @@ final class ActivityController extends AbstractController // TODO revoir le Voter de Activity pour tenir compte qu'une activité peut appartenir a une période // $this->denyAccessUnlessGranted('CHILL_ACTIVITY_SEE', $entity); - $deleteForm = $this->createDeleteForm($id, $person, $accompanyingPeriod); + $deleteForm = $this->createDeleteForm($entity->getId(), $person, $accompanyingPeriod); // TODO /* @@ -318,17 +319,16 @@ final class ActivityController extends AbstractController throw $this->createNotFoundException('Template not found'); } - return $this->render($view, array( + return $this->render($view, [ 'person' => $person, 'accompanyingCourse' => $accompanyingPeriod, 'entity' => $entity, 'delete_form' => $deleteForm->createView(), - )); + ]); } /** * Displays a form to edit an existing Activity entity. - * */ public function editAction($id, Request $request): Response { @@ -343,9 +343,10 @@ final class ActivityController extends AbstractController $view = 'ChillActivityBundle:Activity:editPerson.html.twig'; } - $entity = $em->getRepository('ChillActivityBundle:Activity')->find($id); + /** @var Activity $entity */ + $entity = $em->getRepository(Activity::class)->find($id); - if (!$entity) { + if (null === $entity) { throw $this->createNotFoundException('Unable to find Activity entity.'); } @@ -366,11 +367,12 @@ final class ActivityController extends AbstractController $this->addFlash('success', $this->get('translator')->trans('Success : activity updated!')); $params = $this->buildParamsToUrl($person, $accompanyingPeriod); - $params['id'] = $id; + $params['id'] = $entity->getId(); + return $this->redirectToRoute('chill_activity_activity_show', $params); } - $deleteForm = $this->createDeleteForm($id, $person, $accompanyingPeriod); + $deleteForm = $this->createDeleteForm($entity->getId(), $person, $accompanyingPeriod); /* * TODO @@ -388,19 +390,18 @@ final class ActivityController extends AbstractController $activity_array = $this->serializer->normalize($entity, 'json', ['groups' => 'read']); - return $this->render($view, array( + return $this->render($view, [ 'entity' => $entity, 'edit_form' => $form->createView(), 'delete_form' => $deleteForm->createView(), 'person' => $person, 'accompanyingCourse' => $accompanyingPeriod, 'activity_json' => $activity_array - )); + ]); } /** * Deletes a Activity entity. - * */ public function deleteAction(Request $request, $id) { @@ -415,8 +416,8 @@ final class ActivityController extends AbstractController $view = 'ChillActivityBundle:Activity:confirm_deletePerson.html.twig'; } - /* @var $activity Activity */ - $activity = $em->getRepository('ChillActivityBundle:Activity')->find($id); + /* @var Activity $activity */ + $activity = $em->getRepository(Activity::class)->find($id); if (!$activity) { throw $this->createNotFoundException('Unable to find Activity entity.'); @@ -425,27 +426,28 @@ final class ActivityController extends AbstractController // TODO // $this->denyAccessUnlessGranted('CHILL_ACTIVITY_DELETE', $activity); - $form = $this->createDeleteForm($id, $person, $accompanyingPeriod); + $form = $this->createDeleteForm($activity->getId(), $person, $accompanyingPeriod); if ($request->getMethod() === Request::METHOD_DELETE) { $form->handleRequest($request); if ($form->isValid()) { - - $this->logger->notice("An activity has been removed", array( + $this->logger->notice("An activity has been removed", [ 'by_user' => $this->getUser()->getUsername(), 'activity_id' => $activity->getId(), 'person_id' => $activity->getPerson() ? $activity->getPerson()->getId() : null, 'comment' => $activity->getComment()->getComment(), 'scope_id' => $activity->getScope() ? $activity->getScope()->getId() : null, 'reasons_ids' => $activity->getReasons() - ->map(function ($ar) { return $ar->getId(); }) + ->map( + static fn (ActivityReason $ar): int => $ar->getId() + ) ->toArray(), 'type_id' => $activity->getType()->getId(), 'duration' => $activity->getDurationTime() ? $activity->getDurationTime()->format('U') : null, 'date' => $activity->getDate()->format('Y-m-d'), 'attendee' => $activity->getAttendee() - )); + ]); $em->remove($activity); $em->flush(); @@ -454,6 +456,7 @@ final class ActivityController extends AbstractController ->trans("The activity has been successfully removed.")); $params = $this->buildParamsToUrl($person, $accompanyingPeriod); + return $this->redirectToRoute('chill_activity_activity_list', $params); } } @@ -462,18 +465,18 @@ final class ActivityController extends AbstractController throw $this->createNotFoundException('Template not found'); } - return $this->render($view, array( + return $this->render($view, [ 'activity' => $activity, 'delete_form' => $form->createView(), 'person' => $person, 'accompanyingCourse' => $accompanyingPeriod, - )); + ]); } /** * Creates a form to delete a Activity entity by id. */ - private function createDeleteForm(int $id, ?Person $person, ?AccompanyingPeriod $accompanyingPeriod): Form + private function createDeleteForm(int $id, ?Person $person, ?AccompanyingPeriod $accompanyingPeriod): FormInterface { $params = $this->buildParamsToUrl($person, $accompanyingPeriod); $params['id'] = $id; @@ -481,9 +484,8 @@ final class ActivityController extends AbstractController return $this->createFormBuilder() ->setAction($this->generateUrl('chill_activity_activity_delete', $params)) ->setMethod('DELETE') - ->add('submit', SubmitType::class, array('label' => 'Delete')) - ->getForm() - ; + ->add('submit', SubmitType::class, ['label' => 'Delete']) + ->getForm(); } private function getEntity(Request $request): array @@ -515,21 +517,19 @@ final class ActivityController extends AbstractController } return [ - $person, $accompanyingPeriod + $person, + $accompanyingPeriod ]; } - private function buildParamsToUrl( - ?Person $person, - ?AccompanyingPeriod $accompanyingPeriod - ): array { + private function buildParamsToUrl(?Person $person, ?AccompanyingPeriod $accompanyingPeriod): array { $params = []; - if ($person) { + if (null !== $person) { $params['person_id'] = $person->getId(); } - if ($accompanyingPeriod) { + if (null !== $accompanyingPeriod) { $params['accompanying_period_id'] = $accompanyingPeriod->getId(); } diff --git a/src/Bundle/ChillActivityBundle/Entity/Activity.php b/src/Bundle/ChillActivityBundle/Entity/Activity.php index e25448f10..4ed654c85 100644 --- a/src/Bundle/ChillActivityBundle/Entity/Activity.php +++ b/src/Bundle/ChillActivityBundle/Entity/Activity.php @@ -1,27 +1,8 @@ - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - namespace Chill\ActivityBundle\Entity; use Chill\DocStoreBundle\Entity\Document; -use Chill\DocStoreBundle\Entity\StoredObject; use Chill\MainBundle\Entity\Embeddable\CommentEmbeddable; use Chill\MainBundle\Entity\Location; use Chill\PersonBundle\AccompanyingPeriod\SocialIssueConsistency\AccompanyingPeriodLinkedWithSocialIssuesEntityInterface; @@ -38,7 +19,7 @@ use Chill\MainBundle\Entity\HasCenterInterface; use Chill\MainBundle\Entity\HasScopeInterface; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ArrayCollection; -use Chill\MainBundle\Validator\Constraints\Entity\UserCircleConsistency; +use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Serializer\Annotation\DiscriminatorMap; @@ -202,7 +183,7 @@ class Activity implements HasCenterInterface, HasScopeInterface, AccompanyingPer return $this->id; } - public function setUser(User $user): self + public function setUser(UserInterface $user): self { $this->user = $user;