add post analytic

This commit is contained in:
Simon Vieille 2022-04-19 16:11:51 +02:00
parent 125e70c9a4
commit 6674c65770
Signed by: deblan
GPG key ID: 579388D585F70417
3 changed files with 92 additions and 0 deletions

View file

@ -0,0 +1,38 @@
<?php
namespace App\Analytic;
use App\Core\Analytic\DateRangeAnalytic as BaseDateRangeAnalytic;
/**
* class DateRangeAnalytic.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class DateRangeAnalytic extends BaseDateRangeAnalytic
{
protected string $page;
public function setPath(string $path): self
{
$this->path = $path;
$this->reload = true;
return $this;
}
protected function getEntities(string $type): array
{
$entities = parent::getEntities($type);
foreach ($entities as $key => $entity) {
if ('view' === $type) {
if ($entity->getPath() !== $this->path) {
unset($entities[$key]);
}
}
}
return $entities;
}
}

View file

@ -21,6 +21,8 @@ use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\Form;
use App\Core\Entity\EntityInterface;
use App\Entity\Blog\Post;
use App\Analytic\DateRangeAnalytic;
use App\Core\Repository\Site\NodeRepository;
/**
* @Route("/admin/blog/post")
@ -43,6 +45,7 @@ class PostAdminController extends CrudController
->setPageRoute('filter', 'admin_blog_post_filter')
->setPageRoute('batch', 'admin_blog_post_batch')
->setPageRoute('fetch_quick_data', 'admin_blog_post_fetch_quick_data')
->setPageRoute('analytic_stats', 'admin_blog_post_analytic_stats')
->setForm('new', PostType::class, [])
->setForm('edit', PostType::class, [])
@ -52,6 +55,7 @@ class PostAdminController extends CrudController
->setView('form', 'blog/post_admin/_form.html.twig')
->setView('edit', 'blog/post_admin/edit.html.twig')
->setView('show', 'blog/post_admin/show.html.twig')
->setView('index', 'blog/post_admin/index.html.twig')
->setDefaultSort('index', 'id', 'desc')
->setField('index', 'Titre', TextField::class, [
@ -228,6 +232,42 @@ class PostAdminController extends CrudController
return $this->doDelete($entity, $entityManager, $request);
}
/**
* @Route("/analytic_stats/{entity}", name="admin_blog_post_analytic_stats")
*/
public function stats(
Entity $entity,
DateRangeAnalytic $analytic,
NodeRepository $nodeRepository,
string $range = '7days'
): Response
{
if (!in_array($range, ['7days', '30days', '90days', '1year'])) {
throw $this->createNotFoundException();
}
$node = $nodeRepository->findOneBy([
'code' => 'post',
]);
$analytic
->setDateRange(new \DateTime('now - '.$range), new \DateTime())
->setPath($this->generateUrl('blog_menu_post', [
'post' => $entity->getId(),
'slug' => $entity->getSlug(),
]))
->setNode($node)
;
return $this->render('@Core/analytic/stats.html.twig', [
'range' => $range,
'views' => $analytic->getViews(),
'pathViews' => $analytic->getPathViews(),
'referers' => [],
'node' => $node,
]);
}
public function getSection(): string
{
return 'blog_post';

View file

@ -0,0 +1,14 @@
{% extends '@Core/admin/crud/index.html.twig' %}
{% block list_item_actions_before %}
{% if configuration.action(context, 'analytic_stats', true) %}
{% set analytics = path(
configuration.pageRoute('analytic_stats'),
{entity: item.id}|merge(configuration.pageRouteParams('analytic_stats'))
) %}
<button data-modal="{{ analytics }}" class="btn btn-sm btn-light border border-dark mr-1 mb-1">
<span data-modal="{{ analytics }}" class="fa fa-chart-bar"></span>
</button>
{% endif %}
{% endblock %}