/** * 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; } }
public function alert($type, $text, $close = true) { $id = 'alert' . md5($type . CMS::translit($text)); if (!isset($_COOKIE[$id])) { $str = implode(', ', $this->types); if (in_array($type, $this->types)) { $this->render('_' . __FUNCTION__, array('type' => $type, 'message' => $text, 'close' => $close)); } else { throw new CHttpException(403, Yii::t('app', 'ERROR_TPL_' . strtoupper(__FUNCTION__), array('{tpl}' => __FUNCTION__, '{type}' => $type, '{types}' => CHtml::encode($str)))); } } }
protected function validateAttribute($object, $attribute) { $config = Yii::app()->settings->get('core'); if (isset($object->translateAttributes)) { if (in_array($this->translitAttribute, $object->translateAttributes)) { $p = array(); foreach ($object->translateAttributes as $param) { $p[$param] = $param; } $attr = $p[$this->translitAttribute]; } } else { $attr = $this->translitAttribute; if (!$object->hasAttribute($attr)) { throw new CException(Yii::t('yii', 'Active record "{class}" is trying to select an invalid column "{column}". Note, the column must exist in the table or be an expression with alias.', array('{class}' => get_class($object), '{column}' => $attr))); } } if ($config['translate_object_url']) { Yii::import('mod.core.components.yandexTranslate'); $t = new yandexTranslate(); $tr = $t->translitUrl(array('ru', 'en'), $object->getAttribute($attr)); //die($tr); } else { $tr = CMS::translit($object->getAttribute($attribute)); } if ($object->isNewRecord) { $model = $object::model()->find(array('condition' => '`t`.`' . $attribute . '`=:alias', 'params' => array(':alias' => $tr))); } else { $model = $object::model()->find(array('condition' => '`t`.`' . $attribute . '`=:alias AND `t`.`id`!=' . $object->id, 'params' => array(':alias' => $tr))); } if (isset($model)) { $this->addError($object, $attribute, Yii::t('app', 'ERROR_DUPLICATE_URL', array('{url}' => $tr))); } else { $object->{$attribute} = $tr; } }
/** * @param $data * @param null|ShopCategory $parent */ public function importCategories($data, $parent = null) { foreach ($data->{"Группа"} as $category) { // Find category by external id $model = C1ExternalFinder::getObject(C1ExternalFinder::OBJECT_TYPE_CATEGORY, $category->{"Ид"}); if (!$model) { $model = new ShopCategory(); $model->name = $category->{"Наименование"}; $model->seo_alias = CMS::translit($category->{"Наименование"}); $model->appendTo($this->getRootCategory()); $this->createExternalId(C1ExternalFinder::OBJECT_TYPE_CATEGORY, $model->id, $category->{"Ид"}); } if ($parent === null) { $model->moveAsLast($this->getRootCategory()); } else { $model->moveAsLast($parent); } $model->saveNode(); // Process subcategories if (isset($category->{"Группы"})) { $this->importCategories($category->{"Группы"}, $model); } } }
public function change($id, $data, $item) { if (is_object($item)) { $item->id = $id; } $file = $_FILES['value']; $name = trim($file['name']); $tmp_name = trim($file['tmp_name']); if ($tmp_name != '') { $dir = "./" . Core::option('files_name') . "/vars/{$id}"; CMS::mkdirs($dir, 0775); foreach (IO_FS::Dir($dir) as $f) { @IO_FS::rm($f->path); } $name = CMS::translit(mb_strtolower($name)); $name = preg_replace('{\\s+}', '_', $name); $name = trim(preg_replace('{[^a-z0-9_\\.\\-]}', '', $name)); if ($name == '') { $name = 'noname'; } if ($name[0] == '.') { $name = "noname.{$name}"; } move_uploaded_file($tmp_name, "{$dir}/{$name}"); chmod("{$dir}/{$name}", 0775); $rc = CMS::vars()->on_change_call($id, $value, $data); if (is_string($rc)) { $item->valuesrc = $data['valuesrc']; return $rc; } $item->value = "{$dir}/{$name}"; $item->update_value(); } }
/** * Create/update product * @param bool $new * @throws CHttpException */ public function actionUpdate($new = false) { $this->topButtons = false; $config = Yii::app()->settings->get('shop'); if ($new === true) { $model = new ShopProduct(); } else { $model = ShopProduct::model()->language(Yii::app()->language->active)->findByPk($_GET['id']); } if (!$model) { throw new CHttpException(404, Yii::t('ShopModule.admin', 'NO_FOUND_PRODUCT')); } $oldImage = $model->image; if (!$model->isNewRecord) { $this->topButtons = array(array('label' => Yii::t('ShopModule.admin', 'VIEW_PRODUCT'), 'url' => $model->getAbsoluteUrl(), 'htmlOptions' => array('class' => 'btn btn-info', 'target' => '_blank'))); } // Apply use_configurations, configurable_attributes, type_id if (isset($_GET['ShopProduct'])) { $model->attributes = $_GET['ShopProduct']; } $title = $model->isNewRecord ? Yii::t('ShopModule.admin', 'CREATE_PRODUCT') : Yii::t('ShopModule.admin', 'UPDATE_PRODUCT'); if ($model->type) { $title .= ' "' . Html::encode($model->type->name) . '"'; } $this->pageName = $title; $this->breadcrumbs = array(Yii::t('ShopModule.default', 'MODULE_NAME') => array('/admin/shop'), Yii::t('ShopModule.admin', 'PRODUCTS') => $this->createUrl('index'), $this->pageName); // On create new product first display "Choose type" form first. if ($model->isNewRecord && isset($_GET['ShopProduct']['type_id'])) { if (ShopProductType::model()->countByAttributes(array('id' => $model->type_id)) === '0') { throw new CHttpException(404, Yii::t('ShopModule.admin', 'ERR_PRODUCT_TYPE')); } } // Set main category id to have categories drop-down selected value if ($model->mainCategory) { $model->main_category_id = $model->mainCategory->id; } // Or set selected category from type pre-set. if ($model->type && !Yii::app()->request->isPostRequest && $model->isNewRecord) { $model->main_category_id = $model->type->main_category; } // Set configurable attributes on new record if ($model->isNewRecord) { if ($model->use_configurations && isset($_GET['ShopProduct']['configurable_attributes'])) { $model->configurable_attributes = $_GET['ShopProduct']['configurable_attributes']; } } $form = new TabForm($model->getForm(), $model); // $form->positionTabs = 'vertical'; // Set additional tabs $form->additionalTabs[$model->t('TAB_CAT')] = array('content' => $this->renderPartial('_tree', array('model' => $model), true)); $form->additionalTabs[$model->t('TAB_IMG')] = array('content' => $this->renderPartial('_images', array('model' => $model, 'uploadModel' => $uploadModel), true)); $form->additionalTabs[$model->t('TAB_ATTR')] = array('content' => $this->renderPartial('_attributes', array('model' => $model), true)); $form->additionalTabs[$model->t('TAB_REL')] = array('content' => $this->renderPartial('_relatedProducts', array('exclude' => $model->id, 'product' => $model), true)); //if (Yii::app()->getModule('shop')->variations) $form->additionalTabs[Yii::t('ShopModule.admin', 'UPDATE_PRODUCT_TAB_VARIANTS')] = array('content' => $this->renderPartial('_variations', array('model' => $model), true)); // if($this->isInstallModule('comments')){ // $form->additionalTabs['icon-comment'] = array( // 'content' => $this->renderPartial('_comments', array('model' => $model), true) // ); // } /* $form->additionalTabs = array( 'icon-folder-open' => array( 'content' => $this->renderPartial('_tree', array('model' => $model), true) ), 'icon-copy-3' => array( 'content' => $this->renderPartial('_relatedProducts', array('exclude' => $model->id, 'product' => $model), true) ), 'icon-images' => array( 'content' => $this->renderPartial('_images', array('model' => $model, 'uploadModel' => $uploadModel), true) ), 'icon-paragraph-justify' => array( 'content' => $this->renderPartial('_attributes', array('model' => $model), true), 'visible'=>false ), Yii::t('ShopModule.admin', 'Варианты') => array( 'content' => $this->renderPartial('_variations', array('model' => $model), true) ), 'icon-comment' => array( 'content' => $this->renderPartial('_comments', array('model' => $model), true) ), ); */ if ($model->use_configurations) { $form->additionalTabs[Yii::t('ShopModule.admin', 'UPDATE_PRODUCT_TAB_CONF')] = array('content' => $this->renderPartial('_configurations', array('product' => $model), true)); } if (isset($_GET['ShopProduct']['main_category_id'])) { $model->main_category_id = $_GET['ShopProduct']['main_category_id']; } if (Yii::app()->request->isPostRequest) { $model->attributes = $_POST['ShopProduct']; // Handle related products $model->setRelatedProducts(Yii::app()->getRequest()->getPost('RelatedProductId', array())); if ($config['auto_gen_url']) { $model->name = ShopCategory::model()->findByPk($model->main_category_id)->name . ' ' . ShopManufacturer::model()->findByPk($model->manufacturer_id)->name . ' ' . $model->sku; $model->seo_alias = CMS::translit($model->name); // die($model->name.$model->seo_alias); } /* if($model->currency_id){ $currency = ShopCurrency::model()->findByPk($model->currency_id); $convertPrice = $model->price*$currency->rate/$currency->rate_old; $model->price=$convertPrice; } */ if ($model->validate() && $this->validateAttributes($model)) { $model->uploadFile('image', 'webroot.uploads.product', $oldImage); $model->save(); // Process categories $mainCategoryId = 1; if (isset($_POST['ShopProduct']['main_category_id'])) { $mainCategoryId = $_POST['ShopProduct']['main_category_id']; } $model->setCategories(Yii::app()->request->getPost('categories', array()), $mainCategoryId); // Process attributes $this->processAttributes($model); // Process variants $this->processVariants($model); // Process configurations $this->processConfigurations($model); // Handle images $this->handleUploadedImages($model); // Set main image $this->updateMainImage($model); // Update image titles $this->updateImageTitles(); $model->save(false, false); $this->redirect(array('index')); } else { $this->setFlashMessage(Yii::t('ShopModule.admin', 'ERR_PRODUCT_TYPE')); } } $this->render('update', array('model' => $model, 'form' => $form)); }
?> <?php if ($close) { ?> alert-dismissible fade in<?php } ?> " id="alert<?php echo md5($type . CMS::translit($message)); ?> "> <?php if ($close) { ?> <button type="button" onClick="alertClose('<?php echo md5($type . CMS::translit($message)); ?> '); return false;" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button> <?php } ?> <?php if ($type == 'success') { $icon = 'fa-check'; } elseif ($type == 'danger') { $icon = 'flaticon-warning'; } elseif ($type == 'info') { $icon = 'flaticon-info'; } ?> <i class="<?php
protected function uploaded_filename($name, $data, $file) { return CMS::translit(preg_replace('{[\\s+]+}', '-', trim($file['name']))); }
protected function validate_filename($name) { $name = trim($name); $name = preg_replace('{\\s+}sm', ' ', $name); $name = str_replace(' ', '_', $name); $name = CMS::translit($name); return $name; }
/** * Find or create manufacturer * @param $name * @return integer */ public function getManufacturerIdByName($name) { if (isset($this->manufacturerCache[$name])) { return $this->manufacturerCache[$name]; } $cr = new CDbCriteria(); $cr->with = array('man_translate'); $cr->compare('man_translate.name', $name); $model = ShopManufacturer::model()->find($cr); if (!$model) { $model = new ShopManufacturer(); $model->name = $name; $model->seo_alias = CMS::translit($name); if ($model->validate()) { $model->save(false, false); } } $this->manufacturerCache[$name] = $model->id; return $model->id; }
/** * 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(null); // Add "null" by PANIX $this->stats['date_create']++; } else { $this->stats['date_update']++; } $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 CsvAttributesProcessor($model, $data); 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 if (isset($data['image']) && !empty($data['image'])) { $image = CsvImage::create($data['image']); if ($image && $model->mainImage === null) { $model->addImage($image); } if ($image && $this->deleteDownloadedImages) { $image->deleteTempFile(); } } } else { $errors = $model->getErrors(); $error = array_shift($errors); $this->errors[] = array('line' => $this->line, 'error' => $error[0]); } }