Skip to content
Snippets Groups Projects
Commit e5e00395 authored by Pierre Fleutot's avatar Pierre Fleutot
Browse files

Ajout catégorie label system pour label "merge" pour copie d'entrées. Création...

Ajout catégorie label system pour label "merge" pour copie d'entrées. Création auto dans commande initialisation Balex. Début requête copie d'entrées
parent 8d9e6a0e
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace App\Command; namespace App\Command;
use App\Entity\Label;
use App\Entity\Lexicon; use App\Entity\Lexicon;
use App\Entity\User; use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
...@@ -18,7 +19,7 @@ use Symfony\Component\Uid\UuidV1; ...@@ -18,7 +19,7 @@ use Symfony\Component\Uid\UuidV1;
class BalexInitializeCommand extends Command class BalexInitializeCommand extends Command
{ {
protected static $defaultName = "app:balex:initialize"; protected static $defaultName = "app:balex:initialize";
protected static $defaultDescription = "Initialisation de l'appli Balex"; protected static $defaultDescription = "Initialisation de l'appli Balex. Création lexiques zéros et lexiques nouveaux mots pour chaque langue. Création du label système 'merged";
/** /**
* @var EntityManagerInterface * @var EntityManagerInterface
...@@ -48,6 +49,8 @@ class BalexInitializeCommand extends Command ...@@ -48,6 +49,8 @@ class BalexInitializeCommand extends Command
// $password = $input->getOption('password'); // $password = $input->getOption('password');
//TODO utiliser la liste de langues //TODO utiliser la liste de langues
// Création des lexiques zéro et new word
foreach (User::LANGUAGES as $language) { foreach (User::LANGUAGES as $language) {
// Création d'un lexique Zéro par langue // Création d'un lexique Zéro par langue
$zeroLexicon = new Lexicon(); $zeroLexicon = new Lexicon();
...@@ -63,6 +66,11 @@ class BalexInitializeCommand extends Command ...@@ -63,6 +66,11 @@ class BalexInitializeCommand extends Command
} }
$this->em->flush(); $this->em->flush();
$mergeLabel = new Label(Label::LABEL_CATEGORY_SYSTEM);
$mergeLabel->setName(Label::LABEL_MERGED);
$this->em->persist($mergeLabel);
$this->em->flush();
$io->success('Succès initialisation.'); $io->success('Succès initialisation.');
......
...@@ -140,6 +140,21 @@ class ApiBaseController extends AbstractController ...@@ -140,6 +140,21 @@ class ApiBaseController extends AbstractController
return $missingFields; return $missingFields;
} }
/**
* @param mixed
* @return bool
*/
public function isIntegerArray($array)
{
if (!is_array($array)) {
return false;
}
foreach ($array as $item) {
if (! is_int($item)) return false;
}
return true;
}
public function getLanguages() public function getLanguages()
{ {
return User::LANGUAGES; return User::LANGUAGES;
......
...@@ -124,6 +124,7 @@ class ApiEntryController extends ApiBaseController ...@@ -124,6 +124,7 @@ class ApiEntryController extends ApiBaseController
* @OA\RequestBody( * @OA\RequestBody(
* required=true, * required=true,
* @OA\JsonContent( * @OA\JsonContent(
* required={"graphy", "target_lex"},
* @OA\Property(property="graphy", type="string"), * @OA\Property(property="graphy", type="string"),
* @OA\Property(property="force", type="bool", example="false", description="force la création de l'entrée même si la graphie n'est pas trouvée dans le wiktionnaire"), * @OA\Property(property="force", type="bool", example="false", description="force la création de l'entrée même si la graphie n'est pas trouvée dans le wiktionnaire"),
* @OA\Property(property="target_lex", type="array", * @OA\Property(property="target_lex", type="array",
...@@ -182,6 +183,88 @@ class ApiEntryController extends ApiBaseController ...@@ -182,6 +183,88 @@ class ApiEntryController extends ApiBaseController
return $this->createJsonResponse(200); return $this->createJsonResponse(200);
} }
/**
* Copie une sélection d'entrée vers des lexiques cibles. Copie également les labels spécifiés à partir d'un mot. Si force=true, on crée l'entrée même si le mot n'est pas trouvé dans le wiktionnaire
*
* @Route("/copy", name="api_copy_entries", methods={"POST"})
*
* @OA\Response(response=200, description="success", @OA\JsonContent(type="string"))
* @OA\Response(response=401, description="error", @OA\JsonContent(type="string"))
* @OA\Response(response=403, description="error", @OA\JsonContent(type="string"))
* @OA\Response(response=500, description="error", @OA\JsonContent(type="string"))
* @OA\RequestBody(
* required=true,
* @OA\JsonContent(
* required={"entries", "target_lex", "merge"},
* @OA\Property(property="merge", type="string", example="overwrite OU ignore OU merge"),
* @OA\Property(property="target_lex", type="array",
* example={3, 4},
* @OA\Items(
* type="integer"
* )
* )
* @OA\Property(property="entries", type="array",
* example={1, 2},
* @OA\Items(
* type="integer"
* )
* )
* )
* )
* @OA\Tag(name="Entries")
* @Security(name="OAuth2")
*/
public function copyEntries(Request $request, SerializerInterface $serializer, WiktionaryManager $wiktionaryManager): 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);
if (!$data) {
return $this->createJsonResponse(401, ['error' => sprintf("Json non valide")]);
}
if ($missingFields = $this->getMissingFields($data, ['entries', 'target_lex', 'merge'])) {
return $this->createJsonResponse(401, ['error' => sprintf("Veuillez fournir une valeur pour: %s", implode(', ', $missingFields))]);
}
if (!$this->isIntegerArray($data['target_lex'])) {
return $this->createJsonResponse(401, ['error' => sprintf("target_lex doit être un tableau d'entiers")]);
}
if (!$this->isIntegerArray($data['entries'])) {
return $this->createJsonResponse(401, ['error' => sprintf("entries doit être un tableau d'entiers")]);
}
if (!in_array($data['merge'], Lexicon::MERGE_MODES)) {
return $this->createJsonResponse(401, ['error' => sprintf("Le mode de fusion « merge » doit être overwrite OU ignore OU merge")]);
}
$entries = $this->doctrine->getRepository(Entry::class)->find($data['entries']);
if (!$entries) {
return $this->createJsonResponse(401, ['error' => sprintf("Aucune entrée trouvée")]);
}
$lexicons = $this->getLexicons($data['target_lex']);
if (!$lexicons) {
return $this->createJsonResponse(401);
}
// On récupère ou on crée le mot-vedette
if (!$headword) {
if ($forceCreation || $wiktionaryManager->search($data['graphy'], $language)) {
$headword = $this->newHeadword($data['graphy'], $language);
} else {
return $this->createJsonResponse(401, ['warning' => sprintf("Le mot «%s» n'existe ni dans Balex ni dans le wiktionnaire.", $data['graphy'])]);
}
}
// 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])) {
$entry = $this->createEntryInLexicon($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);
}
}
$this->doctrine->getManager()->flush();
return $this->createJsonResponse(200);
}
/** /**
* @Route("/get/{id}", name="get_entry", methods={"GET"}) * @Route("/get/{id}", name="get_entry", methods={"GET"})
* *
......
...@@ -18,6 +18,7 @@ class Label ...@@ -18,6 +18,7 @@ class Label
const LABEL_CATEGORY_THEMATIC = 'thematic'; const LABEL_CATEGORY_THEMATIC = 'thematic';
const LABEL_CATEGORY_LIST = 'list'; const LABEL_CATEGORY_LIST = 'list';
const LABEL_CATEGORY_MILESTONE = 'milestone'; const LABEL_CATEGORY_MILESTONE = 'milestone';
const LABEL_CATEGORY_SYSTEM = 'system';
const LABEL_LIST_CATEGORIES = [ const LABEL_LIST_CATEGORIES = [
self::LABEL_CATEGORY_MORPHOLOGICAL => self::LABEL_CATEGORY_MORPHOLOGICAL, self::LABEL_CATEGORY_MORPHOLOGICAL => self::LABEL_CATEGORY_MORPHOLOGICAL,
...@@ -26,6 +27,8 @@ class Label ...@@ -26,6 +27,8 @@ class Label
self::LABEL_CATEGORY_MILESTONE => self::LABEL_CATEGORY_MILESTONE, self::LABEL_CATEGORY_MILESTONE => self::LABEL_CATEGORY_MILESTONE,
]; ];
const LABEL_MERGED = 'merged';
/** /**
* @ORM\Id * @ORM\Id
* @ORM\GeneratedValue * @ORM\GeneratedValue
...@@ -111,6 +114,10 @@ class Label ...@@ -111,6 +114,10 @@ class Label
{ {
return $this->getCategory() === self::LABEL_CATEGORY_MILESTONE; return $this->getCategory() === self::LABEL_CATEGORY_MILESTONE;
} }
public function isSystem()
{
return $this->getCategory() === self::LABEL_CATEGORY_SYSTEM;
}
/** /**
* @ORM\PrePersist * @ORM\PrePersist
......
...@@ -20,6 +20,14 @@ class Lexicon ...@@ -20,6 +20,14 @@ class Lexicon
const TYPE_ZERO = 'Zero'; const TYPE_ZERO = 'Zero';
const TYPE_NEW_WORDS = 'New words'; const TYPE_NEW_WORDS = 'New words';
const MERGE_MERGE = 'merge';
const MERGE_OVERWRITE = 'overwrite';
const MERGE_IGNORE = 'ignore';
const MERGE_MODES = [
self::MERGE_OVERWRITE, self::MERGE_IGNORE, self::MERGE_MERGE
];
/** /**
* @ORM\Id * @ORM\Id
* @ORM\GeneratedValue * @ORM\GeneratedValue
......
...@@ -70,6 +70,11 @@ class LabelVoter extends Voter ...@@ -70,6 +70,11 @@ class LabelVoter extends Voter
private function canEdit(Label $label, User $user): bool private function canEdit(Label $label, User $user): bool
{ {
// On ne touche pas aux labels Système
if ($label->isSystem()) {
return false;
}
// On modifie ses propres labels // On modifie ses propres labels
if ($label->getUser() === $user) { if ($label->getUser() === $user) {
return true; return true;
...@@ -86,6 +91,10 @@ class LabelVoter extends Voter ...@@ -86,6 +91,10 @@ class LabelVoter extends Voter
private function canDelete(Label $label, User $user): bool private function canDelete(Label $label, User $user): bool
{ {
// On ne touche pas aux labels Système
if ($label->isSystem()) {
return false;
}
// On supprime ses propres labels // On supprime ses propres labels
if ($label->getUser() === $user) { if ($label->getUser() === $user) {
return true; return true;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment