add an export for person list

This commit is contained in:
2016-12-09 19:51:41 +01:00
parent 58398458c1
commit 60cb1e60f0
4 changed files with 241 additions and 36 deletions

View File

@@ -0,0 +1,226 @@
<?php
namespace Chill\PersonBundle\Export\Export;
use Chill\MainBundle\Export\ListInterface;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use Doctrine\ORM\Query;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Symfony\Component\Security\Core\Role\Role;
use Chill\PersonBundle\Export\Declarations;
use Chill\MainBundle\Export\FormatterInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Translation\TranslatorInterface;
/**
* Render a list of peoples
*
* @author julien
*/
class ListPerson implements ListInterface
{
/**
*
* @var EntityManagerInterface
*/
protected $entityManager;
/**
*
* @var TranslatorInterface
*/
protected $translator;
protected $fields = array(
'id', 'firstName', 'lastName', 'birthdate',
'placeOfBirth', 'gender', 'memo', 'email', 'phonenumber'
);
public function __construct(
EntityManagerInterface $em,
TranslatorInterface $translator
)
{
$this->entityManager = $em;
$this->translator = $translator;
}
/**
* {@inheritDoc}
*
* @param FormBuilderInterface $builder
*/
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('fields', ChoiceType::class, array(
'multiple' => true,
'expanded' => true,
'choices' => array_combine($this->fields, $this->fields),
'label' => 'Fields to include in export'
));
}
/**
* {@inheritDoc}
*
* @return type
*/
public function getAllowedFormattersTypes()
{
return array(FormatterInterface::TYPE_LIST);
}
/**
* {@inheritDoc}
*
* @return string
*/
public function getDescription()
{
return "Create a list of people according to various filters.";
}
/**
* {@inheritDoc}
*
* @param type $key
* @param array $values
* @param type $data
* @return type
*/
public function getLabels($key, array $values, $data)
{
switch ($key) {
case 'birthdate':
// for birthdate, we have to transform the string into a date
// to format the date correctly.
return function($value) {
if ($value === '_header') { return 'birthdate'; }
if (empty($value))
{
return "";
}
$date = \DateTime::createFromFormat('Y-m-d', $value);
// check that the creation could occurs.
if ($date === false) {
throw new \Exception(sprintf("The value %s could "
. "not be converted to %s", $value, \DateTime::class));
}
return $date->format('d-m-Y');
};
case 'gender' :
// for gender, we have to translate men/women statement
return function($value) {
if ($value === '_header') { return 'gender'; }
return $this->translator->trans($value);
};
default:
return function($value) use ($key) {
if ($value === '_header') { return \strtolower($key); }
return $value;
};
}
}
/**
* {@inheritDoc}
*
* @param type $data
* @return type
*/
public function getQueryKeys($data)
{
return $data['fields'];
}
/**
* {@inheritDoc}
*
*/
public function getResult($query, $data)
{
return $query->getQuery()->getResult(Query::HYDRATE_SCALAR);
}
/**
* {@inheritDoc}
*
* @return string
*/
public function getTitle()
{
return "List peoples";
}
/**
* {@inheritDoc}
*
*/
public function getType()
{
return Declarations::PERSON_TYPE;
}
/**
* {@inheritDoc}
*
*/
public function initiateQuery(array $requiredModifiers, array $acl, array $data = array())
{
$centers = array_map(function($el) { return $el['center']; }, $acl);
// throw an error if any fields are present
if (!\array_key_exists('fields', $data)) {
throw new \Doctrine\DBAL\Exception\InvalidArgumentException("any fields "
. "have been checked");
}
$qb = $this->entityManager->createQueryBuilder();
foreach ($this->fields as $f) {
if (in_array($f, $data['fields'])) {
$qb->addSelect(sprintf('person.%s as %s', $f, $f));
}
}
$qb
->from('ChillPersonBundle:Person', 'person')
->join('person.center', 'center')
->andWhere('center IN (:authorized_centers)')
->setParameter('authorized_centers', $centers);
;
return $qb;
}
/**
*
* {@inheritDoc}
*/
public function requiredRole()
{
return new Role(PersonVoter::STATS);
}
/**
*
* {@inheritDoc}
*/
public function supportsModifiers()
{
return array(Declarations::PERSON_TYPE);
}
}