exports: fix activity DateAggregator; add customs DQL Date/Time functions

This commit is contained in:
Mathieu Jaumotte 2022-08-24 15:44:18 +02:00
parent 52902e905a
commit 7173e4be4a
5 changed files with 145 additions and 16 deletions

View File

@ -8,6 +8,7 @@ use Chill\ActivityBundle\Export\Declarations;
use Chill\MainBundle\Export\AggregatorInterface;
use Closure;
use Doctrine\ORM\QueryBuilder;
use RuntimeException;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
@ -16,7 +17,7 @@ class DateAggregator implements AggregatorInterface
{
private const CHOICES = [
'by month' => 'month',
'by week' => 'week', // numéro de la semaine
'by week' => 'week',
'by year' => 'year',
];
@ -32,22 +33,32 @@ class DateAggregator implements AggregatorInterface
public function getLabels($key, array $values, $data)
{
return function ($value): string {
switch ($value) {
case '_header':
return 'By date';
return function ($value) use ($data): string {
switch ($data['frequency']) {
case 'month':
return $this->translator->trans('by month');
if ($value === '_header') {
return 'by month';
}
$month = \DateTime::createFromFormat('!m', $value);
return
sprintf("%02d", $value) .'/'.
$month->format('F') // TODO translation ?!?
;
case 'week':
return $this->translator->trans('by week');
if ($value === '_header') {
return 'by week';
}
return $this->translator->trans('for week') .' '. $value ;
case 'year':
return $this->translator->trans('by year');
if ($value === '_header') {
return 'by year';
}
return $this->translator->trans('in year') .' '. $value ;
default:
throw new LogicException(sprintf('The value %s is not valid', $value));
throw new RuntimeException(sprintf('The value %s is not valid', $value));
}
};
}
@ -59,8 +70,7 @@ class DateAggregator implements AggregatorInterface
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('by_date', ChoiceType::class, [
'label' => 'Frequency',
$builder->add('frequency', ChoiceType::class, [
'choices' => self::CHOICES,
'multiple' => false,
'expanded' => true,
@ -81,7 +91,33 @@ class DateAggregator implements AggregatorInterface
public function alterQuery(QueryBuilder $qb, $data)
{
$qb->addSelect('activity.date AS date_aggregator');
switch ($data['frequency']) {
case 'month':
$qb
//->addSelect("TO_CHAR(activity.date,'Mon') AS MON")
->addSelect('EXTRACT(month FROM activity.date) AS date_aggregator')
//->orderBy('date_aggregator')
;
break;
case 'week':
$qb
->addSelect("TO_CHAR(activity.date, 'IW') AS date_aggregator")
//->orderBy('date_aggregator')
;
break;
case 'year':
$qb
//->addSelect("TO_CHAR(activity.date, 'YYYY') AS date_aggregator")
->addSelect('EXTRACT(year FROM activity.date) AS date_aggregator')
//->orderBy('date_aggregator', 'ASC')
;
break;
default:
throw new RuntimeException(sprintf("The frequency data '%s' is invalid.", $data['frequency']));
}
$groupBy = $qb->getDQLPart('groupBy');

View File

@ -289,9 +289,11 @@ Aggregate by activity reason: Grouper les activités par sujet
Group activity by locationtype: Grouper les activités par type de localisation
Group activity by date: Grouper les activités par date
Frequency: Fréquence
by month: par mois
by week: par semaine
by year: par année
by month: Par mois
by week: Par semaine
for week: Semaine
by year: Par année
in year: En
Group activity by linked users: Grouper les activités par TMS impliqué
Group activity by linked thirdparties: Grouper les activités par tiers impliqué
Accepted thirdparty: Tiers impliqué

View File

@ -22,6 +22,7 @@ use Chill\MainBundle\Controller\UserController;
use Chill\MainBundle\Controller\UserJobApiController;
use Chill\MainBundle\Controller\UserJobController;
use Chill\MainBundle\DependencyInjection\Widget\Factory\WidgetFactoryInterface;
use Chill\MainBundle\Doctrine\DQL\Extract;
use Chill\MainBundle\Doctrine\DQL\GetJsonFieldByKey;
use Chill\MainBundle\Doctrine\DQL\JsonAggregate;
use Chill\MainBundle\Doctrine\DQL\JsonbArrayLength;
@ -31,6 +32,7 @@ use Chill\MainBundle\Doctrine\DQL\Replace;
use Chill\MainBundle\Doctrine\DQL\Similarity;
use Chill\MainBundle\Doctrine\DQL\STContains;
use Chill\MainBundle\Doctrine\DQL\StrictWordSimilarityOPS;
use Chill\MainBundle\Doctrine\DQL\ToChar;
use Chill\MainBundle\Doctrine\DQL\Unaccent;
use Chill\MainBundle\Doctrine\ORM\Hydration\FlatHierarchyEntityHydrator;
use Chill\MainBundle\Doctrine\Type\NativeDateIntervalType;
@ -238,6 +240,10 @@ class ChillMainExtension extends Extension implements
'ST_CONTAINS' => STContains::class,
'JSONB_ARRAY_LENGTH' => JsonbArrayLength::class,
],
'datetime_functions' => [
'EXTRACT' => Extract::class,
'TO_CHAR' => ToChar::class,
],
],
'hydrators' => [
'chill_flat_hierarchy_list' => FlatHierarchyEntityHydrator::class,

View File

@ -0,0 +1,47 @@
<?php
namespace Chill\MainBundle\Doctrine\DQL;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\AST\PathExpression;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
/**
* Extract postgresql function
* https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
*
* Usage : EXTRACT(field FROM value)
*/
class Extract extends FunctionNode
{
private string $field;
private PathExpression $value;
public function getSql(SqlWalker $sqlWalker)
{
return sprintf(
'EXTRACT(%s FROM %s)',
$this->field,
$this->value->dispatch($sqlWalker)
);
}
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$parser->match(Lexer::T_IDENTIFIER);
$this->field = $parser->getLexer()->token['value'];
$parser->match(Lexer::T_FROM);
$this->value = $parser->ScalarExpression();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}

View File

@ -0,0 +1,38 @@
<?php
namespace Chill\MainBundle\Doctrine\DQL;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
/**
* Usage : TO_CHAR(datetime, fmt)
*/
class ToChar extends FunctionNode
{
private $datetime;
private $fmt;
public function getSql(SqlWalker $sqlWalker)
{
return sprintf(
'TO_CHAR(%s, %s)',
$sqlWalker->walkArithmeticPrimary($this->datetime),
$sqlWalker->walkArithmeticPrimary($this->fmt)
);
}
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->datetime = $parser->ArithmeticExpression();
$parser->match(Lexer::T_COMMA);
$this->fmt = $parser->StringExpression();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}