first commit
This commit is contained in:
241
plugins/smImportCsvPlugin/lib/smImportCsvFull.class.php
Normal file
241
plugins/smImportCsvPlugin/lib/smImportCsvFull.class.php
Normal file
@@ -0,0 +1,241 @@
|
||||
<?php
|
||||
|
||||
class smImportCsvFull {
|
||||
|
||||
protected $handle = null;
|
||||
|
||||
protected $item_per_step = 20;
|
||||
|
||||
protected $msg = '';
|
||||
|
||||
public function import($step) {
|
||||
$this->handle = fopen(sfConfig::get('sf_cache_dir').'/import_custom.csv', 'r+');
|
||||
fseek($this->handle, $step);
|
||||
|
||||
$this->readData($step);
|
||||
|
||||
$pos = ftell($this->handle);
|
||||
fclose($this->handle);
|
||||
return $pos;
|
||||
}
|
||||
|
||||
public function readData($step = 0) {
|
||||
$items_read = 0;
|
||||
while (!feof($this->handle) && $items_read < $this->item_per_step) {
|
||||
$data = fgetcsv($this->handle, null, "\t", '"');
|
||||
|
||||
if ($items_read == 0 && $step == 0 && $data[0] != 'symbol' && $data[3] != 'name')
|
||||
throw new Exception('Nieprawidłowy format pliku.', 1);
|
||||
|
||||
$this->processData($data);
|
||||
$items_read++;
|
||||
}
|
||||
}
|
||||
|
||||
public function processData($data) {
|
||||
foreach ($data as $key => $value)
|
||||
$nD[$key] = trim(iconv('windows-1250','UTF-8',$value));
|
||||
$data = $nD;
|
||||
|
||||
if ($data[0] == '' || $data[0] == 'symbol')
|
||||
return false;
|
||||
|
||||
$code = iconv('windows-1250','UTF-8', $data[0]);
|
||||
|
||||
$c = new Criteria();
|
||||
$c->add(ProductPeer::CODE, $code);
|
||||
$p = ProductPeer::doSelectOne($c);
|
||||
if (!is_object($p)) {
|
||||
$p = new Product();
|
||||
$p->setCode($code);
|
||||
}
|
||||
|
||||
$p->setOptName($data[3]);
|
||||
|
||||
$p->setWeight(str_replace("," , ".", $data[5]));
|
||||
|
||||
$p->setActive($data[6]);
|
||||
|
||||
$ct = new Criteria();
|
||||
$ct->add(TaxPeer::VAT, 23);
|
||||
$tax = TaxPeer::doSelectOne($ct);
|
||||
if(!is_object($tax)) {
|
||||
$tax = new Tax();
|
||||
$tax->setVat(23);
|
||||
$tax->setVatName('23%');
|
||||
$tax->save();
|
||||
}
|
||||
$p->setTax($tax);
|
||||
$p->setOptVat(23);
|
||||
$p->setCulture('pl_PL');
|
||||
|
||||
|
||||
$p->setPriceNetto(str_replace("," , ".", $data[10]));
|
||||
$p->setPriceBrutto(str_replace("," , ".", $data[11]));
|
||||
|
||||
$p->setWholesaleANetto(0.00);
|
||||
$p->setWholesaleABrutto(0.00);
|
||||
|
||||
if (!empty($data[24]))
|
||||
{
|
||||
$cm = new Criteria();
|
||||
|
||||
$cm->add(ProducerI18nPeer::NAME, $data[24]);
|
||||
|
||||
$cm->add(ProducerI18nPeer::CULTURE, 'pl_PL');
|
||||
|
||||
$producer = ProducerPeer::doSelectWithI18n($c);
|
||||
|
||||
if (!is_object($producer))
|
||||
{
|
||||
$producer = new Producer();
|
||||
|
||||
$producer->setCulture('pl_PL');
|
||||
|
||||
$producer->setName($data[24]);
|
||||
|
||||
$producer->save();
|
||||
}
|
||||
}
|
||||
|
||||
$p->setProducer($producer);
|
||||
|
||||
$p->setStock(str_replace("," , ".", $data[12]));
|
||||
|
||||
$p->setUom($data[19]);
|
||||
|
||||
$p->setManCode($data[20]);
|
||||
|
||||
$p->setExecutionTime(str_replace("'" , "", $data[21]));
|
||||
|
||||
$p->setShortDescription(str_replace( '\n', '', $data[29]));
|
||||
|
||||
$p->setDescription(str_replace( '\n', '', $data[30]));
|
||||
|
||||
if ($data[22] == 1)
|
||||
{
|
||||
$this->addProductToGroupType($p, 'SALES');
|
||||
}
|
||||
|
||||
if ($data[23] == 1)
|
||||
{
|
||||
$this->addProductToGroupType($p, 'MAIN_PAGE');
|
||||
}
|
||||
|
||||
if ($data[25] == 1)
|
||||
{
|
||||
$this->addProductToGroupType($p, 'RECOMMEND');
|
||||
}
|
||||
|
||||
if ($data[26] == 1)
|
||||
{
|
||||
$this->addProductToGroupType($p, 'NEW');
|
||||
}
|
||||
|
||||
if ($data[27] == 1)
|
||||
{
|
||||
$this->addProductToGroupType($p, 'PROMOTION');
|
||||
}
|
||||
|
||||
if ($data[42] && $data[43] && $data[44])
|
||||
{
|
||||
$php = new ProductHasPositioning();
|
||||
|
||||
$php->setCulture('pl_PL');
|
||||
|
||||
$php->setTitle($data[44]);
|
||||
|
||||
$php->setKeywords($data[43]);
|
||||
|
||||
$php->setDescription($data[42]);
|
||||
|
||||
$p->addProductHasPositioning($php);
|
||||
}
|
||||
|
||||
$images = explode( ',', $data[15] );
|
||||
|
||||
foreach ($images as $image) {
|
||||
|
||||
if ($image === $data[16]) {
|
||||
|
||||
$this->addImage($p, 'http://vulcania.pl/img/72171/'.$image, true);
|
||||
|
||||
} else {
|
||||
|
||||
$this->addImage($p, 'http://vulcania.pl/img/72171/'.$image);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$p->save();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function addImage($p, $imageFilename, $default = false) {
|
||||
|
||||
$fileInfo = pathinfo($imageFilename);
|
||||
if (!empty($imageFilename) && !empty($fileInfo['extension'])) {
|
||||
$c = new Criteria();
|
||||
$c->add(sfAssetPeer::FILENAME, $fileInfo['basename']);
|
||||
$sfAsset = sfAssetPeer::doSelectOne($c);
|
||||
|
||||
if (!is_object($sfAsset)) {
|
||||
$b = new sfWebBrowser(array(), 'sfCurlAdapter', array('ssl_verify' => false));
|
||||
$b->get($imageFilename);
|
||||
$image = $b->getResponseText();
|
||||
|
||||
$input_file = sfConfig::get('sf_upload_dir').DIRECTORY_SEPARATOR.'assets'.DIRECTORY_SEPARATOR.$fileInfo['basename'];
|
||||
file_put_contents($input_file, $image);
|
||||
|
||||
$asset = new ProductHasSfAsset();
|
||||
$asset->setProductId($p->getId());
|
||||
$asset->createAsset($fileInfo['basename'], $input_file, ProductHasSfAssetPeer::IMAGE_FOLDER, $fileInfo['basename']);
|
||||
if ($default == true) $asset->setIsDefault(true);
|
||||
$asset->save();
|
||||
} else {
|
||||
$c = new Criteria();
|
||||
$c->add(ProductHasSfAssetPeer::SF_ASSET_ID, $sfAsset->getId());
|
||||
$c->add(ProductHasSfAssetPeer::PRODUCT_ID, $p->getId());
|
||||
if (!ProductHasSfAssetPeer::doCount($c)) {
|
||||
$asset = new ProductHasSfAsset();
|
||||
$asset->setProductId($p->getId());
|
||||
$asset->setSfAssetId($sfAsset->getId());
|
||||
if ($default == true) $asset->setIsDefault(true);
|
||||
$asset->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function getProductGroupByType($type)
|
||||
{
|
||||
$c = new Criteria();
|
||||
|
||||
$c->add(ProductGroupPeer::PRODUCT_GROUP, $type);
|
||||
|
||||
return ProductGroupPeer::doSelectOne($c);
|
||||
}
|
||||
|
||||
protected function addProductToGroupType($p, $group_type)
|
||||
{
|
||||
$product_group = $this->getProductGroupByType($group_type);
|
||||
|
||||
$product_group_has_product = new ProductGroupHasProduct();
|
||||
|
||||
$product_group_has_product->setProduct($p);
|
||||
|
||||
$product_group_has_product->setProductGroup($product_group);
|
||||
|
||||
$p->addProductGroupHasProduct($product_group_has_product);
|
||||
}
|
||||
|
||||
public function close() {
|
||||
|
||||
}
|
||||
|
||||
public function getMessage() {
|
||||
return $this->msg;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
class smImportCsvListener {
|
||||
|
||||
public static function generateStProduct(sfEvent $event) {
|
||||
$event->getSubject()->attachAdminGeneratorFile('smImportCsvPlugin', 'stProduct.yml');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user