namespace = ''; $this->name = 'import'; $this->addArgument('csv_filename', sfCommandArgument::REQUIRED, 'Název csv souboru'); } protected function execute($arguments = array(), $options = array()) { $databaseManager = new sfDatabaseManager($this->configuration); switch($arguments['csv_filename']) { case 'vzv-celni.csv': $this->importVoziky('import/vzv-celni.csv'); break; case 'vzv-ctyrcestne.csv': $this->importVoziky('import/vzv-ctyrcestne.csv'); break; case 'skladova-technika.csv': $this->importSkladova('import/skladova-technika.csv'); break; case 'specialky.csv': $this->importSkladova('import/specialky.csv'); break; case 'bazar.csv': $this->importBazar('import/bazar.csv'); break; case 'trimme': $this->trimme(); break; case 'picfix': $this->smallPicFix(); break; } } public function importVoziky($csv_path) { if(($file = file($csv_path)) !== false) { foreach($file as $row) { $Product = new Product(); foreach(explode(';', $row) as $i => $field) { $field = trim(str_replace('"', '', $field)); if(!empty($field)) { switch($i) { case 0: $Equipment = Doctrine::getTable('Equipment')->findOneBySlug($field); if(!$Equipment) die('Kategorie '.$field.' nenalezena'."\n"); $Product->setEquipmentId($Equipment->getId()); break; case 1: $Product->setName($field); break; case 2: $Product->setProducer($field); break; case 3: $Product->setTonnage($field); break; case 4: $Product->setUplift($field); break; case 5: $Product->setGear($field); break; } } } $Product->save(); foreach(explode(';', $row) as $i => $field) { $field = trim(str_replace('"', '', $field)); if(!empty($field)) { switch($i) { case 6: $this->newFile($field, $Product); break; case 7: $this->newImage($field, $Product); break; } } } echo $Product->getSlug()."\n"; } } } public function importSkladova($csv_path) { if(($file = file($csv_path)) !== false) { foreach($file as $row) { $Product = new Product(); foreach(explode(';', $row) as $i => $field) { $field = trim(str_replace('"', '', $field)); if(!empty($field)) { switch($i) { case 0: $Equipment = Doctrine::getTable('Equipment')->findOneBySlug($field); if(!$Equipment) die('Kategorie '.$field.' nenalezena'."\n"); $Product->setEquipmentId($Equipment->getId()); break; case 1: $Product->setName($field); break; case 2: $Product->setProducer($field); break; case 3: $Product->setTonnage($field); break; case 4: $Product->setUplift($field); break; case 5: $Product->setGear($field); break; case 6: $Product->setTypeNote($field); break; } } } $Product->save(); foreach(explode(';', $row) as $i => $field) { $field = trim(str_replace('"', '', $field)); if(!empty($field)) { switch($i) { case 7: $this->newFile($field, $Product); break; case 8: $this->newImage($field, $Product); break; } } } echo $Product->getSlug()."\n"; } } } private function newFile($filename, $Product) { $name = $filename; $path = 'import/files/'.$filename.'.pdf'; if(file_exists($path) && !empty($filename)) { $dir = 'web/uploads/files/products/'.$Product->getSlug(); if(!is_dir($dir)) { mkdir($dir, 0777, true); } $filename = $filename.'.pdf'; copy($path, $dir.'/'.$filename); $File = new File(); $File->setProductId($Product->getId()); $File->setName($name); $File->setFilename($filename); $File->setSize(filesize($path)); $File->save(); } else { return false; } } private function newImage($filename, $Product) { $name = $filename; $path = 'import/images/'.$filename.'.jpg'; if(file_exists($path) && !empty($filename)) { $dir = 'web/uploads/images/products/'.$Product->getSlug(); if(!is_dir($dir)) { mkdir($dir, 0777, true); } mkdir($dir.'/small', 0777); mkdir($dir.'/middle', 0777); mkdir($dir.'/big', 0777); $filename = $filename.'.jpg'; copy($path, $dir.'/small/'.$filename); copy($path, $dir.'/middle/'.$filename); copy($path, $dir.'/big/'.$filename); $Image = new Image(); $Image->setProductId($Product->getId()); $Image->setFilename($filename); $Image->setMain(true); $Image->save(); } else { return false; } } public function trimme() { foreach(Doctrine::getTable('Product')->findAll() as $Product) { foreach($Product->getEquipment()->getProductParameters() as $param) { if(is_string($Product[$param])) { $Product[$param] = trim($Product[$param]); } } $Product->save(); } echo "Done\n"; } public function smallPicFix() { $thumbnail = new sfThumbnail(127, 100, true, true, 75); foreach(Doctrine::getTable('Product')->findAll() as $Product) { foreach($Product->getImages() as $Image) { echo $Image->getFilename(); $thumbnail->loadFile('web/'.$Image->getPath(Image::TYPE_SMALL, false)); $thumbnail->save('web/'.$Image->getPath(Image::TYPE_SMALL, false)); echo "\tOK\n"; } } echo "Done\n"; } public static function getCSVFields() { $result = array(); foreach(Doctrine::getTable('Product')->getFieldNames() as $field) { if(!in_array($field, array('id', 'slug', 'equipment_id', 'version', 'updated_at', 'created_at'))) { $result[] = $field; } if($field == 'equipment_id') { $result[] = 'equipment_slug'; } } $result[] = 'pictures'; $result[] = 'files'; return $result; } public function importBazar($csv_path) { if(($file = file($csv_path)) !== false) { foreach($file as $row) { $Product = new Product(); foreach(explode(';', $row) as $i => $field) { $field = trim(str_replace('"', '', $field)); if(!empty($field)) { switch($i) { case 0: $Equipment = Doctrine::getTable('Equipment')->findOneBySlug($field); if(!$Equipment) die('Kategorie '.$field.' nenalezena'."\n"); $Product->setEquipmentId($Equipment->getId()); break; case 1: $Product->setProducer($field); break; case 2: $Product->setName($field); break; case 3: $Product->setTonnage($this->toInt($field)); break; case 4: $Product->setUplift($this->toInt($field)); break; case 5: $Product->setGear($field); break; case 6: $Product->setYear($field); break; case 7: $Product->setMotoHours($field); break; case 8: $Product->setPrice($this->toInt($field)); break; } } } $Product->save(); echo $Product->getSlug()."\n"; } } } protected function toInt($str) { $str = trim(str_replace(' ', '', $str)); return $str * 1; } }