diff --git a/ChillMainBundle.php b/ChillMainBundle.php index bb4c675a7..59bf33e50 100644 --- a/ChillMainBundle.php +++ b/ChillMainBundle.php @@ -6,6 +6,7 @@ use Symfony\Component\HttpKernel\Bundle\Bundle; use Symfony\Component\DependencyInjection\ContainerBuilder; use Chill\MainBundle\DependencyInjection\SearchableServicesCompilerPass; use Chill\MainBundle\DependencyInjection\ConfigConsistencyCompilerPass; +use Chill\MainBundle\DependencyInjection\TimelineCompilerClass; class ChillMainBundle extends Bundle { @@ -14,5 +15,6 @@ class ChillMainBundle extends Bundle parent::build($container); $container->addCompilerPass(new SearchableServicesCompilerPass()); $container->addCompilerPass(new ConfigConsistencyCompilerPass()); + $container->addCompilerPass(new TimelineCompilerClass()); } } diff --git a/DependencyInjection/TimelineCompilerClass.php b/DependencyInjection/TimelineCompilerClass.php new file mode 100644 index 000000000..4fad76538 --- /dev/null +++ b/DependencyInjection/TimelineCompilerClass.php @@ -0,0 +1,66 @@ + + * + * 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 . + */ + +namespace Chill\MainBundle\DependencyInjection; + +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; + +/** + * Add services taggued with `name: chill.timeline` to + * timeline_builder service definition + * + * @author Julien Fastré + */ +class TimelineCompilerClass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container) + { + if (!$container->hasDefinition('chill.main.timeline_builder')) { + throw new \LogicException('service chill.main.timeline_builder ' + . 'is not defined.'); + } + + $definition = $container->getDefinition( + 'chill.main.timeline_builder' + ); + + $taggedServices = $container->findTaggedServiceIds( + 'chill.timeline' + ); + + foreach ($taggedServices as $id => $tagAttributes) { + + foreach ($tagAttributes as $attributes) { + + if (!isset($attributes["context"])) { + throw new \LogicException("the 'context' attribute is missing in your ". + "service '$id' definition"); + } + + $definition->addMethodCall( + 'addProvider', + array($attributes["context"], $id) + ); + } + } + } + +} diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 793c1f3da..e0dcbf936 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -79,3 +79,10 @@ services: chill.main.search_provider: class: Chill\MainBundle\Search\SearchProvider + + chill.main.timeline_builder: + class: Chill\MainBundle\Timeline\TimelineBuilder + arguments: + - "@doctrine.orm.entity_manager" + calls: + - [ setContainer, ["@service_container"]] diff --git a/Resources/views/Timeline/index.html.twig b/Resources/views/Timeline/index.html.twig new file mode 100644 index 000000000..eb5be58d6 --- /dev/null +++ b/Resources/views/Timeline/index.html.twig @@ -0,0 +1,8 @@ +
+{% for result in results %} +
+ {% include result.template with result.templateArgs %} +
+ +{% endfor %} +
\ No newline at end of file diff --git a/Timeline/TimelineBuilder.php b/Timeline/TimelineBuilder.php new file mode 100644 index 000000000..79472c9f2 --- /dev/null +++ b/Timeline/TimelineBuilder.php @@ -0,0 +1,150 @@ + + * + * 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 . + */ + +namespace Chill\MainBundle\Timeline; + +use Doctrine\ORM\Query\ResultSetMapping; +use Doctrine\ORM\EntityManagerInterface; +use Symfony\Component\DependencyInjection\ContainerAwareInterface; + +/** + * Build timeline + * + * @author Julien Fastré + */ +class TimelineBuilder implements ContainerAwareInterface +{ + + use \Symfony\Component\DependencyInjection\ContainerAwareTrait; + + /** + * + * @var \Doctrine\ORM\EntityManagerInterface + */ + private $em; + + public function __construct(EntityManagerInterface $em) + { + $this->em = $em; + } + + /** + * + * @var string references to providers services + */ + private $providers = array(); + + public function getTimeline($context, array $args, $page = 0, $number = 20) + { + $query = $this->buildQuery($context, $args, $page, $number); + $ids = $this->runQuery($query); + $entitiesByKey = $this->getEntities($ids, $context); + + return $this->render($ids, $entitiesByKey); + + } + + public function addProvider($context, $id) + { + $this->providers[$context][] = [$id]; + } + + + private function buildQuery($context, array $args, $page, $number) + { + if (!array_key_exists($context, $this->providers)) { + throw new \LogicException(sprintf('No builders have been defined for "%s"' + . ' context', $context)); + } + + $query = ''; + foreach($this->providers[$context] as $providerIds) { + foreach ($providerIds as $providerId) { + $provider = $this->container->get($providerId); + + $query .= ($query === '') ? + $provider->fetchUnion($context, $args) : + ' UNION '.$provider->fetchUnion($context, $args); + } + } + $query .= sprintf(' ORDER BY date LIMIT %d OFFSET %d', + $number, $page * $number); + + return $query; + + + } + + private function runQuery($query) + { + $resultSetMapping = (new ResultSetMapping()) + ->addScalarResult('id', 'id') + ->addScalarResult('key', 'key') + ->addScalarResult('date', 'date'); + + return $this->em->createNativeQuery($query, $resultSetMapping) + ->getArrayResult(); + } + + private function getEntities(array $queriedIds, $context) + { + //gather entities by key. Having all ids in the same table allow to query from providers + $idsByKey = array(); + + foreach($queriedIds as $result) { + $idsByKey[$result['key']][] = $result['id']; + } + + //fetch entities from providers + $entitiesByKey = array(); + foreach ($idsByKey as $key => $ids) { + //iterate providers for current context + foreach($this->providers[$context] as $providerIds) { + foreach ($providerIds as $providerId){ + $provider = $this->container->get($providerId); + + if ($provider->supportsKey($key)) { + $entitiesByKey[$key] = $provider->getEntities($ids); + } + } + } + } + + return $entitiesByKey; + } + + private function render(array $queriedIds, $entitiesByKey) + { + //add results to a pretty array + $timelineEntries = array(); + foreach ($queriedIds as $result) { + $timelineEntry['date'] = $result['date']; + $timelineEntry['template'] = $entitiesByKey[$result['key']][$result['id']]['template']; + $timelineEntry['templateArgs'] = $entitiesByKey[$result['key']][$result['id']]['entity']; + + $timelineEntries[] = $timelineEntry; + } + + return $this->container->get('templating') + ->render('ChillMainBundle:Timeline:index.html.twig', array( + 'results' => $timelineEntries + )); + + } +} diff --git a/Timeline/TimelineProviderInterface.php b/Timeline/TimelineProviderInterface.php new file mode 100644 index 000000000..a6edeee3d --- /dev/null +++ b/Timeline/TimelineProviderInterface.php @@ -0,0 +1,51 @@ + + * + * 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 . + */ + +namespace Chill\MainBundle\Timeline; + +/** + * Interface for service providing info to timeline + * + * @author Julien Fastré + */ +interface TimelineProviderInterface +{ + /** + * Indicate if the result id may be handled by the service + * + * @param string $key the key present in the SELECT query + * @return boolean + */ + public function supportsKey($key); + + /** + * fetch entities from db and indicate how to render them + * + * @param array $ids + * @return array[] an array of an associative array. 'template' will indicate a template name on how to render the entity, 'entity' will be the arguments of the template + */ + public function getEntities(array $ids); + + /** + * provide a SQL SELECT query to fetch entities id + * + * @return string an SQL SELECT query which will fetch ID. The query must have and id (INT), a key (STRING), and a datetime to order results + */ + public function fetchUnion($context, array $args); +}