add ListPerson

Add / complete list of person.

Some SQL function are added to allow to get the last address at a given date.
This commit is contained in:
2017-01-31 12:18:07 +01:00
parent 045119d61f
commit 25cad2f11d
21 changed files with 747 additions and 270 deletions

View File

@@ -15,7 +15,10 @@ use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Chill\PersonBundle\Entity\Person;
use Chill\CustomFieldsBundle\Entity\CustomField;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\CustomFieldsBundle\Service\CustomFieldProvider;
/**
* Render a list of peoples
@@ -37,26 +40,35 @@ class ListPerson implements ListInterface
protected $translator;
/**
*
*
* @var TranslatableStringHelper
*/
protected $translatableStringHelper;
/**
*
* @var CustomFieldProvider
*/
protected $customFieldProvider;
protected $fields = array(
'id', 'firstName', 'lastName', 'birthdate',
'placeOfBirth', 'gender', 'memo', 'email', 'phonenumber',
'countryOfBirth', 'nationality'
'countryOfBirth', 'nationality', 'address_street_address_1',
'address_street_address_2', 'address_valid_from', 'address_postcode_label',
'address_postcode_code', 'address_country_name', 'address_country_code'
);
public function __construct(
EntityManagerInterface $em,
TranslatorInterface $translator,
TranslatableStringHelper $translatableStringHelper
)
{
TranslatableStringHelper $translatableStringHelper,
CustomFieldProvider $customFieldProvider
) {
$this->entityManager = $em;
$this->translator = $translator;
$this->translatableStringHelper = $translatableStringHelper;
$this->customFieldProvider = $customFieldProvider;
}
/**
@@ -66,11 +78,21 @@ class ListPerson implements ListInterface
*/
public function buildForm(FormBuilderInterface $builder)
{
$choices = array_combine($this->fields, $this->fields);
foreach ($this->getCustomFields() as $cf) {
$choices
[$this->translatableStringHelper->localize($cf->getName())]
=
$cf->getSlug();
}
$builder->add('fields', ChoiceType::class, array(
'multiple' => true,
'expanded' => true,
'choices' => array_combine($this->fields, $this->fields),
'choices' => $choices,
'choices_as_values' => true,
'label' => 'Fields to include in export',
'constraints' => [new Callback(array(
'callback' => function($selected, ExecutionContextInterface $context) {
@@ -84,6 +106,25 @@ class ListPerson implements ListInterface
));
}
/**
* Get custom fields associated with person
*
* @return CustomField[]
*/
private function getCustomFields()
{
return $this->entityManager
->createQuery("SELECT cf "
. "FROM ChillCustomFieldsBundle:CustomField cf "
. "JOIN cf.customFieldGroup g "
. "WHERE cf.type != :title AND g.entity LIKE :entity")
->setParameters(array(
'title' => 'title',
'entity' => \addcslashes(Person::class, "\\")
))
->getResult();
}
/**
* {@inheritDoc}
@@ -163,13 +204,42 @@ class ListPerson implements ListInterface
return $this->translatableStringHelper->localize(
$country->getName());
};
default:
case 'address_country_name':
return function($value) use ($key) {
if ($value === '_header') { return \strtolower($key); }
if ($value === '_header') { return \strtolower($key); }
return $value;
if ($value === NULL) {
return '';
}
return $this->translatableStringHelper->localize(json_decode($value, true));
};
default:
// for fields which are associated with person
if (in_array($key, $this->fields)) {
return function($value) use ($key) {
if ($value === '_header') { return \strtolower($key); }
return $value;
};
} else {
// for fields which are custom fields
/* @var $cf CustomField */
$cf = $this->entityManager
->getRepository(CustomField::class)
->findOneBy(array('slug' => $this->DQLToSlug($key)));
return function($value) use ($cf) {
if ($value === '_header') {
return $this->translatableStringHelper->localize($cf->getName());
}
return $this->customFieldProvider
->getCustomFieldByType($cf->getType())
->render(json_decode($value, true), $cf, 'csv');
};
}
}
}
@@ -182,7 +252,34 @@ class ListPerson implements ListInterface
*/
public function getQueryKeys($data)
{
return $data['fields'];
$fields = array();
foreach ($data['fields'] as $key) {
if (in_array($key, $this->fields)) {
$fields[] = $key;
} else {
// this should be a slug from custom field, we have to clean it
$fields[] = $this->slugToDQL($key);
}
}
return $fields;
}
/**
* clean a slug to be usable by DQL
*
* @param string $slugsanitize
* @return string
*/
private function slugToDQL($slug)
{
return "cf____".\str_replace("-", "____", $slug);
}
private function DQLToSlug($cleanedSlug)
{
return \str_replace("____", "-", \substr($cleanedSlug, 6));
}
/**
@@ -236,12 +333,38 @@ class ListPerson implements ListInterface
case 'nationality':
$qb->addSelect(sprintf('IDENTITY(person.%s) as %s', $f, $f));
break;
case 'address_street_address_1':
case 'address_street_address_2':
case 'address_valid_from':
case 'address_postcode_label':
case 'address_postcode_code':
case 'address_country_name':
case 'address_country_code':
$qb->addSelect(sprintf(
'GET_PERSON_ADDRESS_%s(person.id, :address_date) AS %s',
// get the part after address_
strtoupper(substr($f, 8)),
$f));
$qb->setParameter('address_date', new \DateTime());
break;
default:
$qb->addSelect(sprintf('person.%s as %s', $f, $f));
}
}
}
foreach ($this->getCustomFields() as $cf) {
if (in_array($cf->getSlug(), $data['fields'])) {
$slug = $this->slugToDQL($cf->getSlug());
$qb->addSelect(
sprintf('GET_JSON_FIELD_BY_KEY(person.cFData, :slug%s) AS %s',
$slug, $slug));
$qb->setParameter(sprintf('slug%s', $slug), $cf->getSlug());
//$qb->setParameter(sprintf('name%s', $slug), $cf->getSlug());
}
}
$qb
->from('ChillPersonBundle:Person', 'person')
->join('person.center', 'center')