mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-02 21:13:57 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,47 +1,36 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 Champs-Libres Coopérative <info@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Timeline;
|
||||
|
||||
use Doctrine\ORM\Query\ResultSetMapping;
|
||||
use DateTime;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\NativeQuery;
|
||||
use Doctrine\ORM\Query\ResultSetMapping;
|
||||
use LogicException;
|
||||
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
||||
|
||||
/**
|
||||
* Build timeline
|
||||
* Build timeline.
|
||||
*/
|
||||
class TimelineBuilder implements ContainerAwareInterface
|
||||
{
|
||||
|
||||
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Doctrine\ORM\EntityManagerInterface
|
||||
*/
|
||||
private $em;
|
||||
|
||||
/**
|
||||
* Record provider
|
||||
* Record provider.
|
||||
*
|
||||
* This array has the structure `[ 'service id' => $service ]`
|
||||
*
|
||||
@@ -50,7 +39,7 @@ class TimelineBuilder implements ContainerAwareInterface
|
||||
private $providers = [];
|
||||
|
||||
/**
|
||||
* Record provider and their context
|
||||
* Record provider and their context.
|
||||
*
|
||||
* This array has the structure `[ 'context' => [ 'service id' ] ]`
|
||||
*
|
||||
@@ -64,58 +53,7 @@ class TimelineBuilder implements ContainerAwareInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* return an HTML string with timeline
|
||||
*
|
||||
* This function must be called from controller
|
||||
*
|
||||
* @example https://redmine.champs-libres.coop/projects/chillperson/repository/revisions/bd2e1b1808f73e39532e9538413025df5487cad0/entry/Controller/TimelinePersonController.php#L47 the implementation in person bundle
|
||||
*
|
||||
* @param string $context
|
||||
* @param array $args arguments defined by the bundle which create the context
|
||||
* @param int $firstItem first item number
|
||||
* @param int $number number of items by page
|
||||
* @return string an HTML representation, must be included using `|raw` filter
|
||||
*/
|
||||
public function getTimelineHTML($context, array $args, $firstItem = 0, $number = 20)
|
||||
{
|
||||
list($union, $parameters) = $this->buildUnionQuery($context, $args);
|
||||
|
||||
//add ORDER BY clause and LIMIT
|
||||
$query = $union . sprintf(' ORDER BY date DESC LIMIT %d OFFSET %d',
|
||||
$number, $firstItem);
|
||||
|
||||
// run query and handle results
|
||||
$fetched = $this->runUnionQuery($query, $parameters);
|
||||
$entitiesByKey = $this->getEntities($fetched, $context);
|
||||
|
||||
return $this->render($fetched, $entitiesByKey, $context, $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of items for the given context and args
|
||||
*
|
||||
* @param unknown $context
|
||||
* @param array $args
|
||||
* @return mixed|\Doctrine\DBAL\Driver\Statement|NULL
|
||||
*/
|
||||
public function countItems($context, array $args)
|
||||
{
|
||||
$rsm = (new ResultSetMapping())
|
||||
->addScalarResult('total', 'total', Types::INTEGER);
|
||||
|
||||
list($select, $parameters) = $this->buildUnionQuery($context, $args);
|
||||
|
||||
// embed the union query inside a count query
|
||||
$countQuery = sprintf('SELECT COUNT(sq.id) AS total FROM (%s) as sq', $select);
|
||||
|
||||
$nq = $this->em->createNativeQuery($countQuery, $rsm);
|
||||
$nq->setParameters($parameters);
|
||||
|
||||
return $nq->getSingleScalarResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* add a provider id
|
||||
* add a provider id.
|
||||
*
|
||||
* @internal This function is called by the TimelineCompilerClass
|
||||
*
|
||||
@@ -129,22 +67,46 @@ class TimelineBuilder implements ContainerAwareInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Get providers by context
|
||||
* Return the number of items for the given context and args.
|
||||
*
|
||||
* @param unknown $context
|
||||
*
|
||||
* @return \Doctrine\DBAL\Driver\Statement|mixed|null
|
||||
*/
|
||||
public function countItems($context, array $args)
|
||||
{
|
||||
$rsm = (new ResultSetMapping())
|
||||
->addScalarResult('total', 'total', Types::INTEGER);
|
||||
|
||||
[$select, $parameters] = $this->buildUnionQuery($context, $args);
|
||||
|
||||
// embed the union query inside a count query
|
||||
$countQuery = sprintf('SELECT COUNT(sq.id) AS total FROM (%s) as sq', $select);
|
||||
|
||||
$nq = $this->em->createNativeQuery($countQuery, $rsm);
|
||||
$nq->setParameters($parameters);
|
||||
|
||||
return $nq->getSingleScalarResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get providers by context.
|
||||
*
|
||||
* @param string $context
|
||||
*
|
||||
* @return TimelineProviderInterface[]
|
||||
*/
|
||||
public function getProvidersByContext($context)
|
||||
{
|
||||
//throw an exception if no provider have been defined for this context
|
||||
if (!array_key_exists($context, $this->providersByContext)) {
|
||||
throw new \LogicException(sprintf('No builders have been defined for "%s"'
|
||||
throw new LogicException(sprintf('No builders have been defined for "%s"'
|
||||
. ' context', $context));
|
||||
}
|
||||
|
||||
$providers = [];
|
||||
|
||||
foreach($this->providersByContext[$context] as $providerId) {
|
||||
foreach ($this->providersByContext[$context] as $providerId) {
|
||||
$providers[] = $this->providers[$providerId];
|
||||
}
|
||||
|
||||
@@ -152,11 +114,93 @@ class TimelineBuilder implements ContainerAwareInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* build the UNION query with all providers
|
||||
* return an HTML string with timeline.
|
||||
*
|
||||
* This function must be called from controller
|
||||
*
|
||||
* @example https://redmine.champs-libres.coop/projects/chillperson/repository/revisions/bd2e1b1808f73e39532e9538413025df5487cad0/entry/Controller/TimelinePersonController.php#L47 the implementation in person bundle
|
||||
*
|
||||
* @param string $context
|
||||
* @param array $args arguments defined by the bundle which create the context
|
||||
* @param int $firstItem first item number
|
||||
* @param int $number number of items by page
|
||||
*
|
||||
* @return string an HTML representation, must be included using `|raw` filter
|
||||
*/
|
||||
public function getTimelineHTML($context, array $args, $firstItem = 0, $number = 20)
|
||||
{
|
||||
[$union, $parameters] = $this->buildUnionQuery($context, $args);
|
||||
|
||||
//add ORDER BY clause and LIMIT
|
||||
$query = $union . sprintf(
|
||||
' ORDER BY date DESC LIMIT %d OFFSET %d',
|
||||
$number,
|
||||
$firstItem
|
||||
);
|
||||
|
||||
// run query and handle results
|
||||
$fetched = $this->runUnionQuery($query, $parameters);
|
||||
$entitiesByKey = $this->getEntities($fetched, $context);
|
||||
|
||||
return $this->render($fetched, $entitiesByKey, $context, $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hack to replace the arbitrary "AS" statement in DQL
|
||||
* into proper SQL query
|
||||
* TODO remove
|
||||
* private function replaceASInDQL(string $dql): string
|
||||
* {
|
||||
* $pattern = '/^(SELECT\s+[a-zA-Z0-9\_\.\']{1,}\s+)(AS [a-z0-9\_]{1,})(\s{0,},\s{0,}[a-zA-Z0-9\_\.\']{1,}\s+)(AS [a-z0-9\_]{1,})(\s{0,},\s{0,}[a-zA-Z0-9\_\.\']{1,}\s+)(AS [a-z0-9\_]{1,})(\s+FROM.*)/';
|
||||
* $replacements = '${1} AS id ${3} AS type ${5} AS date ${7}';.
|
||||
*
|
||||
* $s = \preg_replace($pattern, $replacements, $dql, 1);
|
||||
*
|
||||
* if (NULL === $s) {
|
||||
* throw new \RuntimeException('Could not replace the "AS" statement produced by '.
|
||||
* 'DQL with normal SQL AS: '.$dql);
|
||||
* }
|
||||
*
|
||||
* return $s;
|
||||
* }
|
||||
*
|
||||
* @param mixed $data
|
||||
*/
|
||||
|
||||
/**
|
||||
* return the SQL SELECT query as a string,.
|
||||
*
|
||||
* @return array: first parameter is the sql string, second an array with parameters
|
||||
*/
|
||||
private function buildSelectQuery($data): array
|
||||
{
|
||||
return [$data->buildSql(), $data->getParameters()];
|
||||
// dead code
|
||||
$parameters = [];
|
||||
|
||||
$sql = sprintf(
|
||||
'SELECT %s AS id, '
|
||||
. '%s AS "date", '
|
||||
. "'%s' AS type "
|
||||
. 'FROM %s '
|
||||
. 'WHERE %s',
|
||||
$data['id'],
|
||||
$data['date'],
|
||||
$data['type'],
|
||||
$data['FROM'],
|
||||
is_string($data['WHERE']) ? $data['WHERE'] : $data['WHERE'][0]
|
||||
);
|
||||
|
||||
return [$sql, $data['WHERE'][1]];
|
||||
}
|
||||
|
||||
/**
|
||||
* build the UNION query with all providers.
|
||||
*
|
||||
* @uses self::buildSelectQuery to build individual SELECT queries
|
||||
*
|
||||
* @throws \LogicException if no builder have been defined for this context
|
||||
* @throws LogicException if no builder have been defined for this context
|
||||
*
|
||||
* @return array, where first element is the query, the second one an array with the parameters
|
||||
*/
|
||||
private function buildUnionQuery(string $context, array $args): array
|
||||
@@ -165,10 +209,10 @@ class TimelineBuilder implements ContainerAwareInterface
|
||||
$union = '';
|
||||
$parameters = [];
|
||||
|
||||
foreach($this->getProvidersByContext($context) as $provider) {
|
||||
foreach ($this->getProvidersByContext($context) as $provider) {
|
||||
$data = $provider->fetchQuery($context, $args);
|
||||
list($select, $selectParameters) = $this->buildSelectQuery($data);
|
||||
$append = empty($union) ? $select : ' UNION '.$select;
|
||||
[$select, $selectParameters] = $this->buildSelectQuery($data);
|
||||
$append = empty($union) ? $select : ' UNION ' . $select;
|
||||
$union .= $append;
|
||||
$parameters = array_merge($parameters, $selectParameters);
|
||||
}
|
||||
@@ -177,93 +221,28 @@ class TimelineBuilder implements ContainerAwareInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Hack to replace the arbitrary "AS" statement in DQL
|
||||
* into proper SQL query
|
||||
* TODO remove
|
||||
private function replaceASInDQL(string $dql): string
|
||||
{
|
||||
$pattern = '/^(SELECT\s+[a-zA-Z0-9\_\.\']{1,}\s+)(AS [a-z0-9\_]{1,})(\s{0,},\s{0,}[a-zA-Z0-9\_\.\']{1,}\s+)(AS [a-z0-9\_]{1,})(\s{0,},\s{0,}[a-zA-Z0-9\_\.\']{1,}\s+)(AS [a-z0-9\_]{1,})(\s+FROM.*)/';
|
||||
$replacements = '${1} AS id ${3} AS type ${5} AS date ${7}';
|
||||
|
||||
$s = \preg_replace($pattern, $replacements, $dql, 1);
|
||||
|
||||
if (NULL === $s) {
|
||||
throw new \RuntimeException('Could not replace the "AS" statement produced by '.
|
||||
'DQL with normal SQL AS: '.$dql);
|
||||
}
|
||||
|
||||
return $s;
|
||||
}
|
||||
*/
|
||||
|
||||
/**
|
||||
* return the SQL SELECT query as a string,
|
||||
*
|
||||
* @return array: first parameter is the sql string, second an array with parameters
|
||||
*/
|
||||
private function buildSelectQuery($data): array
|
||||
{
|
||||
return [$data->buildSql(), $data->getParameters()];
|
||||
|
||||
// dead code
|
||||
$parameters = [];
|
||||
|
||||
$sql = sprintf(
|
||||
'SELECT %s AS id, '
|
||||
. '%s AS "date", '
|
||||
. "'%s' AS type "
|
||||
. 'FROM %s '
|
||||
. 'WHERE %s',
|
||||
$data['id'],
|
||||
$data['date'],
|
||||
$data['type'],
|
||||
$data['FROM'],
|
||||
is_string($data['WHERE']) ? $data['WHERE'] : $data['WHERE'][0]
|
||||
);
|
||||
|
||||
return [$sql, $data['WHERE'][1]];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* run the UNION query and return result as an array
|
||||
*
|
||||
* @return array an array with the results
|
||||
*/
|
||||
private function runUnionQuery(string $query, array $parameters): array
|
||||
{
|
||||
$resultSetMapping = (new ResultSetMapping())
|
||||
->addScalarResult('id', 'id')
|
||||
->addScalarResult('type', 'type')
|
||||
->addScalarResult('date', 'date');
|
||||
|
||||
return $this->em->createNativeQuery($query, $resultSetMapping)
|
||||
->setParameters($parameters)
|
||||
->getArrayResult();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param array $queriedIds
|
||||
* @param string $context
|
||||
*
|
||||
* @return array with the form array($type => [$entity, $entity, $entity])
|
||||
*/
|
||||
private function getEntities(array $queriedIds, $context)
|
||||
{
|
||||
//gather entities by type to pass all id with same type to the TimelineProvider.
|
||||
$idsByType = array();
|
||||
$idsByType = [];
|
||||
|
||||
foreach($queriedIds as $result) {
|
||||
foreach ($queriedIds as $result) {
|
||||
$idsByType[$result['type']][] = $result['id'];
|
||||
}
|
||||
|
||||
//fetch entities from providers
|
||||
$entitiesByType = array();
|
||||
$entitiesByType = [];
|
||||
|
||||
foreach ($idsByType as $type => $ids) {
|
||||
//iterate providers for current context
|
||||
foreach($this->getProvidersByContext($context) as $provider) {
|
||||
foreach ($this->getProvidersByContext($context) as $provider) {
|
||||
if ($provider->supportsType($type)) {
|
||||
$entitiesByType[$type] = $provider->getEntities($ids);
|
||||
|
||||
break; //we assume that providers have unique keys => we break the loop
|
||||
}
|
||||
}
|
||||
@@ -272,40 +251,6 @@ class TimelineBuilder implements ContainerAwareInterface
|
||||
return $entitiesByType;
|
||||
}
|
||||
|
||||
/**
|
||||
* render the timeline as HTML
|
||||
*
|
||||
* @param array $fetched
|
||||
* @param array $entitiesByType
|
||||
* @param string $context
|
||||
* @param mixed[] $args
|
||||
* @return string the HTML representation of the timeline
|
||||
*/
|
||||
private function render(array $fetched, array $entitiesByType, $context, array $args)
|
||||
{
|
||||
//add results to a pretty array
|
||||
$timelineEntries = array();
|
||||
foreach ($fetched as $result) {
|
||||
$data = $this->getTemplateData(
|
||||
$result['type'],
|
||||
$entitiesByType[$result['type']][$result['id']], //the entity
|
||||
$context,
|
||||
$args);
|
||||
|
||||
$timelineEntries[] = [
|
||||
'date' => new \DateTime($result['date']),
|
||||
'template' => $data['template'],
|
||||
'template_data' => $data['template_data']
|
||||
];
|
||||
}
|
||||
|
||||
return $this->container->get('templating')
|
||||
->render('@ChillMain/Timeline/chain_timelines.html.twig', array(
|
||||
'results' => $timelineEntries
|
||||
));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* get the template data from the provider for the given entity, by type.
|
||||
*
|
||||
@@ -313,14 +258,66 @@ class TimelineBuilder implements ContainerAwareInterface
|
||||
* @param mixed $entity
|
||||
* @param string $context
|
||||
* @param mixed[] $args
|
||||
*
|
||||
* @return array the template data fetched from the provider
|
||||
*/
|
||||
private function getTemplateData($type, $entity, $context, array $args)
|
||||
{
|
||||
foreach($this->getProvidersByContext($context) as $provider) {
|
||||
foreach ($this->getProvidersByContext($context) as $provider) {
|
||||
if ($provider->supportsType($type)) {
|
||||
return $provider->getEntityTemplate($entity, $context, $args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* render the timeline as HTML.
|
||||
*
|
||||
* @param string $context
|
||||
* @param mixed[] $args
|
||||
*
|
||||
* @return string the HTML representation of the timeline
|
||||
*/
|
||||
private function render(array $fetched, array $entitiesByType, $context, array $args)
|
||||
{
|
||||
//add results to a pretty array
|
||||
$timelineEntries = [];
|
||||
|
||||
foreach ($fetched as $result) {
|
||||
$data = $this->getTemplateData(
|
||||
$result['type'],
|
||||
$entitiesByType[$result['type']][$result['id']], //the entity
|
||||
$context,
|
||||
$args
|
||||
);
|
||||
|
||||
$timelineEntries[] = [
|
||||
'date' => new DateTime($result['date']),
|
||||
'template' => $data['template'],
|
||||
'template_data' => $data['template_data'],
|
||||
];
|
||||
}
|
||||
|
||||
return $this->container->get('templating')
|
||||
->render('@ChillMain/Timeline/chain_timelines.html.twig', [
|
||||
'results' => $timelineEntries,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* run the UNION query and return result as an array.
|
||||
*
|
||||
* @return array an array with the results
|
||||
*/
|
||||
private function runUnionQuery(string $query, array $parameters): array
|
||||
{
|
||||
$resultSetMapping = (new ResultSetMapping())
|
||||
->addScalarResult('id', 'id')
|
||||
->addScalarResult('type', 'type')
|
||||
->addScalarResult('date', 'date');
|
||||
|
||||
return $this->em->createNativeQuery($query, $resultSetMapping)
|
||||
->setParameters($parameters)
|
||||
->getArrayResult();
|
||||
}
|
||||
}
|
||||
|
@@ -1,29 +1,21 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2015 Champs-Libres Coopérative <info@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Timeline;
|
||||
|
||||
use LogicException;
|
||||
|
||||
/**
|
||||
* Interface for service providing info to timeline
|
||||
*
|
||||
* Interface for service providing info to timeline.
|
||||
*
|
||||
* Services implementing those interface must be tagged like this :
|
||||
*
|
||||
*
|
||||
* ```
|
||||
* services:
|
||||
* my_timeline:
|
||||
@@ -34,97 +26,96 @@ namespace Chill\MainBundle\Timeline;
|
||||
* # a second 'center' context :
|
||||
* - { name: timeline, context: center }
|
||||
* ```
|
||||
*
|
||||
*
|
||||
* The bundle which will call the timeline will document available context and
|
||||
* the arguments provided by the context.
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
interface TimelineProviderInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* provide data to build a SQL SELECT query to fetch entities
|
||||
*
|
||||
* provide data to build a SQL SELECT query to fetch entities.
|
||||
*
|
||||
* The TimeLineBuilder will create a full SELECT query and append
|
||||
* the query into an UNION of SELECT queries. This permit to fetch
|
||||
* all entities from different table in a single query.
|
||||
*
|
||||
*
|
||||
* The associative array MUST have the following key :
|
||||
* - `id` : the name of the id column
|
||||
* - `type`: a string to indicate the type
|
||||
* - `date`: the name of the datetime column, used to order entities by date
|
||||
* - `FROM` (in capital) : the FROM clause. May contains JOIN instructions
|
||||
*
|
||||
*
|
||||
* Those key are optional:
|
||||
* - `WHERE` (in capital) : the WHERE clause.
|
||||
*
|
||||
* - `WHERE` (in capital) : the WHERE clause.
|
||||
*
|
||||
* Where relevant, the data must be quoted to avoid SQL injection.
|
||||
*
|
||||
*
|
||||
* `$context` and `$args` are defined by the bundle which will call the timeline
|
||||
* rendering.
|
||||
*
|
||||
* rendering.
|
||||
*
|
||||
* @param string $context
|
||||
* @param mixed[] $args the argument to the context.
|
||||
*
|
||||
* @return string[]
|
||||
* @throw \LogicException if the context is not supported
|
||||
*/
|
||||
public function fetchQuery($context, array $args);
|
||||
|
||||
/**
|
||||
* Indicate if the result type may be handled by the service
|
||||
*
|
||||
* @param string $type the key present in the SELECT query
|
||||
* @return boolean
|
||||
*/
|
||||
public function supportsType($type);
|
||||
|
||||
/**
|
||||
* fetch entities from db into an associative array. The keys **MUST BE**
|
||||
* the id
|
||||
*
|
||||
* All ids returned by all SELECT queries
|
||||
* the id.
|
||||
*
|
||||
* All ids returned by all SELECT queries
|
||||
* (@see TimeLineProviderInterface::fetchQuery) and with the type
|
||||
* supported by the provider (@see TimelineProviderInterface::supportsType)
|
||||
* will be passed as argument.
|
||||
*
|
||||
*
|
||||
* @param array $ids an array of id
|
||||
*
|
||||
* @return mixed[] an associative array of entities, with id as key
|
||||
*/
|
||||
public function getEntities(array $ids);
|
||||
|
||||
|
||||
/**
|
||||
* return an associative array with argument to render the entity
|
||||
* in an html template, which will be included in the timeline page
|
||||
*
|
||||
* in an html template, which will be included in the timeline page.
|
||||
*
|
||||
* The result must have the following key :
|
||||
*
|
||||
*
|
||||
* - `template` : the template FQDN
|
||||
* - `template_data`: the data required by the template
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
*
|
||||
* ```
|
||||
* array(
|
||||
* array(
|
||||
* 'template' => 'ChillMyBundle:timeline:template.html.twig',
|
||||
* 'template_data' => array(
|
||||
* 'accompanyingPeriod' => $entity,
|
||||
* 'person' => $args['person']
|
||||
* 'accompanyingPeriod' => $entity,
|
||||
* 'person' => $args['person']
|
||||
* )
|
||||
* );
|
||||
* ```
|
||||
*
|
||||
*
|
||||
* `$context` and `$args` are defined by the bundle which will call the timeline
|
||||
* rendering.
|
||||
*
|
||||
* rendering.
|
||||
*
|
||||
* @param type $entity
|
||||
* @param type $context
|
||||
* @param array $args
|
||||
*
|
||||
* @throws LogicException if the context is not supported
|
||||
*
|
||||
* @return mixed[]
|
||||
* @throws \LogicException if the context is not supported
|
||||
*/
|
||||
public function getEntityTemplate($entity, $context, array $args);
|
||||
|
||||
|
||||
/**
|
||||
* Indicate if the result type may be handled by the service.
|
||||
*
|
||||
* @param string $type the key present in the SELECT query
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function supportsType($type);
|
||||
}
|
||||
|
@@ -1,30 +1,38 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Timeline;
|
||||
|
||||
use function strtr;
|
||||
|
||||
class TimelineSingleQuery
|
||||
{
|
||||
private ?string $id;
|
||||
|
||||
private ?string $date;
|
||||
|
||||
private ?string $key;
|
||||
|
||||
private ?string $from;
|
||||
|
||||
private ?string $where;
|
||||
|
||||
private array $parameters = [];
|
||||
|
||||
private bool $distinct = false;
|
||||
|
||||
private ?string $from;
|
||||
|
||||
private ?string $id;
|
||||
|
||||
private ?string $key;
|
||||
|
||||
private array $parameters = [];
|
||||
|
||||
private ?string $where;
|
||||
|
||||
public function __construct(
|
||||
string $id = null,
|
||||
string $date = null,
|
||||
string $key = null,
|
||||
string $from = null,
|
||||
string $where = null,
|
||||
?string $id = null,
|
||||
?string $date = null,
|
||||
?string $key = null,
|
||||
?string $from = null,
|
||||
?string $where = null,
|
||||
array $parameters = []
|
||||
) {
|
||||
$this->id = $id;
|
||||
@@ -35,6 +43,27 @@ class TimelineSingleQuery
|
||||
$this->parameters = $parameters;
|
||||
}
|
||||
|
||||
public function buildSql(): string
|
||||
{
|
||||
$parameters = [];
|
||||
|
||||
return strtr(
|
||||
'SELECT {distinct} {id} AS id, '
|
||||
. '{date} AS "date", '
|
||||
. "'{key}' AS type "
|
||||
. 'FROM {from} '
|
||||
. 'WHERE {where}',
|
||||
[
|
||||
'{distinct}' => $this->distinct ? 'DISTINCT' : '',
|
||||
'{id}' => $this->getId(),
|
||||
'{date}' => $this->getDate(),
|
||||
'{key}' => $this->getKey(),
|
||||
'{from}' => $this->getFrom(),
|
||||
'{where}' => $this->getWhere(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public static function fromArray(array $a)
|
||||
{
|
||||
return new TimelineSingleQuery(
|
||||
@@ -43,78 +72,49 @@ class TimelineSingleQuery
|
||||
$a['type'] ?? $a['key'] ?? null,
|
||||
$a['FROM'] ?? $a['from'] ?? null,
|
||||
$a['WHERE'] ?? $a['where'] ?? null,
|
||||
$a['parameters'] ?? null);
|
||||
}
|
||||
|
||||
public function getId(): string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setId(string $id): self
|
||||
{
|
||||
$this->id = $id;
|
||||
|
||||
return $this;
|
||||
$a['parameters'] ?? null
|
||||
);
|
||||
}
|
||||
|
||||
public function getDate(): string
|
||||
{
|
||||
return $this->date;
|
||||
}
|
||||
|
||||
public function setDate(string $date): self
|
||||
|
||||
public function getFrom(): string
|
||||
{
|
||||
$this->date = $date;
|
||||
|
||||
return $this;
|
||||
return $this->from;
|
||||
}
|
||||
|
||||
public function getId(): string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getKey(): string
|
||||
{
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
public function setKey(string $key): self
|
||||
{
|
||||
$this->key = $key;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getFrom(): string
|
||||
public function getParameters(): array
|
||||
{
|
||||
return $this->from;
|
||||
}
|
||||
|
||||
public function setFrom(string $from): self
|
||||
{
|
||||
$this->from = $from;
|
||||
|
||||
return $this;
|
||||
return $this->parameters;
|
||||
}
|
||||
|
||||
public function getWhere(): string
|
||||
{
|
||||
return $this->where;
|
||||
}
|
||||
|
||||
public function setWhere(string $where): self
|
||||
|
||||
public function isDistinct(): bool
|
||||
{
|
||||
$this->where = $where;
|
||||
|
||||
return $this;
|
||||
return $this->distinct;
|
||||
}
|
||||
|
||||
public function getParameters(): array
|
||||
public function setDate(string $date): self
|
||||
{
|
||||
return $this->parameters;
|
||||
}
|
||||
|
||||
public function setParameters(array $parameters): self
|
||||
{
|
||||
$this->parameters = $parameters;
|
||||
|
||||
$this->date = $date;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -125,31 +125,38 @@ class TimelineSingleQuery
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isDistinct(): bool
|
||||
public function setFrom(string $from): self
|
||||
{
|
||||
return $this->distinct;
|
||||
$this->from = $from;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function buildSql(): string
|
||||
public function setId(string $id): self
|
||||
{
|
||||
$parameters = [];
|
||||
$this->id = $id;
|
||||
|
||||
$sql = \strtr(
|
||||
'SELECT {distinct} {id} AS id, '
|
||||
. '{date} AS "date", '
|
||||
. "'{key}' AS type "
|
||||
. 'FROM {from} '
|
||||
. 'WHERE {where}',
|
||||
[
|
||||
'{distinct}' => $this->distinct ? 'DISTINCT' : '',
|
||||
'{id}' => $this->getId(),
|
||||
'{date}' => $this->getDate(),
|
||||
'{key}' => $this->getKey(),
|
||||
'{from}' => $this->getFrom(),
|
||||
'{where}' => $this->getWhere(),
|
||||
]
|
||||
);
|
||||
return $this;
|
||||
}
|
||||
|
||||
return $sql;
|
||||
public function setKey(string $key): self
|
||||
{
|
||||
$this->key = $key;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setParameters(array $parameters): self
|
||||
{
|
||||
$this->parameters = $parameters;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setWhere(string $where): self
|
||||
{
|
||||
$this->where = $where;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user