diff --git a/common/components/artboxtree/ArtboxTreeBehavior.php b/common/components/artboxtree/ArtboxTreeBehavior.php index a5dc4b8..0a20864 100644 --- a/common/components/artboxtree/ArtboxTreeBehavior.php +++ b/common/components/artboxtree/ArtboxTreeBehavior.php @@ -20,6 +20,7 @@ class ArtboxTreeBehavior extends Behavior { public $keyNameGroup = 'group'; public $keyNamePath = 'path_int'; public $keyNameDepth = 'depth'; // @todo -> $keyNameDepth; + public $primaryKeyMode = true; /** * @var string @@ -88,8 +89,31 @@ class ArtboxTreeBehavior extends Behavior { * get all-level children items * use MP-method */ - public function getAllChildren($depth = null) { - return $this->getAllChildrenMP($depth); + public function getAllChildren($depth = null, $where = []) { + return $this->getAllChildrenMP($depth)->where($where); + } + + /* + * get all-level children items + * use MP-method + */ + public function getAllChildrenTree($depth = null, $where = []) { + return $this->buildTree($this->getAllChildrenMP($depth, $where)->all(), $this->owner->getAttribute($this->keyNameId)); + } + + protected function buildTree(array $data, $parentId = 0) { + $result = []; + foreach ($data as $key => $element) { + if ($element->getAttribute($this->keyNameParentId) == $parentId) { + unset($data[$key]); + $children = $this->buildTree($data, $element->getAttribute($this->keyNameId)); + $result[] = [ + 'item' => $element, + 'children' => $children + ]; + } + } + return $result; } @@ -103,9 +127,30 @@ class ArtboxTreeBehavior extends Behavior { * Full-path (use MP-method) */ public function getParentsMP($depth = null) { + $tableName = $this->owner->tableName(); + $path = $this->owner->getAttribute($this->keyNamePath); + $query = $this->owner->find() + ->andWhere(['<@', "{$tableName}.[[{$this->keyNamePath}]]", $path]); + if ($depth > 0) { + $query->andWhere(['>=', "{$tableName}.[[{$this->keyNameDepth}]]", $this->owner->getAttribute($this->keyNameDepth) - $depth]); + } + $query->andWhere(['<', "{$tableName}.[[{$this->keyNameDepth}]]", $this->owner->getAttribute($this->keyNameDepth)]); + + $orderBy = []; + $orderBy["{$tableName}.[[{$this->keyNameDepth}]]"] = SORT_ASC; + $orderBy["{$tableName}.[[{$this->keyNameId}]]"] = SORT_ASC; + + $query + ->andWhere($this->groupWhere()) + ->addOrderBy($orderBy); + $query->multiple = true; + + return $query; + } + /*public function getParentsMP($depth = null) { $path = $this->getParentPath(); if ($path !== null) { - $paths = str_replace(['{', '}'], '', explode(',', $path)); + $paths = explode(',', trim($path, '{}')); if (!$this->primaryKeyMode) { $path = null; $paths = array_map( @@ -123,7 +168,6 @@ class ArtboxTreeBehavior extends Behavior { } $tableName = $this->owner->tableName(); - $condition = ['and']; if ($this->primaryKeyMode) { $condition[] = ["{$tableName}.[[{$this->keyNameId}]]" => $paths]; } else { @@ -132,12 +176,12 @@ class ArtboxTreeBehavior extends Behavior { $query = $this->owner->find() ->andWhere($condition) - ->andWhere($this->treeCondition()) + ->andWhere($this->groupWhere()) ->addOrderBy(["{$tableName}.[[{$this->keyNamePath}]]" => SORT_ASC]); $query->multiple = true; return $query; - } + }*/ /** * @param bool $asArray = false @@ -147,14 +191,26 @@ class ArtboxTreeBehavior extends Behavior { { return static::getParentPathInternal($this->owner->getAttribute($this->keyNamePath), $asArray); } + /** + * @return array + */ + protected function groupWhere() + { + $tableName = $this->owner->tableName(); + if ($this->keyNameGroup === null) { + return []; + } else { + return ["{$tableName}.[[{$this->keyNameGroup}]]" => $this->owner->getAttribute($this->keyNameGroup)]; + } + } + public function getAllChildrenMP($depth = null) { $tableName = $this->owner->tableName(); $path = $this->owner->getAttribute($this->keyNamePath); $query = $this->owner->find() - ->andWhere(['@>', "{$tableName}.[[{$this->keyNamePath}]]", $this->getLike($path), false]); - + ->andWhere(['@>', "{$tableName}.[[{$this->keyNamePath}]]", $path]); if ($depth > 0) { $query->andWhere(['<=', "{$tableName}.[[{$this->keyNameDepth}]]", $this->owner->getAttribute($this->keyNameDepth) + $depth]); @@ -165,7 +221,7 @@ class ArtboxTreeBehavior extends Behavior { $orderBy["{$tableName}.[[{$this->keyNameId}]]"] = SORT_ASC; $query - ->andWhere($this->treeCondition()) + ->andWhere($this->groupWhere()) ->addOrderBy($orderBy); $query->multiple = true; @@ -186,7 +242,13 @@ class ArtboxTreeBehavior extends Behavior { $parent_id = $this->owner->getAttribute($this->keyNameParentId); if (empty($parent_id)) return null; - return $this->owner->find()->where([$this->keyNameId => $parent_id, $this->keyNameGroup => $this->owner->getAttribute($this->keyNameGroup)])->one(); + + $where = [$this->keyNameId => $parent_id]; + if ($this->keyNameGroup) { + $where[$this->keyNameGroup] = $this->owner->getAttribute($this->keyNameGroup); + } + + return $this->owner->find()->where($where)->one(); } /* @@ -216,7 +278,11 @@ class ArtboxTreeBehavior extends Behavior { * @return ActiveQuery */ public function getChildrenAL() { - return $this->owner->find()->where([$this->keyNameParentId => $this->owner->getAttribute($this->keyNameId), $this->keyNameGroup => $this->owner->getAttribute($this->keyNameGroup)]); + $where = [$this->keyNameParentId => $this->owner->getAttribute($this->keyNameId)]; + if ($this->keyNameGroup) { + $where[$this->keyNameGroup] = $this->owner->getAttribute($this->keyNameGroup); + } + return $this->owner->find()->where($where); } // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -271,7 +337,7 @@ class ArtboxTreeBehavior extends Behavior { */ protected static function getParentPathInternal($path, $asArray = false) { - $path = str_replace(['{', '}'], '', explode(',', $path)); + $path = explode(',', trim($path, '{}')); array_pop($path); if ($asArray) { return $path; @@ -348,7 +414,7 @@ class ArtboxTreeBehavior extends Behavior { // $this->keyNamePath => $path // ]); - $this->owner->setAttribute('path_int', $path); + $this->owner->setAttribute($this->keyNamePath, $path); // $this->owner->setAttribute($this->keyNamePath, $path); $this->owner->setAttribute($this->keyNameDepth, $depth); } diff --git a/common/components/artboxtree/ArtboxTreeQueryTrait.php b/common/components/artboxtree/ArtboxTreeQueryTrait.php index 90891ca..caf5a90 100644 --- a/common/components/artboxtree/ArtboxTreeQueryTrait.php +++ b/common/components/artboxtree/ArtboxTreeQueryTrait.php @@ -23,18 +23,17 @@ trait ArtboxTreeQueryTrait { return self::$model; } - public function getTree($group, $cached = true) { - if ($cached && isset(self::$cache_tree[$group])) - return self::$cache_tree[$group]; - + public function getTree($group = null) { $model = $this->getModel(); - $data = $this->andWhere([$model->keyNameGroup => $group])->all(); + if ($group !== null) { + $data = $this->andWhere([$model->keyNameGroup => $group])->all(); + } else { + $data = $this->all(); + } if (empty($data)) return []; - self::$cache_tree[$group] = $this->buildTree($data); - - return self::$cache_tree[$group]; + return $this->buildTree($data); } private function _recursiveRebuild($tree, $parentPath = null, $depth = 0) { diff --git a/common/config/main.php b/common/config/main.php index ea644d7..ce76195 100644 --- a/common/config/main.php +++ b/common/config/main.php @@ -86,16 +86,13 @@ return [ 'linked_key' => 'product_id', ], 'entity2' => [ - 'model' => '\common\modules\rubrication\models\TaxOption', + 'model' => '\common\modules\product\models\Category', 'label' => 'Category', - 'listField' => 'ValueRenderFlash', - 'key' => 'tax_option_id', + 'listField' => 'name', + 'key' => 'category_id', 'linked_key' => 'category_id', - 'where' => [ - 'tax_group_id' => 1 - ], 'hierarchy' => [ - 'key' => 'tax_option_id', + 'key' => 'category_id', 'parentKey' => 'parent_id', ] ], @@ -103,6 +100,33 @@ return [ 'model' => '\common\modules\product\models\ProductCategory', ] ], + 'tax_group_to_category' => [ + 'name' => Yii::t('product', 'Характеристики по категориям'), + 'field' => 'option_to_category', + 'entity1' => [ + 'model' => '\common\modules\rubrication\models\TaxGroup', + 'label' => 'Group', + 'listField' => 'name', + 'key' => 'tax_group_id', + 'linked_key' => 'entity1_id', + ], + 'entity2' => [ + 'model' => '\common\modules\product\models\Category', + 'label' => 'Category', + 'listField' => 'name', + 'key' => 'category_id', + 'linked_key' => 'entity2_id', + 'hierarchy' => [ + 'key' => 'category_id', + 'parentKey' => 'parent_id', + ] + ], + 'via' => [ + 'model' => '\common\modules\relation\models\Relation', + 'alias' => 'alias', + ] + ], + /* 'relation_categories' => [ 'name' => Yii::t('relation', 'Relation categories'), 'field' => 'categories', @@ -194,7 +218,7 @@ return [ 'model' => 'common\modules\rubrication\models\TaxOptionRelation', 'alias' => 'alias', ] - ] + ]*/ ] ], 'comment' => [ diff --git a/common/modules/product/controllers/ManageController.php b/common/modules/product/controllers/ManageController.php index fdfe194..8ecad53 100644 --- a/common/modules/product/controllers/ManageController.php +++ b/common/modules/product/controllers/ManageController.php @@ -2,6 +2,9 @@ namespace common\modules\product\controllers; +use common\modules\product\helpers\ProductHelper; +use common\modules\product\models\Category; +use common\modules\product\models\ProductVariant; use Yii; use common\modules\product\models\Product; use common\modules\product\models\ProductSearch; @@ -29,6 +32,38 @@ class ManageController extends Controller ]; } + public function actionTest() { + $categories = Category::find()->where(['depth' => 2])->all(); + $cats_ids = []; + foreach($categories as $cat) { + $cats_ids[] = $cat->category_id; + } + + $brands = ProductHelper::getBrands()->all(); + $brands_ids = []; + foreach($brands as $brand) { + $brands_ids[] = $brand->brand_id; + } + + for($i=1;$i<=1000;$i++) { + $uniqid = uniqid(); + $model = new Product(); + $model->name = 'Test '. $uniqid; + $model->brand_id = $brands_ids[array_rand($brands_ids, 1)]; + $model->categories = [$cats_ids[array_rand($cats_ids, 1)]]; + $model->save(); + + $variantModel = new ProductVariant(); + $variantModel->product_id = $model->product_id; + $variantModel->name = 'test-'. $uniqid; + $variantModel->sku = $variantModel->name; + $variantModel->price = rand(5, 200000); + $variantModel->price_old = rand(0, 5) > 3 ? $variantModel->price* (1+rand(0, 10) / 10) : $variantModel->price; + $variantModel->product_unit_id = rand(1, 5); + $variantModel->save(); + } + } + /** * Lists all Product models. * @return mixed diff --git a/common/modules/product/helpers/ProductHelper.php b/common/modules/product/helpers/ProductHelper.php index fc2b49c..3ce35cb 100644 --- a/common/modules/product/helpers/ProductHelper.php +++ b/common/modules/product/helpers/ProductHelper.php @@ -2,23 +2,16 @@ namespace common\modules\product\helpers; -use common\modules\rubrication\models\TaxOption; +use common\modules\product\models\Brand; +use common\modules\product\models\Category; use yii\base\Object; class ProductHelper extends Object { - public static function getCategoryGroupId() { - return \Yii::$app->getModule('product')->params['category_group']; - } - - public static function getBrandGroupId() { - return \Yii::$app->getModule('product')->params['brand_group']; - } - public static function getCategories() { - return TaxOption::find()->getTree(self::getCategoryGroupId()); + return Category::find()->getTree(); } public static function getBrands() { - return TaxOption::find()->where(['tax_group_id' => self::getBrandGroupId()]); + return Brand::find(); } } \ No newline at end of file diff --git a/common/modules/product/models/Category.php b/common/modules/product/models/Category.php index 2b5be1b..7362ee4 100644 --- a/common/modules/product/models/Category.php +++ b/common/modules/product/models/Category.php @@ -4,6 +4,7 @@ namespace common\modules\product\models; use common\behaviors\Slug; use common\components\artboxtree\ArtboxTreeBehavior; +use common\modules\relation\relationBehavior; use common\modules\rubrication\behaviors\ArtboxSynonymBehavior; use Yii; @@ -53,6 +54,12 @@ class Category extends \yii\db\ActiveRecord 'valueFields' => [ // postKey => DBFieldName 'name' => 'value' ] + ], + [ + 'class' => relationBehavior::className(), + 'relations' => [ + 'product_categories' => 'entity2' // Products of category + ] ] ]; } diff --git a/common/modules/product/models/Product.php b/common/modules/product/models/Product.php index 1fbc8f7..e55b350 100644 --- a/common/modules/product/models/Product.php +++ b/common/modules/product/models/Product.php @@ -11,13 +11,18 @@ use yii\db\ActiveQuery; * This is the model class for table "{{%product}}". * * @property string $name - * @property integer $tax_brand_id + * @property integer $brand_id * @property integer $product_id - * - * @property TaxOption $categories + * @property Category $category + * @property array $categories + * @property ProductVariant $variant + * @property ProductImage $image + * @property array $images */ class Product extends \yii\db\ActiveRecord { + /** @var array $variants */ + public $_variants = []; /** * @inheritdoc */ @@ -47,9 +52,9 @@ class Product extends \yii\db\ActiveRecord public function rules() { return [ - [['tax_brand_id'], 'integer'], + [['brand_id'], 'integer'], [['name'], 'string', 'max' => 150], - [['categories'], 'safe'], + [['categories', 'variants'], 'safe'], // [['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'product_id']], ]; } @@ -60,11 +65,11 @@ class Product extends \yii\db\ActiveRecord public function attributeLabels() { return [ - 'product_id' => Yii::t('product', 'Product ID'), + 'product_id' => Yii::t('product', 'ID'), 'name' => Yii::t('product', 'Name'), - 'tax_brand_id' => Yii::t('product', 'Brand'), - 'brand' => Yii::t('product', 'Brand'), + 'brand_id' => Yii::t('product', 'Brand'), 'categories' => Yii::t('product', 'Categories'), // relation behavior field + 'category' => Yii::t('product', 'Category'), // relation behavior field ]; } @@ -73,17 +78,63 @@ class Product extends \yii\db\ActiveRecord */ public function getBrand() { - return $this->hasOne(TaxOption::className(), ['tax_option_id' => 'tax_brand_id']); + return $this->hasOne(Brand::className(), ['brand_id' => 'brand_id']); } - public function getFullName() + /** + * @return \yii\db\ActiveQuery + */ + public function getImage() + { + return $this->hasOne(ProductImage::className(), ['product_id' => 'product_id']); + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getImages() { - return $this->brandname .' '. $this->name; + return $this->hasMany(ProductImage::className(), ['product_id' => 'product_id']); } - public function getBrandName() + /** + * @return \yii\db\ActiveQuery + */ + public function getVariant() { - return $this->getBrand()->one()->valueRenderHTML; + return $this->hasOne(ProductVariant::className(), ['product_id' => 'product_id']); + } + + public function getVariantPrice() { + return $this->variant->price; + } + /** + * @return \yii\db\ActiveQuery + */ + public function getVariants() + { + return $this->hasMany(ProductVariant::className(), ['product_id' => 'product_id']); + } + + public function setVariants($variants) { + $this->_variants = $variants; + } + + public function getFullName() + { + return $this->brand->name .' '. $this->name; + } + + public function getCategories() { + return $this->getRelations('product_categories'); + } + + public function getCategoriesNames() { + $result = []; + foreach($this->categories as $category) { + $result[] = $category->name; + } + return $result; } public function getCategory() { @@ -91,8 +142,8 @@ class Product extends \yii\db\ActiveRecord $categories = $this->getRelations('product_categories'); $count = $categories->count(); if ($count == 0) - return 'None'; - return $categories->one()->ValueRenderFlash . ($count > 1 ? ' + '. $count : ''); + return; + return $categories->one(); } /** @@ -103,4 +154,28 @@ class Product extends \yii\db\ActiveRecord { return new ProductQuery(get_called_class()); } + + public function afterSave($insert, $changedAttributes) + { + parent::afterSave($insert, $changedAttributes); + + $todel = []; + foreach ($this->variants ? : [] as $_variant) { + $todel[$_variant->product_variant_id] = $_variant->product_variant_id; + } + foreach ($this->_variants as $_variant) { + if (!empty($_variant['product_variant_id'])) { + unset($todel[$_variant['product_variant_id']]); + $model = ProductVariant::findOne($_variant['product_variant_id']); + } else { + $model = new ProductVariant(); + } + $_variant['product_id'] = $this->product_id; + $model->load(['ProductVariant' => $_variant]); + $model->save(); + } + if (!empty($todel)) { + ProductVariant::deleteAll(['product_variant_id' => $todel]); + } + } } diff --git a/common/modules/product/models/ProductCategory.php b/common/modules/product/models/ProductCategory.php index a5d1245..e95649a 100644 --- a/common/modules/product/models/ProductCategory.php +++ b/common/modules/product/models/ProductCategory.php @@ -26,15 +26,14 @@ class ProductCategory extends Relation return 'product_category'; } - /** * @inheritdoc */ public function rules() { return [ - [['product_id', 'dev_category_id'], 'integer'], - [['dev_category_id'], 'exist', 'skipOnError' => true, 'targetClass' => TaxOption::className(), 'targetAttribute' => ['dev_category_id' => 'tax_option_id']], + [['product_id', 'category_id'], 'integer'], + [['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['category_id' => 'category_id']], [['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'product_id']], ]; } @@ -46,7 +45,7 @@ class ProductCategory extends Relation { return [ 'product_id' => Yii::t('product', 'Product'), - 'dev_category_id' => Yii::t('product', 'Category'), + 'category_id' => Yii::t('product', 'Category'), ]; } diff --git a/common/modules/product/models/ProductImage.php b/common/modules/product/models/ProductImage.php new file mode 100644 index 0000000..87ce4d9 --- /dev/null +++ b/common/modules/product/models/ProductImage.php @@ -0,0 +1,147 @@ + 255], + [['image'], 'safe'], + [['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'product_id']], + [['image'], 'file', 'extensions'=>'jpg, gif, png'], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'product_image_id' => Yii::t('product', 'Product Image ID'), + 'product_id' => Yii::t('product', 'Product ID'), + 'image' => Yii::t('product', 'Image'), + 'alt' => Yii::t('product', 'Alt'), + 'title' => Yii::t('product', 'Title'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getProduct() + { + return $this->hasOne(Product::className(), ['product_id' => 'product_id']); + } + + /** + * @inheritdoc + * @return ProductImageQuery the active query used by this AR class. + */ + public static function find() + { + return new ProductImageQuery(get_called_class()); + } + + /** + * fetch stored image file name with complete path + * @return string + */ + public function getImageFile() + { + return isset($this->image) ? Yii::$app->params['uploadPath'] . $this->image : null; + } + + /** + * fetch stored image url + * @return string + */ + public function getImageUrl() + { + // return a default image placeholder if your source image is not found + $image = isset($this->image) ? $this->image : 'default.jpg'; + return Yii::$app->params['uploadUrl'] . $image; + } + + /** + * Process upload of image + * + * @return mixed the uploaded image instance + */ + public function uploadImage() { + // get the uploaded file instance. for multiple file uploads + // the following data will return an array (you may need to use + // getInstances method) + $image = UploadedFile::getInstance($this, 'image'); + + // if no image was uploaded abort the upload + if (empty($image)) { + return false; + } + + // store the source file name + $this->filename = $image->name; + $ext = end((explode(".", $image->name))); + + // generate a unique file name + $this->image = Yii::$app->security->generateRandomString().".{$ext}"; + + // the uploaded image instance + return $image; + } + + /** + * Process deletion of image + * + * @return boolean the status of deletion + */ + public function deleteImage() { + $file = $this->getImageFile(); + + // check if file exists on server + if (empty($file) || !file_exists($file)) { + return false; + } + + // check if uploaded file can be deleted on server + if (!unlink($file)) { + return false; + } + + // if deletion successful, reset your file attributes + $this->image = null; + $this->filename = null; + + return true; + } +} diff --git a/common/modules/product/models/ProductImageQuery.php b/common/modules/product/models/ProductImageQuery.php new file mode 100644 index 0000000..0238443 --- /dev/null +++ b/common/modules/product/models/ProductImageQuery.php @@ -0,0 +1,34 @@ +andWhere('[[status]]=1'); + }*/ + + /** + * @inheritdoc + * @return ProductImage[]|array + */ + public function all($db = null) + { + return parent::all($db); + } + + /** + * @inheritdoc + * @return ProductImage|array|null + */ + public function one($db = null) + { + return parent::one($db); + } +} diff --git a/common/modules/product/models/ProductQuery.php b/common/modules/product/models/ProductQuery.php index 373c74b..faa8595 100644 --- a/common/modules/product/models/ProductQuery.php +++ b/common/modules/product/models/ProductQuery.php @@ -31,4 +31,15 @@ class ProductQuery extends \yii\db\ActiveQuery { return parent::one($db); } + + /** + * Select category by alias + * @param $slug + * @return $this + */ + public function byAlias($alias) + { + $this->andFilterWhere(['alias' => $alias]); + return $this; + } } diff --git a/common/modules/product/models/ProductSearch.php b/common/modules/product/models/ProductSearch.php index 4d2c42d..e75df3f 100644 --- a/common/modules/product/models/ProductSearch.php +++ b/common/modules/product/models/ProductSearch.php @@ -67,4 +67,16 @@ class ProductSearch extends Product return $dataProvider; } + + public static function findByAlias($alias) { + /** @var ProductQuery $query */ + $query = Category::find(); + $query->byAlias($alias); + if (($model = $query->one()) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested product does not exist.'); + } + } + } diff --git a/common/modules/product/models/ProductVariant.php b/common/modules/product/models/ProductVariant.php new file mode 100644 index 0000000..a748284 --- /dev/null +++ b/common/modules/product/models/ProductVariant.php @@ -0,0 +1,78 @@ + 255], + [['product_unit_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProductUnit::className(), 'targetAttribute' => ['product_unit_id' => 'product_unit_id']], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'product_variant_id' => Yii::t('product', 'Product Variant ID'), + 'product_id' => Yii::t('product', 'Product ID'), + 'name' => Yii::t('product', 'Name'), + 'sku' => Yii::t('product', 'Sku'), + 'price' => Yii::t('product', 'Price'), + 'price_old' => Yii::t('product', 'Price Old'), + 'stock' => Yii::t('product', 'Stock'), + 'product_unit_id' => Yii::t('product', 'Product Unit ID'), + ]; + } + + /** + * @return \yii\db\ActiveQuery + */ + public function getProductUnit() + { + return $this->hasOne(ProductUnit::className(), ['product_unit_id' => 'product_unit_id']); + } + + /** + * @inheritdoc + * @return ProductVariantQuery the active query used by this AR class. + */ + public static function find() + { + return new ProductVariantQuery(get_called_class()); + } +} diff --git a/common/modules/product/models/ProductVariantQuery.php b/common/modules/product/models/ProductVariantQuery.php new file mode 100644 index 0000000..69a2883 --- /dev/null +++ b/common/modules/product/models/ProductVariantQuery.php @@ -0,0 +1,34 @@ +andWhere('[[status]]=1'); + }*/ + + /** + * @inheritdoc + * @return ProductVariant[]|array + */ + public function all($db = null) + { + return parent::all($db); + } + + /** + * @inheritdoc + * @return ProductVariant|array|null + */ + public function one($db = null) + { + return parent::one($db); + } +} diff --git a/common/modules/product/views/manage/_form.php b/common/modules/product/views/manage/_form.php index f1233cb..984e985 100644 --- a/common/modules/product/views/manage/_form.php +++ b/common/modules/product/views/manage/_form.php @@ -5,6 +5,9 @@ use yii\widgets\ActiveForm; use yii\helpers\ArrayHelper; use common\components\artboxtree\ArtboxTreeHelper; use common\modules\product\helpers\ProductHelper; +use kartik\file\FileInput; +use unclead\widgets\MultipleInput; +use unclead\widgets\MultipleInputColumn; /* @var $this yii\web\View */ /* @var $model common\modules\product\models\Product */ @@ -17,23 +20,76 @@ use common\modules\product\helpers\ProductHelper; field($model, 'name')->textInput(['maxlength' => true]) ?> - field($model, 'tax_brand_id')->dropDownList( - ArrayHelper::map(ProductHelper::getBrands()->all(), 'tax_option_id', 'ValueRenderFlash'), + field($model, 'brand_id')->dropDownList( + ArrayHelper::map(ProductHelper::getBrands()->all(), 'brand_id', 'name'), [ 'prompt' => Yii::t('product', 'Select brand') ] ) ?> - categories); - ?> + field($model, 'categories')->dropDownList( - ArtboxTreeHelper::treeMap(ProductHelper::getCategories(), 'tax_option_id', 'ValueRenderFlash'), + ArtboxTreeHelper::treeMap(ProductHelper::getCategories(), 'category_id', 'name'), [ // 'prompt' => Yii::t('product', 'Select category'), 'multiple' => true ] ) ?> + field($model, 'images[]')->widget(FileInput::classname(), [ + 'options' => [ + 'accept' => 'image/*', + 'multiple' => true, + ], + 'pluginOptions' => [ +// 'uploadUrl' => \yii\helpers\Url::to(['/site/file-upload']), + ] + ]); + */?> + + field($model, 'variants')->widget(MultipleInput::className(), [ + 'columns' => [ + [ + 'name' => 'product_variant_id', + 'type' => MultipleInputColumn::TYPE_HIDDEN_INPUT, + ], + [ + 'name' => 'name', + 'type' => MultipleInputColumn::TYPE_TEXT_INPUT, + 'title' => 'Name', + ], + [ + 'name' => 'sku', + 'type' => MultipleInputColumn::TYPE_TEXT_INPUT, + 'title' => 'SKU', + ], + [ + 'name' => 'price', + 'type' => MultipleInputColumn::TYPE_TEXT_INPUT, + 'title' => 'Price', + ], + [ + 'name' => 'price_old', + 'type' => MultipleInputColumn::TYPE_TEXT_INPUT, + 'title' => 'Old Price', + ], + [ + 'name' => 'product_unit_id', + 'type' => MultipleInputColumn::TYPE_DROPDOWN, + 'title' => 'Unit', + 'items' => ArrayHelper::map(\common\modules\product\models\ProductUnit::find()->all(), 'product_unit_id', 'name'), + ], + [ + 'name' => 'stock', + 'type' => MultipleInputColumn::TYPE_TEXT_INPUT, + 'title' => 'Stock', + 'options' => [ + 'placeholder' => '∞' + ], + ], + ], + ]); + ?> +
isNewRecord ? Yii::t('product', 'Create') : Yii::t('product', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
diff --git a/common/modules/product/views/manage/_search.php b/common/modules/product/views/manage/_search.php index f051c6a..00015dc 100644 --- a/common/modules/product/views/manage/_search.php +++ b/common/modules/product/views/manage/_search.php @@ -17,7 +17,7 @@ use yii\widgets\ActiveForm; field($model, 'name') ?> - field($model, 'tax_brand_id') ?> + field($model, 'brand_id') ?> field($model, 'product_id') ?> diff --git a/common/modules/product/views/manage/index.php b/common/modules/product/views/manage/index.php index 82254a2..0b5e061 100644 --- a/common/modules/product/views/manage/index.php +++ b/common/modules/product/views/manage/index.php @@ -22,11 +22,10 @@ $this->params['breadcrumbs'][] = $this->title; 'filterModel' => $searchModel, 'columns' => [ ['class' => 'yii\grid\SerialColumn'], - -// 'product_id', - 'fullname', - 'brandname', - 'category', + 'product_id', + 'name', + 'brand.name', + 'category.name', ['class' => 'yii\grid\ActionColumn'], ], diff --git a/common/modules/product/views/manage/view.php b/common/modules/product/views/manage/view.php index e6d9c7c..9cb1d2a 100644 --- a/common/modules/product/views/manage/view.php +++ b/common/modules/product/views/manage/view.php @@ -28,9 +28,11 @@ $this->params['breadcrumbs'][] = $this->title; $model, 'attributes' => [ - 'name', - 'tax_brand_id', 'product_id', + 'name', + 'fullname', + 'brand.name', + 'category.name', ], ]) ?> diff --git a/common/modules/product/widgets/views/submenu.php b/common/modules/product/widgets/views/submenu.php index f527945..10878e8 100644 --- a/common/modules/product/widgets/views/submenu.php +++ b/common/modules/product/widgets/views/submenu.php @@ -1,5 +1,5 @@