Skip to content

Commit

Permalink
UniqueValueFinder trait added
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfyWin committed Sep 9, 2024
1 parent 2d26563 commit 67fcc7c
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 116 deletions.
4 changes: 3 additions & 1 deletion src/main/core/Entity/Planning/PlannedObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
* @ORM\Entity(repositoryClass="Claroline\CoreBundle\Repository\Planning\PlannedObjectRepository")
*
* @ORM\Table(name="claro_planned_object")
*/
class PlannedObject
Expand Down Expand Up @@ -74,6 +75,7 @@ class PlannedObject

/**
* @ORM\ManyToOne(targetEntity="Claroline\CoreBundle\Entity\Location")
*
* @ORM\JoinColumn(name="location_id", nullable=true, onDelete="SET NULL")
*/
private ?Location $location = null;
Expand Down
20 changes: 20 additions & 0 deletions src/main/core/Repository/Planning/PlannedObjectRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Claroline Connect package.
*
* (c) Claroline Consortium <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Claroline\CoreBundle\Repository\Planning;

use Claroline\AppBundle\Repository\UniqueValueFinder;
use Doctrine\ORM\EntityRepository;

class PlannedObjectRepository extends EntityRepository
{
use UniqueValueFinder;
}
3 changes: 3 additions & 0 deletions src/main/core/Repository/Planning/PlanningRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@

namespace Claroline\CoreBundle\Repository\Planning;

use Claroline\AppBundle\Repository\UniqueValueFinder;
use Claroline\CoreBundle\Library\Normalizer\DateNormalizer;
use Doctrine\ORM\EntityRepository;

class PlanningRepository extends EntityRepository
{
use UniqueValueFinder;

public function areDatesAvailable(string $objectId, \DateTimeInterface $start, \DateTimeInterface $end): bool
{
$count = (int) $this->getEntityManager()
Expand Down
19 changes: 0 additions & 19 deletions src/plugin/cursus/Manager/CourseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class CourseManager
private SessionManager $sessionManager;

private $courseUserRepo;
private $courseRepo;

public function __construct(
TranslatorInterface $translator,
Expand All @@ -60,7 +59,6 @@ public function __construct(
$this->sessionManager = $sessionManager;

$this->courseUserRepo = $this->om->getRepository(CourseUser::class);
$this->courseRepo = $this->om->getRepository(Course::class);
}

public function generateFromTemplate(Course $course, string $locale): string
Expand Down Expand Up @@ -193,21 +191,4 @@ public function moveToPending(Course $course, array $sessionUsers): void
}
}
}

public function getCopyName(string $name): string
{
$existingNames = $this->courseRepo->findNamesWithPrefix($name);

if (empty($existingNames)) {
return $name;
}

$index = count($existingNames);
do {
++$index;
$newName = $name.$index;
} while (in_array($newName, $existingNames));

return $newName;
}
}
20 changes: 0 additions & 20 deletions src/plugin/cursus/Manager/EventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use Claroline\CursusBundle\Entity\Registration\EventGroup;
use Claroline\CursusBundle\Entity\Registration\EventUser;
use Claroline\CursusBundle\Entity\Session;
use Claroline\CursusBundle\Repository\EventRepository;
use Claroline\CursusBundle\Repository\Registration\EventGroupRepository;
use Claroline\CursusBundle\Repository\Registration\EventUserRepository;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Expand All @@ -37,7 +36,6 @@ class EventManager
{
private EventUserRepository $eventUserRepo;
private EventGroupRepository $eventGroupRepo;
private EventRepository $eventRepo;

public function __construct(
private readonly EventDispatcherInterface $eventDispatcher,
Expand All @@ -49,7 +47,6 @@ public function __construct(
) {
$this->eventUserRepo = $om->getRepository(EventUser::class);
$this->eventGroupRepo = $om->getRepository(EventGroup::class);
$this->eventRepo = $om->getRepository(Event::class);
}

public function getBySessionAndUser(Session $session, User $user): ?array
Expand Down Expand Up @@ -399,21 +396,4 @@ private function getTemplatePlaceholders(Event $event): array
$this->templateManager->formatDatePlaceholder('event_end', $event->getEndDate()),
);
}

