diff --git a/src/Controller/Blog/PostAdminController.php b/src/Controller/Blog/PostAdminController.php index 7ae5333..3912bc6 100644 --- a/src/Controller/Blog/PostAdminController.php +++ b/src/Controller/Blog/PostAdminController.php @@ -91,7 +91,7 @@ class PostAdminController extends AdminController ); $entityManager->update($entity); - $this->addFlash('success', 'The form is not valid.'); + $this->addFlash('success', 'The data has been saved.'); return $this->redirectToRoute('admin_blog_post_edit', [ 'entity' => $entity->getId(), @@ -116,6 +116,50 @@ class PostAdminController extends AdminController ]); } + /** + * @Route("/fetch_quick_data/{entity}", name="admin_blog_post_fetch_quick_data") + */ + public function fetchQuickData(Entity $entity, EntityManager $entityManager): Response + { + if (!$entity->getIsQuick()) { + $this->addFlash('warning', 'L\'article n\'est pas Quick'); + } else { + try { + $query = '?'.http_build_query(['url' => $entity->getQuickUrl()]); + + $apiOpengraph = 'https://api-page.deblan.org/do/opengraph'.$query; + $apiGraby = 'https://api-page.deblan.org/do/graby'.$query; + + $opengraph = json_decode(file_get_contents($apiOpengraph), true); + $graby = json_decode(file_get_contents($apiGraby), true); + + if (!$entity->getTitle()) { + $entity->setTitle($opengraph['title'] ?? $graby['title'] ?? null); + } + + if (!$entity->getContent()) { + $entity->setContent($opengraph['description'] ?? $graby['summary'] ?? null); + } + + $entity + ->setQuickImage($opengraph['images'][0]['url'] ?? $graby['image'] ?? null) + ->setQuickVideo($opengraph['videos'][0]['url'] ?? null) + ->setQuickVideoWidth($opengraph['videos'][0]['width'] ?? null) + ->setQuickVideoHeight($opengraph['videos'][0]['height'] ?? null); + + $entityManager->update($entity); + + $this->addFlash('success', 'The data has been saved.'); + } catch (\Exception $e) { + $this->addFlash('error', 'Une erreur s\'est produite'); + } + } + + return $this->redirectToRoute('admin_blog_post_edit', [ + 'entity' => $entity->getId(), + ]); + } + /** * @Route("/filters", name="admin_blog_post_filters") */ diff --git a/src/Entity/Blog/Post.php b/src/Entity/Blog/Post.php index e00916d..dc4addf 100644 --- a/src/Entity/Blog/Post.php +++ b/src/Entity/Blog/Post.php @@ -33,7 +33,7 @@ class Post implements EntityInterface private $id; /** - * @ORM\Column(type="string", length=255) + * @ORM\Column(type="string", length=255, nullable=true) */ private $title; @@ -128,7 +128,7 @@ class Post implements EntityInterface return $this->title; } - public function setTitle(string $title): self + public function setTitle(?string $title): self { $this->title = $title; diff --git a/src/Form/Blog/PostType.php b/src/Form/Blog/PostType.php index e6d8f84..b9b5dee 100644 --- a/src/Form/Blog/PostType.php +++ b/src/Form/Blog/PostType.php @@ -31,11 +31,10 @@ class PostType extends AbstractType TextType::class, [ 'label' => 'Titre', - 'required' => true, + 'required' => false, 'attr' => [ ], 'constraints' => [ - new NotBlank(), ], ] ); diff --git a/templates/blog/post_admin/edit.html.twig b/templates/blog/post_admin/edit.html.twig index 10e159b..4213134 100644 --- a/templates/blog/post_admin/edit.html.twig +++ b/templates/blog/post_admin/edit.html.twig @@ -31,6 +31,11 @@