household list of accompanying periods + upgrade DBAL version to 3.1

This commit is contained in:
2021-11-08 10:57:14 +00:00
committed by Julien Fastré
parent 092ea4d57f
commit 7fabe0214e
49 changed files with 353 additions and 286 deletions

View File

@@ -22,7 +22,7 @@ namespace Chill\PersonBundle\Widget;
use Chill\MainBundle\Templating\Widget\WidgetInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\Expr;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
@@ -33,11 +33,11 @@ use Chill\CustomFieldsBundle\Entity\CustomField;
use Twig\Environment;
/**
* add a widget with person list.
*
* add a widget with person list.
*
* The configuration is defined by `PersonListWidgetFactory`
*
* If the options 'custom_fields' is used, the custom fields entity will be
*
* If the options 'custom_fields' is used, the custom fields entity will be
* queried from the db and transmitted to the view under the `customFields` variable.
*/
class PersonListWidget implements WidgetInterface
@@ -48,33 +48,33 @@ class PersonListWidget implements WidgetInterface
* @var PersonRepository
*/
protected $personRepository;
/**
* The entity manager
*
* @var EntityManager
*/
protected $entityManager;
/**
* the authorization helper
*
*
* @var AuthorizationHelper;
*/
protected $authorizationHelper;
/**
*
* @var TokenStorage
*/
protected $tokenStorage;
/**
*
* @var UserInterface
*/
protected $user;
public function __construct(
PersonRepository $personRepostory,
EntityManager $em,
@@ -88,26 +88,26 @@ class PersonListWidget implements WidgetInterface
}
/**
*
*
* @param type $place
* @param array $context
* @param array $config
* @return string
*/
public function render(Environment $env, $place, array $context, array $config)
{
{
$numberOfItems = $config['number_of_items'] ?? 20;
$qb = $this->personRepository
->createQueryBuilder('person');
// show only the person from the authorized centers
$and = $qb->expr()->andX();
$centers = $this->authorizationHelper
->getReachableCenters($this->getUser(), new Role(PersonVoter::SEE));
$and->add($qb->expr()->in('person.center', ':centers'));
$qb->setParameter('centers', $centers);
// add the "only active" query
if (\array_key_exists('only_active', $config) && $config['only_active'] === true) {
@@ -123,37 +123,37 @@ class PersonListWidget implements WidgetInterface
(new Expr())->between(':now', 'ap.openingDate', 'ap.closingDate')
);
$and->add($or);
$qb->setParameter('now', new \DateTime(), Type::DATE);
$qb->setParameter('now', new \DateTime(), Types::DATE_MUTABLE);
}
if (\array_key_exists('filtering_class', $config) && $config['filtering_class'] !== NULL) {
$filteringClass = new $config['filtering_class'];
if ( ! $filteringClass instanceof PersonListWidget\PersonFilteringInterface) {
throw new \UnexpectedValueException(sprintf("the class %s does not "
. "implements %s", $config['filtering_class'],
. "implements %s", $config['filtering_class'],
PersonListWidget\PersonFilteringInterface::class));
}
$ids = $filteringClass->getPersonIds($this->entityManager,
$ids = $filteringClass->getPersonIds($this->entityManager,
$this->getUser());
$in = (new Expr())->in('person.id', ':ids');
$and->add($in);
$qb->setParameter('ids', $ids);
}
// adding the where clause to the query
$qb->where($and);
// ordering the query by lastname, firstname
$qb->addOrderBy('person.lastName', 'ASC')
->addOrderBy('person.firstName', 'ASC');
$qb->setFirstResult(0)->setMaxResults($numberOfItems);
$persons = $qb->getQuery()->getResult();
// get some custom field when the view is overriden and we want to
// get some custom field when the view is overriden and we want to
// show some custom field in the overriden view.
$cfields = array();
if (isset($config['custom_fields'])) {
@@ -166,39 +166,39 @@ class PersonListWidget implements WidgetInterface
$cfields[$cf->getSlug()] = $cf;
}
}
}
}
return $env->render(
'ChillPersonBundle:Widget:homepage_person_list.html.twig',
array(
'persons' => $persons,
'customFields' => $cfields
)
);
}
/**
*
*
* @return UserInterface
* @throws \RuntimeException
*/
private function getUser()
{
$token = $this->tokenStorage->getToken();
if ($token === null) {
throw new \RuntimeException("the token should not be null");
}
$user = $token->getUser();
if (!$user instanceof UserInterface || $user == null) {
throw new \RuntimeException("the user should implement UserInterface. "
. "Are you logged in ?");
}
return $user;
}
}
}