mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 07:03:49 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,22 +1,30 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\ActivityBundle\Export\Aggregator;
|
||||
|
||||
use Chill\ActivityBundle\Repository\ActivityReasonCategoryRepository;
|
||||
use Chill\ActivityBundle\Repository\ActivityReasonRepository;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Chill\MainBundle\Export\AggregatorInterface;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Chill\ActivityBundle\Security\Authorization\ActivityStatsVoter;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Doctrine\ORM\Query\Expr\Join;
|
||||
use Chill\MainBundle\Export\AggregatorInterface;
|
||||
use Chill\MainBundle\Export\ExportElementValidatedInterface;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
|
||||
use Doctrine\ORM\Query\Expr\Join;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
|
||||
class ActivityReasonAggregator implements AggregatorInterface, ExportElementValidatedInterface
|
||||
{
|
||||
@@ -36,42 +44,47 @@ class ActivityReasonAggregator implements AggregatorInterface, ExportElementVali
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
}
|
||||
|
||||
public function addRole()
|
||||
{
|
||||
return new Role(ActivityStatsVoter::STATS);
|
||||
}
|
||||
|
||||
public function alterQuery(QueryBuilder $qb, $data)
|
||||
{
|
||||
// add select element
|
||||
if ($data['level'] === 'reasons') {
|
||||
if ('reasons' === $data['level']) {
|
||||
$elem = 'reasons.id';
|
||||
$alias = 'activity_reasons_id';
|
||||
} elseif ($data['level'] === 'categories') {
|
||||
} elseif ('categories' === $data['level']) {
|
||||
$elem = 'category.id';
|
||||
$alias = 'activity_categories_id';
|
||||
} else {
|
||||
throw new \RuntimeException('The data provided are not recognized.');
|
||||
throw new RuntimeException('The data provided are not recognized.');
|
||||
}
|
||||
|
||||
$qb->addSelect($elem.' as '.$alias);
|
||||
$qb->addSelect($elem . ' as ' . $alias);
|
||||
|
||||
// make a jointure only if needed
|
||||
$join = $qb->getDQLPart('join');
|
||||
|
||||
if (
|
||||
(array_key_exists('activity', $join)
|
||||
&&
|
||||
!$this->checkJoinAlreadyDefined($join['activity'], 'reasons')
|
||||
(
|
||||
array_key_exists('activity', $join)
|
||||
&& !$this->checkJoinAlreadyDefined($join['activity'], 'reasons')
|
||||
)
|
||||
OR
|
||||
(! array_key_exists('activity', $join))
|
||||
or (!array_key_exists('activity', $join))
|
||||
) {
|
||||
$qb->add(
|
||||
'join',
|
||||
[
|
||||
'activity' => new Join(Join::INNER_JOIN, 'activity.reasons', 'reasons')
|
||||
'activity' => new Join(Join::INNER_JOIN, 'activity.reasons', 'reasons'),
|
||||
],
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
// join category if necessary
|
||||
if ($alias === 'activity_categories_id') {
|
||||
if ('activity_categories_id' === $alias) {
|
||||
// add join only if needed
|
||||
if (!$this->checkJoinAlreadyDefined($qb->getDQLPart('join')['activity'], 'category')) {
|
||||
$qb->join('reasons.category', 'category');
|
||||
@@ -88,24 +101,6 @@ class ActivityReasonAggregator implements AggregatorInterface, ExportElementVali
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a join between Activity and another alias
|
||||
*
|
||||
* @param Join[] $joins
|
||||
* @param string $alias the alias to search for
|
||||
* @return boolean
|
||||
*/
|
||||
private function checkJoinAlreadyDefined(array $joins, $alias)
|
||||
{
|
||||
foreach ($joins as $join) {
|
||||
if ($join->getAlias() === $alias) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function applyOn()
|
||||
{
|
||||
return 'activity';
|
||||
@@ -118,52 +113,37 @@ class ActivityReasonAggregator implements AggregatorInterface, ExportElementVali
|
||||
ChoiceType::class,
|
||||
[
|
||||
'choices' => [
|
||||
'By reason' => 'reasons',
|
||||
'By category of reason' => 'categories'
|
||||
'By reason' => 'reasons',
|
||||
'By category of reason' => 'categories',
|
||||
],
|
||||
'multiple' => false,
|
||||
'expanded' => true,
|
||||
'label' => "Reason's level"
|
||||
'label' => "Reason's level",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function validateForm($data, ExecutionContextInterface $context)
|
||||
{
|
||||
if ($data['level'] === null) {
|
||||
$context
|
||||
->buildViolation("The reasons's level should not be empty.")
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
|
||||
public function getTitle()
|
||||
{
|
||||
return 'Aggregate by activity reason';
|
||||
}
|
||||
|
||||
public function addRole()
|
||||
{
|
||||
return new Role(ActivityStatsVoter::STATS);
|
||||
}
|
||||
|
||||
public function getLabels($key, array $values, $data)
|
||||
{
|
||||
// for performance reason, we load data from db only once
|
||||
switch ($data['level']) {
|
||||
case 'reasons':
|
||||
$this->activityReasonRepository->findBy(['id' => $values]);
|
||||
|
||||
break;
|
||||
|
||||
case 'categories':
|
||||
$this->activityReasonCategoryRepository->findBy(['id' => $values]);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \RuntimeException(sprintf("The level data '%s' is invalid.", $data['level']));
|
||||
throw new RuntimeException(sprintf("The level data '%s' is invalid.", $data['level']));
|
||||
}
|
||||
|
||||
return function($value) use ($data) {
|
||||
if ($value === '_header') {
|
||||
return $data['level'] === 'reasons' ? 'Group by reasons' : 'Group by categories of reason';
|
||||
return function ($value) use ($data) {
|
||||
if ('_header' === $value) {
|
||||
return 'reasons' === $data['level'] ? 'Group by reasons' : 'Group by categories of reason';
|
||||
}
|
||||
|
||||
switch ($data['level']) {
|
||||
@@ -171,32 +151,63 @@ class ActivityReasonAggregator implements AggregatorInterface, ExportElementVali
|
||||
$r = $this->activityReasonRepository->find($value);
|
||||
|
||||
return sprintf(
|
||||
"%s > %s",
|
||||
'%s > %s',
|
||||
$this->translatableStringHelper->localize($r->getCategory()->getName()),
|
||||
$this->translatableStringHelper->localize($r->getName())
|
||||
);
|
||||
|
||||
case 'categories':
|
||||
$c = $this->activityReasonCategoryRepository->find($value);
|
||||
|
||||
return $this->translatableStringHelper->localize($c->getName());
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
public function getQueryKeys($data)
|
||||
{
|
||||
// add select element
|
||||
if ($data['level'] === 'reasons') {
|
||||
return array('activity_reasons_id');
|
||||
if ('reasons' === $data['level']) {
|
||||
return ['activity_reasons_id'];
|
||||
}
|
||||
|
||||
if ($data['level'] === 'categories') {
|
||||
return array ('activity_categories_id');
|
||||
if ('categories' === $data['level']) {
|
||||
return ['activity_categories_id'];
|
||||
}
|
||||
|
||||
throw new \RuntimeException('The data provided are not recognised.');
|
||||
|
||||
throw new RuntimeException('The data provided are not recognised.');
|
||||
}
|
||||
|
||||
public function getTitle()
|
||||
{
|
||||
return 'Aggregate by activity reason';
|
||||
}
|
||||
|
||||
public function validateForm($data, ExecutionContextInterface $context)
|
||||
{
|
||||
if (null === $data['level']) {
|
||||
$context
|
||||
->buildViolation("The reasons's level should not be empty.")
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a join between Activity and another alias.
|
||||
*
|
||||
* @param Join[] $joins
|
||||
* @param string $alias the alias to search for
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function checkJoinAlreadyDefined(array $joins, $alias)
|
||||
{
|
||||
foreach ($joins as $join) {
|
||||
if ($join->getAlias() === $alias) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@@ -1,26 +1,34 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\ActivityBundle\Export\Aggregator;
|
||||
|
||||
use Chill\ActivityBundle\Repository\ActivityTypeRepository;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Chill\MainBundle\Export\AggregatorInterface;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Chill\ActivityBundle\Security\Authorization\ActivityStatsVoter;
|
||||
use Chill\MainBundle\Export\AggregatorInterface;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelperInterface;
|
||||
use Closure;
|
||||
use Doctrine\ORM\Query\Expr\Join;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
|
||||
class ActivityTypeAggregator implements AggregatorInterface
|
||||
{
|
||||
public const KEY = 'activity_type_aggregator';
|
||||
|
||||
protected ActivityTypeRepository $activityTypeRepository;
|
||||
|
||||
protected TranslatableStringHelperInterface $translatableStringHelper;
|
||||
|
||||
public const KEY = 'activity_type_aggregator';
|
||||
|
||||
public function __construct(
|
||||
ActivityTypeRepository $activityTypeRepository,
|
||||
TranslatableStringHelperInterface $translatableStringHelper
|
||||
@@ -29,6 +37,11 @@ class ActivityTypeAggregator implements AggregatorInterface
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
}
|
||||
|
||||
public function addRole()
|
||||
{
|
||||
return new Role(ActivityStatsVoter::STATS);
|
||||
}
|
||||
|
||||
public function alterQuery(QueryBuilder $qb, $data)
|
||||
{
|
||||
// add select element
|
||||
@@ -38,24 +51,6 @@ class ActivityTypeAggregator implements AggregatorInterface
|
||||
$qb->addGroupBy(self::KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a join between Activity and another alias
|
||||
*
|
||||
* @param Join[] $joins
|
||||
* @param string $alias the alias to search for
|
||||
* @return boolean
|
||||
*/
|
||||
private function checkJoinAlreadyDefined(array $joins, $alias)
|
||||
{
|
||||
foreach ($joins as $join) {
|
||||
if ($join->getAlias() === $alias) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function applyOn()
|
||||
{
|
||||
return 'activity';
|
||||
@@ -66,23 +61,13 @@ class ActivityTypeAggregator implements AggregatorInterface
|
||||
// no form required for this aggregator
|
||||
}
|
||||
|
||||
public function getTitle()
|
||||
{
|
||||
return 'Aggregate by activity type';
|
||||
}
|
||||
|
||||
public function addRole()
|
||||
{
|
||||
return new Role(ActivityStatsVoter::STATS);
|
||||
}
|
||||
|
||||
public function getLabels($key, array $values, $data): \Closure
|
||||
public function getLabels($key, array $values, $data): Closure
|
||||
{
|
||||
// for performance reason, we load data from db only once
|
||||
$this->activityTypeRepository->findBy(['id' => $values]);
|
||||
|
||||
return function($value): string {
|
||||
if ($value === '_header') {
|
||||
return function ($value): string {
|
||||
if ('_header' === $value) {
|
||||
return 'Activity type';
|
||||
}
|
||||
|
||||
@@ -97,4 +82,27 @@ class ActivityTypeAggregator implements AggregatorInterface
|
||||
return [self::KEY];
|
||||
}
|
||||
|
||||
public function getTitle()
|
||||
{
|
||||
return 'Aggregate by activity type';
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a join between Activity and another alias.
|
||||
*
|
||||
* @param Join[] $joins
|
||||
* @param string $alias the alias to search for
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function checkJoinAlreadyDefined(array $joins, $alias)
|
||||
{
|
||||
foreach ($joins as $join) {
|
||||
if ($join->getAlias() === $alias) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@@ -1,13 +1,21 @@
|
||||
<?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\ActivityBundle\Export\Aggregator;
|
||||
|
||||
use Chill\MainBundle\Repository\UserRepository;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Chill\MainBundle\Export\AggregatorInterface;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Chill\ActivityBundle\Security\Authorization\ActivityStatsVoter;
|
||||
use Chill\MainBundle\Export\AggregatorInterface;
|
||||
use Chill\MainBundle\Repository\UserRepository;
|
||||
use Closure;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
|
||||
class ActivityUserAggregator implements AggregatorInterface
|
||||
{
|
||||
@@ -45,13 +53,13 @@ class ActivityUserAggregator implements AggregatorInterface
|
||||
// nothing to add
|
||||
}
|
||||
|
||||
public function getLabels($key, $values, $data): \Closure
|
||||
public function getLabels($key, $values, $data): Closure
|
||||
{
|
||||
// preload users at once
|
||||
$this->userRepository->findBy(['id' => $values]);
|
||||
|
||||
return function($value) {
|
||||
if ($value === '_header') {
|
||||
return function ($value) {
|
||||
if ('_header' === $value) {
|
||||
return 'activity user';
|
||||
}
|
||||
|
||||
@@ -61,7 +69,7 @@ class ActivityUserAggregator implements AggregatorInterface
|
||||
|
||||
public function getQueryKeys($data)
|
||||
{
|
||||
return [ self::KEY ];
|
||||
return [self::KEY];
|
||||
}
|
||||
|
||||
public function getTitle(): string
|
||||
|
Reference in New Issue
Block a user