diff --git a/src/plugin/cursus/Manager/CourseManager.php b/src/plugin/cursus/Manager/CourseManager.php index d2efc5ae6e..d1f7ca1976 100644 --- a/src/plugin/cursus/Manager/CourseManager.php +++ b/src/plugin/cursus/Manager/CourseManager.php @@ -205,7 +205,7 @@ public function getCopyName(string $name): string $index = count($existingNames); do { ++$index; - $newName = $name.'_'.$index; + $newName = $name.$index; } while (in_array($newName, $existingNames)); return $newName; diff --git a/src/plugin/cursus/Manager/EventManager.php b/src/plugin/cursus/Manager/EventManager.php index 04d410d7c3..922d1b9c9f 100644 --- a/src/plugin/cursus/Manager/EventManager.php +++ b/src/plugin/cursus/Manager/EventManager.php @@ -413,7 +413,7 @@ private function getTemplatePlaceholders(Event $event): array public function getCopyName(string $name): string { - $existingNames = $this->eventRepo->findCodesWithPrefix($name); + $existingNames = $this->eventRepo->findNamesWithPrefix($name); if (empty($existingNames)) { return $name; @@ -422,7 +422,7 @@ public function getCopyName(string $name): string $index = count($existingNames); do { ++$index; - $newName = $name.'_'.$index; + $newName = $name.$index; } while (in_array($newName, $existingNames)); return $newName; diff --git a/src/plugin/cursus/Manager/SessionManager.php b/src/plugin/cursus/Manager/SessionManager.php index 18ac8d07f3..90718a14b1 100644 --- a/src/plugin/cursus/Manager/SessionManager.php +++ b/src/plugin/cursus/Manager/SessionManager.php @@ -516,7 +516,7 @@ public function getCopyName(string $name): string $index = count($existingNames); do { ++$index; - $newName = $name.'_'.$index; + $newName = $name.$index; } while (in_array($newName, $existingNames)); return $newName; diff --git a/src/plugin/cursus/Repository/EventRepository.php b/src/plugin/cursus/Repository/EventRepository.php index 7e452f60a0..75e5af5a4f 100644 --- a/src/plugin/cursus/Repository/EventRepository.php +++ b/src/plugin/cursus/Repository/EventRepository.php @@ -70,16 +70,17 @@ private function countUsers(Event $event, string $type) ->getSingleScalarResult(); } - public function findCodesWithPrefix(string $prefix): array + public function findNamesWithPrefix(string $prefix): array { return array_map( function (array $event) { - return $event['code']; + return $event['name']; }, $this->getEntityManager()->createQuery(' - SELECT e.code + SELECT po.name FROM Claroline\CursusBundle\Entity\Event e - WHERE e.code LIKE :search + JOIN Claroline\CoreBundle\Entity\Planning\PlannedObject po WITH e.plannedObject = po.id + WHERE po.name LIKE :search ') ->setParameter('search', addcslashes($prefix, '%_').'%') ->getResult() diff --git a/src/plugin/cursus/Serializer/CourseSerializer.php b/src/plugin/cursus/Serializer/CourseSerializer.php index fda906aad3..59419852c0 100644 --- a/src/plugin/cursus/Serializer/CourseSerializer.php +++ b/src/plugin/cursus/Serializer/CourseSerializer.php @@ -181,7 +181,12 @@ public function serialize(Course $course, array $options = []): array public function deserialize(array $data, Course $course, array $options): Course { - $this->sipe('id', 'setUuid', $data, $course); + if (!in_array(SerializerInterface::REFRESH_UUID, $options)) { + $this->sipe('id', 'setUuid', $data, $course); + } else { + $course->refreshUuid(); + } + $this->sipe('code', 'setCode', $data, $course); $this->sipe('name', 'setName', $data, $course); $this->sipe('description', 'setDescription', $data, $course); diff --git a/src/plugin/cursus/Serializer/EventSerializer.php b/src/plugin/cursus/Serializer/EventSerializer.php index 08afb000af..a57d610256 100644 --- a/src/plugin/cursus/Serializer/EventSerializer.php +++ b/src/plugin/cursus/Serializer/EventSerializer.php @@ -106,11 +106,16 @@ public function serialize(Event $event, array $options = []): array return $serialized; } - public function deserialize(array $data, Event $event): Event + public function deserialize(array $data, Event $event, array $options = []): Event { $this->plannedObjectSerializer->deserialize($data, $event->getPlannedObject()); - $this->sipe('id', 'setUuid', $data, $event); + if (!in_array(SerializerInterface::REFRESH_UUID, $options)) { + $this->sipe('id', 'setUuid', $data, $event); + } else { + $event->refreshUuid(); + } + $this->sipe('code', 'setCode', $data, $event); $this->sipe('restrictions.users', 'setMaxUsers', $data, $event); $this->sipe('registration.registrationType', 'setRegistrationType', $data, $event); diff --git a/src/plugin/cursus/Serializer/SessionSerializer.php b/src/plugin/cursus/Serializer/SessionSerializer.php index 1a32199d0c..3ea848f845 100644 --- a/src/plugin/cursus/Serializer/SessionSerializer.php +++ b/src/plugin/cursus/Serializer/SessionSerializer.php @@ -176,9 +176,14 @@ public function serialize(Session $session, array $options = []): array ]; } - public function deserialize(array $data, Session $session): Session + public function deserialize(array $data, Session $session, array $options): Session { - $this->sipe('id', 'setUuid', $data, $session); + if (!in_array(SerializerInterface::REFRESH_UUID, $options)) { + $this->sipe('id', 'setUuid', $data, $session); + } else { + $session->refreshUuid(); + } + $this->sipe('code', 'setCode', $data, $session); $this->sipe('name', 'setName', $data, $session); $this->sipe('description', 'setDescription', $data, $session); diff --git a/src/plugin/cursus/Subscriber/Crud/CourseSubscriber.php b/src/plugin/cursus/Subscriber/Crud/CourseSubscriber.php index 0178bf5ea0..7eb1a2dae9 100644 --- a/src/plugin/cursus/Subscriber/Crud/CourseSubscriber.php +++ b/src/plugin/cursus/Subscriber/Crud/CourseSubscriber.php @@ -147,17 +147,12 @@ public function preCopy(CopyEvent $event): void /** @var Course $copy */ $copy = $event->getCopy(); - $copy->refreshUuid(); $copy->setCreatedAt(new \DateTime()); $copy->setUpdatedAt(new \DateTime()); $copyName = $this->manager->getCopyName($original->getName()); $copy->setSlug($copyName); $copy->setName($copyName); $copy->setCode($copyName); - - foreach ($original->getSessions() as $session) { - $this->crud->copy($session, [], ['parent' => $copy]); - } } public function postCopy(CopyEvent $event): void @@ -165,6 +160,13 @@ public function postCopy(CopyEvent $event): void /** @var Course $course */ $course = $event->getCopy(); + /** @var Course $original */ + $original = $event->getObject(); + + foreach ($original->getSessions() as $session) { + $this->crud->copy($session, [], ['parent' => $course]); + } + if ($course->getPoster()) { $this->fileManager->linkFile(Course::class, $course->getUuid(), $course->getPoster()); } diff --git a/src/plugin/cursus/Subscriber/Crud/SessionSubscriber.php b/src/plugin/cursus/Subscriber/Crud/SessionSubscriber.php index bfc12a5f6e..9034275653 100644 --- a/src/plugin/cursus/Subscriber/Crud/SessionSubscriber.php +++ b/src/plugin/cursus/Subscriber/Crud/SessionSubscriber.php @@ -44,6 +44,7 @@ public static function getSubscribedEvents(): array CrudEvents::getEventName(CrudEvents::POST_UPDATE, Session::class) => 'postUpdate', CrudEvents::getEventName(CrudEvents::POST_DELETE, Session::class) => 'postDelete', CrudEvents::getEventName(CrudEvents::PRE_COPY, Session::class) => 'preCopy', + CrudEvents::getEventName(CrudEvents::POST_COPY, Session::class) => 'postCopy', ]; } @@ -170,22 +171,37 @@ public function preCopy(CopyEvent $event): void /** @var Course $course */ $course = $event->getExtra()['parent']; - /** @var Session $original */ - $original = $event->getObject(); - /** @var Session $copy */ $copy = $event->getCopy(); - $copy->refreshUuid(); $copy->setCourse($course); $copy->setCreatedAt(new \DateTime()); $copy->setUpdatedAt(new \DateTime()); - $copyName = $this->sessionManager->getCopyName($original->getName()); + + $copyName = $this->sessionManager->getCopyName($copy->getName()); $copy->setName($copyName); $copy->setCode($copyName); + } + + public function postCopy(CopyEvent $event): void + { + /** @var Session $original */ + $original = $event->getObject(); + + /** @var Session $copy */ + $copy = $event->getCopy(); foreach ($original->getEvents() as $seance) { $this->crud->copy($seance, [], ['parent' => $copy]); } + + if ($copy->getPoster()) { + $this->fileManager->linkFile(Session::class, $copy->getUuid(), $copy->getPoster()); + } + + if ($copy->getThumbnail()) { + $this->fileManager->linkFile(Session::class, $copy->getUuid(), $copy->getThumbnail()); + } + } }