diff --git a/backend/views/slider/_form.php b/backend/views/slider/_form.php index 4ea7c7e..c1bbcd5 100755 --- a/backend/views/slider/_form.php +++ b/backend/views/slider/_form.php @@ -13,6 +13,8 @@ use kartik\select2\Select2; + = $form->field($model, 'main_page')->checkbox() ?> + = $form->field($model, 'speed')->textInput() ?> = $form->field($model, 'duration')->textInput() ?> diff --git a/common/behaviors/DefaultVariantBehavior.php b/common/behaviors/DefaultVariantBehavior.php new file mode 100644 index 0000000..ce5be67 --- /dev/null +++ b/common/behaviors/DefaultVariantBehavior.php @@ -0,0 +1,78 @@ + 'addDefaultVariant', + ]; + } + + /** + * Creates new default product's variant and sets it's to stock + * marked as default and sets to it unit also marked as default + */ + public function addDefaultVariant() + { + /** + * @var Stock $stock + * @var ProductUnit $defaultUnit + */ + $defaultVariant = new ProductVariant(); + $defaultVariant->product_id = $this->owner->product_id; + + /** + * Gets default unit for variant + */ + $defaultUnit = ProductUnit::find() + ->where([ + 'is_default' => true, + ])->one(); + $defaultVariant->product_unit_id = $defaultUnit->product_unit_id; + $defaultVariant->stock = 1; + $defaultVariant->name = 'default'; + $defaultVariant->sku = 'default'; + $defaultVariant->save(); + + /** + * Gets default stock + */ + $stock = Stock::find() + ->where([ + 'is_default' => true, + ])->one(); + + /** + * Add a new stock record + */ + $defaultStock = new ProductStock(); + $defaultStock->product_id = $this->owner->product_id; + $defaultStock->stock_id = $stock->stock_id; + $defaultStock->quantity = $defaultVariant->stock; + $defaultStock->product_variant_id = $defaultVariant->product_variant_id; + $defaultStock->save(); + + } + +} \ No newline at end of file diff --git a/common/models/Slider.php b/common/models/Slider.php index e1d5520..9b36f0f 100755 --- a/common/models/Slider.php +++ b/common/models/Slider.php @@ -14,7 +14,7 @@ use Yii; * @property integer $status * @property integer $width * @property integer $height - * + * @property boolean $main_page * @property SliderImage[] $sliderImages */ class Slider extends \yii\db\ActiveRecord @@ -35,6 +35,7 @@ class Slider extends \yii\db\ActiveRecord return [ [['speed', 'duration', 'status', 'width', 'height'], 'integer'], [['title'], 'string', 'max' => 200], + [['main_page'], 'safe'], [['width', 'height'], 'required'], ['title', 'unique', 'targetClass' => '\common\models\Slider', 'message' => Yii::t('app','message',[ 'field' => 'Title' @@ -55,6 +56,7 @@ class Slider extends \yii\db\ActiveRecord 'status' => Yii::t('app', 'status'), 'width' => Yii::t('app', 'width'), 'height' => Yii::t('app', 'height'), + 'main_page' => 'Отображать на главной', ]; } diff --git a/common/modules/product/CatalogUrlManager.php b/common/modules/product/CatalogUrlManager.php index bc52f41..823168b 100755 --- a/common/modules/product/CatalogUrlManager.php +++ b/common/modules/product/CatalogUrlManager.php @@ -6,6 +6,7 @@ namespace common\modules\product; use common\modules\product\models\CategorySearch; use common\modules\product\models\ProductSearch; use yii\helpers\Url; +use yii\helpers\VarDumper; use yii\web\HttpException; use yii\web\UrlRuleInterface; @@ -59,19 +60,14 @@ class CatalogUrlManager implements UrlRuleInterface { } } } elseif ($paths[0] == 'product') { - if (!empty($paths[2])) { - throw new HttpException(404 ,'Page not found'); - } - $product = ProductSearch::findByAlias($paths[1]); - if (empty($product->product_id)) { - throw new HttpException(404 ,'Page not found'); - } + $route = 'catalog/product'; + $params = [ - 'product' => $product, + 'product' => $paths[1], + 'variant' => $paths[2] ]; } - return [$route, $params]; } /** @@ -118,7 +114,15 @@ class CatalogUrlManager implements UrlRuleInterface { $product_alias = is_object($params['product']) ? $params['product']->alias : strtolower($params['product']); unset($params['product']); } - $url = 'product/'. $product_alias; + if (!empty($params['variant'])) { + $variant_alias = is_object($params['variant']) ? $params['variant']->sku : strtolower($params['variant']); + unset($params['variant']); + } + + /** + * todo refactor this + */ + $url = 'product/'. $product_alias.'/'.$variant_alias; if (!empty($params) && ($query = http_build_query($params)) !== '') { $url .= '?' . $query; diff --git a/common/modules/product/controllers/VariantController.php b/common/modules/product/controllers/VariantController.php index 35acda2..7e35e64 100755 --- a/common/modules/product/controllers/VariantController.php +++ b/common/modules/product/controllers/VariantController.php @@ -231,7 +231,7 @@ class VariantController extends Controller } $ProductStocks = $sorted_array; $stock_names = array_keys($ProductStocks); - $stocks = Stock::find()->where(['name' => $stock_names])->indexBy('name')->one(); + $stocks = Stock::find()->where(['name' => $stock_names])->indexBy('name')->all(); foreach($ProductStocks as $name => $quantity) { $quantity = (int) $quantity; if(!array_key_exists($name, $stocks)) { diff --git a/common/modules/product/helpers/ProductHelper.php b/common/modules/product/helpers/ProductHelper.php index f12b200..25c83c5 100755 --- a/common/modules/product/helpers/ProductHelper.php +++ b/common/modules/product/helpers/ProductHelper.php @@ -9,7 +9,8 @@ use yii\base\Object; use Yii; use yii\db\ActiveQuery; - + use yii\helpers\VarDumper; + class ProductHelper extends Object { @@ -242,6 +243,7 @@ } break; default: + /** * @todo Refactor this */ diff --git a/common/modules/product/models/Category.php b/common/modules/product/models/Category.php index 29971d2..36241ac 100755 --- a/common/modules/product/models/Category.php +++ b/common/modules/product/models/Category.php @@ -7,7 +7,10 @@ use common\components\artboxtree\ArtboxTreeHelper; use common\modules\rubrication\models\TaxGroup; use Yii; +use yii\db\ActiveQuery; use yii\db\Query; +use yii\helpers\VarDumper; + /** * This is the model class for table "category". * @@ -132,6 +135,18 @@ class Category extends \yii\db\ActiveRecord ->viaTable('product_category', ['category_id' => 'category_id']); } + /** + * Custom method for baccara + * + * @return ActiveQuery + */ + public function getBaccaraProducts() { + return Product::find()->joinWith('categories') + ->where([ + 'category.category_id' => $this->getChildrenByDepth(2)->column(), + ]); + } + /** * @return \yii\db\ActiveQuery @@ -204,6 +219,8 @@ class Category extends \yii\db\ActiveRecord } public function getActiveFilters() { + + $query1 = (new Query()) ->distinct() ->select([ @@ -215,7 +232,7 @@ class Category extends \yii\db\ActiveRecord ->innerJoin('product_variant', 'product_variant.product_variant_id = product_variant_option.product_variant_id') ->innerJoin('product', 'product.product_id = product_variant.product_id') ->innerJoin('product_category', 'product_category.product_id = product.product_id') - ->where(['product_category.category_id' => $this->category_id, 'tax_group.is_filter' => TRUE]) + ->where(['product_category.category_id' => $this->getChildrenByDepth(2)->asArray()->column(), 'tax_group.is_filter' => TRUE]) ->andWhere(['!=', 'product_variant.stock', 0]); $query2 = (new Query()) @@ -229,7 +246,7 @@ class Category extends \yii\db\ActiveRecord ->innerJoin('product', 'product.product_id = product_option.product_id') ->innerJoin('product_category', 'product_category.product_id = product.product_id') ->innerJoin('product_variant', 'product_variant.product_id = product.product_id') - ->where(['product_category.category_id' => $this->category_id, 'tax_group.is_filter' => TRUE]) + ->where(['product_category.category_id' => $this->getChildrenByDepth(2)->asArray()->column(), 'tax_group.is_filter' => TRUE]) ->andWhere(['!=', 'product_variant.stock', 0]); $query3 = (new Query()) ->select([ @@ -274,6 +291,12 @@ class Category extends \yii\db\ActiveRecord } + public function getChildrenByDepth($depth) + { + return $this->getAllChildren($depth)->where([ + 'depth' => $depth, + ])->asArray()->select(['category_id']); + } public function setTaxGroup($value) { diff --git a/common/modules/product/models/Product.php b/common/modules/product/models/Product.php index 8de286e..af1bed9 100755 --- a/common/modules/product/models/Product.php +++ b/common/modules/product/models/Product.php @@ -12,6 +12,7 @@ use yii\db\ActiveQuery; use yii\db\ActiveRecord; use yii\helpers\ArrayHelper; + use common\behaviors\DefaultVariantBehavior; /** * This is the model class for table "{{%product}}". @@ -35,6 +36,7 @@ * @property string $description * @property TaxOption[] $options * @property ProductVariant[] $variants + * @property string $alias */ class Product extends \yii\db\ActiveRecord { @@ -55,6 +57,9 @@ 'class' => FilterBehavior::className(), ], [ + 'class' => DefaultVariantBehavior::className(), + ], + [ 'class' => Slug::className(), 'in_attribute' => 'name', 'out_attribute' => 'alias', diff --git a/common/modules/product/views/variant/create.php b/common/modules/product/views/variant/create.php index 1c3d2de..76de451 100755 --- a/common/modules/product/views/variant/create.php +++ b/common/modules/product/views/variant/create.php @@ -1,5 +1,6 @@ title = Yii::t('product', 'Create Product'); -$this->params['breadcrumbs'][] = ['label' => Yii::t('product', 'Products'), 'url' => ['index']]; -$this->params['breadcrumbs'][] = $this->title; +$this->params['breadcrumbs'][] = ['label' => Yii::t('product', 'Products'), 'url' => ['/product/manage']]; +$this->params['breadcrumbs'][] = ['label' => $model->product->name, 'url' => ['view', 'id' => $model->product->product_id]]; +$this->params['breadcrumbs'][] = ['label' => Yii::t('product', 'Variants'), 'url' => Url::to(['/product/variant', 'product_id' => $model->product->product_id])]; +$this->params['breadcrumbs'][] = Yii::t('product', 'Create'); ?>