/** * Duplicate one product and return model * * @param ShopProduct $model * @return ShopProduct */ public function duplicateProduct(ShopProduct $model) { $product = new ShopProduct(null); // Add "null" by PANIX $product->attributes = $model->attributes; $behaviors = $model->behaviors(); foreach ($behaviors['TranslateBehavior']['translateAttributes'] as $attr) { $product->{$attr} = $model->{$attr}; } $product->name .= $this->getSuffix(); $product->seo_alias .= CMS::translit($this->getSuffix()) . '-' . time(); if ($product->validate()) { if ($product->save(false, false)) { foreach ($this->duplicate as $feature) { $method_name = 'copy' . ucfirst($feature); if (method_exists($this, $method_name)) { $this->{$method_name}($model, $product); } } return $product; } else { die(__FUNCTION__ . ': Error save'); return false; } } else { // print_r($product->getErrors());die; } }
/** * Import catalog products */ public function importProducts() { foreach ($this->xml->{"Каталог"}->{"Товары"}->{"Товар"} as $product) { $createExId = false; $model = C1ExternalFinder::getObject(C1ExternalFinder::OBJECT_TYPE_PRODUCT, $product->{"Ид"}); if (!$model) { $model = new ShopProduct(); // Add "null" by PANIX $model->type_id = self::DEFAULT_TYPE; $model->price = 0; $model->switch = 1; $createExId = true; } $model->name = $product->{"Наименование"}; $model->seo_alias = CMS::translit($model->name); $model->sku = $product->{"Артикул"}; if ($model->save(false, false)) { } else { // Yii::log(CJSON::encode($model->getErrors()),'info','application'); } // Create external id if ($createExId === true) { $this->createExternalId(C1ExternalFinder::OBJECT_TYPE_PRODUCT, $model->id, $product->{"Ид"}); } // Set category $categoryId = C1ExternalFinder::getObject(C1ExternalFinder::OBJECT_TYPE_CATEGORY, $product->{"Группы"}->{"Ид"}, false); $model->setCategories(array($categoryId), $categoryId); // Set image // $image = C1ProductImage::create($this->buildPathToTempFile($product->{"Картинка"})); // if ($image && !$model->mainImage) /// $model->addImage($image); if (!empty($product->{"Картинка"})) { foreach ($product->{"Картинка"} as $pi) { $image = C1ProductImage::create($this->buildPathToTempFile($pi)); if ($image && !$model->mainImage) { $model->addImage($image); } } } // Process properties if (isset($product->{"ЗначенияСвойств"}->{"ЗначенияСвойства"})) { $attrsdata = array(); foreach ($product->{"ЗначенияСвойств"}->{"ЗначенияСвойства"} as $attribute) { $attributeModel = C1ExternalFinder::getObject(C1ExternalFinder::OBJECT_TYPE_ATTRIBUTE, $attribute->{"Ид"}); if ($attributeModel && $attribute->{"Значение"} != '') { $cr = new CDbCriteria(); $cr->with = 'option_translate'; $cr->compare('option_translate.value', $attribute->{"Значение"}); $option = ShopAttributeOption::model()->find($cr); if (!$option) { $option = $this->addOptionToAttribute($attributeModel->id, $attribute->{"Значение"}); } $attrsdata[$attributeModel->name] = $option->id; } } if (!empty($attrsdata)) { $model->setEavAttributes($attrsdata, true); } } } }
/** * Create/update product from key=>value array * @param $data array of product attributes */ protected function importRow($data) { if (!isset($data['category']) || empty($data['category'])) { $data['category'] = 'root'; } $newProduct = false; $category_id = $this->getCategoryByPath($data['category']); // Search product by name, category // or create new one $cr = new CDbCriteria(); $cr->with = array('translate'); // if (isset($data['seo_alias']) && !empty($data['seo_alias']) && $data['seo_alias'] != '') // $cr->compare('t.seo_alias', $data['seo_alias']); if (isset($data['sku']) && !empty($data['sku']) && $data['sku'] != '') { $cr->compare('t.sku', $data['sku']); } else { $cr->compare('translate.name', $data['name']); } $model = ShopProduct::model()->applyCategories($category_id)->find($cr); if (!$model) { $newProduct = true; $model = new ShopProduct(); $this->stats['create']++; } else { $this->stats['update']++; } $model->name = $data['name']; $model->price = $data['price']; $model->seo_alias = CMS::translit($data['name']); // Process product type $model->type_id = $this->getTypeIdByName($data['type']); // Manufacturer if (isset($data['manufacturer']) && !empty($data['manufacturer'])) { $model->manufacturer_id = $this->getManufacturerIdByName($data['manufacturer']); } // Update product variables and eav attributes. $attributes = new XmlAttributesProcessor($model, $data['attributes']); if ($model->validate()) { $categories = array($category_id); if (isset($data['additionalCategories'])) { $categories = array_merge($categories, $this->getAdditionalCategories($data['additionalCategories'])); } if (!$newProduct) { foreach ($model->categorization as $c) { $categories[] = $c->category; } $categories = array_unique($categories); } // Save product $model->save(false, false); // Update EAV data $attributes->save(); // Update categories $model->setCategories($categories, $category_id); // Process product main image if product doesn't have one $this->setImages($model, $data); } else { $errors = $model->getErrors(); $error = array_shift($errors); $this->errors[] = array('line' => $this->line, 'error' => $error[0]); } }
function shop_save_product($edit = 0) { global $xoopsSecurity, $xoopsUser, $xoopsModule, $xoopsModuleConfig; $q = ''; foreach ($_POST as $k => $v) { ${$k} = $v; if ($k == 'XOOPS_TOKEN_REQUEST' || $k == 'action') { continue; } $q .= $q == '' ? "{$k}=" . rawurlencode($v) : "&{$k}=" . rawurlencode($v); } $q = "action=" . ($edit ? 'edit&id=' . $id : 'new') . '&' . $q; if (!$xoopsSecurity->check()) { redirectMsg("products.php?{$q}", __('Session token expired!', 'shop'), 1); die; } if ($edit) { if ($id <= 0) { redirectMsg("products.php", __('Product ID has not been provided', 'shop'), 1); die; } $product = new ShopProduct($id); if ($product->isNew()) { redirectMsg("products.php", __('Specified product does not exists!', 'shop'), 1); die; } } else { $product = new ShopProduct(); } if ($name == '' || $description == '' || empty($cats)) { redirectMsg("products.php?{$q}", __('Please fill al required data!', 'shop'), 1); die; } if (!isset($nameid)) { $nameid = TextCleaner::getInstance()->sweetstring($name); } /** * Comprobamos que no exista otra página con el mismo título */ $db = XoopsDatabaseFactory::getDatabaseConnection(); $sql = "SELECT COUNT(*) FROM " . $db->prefix("shop_products") . " WHERE nameid='{$nameid}'"; $sql .= $edit ? " AND id_product<>" . $product->id() : ''; list($num) = $db->fetchRow($db->query($sql)); if ($num > 0) { redirectMsg('products.php?' . $q, __('Another product with same name already exists!', 'shop'), 1); die; } #Guardamos los datos del Post $product->setVar('name', $name); $product->setVar('nameid', $nameid); $product->setVar('description', $description); $product->setVar('price', $price); $product->setVar('buy', $buy != '' && $buy != 'http://' ? $buy : ''); $product->setVar('type', $type); $product->setVar('available', $available); // Add Metas foreach ($meta_name as $k => $v) { $product->add_meta($v, $meta_value[$k]); } $product->add_categories($cats, true); //Imagen include_once RMCPATH . '/class/uploader.php'; $folder = XOOPS_UPLOAD_PATH . '/minishop'; $folderths = XOOPS_UPLOAD_PATH . '/minishop/ths'; if ($edit) { $image = $product->getVar('image'); $filename = $product->getVar('image'); } else { $filename = ''; } //Obtenemos el tamaño de la imagen $thSize = explode("|", $xoopsModuleConfig['thssize']); $imgSize = explode("|", $xoopsModuleConfig['imgsize']); $up = new RMFileUploader($folder, $xoopsModuleConfig['maxsize'] * 1024, array('jpg', 'png', 'gif')); if ($up->fetchMedia('image')) { if (!$up->upload()) { redirectMsg('./products.php', $up->getErrors(), 1); die; } if ($edit && $product->getVar('image') != '') { @unlink(XOOPS_UPLOAD_PATH . '/minishop/' . $product->getVar('image')); @unlink(XOOPS_UPLOAD_PATH . '/minishop/ths/' . $product->getVar('image')); } $filename = $up->getSavedFileName(); $fullpath = $up->getSavedDestination(); // Redimensionamos la imagen $redim = new RMImageResizer($fullpath, $fullpath); if ($xoopsModuleConfig['imgredim']) { $redim->resizeAndCrop($imgSize[0], $imgSize[1]); } else { $redim->resizeWidthOrHeight($imgSize[0], $imgSize[1]); } $redim->setTargetFile($folderths . "/{$filename}"); if ($xoopsModuleConfig['thsredim']) { $redim->resizeAndCrop($thSize[0], $thSize[1]); } else { $redim->resizeWidthOrHeight($thSize[0], $thSize[1]); } } $product->setVar('image', $filename); if ($product->save()) { redirectMsg("products.php", __('Database updated successfully!', 'shop'), 0); } else { redirectMsg("products.php?{$q}", __('Errors ocurred while trying to update database', 'shop') . "<br />" . $product->errors(), 1); } }