Add command to import csv for outlets and hubs
This commit is contained in:
parent
908a79a909
commit
cbde853935
2 changed files with 193 additions and 3 deletions
191
src/Command/ImportOutletsCommand.php
Normal file
191
src/Command/ImportOutletsCommand.php
Normal file
|
|
@ -0,0 +1,191 @@
|
|||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
|
||||
use App\Entity\Outlet;
|
||||
use App\Entity\Hub;
|
||||
|
||||
use CrEOF\Spatial\PHP\Types\Geometry\Point;
|
||||
use DateTime;
|
||||
|
||||
class ImportOutletsCommand extends Command
|
||||
{
|
||||
private $object_manager;
|
||||
|
||||
public function __construct(ObjectManager $om, ValidatorInterface $validator)
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue