From e91a89ef36ebd69f19429bab4d60268d8f9b0e6c Mon Sep 17 00:00:00 2001 From: pfleu <pierre.fleutot@audemarspiguet.com> Date: Wed, 16 Apr 2025 18:38:48 +0200 Subject: [PATCH] =?UTF-8?q?Api=20:=20Entry=20:=20create=20:=20Fix=20cr?= =?UTF-8?q?=C3=A9ation=20pour=20prendre=20en=20compte=20le=20cas=20o=C3=B9?= =?UTF-8?q?=20on=20aurait=20une=20entr=C3=A9e=20existante=20et=20pas=20de?= =?UTF-8?q?=20donn=C3=A9es=20wiktionnary?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/assets/js/app.js | 2 +- src/Controller/ApiEntryController.php | 58 ++++++++++++++++++--------- src/Controller/AppBaseController.php | 16 +++++++- src/Controller/HeadwordController.php | 2 +- src/Controller/LabelController.php | 2 +- src/Controller/LexiconController.php | 2 +- 6 files changed, 57 insertions(+), 25 deletions(-) diff --git a/public/assets/js/app.js b/public/assets/js/app.js index 7f8e93d..548f966 100755 --- a/public/assets/js/app.js +++ b/public/assets/js/app.js @@ -425,7 +425,7 @@ function initializeAjaxLinks() { }) } -// Barre de recherche : on cherche le mot en ajax. Si on le trouve dans le wiko, on l'importe et on redirige vers lexique zéro, sinon on ouvre la modale de confirmation d'ajout +// Barre de recherche : on cherche le mot en ajax. Si on le trouve dans Balex on redirige, sinon on le cherche dans le wiko, on l'importe et on redirige vers lexique zéro, sinon on ouvre la modale de confirmation d'ajout function initializeSearchHeadwords() { $form = $('#searchHeadwordsForm'); $form.on('submit', function (e) { diff --git a/src/Controller/ApiEntryController.php b/src/Controller/ApiEntryController.php index 0412855..dd0024e 100644 --- a/src/Controller/ApiEntryController.php +++ b/src/Controller/ApiEntryController.php @@ -120,7 +120,7 @@ class ApiEntryController extends AppBaseController * @OA\Tag(name="Entries") * @Security(name="OAuth2") */ - public function createEntry(Request $request, SerializerInterface $serializer, WiktionaryManager $wiktionaryManager): Response + public function createEntry(Request $request, SerializerInterface $serializer, WiktionaryManager $wiktionaryManager, EntryManager $entryManager): Response { // But: Créer une entrée à partir d'une graphie, intégrer l'entrée dans les lexiques demandés. Un mot-vedette correspondant à la graphie doit exister. $data = json_decode($request->getContent(), true); @@ -139,34 +139,54 @@ class ApiEntryController extends AppBaseController return $this->createJsonResponse(401, ['error' => sprintf("Les lexiques cibles doivent tous appartenir à la même langue")]); } - $forceCreation = isset($data['force']) && $data['force'] === true; - $headword = $this->getHeadwordForWord($data['graphy'], $language); - - // On récupère ou on crée le mot-vedette - if (!$headword) { + // TODO faut-il chercher par graphie ou par headword? Par ex. si $data['graphy'] = "souris" + // TODO Si l'entrée liée au headword "sourire" existe déjà , liée à la graphie "souris", si on cherche par headword on ne va pas trouver l'entrée donc on pourra créer + // TODO Si on cherche par graphie, on va la trouver on ne pourra pas créer, on va dupliquer l'existante + // Plus logique de chercher par headword, car on crée un headword à partir de la graphie donnée en paramètre !!! + $zeroEntry = $this->doctrine->getRepository(Entry::class) + ->search([ + 'headword' => $data['graphy'], + 'language' => $language, + 'lexicons' => [$this->getZeroLexicon($language)], + ]); + + if ($zeroEntry) { + $headword = $zeroEntry->getHeadword(); +// $this->warning[] = sprintf("Cette entrée existe déjà dans le lexique %s.", $zeroEntry->getLexicon()->getName()); + } else { $wiktionaryData = $wiktionaryManager->search($data['graphy'], $language); - if ($forceCreation || $wiktionaryData) { - $headword = $this->newHeadword($data['graphy'], $language); - } else { + + // Si on n'a pas forcé la création et qu'on a pas de données du wiktionnaire, on ne crée pas l'entrée + $forceCreation = isset($data['force']) && $data['force'] === true; + if (!$forceCreation && !$wiktionaryData) { return $this->createJsonResponse(401, ['warning' => sprintf("Le mot «%s» n'existe ni dans Balex ni dans le wiktionnaire.", $data['graphy'])]); } + + // On récupère ou on crée le mot-vedette + $headword = $this->getHeadwordForWord($data['graphy'], $language) ? : $this->newHeadword($data['graphy'], $language); + + // Si le mot n'existe pas dans le wiktionnaire, on le crée uniquement dans le lexique des nouveaux mots + if (!$wiktionaryData) { + $this->createEntryInNewWordsLexicon($headword); + return $this->createJsonResponse(401, ['warning' => sprintf("Entrée créée uniquement dans le lexique des nouveaux mots, car le mot «%s» n'existe ni dans Balex ni dans le wiktionnaire.", $data['graphy'])]); + } } - // Si le mot n'existe pas dans le wiktionnaire, on le crée uniquement dans le lexique des nouveaux mots - if (!$wiktionaryData) { - $this->createEntryInNewWordsLexicon($headword); - $this->success[] = sprintf("Entrée créée dans le lexique des nouveaux mots."); - } else { - // Sinon on crée l'entrée dans chaque lexique si elle n'y existe pas (et dans le lexique Zéro si possible) - foreach ($lexicons as $lexicon) { - if (!$this->doctrine->getRepository(Entry::class)->findBy(['lexicon' => $lexicon, 'headword' => $headword])) { + // On crée ou on copie l'entrée dans chaque lexique si elle n'y existe pas (et dans le lexique Zéro si possible) + foreach ($lexicons as $lexicon) { + if (!$this->doctrine->getRepository(Entry::class)->findBy(['lexicon' => $lexicon, 'headword' => $headword])) { + if ($zeroEntry) { + $entryManager->copyAndMergeEntryInLexicon($zeroEntry, $lexicon); + $this->success[] = sprintf("Entrée copiée dans le lexique : %s.", $lexicon); + } else { $entry = $this->getOrCreateEntryInLexicon($headword, $lexicon); $this->success[] = sprintf("Entrée créée dans le lexique : %s.", $lexicon); - } else { - $this->warning[] = sprintf("Cette entrée est déjà présente dans le lexique %s.", $lexicon); } + } else { + $this->warning[] = sprintf("Cette entrée est déjà présente dans le lexique %s.", $lexicon); } } + $this->doctrine->getManager()->flush(); return $this->createJsonResponse(200); diff --git a/src/Controller/AppBaseController.php b/src/Controller/AppBaseController.php index 6265c32..838b529 100644 --- a/src/Controller/AppBaseController.php +++ b/src/Controller/AppBaseController.php @@ -449,12 +449,24 @@ class AppBaseController extends AbstractController * On initialise avec des infos minimales * * @param Headword $headword - * @return Entry + * @return bool * @throws \Exception */ public function createEntryInNewWordsLexicon(Headword $headword) { $newWordsLexicon = $this->em->getRepository(Lexicon::class)->findOneBy(['language' => $headword->getLanguage(), 'category' => Lexicon::TYPE_NEW_WORDS]); + if (!$newWordsLexicon) { + throw new \Exception(sprintf("Pas de lexique de nouveaux mots trouvé pour la langue %s.", $headword->getLanguage())); + } + + $existingEntry = $this->doctrine->getRepository(Entry::class)->findOneBy([ + 'lexicon' => $newWordsLexicon, + 'headword' => $headword, + ]); + if ($existingEntry) { + return false; + } + $entry = new Entry(); $entry->setCreatedBy($this->getUser()); $this->em->persist($entry); @@ -469,7 +481,7 @@ class AppBaseController extends AbstractController $updateRequest->setNewWordEntry($entry); $this->em->persist($updateRequest); - return $entry; + return true; } /** diff --git a/src/Controller/HeadwordController.php b/src/Controller/HeadwordController.php index 1e7f43c..fa5ef49 100644 --- a/src/Controller/HeadwordController.php +++ b/src/Controller/HeadwordController.php @@ -41,7 +41,7 @@ class HeadwordController extends AppBaseController /** * @Route("/search", name="app_headword_search") * - * Barre de recherche : on cherche le mot en ajax. Si on le trouve dans le wiko, on l'importe et on redirige vers lexique zéro, sinon on ouvre la modale de confirmation d'ajout + * Barre de recherche : on cherche le mot en ajax. Si on le trouve dans Balex on redirige, sinon on le cherche dans le wiko, on l'importe et on redirige vers lexique zéro, sinon on ouvre la modale de confirmation d'ajout */ public function search(Request $request, WiktionaryManager $wiktionaryManager): Response { diff --git a/src/Controller/LabelController.php b/src/Controller/LabelController.php index 812cef0..d26e942 100644 --- a/src/Controller/LabelController.php +++ b/src/Controller/LabelController.php @@ -555,7 +555,7 @@ class LabelController extends AppBaseController * @Route("/{id}/add-word-from-searchbar", name="app_label_add_word_from_search_bar") * @IsGranted("LABEL_EDIT", subject="label") * - * Barre de recherche : on cherche le mot en ajax. Si on le trouve dans le wiko, on l'importe et on recharge la page, sinon on ouvre la modale de confirmation d'ajout + * Barre de recherche : on cherche le mot en ajax. Si on le trouve dans Balex on redirige, sinon on le cherche dans le wiko, on l'importe et on recharge la page, sinon on ouvre la modale de confirmation d'ajout */ public function addWordFromSearchBar(Request $request, Label $label, WiktionaryManager $wiktionaryManager): Response { diff --git a/src/Controller/LexiconController.php b/src/Controller/LexiconController.php index 87583f3..26f4882 100644 --- a/src/Controller/LexiconController.php +++ b/src/Controller/LexiconController.php @@ -280,7 +280,7 @@ class LexiconController extends AppBaseController * @Route("/{id}/add-word-from-searchbar", name="app_lexicon_add_word_from_search_bar") * @IsGranted("LEXICON_EDIT", subject="lexicon") * - * Barre de recherche : on cherche le mot en ajax. Si on le trouve dans le wiko, on l'importe et on recharge la page, sinon on ouvre la modale de confirmation d'ajout + * Barre de recherche : on cherche le mot en ajax. Si on le trouve dans Balex on redirige, sinon on le cherche dans le wiko, on l'importe et on recharge la page, sinon on ouvre la modale de confirmation d'ajout */ public function addWordFromSearchBar(Request $request, Lexicon $lexicon, WiktionaryManager $wiktionaryManager): Response { -- GitLab