mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
exports: fix activity DateAggregator; add customs DQL Date/Time functions
This commit is contained in:
@@ -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');
|
||||
|
||||
|
Reference in New Issue
Block a user