From c78526573bbcefa716b729a14175c676bc585390 Mon Sep 17 00:00:00 2001 From: Karnovsky A Date: Wed, 1 Jun 2016 11:17:16 +0300 Subject: [PATCH] - --- backend/views/layouts/main-sidebar.php | 10 ++++++++++ common/modules/product/controllers/ManageController.php | 9 +++++++++ common/modules/product/helpers/ProductHelper.php | 11 +++++++++-- common/modules/product/models/BrandQuery.php | 4 ++-- common/modules/product/models/BrandSearch.php | 15 ++++++++------- common/modules/product/models/Category.php | 9 ++++----- common/modules/product/models/Export.php | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/modules/product/models/Product.php | 40 ++++++++++++++++++++++++++++++++++++++-- common/modules/product/models/ProductVariant.php | 8 -------- common/modules/product/models/ProductVariantType.php | 6 ++++++ common/modules/product/models/import.php | 14 +++++++++----- common/modules/product/widgets/views/product_smart.php | 14 +++++++------- frontend/controllers/CatalogController.php | 16 +--------------- frontend/views/catalog/product.php | 12 +++++++----- frontend/views/catalog/product_item.php | 14 +++++++------- frontend/widgets/Rubrics.php | 2 +- 16 files changed, 205 insertions(+), 66 deletions(-) create mode 100644 common/modules/product/models/Export.php diff --git a/backend/views/layouts/main-sidebar.php b/backend/views/layouts/main-sidebar.php index f9016ef..0f10d96 100755 --- a/backend/views/layouts/main-sidebar.php +++ b/backend/views/layouts/main-sidebar.php @@ -45,10 +45,20 @@ use yii\widgets\Menu; 'options' => ['class'=>\Yii::$app->user->can('product') ? '' :'hide'], ], [ + 'label' => 'Типы вариантов', + 'url' => ['/product/product-variant-type'], + 'options' => ['class'=>\Yii::$app->user->can('product') ? '' :'hide'], + ], + [ 'label' => 'Импорт товаров', 'url' => ['/product/manage/import'], 'options' => ['class'=>\Yii::$app->user->can('product') ? '' :'hide'], ], + [ + 'label' => 'Экспорт товаров', + 'url' => ['/product/manage/export'], + 'options' => ['class'=>\Yii::$app->user->can('product') ? '' :'hide'], + ], ] ], [ diff --git a/common/modules/product/controllers/ManageController.php b/common/modules/product/controllers/ManageController.php index ca9e71f..6b56c18 100755 --- a/common/modules/product/controllers/ManageController.php +++ b/common/modules/product/controllers/ManageController.php @@ -4,6 +4,7 @@ namespace common\modules\product\controllers; use common\modules\product\helpers\ProductHelper; use common\modules\product\models\Category; +use common\modules\product\models\Export; use common\modules\product\models\Import; use common\modules\product\models\ProductImage; use common\modules\product\models\ProductVariant; @@ -278,6 +279,14 @@ class ManageController extends Controller ]); } + public function actionExport() { + $model = new Export(); + if (($file = $model->process(Yii::getAlias('@uploadDir')))) { + return Yii::$app->response->sendFile($file)->send(); + } + throw new NotFoundHttpException('Error'); + } + /** * Finds the Product model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. diff --git a/common/modules/product/helpers/ProductHelper.php b/common/modules/product/helpers/ProductHelper.php index 6705995..1e0eb83 100755 --- a/common/modules/product/helpers/ProductHelper.php +++ b/common/modules/product/helpers/ProductHelper.php @@ -10,12 +10,19 @@ use yii\base\Object; use Yii; class ProductHelper extends Object { + const PRODUCT_TAX_GROUP_ID_TARGET = 20; + const PRODUCT_TAX_GROUP_ID_YEAR = 21; + const PRODUCT_TAX_GROUP_ID_SEX = 22; + + const PRODUCT_VARIANT_TYPE_COLOR = 1; + const PRODUCT_VARIANT_TYPE_SIZE = 2; + public static function getCategories() { - return Category::find()->with('categoryName')->getTree(); + return Category::find()->getTree(); // with('categoryName')-> } public static function getBrands() { - return Brand::find()->with('brandName'); + return Brand::find(); // ->with('brandName') } /* diff --git a/common/modules/product/models/BrandQuery.php b/common/modules/product/models/BrandQuery.php index 74ee37d..5351a07 100755 --- a/common/modules/product/models/BrandQuery.php +++ b/common/modules/product/models/BrandQuery.php @@ -20,7 +20,7 @@ class BrandQuery extends \yii\db\ActiveQuery */ public function all($db = null) { - $this->with('brandName'); +// $this->with('brandName'); return parent::all($db); } @@ -30,7 +30,7 @@ class BrandQuery extends \yii\db\ActiveQuery */ public function one($db = null) { - $this->with('brandName'); +// $this->with('brandName'); return parent::one($db); } diff --git a/common/modules/product/models/BrandSearch.php b/common/modules/product/models/BrandSearch.php index a3a54a3..7662917 100755 --- a/common/modules/product/models/BrandSearch.php +++ b/common/modules/product/models/BrandSearch.php @@ -42,7 +42,7 @@ class BrandSearch extends Brand */ public function search($params) { - $query = Brand::find()->with('brandName'); + $query = Brand::find(); // ->with('brandName') // add conditions that should always apply here @@ -71,15 +71,17 @@ class BrandSearch extends Brand 'brand_name_id' => $this->brand_name_id, ]); - $query->joinWith('brandName'); - + $query->andFilterWhere(['ilike', 'alias', $this->alias]) ->andFilterWhere(['ilike', 'image', $this->image]) ->andFilterWhere(['ilike', 'meta_title', $this->meta_title]) ->andFilterWhere(['ilike', 'meta_desc', $this->meta_desc]) ->andFilterWhere(['ilike', 'meta_robots', $this->meta_robots]) - ->andFilterWhere(['ilike', 'seo_text', $this->seo_text]) - ->andFilterWhere(['ilike', 'brand_name.value', $this->brand_name]); + ->andFilterWhere(['ilike', 'seo_text', $this->seo_text]); + if (!empty($this->brand_name)) { + $query->joinWith('brandName'); + $query->andFilterWhere(['ilike', 'brand_name.value', $this->brand_name]); + } $query->orderBy('brand_id', 'asc'); @@ -94,8 +96,7 @@ class BrandSearch extends Brand ]) ->innerJoin(Product::tableName(), Product::tableName() .'.brand_id='. Brand::tableName() .'.brand_id') ->innerJoin(ProductCategory::tableName(), ProductCategory::tableName() .'.product_id='. Product::tableName() .'.product_id') - - ->with('brandName'); + ->with(['brandName']); if (!empty($category)) { $query->where([ ProductCategory::tableName() .'.category_id' => $category->category_id diff --git a/common/modules/product/models/Category.php b/common/modules/product/models/Category.php index 0d1169b..e5b2eec 100755 --- a/common/modules/product/models/Category.php +++ b/common/modules/product/models/Category.php @@ -134,11 +134,10 @@ class Category extends \yii\db\ActiveRecord } public function getProducts() { - return $this->hasMany(Product::className(), ['product_id' => 'product_id']) - ->viaTable('product_category', ['category_id' => 'category_id']) - ->joinWith(['variants']) - ->andOnCondition(['!=', ProductVariant::tableName() .'.stock', 0]); -// return $this->getRelations('product_categories'); +// return $this->hasMany(Product::className(), ['product_id' => 'product_id']) +// ->viaTable('product_category', ['category_id' => 'category_id']) +// ->andOnCondition(['!=', ProductVariant::tableName() .'.stock', 0]); + return $this->getRelations('product_categories'); } /** diff --git a/common/modules/product/models/Export.php b/common/modules/product/models/Export.php new file mode 100644 index 0000000..ce9dcb0 --- /dev/null +++ b/common/modules/product/models/Export.php @@ -0,0 +1,87 @@ +joinWith(['variants'])->where(['!=', ProductVariant::tableName() .'.stock', 0])->select('product.product_id')->all(); + $i = 0; + foreach ($products as $product_id) + { + $product = Product::findOne($product_id); + $i++; + /*if ($i>1e2) { + break; + }*/ + $mods = []; + + foreach ($product->enabledVariants as $variant) + { + $size = $color = ''; + if ($product->product_variant_type_id) { + $productVariantType = ProductVariantType::findOne($product->product_variant_type_id); + if ($productVariantType) { + if ($productVariantType->product_variant_type_id == ProductHelper::PRODUCT_VARIANT_TYPE_COLOR) { + $color = $product->name; + } elseif ($productVariantType->product_variant_type_id == ProductHelper::PRODUCT_VARIANT_TYPE_SIZE) { + $size = $product->name; + } + } + } + $mods[] = $variant->sku . '=' . $size . '=' . $color . '=' . $variant->imageUrl; + } + + $fotos = []; + + foreach ($product->images as $image) + { + $fotos[] = $image->imageUrl; + } + + $filters = $product->properties; + + $list = [ + $product->category->name, + $product->brand->name, + $product->name, + '', + ((! empty($product->description)) ? $product->description : ''), + ((! empty($filters[ProductHelper::PRODUCT_TAX_GROUP_ID_TARGET])) ? implode (',', $filters[ProductHelper::PRODUCT_TAX_GROUP_ID_TARGET]) : ''), + '', + ((! empty($filters[ProductHelper::PRODUCT_TAX_GROUP_ID_SEX])) ? implode (',', $filters[ProductHelper::PRODUCT_TAX_GROUP_ID_SEX]) : ''), + ((! empty($filters[ProductHelper::PRODUCT_TAX_GROUP_ID_YEAR])) ? implode (',', $filters[ProductHelper::PRODUCT_TAX_GROUP_ID_YEAR]) : ''), + $product->price_old, + $product->price, + intval($product->akciya), + '', + intval($product->new), + intval($product->top), + '', + $product->video, + implode (',', $fotos), + ]; + + $to_write = array_merge ($list, $mods); + foreach($to_write as &$cell) { + $cell = iconv("UTF-8", "WINDOWS-1251", $cell); + } + + fputcsv($handle, $to_write, ';'); + } + + fclose ($handle); + + return $dirName .'/'. $filename; + } +} \ No newline at end of file diff --git a/common/modules/product/models/Product.php b/common/modules/product/models/Product.php index 921bc45..aa8433f 100755 --- a/common/modules/product/models/Product.php +++ b/common/modules/product/models/Product.php @@ -3,6 +3,7 @@ namespace common\modules\product\models; use common\behaviors\Slug; +use common\modules\rubrication\models\TaxGroup; use common\modules\rubrication\models\TaxOption; use Yii; use common\modules\relation\relationBehavior; @@ -163,7 +164,7 @@ class Product extends \yii\db\ActiveRecord } public function getEnabledVariantPrice() { - return $this->enabledVariant->price; + return $this->enabledVariants[0]->price; } /** @@ -179,6 +180,26 @@ class Product extends \yii\db\ActiveRecord return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id'])->andOnCondition(['!=', ProductVariant::tableName() .'.stock', 0]); } + /* + * Get variants grouped by type + */ + public function getEnabledVariantsGrouped() + { + $variants = []; + foreach ($this->enabledVariants as $variant) { + $variants[$variant->product_variant_type_id][] = $variant; + } + if (empty($variants)) { + return []; + } + $variants_type = []; + foreach(ProductVariantType::find()->where(['product_variant_type_id' => array_keys($variants)])->all() as $variant_type) { + $variant_type->_variants = $variants[$variant_type->product_variant_type_id]; + $variants_type[] = $variant_type; + } + return $variants_type; + } + public function setVariants($variants) { $this->_variants = $variants; } @@ -206,7 +227,8 @@ class Product extends \yii\db\ActiveRecord } public function getOptions() { - return $this->getRelations('product_option'); + return $this->hasMany(TaxOption::className(), ['tax_option_id' => 'option_id'])->viaTable('product_option', ['product_id' => 'product_id']); +// return $this->getRelations('product_option'); } public function getStocks() { @@ -347,4 +369,18 @@ class Product extends \yii\db\ActiveRecord } return $op; } + + public function getProperties() { + $groups = $options = []; + foreach ($this->options as $option) { + $options[$option->tax_group_id][] = $option; + } + foreach (TaxGroup::find()->where(['tax_group_id' => array_keys($options)])->all() as $group) { + if (!empty($options[$group->tax_group_id])) { + $group->_options = $options[$group->tax_group_id]; + $groups[] = $group; + } + } + return $groups; + } } diff --git a/common/modules/product/models/ProductVariant.php b/common/modules/product/models/ProductVariant.php index 90f3b64..044260c 100755 --- a/common/modules/product/models/ProductVariant.php +++ b/common/modules/product/models/ProductVariant.php @@ -169,14 +169,6 @@ class ProductVariant extends \yii\db\ActiveRecord $this->stocks = (array) $stocks; } - /*public function getStocks() { - return $this->hasMany(Stock::className(), ['stock_id' => 'stock_id'])->viaTable(ProductStock::tableName(), ['product_variant_id' => 'product_variant_id']); - } - - public function getStocksIds() { - return ArrayHelper::getColumn($this->hasMany(Stock::className(), ['stock_id' => 'stock_id'])->viaTable(ProductStock::tableName(), ['product_variant_id' => 'product_variant_id'])->all(), 'stock_id'); - }*/ - public function afterSave($insert, $changedAttributes) { if (!empty($this->stocks)) { diff --git a/common/modules/product/models/ProductVariantType.php b/common/modules/product/models/ProductVariantType.php index 1dcb7d5..d8e813a 100755 --- a/common/modules/product/models/ProductVariantType.php +++ b/common/modules/product/models/ProductVariantType.php @@ -12,6 +12,8 @@ use Yii; */ class ProductVariantType extends \yii\db\ActiveRecord { + public $_variants = []; + /** * @inheritdoc */ @@ -40,4 +42,8 @@ class ProductVariantType extends \yii\db\ActiveRecord 'name' => 'Name', ]; } + + public function getId() { + return $this->product_variant_type_id; + } } diff --git a/common/modules/product/models/import.php b/common/modules/product/models/import.php index 2d72124..6737fa2 100644 --- a/common/modules/product/models/import.php +++ b/common/modules/product/models/import.php @@ -2,6 +2,7 @@ namespace common\modules\product\models; +use common\modules\product\helpers\ProductHelper; use common\modules\product\models\Category; use common\modules\product\models\CategoryName; use common\modules\product\models\ProductImage; @@ -64,6 +65,8 @@ class Import extends Model { $j = 0; + $is_utf = (preg_match('//u', file_get_contents(Yii::getAlias('@uploadDir') .'/'. Yii::getAlias('@uploadFileProducts'), null, null, null, 1000000))); + while (($data = fgetcsv ($handle, 10000, ";")) !== FALSE) { $j++; @@ -73,8 +76,9 @@ class Import extends Model { foreach ($data as &$value) { - //$value = mb_convert_encoding ($value, "UTF-8", mb_detect_encoding ($value)); - $value = iconv ('windows-1251', "UTF-8//TRANSLIT//IGNORE", $value); + if (!$is_utf) { + $value = iconv ('windows-1251', "UTF-8//TRANSLIT//IGNORE", $value); + } $value = trim ($value); } @@ -277,7 +281,7 @@ class Import extends Model { if (!$filter) { continue; } - if ( ($value = TaxValueString::find()->innerJoinWith('taxOption')->andFilterWhere(['ilike', 'value', $filter])->andFilterWhere(['tax_option.tax_group_id' => 20])->one()) === null ) { + 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; @@ -302,7 +306,7 @@ class Import extends Model { if (!$filter) { continue; } - if ( ($value = TaxValueString::find()->innerJoinWith('taxOption')->andFilterWhere(['ilike', 'value', $filter])->andFilterWhere(['tax_option.tax_group_id' => 21])->one()) === null ) { + 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; @@ -327,7 +331,7 @@ class Import extends Model { if (!$filter) { continue; } - if ( ($value = TaxValueString::find()->innerJoinWith('taxOption')->andFilterWhere(['ilike', 'value', $filter])->andFilterWhere(['tax_option.tax_group_id' => 22])->one()) === null ) { + 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; diff --git a/common/modules/product/widgets/views/product_smart.php b/common/modules/product/widgets/views/product_smart.php index 05c796f..b47be81 100755 --- a/common/modules/product/widgets/views/product_smart.php +++ b/common/modules/product/widgets/views/product_smart.php @@ -8,9 +8,9 @@ use yii\helpers\Url; - enabledVariant->imageUrl, 'list')?> + enabledVariants[0]->imageUrl, 'list')?> is_top) || !empty($product->is_new) || !empty($product->akciya)) :?> @@ -34,17 +34,17 @@ use yii\helpers\Url;

- enabledVariant->price_old != 0 && $product->enabledVariant->price_old != $product->enabledVariant->price) :?> - enabledVariant->price_old ?> грн.  + enabledVariants[0]->price_old != 0 && $product->enabledVariants[0]->price_old != $product->enabledVariants[0]->price) :?> + enabledVariants[0]->price_old ?> грн.  - enabledVariant->price?> грн.

