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