Skip to content

Commit

Permalink
Semaine type : répare la fonction de duplication d'un jour entier (#1043
Browse files Browse the repository at this point in the history
)

* PeriodPosition: add updated_at & updated_by

* Period: improve isEmpty & isFull methods

* Period: fix duplicate feature
  • Loading branch information
raphodn authored Oct 21, 2023
1 parent d983f90 commit 381fb03
Show file tree
Hide file tree
Showing 6 changed files with 255 additions and 139 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Application\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20231021183520 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}

public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE period_position ADD updated_by_id INT DEFAULT NULL, ADD updated_at DATETIME NOT NULL');
$this->addSql('ALTER TABLE period_position ADD CONSTRAINT FK_2367D496896DBBDE FOREIGN KEY (updated_by_id) REFERENCES fos_user (id)');
$this->addSql('CREATE INDEX IDX_2367D496896DBBDE ON period_position (updated_by_id)');
}

public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

$this->addSql('ALTER TABLE period_position DROP FOREIGN KEY FK_2367D496896DBBDE');
$this->addSql('DROP INDEX IDX_2367D496896DBBDE ON period_position');
$this->addSql('ALTER TABLE period_position DROP updated_by_id, DROP updated_at');
}
}
17 changes: 7 additions & 10 deletions app/Resources/views/admin/period/copy_periods.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@
{% endblock %}

{% block content %}
<h4>Dupliquer les créneaux types d'un jour vers un autre</h4>
<h4>Dupliquer les créneaux types d'un jour vers un autre</h4>

{{ form_start(form) }}
{{ form_widget(form) }}
<div>
<button type="submit" class="btn waves-effect waves-light">Copier les créneaux type</button>
</div>
{{ form_end(form) }}
{% endblock %}

{% block javascripts %}
{{ form_start(form) }}
{{ form_widget(form) }}
<div>
<button type="submit" class="btn waves-effect waves-light">Dupliquer les créneaux type</button>
</div>
{{ form_end(form) }}
{% endblock %}
2 changes: 1 addition & 1 deletion app/Resources/views/admin/period/edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
{% endblock %}

{% set periodPositionCount = period.positions | length %}
{% set periodPositionBookedCount = period.positionsBooked | length %}
{% set periodPositionBookedCount = period.positionsWithFilter(true, null) | length %}
{% set periodPositionFreeCount = periodPositionCount - periodPositionBookedCount %}