public function getCopyName(string $name): string
{
$existingNames = $this->eventRepo->findNamesWithPrefix($name);

if (empty($existingNames)) {
return $name;
}

$index = count($existingNames);
do {
++$index;
$newName = $name.$index;
} while (in_array($newName, $existingNames));

return $newName;
}
}
17 changes: 0 additions & 17 deletions src/plugin/cursus/Manager/SessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,21 +504,4 @@ public function unregisterGroup(SessionGroup $sessionGroup): void
$this->sessionEventManager->removeGroups($eventRegistration->getEvent(), [$eventRegistration]);
}
}

public function getCopyName(string $name): string
{
$existingNames = $this->sessionRepo->findNamesWithPrefix($name);

if (empty($existingNames)) {
return $name;
}

$index = count($existingNames);
do {
++$index;
$newName = $name.$index;
} while (in_array($newName, $existingNames));

return $newName;
}
}
19 changes: 3 additions & 16 deletions src/plugin/cursus/Repository/CourseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Claroline\CursusBundle\Repository;

use Claroline\AppBundle\Repository\UniqueValueFinder;
use Claroline\CoreBundle\Entity\Facet\FieldFacet;
use Claroline\CoreBundle\Entity\User;
use Claroline\CoreBundle\Entity\Workspace\Workspace;
Expand All @@ -21,6 +22,8 @@

class CourseRepository extends EntityRepository
{
use UniqueValueFinder;

public function search(string $search, int $nbResults)
{
return $this->createQueryBuilder('c')
Expand Down Expand Up @@ -248,20 +251,4 @@ private function countUsers(Course $course, string $type, Session $session = nul

return (int) $query->getSingleScalarResult();
}

public function findNamesWithPrefix(string $prefix): array
{
return array_map(
function (array $course) {
return $course['name'];
},
$this->getEntityManager()->createQuery('
SELECT c.name
FROM Claroline\CursusBundle\Entity\Course c
WHERE c.name LIKE :search
')
->setParameter('search', addcslashes($prefix, '%_').'%')
->getResult()
);
}
}
20 changes: 3 additions & 17 deletions src/plugin/cursus/Repository/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@

namespace Claroline\CursusBundle\Repository;

use Claroline\AppBundle\Repository\UniqueValueFinder;
use Claroline\CoreBundle\Entity\User;
use Claroline\CursusBundle\Entity\Event;
use Claroline\CursusBundle\Entity\Registration\AbstractRegistration;
use Doctrine\ORM\EntityRepository;

class EventRepository extends EntityRepository
{
use UniqueValueFinder;

public function countParticipants(Event $event): array
{
return [
Expand Down Expand Up @@ -71,21 +74,4 @@ private function countUsers(Event $event, string $type): int
])
->getSingleScalarResult();
}

public function findNamesWithPrefix(string $prefix): array
{
return array_map(
function (array $event) {
return $event['name'];
},
$this->getEntityManager()->createQuery('
SELECT po.name
FROM Claroline\CursusBundle\Entity\Event e
JOIN Claroline\CoreBundle\Entity\Planning\PlannedObject po WITH e.plannedObject = po.id
WHERE po.name LIKE :search
')
->setParameter('search', addcslashes($prefix, '%_').'%')
->getResult()
);
}
}
19 changes: 3 additions & 16 deletions src/plugin/cursus/Repository/SessionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Claroline\CursusBundle\Repository;

use Claroline\AppBundle\Repository\UniqueValueFinder;
use Claroline\CoreBundle\Entity\User;
use Claroline\CoreBundle\Entity\Workspace\Workspace;
use Claroline\CursusBundle\Entity\Course;
Expand All @@ -20,6 +21,8 @@

class SessionRepository extends EntityRepository
{
use UniqueValueFinder;

public function findByWorkspace(Workspace $workspace)
{
return $this->getEntityManager()
Expand Down Expand Up @@ -130,20 +133,4 @@ private function countUsers(Session $session, string $type): int
])
->getSingleScalarResult();
}

public function findNamesWithPrefix(string $prefix): array
{
return array_map(
function (array $session) {
return $session['name'];
},
$this->getEntityManager()->createQuery('
SELECT s.name
FROM Claroline\CursusBundle\Entity\Session s
WHERE s.name LIKE :search
')
->setParameter('search', addcslashes($prefix, '%_').'%')
->getResult()
);
}
}
2 changes: 1 addition & 1 deletion src/plugin/cursus/Resources/config/services/subscriber.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ services:
- '@Claroline\AppBundle\Persistence\ObjectManager'
- '@Claroline\CoreBundle\Manager\FileManager'
- '@Claroline\AppBundle\API\Crud'
- '@Claroline\CursusBundle\Manager\CourseManager'
tags:
- { name: kernel.event_subscriber }

Expand All @@ -35,6 +34,7 @@ services:
Claroline\CursusBundle\Subscriber\Crud\SessionSubscriber:
arguments:
- '@security.token_storage'
- '@Claroline\AppBundle\Persistence\ObjectManager'
- '@Claroline\CoreBundle\Manager\FileManager'
- '@Claroline\CursusBundle\Manager\SessionManager'
- '@Claroline\AppBundle\API\Crud'
Expand Down
9 changes: 4 additions & 5 deletions src/plugin/cursus/Subscriber/Crud/CourseSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use Claroline\CoreBundle\Entity\User;
use Claroline\CoreBundle\Manager\FileManager;
use Claroline\CursusBundle\Entity\Course;
use Claroline\CursusBundle\Manager\CourseManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

Expand All @@ -33,7 +32,6 @@ public function __construct(
private readonly ObjectManager $om,
private readonly FileManager $fileManager,
private readonly Crud $crud,
private readonly CourseManager $manager,
) {
}

Expand Down Expand Up @@ -149,10 +147,11 @@ public function preCopy(CopyEvent $event): void

$copy->setCreatedAt(new \DateTime());
$copy->setUpdatedAt(new \DateTime());
$copyName = $this->manager->getCopyName($original->getName());
$copy->setSlug($copyName);
$copyName = $this->om->getRepository(Course::class)->findNextUnique('name', $original->getName());
$copyCode = $this->om->getRepository(Course::class)->findNextUnique('code', $original->getCode());
$copy->setName($copyName);
$copy->setCode($copyName);
$copy->setCode($copyCode);
$copy->setSlug($copyName);
}

