mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
459 lines
15 KiB
PHP
459 lines
15 KiB
PHP
<?php
|
|
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
|
|
* <http://www.champs-libres.coop>, <info@champs-libres.coop>
|
|
*
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
namespace Chill\ActivityBundle\Controller;
|
|
|
|
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
|
use Chill\PersonBundle\Privacy\PrivacyEvent;
|
|
use Psr\Log\LoggerInterface;
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
|
use Symfony\Component\Security\Core\Role\Role;
|
|
use Chill\ActivityBundle\Entity\Activity;
|
|
use Chill\PersonBundle\Entity\Person;
|
|
use Chill\ActivityBundle\Form\ActivityType;
|
|
|
|
/**
|
|
* Class ActivityController
|
|
*
|
|
* @package Chill\ActivityBundle\Controller
|
|
*/
|
|
class ActivityController extends AbstractController
|
|
{
|
|
|
|
/**
|
|
* @var EventDispatcherInterface
|
|
*/
|
|
protected $eventDispatcher;
|
|
|
|
/**
|
|
* @var AuthorizationHelper
|
|
*/
|
|
protected $authorizationHelper;
|
|
|
|
/**
|
|
* @var LoggerInterface
|
|
*/
|
|
protected $logger;
|
|
|
|
/**
|
|
* ActivityController constructor.
|
|
*
|
|
* @param EventDispatcherInterface $eventDispatcher
|
|
* @param AuthorizationHelper $authorizationHelper
|
|
*/
|
|
public function __construct(
|
|
EventDispatcherInterface $eventDispatcher,
|
|
AuthorizationHelper $authorizationHelper,
|
|
LoggerInterface $logger
|
|
) {
|
|
$this->eventDispatcher = $eventDispatcher;
|
|
$this->authorizationHelper = $authorizationHelper;
|
|
$this->logger = $logger;
|
|
}
|
|
|
|
/**
|
|
* Lists all Activity entities.
|
|
*
|
|
*/
|
|
public function listAction($person_id, Request $request)
|
|
{
|
|
$em = $this->getDoctrine()->getManager();
|
|
$person = $em->getRepository('ChillPersonBundle:Person')->find($person_id);
|
|
|
|
if ($person === NULL) {
|
|
throw $this->createNotFoundException('Person not found');
|
|
}
|
|
|
|
$this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person);
|
|
|
|
$reachableScopes = $this->authorizationHelper
|
|
->getReachableScopes($this->getUser(), new Role('CHILL_ACTIVITY_SEE'),
|
|
$person->getCenter());
|
|
|
|
$activities = $em->getRepository('ChillActivityBundle:Activity')
|
|
->findBy(
|
|
array('person' => $person, 'scope' => $reachableScopes),
|
|
array('date' => 'DESC')
|
|
);
|
|
|
|
$event = new PrivacyEvent($person, array(
|
|
'element_class' => Activity::class,
|
|
'action' => 'list'
|
|
));
|
|
$this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event);
|
|
|
|
return $this->render('ChillActivityBundle:Activity:list.html.twig', array(
|
|
'activities' => $activities,
|
|
'person' => $person
|
|
));
|
|
}
|
|
/**
|
|
* Creates a new Activity entity.
|
|
*
|
|
*/
|
|
public function createAction($person_id, Request $request)
|
|
{
|
|
$em = $this->getDoctrine()->getManager();
|
|
$person = $em->getRepository('ChillPersonBundle:Person')->find($person_id);
|
|
|
|
if ($person === NULL) {
|
|
throw $this->createNotFoundException('person not found');
|
|
}
|
|
|
|
$this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person);
|
|
|
|
$entity = new Activity();
|
|
$entity->setPerson($person);
|
|
$form = $this->createCreateForm($entity, $person);
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isValid()) {
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$this->denyAccessUnlessGranted('CHILL_ACTIVITY_CREATE', $entity,
|
|
'creation of this activity not allowed');
|
|
|
|
$em->persist($entity);
|
|
$em->flush();
|
|
|
|
$this->get('session')
|
|
->getFlashBag()
|
|
->add('success',
|
|
$this->get('translator')
|
|
->trans('Success : activity created!')
|
|
);
|
|
|
|
return $this->redirect(
|
|
$this->generateUrl('chill_activity_activity_show',
|
|
array('id' => $entity->getId(), 'person_id' => $person_id)));
|
|
}
|
|
|
|
$this->get('session')
|
|
->getFlashBag()->add('danger',
|
|
$this->get('translator')
|
|
->trans('The form is not valid. The activity has not been created !')
|
|
);
|
|
|
|
return $this->render('ChillActivityBundle:Activity:new.html.twig', array(
|
|
'entity' => $entity,
|
|
'form' => $form->createView(),
|
|
'person' => $person
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Creates a form to create a Activity entity.
|
|
*
|
|
* @param Activity $entity The entity
|
|
*
|
|
* @return \Symfony\Component\Form\Form The form
|
|
*/
|
|
private function createCreateForm(Activity $entity)
|
|
{
|
|
$form = $this->createForm(ActivityType::class, $entity,
|
|
array(
|
|
'action' => $this->generateUrl('chill_activity_activity_create', [
|
|
'person_id' => $entity->getPerson()->getId(),
|
|
]),
|
|
'method' => 'POST',
|
|
'center' => $entity->getCenter(),
|
|
'role' => new Role('CHILL_ACTIVITY_CREATE')
|
|
)
|
|
);
|
|
|
|
return $form;
|
|
}
|
|
|
|
/**
|
|
* Displays a form to create a new Activity entity.
|
|
*
|
|
*/
|
|
public function newAction($person_id)
|
|
{
|
|
$em = $this->getDoctrine()->getManager();
|
|
$person = $em->getRepository('ChillPersonBundle:Person')->find($person_id);
|
|
|
|
if ($person === NULL){
|
|
throw $this->createNotFoundException('Person not found');
|
|
}
|
|
|
|
$this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person);
|
|
|
|
$entity = new Activity();
|
|
$entity->setUser($this->get('security.token_storage')->getToken()->getUser());
|
|
$entity->setPerson($person);
|
|
$entity->setDate(new \DateTime('now'));
|
|
|
|
$this->denyAccessUnlessGranted('CHILL_ACTIVITY_CREATE', $entity);
|
|
|
|
$form = $this->createCreateForm($entity, $person);
|
|
|
|
return $this->render('ChillActivityBundle:Activity:new.html.twig', array(
|
|
'person' => $person,
|
|
'entity' => $entity,
|
|
'form' => $form->createView(),
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Finds and displays a Activity entity.
|
|
*
|
|
*/
|
|
public function showAction($person_id, $id)
|
|
{
|
|
$em = $this->getDoctrine()->getManager();
|
|
$person = $em->getRepository('ChillPersonBundle:Person')->find($person_id);
|
|
|
|
if (!$person) {
|
|
throw $this->createNotFoundException('person not found');
|
|
}
|
|
|
|
$this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person);
|
|
|
|
$entity = $em->getRepository('ChillActivityBundle:Activity')->find($id);
|
|
|
|
if (!$entity) {
|
|
throw $this->createNotFoundException('Unable to find Activity entity.');
|
|
}
|
|
|
|
$this->denyAccessUnlessGranted('CHILL_ACTIVITY_SEE', $entity);
|
|
|
|
$deleteForm = $this->createDeleteForm($id, $person);
|
|
|
|
$event = new PrivacyEvent($person, array(
|
|
'element_class' => Activity::class,
|
|
'element_id' => $entity->getId(),
|
|
'action' => 'show'
|
|
));
|
|
$this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event);
|
|
|
|
return $this->render('ChillActivityBundle:Activity:show.html.twig', array(
|
|
'person' => $person,
|
|
'entity' => $entity,
|
|
'delete_form' => $deleteForm->createView(),
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Displays a form to edit an existing Activity entity.
|
|
*
|
|
*/
|
|
public function editAction($person_id, $id)
|
|
{
|
|
$em = $this->getDoctrine()->getManager();
|
|
$person = $em->getRepository('ChillPersonBundle:Person')->find($person_id);
|
|
|
|
if (!$person) {
|
|
throw $this->createNotFoundException('person not found');
|
|
}
|
|
|
|
$this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person);
|
|
|
|
$entity = $em->getRepository('ChillActivityBundle:Activity')->find($id);
|
|
|
|
if (!$entity) {
|
|
throw $this->createNotFoundException('Unable to find Activity entity.');
|
|
}
|
|
|
|
$this->denyAccessUnlessGranted('CHILL_ACTIVITY_UPDATE', $entity);
|
|
|
|
$editForm = $this->createEditForm($entity);
|
|
$deleteForm = $this->createDeleteForm($id, $person);
|
|
|
|
$event = new PrivacyEvent($person, array(
|
|
'element_class' => Activity::class,
|
|
'element_id' => $entity->getId(),
|
|
'action' => 'edit'
|
|
));
|
|
$this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event);
|
|
|
|
return $this->render('ChillActivityBundle:Activity:edit.html.twig', array(
|
|
'entity' => $entity,
|
|
'edit_form' => $editForm->createView(),
|
|
'delete_form' => $deleteForm->createView(),
|
|
'person' => $person
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Creates a form to edit a Activity entity.
|
|
*
|
|
* @param Activity $entity The entity
|
|
*
|
|
* @return \Symfony\Component\Form\Form The form
|
|
*/
|
|
private function createEditForm(Activity $entity)
|
|
{
|
|
$form = $this->createForm(ActivityType::class, $entity, array(
|
|
'action' => $this->generateUrl('chill_activity_activity_update',
|
|
array(
|
|
'id' => $entity->getId(),
|
|
'person_id' => $entity->getPerson()->getId()
|
|
)),
|
|
'method' => 'PUT',
|
|
'center' => $entity->getCenter(),
|
|
'role' => new Role('CHILL_ACTIVITY_UPDATE')
|
|
));
|
|
|
|
return $form;
|
|
}
|
|
/**
|
|
* Edits an existing Activity entity.
|
|
*
|
|
*/
|
|
public function updateAction(Request $request, $person_id, $id)
|
|
{
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$person = $em->getRepository('ChillPersonBundle:Person')->find($person_id);
|
|
$entity = $em->getRepository('ChillActivityBundle:Activity')->find($id);
|
|
|
|
if (!$entity) {
|
|
throw $this->createNotFoundException('Unable to find Activity entity.');
|
|
}
|
|
|
|
$this->denyAccessUnlessGranted('CHILL_ACTIVITY_UPDATE', $entity);
|
|
|
|
$deleteForm = $this->createDeleteForm($id, $person);
|
|
$editForm = $this->createEditForm($entity);
|
|
$editForm->handleRequest($request);
|
|
|
|
$event = new PrivacyEvent($person, array(
|
|
'element_class' => Activity::class,
|
|
'element_id' => $entity->getId(),
|
|
'action' => 'update'
|
|
));
|
|
$this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event);
|
|
|
|
if ($editForm->isValid()) {
|
|
$em->flush();
|
|
|
|
$this->get('session')
|
|
->getFlashBag()
|
|
->add('success',
|
|
$this->get('translator')
|
|
->trans('Success : activity updated!')
|
|
);
|
|
|
|
return $this->redirect($this->generateUrl('chill_activity_activity_show', array('id' => $id, 'person_id' => $person_id)));
|
|
}
|
|
|
|
$this->get('session')
|
|
->getFlashBag()
|
|
->add('error',
|
|
$this->get('translator')
|
|
->trans('This form contains errors')
|
|
);
|
|
|
|
return $this->render('ChillActivityBundle:Activity:edit.html.twig', array(
|
|
'person' => $entity->getPerson(),
|
|
'entity' => $entity,
|
|
'edit_form' => $editForm->createView(),
|
|
'delete_form' => $deleteForm->createView(),
|
|
));
|
|
}
|
|
|
|
/**
|
|
* Deletes a Activity entity.
|
|
*
|
|
*/
|
|
public function deleteAction(Request $request, $id, $person_id)
|
|
{
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
/* @var $activity Activity */
|
|
$activity = $em->getRepository('ChillActivityBundle:Activity')
|
|
->find($id);
|
|
$person = $activity->getPerson();
|
|
|
|
if (!$activity) {
|
|
throw $this->createNotFoundException('Unable to find Activity entity.');
|
|
}
|
|
|
|
$this->denyAccessUnlessGranted('CHILL_ACTIVITY_DELETE', $activity);
|
|
|
|
$form = $this->createDeleteForm($id, $person);
|
|
|
|
if ($request->getMethod() === Request::METHOD_DELETE) {
|
|
$form->handleRequest($request);
|
|
|
|
if ($form->isValid()) {
|
|
|
|
$this->logger->notice("An activity has been removed", array(
|
|
'by_user' => $this->getUser()->getUsername(),
|
|
'activity_id' => $activity->getId(),
|
|
'person_id' => $activity->getPerson()->getId(),
|
|
'comment' => $activity->getComment()->getComment(),
|
|
'scope_id' => $activity->getScope()->getId(),
|
|
'reasons_ids' => $activity->getReasons()
|
|
->map(function ($ar) { return $ar->getId(); })
|
|
->toArray(),
|
|
'type_id' => $activity->getType()->getId(),
|
|
'duration' => $activity->getDurationTime()->format('U'),
|
|
'date' => $activity->getDate()->format('Y-m-d'),
|
|
'attendee' => $activity->getAttendee()
|
|
));
|
|
|
|
$em->remove($activity);
|
|
$em->flush();
|
|
|
|
$this->addFlash('success', $this->get('translator')
|
|
->trans("The activity has been successfully removed."));
|
|
|
|
return $this->redirect($this->generateUrl(
|
|
'chill_activity_activity_list', array(
|
|
'person_id' => $person_id
|
|
)));
|
|
}
|
|
}
|
|
|
|
return $this->render('ChillActivityBundle:Activity:confirm_delete.html.twig', array(
|
|
'activity' => $activity,
|
|
'delete_form' => $form->createView()
|
|
));
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
* Creates a form to delete a Activity entity by id.
|
|
*
|
|
* @param mixed $id The entity id
|
|
*
|
|
* @return \Symfony\Component\Form\Form The form
|
|
*/
|
|
private function createDeleteForm($id, $person)
|
|
{
|
|
return $this->createFormBuilder()
|
|
->setAction($this->generateUrl(
|
|
'chill_activity_activity_delete',
|
|
array('id' => $id, 'person_id' => $person->getId())))
|
|
->setMethod('DELETE')
|
|
->add('submit', SubmitType::class, array('label' => 'Delete'))
|
|
->getForm()
|
|
;
|
|
}
|
|
}
|