Add first step to select Activity Type

This commit is contained in:
Jean-Francois Monfort 2021-04-15 13:08:30 +02:00
parent dad8fd5378
commit ff450215c6
8 changed files with 101 additions and 46 deletions

View File

@ -22,6 +22,7 @@
namespace Chill\ActivityBundle\Controller; namespace Chill\ActivityBundle\Controller;
use Chill\ActivityBundle\Form\ActivitySelectTypeType;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Chill\PersonBundle\Privacy\PrivacyEvent; use Chill\PersonBundle\Privacy\PrivacyEvent;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -29,9 +30,9 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Role\Role; use Symfony\Component\Security\Core\Role\Role;
use Chill\ActivityBundle\Entity\Activity; use Chill\ActivityBundle\Entity\Activity;
use Chill\PersonBundle\Entity\Person;
use Chill\ActivityBundle\Form\ActivityType; use Chill\ActivityBundle\Form\ActivityType;
/** /**
@ -41,27 +42,18 @@ use Chill\ActivityBundle\Form\ActivityType;
*/ */
class ActivityController extends AbstractController class ActivityController extends AbstractController
{ {
protected EventDispatcherInterface $eventDispatcher;
/** protected AuthorizationHelper $authorizationHelper;
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/** protected LoggerInterface $logger;
* @var AuthorizationHelper
*/
protected $authorizationHelper;
/**
* @var LoggerInterface
*/
protected $logger;
/** /**
* ActivityController constructor. * ActivityController constructor.
* *
* @param EventDispatcherInterface $eventDispatcher * @param EventDispatcherInterface $eventDispatcher
* @param AuthorizationHelper $authorizationHelper * @param AuthorizationHelper $authorizationHelper
* @param \Psr\Log\LoggerInterface $logger
*/ */
public function __construct( public function __construct(
EventDispatcherInterface $eventDispatcher, EventDispatcherInterface $eventDispatcher,
@ -89,7 +81,7 @@ class ActivityController extends AbstractController
$this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person); $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person);
$reachableScopes = $this->authorizationHelper $reachableScopes = $this->authorizationHelper
->getReachableScopes($this->getUser(), new Role('CHILL_ACTIVITY_SEE'), ->getReachableCircles($this->getUser(), new Role('CHILL_ACTIVITY_SEE'),
$person->getCenter()); $person->getCenter());
$activities = $em->getRepository('ChillActivityBundle:Activity') $activities = $em->getRepository('ChillActivityBundle:Activity')
@ -109,6 +101,35 @@ class ActivityController extends AbstractController
'person' => $person 'person' => $person
)); ));
} }
public function selectTypeAction(int $person_id, Request $request): Response
{
$em = $this->getDoctrine()->getManager();
$person = $em->getRepository('ChillPersonBundle:Person')->find($person_id);
if ($person === NULL) {
throw $this->createNotFoundException('Person not found');
}
$form = $this->createForm(ActivitySelectTypeType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$activityType = $form->get('type')->getData();
if ($activityType instanceof \Chill\ActivityBundle\Entity\ActivityType) {
return $this->redirectToRoute('chill_activity_activity_new', [
'person_id' => $person->getId(),
'activityType_id' => $activityType->getId(),
]);
}
}
return $this->render('ChillActivityBundle:Activity:selectType.html.twig', [
'form' => $form->createView(),
'person' => $person
]);
}
/** /**
* Creates a new Activity entity. * Creates a new Activity entity.
* *
@ -126,7 +147,7 @@ class ActivityController extends AbstractController
$entity = new Activity(); $entity = new Activity();
$entity->setPerson($person); $entity->setPerson($person);
$form = $this->createCreateForm($entity, $person); $form = $this->createCreateForm($entity);
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isValid()) { if ($form->isValid()) {
@ -190,7 +211,7 @@ class ActivityController extends AbstractController
* Displays a form to create a new Activity entity. * Displays a form to create a new Activity entity.
* *
*/ */
public function newAction($person_id) public function newAction($person_id, Request $request)
{ {
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
$person = $em->getRepository('ChillPersonBundle:Person')->find($person_id); $person = $em->getRepository('ChillPersonBundle:Person')->find($person_id);
@ -199,16 +220,27 @@ class ActivityController extends AbstractController
throw $this->createNotFoundException('Person not found'); throw $this->createNotFoundException('Person not found');
} }
$activityType_id = $request->get('activityType_id', 0);
$activityType = $em->getRepository(\Chill\ActivityBundle\Entity\ActivityType::class)
->find($activityType_id);
if (!$activityType instanceof \Chill\ActivityBundle\Entity\ActivityType) {
return $this->redirectToRoute('chill_activity_activity_select_type', [
'person_id' => $person->getId(),
]);
}
$this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person); $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person);
$entity = new Activity(); $entity = new Activity();
$entity->setUser($this->get('security.token_storage')->getToken()->getUser()); $entity->setUser($this->getUser());
$entity->setPerson($person); $entity->setPerson($person);
$entity->setType($activityType);
$entity->setDate(new \DateTime('now')); $entity->setDate(new \DateTime('now'));
$this->denyAccessUnlessGranted('CHILL_ACTIVITY_CREATE', $entity); $this->denyAccessUnlessGranted('CHILL_ACTIVITY_CREATE', $entity);
$form = $this->createCreateForm($entity, $person); $form = $this->createCreateForm($entity);
return $this->render('ChillActivityBundle:Activity:new.html.twig', array( return $this->render('ChillActivityBundle:Activity:new.html.twig', array(
'person' => $person, 'person' => $person,

View File

@ -235,6 +235,7 @@ class ActivityType
*/ */
public function getName(?string $locale = null) public function getName(?string $locale = null)
{ {
// TODO
if ($locale) { if ($locale) {
if (isset($this->name[$locale])) { if (isset($this->name[$locale])) {
return $this->name[$locale]; return $this->name[$locale];

View File

@ -72,22 +72,9 @@ class ActivityTypeCategory
* *
* @return array | string * @return array | string
*/ */
public function getName(?string $locale = null) public function getName(): array
{ {
if ($locale) { return $this->name;
if (isset($this->name[$locale])) {
return $this->name[$locale];
} else {
foreach ($this->name as $name) {
if (!empty($name)) {
return $name;
}
}
}
return '';
} else {
return $this->name;
}
} }
/** /**

View File

@ -0,0 +1,19 @@
<?php
namespace Chill\ActivityBundle\Form;
use Chill\ActivityBundle\Form\Type\TranslatableActivityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class ActivitySelectTypeType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('type', TranslatableActivityType::class, array(
'placeholder' => 'Choose a type',
'active_only' => true,
'mapped' => false,
));
}
}

View File

@ -15,7 +15,6 @@ use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToTimestampTra
use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Chill\ActivityBundle\Form\Type\TranslatableActivityType; use Chill\ActivityBundle\Form\Type\TranslatableActivityType;
use Chill\ActivityBundle\Form\Type\TranslatableActivityReason; use Chill\ActivityBundle\Form\Type\TranslatableActivityReason;
use Chill\MainBundle\Form\Type\UserPickerType; use Chill\MainBundle\Form\Type\UserPickerType;
@ -24,7 +23,6 @@ use Chill\MainBundle\Form\Type\ChillDateType;
class ActivityType extends AbstractType class ActivityType extends AbstractType
{ {
/** /**
* the user running this form * the user running this form
* *
@ -36,7 +34,7 @@ class ActivityType extends AbstractType
* *
* @var AuthorizationHelper * @var AuthorizationHelper
*/ */
protected $authorizationHelper; protected AuthorizationHelper $authorizationHelper;
/** /**
* *
@ -113,10 +111,6 @@ class ActivityType extends AbstractType
'multiple' => true, 'multiple' => true,
'required' => false, 'required' => false,
)) ))
->add('type', TranslatableActivityType::class, array(
'placeholder' => 'Choose a type',
'active_only' => true
))
->add('comment', CommentType::class, [ ->add('comment', CommentType::class, [
'required' => false, 'required' => false,
]) ])

View File

@ -21,7 +21,7 @@
{% block title 'Activity creation' |trans %} {% block title 'Activity creation' |trans %}
{% block personcontent %} {% block personcontent %}
<h2 class="chill-red">{{ "Activity creation"|trans }}</h1> <h2 class="chill-red">{{ "Activity creation"|trans }}</h2>
{{ form_start(form) }} {{ form_start(form) }}
@ -32,14 +32,13 @@
{{ form_row(form.date) }} {{ form_row(form.date) }}
{{ form_row(form.durationTime) }} {{ form_row(form.durationTime) }}
{{ form_row(form.type) }}
{{ form_row(form.attendee) }} {{ form_row(form.attendee) }}
{{ form_row(form.reasons) }} {{ form_row(form.reasons) }}
{{ form_row(form.comment) }} {{ form_row(form.comment) }}
<div class="grid-12 centered sticky-form-buttons"> <div class="grid-12 centered sticky-form-buttons">
<button class="sc-button green margin-10" type="submit"><i class="fa fa-save"></i> {{ 'Add a new activity'|trans }}</button> <button class="sc-button green margin-10" type="submit"><i class="fa fa-save"></i> {{ 'Add a new activity'|trans }}</button>
</div> </div>
{{ form_end(form) }} {{ form_end(form) }}
{% endblock %} {% endblock %}

View File

@ -0,0 +1,19 @@
{% extends "@ChillPerson/layout.html.twig" %}
{% set activeRouteKey = 'chill_activity_activity_new' %}
{% block title 'Activity creation'|trans %}
{% block personcontent %}
<h2 class="chill-red">{{ "Activity creation"|trans }}</h2>
{{ form_start(form) }}
{{ form_row(form.type) }}
<div class="grid-12 centered sticky-form-buttons">
<button class="sc-button green margin-10" type="submit"><i class="fa fa-save"></i> {{ 'Next Step'|trans }}</button>
</div>
{{ form_end(form) }}
{% endblock %}

View File

@ -6,6 +6,10 @@ chill_activity_activity_show:
path: /{_locale}/person/{person_id}/activity/{id}/show path: /{_locale}/person/{person_id}/activity/{id}/show
controller: Chill\ActivityBundle\Controller\ActivityController::showAction controller: Chill\ActivityBundle\Controller\ActivityController::showAction
chill_activity_activity_select_type:
path: /{_locale}/person/{person_id}/activity/select-type
controller: Chill\ActivityBundle\Controller\ActivityController::selectTypeAction
chill_activity_activity_new: chill_activity_activity_new:
path: /{_locale}/person/{person_id}/activity/new path: /{_locale}/person/{person_id}/activity/new
controller: Chill\ActivityBundle\Controller\ActivityController::newAction controller: Chill\ActivityBundle\Controller\ActivityController::newAction