em = $om; // load existing battery data $this->loadBatteryManufacturers(); $this->loadBatteryModels(); $this->loadBatterySizes(); $this->loadBatteries(); // load existing vehicle data $this->loadVehicleManufacturers(); $this->loadVehicleMakes(); parent::__construct(); } protected function configure() { $this->setName('cmbvehiclecompatibility:import') ->setDescription('Retrieve from a CSV file battery and vehicle information.') ->setHelp('Creates battery manufacturers, models, sizes, vehicle makes, and models based on data from imported CSV.') ->addArgument('file', InputArgument::REQUIRED, 'Path to the CSV file.'); } protected function execute(InputInterface $input, OutputInterface $output) { $csv_file = $input->getArgument('file'); // attempt to open file try { $fh = fopen($csv_file, "r"); } catch (Exception $e) { throw new Exception('The file "' . $csv_file . '" could be read.'); } // get entity manager $em = $this->em; $row_num = 0; error_log('Processing vehicle compatibility csv file...'); while (($fields = fgetcsv($fh)) !== false) { $comp_batteries = []; if ($row_num < 2) { $row_num++; continue; } // initialize size battery array for cases where the battery size has '/' $sdfc_sizes = []; $ultramax_sizes = []; $motolite_sizes = []; $marathoner_sizes = []; $excel_sizes = []; // battery info $sdfc_size = trim($fields[self::F_BATT_SDFC]); $ultramax_size = trim($fields[self::F_BATT_ULTRAMAX]); $motolite_size = trim($fields[self::F_BATT_MOTOLITE]); $marathoner_size = trim($fields[self::F_BATT_MARATHONER]); $excel_size = trim($fields[self::F_BATT_EXCEL]); // check the sizes for '/' $pos = stripos($sdfc_size, '/'); if ($pos == false) { // no '/' in size $sdfc_sizes[] = $this->normalizeName($sdfc_size); } else { // we have '/' in size so we have to explode $sizes = explode('/', $sdfc_size); foreach ($sizes as $size) { $sdfc_sizes[] = $this->normalizeName($size); } } $pos = stripos($motolite_size, '/'); if ($pos == false) { // no '/' in size $motolite_sizes[] = $this->normalizeName($motolite_size); } else { // we have '/' in size so we have to explode $sizes = explode('/', $motolite_size); foreach ($sizes as $size) { $motolite_sizes[] = $this->normalizeName($size); } } $pos = stripos($marathoner_size, '/'); if ($pos == false) { // no '/' in size $marathoner_sizes[] = $this->normalizeName($marathoner_size); } else { // we have '/' in size so we have to explode $sizes = explode('/', $marathoner_size); foreach ($sizes as $size) { $marathoner_sizes[] = $this->normalizeName($size); } } $pos = stripos($ultramax_size, '/'); if ($pos == false) { // no '/' in size $ultramax_sizes[] = $this->normalizeName($ultramax_size); } else { // we have '/' in size so we have to explode $sizes = explode('/', $ultramax_size); foreach ($sizes as $size) { $ultramax_sizes[] = $this->normalizeName($size); } } $pos = stripos($excel_size, '/'); if ($pos == false) { // no '/' in size $excel_sizes[] = $this->normalizeName($excel_size); } else { // we have '/' in size so we have to explode $sizes = explode('/', $excel_size); foreach ($sizes as $size) { $excel_sizes[] = $this->normalizeName($size); } } // normalize the battery manufacturers and battery models $norm_century = $this->normalizeName(self::STR_CENTURY); $norm_sdfc = $this->normalizeName(self::STR_SDFC); $norm_motolite = $this->normalizeName(self::STR_MOTOLITE); $norm_wetcharged = $this->normalizeName(self::STR_WETCHARGED); $norm_marathoner = $this->normalizeName(self::STR_MARATHONER); $norm_ultramax = $this->normalizeName(self::STR_ULTRAMAX); $norm_excel = $this->normalizeName(self::STR_EXCEL); //foreach($sdfc_sizes as $size) //{ // error_log('sdfc size ' . $size); //} //foreach($motolite_sizes as $size) //{ // error_log('motolite size ' . $size); //} //foreach($marathoner_sizes as $size) //{ // error_log('marathoner size ' . $size); //} // vehicle info $manufacturer = trim($fields[self::F_VEHICLE_MANUFACTURER]); $make = trim($fields[self::F_VEHICLE_MAKE]); $year = trim($fields[self::F_VEHICLE_YEAR]); // vehicle data // check if vehicle manufacturer has been added if (!isset($this->vmanu_hash[$manufacturer])) $this->addVehicleManufacturer($manufacturer); // check if vehicle make has been added if (!isset($this->vmake_hash[$manufacturer][$make])) { foreach($sdfc_sizes as $size) { if (!(empty($size))) { if (isset($this->batt_hash[$norm_century][$norm_sdfc][$size])) $comp_batteries[] = $this->batt_hash[$norm_century][$norm_sdfc][$size]; else error_log('Not in the system: ' . $norm_century . ' ' . $norm_sdfc . ' ' . $size); } } foreach($ultramax_sizes as $size) { if (!(empty($size))) { if (isset($this->batt_hash[$norm_ultramax][$norm_ultramax][$size])) $comp_batteries[] = $this->batt_hash[$norm_ultramax][$norm_ultramax][$size]; else error_log('Not in the system: ' . $norm_ultramax . ' ' . $norm_ultramax . ' ' . $size); } } foreach($motolite_sizes as $size) { if (!(empty($size))) { if (isset($this->batt_hash[$norm_motolite][$norm_wetcharged][$size])) $comp_batteries[] = $this->batt_hash[$norm_motolite][$norm_wetcharged][$size]; else error_log('Not in the system: ' . $norm_motolite . ' ' . $norm_wetcharged . ' ' . $size); } } foreach($marathoner_sizes as $size) { if (!(empty($size))) { if (isset($this->batt_hash[$norm_century][$norm_marathoner][$size])) $comp_batteries[] = $this->batt_hash[$norm_century][$norm_marathoner][$size]; else error_log('Not in the system: ' . $norm_century . ' ' . $norm_marathoner . ' ' . $size); } } foreach($excel_sizes as $size) { if (!(empty($size))) { if (isset($this->batt_hash[$norm_excel][$norm_excel][$size])) $comp_batteries[] = $this->batt_hash[$norm_excel][$norm_excel][$size]; else error_log('Not in the system: ' . $norm_excel . ' ' . $norm_excel . ' ' . $size); } } $this->addVehicleMake($manufacturer, $make, $year, $comp_batteries); } $row_num++; } } protected function addVehicleManufacturer($name) { // save to db $vehicle_manufacturer = new VehicleManufacturer(); $vehicle_manufacturer->setName($name); $this->em->persist($vehicle_manufacturer); $this->em->flush(); // add to hash $this->vmanu_hash[$name] = $vehicle_manufacturer; } protected function addVehicleMake($manufacturer, $make, $year, $batteries) { // save to db $vehicle = new Vehicle(); $vmanu = $this->vmanu_hash[$manufacturer]; // parse year from and year to $year_from = ''; $year_to = ''; if (!empty($year)) { $model_years = explode('-', $year); $year_from = $model_years[0]; if (!empty($year_to)) $year_to = $model_years[1]; // check if $year_to is the string "Present" // if so, set to 0, for now if ($year_to == self::STR_PRESENT) $year_to = 0; } $vehicle->setManufacturer($vmanu) ->setMake($make) ->setModelYearFrom($year_from) ->setModelYearTo($year_to); // add vehicle to battery foreach ($batteries as $battery) { $battery->addVehicle($vehicle); $this->em->persist($battery); } // add vehicle to manufacturer $vmanu->addVehicle($vehicle); $this->em->persist($vmanu); $this->em->persist($vehicle); $this->em->flush(); // add to hash $this->vmake_hash[$manufacturer][$make] = $vehicle; } protected function loadBatteryManufacturers() { $this->bmanu_hash = []; $batt_manufacturers = $this->em->getRepository(BatteryManufacturer::class)->findAll(); foreach ($batt_manufacturers as $batt_manu) { $name = $this->normalizeName($batt_manu->getName()); $this->bmanu_hash[$name] = $batt_manu; } } protected function loadBatteryModels() { $this->bmodel_hash = []; $batt_models = $this->em->getRepository(BatteryModel::class)->findAll(); foreach ($batt_models as $batt_model) { $name = $this->normalizeName($batt_model->getName()); $this->bmodel_hash[$name] = $batt_model; } } protected function loadBatterySizes() { $this->bsize_hash = []; $batt_sizes = $this->em->getRepository(BatterySize::class)->findAll(); foreach ($batt_sizes as $batt_size) { $name = $this->normalizeName($batt_size->getName()); $this->bsize_hash[$name] = $batt_size; } } protected function loadBatteries() { $this->batt_hash = []; $batts = $this->em->getRepository(Battery::class)->findAll(); foreach ($batts as $batt) { $brand = $this->normalizeName($batt->getManufacturer()->getName()); $model = $this->normalizeName($batt->getModel()->getName()); $size = $this->normalizeName($batt->getSize()->getName()); $this->batt_hash[$brand][$model][$size] = $batt; } } protected function loadVehicleManufacturers() { $this->vmanu_hash = []; $vmanus = $this->em->getRepository(VehicleManufacturer::class)->findAll(); foreach ($vmanus as $vmanu) { $name = $vmanu->getName(); $this->vmanu_hash[$name] = $vmanu; } } protected function loadVehicleMakes() { $this->vmake_hash = []; $vmakes = $this->em->getRepository(Vehicle::class)->findAll(); foreach ($vmakes as $vmake) { $manufacturer = $vmake->getManufacturer()->getName(); $make = $vmake->getMake(); $this->vmake_hash[$manufacturer][$make] = $vmake; } } protected function normalizeName($name) { // check for M-42. Need to convert to M42 if (strcasecmp($name, self::STR_M_42) == 0) { $normalized_key = strtolower(self::STR_M42); } else { $normalized_key = trim(strtolower($name)); } return $normalized_key; } }