public function postCopy(CopyEvent $event): void
Expand Down
8 changes: 6 additions & 2 deletions src/plugin/cursus/Subscriber/Crud/EventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Claroline\AppBundle\Event\Crud\CopyEvent;
use Claroline\AppBundle\Event\Crud\CreateEvent;
use Claroline\CoreBundle\Entity\Planning\PlannedObject;
use Claroline\CoreBundle\Subscriber\Crud\Planning\AbstractPlannedSubscriber;
use Claroline\CursusBundle\Entity\Event;
use Claroline\CursusBundle\Entity\Registration\AbstractRegistration;
Expand Down Expand Up @@ -116,8 +117,11 @@ public function preCopy(CopyEvent $event): void

$copy->setUuid(BaseUuid::uuid4()->toString());

$copyName = $this->manager->getCopyName($original->getName());
$copy->setCode($copyName);
$plannedObjectRepo = $this->om->getRepository(PlannedObject::class);
$copyName = $plannedObjectRepo->findNextUnique('name', $original->getPlannedObject()->getName());
$copyCode = $this->om->getRepository(Event::class)->findNextUnique('code', $original->getCode());

$copy->setCode($copyCode);
$copy->setSession($session);
$copy->getPlannedObject()->setName($copyName);
}
Expand Down
7 changes: 5 additions & 2 deletions src/plugin/cursus/Subscriber/Crud/SessionSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Claroline\AppBundle\Event\Crud\DeleteEvent;
use Claroline\AppBundle\Event\Crud\UpdateEvent;
use Claroline\AppBundle\Event\CrudEvents;
use Claroline\AppBundle\Persistence\ObjectManager;
use Claroline\CoreBundle\Entity\User;
use Claroline\CoreBundle\Manager\FileManager;
use Claroline\CursusBundle\Entity\Course;
Expand All @@ -29,6 +30,7 @@ class SessionSubscriber implements EventSubscriberInterface
{
public function __construct(
private readonly TokenStorageInterface $tokenStorage,
private readonly ObjectManager $om,
private readonly FileManager $fileManager,
private readonly SessionManager $sessionManager,
private readonly Crud $crud,
Expand Down Expand Up @@ -178,9 +180,10 @@ public function preCopy(CopyEvent $event): void
$copy->setCreatedAt(new \DateTime());
$copy->setUpdatedAt(new \DateTime());

$copyName = $this->sessionManager->getCopyName($copy->getName());
$copyName = $this->om->getRepository(Session::class)->findNextUnique('name', $copy->getName());
$copyCode = $this->om->getRepository(Session::class)->findNextUnique('code', $copy->getCode());
$copy->setName($copyName);
$copy->setCode($copyName);
$copy->setCode($copyCode);
}

public function postCopy(CopyEvent $event): void
Expand Down

0 comments on commit 67fcc7c

Please sign in to comment.