adapt import person script: add email, mobilenumber, gender. allow empties values cases

This commit is contained in:
Tchama 2019-07-05 16:31:32 +02:00
parent 104181ba71
commit d4262d0b28

View File

@ -38,44 +38,39 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\Event;
/**
*
* Class ImportPeopleFromCSVCommand
*
* @package Chill\PersonBundle\Command
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class ImportPeopleFromCSVCommand extends ContainerAwareCommand
{
/**
*
* @var InputInterface
*/
protected $input;
/**
*
* @var OutputInterface
*/
protected $output;
/**
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
*
* @var \Chill\MainBundle\Templating\TranslatableStringHelper
*/
protected $helper;
/**
*
* @var \Doctrine\Common\Persistence\ObjectManager
*/
protected $em;
/**
*
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
@ -88,13 +83,11 @@ class ImportPeopleFromCSVCommand extends ContainerAwareCommand
protected $line;
/**
*
* @var array where key are column names, and value the custom field slug
*/
protected $customFieldMapping = array();
/**
*
* @var CustomFieldProvider
*/
protected $customFieldProvider;
@ -112,10 +105,13 @@ class ImportPeopleFromCSVCommand extends ContainerAwareCommand
['firstname', 'The column header for firstname', 'firstname'],
['lastname', 'The column header for lastname', 'lastname'],
['birthdate', 'The column header for birthdate', 'birthdate'],
['gender', 'The column header for gender', 'gender'],
['opening_date', 'The column header for opening date', 'opening_date'],
['closing_date', 'The column header for closing date', 'closing_date'],
['memo', 'The column header for memo', 'memo'],
['email', 'The column header for email', 'email'],
['phonenumber', 'The column header for phonenumber', 'phonenumber'],
['mobilenumber', 'The column header for mobilenumber', 'mobilenumber'],
['street1', 'The column header for street 1', 'street1'],
['postalcode', 'The column header for postal code', 'postalcode'],
['locality', 'The column header for locality', 'locality'],
@ -144,8 +140,10 @@ class ImportPeopleFromCSVCommand extends ContainerAwareCommand
parent::__construct('chill:person:import');
}
/**
*
*/
protected function configure()
{
$this
@ -256,7 +254,10 @@ EOF
return $this;
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
// preparing the basic
@ -286,6 +287,9 @@ EOF
$this->loadAnswerMatching();
}
/**
* @param $row
*/
protected function matchColumnToCustomField($row)
{
@ -359,6 +363,9 @@ EOF
}
}
/**
*
*/
protected function dumpAnswerMatching()
{
if ($this->input->hasOption('dump-choice-matching') && !empty($this->input->getOption('dump-choice-matching'))) {
@ -372,6 +379,12 @@ EOF
}
}
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|null|void
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
@ -499,10 +512,11 @@ EOF
}
/**
*
*
* @param array $row
* @param array $headers the processed header : an array as prepared by self::processingHeaders
* @return Person
* @throws \Exception
*/
protected function createPerson($row, $headers)
{
@ -534,6 +548,9 @@ EOF
case 'birthdate':
$this->processBirthdate($person, $value);
break;
case 'gender':
$person->setGender($value);
break;
case 'opening_date':
// we have processed this when creating the person object, skipping;
break;
@ -543,9 +560,15 @@ EOF
case 'memo':
$person->setMemo($value);
break;
case 'email':
$person->setEmail($value);
break;
case 'phonenumber':
$person->setPhonenumber($value);
break;
case 'mobilenumber':
$person->setMobilenumber($value);
break;
// we just keep the column number for those data
case 'postalcode':
@ -563,26 +586,34 @@ EOF
// handle address
if (\in_array('postalcode', $headers)) {
$address = new Address();
$postalCode = $this->guessPostalCode($postalCodeValue, $localityValue ?? '');
if ($postalCode === null) {
throw new \Exception("The locality is not found");
if (! empty($postalCodeValue)) {
$address = new Address();
$postalCode = $this->guessPostalCode($postalCodeValue, $localityValue ?? '');
if ($postalCode === null) {
throw new \Exception("The locality is not found");
}
$address->setPostcode($postalCode);
if (\in_array('street1', $headers)) {
$address->setStreetAddress1($street1Value);
}
$address->setValidFrom(new \DateTime('today'));
$person->addAddress($address);
}
$address->setPostcode($postalCode);
if (\in_array('street1', $headers)) {
$address->setStreetAddress1($street1Value);
}
$address->setValidFrom(new \DateTime('today'));
$person->addAddress($address);
}
return $person;
}
/**
* @param $row
* @param $headers
* @return Center|mixed|null|object
*/
protected function getCenter($row, $headers)
{
if ($this->input->hasOption('force-center') && !empty($this->input->getOption('force-center'))) {
@ -606,6 +637,10 @@ EOF
}
}
/**
* @param $centerName
* @return Center|mixed|null|object
*/
protected function guessCenter($centerName)
{
if (!\array_key_exists('_center_picked', $this->cacheAnswersMapping)) {
@ -673,6 +708,11 @@ EOF
return $center;
}
/**
* @param $postalCode
* @param $locality
* @return mixed|null
*/
protected function guessPostalCode($postalCode, $locality)
{
if (!\array_key_exists('_postal_code_picked', $this->cacheAnswersMapping)) {
@ -738,9 +778,15 @@ EOF
return $pc;
}
/**
* @param Person $person
* @param $value
* @throws \Exception
*/
protected function processBirthdate(Person $person, $value)
{
if (empty($value)) { return; }
$date = $this->processDate($value, $this->input->getOption('birthdate_format'));
if ($date instanceof \DateTime) {
@ -763,8 +809,15 @@ EOF
}
/**
* @param Person $person
* @param $value
* @throws \Exception
*/
protected function processClosingDate(Person $person, $value)
{
if (empty($value)) { return; }
// we skip if the opening date is now (or after yesterday)
/* @var $period \Chill\PersonBundle\Entity\AccompanyingPeriod */
$period = $person->getCurrentAccompanyingPeriod();
@ -797,6 +850,11 @@ EOF
$value));
}
/**
* @param Person $person
* @param $row
* @throws \Exception
*/
protected function processingCustomFields(Person $person, $row)
{
/* @var $factory \Symfony\Component\Form\FormFactory */
@ -836,9 +894,10 @@ EOF
/**
* Process a text type on a custom field
*
*
* @param type $value
* @param \Chill\PersonBundle\Command\Symfony\Component\Form\FormInterface $form
* @param \Symfony\Component\Form\FormInterface $form
* @return type
*/
protected function processTextType(
$value,
@ -861,14 +920,15 @@ EOF
/**
* Process a custom field choice.
*
*
* The method try to guess if the result exists amongst the text of the possible
* choices. If the texts exists, then this is picked. Else, ask the user.
*
*
* @param string $value
* @param \Symfony\Component\Form\FormInterface $form
* @param \Chill\CustomFieldsBundle\Entity\CustomField $cf
* @return string
* @throws \Exception
*/
protected function processChoiceType(
$value,
@ -982,11 +1042,12 @@ EOF
}
/**
* Recursive method to collect the possibles answer from a ChoiceType (or
* Recursive method to collect the possibles answer from a ChoiceType (or
* its inherited types).
*
*
* @param \Symfony\Component\Form\FormInterface $form
* @return array where
* @return array where
* @throws \Exception
*/
private function collectChoicesAnswers($choices)
{
@ -1011,8 +1072,11 @@ EOF
return $answers;
}
/**
* @param $value
* @param $formats
* @return bool|\DateTime
*/
protected function processDate($value, $formats)
{
$possibleFormats = explode("|", $formats);
@ -1046,6 +1110,4 @@ EOF
}
}