Merge branch 'master' of git.framasoft.org:Chill-project/Chill-Person

This commit is contained in:
Julien Fastré 2016-10-06 15:12:42 +02:00
commit 89aa9a0fb4
9 changed files with 181 additions and 10 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ parameters.yml
Resources/node_modules/ Resources/node_modules/
Tests/Fixtures/App/app/config/parameters.yml Tests/Fixtures/App/app/config/parameters.yml
/nbproject/private/

View File

@ -89,6 +89,7 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac
public function prepend(ContainerBuilder $container) public function prepend(ContainerBuilder $container)
{ {
$this->prependRoleHierarchy($container); $this->prependRoleHierarchy($container);
$this->prependHomepageWidget($container);
$bundles = $container->getParameter('kernel.bundles'); $bundles = $container->getParameter('kernel.bundles');
//add ChillMain to assetic-enabled bundles //add ChillMain to assetic-enabled bundles
@ -125,6 +126,31 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac
)); ));
} }
/**
*
* Add a widget "add a person" on the homepage, automatically
*
* @param \Chill\PersonBundle\DependencyInjection\containerBuilder $container
*/
protected function prependHomepageWidget(containerBuilder $container)
{
$container->prependExtensionConfig('chill_main', array(
'widgets' => array(
'homepage' => array(
array(
'widget_alias' => 'add_person',
'order' => 2
)
)
)
));
}
/**
* Add role hierarchy.
*
* @param ContainerBuilder $container
*/
protected function prependRoleHierarchy(ContainerBuilder $container) protected function prependRoleHierarchy(ContainerBuilder $container)
{ {
$container->prependExtensionConfig('security', array( $container->prependExtensionConfig('security', array(

View File

@ -20,9 +20,6 @@ chill_person_new:
defaults: {_controller: ChillPersonBundle:Person:new } defaults: {_controller: ChillPersonBundle:Person:new }
options: options:
menus: menus:
homepage:
order: 10
label: Add a person
section: section:
order: 10 order: 10
label: Add a person label: Add a person

View File

@ -3,11 +3,12 @@ services:
class: Chill\PersonBundle\Widget\PersonListWidget class: Chill\PersonBundle\Widget\PersonListWidget
arguments: arguments:
- "@chill.person.repository.person" - "@chill.person.repository.person"
- "@doctrine.orm.entity_manager"
- "@chill.main.security.authorization.helper" - "@chill.main.security.authorization.helper"
- "@security.token_storage" - "@security.token_storage"
# this widget is defined by the PersonListWidgetFactory # this widget is defined by the PersonListWidgetFactory
chill_person.widget.add_person: chill_person.widget.add_person:
class: Chill\PersonBundle\Widget\AddPersonWidget class: Chill\PersonBundle\Widget\AddAPersonWidget
tags: tags:
- { name: chill_widget, alias: add_person, place: homepage } - { name: chill_widget, alias: add_person, place: homepage }

View File

@ -0,0 +1,8 @@
<div class="grid-8 centered" style="text-align:center; margin-top: 1.5em;">
<a href="{{ path('chill_person_new') }}">
<div class="grid-3 sc-button blue" style="float: inherit;">
{{ 'Add a person'|trans }}
</div>
</a>
</div>

View File

@ -0,0 +1,28 @@
<?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;
/**
* Add a button "add a person"
*
* @author julien
*/
class AddAPersonWidget implements WidgetInterface
{
public function render(
\Twig_Environment $env,
$place,
array $context,
array $config) {
return $env->render("ChillPersonBundle:Widget:homepage_add_a_person.html.twig");
}
}

View File

@ -28,6 +28,7 @@ use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Chill\PersonBundle\Security\Authorization\PersonVoter; use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Symfony\Component\Security\Core\Role\Role; use Symfony\Component\Security\Core\Role\Role;
use Doctrine\ORM\EntityManager;
/** /**
* add a widget with person list. * add a widget with person list.
@ -44,6 +45,13 @@ class PersonListWidget implements WidgetInterface
*/ */
protected $personRepository; protected $personRepository;
/**
* The entity manager
*
* @var EntityManager
*/
protected $entityManager;
/** /**
* the authorization helper * the authorization helper
* *
@ -65,13 +73,14 @@ class PersonListWidget implements WidgetInterface
public function __construct( public function __construct(
EntityRepository $personRepostory, EntityRepository $personRepostory,
EntityManager $em,
AuthorizationHelper $authorizationHelper, AuthorizationHelper $authorizationHelper,
TokenStorage $tokenStorage TokenStorage $tokenStorage
) { ) {
$this->personRepository = $personRepostory; $this->personRepository = $personRepostory;
$this->authorizationHelper = $authorizationHelper; $this->authorizationHelper = $authorizationHelper;
$this->tokenStorage = $tokenStorage; $this->tokenStorage = $tokenStorage;
$this->entityManager = $em;
} }
/** /**
@ -94,7 +103,7 @@ class PersonListWidget implements WidgetInterface
$qb->setParameter('centers', $centers); $qb->setParameter('centers', $centers);
// add the "only active" query
if ($config['only_active'] === true) { if ($config['only_active'] === true) {
$qb->join('person.accompanyingPeriods', 'ap'); $qb->join('person.accompanyingPeriods', 'ap');
$or = new Expr\Orx(); $or = new Expr\Orx();
@ -110,6 +119,23 @@ class PersonListWidget implements WidgetInterface
$and->add($or); $and->add($or);
$qb->setParameter('now', new \DateTime(), Type::DATE); $qb->setParameter('now', new \DateTime(), Type::DATE);
} }
if ($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); $qb->where($and);

View File

@ -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);
}

View File

@ -19,12 +19,8 @@
namespace Chill\PersonBundle\Widget; namespace Chill\PersonBundle\Widget;
use Chill\MainBundle\DependencyInjection\Widget\Factory\WidgetFactoryInterface;
use Chill\MainBundle\DependencyInjection\Widget\Factory\AbstractWidgetFactory; use Chill\MainBundle\DependencyInjection\Widget\Factory\AbstractWidgetFactory;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeBuilder; use Symfony\Component\Config\Definition\Builder\NodeBuilder;
/** /**
@ -40,6 +36,9 @@ class PersonListWidgetFactory extends AbstractWidgetFactory
$node->integerNode('number_of_items') $node->integerNode('number_of_items')
->defaultValue(50) ->defaultValue(50)
->end(); ->end();
$node->scalarNode('filtering_class')
->defaultNull()
->end();
} }