From 5ddc0e7a53b390b6e176798b65afdab7ab5ade13 Mon Sep 17 00:00:00 2001 From: Pol Dellaiera Date: Tue, 22 Jun 2021 15:31:49 +0200 Subject: [PATCH] Simplify loading of Symfony commands. --- .../Command/ChillPersonMoveCommand.php | 71 +-- .../Command/ImportPeopleFromCSVCommand.php | 484 ++++++++---------- .../ChillPersonExtension.php | 1 - .../ChillPersonBundle/config/services.yaml | 7 + .../config/services/command.yaml | 19 - 5 files changed, 246 insertions(+), 336 deletions(-) delete mode 100644 src/Bundle/ChillPersonBundle/config/services/command.yaml diff --git a/src/Bundle/ChillPersonBundle/Command/ChillPersonMoveCommand.php b/src/Bundle/ChillPersonBundle/Command/ChillPersonMoveCommand.php index 4f2b2098a..b68997ed6 100644 --- a/src/Bundle/ChillPersonBundle/Command/ChillPersonMoveCommand.php +++ b/src/Bundle/ChillPersonBundle/Command/ChillPersonMoveCommand.php @@ -1,25 +1,7 @@ - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ namespace Chill\PersonBundle\Command; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -28,39 +10,28 @@ use Doctrine\ORM\EntityManagerInterface; use Chill\PersonBundle\Entity\Person; use Symfony\Component\Console\Exception\RuntimeException; use Psr\Log\LoggerInterface; +use Symfony\Component\Console\Command\Command; -class ChillPersonMoveCommand extends ContainerAwareCommand +final class ChillPersonMoveCommand extends Command { - /** - * - * @var PersonMove - */ - protected $mover; - - /** - * - * @var EntityManagerInterface - */ - protected $em; - - /** - * - * @var LoggerInterface - */ - protected $chillLogger; - + private PersonMove $mover; + + private EntityManagerInterface $em; + + private LoggerInterface $chillLogger; + public function __construct( - PersonMove $mover, + PersonMove $mover, EntityManagerInterface $em, LoggerInterface $chillLogger ) { parent::__construct('chill:person:move'); - + $this->mover = $mover; $this->em = $em; $this->chillLogger = $chillLogger; } - + protected function configure() { $this @@ -73,14 +44,14 @@ class ChillPersonMoveCommand extends ContainerAwareCommand ->addOption('delete-entity', null, InputOption::VALUE_REQUIRED|InputOption::VALUE_IS_ARRAY, "entity to delete", []) ; } - + protected function interact(InputInterface $input, OutputInterface $output) { if (FALSE === $input->hasOption('dump-sql') && FALSE === $input->hasOption('force')) { $msg = "You must use \"--dump-sql\" or \"--force\""; throw new RuntimeException($msg); } - + foreach (["from", "to"] as $name) { if (empty($input->getOption($name))) { throw new RuntimeException("You must set a \"$name\" option"); @@ -90,7 +61,7 @@ class ChillPersonMoveCommand extends ContainerAwareCommand throw new RuntimeException("The id in \"$name\" field does not contains " . "only digits: $id"); } - } + } } protected function execute(InputInterface $input, OutputInterface $output) @@ -99,16 +70,16 @@ class ChillPersonMoveCommand extends ContainerAwareCommand $from = $repository->find($input->getOption('from')); $to = $repository->find($input->getOption('to')); $deleteEntities = $input->getOption('delete-entity'); - + if ($from === NULL) { throw new RuntimeException(sprintf("Person \"from\" with id %d not found", $input->getOption('from'))); } if ($to === NULL) { throw new RuntimeException(sprintf("Person \"to\" with id %d not found", $input->getOption('to'))); } - + $sqls = $this->mover->getSQL($from, $to, $deleteEntities); - + if ($input->getOption('dump-sql')) { foreach($sqls as $sql) { $output->writeln($sql); @@ -125,25 +96,25 @@ class ChillPersonMoveCommand extends ContainerAwareCommand $connection->executeQuery($sql); } $connection->commit(); - + $this->chillLogger->notice("Move a person from command line succeeded", $ctxt); } } - + protected function buildLoggingContext(Person $from, Person $to, $deleteEntities, $sqls) { $ctxt = [ 'from' => $from->getId(), 'to' => $to->getId() ]; - + foreach ($deleteEntities as $key => $de) { $ctxt['delete_entity_'.$key] = $de; } foreach ($sqls as $key => $sql) { $ctxt['sql_'.$key] = $sql; } - + return $ctxt; } diff --git a/src/Bundle/ChillPersonBundle/Command/ImportPeopleFromCSVCommand.php b/src/Bundle/ChillPersonBundle/Command/ImportPeopleFromCSVCommand.php index d5e4c597c..35f596295 100644 --- a/src/Bundle/ChillPersonBundle/Command/ImportPeopleFromCSVCommand.php +++ b/src/Bundle/ChillPersonBundle/Command/ImportPeopleFromCSVCommand.php @@ -1,22 +1,5 @@ - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - namespace Chill\PersonBundle\Command; use Chill\MainBundle\Templating\TranslatableStringHelper; @@ -40,65 +23,37 @@ use Symfony\Component\Console\Question\ConfirmationQuestion; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\Event; use Symfony\Component\Form\FormFactory; +use Symfony\Component\Form\FormFactoryInterface; -/** - * Class ImportPeopleFromCSVCommand - * - * @package Chill\PersonBundle\Command - * @author Julien Fastré - */ -class ImportPeopleFromCSVCommand extends Command +final class ImportPeopleFromCSVCommand extends Command { + private InputInterface $input; + + private OutputInterface $output; + + private LoggerInterface $logger; + + private TranslatableStringHelper $helper; + + private EntityManagerInterface $em; + + private EventDispatcherInterface $eventDispatcher; + /** - * @var InputInterface + * The line currently read */ - protected $input; - + private int $line; + /** - * @var OutputInterface + * Where key are column names, and value the custom field slug */ - protected $output; - - /** - * @var \Psr\Log\LoggerInterface - */ - protected $logger; - - /** - * @var \Chill\MainBundle\Templating\TranslatableStringHelper - */ - protected $helper; - - /** - * @var \Doctrine\Persistence\ObjectManager - */ - protected $em; - - /** - * @var EventDispatcherInterface - */ - protected $eventDispatcher; - - /** - * the line currently read - * - * @var int - */ - protected $line; - - /** - * @var array where key are column names, and value the custom field slug - */ - protected $customFieldMapping = array(); - - /** - * @var CustomFieldProvider - */ - protected $customFieldProvider; - + private array $customFieldMapping = []; + + private CustomFieldProvider $customFieldProvider; + /** * Contains an array of information searched in the file. - * + * * position 0: the information key (which will be used in this process) * position 1: the helper * position 2: the default value @@ -121,19 +76,16 @@ class ImportPeopleFromCSVCommand extends Command ['locality', 'The column header for locality', 'locality'], ['center', 'The column header for center', 'center'] ); - + /** * Different possible format to interpret a date * * @var string */ protected static $defaultDateInterpreter = "%d/%m/%Y|%e/%m/%y|%d/%m/%Y|%e/%m/%Y"; - - /** - * @var FormFactory - */ - protected $formFactory; - + + private FormFactoryInterface $formFactory; + /** * ImportPeopleFromCSVCommand constructor. * @@ -150,7 +102,7 @@ class ImportPeopleFromCSVCommand extends Command EntityManagerInterface $em, CustomFieldProvider $customFieldProvider, EventDispatcherInterface $eventDispatcher, - FormFactory $formFactory + FormFactoryInterface $formFactory ) { $this->logger = $logger; $this->helper = $helper; @@ -158,10 +110,10 @@ class ImportPeopleFromCSVCommand extends Command $this->customFieldProvider = $customFieldProvider; $this->eventDispatcher = $eventDispatcher; $this->formFactory = $formFactory; - + parent::__construct('chill:person:import'); } - + /** * */ @@ -171,10 +123,10 @@ class ImportPeopleFromCSVCommand extends Command ->addArgument('csv_file', InputArgument::REQUIRED, "The CSV file to import") ->setDescription("Import people from a csv file") ->setHelp(<<addArgument('locale', InputArgument::REQUIRED, + ->addArgument('locale', InputArgument::REQUIRED, "The locale to use in displaying translatable strings from entities") ->addOption( - 'force-center', - null, + 'force-center', + null, InputOption::VALUE_REQUIRED, "The id of the center" ) @@ -197,10 +149,10 @@ EOF "Persist people in the database (default is not to persist people)" ) ->addOption( - 'delimiter', - 'd', - InputOption::VALUE_OPTIONAL, - "The delimiter character of the csv file", + 'delimiter', + 'd', + InputOption::VALUE_OPTIONAL, + "The delimiter character of the csv file", ",") ->addOption( 'enclosure', @@ -236,33 +188,33 @@ EOF "The path of the file to load the matching between label in CSV and answers" ) ; - + // mapping columns foreach (self::$mapping as $m) { $this->addOptionShortcut($m[0], $m[1], $m[2]); } - + // other information $this->addOptionShortcut('birthdate_format', 'Format preference for ' - . 'birthdate. See help for date formats preferences.', + . 'birthdate. See help for date formats preferences.', self::$defaultDateInterpreter); $this->addOptionShortcut('opening_date_format', 'Format preference for ' . 'opening date. See help for date formats preferences.', self::$defaultDateInterpreter); $this->addOptionShortcut('closing_date_format', 'Format preference for ' - . 'closing date. See help for date formats preferences.', + . 'closing date. See help for date formats preferences.', self::$defaultDateInterpreter); - + // mapping column to custom fields - $this->addOption('custom-field', NULL, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, + $this->addOption('custom-field', NULL, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, "Mapping a column to a custom fields key. Example: 1=cf_slug"); $this->addOption('skip-interactive-field-mapping', null, InputOption::VALUE_NONE, "Do not ask for interactive mapping"); } - + /** * This function is a shortcut to addOption. - * + * * @param string $name * @param string $description * @param string $default @@ -271,10 +223,10 @@ EOF protected function addOptionShortcut($name, $description, $default) { $this->addOption($name, null, InputOption::VALUE_OPTIONAL, $description, $default); - + return $this; } - + /** * @param InputInterface $input * @param OutputInterface $output @@ -285,17 +237,17 @@ EOF $this->input = $input; $this->output = $output; $this->logger = new ConsoleLogger($output); - + $csv = $this->openCSV(); - + // getting the first row if (($row = fgetcsv( - $csv, - $input->getOption('length'), - $input->getOption('delimiter'), - $input->getOption('enclosure'), + $csv, + $input->getOption('length'), + $input->getOption('delimiter'), + $input->getOption('enclosure'), $input->getOption('escape'))) !== false) { - + try { $this->matchColumnToCustomField($row); } finally { @@ -303,33 +255,33 @@ EOF fclose($csv); } } - + // load the matching between csv and label $this->loadAnswerMatching(); } - + /** * @param $row */ protected function matchColumnToCustomField($row) { - + $cfMappingsOptions = $this->input->getOption('custom-field'); /* @var $em \Doctrine\Persistence\ObjectManager */ $em = $this->em; - + foreach($cfMappingsOptions as $cfMappingStringOption) { list($rowNumber, $cfSlug) = preg_split('|=|', $cfMappingStringOption); - + // check that the column exists, getting the column name $column = $row[$rowNumber]; - + if (empty($column)) { $message = "The column with row $rowNumber is empty."; $this->logger->error($message); throw new \RuntimeException($message); } - + // check a custom field exists try { $customField = $em->createQuery("SELECT cf " @@ -357,15 +309,15 @@ EOF . "Stopping this command."); throw new \RuntimeException("The custom field with slug $cfSlug could not be found. " . "Stopping this command."); - } - + } + $this->logger->notice(sprintf("Matched custom field %s (question : '%s') on column %d (displayed in the file as '%s')", $customField->getSlug(), $this->helper->localize($customField->getName()), $rowNumber, $column)); - + $this->customFieldMapping[$rowNumber] = $customField; } } - + /** * Load the mapping between answer in CSV and value in choices from a json file */ @@ -383,7 +335,7 @@ EOF } } } - + /** * */ @@ -392,14 +344,14 @@ EOF if ($this->input->hasOption('dump-choice-matching') && !empty($this->input->getOption('dump-choice-matching'))) { $this->logger->debug("Dump the matching between answer and choices"); $str = json_encode($this->cacheAnswersMapping, JSON_PRETTY_PRINT); - + $fs = new Filesystem(); $filename = $this->input->getOption('dump-choice-matching'); - + $fs->dumpFile($filename, $str); } } - + /** * @param InputInterface $input * @param OutputInterface $output @@ -410,22 +362,22 @@ EOF { $this->input = $input; $this->output = $output; - + $this->logger->debug("Setting locale to ".$input->getArgument('locale')); setlocale(LC_TIME, $input->getArgument('locale')); - + // opening csv as resource $csv = $this->openCSV(); - + $num = 0; $line = $this->line = 1; - + try { while (($row = fgetcsv( - $csv, - $input->getOption('length'), - $input->getOption('delimiter'), - $input->getOption('enclosure'), + $csv, + $input->getOption('length'), + $input->getOption('delimiter'), + $input->getOption('enclosure'), $input->getOption('escape'))) !== false) { $this->logger->debug("Processing line ".$this->line); if ($line === 1 ) { @@ -434,11 +386,11 @@ EOF $headers = $this->processingHeaders($row); } else { $person = $this->createPerson($row, $headers); - + if (count($this->customFieldMapping) > 0) { $this->processingCustomFields($person, $row); } - + $event = new Event(); $event->person = $person; $event->rawHeaders = $rawHeaders; @@ -449,21 +401,21 @@ EOF $event->input = $this->input; $event->output = $this->output; $event->helperSet = $this->getHelperSet(); - + $this->eventDispatcher->dispatch('chill_person.person_import', $event); - - if ($this->input->getOption('force') === TRUE + + if ($this->input->getOption('force') === TRUE && $event->skipPerson === false) { $this->em->persist($person); } - + $num ++; } $line ++; $this->line++; } - + if ($this->input->getOption('force') === true) { $this->logger->debug('persisting entitites'); $this->em->flush(); @@ -475,9 +427,9 @@ EOF $this->dumpAnswerMatching(); } } - + /** - * + * * @return resource * @throws \RuntimeException */ @@ -485,23 +437,23 @@ EOF { $fs = new Filesystem(); $filename = $this->input->getArgument('csv_file'); - + if (!$fs->exists($filename)) { throw new \RuntimeException("The file does not exists or you do not " . "have the right to read it."); } - + $resource = fopen($filename, 'r'); - + if ($resource == FALSE) { throw new \RuntimeException("The file '$filename' could not be opened."); } - + return $resource; } - + /** - * + * * @param type $firstRow * @return array where keys are column number, and value is information mapped */ @@ -510,11 +462,11 @@ EOF $availableOptions = array_map(function($m) { return $m[0]; }, self::$mapping); $matchedColumnHeaders = array(); $headers = array(); - + foreach($availableOptions as $option) { $matchedColumnHeaders[$option] = $this->input->getOption($option); } - + foreach($firstRow as $key => $content) { $content = trim($content); if (in_array($content, $matchedColumnHeaders)) { @@ -524,11 +476,11 @@ EOF } else { $this->logger->notice("Column with content '$content' is ignored"); } - } - + } + return $headers; } - + /** * * @param array $row @@ -541,21 +493,21 @@ EOF // trying to get the opening date $openingDateString = trim($row[array_search('opening_date', $headers)]); $openingDate = $this->processDate($openingDateString, $this->input->getOption('opening_date_format')); - + $person = $openingDate instanceof \DateTime ? new Person($openingDate) : new Person(); // add the center $center = $this->getCenter($row, $headers); - + if ($center === null) { throw new \Exception("center not found"); } - + $person->setCenter($center); - + foreach($headers as $column => $info) { - + $value = trim($row[$column]); - + switch($info) { case 'firstname': $person->setFirstName($value); @@ -582,12 +534,12 @@ EOF $person->setEmail($value); break; case 'phonenumber': - $person->setPhonenumber($value); + $person->setPhonenumber($value); break; case 'mobilenumber': $person->setMobilenumber($value); break; - + // we just keep the column number for those data case 'postalcode': $postalCodeValue = $value; @@ -600,33 +552,33 @@ EOF break; } } - + // handle address if (\in_array('postalcode', $headers)) { - + 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); } } - + return $person; } - + /** * @param $row * @param $headers @@ -640,7 +592,7 @@ EOF } else { $columnCenter = \array_search('center', $headers); $centerName = \trim($row[$columnCenter]); - + try { return $this->em->createQuery('SELECT c FROM ChillMainBundle:Center c ' . 'WHERE c.name = :center_name') @@ -654,7 +606,7 @@ EOF } } } - + /** * @param $centerName * @return Center|mixed|null|object @@ -664,68 +616,68 @@ EOF if (!\array_key_exists('_center_picked', $this->cacheAnswersMapping)) { $this->cacheAnswersMapping['_center_picked'] = []; } - + if (\array_key_exists($centerName, $this->cacheAnswersMapping['_center_picked'])) { $id = $this->cacheAnswersMapping['_center_picked'][$centerName]; - + return $this->em->getRepository(Center::class) ->find($id); } - + $centers = $this->em->createQuery("SELECT c FROM ChillMainBundle:Center c " . "ORDER BY SIMILARITY(c.name, :center_name) DESC") ->setParameter('center_name', $centerName) ->setMaxResults(10) ->getResult() ; - + if (count($centers) > 1) { if (\strtolower($centers[0]->getName()) === \strtolower($centerName)) { return $centers[0]; } } - + $centersByName = []; - $names = \array_map(function(Center $c) use (&$centersByName) { + $names = \array_map(function(Center $c) use (&$centersByName) { $n = $c->getName(); $centersByName[$n] = $c; return $n; - + }, $centers); $names[] = "none of them"; - + $helper = $this->getHelper('question'); - $question = new ChoiceQuestion(sprintf("Which center match the name \"%s\" ? (default to \"%s\")", $centerName, $names[0]), + $question = new ChoiceQuestion(sprintf("Which center match the name \"%s\" ? (default to \"%s\")", $centerName, $names[0]), $names, 0); - + $answer = $helper->ask($this->input, $this->output, $question); - + if ($answer === 'none of them') { $questionCreate = new ConfirmationQuestion("Would you like to create it ?", false); $create = $helper->ask($this->input, $this->output, $questionCreate); - + if ($create) { $center = (new Center()) ->setName($centerName) ; - + if ($this->input->getOption('force') === TRUE) { $this->em->persist($center); $this->em->flush(); } - + return $center; } } - + $center = $centersByName[$answer]; - + $this->cacheAnswersMapping['_center_picked'][$centerName] = $center->getId(); - + return $center; } - + /** * @param $postalCode * @param $locality @@ -736,15 +688,15 @@ EOF if (!\array_key_exists('_postal_code_picked', $this->cacheAnswersMapping)) { $this->cacheAnswersMapping['_postal_code_picked'] = []; } - + if (\array_key_exists($postalCode, $this->cacheAnswersMapping['_postal_code_picked'])) { if (\array_key_exists($locality, $this->cacheAnswersMapping['_postal_code_picked'][$postalCode])) { $id = $this->cacheAnswersMapping['_postal_code_picked'][$postalCode][$locality]; - + return $this->em->getRepository(PostalCode::class)->find($id); } } - + $postalCodes = $this->em->createQuery("SELECT pc FROM ".PostalCode::class." pc " . "WHERE pc.code = :postal_code " . "ORDER BY SIMILARITY(pc.name, :locality) DESC " @@ -754,48 +706,48 @@ EOF ->setParameter('locality', $locality) ->getResult() ; - + if (count($postalCodes) >= 1) { - if ($postalCodes[0]->getCode() === $postalCode + if ($postalCodes[0]->getCode() === $postalCode && $postalCodes[0]->getName() === $locality) { return $postalCodes[0]; } } - + if (count($postalCodes) === 0) { return null; } - + $postalCodeByName = []; $names = \array_map(function(PostalCode $pc) use (&$postalCodeByName) { $n = $pc->getName(); $postalCodeByName[$n] = $pc; - + return $n; }, $postalCodes); $names[] = 'none of them'; - + $helper = $this->getHelper('question'); $question = new ChoiceQuestion(sprintf("Which postal code match the " - . "name \"%s\" with postal code \"%s\" ? (default to \"%s\")", - $locality, $postalCode, $names[0]), + . "name \"%s\" with postal code \"%s\" ? (default to \"%s\")", + $locality, $postalCode, $names[0]), $names, 0); - + $answer = $helper->ask($this->input, $this->output, $question); - + if ($answer === 'none of them') { return null; } - + $pc = $postalCodeByName[$answer]; - - $this->cacheAnswersMapping['_postal_code_picked'][$postalCode][$locality] = + + $this->cacheAnswersMapping['_postal_code_picked'][$postalCode][$locality] = $pc->getId(); - + return $pc; } - + /** * @param Person $person * @param $value @@ -804,29 +756,29 @@ EOF protected function processBirthdate(Person $person, $value) { if (empty($value)) { return; } - + $date = $this->processDate($value, $this->input->getOption('birthdate_format')); - + if ($date instanceof \DateTime) { // we correct birthdate if the date is in the future // the most common error is to set date 100 years to late (ex. 2063 instead of 1963) if ($date > new \DateTime('yesterday')) { $date = $date->sub(new \DateInterval('P100Y')); } - + $person->setBirthdate($date); - + return; } - + // if we arrive here, we could not process the date $this->logger->warning(sprintf( "Line %d : the birthdate could not be interpreted. Was %s.", $this->line, $value)); - + } - + /** * @param Person $person * @param $value @@ -835,39 +787,39 @@ EOF 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(); - + if ($period->getOpeningDate() > new \DateTime('yesterday')) { $this->logger->debug(sprintf("skipping a closing date because opening date is after yesterday (%s)", $period->getOpeningDate()->format('Y-m-d'))); return; } - - + + $date = $this->processDate($value, $this->input->getOption('closing_date_format')); - + if ($date instanceof \DateTime) { // we correct birthdate if the date is in the future // the most common error is to set date 100 years to late (ex. 2063 instead of 1963) if ($date > new \DateTime('yesterday')) { $date = $date->sub(new \DateInterval('P100Y')); } - + $period->setClosingDate($date); $person->close(); return; } - + // if we arrive here, we could not process the date $this->logger->warning(sprintf( "Line %d : the closing date could not be interpreted. Was %s.", $this->line, $value)); } - + /** * @param Person $person * @param $row @@ -875,27 +827,27 @@ EOF */ protected function processingCustomFields(Person $person, $row) { - + /* @var $cfProvider \Chill\CustomFieldsBundle\Service\CustomFieldProvider */ $cfProvider = $this->customFieldProvider; $cfData = array(); - + /* @var $$customField \Chill\CustomFieldsBundle\Entity\CustomField */ foreach($this->customFieldMapping as $rowNumber => $customField) { $builder = $this->formFactory->createBuilder(); $cfProvider->getCustomFieldByType($customField->getType()) ->buildForm($builder, $customField); $form = $builder->getForm(); - + // get the type of the form $type = get_class($form->get($customField->getSlug()) ->getConfig()->getType()->getInnerType()); - $this->logger->debug(sprintf("Processing a form of type %s", + $this->logger->debug(sprintf("Processing a form of type %s", $type)); switch ($type) { case \Symfony\Component\Form\Extension\Core\Type\TextType::class: - $cfData[$customField->getSlug()] = + $cfData[$customField->getSlug()] = $this->processTextType($row[$rowNumber], $form, $customField); break; case \Symfony\Component\Form\Extension\Core\Type\ChoiceType::class: @@ -903,12 +855,12 @@ EOF $cfData[$customField->getSlug()] = $this->processChoiceType($row[$rowNumber], $form, $customField); } - + } - + $person->setCFData($cfData); } - + /** * Process a text type on a custom field * @@ -917,24 +869,24 @@ EOF * @return type */ protected function processTextType( - $value, - \Symfony\Component\Form\FormInterface $form, + $value, + \Symfony\Component\Form\FormInterface $form, \Chill\CustomFieldsBundle\Entity\CustomField $cf ) { $form->submit(array($cf->getSlug() => $value)); - + $value = $form->getData()[$cf->getSlug()]; - + $this->logger->debug(sprintf("Found value : %s for custom field with question " . "'%s'", $value, $this->helper->localize($cf->getName()))); - + return $value; } - + protected $cacheAnswersMapping = array(); - - + + /** * Process a custom field choice. * @@ -949,14 +901,14 @@ EOF */ protected function processChoiceType( $value, - \Symfony\Component\Form\FormInterface $form, + \Symfony\Component\Form\FormInterface $form, \Chill\CustomFieldsBundle\Entity\CustomField $cf ) { // getting the possible answer and their value : $view = $form->get($cf->getSlug())->createView(); $answers = $this->collectChoicesAnswers($view->vars['choices']); - + // if we do not have any answer on the question, throw an error. if (count($answers) === 0) { $message = sprintf( @@ -964,34 +916,34 @@ EOF $this->helper->localize($cf->getName()), $cf->getSlug() ); - + $this->logger->error($message, array( 'method' => __METHOD__, 'slug' => $cf->getSlug(), 'question' => $this->helper->localize($cf->getName()) )); - + throw new \RuntimeException($message); } - + if ($view->vars['required'] === false) { $answers[null] = '** no answer'; } - + // the answer does not exists in cache. Try to find it, or asks the user if (!isset($this->cacheAnswersMapping[$cf->getSlug()][$value])) { - + // try to find the answer (with array_keys and a search value $values = array_keys( - array_map(function($label) { return trim(strtolower($label)); }, $answers), + array_map(function($label) { return trim(strtolower($label)); }, $answers), trim(strtolower($value)), true ); - + if (count($values) === 1) { // we could guess an answer ! $this->logger->info("This question accept multiple answers"); - $this->cacheAnswersMapping[$cf->getSlug()][$value] = + $this->cacheAnswersMapping[$cf->getSlug()][$value] = $view->vars['multiple'] == false ? $values[0] : array($values[0]); $this->logger->info(sprintf("Guessed that value '%s' match with key '%s' " . "because the CSV and the label are equals.", @@ -1021,20 +973,20 @@ EOF array_keys($matchingTableRowAnswer) ); $question->setErrorMessage("This choice is not possible"); - + if ($view->vars['multiple']) { $this->logger->debug("this question is multiple"); $question->setMultiselect(true); } - + $selected = $this->getHelper('question')->ask($this->input, $this->output, $question); - $this->output->writeln(sprintf('You have selected "%s"', - is_array($answers[$matchingTableRowAnswer[$selected]]) ? + $this->output->writeln(sprintf('You have selected "%s"', + is_array($answers[$matchingTableRowAnswer[$selected]]) ? implode(',', $answers[$matchingTableRowAnswer[$selected]]) : $answers[$matchingTableRowAnswer[$selected]]) ); - + // recording value in cache $this->cacheAnswersMapping[$cf->getSlug()][$value] = $matchingTableRowAnswer[$selected]; $this->logger->debug(sprintf("Setting the value '%s' in cache for customfield '%s' and answer '%s'", @@ -1045,19 +997,19 @@ EOF $value)); } } - + $form->submit(array($cf->getSlug() => $this->cacheAnswersMapping[$cf->getSlug()][$value])); $value = $form->getData()[$cf->getSlug()]; - + $this->logger->debug(sprintf( - "Found value : %s for custom field with question '%s'", - is_array($value) ? implode(',', $value) : $value, + "Found value : %s for custom field with question '%s'", + is_array($value) ? implode(',', $value) : $value, $this->helper->localize($cf->getName())) ); - + return $value; } - + /** * Recursive method to collect the possibles answer from a ChoiceType (or * its inherited types). @@ -1069,7 +1021,7 @@ EOF private function collectChoicesAnswers($choices) { $answers = array(); - + /* @var $choice \Symfony\Component\Form\ChoiceList\View\ChoiceView */ foreach($choices as $choice) { if ($choice instanceof \Symfony\Component\Form\ChoiceList\View\ChoiceView) { @@ -1085,10 +1037,10 @@ EOF )); } } - + return $answers; } - + /** * @param $value * @param $formats @@ -1097,13 +1049,13 @@ EOF protected function processDate($value, $formats) { $possibleFormats = explode("|", $formats); - + foreach($possibleFormats as $format) { $this->logger->debug("Trying format $format", array(__METHOD__)); $dateR = strptime($value, $format); - + if (is_array($dateR) && $dateR['unparsed'] === '') { - $string = sprintf("%04d-%02d-%02d %02d:%02d:%02d", + $string = sprintf("%04d-%02d-%02d %02d:%02d:%02d", ($dateR['tm_year']+1900), ($dateR['tm_mon']+1), ($dateR['tm_mday']), @@ -1112,19 +1064,19 @@ EOF ($dateR['tm_sec'])); $date = \DateTime::createFromFormat("Y-m-d H:i:s", $string); $this->logger->debug(sprintf("Interpreting %s as date %s", $value, $date->format("Y-m-d H:i:s"))); - + return $date; } } - + // if we arrive here, we could not process the date $this->logger->debug(sprintf( "Line %d : a date could not be interpreted. Was %s.", $this->line, $value)); - + return false; } - - + + } diff --git a/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php b/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php index 79dae3255..f52564cfa 100644 --- a/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php +++ b/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php @@ -69,7 +69,6 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac $loader->load('services/search.yaml'); $loader->load('services/menu.yaml'); $loader->load('services/privacyEvent.yaml'); - $loader->load('services/command.yaml'); $loader->load('services/actions.yaml'); $loader->load('services/form.yaml'); $loader->load('services/alt_names.yaml'); diff --git a/src/Bundle/ChillPersonBundle/config/services.yaml b/src/Bundle/ChillPersonBundle/config/services.yaml index 7f70a02dd..463ad0141 100644 --- a/src/Bundle/ChillPersonBundle/config/services.yaml +++ b/src/Bundle/ChillPersonBundle/config/services.yaml @@ -12,6 +12,13 @@ services: tags: - { name: 'serializer.normalizer', priority: 64 } + Chill\PersonBundle\Command\: + resource: '../Command/' + autowire: true + autoconfigure: true + tags: + - { name: console.command } + chill.person.form.type.select2maritalstatus: class: Chill\PersonBundle\Form\Type\Select2MaritalStatusType arguments: diff --git a/src/Bundle/ChillPersonBundle/config/services/command.yaml b/src/Bundle/ChillPersonBundle/config/services/command.yaml deleted file mode 100644 index 685a348dd..000000000 --- a/src/Bundle/ChillPersonBundle/config/services/command.yaml +++ /dev/null @@ -1,19 +0,0 @@ -services: - Chill\PersonBundle\Command\ChillPersonMoveCommand: - arguments: - $em: '@Doctrine\ORM\EntityManagerInterface' - $mover: '@Chill\PersonBundle\Actions\Remove\PersonMove' - $chillLogger: '@chill.main.logger' - tags: - - { name: console.command } - - Chill\PersonBundle\Command\ImportPeopleFromCSVCommand: - arguments: - $logger: '@logger' - $helper: '@Chill\MainBundle\Templating\TranslatableStringHelper' - $em: '@Doctrine\ORM\EntityManagerInterface' - $customFieldProvider: '@Chill\CustomFieldsBundle\Service\CustomFieldProvider' - $eventDispatcher: '@Symfony\Component\EventDispatcher\EventDispatcherInterface' - $formFactory: '@form.factory' - tags: - - { name: console.command }