From 60cb1e60f03dbf4701104fee117a05fe515e0a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 9 Dec 2016 19:51:41 +0100 Subject: [PATCH] add an export for person list --- DependencyInjection/ChillPersonExtension.php | 1 + Export/Export/ListPerson.php | 226 +++++++++++++++++++ Resources/config/services.yml | 36 --- Resources/translations/messages.fr.yml | 14 ++ 4 files changed, 241 insertions(+), 36 deletions(-) create mode 100644 Export/Export/ListPerson.php diff --git a/DependencyInjection/ChillPersonExtension.php b/DependencyInjection/ChillPersonExtension.php index 925aa940e..4121c737b 100644 --- a/DependencyInjection/ChillPersonExtension.php +++ b/DependencyInjection/ChillPersonExtension.php @@ -55,6 +55,7 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('services.yml'); $loader->load('services/widgets.yml'); + $loader->load('services/exports.yml'); } private function handlePersonFieldsParameters(ContainerBuilder $container, $config) diff --git a/Export/Export/ListPerson.php b/Export/Export/ListPerson.php new file mode 100644 index 000000000..7a565893c --- /dev/null +++ b/Export/Export/ListPerson.php @@ -0,0 +1,226 @@ +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); + } +} diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 3f753cf96..7a96b25d1 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -70,42 +70,6 @@ services: - "@chill.main.form.data_transformer.center_transformer" tags: - { name: form.type, alias: chill_personbundle_person_creation } - - chill.person.export.export_count_person: - class: Chill\PersonBundle\Export\Export\CountPerson - arguments: - - "@doctrine.orm.entity_manager" - tags: - - { name: chill.export, alias: count_person } - - chill.person.export.filter_gender: - class: Chill\PersonBundle\Export\Filter\GenderFilter - tags: - - { name: chill.export_filter, alias: person_gender_filter } - - - chill.person.export.filter_nationality: - class: Chill\PersonBundle\Export\Filter\NationalityFilter - arguments: - - "@chill.main.helper.translatable_string" - tags: - - { name: chill.export_filter, alias: person_nationality_filter } - - chill.person.export.aggregator_nationality: - class: Chill\PersonBundle\Export\Aggregator\NationalityAggregator - arguments: - - "@chill.main.countries_repository" - - "@chill.main.helper.translatable_string" - - "@translator" - tags: - - { name: chill.export_aggregator, alias: person_nationality_aggregator } - - chill.person.export.aggregator_gender: - class: Chill\PersonBundle\Export\Aggregator\GenderAggregator - arguments: - - "@translator" - tags: - - { name: chill.export_aggregator, alias: person_gender_aggregator } chill.person.form.type.pick_person: class: Chill\PersonBundle\Form\Type\PickPersonType diff --git a/Resources/translations/messages.fr.yml b/Resources/translations/messages.fr.yml index b15e4a83e..8f6f5d82c 100644 --- a/Resources/translations/messages.fr.yml +++ b/Resources/translations/messages.fr.yml @@ -1,16 +1,26 @@ Edit: Modifier 'First name': Prénom +firstname: prénom +firstName: prénom 'Last name': Nom +lastname: nom +lastName: nom Name: Nom +id: identifiant Birthdate: 'Date de naissance' +birthdate: date de naissance 'Date of birth': 'Date de naissance' +dateOfBirth: date de naissance +dateofbirth: date de naissance 'Unknown date of birth': 'Date de naissance inconnue' Nationality: Nationalité 'Without nationality': 'Sans nationalité' Gender: Genre +gender: genre 'Creation date': 'Date d''ouverture' 'Not given': 'Non renseigné' 'Place of birth': 'Lieu de naissance' +placeOfBirth: lieu de naissance 'Country of birth': 'Pays de naissance' 'Unknown country of birth': 'Pays inconnu' 'Marital status': 'État civil' @@ -21,6 +31,7 @@ Email: 'Courrier électronique' Address: Adresse Memo: Mémo Phonenumber: 'Numéro de téléphone' +phonenumber: numéro de téléphone '{0} Born the %date% | {1} Born the %date%': '{0} Né le %date% | {1} Née le %date%' 'Spoken languages': 'Langues parlées' 'Unknown spoken languages': 'Langues parlées inconnues' @@ -128,6 +139,9 @@ Accompanyied people: Personnes accompagnées ## exports Count peoples by various parameters.: Compte le nombre de personnes en fonction de différents filtres. Count peoples: Nombre de personnes +List peoples: Liste des personnes +Create a list of people according to various filters.: Crée une liste des personnes selon différents filtres. +Fields to include in export: Champs à inclure dans l'export ## filters Filter by person gender: Filtrer par genre de la personne