mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
exports: improve activity DateAggregator
- put orderby at the end - addSelect with TO_CHAR() function (cancel EXRACT()) - uniformize addSelect cases - orderBy with variable - improve getLabels
This commit is contained in:
parent
f5baa2c152
commit
b5139ec460
@ -34,31 +34,26 @@ class DateAggregator implements AggregatorInterface
|
|||||||
public function getLabels($key, array $values, $data)
|
public function getLabels($key, array $values, $data)
|
||||||
{
|
{
|
||||||
return function ($value) use ($data): string {
|
return function ($value) use ($data): string {
|
||||||
|
if ($value === '_header') {
|
||||||
|
return 'by '. $data['frequency'];
|
||||||
|
}
|
||||||
switch ($data['frequency']) {
|
switch ($data['frequency']) {
|
||||||
case 'month':
|
case 'month':
|
||||||
if ($value === '_header') {
|
|
||||||
return 'by month';
|
|
||||||
}
|
|
||||||
$month = \DateTime::createFromFormat('!m', $value);
|
$month = \DateTime::createFromFormat('!m', $value);
|
||||||
return
|
return sprintf(
|
||||||
sprintf("%02d", $value) .'/'.
|
"%02d (%s)",
|
||||||
$month->format('F') // TODO translation ?!?
|
$value,
|
||||||
;
|
$month->format('M')
|
||||||
|
);
|
||||||
|
|
||||||
case 'week':
|
case 'week':
|
||||||
if ($value === '_header') {
|
//return $this->translator->trans('for week') .' '. $value ;
|
||||||
return 'by week';
|
|
||||||
}
|
|
||||||
return $this->translator->trans('for week') .' '. $value ;
|
|
||||||
|
|
||||||
case 'year':
|
case 'year':
|
||||||
if ($value === '_header') {
|
//return $this->translator->trans('in year') .' '. $value ;
|
||||||
return 'by year';
|
|
||||||
}
|
|
||||||
return $this->translator->trans('in year') .' '. $value ;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new RuntimeException(sprintf('The value %s is not valid', $value));
|
return $value;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -91,34 +86,24 @@ class DateAggregator implements AggregatorInterface
|
|||||||
|
|
||||||
public function alterQuery(QueryBuilder $qb, $data)
|
public function alterQuery(QueryBuilder $qb, $data)
|
||||||
{
|
{
|
||||||
|
$order = null;
|
||||||
|
|
||||||
switch ($data['frequency']) {
|
switch ($data['frequency']) {
|
||||||
case 'month':
|
case 'month':
|
||||||
$qb
|
$fmt = 'MM'; break;
|
||||||
//->addSelect("TO_CHAR(activity.date,'Mon') AS MON")
|
|
||||||
->addSelect('EXTRACT(month FROM activity.date) AS date_aggregator')
|
|
||||||
//->orderBy('date_aggregator')
|
|
||||||
;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'week':
|
case 'week':
|
||||||
$qb
|
$fmt = 'IW'; break;
|
||||||
->addSelect("TO_CHAR(activity.date, 'IW') AS date_aggregator")
|
|
||||||
//->orderBy('date_aggregator')
|
|
||||||
;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'year':
|
case 'year':
|
||||||
$qb
|
$fmt = 'YYYY'; $order = 'DESC'; break;
|
||||||
//->addSelect("TO_CHAR(activity.date, 'YYYY') AS date_aggregator")
|
|
||||||
->addSelect('EXTRACT(year FROM activity.date) AS date_aggregator')
|
|
||||||
//->orderBy('date_aggregator', 'ASC')
|
|
||||||
;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new RuntimeException(sprintf("The frequency data '%s' is invalid.", $data['frequency']));
|
throw new RuntimeException(sprintf("The frequency data '%s' is invalid.", $data['frequency']));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$qb->addSelect(sprintf("TO_CHAR(activity.date, '%s') AS date_aggregator", $fmt));
|
||||||
|
|
||||||
$groupBy = $qb->getDQLPart('groupBy');
|
$groupBy = $qb->getDQLPart('groupBy');
|
||||||
|
|
||||||
if (!empty($groupBy)) {
|
if (!empty($groupBy)) {
|
||||||
@ -126,6 +111,14 @@ class DateAggregator implements AggregatorInterface
|
|||||||
} else {
|
} else {
|
||||||
$qb->groupBy('date_aggregator');
|
$qb->groupBy('date_aggregator');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$orderBy = $qb->getDQLPart('orderBy');
|
||||||
|
|
||||||
|
if (!empty($orderBy)) {
|
||||||
|
$qb->addOrderBy('date_aggregator', $order);
|
||||||
|
} else {
|
||||||
|
$qb->orderBy('date_aggregator', $order);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function applyOn(): string
|
public function applyOn(): string
|
||||||
|
Loading…
x
Reference in New Issue
Block a user