/** * Создает новую модель категории. * Если создание прошло успешно - перенаправляет на просмотр. * * @return void */ public function actionCreate() { $model = new StoreCategory(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (($data = Yii::app()->getRequest()->getPost('StoreCategory')) !== null) { $model->setAttributes($data); if ($model->save()) { Yii::app()->user->setFlash(yupe\widgets\YFlashMessages::SUCCESS_MESSAGE, Yii::t('StoreModule.store', 'Record was created!')); $this->redirect((array) Yii::app()->getRequest()->getPost('submit-type', ['create'])); } } $this->render('create', ['model' => $model]); }
public function actionAjax_Create() { if (isset($_POST['StoreCategory'])) { $model = new StoreCategory(); //set the submitted values $model->attributes = $_POST['StoreCategory']; //return the JSON result to provide feedback. if ($model->save(false)) { echo json_encode(array('success' => true, 'id' => $model->primaryKey)); exit; } else { echo json_encode(array('success' => false)); exit; } } }
public function actionCat() { $db = Yii::app()->db; Yii::app()->setImport(['application.modules.store.models.*']); //Создаем категории $cat1 = $db->createCommand("SELECT * FROM category_promItem c WHERE c.id_parent=0")->queryAll(); foreach ($cat1 as $c1) { $slug = \yupe\helpers\YText::translit($c1['name_rus']) . $c1['id']; $StoreCategory1 = StoreCategory::model()->findByAttributes(['slug' => $slug]); if (empty($StoreCategory1)) { $StoreCategory1 = new StoreCategory(); $StoreCategory1->name = $c1['name_rus']; $StoreCategory1->slug = $slug; $StoreCategory1->parent_id = 1; $StoreCategory1->save(); } $cat2 = $db->createCommand("SELECT * FROM category_promItem c WHERE c.id_parent=" . $c1['id'])->queryAll(); foreach ($cat2 as $c2) { $slug = \yupe\helpers\YText::translit($c2['name_rus']) . $c2['id']; $StoreCategory2 = StoreCategory::model()->findByAttributes(['slug' => $slug]); if (empty($StoreCategory2)) { $StoreCategory2 = new StoreCategory(); $StoreCategory2->name = $c2['name_rus']; $StoreCategory2->slug = $slug; $StoreCategory2->parent_id = $StoreCategory1->id; $StoreCategory2->save(); } $cat3 = $db->createCommand("SELECT * FROM category_promItem c WHERE c.id_parent=" . $c2['id'])->queryAll(); foreach ($cat3 as $c3) { $slug = \yupe\helpers\YText::translit($c3['name_rus']) . $c3['id']; $StoreCategory3 = StoreCategory::model()->findByAttributes(['slug' => $slug]); if (empty($StoreCategory3)) { $StoreCategory3 = new StoreCategory(); $StoreCategory3->name = $c3['name_rus']; $StoreCategory3->slug = $slug; $StoreCategory3->parent_id = $StoreCategory2->id; $StoreCategory3->save(); $cat4 = $db->createCommand("SELECT * FROM category_promItem c WHERE c.id_parent=" . $c3['id'])->queryAll(); foreach ($cat4 as $c4) { $slug = \yupe\helpers\YText::translit($c4['name_rus']) . $c4['id']; $StoreCategory4 = StoreCategory::model()->findByAttributes(['slug' => $slug]); if (empty($StoreCategory4)) { $StoreCategory4 = new StoreCategory(); $StoreCategory4->name = $c4['name_rus']; $StoreCategory4->slug = $slug; $StoreCategory4->parent_id = $StoreCategory3->id; $StoreCategory4->save(); } } } } } } //Создаем атрибуты /*$ids_item = []; $propertys = $db->createCommand("SELECT * FROM `property`")->queryAll(); foreach($propertys as $property) { $item = $db->createCommand("SELECT * FROM `items` WHERE id=".$property['id_item'])->queryRow(); $name = \yupe\helpers\YText::translit($item['name']).$property['id_category']; $Attribute = Attribute::model()->findByAttributes(['name' => $name]); if(empty($Attribute)) { $Attribute = new Attribute; $Attribute->name = $name; $Attribute->title = $item['name']; $Attribute->type = Attribute::TYPE_DROPDOWN; $Attribute->required = true; $Attribute->save(); $values = explode('|', $property['array_value']); foreach($values as $value) { $AttributeOption = new AttributeOption; $AttributeOption->attribute_id = $Attribute->id; $AttributeOption->value = $value; $AttributeOption->save(); } } $ids_item[$property['id']] = $Attribute->id; } //Создаем типы $cats = $db->createCommand("SELECT c1.* FROM category c1 LEFT JOIN category c2 ON c1.id=c2.id_parent WHERE c2.id IS NULL GROUP BY c1.name_rus")->queryAll(); foreach($cats as $cat) { $Type = new Type; $Type->name = $cat['name_rus']; $Type->save(); $props = $db->createCommand("SELECT * FROM `property` WHERE id_category=".$cat['id'])->queryAll(); foreach($props as $prop) { $TypeAttribute = new TypeAttribute; $TypeAttribute->type_id = $Type->id; $TypeAttribute->attribute_id = $ids_item[$prop['id']]; $TypeAttribute->save(); } for($i=2;$i<9;++$i) { $TypeAttribute = new TypeAttribute; $TypeAttribute->type_id = $Type->id; $TypeAttribute->attribute_id = $i; $TypeAttribute->save(); } } echo "\n";*/ }
public function run($args) { $max_id = isset($args[0]) ? $args[0] : 0; $root = dirname(dirname(__DIR__)); $parceBase = 'tiu_last'; $log_spec = '/tmp/importTiuSpec.log'; $log = '/tmp/importTiu.log'; $k = file_get_contents($root . $log); $uploadDirectory = Yii::getPathOfAlias('webroot') . '/uploads/'; $db = \Yii::app()->db; Yii::app()->setImport(['application.modules.store.models.*', 'application.models.*']); if ($max_id == 0) { $max_id = $db->createCommand()->select('MAX(id)')->from($parceBase . '.product')->queryScalar(); } // импорт категорий if ($k == 0) { // перед созданием категорий все чистим echo "Clear tables and files... \n"; $this->clear($uploadDirectory); echo "Set defaults... \n"; $this->setDefault(); $db->setActive(true); Yii::app()->language = 'ru'; $categories = $db->createCommand("\n SELECT name, id_item, id_parent FROM " . $parceBase . ".category_all\n ")->queryAll(); echo "Import categories... \n"; foreach ($categories as $dump_category) { $category = new StoreCategory(); $category->name_ru = $dump_category['name']; $category->tiu_id_item = $dump_category['id_item']; $category->status = 1; $name_translit = \yupe\helpers\YText::translit(str_replace(" ", "-", $dump_category['name'])); $slug = $db->createCommand()->select('slug')->from('site_store_category')->where('slug = "' . $name_translit . '"')->queryScalar(); if ($slug === FALSE) { $category->slug = $name_translit; } else { //$category->slug = time() . '_' . $name_translit; $category->slug = substr(md5($category->tiu_id_item), 0, 10) . '_' . $name_translit; //echo $category->slug."\n"; } $parent_id = $db->createCommand()->select('id')->from('site_store_category')->where('tiu_id_item = "' . $dump_category['id_parent'] . '"')->queryScalar(); $category->parent_id = $parent_id ? $parent_id : 1; if (!$category->save()) { echo "\n"; echo "Error save StoreCategory for tiu_id_item = " . $category->tiu_id_item . "\n"; echo "\n"; } } // создание типов echo "Create types... \n"; $db->setActive(true); $cats = $db->createCommand("SELECT * FROM site_store_category")->queryAll(); foreach ($cats as $cat) { $Type = new Type(); $Type->name = $cat['slug']; if (!$Type->save()) { echo "\n"; echo "Error save Type for {$cat['slug']} \n"; echo "\n"; continue; } // добавим постоянные характеристики foreach ($this->attr_id as $attr_id) { $TypeAttribute = new TypeAttribute(); $TypeAttribute->type_id = $Type->id; $TypeAttribute->attribute_id = $attr_id; $TypeAttribute->save(); } } $k = 1; file_put_contents($root . $log, $k); } // импорт продуктов echo "Import products... \n"; echo "Max ID {$max_id} \n"; for ($k; $k <= $max_id; $k++) { // $db->setActive(false); $db->setActive(true); $dump_product = $db->createCommand()->select('*')->from($parceBase . '.product')->where('id =' . $k)->queryRow(); if ($dump_product) { $name_translit = \yupe\helpers\YText::translit(str_replace(" ", "", $dump_product['name'])); if (empty($name_translit)) { file_put_contents($root . $log, $dump_product['id']); continue; // нет имени } $id_cat = $db->createCommand()->select('id_cat')->from($parceBase . '.productToCat')->where('id_product =' . $dump_product['id'])->queryScalar(); if ($id_cat === FALSE) { file_put_contents($root . $log, $dump_product['id']); continue; // нет категории } // скачаем файлы $image = $db->createCommand()->select('url')->from($parceBase . '.image')->where('id_item =' . $dump_product['id'])->queryRow(); if ($image !== null && $image['url'] != '') { $imageContent = $this->getData($image['url']); if ($imageContent) { $this->addToFiles('qqurl', $image['url']); $file = new File(); $fileinfo = $file->uploadFile($uploadDirectory, array("jpg", "png", "jpeg"), 'Product'); // создание продукта $sl = $db->createCommand()->select('slug')->from('site_store_product')->where('slug = "' . $name_translit . '"')->queryScalar(); if ($sl === FALSE) { $slug = $name_translit; } else { //$slug = time() . '_' . $name_translit; $slug = substr(md5($dump_product['id']), 0, 10) . '_' . $name_translit; } $category = $db->createCommand()->select('id, slug')->from('site_store_category')->where('tiu_id_item = "' . $id_cat . '"')->queryRow(); if ($category) { $category_id = $category['id']; $type_id = $db->createCommand()->select('id')->from('site_store_type')->where("name = '{$category['slug']}'")->queryScalar(); if ($type_id === FALSE) { print_r(" Error: can`t find StoreType for category_id = {$category_id} \n"); file_put_contents($root . $log, $dump_product['id']); continue; } $sql = "INSERT INTO `site_store_product` (`name`, `description`, `price`, `user_id`, `slug`, `category_id`, `type_id`, `tiu_id`) VALUES ('" . $dump_product["name"] . "',:description,'" . $dump_product["price"] . "',1,'" . $slug . "'," . $category_id . "," . $type_id . "," . $dump_product['id'] . ")"; } else { $sql = "INSERT INTO `site_store_product` (`name`, `description`, `price`, `user_id`, `slug`, `category_id`, `type_id`, `tiu_id`) VALUES ('" . $dump_product["name"] . "',:description,'" . $dump_product["price"] . "',1,'" . $slug . "',NULL,NULL," . $dump_product['id'] . ")"; $category_id = NULL; $type_id = NULL; } $command = $db->createCommand($sql); $command->bindParam(':description', $dump_product["description"], PDO::PARAM_STR); $sql = $db->quoteValue($sql); $command->execute(); $product_id = $db->getLastInsertID(); // id в c2go // обработаем характеристики текущего продукта $specs = $db->createCommand()->select('id_product, name, value')->from($parceBase . '.specification')->where('id_product =' . $dump_product['id'])->queryAll(); foreach ($specs as $spec) { if (!empty($spec['name']) and !empty($spec['value'])) { $name = \yupe\helpers\YText::translit($spec['name']); if (in_array($name, $this->attr_name)) { // пропускаем постоянные continue; } $sql = "INSERT INTO `site_store_product_option` (`product_id`, `title`, `value`) \n VALUES ('" . $product_id . "',:name,:value)"; $command = $db->createCommand($sql); $command->bindParam(':name', $spec["name"], PDO::PARAM_STR); $command->bindParam(':value', $spec["value"], PDO::PARAM_STR); $sql = $db->quoteValue($sql); $command->execute(); } //$this->saveSpec($spec, $product_id, $category_id, $type_id); } file_put_contents($root . $log_spec, $dump_product['id']); // связь продукта и файла if (array_key_exists('id', $fileinfo)) { $model = File::model()->findByPk($fileinfo['id']); if ($model) { $model->record_id = $product_id; $model->update(); } } print_r($dump_product['id'] . ' ' . $slug . "\n"); } } file_put_contents($root . $log, $dump_product['id']); } } \Yii::app()->mail->send('*****@*****.**', '*****@*****.**', 'Импорт', 'Импорт категорий, товаров и характеристик завершен'); //file_put_contents($root . $log, 0); }