diff --git a/common/modules/product/models/Import.php b/common/modules/product/models/Import.php index ec01d86..e9f6490 100755 --- a/common/modules/product/models/Import.php +++ b/common/modules/product/models/Import.php @@ -7,6 +7,7 @@ use common\modules\product\models\Category; use common\modules\product\models\CategoryName; use common\modules\product\models\ProductImage; use common\modules\product\models\ProductVariantType; +use common\modules\rubrication\models\TaxGroup; use common\modules\rubrication\models\TaxOption; use common\modules\rubrication\models\TaxValueString; use Yii; @@ -190,6 +191,7 @@ class Import extends Model { } public function goProducts($from = 0, $limit = null) { + set_time_limit(0); $new_products = $linked_products = 0; @@ -200,6 +202,7 @@ class Import extends Model { $filesize = filesize(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts')); + if ($from) { fseek($handle, $from); } @@ -212,6 +215,8 @@ class Import extends Model { while (($data = fgetcsv ($handle, 10000, ";")) !== FALSE && (empty($limit) || $j++ < $limit)) { + + foreach ($data as &$value) { if (!$is_utf) { @@ -229,9 +234,10 @@ class Import extends Model { } } + // 1 Группа (категория) - $catalog_name = $data[0]; - if (empty ($catalog_name)) + $catalog_names = explode(',',$data[0]); + if (empty ($catalog_names)) { $result_items[] = "Не указана категория (строка $j)"; continue; @@ -259,54 +265,43 @@ class Import extends Model { // 5 Описание Рус $product_body_ru = $data[4]; - // 6 Фильтр (через запятую) - $filters = explode (',', $data[5]); - - // 7 Доп фильтр через запятую - $filters_extra = explode (',', $data[6]); - - // 8 Пол череззапятую (мужской, женский, детский, унисекс) - $gender = explode (',', $data[7]); - - // 9 Год - $years = explode (',', $data[8]); + // 6 Фильтр (['pol'='мужской']*['god' = '2013']*['volume'='25 л']*['size'='49 x 30 x 20см']*['composition'='600D полиэстер']) + $filters = explode ('*', $data[5]); // 11 Цена акция - $product_cost_old = floatval($data[10]); + $product_cost_old = floatval($data[7]); // 10 Цена if ($product_cost_old) { - $product_cost_old = floatval($data[9]); - $product_cost = floatval($data[10]); + $product_cost_old = floatval($data[6]); + $product_cost = floatval($data[7]); } // 12 Акция - $product_akciya = (bool)$data[11]; + $product_akciya = (bool)$data[8]; // 13 Сопуд. Тов. - $similar = explode (',', $data[12]); + $similar = explode (',', $data[9]); // 14 Новинки - $product_new = (bool)$data[13]; + $product_new = (bool)$data[10]; // 15 Топ продаж - $product_top = (bool)$data[14]; + $product_top = (bool)$data[11]; - // 16 Сетка Характеристик - $feature = explode ('=', $data[15]); // 17 ВИДЕО КОД - $product_video = $data[16]; + $product_video = $data[12]; // 18 Галлерея фото - if (trim($data[17])) { - $fotos = explode (',', trim($data[17])); + if (trim($data[13])) { + $fotos = explode (',', trim($data[13])); } // 19 Штрих код товара. // расшифровал - это модификации товара! - $product_image = explode ('=', $data[18]); + $product_image = explode ('=', $data[14]); $product_image = @$product_image[3]; if ( ($_product = Product::find()->filterWhere(['ilike', 'name', trim($product_name)])->one()) === null ) { @@ -315,14 +310,20 @@ class Import extends Model { $is_new_product = empty($_product->product_id); - // ==== Set category ==== - if ( ($category = CategoryName::find()->filterWhere(['ilike', 'value', trim($catalog_name)])->one()) === null ) { - // Create category - $category = new Category(); - $category->name = trim($catalog_name); - $category->save(); + foreach($catalog_names as $catalog_name){ + // ==== Set category ==== + if ( ($category = CategoryName::find()->filterWhere(['ilike', 'value', trim($catalog_name)])->one()) === null ) { + // Create category + $category = new Category(); + $category->name = trim($catalog_name); + $category->save(); + } + + $category_id[] = $category->category_id; } - $_product->categories = [$category->category_id]; + + + $_product->categories = $category_id; // ===== Set brand ==== if ( $brand_name ) { @@ -349,6 +350,8 @@ class Import extends Model { continue; } + + if (!empty($fotos)) { foreach($fotos as $foto) { $source_image = Yii::getAlias('@uploadDir') . '/product_images/'. urlencode($foto); @@ -365,18 +368,20 @@ class Import extends Model { } } + // нужно для проставления характеристик относящихся к модификациям $MOD_ARRAY = []; - for ($i = 18; $i < count ($data); $i ++) + for ($i = 14; $i < count ($data); $i ++) { if (! empty ($data[$i])) { $mod_arr = explode ('=', $data[$i]); $mod_art = $mod_arr[0]; - $mod_size = $mod_arr[1]; + $variant_filters = explode ('*', $mod_arr[1]); $mod_color = $mod_arr[2]; $mod_image = $mod_arr[3]; + $mod_stock = isset($mod_arr[4]) ?$mod_arr[4]:1; $mod_cost = floatval($product_cost); $mod_old_cost = floatval($product_cost_old); @@ -390,18 +395,26 @@ class Import extends Model { $_productVariant->sku = $mod_art; $_productVariant->price = $mod_cost; $_productVariant->price_old = $mod_old_cost; - $_productVariant->stock = 1; + $_productVariant->stock = $mod_stock; $product_variant_type_name = ''; if (! empty ($mod_color)) { $product_variant_type_name = 'Цвет'; $_productVariant->name = $mod_color; } - elseif (! empty ($mod_size)) { - $product_variant_type_name = 'Размер'; - $_productVariant->name = $mod_size; + + if (! empty ($variant_filters)) { + + $variants_options = $this->saveFilters($variant_filters,1,$category_id); + + } + + + if (isset($variants_options) && !empty($variants_options)) { + $_productVariant->options = $variants_options; } + // ===== Set variant type ==== if ( $product_variant_type_name ) { if ( ($product_variant_type = ProductVariantType::find()->filterWhere(['ilike', 'name', $product_variant_type_name])->one()) !== null ) { @@ -413,6 +426,7 @@ class Import extends Model { $_productVariant->product_variant_type_id = $product_variant_type->product_variant_type_id; } } + $_productVariant->save(false); $MOD_ARRAY[] = $_productVariant->product_variant_id; @@ -433,84 +447,17 @@ class Import extends Model { } } - $options = []; + if (! empty ($filters)) { - // Set Naznachenie (tax_group_id = 20) - foreach($filters as $filter) { - $filter = trim($filter); - if (!$filter) { - continue; - } - if ( ($value = TaxValueString::find()->innerJoinWith('taxOption')->andFilterWhere(['ilike', 'value', $filter])->andFilterWhere(['tax_option.tax_group_id' => ProductHelper::PRODUCT_TAX_GROUP_ID_TARGET])->one()) === null ) { - // Create option - $option = new TaxOption(); - $option->tax_group_id = 20; - $option->save(); - - $value = new TaxValueString(); - $value->tax_option_id = $option->tax_option_id; - $value->value = $filter; - $value->save(); - - $option->default_value = $value->tax_value_id; - $option->save(); - } - $options[] = $value->tax_option_id; - } - } - if (! empty ($years)) { - // Set God (tax_group_id = 21) - foreach($years as $filter) { - $filter = trim($filter); - if (!$filter) { - continue; - } - if ( ($value = TaxValueString::find()->innerJoinWith('taxOption')->andFilterWhere(['ilike', 'value', $filter])->andFilterWhere(['tax_option.tax_group_id' => ProductHelper::PRODUCT_TAX_GROUP_ID_YEAR])->one()) === null ) { - // Create option - $option = new TaxOption(); - $option->tax_group_id = 21; - $option->save(); - - $value = new TaxValueString(); - $value->tax_option_id = $option->tax_option_id; - $value->value = $filter; - $value->save(); - - $option->default_value = $value->tax_value_id; - $option->save(); - } - $options[] = $value->tax_option_id; - } - } + $options = $this->saveFilters($filters,0,$category_id); - if (! empty ($gender)) { - // Set Pol (tax_group_id = 22) - foreach($gender as $filter) { - $filter = trim($filter); - if (!$filter) { - continue; - } - if ( ($value = TaxValueString::find()->innerJoinWith('taxOption')->andFilterWhere(['ilike', 'value', $filter])->andFilterWhere(['tax_option.tax_group_id' => ProductHelper::PRODUCT_TAX_GROUP_ID_SEX])->one()) === null ) { - // Create option - $option = new TaxOption(); - $option->tax_group_id = 22; - $option->save(); - - $value = new TaxValueString(); - $value->tax_option_id = $option->tax_option_id; - $value->value = $filter; - $value->save(); - - $option->default_value = $value->tax_value_id; - $option->save(); - } - $options[] = $value->tax_option_id; - } } - if (!empty($options)) { + + if (isset($options) && !empty($options)) { + print_r($options); $_product->options = $options; } @@ -529,9 +476,9 @@ class Import extends Model { fclose ($handle); - if ($result['end']) { - unlink(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts')); - } +// if ($result['end']) { +// unlink(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts')); +// } return $result; } @@ -554,4 +501,65 @@ class Import extends Model { fputs ($fg, $str); fclose ($fg); } + + + /** + * @param $filters array of filters like [['pol'='мужской'],['god' = '2013'],['volume'='25 л']*['size'='49 x 30 x 20см'],['composition'='600D полиэстер']] + * @param $level 0 for products and 1 for product variant + * @param $catalog_names array catalogs id + * @return array + */ + private function saveFilters($filters, $level,$catalog_names){ + $options = []; + foreach($filters as $filter) { + + preg_match_all('/\[(.*):(.*)\]/',$filter,$filter); + + if (empty($filter[1][0])) { + continue; + } + $filter_name = trim($filter[1][0]); + + $taxGroup = TaxGroup::find()->where(['alias'=>$filter_name])->one(); + if(!$taxGroup instanceof TaxGroup){ + $taxGroup = new TaxGroup(); + $taxGroup->alias = $filter_name; + $taxGroup->level = $level; + $taxGroup->name = $filter_name; + $taxGroup->module = 'string'; + $taxGroup->hierarchical = FALSE; + $taxGroup->group_to_category = $catalog_names; + $taxGroup->is_filter = FALSE; + $taxGroup->save(); + } + + $filters_options = explode(',',$filter[2][0]); + + foreach($filters_options as $filter_options){ + $value = TaxValueString::find()->innerJoinWith('taxOption')->andFilterWhere(['ilike', 'value', $filter_options])->andFilterWhere(['tax_option.tax_group_id' => $taxGroup->tax_group_id])->one(); + + if (!$value instanceof TaxValueString) { + // Create option + $option = new TaxOption(); + $option->tax_group_id = $taxGroup->tax_group_id; + $option->alias = $filter_options; + $option->save(); + + $value = new TaxValueString(); + $value->tax_option_id = $option->tax_option_id; + $value->value = $filter_options; + $value->save(); + + $option->default_value = $value->tax_value_id; + $option->save(); + } + $options[] = $value->tax_option_id; + + } + + + } + + return $options; + } } \ No newline at end of file diff --git a/common/modules/relation/relationBehavior.php b/common/modules/relation/relationBehavior.php index 66df8e9..138977f 100755 --- a/common/modules/relation/relationBehavior.php +++ b/common/modules/relation/relationBehavior.php @@ -32,6 +32,7 @@ class relationBehavior extends Behavior { * Inicialize behavior (read and prepare params) */ public function init() { + foreach ($this->relations as $relation_key => &$relation) { if (is_string($relation)) { // Get data from module's data @@ -45,6 +46,7 @@ class relationBehavior extends Behavior { } } $this->fields[$relation['field']] = $relation_key; + } } @@ -61,6 +63,7 @@ class relationBehavior extends Behavior { } public function relationsAfterSave($insert) { + if (is_array($modelPrimaryKey = $this->owner->getPrimaryKey())) { throw new ErrorException('This behavior does not support composite primary keys'); } @@ -93,6 +96,7 @@ class relationBehavior extends Behavior { ->execute(); if (!empty($values)) { + foreach($values as $value) { $insertData = [ $relation['inner']['linked_key'] => $this->owner->{$relation['inner']['key']}, diff --git a/console/controllers/ImportController.php b/console/controllers/ImportController.php index 89ec367..2411b11 100755 --- a/console/controllers/ImportController.php +++ b/console/controllers/ImportController.php @@ -20,6 +20,9 @@ use yii\console\Controller; use yii\helpers\Console; class ImportController extends Controller { + public $errors = []; + + private function getProductsFile($file_type = 'uploadFileProducts') { $filename = Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@'. $file_type); if (!is_file($filename)) { @@ -30,16 +33,16 @@ class ImportController extends Controller { } public function actionProducts() { - if (file_exists(Yii::getAlias('@uploadDir/goProducts.lock'))) { - $this->errors[] = 'Task already executed'; - return Controller::EXIT_CODE_ERROR; - } - $ff = fopen(Yii::getAlias('@uploadDir/goProducts.lock'), 'w+'); - fclose($ff); +// if (file_exists(Yii::getAlias('@uploadDir/goProducts.lock'))) { +// $this->errors[] = 'Task already executed'; +// return Controller::EXIT_CODE_ERROR; +// } +// $ff = fopen(Yii::getAlias('@uploadDir/goProducts.lock'), 'w+'); +// fclose($ff); $model = new Import(); - $data = $model->goProducts(0, null); - unlink(Yii::getAlias('@uploadDir/goProducts.lock')); - return Controller::EXIT_CODE_NORMAL; + $model->goProducts(0, null); +// unlink(Yii::getAlias('@uploadDir/goProducts.lock')); +// return Controller::EXIT_CODE_NORMAL; } public function actionPrices() { diff --git a/frontend/widgets/Seo.php b/frontend/widgets/Seo.php index b940226..c97b7f2 100755 --- a/frontend/widgets/Seo.php +++ b/frontend/widgets/Seo.php @@ -94,17 +94,13 @@ class Seo extends Widget - if (isset($filter['brands']) && count($filter['brands']) == 1 ) { + if (isset($filter['brands']) && count($filter['brands']) == 1) { $model = Brand::find()->where(['alias' => $filter['brands'][0]])->one(); + return $this->selectSeoData(self::H1) ? $this->selectSeoData(self::H1) : $model->name; - if($this->selectSeoData(self::H1) == $this->category_name) { - return $this->selectSeoData(self::H1) . ' ' . $model->name; - }else { - return $this->selectSeoData(self::H1); - } - } else if (isset($filter['options']["naznacenie"]) && count($filter['options']["naznacenie"]) == 1) { + } if (isset($filter['options']["naznacenie"]) && count($filter['options']["naznacenie"]) == 1) { $model = TaxOption::find()->where(['alias' => $filter['options']["naznacenie"]])->one(); return $this->selectSeoData(self::H1).' '.$model->value->value; @@ -117,25 +113,11 @@ class Seo extends Widget case self::TITLE: $filter = \Yii::$app->request->get('filter', []); - // var_dump($filter );die(); -// if(!empty($filter)){ -// $filter_row = ''; -// foreach($filter as $sub_filter_name => $sub_filter_array){ -// if($sub_filter_name=='options'){ -// foreach($sub_filter_array as $f_name=>$f_values){ -// $filter_row .= $f_name.':'.implode(',',$f_values).'|'; -// } -// } -// -// } -// $this->fields['name'] = $filter_row; -// } + $title = $this->selectSeoData(self::TITLE); -// if(!empty($filter_row)){ -// return $filter_row; -// } + if(!empty($filter) && $title == $this->fields['meta-title']) { $array = [ -- libgit2 0.21.4