add quick post data fetcher

This commit is contained in:
Simon Vieille 2021-04-01 12:17:14 +02:00
parent f5c522b52e
commit c3ae220630
4 changed files with 53 additions and 5 deletions

View file

@ -91,7 +91,7 @@ class PostAdminController extends AdminController
); );
$entityManager->update($entity); $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', [ return $this->redirectToRoute('admin_blog_post_edit', [
'entity' => $entity->getId(), '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") * @Route("/filters", name="admin_blog_post_filters")
*/ */

View file

@ -33,7 +33,7 @@ class Post implements EntityInterface
private $id; private $id;
/** /**
* @ORM\Column(type="string", length=255) * @ORM\Column(type="string", length=255, nullable=true)
*/ */
private $title; private $title;
@ -128,7 +128,7 @@ class Post implements EntityInterface
return $this->title; return $this->title;
} }
public function setTitle(string $title): self public function setTitle(?string $title): self
{ {
$this->title = $title; $this->title = $title;

View file

@ -31,11 +31,10 @@ class PostType extends AbstractType
TextType::class, TextType::class,
[ [
'label' => 'Titre', 'label' => 'Titre',
'required' => true, 'required' => false,
'attr' => [ 'attr' => [
], ],
'constraints' => [ 'constraints' => [
new NotBlank(),
], ],
] ]
); );

View file

@ -31,6 +31,11 @@
</span> </span>
</button> </button>
<div class="dropdown-menu dropdown-menu-right"> <div class="dropdown-menu dropdown-menu-right">
{% if entity.isQuick %}
<a href="{{ path('admin_blog_post_fetch_quick_data', {entity: entity.id}) }}" class="dropdown-item">
Récupérer données Quick
</a>
{% endif %}
<button type="submit" form="form-delete" class="dropdown-item"> <button type="submit" form="form-delete" class="dropdown-item">
Supprimer Supprimer
</button> </button>