From cbde853935f70a0b4b18c16e8f645e10d6bbd155 Mon Sep 17 00:00:00 2001 From: Ramon Gutierrez Date: Thu, 1 Feb 2018 04:18:11 +0800 Subject: [PATCH] Add command to import csv for outlets and hubs --- src/Command/ImportOutletsCommand.php | 191 +++++++++++++++++++++++++++ src/Ramcar/Location.php | 5 +- 2 files changed, 193 insertions(+), 3 deletions(-) create mode 100644 src/Command/ImportOutletsCommand.php diff --git a/src/Command/ImportOutletsCommand.php b/src/Command/ImportOutletsCommand.php new file mode 100644 index 00000000..dbcb5979 --- /dev/null +++ b/src/Command/ImportOutletsCommand.php @@ -0,0 +1,191 @@ +object_manager = $om; + $this->validator = $validator; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('outlets:import') + ->setDescription('Import a CSV file with outlets and hubs.') + ->setHelp('Creates outlets and hubs based off imported CSV.') + ->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $csv_file = $input->getArgument('file'); + + // CSV column order: + // 0 - area + // 1 - name + // 2 - branch + // 3 - address + // 4 - hub yes/no + // 5 - annex yes/no + // 6 - latitude + // 7 - longitude + + // attempt to open file + try + { + $handle = fopen($csv_file, "r"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $csv_file . '" could be read.'); + } + + // get entity manager + $em = $this->object_manager; + + // row counter + $row = 1; + + // has error? + $has_error = false; + + // loop through rows + while (($fields = fgetcsv($handle)) !== false) + { + $output->writeln("Parsing row " . $row . "..."); + + // check if this is also a hub + $isHub = $fields[4] == 'Yes' ? true : false; + + if ($isHub) + { + $output->writeln("This is a hub! Creating and validating hub entity..."); + + // create hub first + $hub = new Hub(); + + // set properties + $this->setObject($hub, $fields); + + // validate + $errors = $this->validator->validate($hub); + $error_array = []; + + // add errors to list + foreach ($errors as $error) + { + $error_array[$error->getPropertyPath()] = $error->getMessage(); + } + + if (!empty($error_array)) + { + foreach ($error_array as $key => $message) + { + $output->writeln("[Hub] Error on row " . $row . ", field '" . $key . "': " . $message); + } + + // stop on error + $has_error = true; + break; + } + + // save hub + $em->persist($hub); + $output->writeln("Saved hub data for row ". $row . "."); + } + + $output->writeln("Creating and validating outlet entity..."); + + // create outlet + $outlet = new Outlet(); + + // set properties + $this->setObject($outlet, $fields); + + // validate + $errors = $this->validator->validate($outlet); + $error_array = []; + + // add errors to list + foreach ($errors as $error) + { + $error_array[$error->getPropertyPath()] = $error->getMessage(); + } + + if (!empty($error_array)) + { + foreach ($error_array as $key => $message) + { + $output->writeln("[Outlet] Error on row " . $row . ", field '" . $key . "': " . $message); + } + + // stop on error + $has_error = true; + break; + } + + if ($isHub) + { + // assign to hub + $outlet->setHub($hub); + } + + // save outlet + $em->persist($outlet); + $output->writeln("Saved outlet data for row ". $row . "."); + + // increment row + $row++; + } + + if (!$has_error) + { + // apply db changes + $em->flush(); + $output->writeln("Done! All changes saved to database."); + } + else + { + $output->writeln("Errors occurred! Please check the CSV file is in the proper format."); + } + + } + + protected function setObject($obj, $fields) + { + // times + $format = 'g:i A'; + $time_open = DateTime::createFromFormat($format, "12:00 AM"); + $time_close = DateTime::createFromFormat($format, "11:59 PM"); + + // coordinates + $point = new Point($fields[7], $fields[6]); + + // set properties + $obj->setName($fields[1]) + ->setBranch($fields[2]) + ->setAddress($fields[3]) + ->setTimeOpen($time_open) + ->setTimeClose($time_close) + ->setCoordinates($point); + } +} diff --git a/src/Ramcar/Location.php b/src/Ramcar/Location.php index 31a4ef51..4c74a051 100644 --- a/src/Ramcar/Location.php +++ b/src/Ramcar/Location.php @@ -34,7 +34,7 @@ trait Location // address /** - * @ORM\Column(type="string", length=80) + * @ORM\Column(type="text") * @Assert\NotBlank() */ protected $address; @@ -48,8 +48,7 @@ trait Location // contact numbers // this is displayed in a textarea /** - * @ORM\Column(type="string", length=200) - * @Assert\NotBlank() + * @ORM\Column(type="string", length=200, nullable=true) */ protected $contact_nums;