{% block content %}
Expand Down
52 changes: 32 additions & 20 deletions src/AppBundle/Controller/AdminPeriodController.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,12 @@ public function newPeriodPositionAction(Request $request, Period $period)
public function deletePeriodPositionAction(Request $request, Period $period, PeriodPosition $position)
{
$session = new Session();
$em = $this->getDoctrine()->getManager();

$form = $this->createPeriodPositionDeleteForm($period, $position);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove($position);
$em->flush();

Expand All @@ -276,6 +276,7 @@ public function bookPeriodPositionAction(Request $request, Period $period, Perio
{
$session = new Session();
$em = $this->getDoctrine()->getManager();
$current_user = $this->get('security.token_storage')->getToken()->getUser();

$form = $this->createPeriodPositionBookForm($period, $position);
$form->handleRequest($request);
Expand All @@ -288,19 +289,17 @@ public function bookPeriodPositionAction(Request $request, Period $period, Perio

$beneficiary = $form->get("shifter")->getData();
if ($position->getFormation() && !$beneficiary->getFormations()->contains($position->getFormation())) {
$session
->getFlashBag()
->add("error", "Désolé, ce bénévole n'a pas la qualification nécessaire (" . $position->getFormation()->getName() . ")");
$session->getFlashBag()->add("error", "Désolé, ce bénévole n'a pas la qualification nécessaire (" . $position->getFormation()->getName() . ")");
return new Response($this->generateUrl('admin_period_edit',array('id'=>$period->getId())), 205);
}

if (!$position->getBooker()) {
$current_user = $this->get('security.token_storage')->getToken()->getUser();
$position->setBooker($current_user);
$position->setBookedTime(new \DateTime('now'));
}

$position->setShifter($beneficiary);
$position->setUpdatedBy($current_user);

$em->persist($position);
$em->flush();

Expand All @@ -320,6 +319,7 @@ public function freePeriodPositionAction(Request $request, Period $period, Perio
{
$session = new Session();
$em = $this->getDoctrine()->getManager();
$current_user = $this->get('security.token_storage')->getToken()->getUser();

$form = $this->createPeriodPositionFreeForm($period, $position);
$form->handleRequest($request);
Expand All @@ -331,8 +331,8 @@ public function freePeriodPositionAction(Request $request, Period $period, Perio

// free position
$position->free();
$position->setUpdatedBy($current_user);

$em = $this->getDoctrine()->getManager();
$em->persist($position);
$em->flush();

Expand Down Expand Up @@ -372,15 +372,22 @@ public function deletePeriodAction(Request $request, Period $period)
/**
* Duplicate a period
*
* @Route("/copyPeriod/", name="admin_period_copy", methods={"GET","POST"})
* @Route("/copy", name="admin_period_copy", methods={"GET","POST"})
* @Security("has_role('ROLE_ADMIN')")
*/
public function copyPeriodAction(Request $request)
{
$session = new Session();
$em = $this->getDoctrine()->getManager();
$current_user = $this->get('security.token_storage')->getToken()->getUser();

$now = new \DateTime('now');

$form = $this->createFormBuilder()
->setAction($this->generateUrl('admin_period_copy'))
->add('day_of_week_from', ChoiceType::class, array('label' => 'Jour de la semaine référence', 'choices' => Period::DAYS_OF_WEEK_LIST_WITH_INT))
->add('day_of_week_to', ChoiceType::class, array('label' => 'Jour de la semaine destination', 'choices' => Period::DAYS_OF_WEEK_LIST_WITH_INT))
// TODO: if use_fly_and_fixed, give option to chose if shifter/booker is copied as well
->setAction($this->generateUrl('admin_period_copy'))
->getForm();

$form->handleRequest($request);
Expand All @@ -389,24 +396,30 @@ public function copyPeriodAction(Request $request)
$from = $form->get('day_of_week_from')->getData();
$to = $form->get('day_of_week_to')->getData();

$em = $this->getDoctrine()->getManager();
$periods = $em->getRepository('AppBundle:Period')->findBy(array('dayOfWeek'=>$from));

$count = 0;
foreach ($periods as $period){
foreach ($periods as $period) {
$p = clone $period;
$p->setDayOfWeek($to);
foreach ($period->getPositions() as $position){
$p->addPosition($position);
$p->setCreatedAt($now);
$p->setCreatedBy($current_user);
$p->setUpdatedAtValue();
$p->setUpdatedBy(null);
foreach ($period->getPositions() as $position) {
$pp = clone $position;
$pp->setCreatedAt($now);
$pp->setCreatedBy($current_user);
$pp->setUpdatedAtValue();
$pp->setUpdatedBy(null);
$p->addPosition($pp);
}
$em->persist($p);
$count++;
}
$em->flush();

$session = new Session();
$session->getFlashBag()->add('success', $count . ' creneaux copiés de' . array_search($from, Period::DAYS_OF_WEEK_LIST_WITH_INT) . ' à ' . array_search($to, Period::DAYS_OF_WEEK_LIST_WITH_INT));

$session->getFlashBag()->add('success', $count . ' créneaux copiés de ' . array_search($from, Period::DAYS_OF_WEEK_LIST_WITH_INT) . ' à ' . array_search($to, Period::DAYS_OF_WEEK_LIST_WITH_INT));
return $this->redirectToRoute('admin_period_index');
}

Expand All @@ -423,16 +436,17 @@ public function copyPeriodAction(Request $request)
*/
public function generateShiftsForDateAction(Request $request, KernelInterface $kernel)
{
$session = new Session();

$form = $this->createFormBuilder()
->setAction($this->generateUrl('admin_shifts_generation'))
->add('date_from',TextType::class,array('label'=>'du*','attr'=>array('class'=>'datepicker')))
->add('date_to',TextType::class,array('label'=>'au' ,'attr'=>array('class'=>'datepicker','require' => false)))
->add('date_to',TextType::class,array('label'=>'au', 'attr'=>array('class'=>'datepicker','require' => false)))
->getForm();

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {

$date_from = $form->get('date_from')->getData();
$date_to = $form->get('date_to')->getData();

Expand All @@ -449,9 +463,7 @@ public function generateShiftsForDateAction(Request $request, KernelInterface $k
$application->run($input, $output);
$content = $output->fetch();

$session = new Session();
$session->getFlashBag()->add('success',$content);

return $this->redirectToRoute('admin_period_index');
}

Expand Down
Loading

0 comments on commit 381fb03

Please sign in to comment.