+ enabledVariants[0]->price?> грн.

Купить
@@ -55,7 +55,7 @@ use yii\helpers\Url; 'catalog/product', 'product' => $product, '#' => 'm' . $variant->product_variant_id]) ?>"> - imageUrl, 'product_variant')?> + image) && !empty($variant->image->imageUrl) ? $variant->image->imageUrl : '/images/no_photo.png'), 'product_variant')?> diff --git a/frontend/controllers/CatalogController.php b/frontend/controllers/CatalogController.php index 839f35b..93a1f6c 100755 --- a/frontend/controllers/CatalogController.php +++ b/frontend/controllers/CatalogController.php @@ -163,25 +163,11 @@ class CatalogController extends \yii\web\Controller throw new HttpException(404, 'Товар не найден'); } - $groups = []; - foreach($product->category->getTaxGroups()->all() as $_group) { - $groups[$_group->tax_group_id] = $_group; - } - foreach ($product->options as $option) { - $groups[$option->tax_group_id]->_options[] = $option; - } - foreach($groups as $i => $group) { - if (empty($group->_options)) - unset($groups[$i]); - } - $category = $product->category; - ProductHelper::addLastProsucts($product->product_id); return $this->render('product', [ 'product' => $product, - 'category' => $category, - 'properties' => $groups, + 'category' => $product->category, ]); } diff --git a/frontend/views/catalog/product.php b/frontend/views/catalog/product.php index 5bce498..188fc4f 100755 --- a/frontend/views/catalog/product.php +++ b/frontend/views/catalog/product.php @@ -13,7 +13,7 @@ $this->title = $product->fullname; //} $this->params['breadcrumbs'][] = ['label' => 'Каталог', 'url' => ['catalog/category']]; $this->params['breadcrumbs'][] = ['label' => $product->category->categoryName->value, 'url' => ['catalog/category', 'category' => $product->category]]; -$this->params['breadcrumbs'][] = $product->fullname .' #'. $product->enabledVariant->sku; +$this->params['breadcrumbs'][] = $product->fullname .' #'. $product->enabledVariants[0]->sku; $this->registerJs (' @@ -94,9 +94,10 @@ $this->registerJs ("

