mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 15:13:50 +00:00
fix folder name
This commit is contained in:
29
src/Bundle/ChillPersonBundle/Widget/AddAPersonWidget.php
Normal file
29
src/Bundle/ChillPersonBundle/Widget/AddAPersonWidget.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
namespace Chill\PersonBundle\Widget;
|
||||
|
||||
use Chill\MainBundle\Templating\Widget\WidgetInterface;
|
||||
use Twig\Environment;
|
||||
|
||||
|
||||
/**
|
||||
* Add a button "add a person"
|
||||
*
|
||||
* @author julien
|
||||
*/
|
||||
class AddAPersonWidget implements WidgetInterface
|
||||
{
|
||||
public function render(
|
||||
Environment $env,
|
||||
$place,
|
||||
array $context,
|
||||
array $config) {
|
||||
return $env->render("ChillPersonBundle:Widget:homepage_add_a_person.html.twig");
|
||||
}
|
||||
}
|
204
src/Bundle/ChillPersonBundle/Widget/PersonListWidget.php
Normal file
204
src/Bundle/ChillPersonBundle/Widget/PersonListWidget.php
Normal file
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2016 Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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 Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
|
||||
use Chill\PersonBundle\Security\Authorization\PersonVoter;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Chill\CustomFieldsBundle\Entity\CustomField;
|
||||
use Twig\Environment;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* queried from the db and transmitted to the view under the `customFields` variable.
|
||||
*/
|
||||
class PersonListWidget implements WidgetInterface
|
||||
{
|
||||
/**
|
||||
* Repository for persons
|
||||
*
|
||||
* @var EntityRepository
|
||||
*/
|
||||
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(
|
||||
EntityRepository $personRepostory,
|
||||
EntityManager $em,
|
||||
AuthorizationHelper $authorizationHelper,
|
||||
TokenStorage $tokenStorage
|
||||
) {
|
||||
$this->personRepository = $personRepostory;
|
||||
$this->authorizationHelper = $authorizationHelper;
|
||||
$this->tokenStorage = $tokenStorage;
|
||||
$this->entityManager = $em;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @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) {
|
||||
$qb->join('person.accompanyingPeriods', 'ap');
|
||||
$or = new Expr\Orx();
|
||||
// add the case where closingDate IS NULL
|
||||
$andWhenClosingDateIsNull = new Expr\Andx();
|
||||
$andWhenClosingDateIsNull->add((new Expr())->isNull('ap.closingDate'));
|
||||
$andWhenClosingDateIsNull->add((new Expr())->gte(':now', 'ap.openingDate'));
|
||||
$or->add($andWhenClosingDateIsNull);
|
||||
// add the case when closingDate is in the future
|
||||
$or->add(
|
||||
(new Expr())->between(':now', 'ap.openingDate', 'ap.closingDate')
|
||||
);
|
||||
$and->add($or);
|
||||
$qb->setParameter('now', new \DateTime(), Type::DATE);
|
||||
}
|
||||
|
||||
|
||||
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'],
|
||||
PersonListWidget\PersonFilteringInterface::class));
|
||||
}
|
||||
$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
|
||||
// show some custom field in the overriden view.
|
||||
$cfields = array();
|
||||
if (isset($config['custom_fields'])) {
|
||||
if (count($config['custom_fields']) > 0) {
|
||||
$cfs = $this->entityManager
|
||||
->getRepository('ChillCustomFieldsBundle:CustomField')
|
||||
->findBy(array('slug' => $config['custom_fields']));
|
||||
// store the custom fields in a array
|
||||
foreach($cfs as $cf) {
|
||||
$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;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
|
||||
/*
|
||||
* Copyright (C) 2016 Champs-Libres Coopérative <info@champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Chill\PersonBundle\Widget\PersonListWidget;
|
||||
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
|
||||
|
||||
/**
|
||||
* Interface to implement on classes called in configuration for
|
||||
* PersonListWidget (`person_list`), under the key `filtering_class` :
|
||||
*
|
||||
* ```
|
||||
* widgets:
|
||||
* homepage:
|
||||
* person_list:
|
||||
* # where \FQDN\To\Class implements PersonFiltering
|
||||
* class_filtering: \FQDN\To\Class
|
||||
* ```
|
||||
*
|
||||
*/
|
||||
interface PersonFilteringInterface
|
||||
{
|
||||
/**
|
||||
* Return an array of persons id to show.
|
||||
*
|
||||
* Those ids are inserted into the query like this (where ids is the array
|
||||
* returned by this class) :
|
||||
*
|
||||
* ```
|
||||
* SELECT p FROM ChillPersonBundle:Persons p
|
||||
* WHERE p.id IN (:ids)
|
||||
* AND
|
||||
* -- security/authorization statement: restraint person to authorized centers
|
||||
* p.center in :authorized_centers
|
||||
* ```
|
||||
*
|
||||
* Example of use : filtering based on custom field data :
|
||||
* ```
|
||||
|
||||
class HomepagePersonFiltering implements PersonFilteringInterface {
|
||||
|
||||
public function getPersonIds(EntityManager $em, User $user)
|
||||
{
|
||||
$rsmBuilder = new ResultSetMappingBuilder($em);
|
||||
$rsmBuilder->addScalarResult('id', 'id', Type::BIGINT);
|
||||
|
||||
$personTable = $em->getClassMetadata('ChillPersonBundle:Person')
|
||||
->getTableName();
|
||||
$personIdColumn = $em->getClassMetadata('ChillPersonBundle:Person')
|
||||
->getColumnName('id');
|
||||
$personCfDataColumn = $em->getClassMetadata('ChillPersonBundle:Person')
|
||||
->getColumnName('cfData');
|
||||
|
||||
return $em->createNativeQuery(sprintf("SELECT %s FROM %s WHERE "
|
||||
. "jsonb_exists(%s, 'school-2fb5440e-192c-11e6-b2fd-74d02b0c9b55')",
|
||||
$personIdColumn, $personTable, $personCfDataColumn), $rsmBuilder)
|
||||
->getScalarResult();
|
||||
}
|
||||
}
|
||||
* ```
|
||||
*
|
||||
* @param EntityManager $em
|
||||
* @return int[] an array of persons id to show
|
||||
*/
|
||||
public function getPersonIds(EntityManager $em, User $user);
|
||||
}
|
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2016 Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace Chill\PersonBundle\Widget;
|
||||
|
||||
use Chill\MainBundle\DependencyInjection\Widget\Factory\AbstractWidgetFactory;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
|
||||
|
||||
/**
|
||||
* add configuration for the person_list widget.
|
||||
*/
|
||||
class PersonListWidgetFactory extends AbstractWidgetFactory
|
||||
{
|
||||
public function configureOptions($place, NodeBuilder $node)
|
||||
{
|
||||
$node->booleanNode('only_active')
|
||||
->defaultTrue()
|
||||
->end();
|
||||
$node->integerNode('number_of_items')
|
||||
->defaultValue(50)
|
||||
->end();
|
||||
$node->scalarNode('filtering_class')
|
||||
->defaultNull()
|
||||
->end();
|
||||
$node->arrayNode('custom_fields')
|
||||
->prototype('scalar')->end()
|
||||
->info("Add some custom field to the view. Add the slug of some custom field"
|
||||
. " if you want to override the view and show their value in the list")
|
||||
->example(array("custom-field-slug-1", "custom-field-slug-2"))
|
||||
->requiresAtLeastOneElement()
|
||||
->end();
|
||||
}
|
||||
|
||||
public function getAllowedPlaces()
|
||||
{
|
||||
return array('homepage');
|
||||
}
|
||||
|
||||
public function getWidgetAlias()
|
||||
{
|
||||
return 'person_list';
|
||||
}
|
||||
|
||||
public function getServiceId(ContainerBuilder $containerBuilder, $place, $order, array $config)
|
||||
{
|
||||
return 'chill_person.widget.person_list';
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user