mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-05 07:19:49 +00:00
Fix TimelineBuilder service.
This commit is contained in:
parent
d2a93f0763
commit
1f488109e3
@ -29,40 +29,40 @@ use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
|||||||
*
|
*
|
||||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||||
*/
|
*/
|
||||||
class TimelineBuilder implements ContainerAwareInterface
|
class TimelineBuilder implements ContainerAwareInterface, TimelineBuilderInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
|
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @var \Doctrine\ORM\EntityManagerInterface
|
* @var \Doctrine\ORM\EntityManagerInterface
|
||||||
*/
|
*/
|
||||||
private $em;
|
private $em;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Record provider
|
* Record provider
|
||||||
*
|
*
|
||||||
* This array has the structure `[ 'service id' => $service ]`
|
* This array has the structure `[ 'service id' => $service ]`
|
||||||
*
|
*
|
||||||
* @var TimelineProviderInterface[]
|
* @var TimelineProviderInterface[]
|
||||||
*/
|
*/
|
||||||
private $providers = [];
|
private $providers = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Record provider and their context
|
* Record provider and their context
|
||||||
*
|
*
|
||||||
* This array has the structure `[ 'context' => [ 'service id' ] ]`
|
* This array has the structure `[ 'context' => [ 'service id' ] ]`
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private $providersByContext = [];
|
private $providersByContext = [];
|
||||||
|
|
||||||
public function __construct(EntityManagerInterface $em)
|
public function __construct(EntityManagerInterface $em)
|
||||||
{
|
{
|
||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* return an HTML string with timeline
|
* return an HTML string with timeline
|
||||||
*
|
*
|
||||||
@ -79,18 +79,18 @@ class TimelineBuilder implements ContainerAwareInterface
|
|||||||
public function getTimelineHTML($context, array $args, $firstItem = 0, $number = 20)
|
public function getTimelineHTML($context, array $args, $firstItem = 0, $number = 20)
|
||||||
{
|
{
|
||||||
$union = $this->buildUnionQuery($context, $args);
|
$union = $this->buildUnionQuery($context, $args);
|
||||||
|
|
||||||
//add ORDER BY clause and LIMIT
|
//add ORDER BY clause and LIMIT
|
||||||
$query = $union . sprintf(' ORDER BY date DESC LIMIT %d OFFSET %d',
|
$query = $union . sprintf(' ORDER BY date DESC LIMIT %d OFFSET %d',
|
||||||
$number, $firstItem);
|
$number, $firstItem);
|
||||||
|
|
||||||
// run query and handle results
|
// run query and handle results
|
||||||
$fetched = $this->runUnionQuery($query);
|
$fetched = $this->runUnionQuery($query);
|
||||||
$entitiesByKey = $this->getEntities($fetched, $context);
|
$entitiesByKey = $this->getEntities($fetched, $context);
|
||||||
|
|
||||||
return $this->render($fetched, $entitiesByKey, $context, $args);
|
return $this->render($fetched, $entitiesByKey, $context, $args);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the number of items for the given context and args
|
* Return the number of items for the given context and args
|
||||||
*
|
*
|
||||||
@ -101,17 +101,17 @@ class TimelineBuilder implements ContainerAwareInterface
|
|||||||
public function countItems($context, array $args)
|
public function countItems($context, array $args)
|
||||||
{
|
{
|
||||||
$union = $this->buildUnionQuery($context, $args);
|
$union = $this->buildUnionQuery($context, $args);
|
||||||
|
|
||||||
// embed the union query inside a count query
|
// embed the union query inside a count query
|
||||||
$count = sprintf('SELECT COUNT(sq.id) AS total FROM (%s) as sq', $union);
|
$count = sprintf('SELECT COUNT(sq.id) AS total FROM (%s) as sq', $union);
|
||||||
|
|
||||||
$rsm = (new ResultSetMapping())
|
$rsm = (new ResultSetMapping())
|
||||||
->addScalarResult('total', 'total', Type::INTEGER);
|
->addScalarResult('total', 'total', Type::INTEGER);
|
||||||
|
|
||||||
return $this->em->createNativeQuery($count, $rsm)
|
return $this->em->createNativeQuery($count, $rsm)
|
||||||
->getSingleScalarResult();
|
->getSingleScalarResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* add a provider id
|
* add a provider id
|
||||||
*
|
*
|
||||||
@ -125,7 +125,7 @@ class TimelineBuilder implements ContainerAwareInterface
|
|||||||
$this->providersByContext[$context][] = $id;
|
$this->providersByContext[$context][] = $id;
|
||||||
$this->providers[$id] = $provider;
|
$this->providers[$id] = $provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get providers by context
|
* Get providers by context
|
||||||
*
|
*
|
||||||
@ -139,16 +139,16 @@ class TimelineBuilder implements ContainerAwareInterface
|
|||||||
throw new \LogicException(sprintf('No builders have been defined for "%s"'
|
throw new \LogicException(sprintf('No builders have been defined for "%s"'
|
||||||
. ' context', $context));
|
. ' context', $context));
|
||||||
}
|
}
|
||||||
|
|
||||||
$providers = [];
|
$providers = [];
|
||||||
|
|
||||||
foreach($this->providersByContext[$context] as $providerId) {
|
foreach($this->providersByContext[$context] as $providerId) {
|
||||||
$providers[] = $this->providers[$providerId];
|
$providers[] = $this->providers[$providerId];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $providers;
|
return $providers;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* build the UNION query with all providers
|
* build the UNION query with all providers
|
||||||
*
|
*
|
||||||
@ -170,10 +170,10 @@ class TimelineBuilder implements ContainerAwareInterface
|
|||||||
$append = ($union === '') ? $select : ' UNION '.$select;
|
$append = ($union === '') ? $select : ' UNION '.$select;
|
||||||
$union .= $append;
|
$union .= $append;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $union;
|
return $union;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* return the SQL SELECT query as a string,
|
* return the SQL SELECT query as a string,
|
||||||
*
|
*
|
||||||
@ -186,7 +186,7 @@ class TimelineBuilder implements ContainerAwareInterface
|
|||||||
private function buildSelectQuery(TimelineProviderInterface $provider, $context, array $args)
|
private function buildSelectQuery(TimelineProviderInterface $provider, $context, array $args)
|
||||||
{
|
{
|
||||||
$data = $provider->fetchQuery($context, $args);
|
$data = $provider->fetchQuery($context, $args);
|
||||||
|
|
||||||
return sprintf(
|
return sprintf(
|
||||||
'SELECT %s AS id, '
|
'SELECT %s AS id, '
|
||||||
. '%s AS "date", '
|
. '%s AS "date", '
|
||||||
@ -199,7 +199,7 @@ class TimelineBuilder implements ContainerAwareInterface
|
|||||||
$data['FROM'],
|
$data['FROM'],
|
||||||
$data['WHERE']);
|
$data['WHERE']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* run the UNION query and return result as an array
|
* run the UNION query and return result as an array
|
||||||
*
|
*
|
||||||
@ -212,11 +212,11 @@ class TimelineBuilder implements ContainerAwareInterface
|
|||||||
->addScalarResult('id', 'id')
|
->addScalarResult('id', 'id')
|
||||||
->addScalarResult('type', 'type')
|
->addScalarResult('type', 'type')
|
||||||
->addScalarResult('date', 'date');
|
->addScalarResult('date', 'date');
|
||||||
|
|
||||||
return $this->em->createNativeQuery($query, $resultSetMapping)
|
return $this->em->createNativeQuery($query, $resultSetMapping)
|
||||||
->getArrayResult();
|
->getArrayResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param array $queriedIds
|
* @param array $queriedIds
|
||||||
@ -227,11 +227,11 @@ class TimelineBuilder implements ContainerAwareInterface
|
|||||||
{
|
{
|
||||||
//gather entities by type to pass all id with same type to the TimelineProvider.
|
//gather entities by type to pass all id with same type to the TimelineProvider.
|
||||||
$idsByType = array();
|
$idsByType = array();
|
||||||
|
|
||||||
foreach($queriedIds as $result) {
|
foreach($queriedIds as $result) {
|
||||||
$idsByType[$result['type']][] = $result['id'];
|
$idsByType[$result['type']][] = $result['id'];
|
||||||
}
|
}
|
||||||
|
|
||||||
//fetch entities from providers
|
//fetch entities from providers
|
||||||
$entitiesByType = array();
|
$entitiesByType = array();
|
||||||
foreach ($idsByType as $type => $ids) {
|
foreach ($idsByType as $type => $ids) {
|
||||||
@ -243,10 +243,10 @@ class TimelineBuilder implements ContainerAwareInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $entitiesByType;
|
return $entitiesByType;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* render the timeline as HTML
|
* render the timeline as HTML
|
||||||
*
|
*
|
||||||
@ -269,17 +269,17 @@ class TimelineBuilder implements ContainerAwareInterface
|
|||||||
$timelineEntry['date'] = new \DateTime($result['date']);
|
$timelineEntry['date'] = new \DateTime($result['date']);
|
||||||
$timelineEntry['template'] = $data['template'];
|
$timelineEntry['template'] = $data['template'];
|
||||||
$timelineEntry['template_data'] = $data['template_data'];
|
$timelineEntry['template_data'] = $data['template_data'];
|
||||||
|
|
||||||
$timelineEntries[] = $timelineEntry;
|
$timelineEntries[] = $timelineEntry;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->container->get('templating')
|
return $this->container->get('templating')
|
||||||
->render('@ChillMain/Timeline/index.html.twig', array(
|
->render('@ChillMain/Timeline/index.html.twig', array(
|
||||||
'results' => $timelineEntries
|
'results' => $timelineEntries
|
||||||
));
|
));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* get the template data from the provider for the given entity, by type.
|
* get the template data from the provider for the given entity, by type.
|
||||||
*
|
*
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2015 Champs-Libres Coopérative <info@champs-libres.coop>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Chill\MainBundle\Timeline;
|
||||||
|
|
||||||
|
interface TimelineBuilderInterface
|
||||||
|
{
|
||||||
|
}
|
@ -4,4 +4,6 @@ services:
|
|||||||
arguments:
|
arguments:
|
||||||
- "@doctrine.orm.entity_manager"
|
- "@doctrine.orm.entity_manager"
|
||||||
calls:
|
calls:
|
||||||
- [ setContainer, ["@service_container"]]
|
- [ setContainer, ["@service_container"]]
|
||||||
|
|
||||||
|
Chill\MainBundle\Timeline\TimelineBuilderInterface: "@chill_main.timeline_builder"
|
||||||
|
@ -26,6 +26,7 @@ use Symfony\Component\HttpFoundation\Response;
|
|||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Chill\MainBundle\Timeline\TimelineBuilder;
|
use Chill\MainBundle\Timeline\TimelineBuilder;
|
||||||
use Chill\MainBundle\Pagination\PaginatorFactory;
|
use Chill\MainBundle\Pagination\PaginatorFactory;
|
||||||
|
use Chill\MainBundle\Timeline\TimelineBuilderInterface;
|
||||||
use Chill\PersonBundle\Security\Authorization\PersonVoter;
|
use Chill\PersonBundle\Security\Authorization\PersonVoter;
|
||||||
use Symfony\Component\Routing\Annotation\Route;
|
use Symfony\Component\Routing\Annotation\Route;
|
||||||
|
|
||||||
@ -37,24 +38,24 @@ use Symfony\Component\Routing\Annotation\Route;
|
|||||||
*/
|
*/
|
||||||
class TimelinePersonController extends AbstractController
|
class TimelinePersonController extends AbstractController
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var EventDispatcherInterface
|
* @var EventDispatcherInterface
|
||||||
*/
|
*/
|
||||||
protected $eventDispatcher;
|
protected $eventDispatcher;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @var TimelineBuilder
|
* @var TimelineBuilderInterface
|
||||||
*/
|
*/
|
||||||
protected $timelineBuilder;
|
protected $timelineBuilder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @var PaginatorFactory
|
* @var PaginatorFactory
|
||||||
*/
|
*/
|
||||||
protected $paginatorFactory;
|
protected $paginatorFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TimelinePersonController constructor.
|
* TimelinePersonController constructor.
|
||||||
*
|
*
|
||||||
@ -62,7 +63,7 @@ class TimelinePersonController extends AbstractController
|
|||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
EventDispatcherInterface $eventDispatcher,
|
EventDispatcherInterface $eventDispatcher,
|
||||||
TimelineBuilder $timelineBuilder,
|
TimelineBuilderInterface $timelineBuilder,
|
||||||
PaginatorFactory $paginatorFactory
|
PaginatorFactory $paginatorFactory
|
||||||
) {
|
) {
|
||||||
$this->eventDispatcher = $eventDispatcher;
|
$this->eventDispatcher = $eventDispatcher;
|
||||||
@ -93,22 +94,22 @@ class TimelinePersonController extends AbstractController
|
|||||||
if ($person === NULL) {
|
if ($person === NULL) {
|
||||||
throw $this->createNotFoundException();
|
throw $this->createNotFoundException();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->denyAccessUnlessGranted(PersonVoter::SEE, $person);
|
$this->denyAccessUnlessGranted(PersonVoter::SEE, $person);
|
||||||
|
|
||||||
$nbItems = $this->timelineBuilder->countItems('person',
|
$nbItems = $this->timelineBuilder->countItems('person',
|
||||||
[ 'person' => $person ]
|
[ 'person' => $person ]
|
||||||
);
|
);
|
||||||
|
|
||||||
$paginator = $this->paginatorFactory->create($nbItems);
|
$paginator = $this->paginatorFactory->create($nbItems);
|
||||||
|
|
||||||
$event = new PrivacyEvent($person, array('action' => 'timeline'));
|
$event = new PrivacyEvent($person, array('action' => 'timeline'));
|
||||||
$this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event);
|
$this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event);
|
||||||
|
|
||||||
return $this->render('ChillPersonBundle:Timeline:index.html.twig', array
|
return $this->render('ChillPersonBundle:Timeline:index.html.twig', array
|
||||||
(
|
(
|
||||||
'timeline' => $this->timelineBuilder->getTimelineHTML(
|
'timeline' => $this->timelineBuilder->getTimelineHTML(
|
||||||
'person',
|
'person',
|
||||||
array('person' => $person),
|
array('person' => $person),
|
||||||
$paginator->getCurrentPage()->getFirstItemNumber(),
|
$paginator->getCurrentPage()->getFirstItemNumber(),
|
||||||
$paginator->getItemsPerPage()
|
$paginator->getItemsPerPage()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user