fullname ?>

-
Цветовые решения
+ enabledVariantsGrouped as $variantGroup) :?> +
name2?>
    - enabledVariants as $variant): ?> + _variants as $variant): ?>
  • registerJs ("
+
код:
цвет:
@@ -142,7 +144,7 @@ $this->registerJs ("
  • Характеристики

    Бренд: brand->name ?>

    - + properties as $group): ?>

    name ?> _options as $option) :?> ValueRenderHTML?>

    @@ -165,7 +167,7 @@ $this->registerJs ("
      diff --git a/frontend/views/catalog/product_item.php b/frontend/views/catalog/product_item.php index fc1027a..c7a4462 100755 --- a/frontend/views/catalog/product_item.php +++ b/frontend/views/catalog/product_item.php @@ -5,8 +5,8 @@ use yii\helpers\Url;
    • is_top) || !empty($product->is_new) || !empty($product->akciya)) :?> @@ -25,7 +25,7 @@ use yii\helpers\Url; fullname ?> @@ -35,12 +35,12 @@ use yii\helpers\Url; echo '
      '; echo '

      '; // есть скидка - if ($product->enabledVariant->price_old != 0 && $product->enabledVariant->price_old != $product->enabledVariant->price) + if ($product->enabledVariants[0]->price_old != 0 && $product->enabledVariants[0]->price_old != $product->enabledVariants[0]->price) { - echo ''.$product->enabledVariant->price_old.' грн. '; + echo ''.$product->enabledVariants[0]->price_old.' грн. '; } - echo $product->enabledVariant->price.' грн.

      '; + echo $product->enabledVariants[0]->price.' грн.

      '; echo '
      '; @@ -49,7 +49,7 @@ use yii\helpers\Url; Купить
      diff --git a/frontend/widgets/Rubrics.php b/frontend/widgets/Rubrics.php index 01d562e..acbc086 100755 --- a/frontend/widgets/Rubrics.php +++ b/frontend/widgets/Rubrics.php @@ -32,7 +32,7 @@ class Rubrics extends Widget { $items = []; if (empty($this->categories)) { - $this->categories = Category::find ()->orderBy('category_id', SORT_ASC)->all(); + $this->categories = Category::find()->orderBy('category_id', SORT_ASC)->all(); } foreach ($this->categories as $category) { if (!empty($this->includes) && !in_array($category->category_id, $this->includes)) { -- libgit2 0.21.4