From af036678130d7605d53d59b12b79146c02a911cc Mon Sep 17 00:00:00 2001 From: yarik Date: Tue, 18 Oct 2016 13:32:26 +0300 Subject: [PATCH] Image behaviors --- backend/controllers/ProjectController.php | 18 ------------------ common/behaviors/ImageBehavior.php | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/behaviors/MultipleImgBehavior.php | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/behaviors/SaveImgBehavior.php | 64 +++++++++++++++++++++++++++++++++++++++++----------------------- common/behaviors/SaveMultipleFileBehavior.php | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/models/Articles.php | 11 ++++++++--- common/models/Project.php | 106 +++++++++++++++++++++++++++++++++------------------------------------------------------------------------- common/modules/product/controllers/ManageController.php | 62 -------------------------------------------------------------- common/modules/product/controllers/VariantController.php | 47 ----------------------------------------------- common/modules/product/models/Export.php | 33 ++++++++++++++++++--------------- common/modules/product/models/Import.php | 8 ++++++-- common/modules/product/models/Product.php | 231 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------------------------------------------------------------------------------------------------- common/modules/product/models/ProductImage.php | 257 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------------------------------------------------------------------- common/modules/product/models/ProductSearch.php | 229 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------- common/modules/product/models/ProductVariant.php | 120 +++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------------- common/modules/product/views/manage/export-process.php | 0 common/modules/product/views/manage/export.php | 0 common/modules/product/views/manage/index.php | 226 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------- common/modules/product/views/variant/_form.php | 16 +++++++++------- console/migrations/m161011_104931_create_stock_lang_table.php | 0 20 files changed, 898 insertions(+), 833 deletions(-) create mode 100644 common/behaviors/ImageBehavior.php create mode 100644 common/behaviors/MultipleImgBehavior.php create mode 100644 common/behaviors/SaveMultipleFileBehavior.php mode change 100644 => 100755 common/modules/product/views/manage/export-process.php mode change 100644 => 100755 common/modules/product/views/manage/export.php mode change 100644 => 100755 console/migrations/m161011_104931_create_stock_lang_table.php diff --git a/backend/controllers/ProjectController.php b/backend/controllers/ProjectController.php index 4c16af2..14d6a6e 100755 --- a/backend/controllers/ProjectController.php +++ b/backend/controllers/ProjectController.php @@ -92,16 +92,7 @@ $model->generateLangs(); if($model->load(Yii::$app->request->post())) { $model->loadLangs(\Yii::$app->request); - $model->imagesUpload = UploadedFile::getInstances($model, 'imagesUpload'); if($model->save() && $model->transactionStatus) { - if($model->imagesUpload && ( ( $images = $model->imagesUpload() ) !== false )) { - foreach($images as $image) { - $imageModel = new ProjectImage(); - $imageModel->project_id = $model->project_id; - $imageModel->image = $image; - $imageModel->save(); - } - } return $this->redirect([ 'view', 'id' => $model->project_id, @@ -128,16 +119,7 @@ $model->generateLangs(); if($model->load(Yii::$app->request->post())) { $model->loadLangs(\Yii::$app->request); - $model->imagesUpload = UploadedFile::getInstances($model, 'imagesUpload'); if($model->save() && $model->transactionStatus) { - if($model->imagesUpload && ( ( $images = $model->imagesUpload() ) !== false )) { - foreach($images as $image) { - $imageModel = new ProjectImage(); - $imageModel->project_id = $model->project_id; - $imageModel->image = $image; - $imageModel->save(); - } - } return $this->redirect([ 'view', 'id' => $model->project_id, diff --git a/common/behaviors/ImageBehavior.php b/common/behaviors/ImageBehavior.php new file mode 100644 index 0000000..a458740 --- /dev/null +++ b/common/behaviors/ImageBehavior.php @@ -0,0 +1,68 @@ + 'beforeDelete', + ]; + } + + /** + * @param Event $event + */ + public function beforeDelete($event) + { + $file = $this->getImageFile(); + if(file_exists($file)) { + unlink($file); + } + } + + /** + * Get image file path + * + * @return null|string + */ + public function getImageFile() + { + $link = $this->link; + return empty( $this->owner->$link ) ? NULL : \Yii::getAlias('@storage/' . $this->directory . '/' . $this->owner->$link); + } + + /** + * Get image file url + * + * @return null|string + */ + public function getImageUrl() + { + $link = $this->link; + return empty( $this->owner->$link ) ? NULL : '/storage/' . $this->directory . '/' . $this->owner->$link; + } + } \ No newline at end of file diff --git a/common/behaviors/MultipleImgBehavior.php b/common/behaviors/MultipleImgBehavior.php new file mode 100644 index 0000000..4926e94 --- /dev/null +++ b/common/behaviors/MultipleImgBehavior.php @@ -0,0 +1,116 @@ +owner; + return $owner->hasOne($this->model, $this->links); + } + + /** + * All images query + * + * @return \yii\db\ActiveQuery + */ + public function getImages() + { + /** + * @var ActiveRecord $owner + */ + $owner = $this->owner; + return $owner->hasMany($this->model, $this->links); + } + + /** + * Get images config array for FileInput + * + * @return array + */ + public function getImagesConfig() + { + $op = []; + $images = $this->getImages()->all(); + $config = $this->config; + if(!isset( $config[ 'id' ] )) { + return $op; + } + foreach($images as $image) { + $op[] = [ + 'caption' => ( isset( $config[ 'caption' ] ) ) ? $image->{$config[ 'caption' ]} : '', + 'url' => ( isset( $config[ 'delete_action' ] ) ) ? Url::to([ + $config[ 'delete_action' ], + 'id' => $image->{$config[ 'id' ]}, + ]) : '', + 'key' => $image->{$config[ 'id' ]}, + 'extra' => [ + 'id' => $image->{$config[ 'id' ]}, + ], + ]; + } + return $op; + } + + /** + * Get images HTML + * + * @param string $preset + * + * @return array + */ + public function getImagesHTML($preset = 'admin_thumb') + { + $op = []; + $images = $this->getImages()->all(); + foreach($images as $image) { + $op[] = ArtboxImageHelper::getImage($image->imageUrl, $preset); + } + return $op; + } + } \ No newline at end of file diff --git a/common/behaviors/SaveImgBehavior.php b/common/behaviors/SaveImgBehavior.php index 8b16a06..7772400 100755 --- a/common/behaviors/SaveImgBehavior.php +++ b/common/behaviors/SaveImgBehavior.php @@ -6,6 +6,7 @@ use yii\base\ModelEvent; use yii\db\ActiveRecord; use yii\web\UploadedFile; + /** * Class Save Image Behavior * @property ActiveRecord $owner @@ -15,6 +16,7 @@ { public $fields; + public $is_language = false; public function events() @@ -24,25 +26,26 @@ ActiveRecord::EVENT_BEFORE_INSERT => 'beforeSave', ]; } - + /** * @param ModelEvent $event */ - public function beforeSave($event) { - foreach($this->fields as $field){ - $field_name = $field['name']; + public function beforeSave($event) + { + foreach($this->fields as $field) { + $field_name = $field[ 'name' ]; $name = $field_name; if($this->is_language) { - $name = '['.$this->owner->language_id.']'.$name; + $name = '[' . $this->owner->language_id . ']' . $name; } - + $image = UploadedFile::getInstance($this->owner, $name); - if(empty($image) && $event->name == ActiveRecord::EVENT_BEFORE_UPDATE) { + if(empty( $image ) && $event->name == ActiveRecord::EVENT_BEFORE_UPDATE) { $this->owner->$field_name = $this->owner->getOldAttribute($field_name); - } elseif(!empty($image)) { - $imgDir = \Yii::getAlias('@storage/'.$field['directory'].'/'); - + } elseif(!empty( $image )) { + $imgDir = \Yii::getAlias('@storage/' . $field[ 'directory' ] . '/'); + if(!is_dir($imgDir)) { mkdir($imgDir, 0755, true); } @@ -50,28 +53,43 @@ $baseName = $image->baseName; $iteration = 0; - $file_name = $imgDir.$baseName.'.'.$image->extension; + $file_name = $imgDir . $baseName . '.' . $image->extension; while(file_exists($file_name)) { - $baseName = $image->baseName.'_'.++$iteration; - $file_name = $imgDir.$baseName.'.'.$image->extension; + $baseName = $image->baseName . '_' . ++$iteration; + $file_name = $imgDir . $baseName . '.' . $image->extension; } - unset($iteration); + unset( $iteration ); - $this->owner->$field_name = $baseName.'.'.$image->extension; + $this->owner->$field_name = $baseName . '.' . $image->extension; $image->saveAs($file_name); } } } - - public function getImageFile($image = 'image') { - return empty($this->owner->$image) ? null : '/storage/'.$this->fields[0]['directory'].'/'. $this->owner->$image; - } - public function getImageUrl($image = 'image') { - return empty($this->owner->$image) ? null : '/storage/'.$this->fields[0]['directory'].'/'. $this->owner->$image; + /** + * @param int $field + * + * @return null|string + */ + public function getImageFile($field = 0) + { + $fieldset = $this->fields[ $field ]; + $name = $fieldset[ 'name' ]; + $directory = $fieldset[ 'directory' ]; + return empty( $this->owner->$name ) ? NULL : '/storage/' . $directory . '/' . $this->owner->$name; } - - + /** + * @param int $field + * + * @return null|string + */ + public function getImageUrl($field = 0) + { + $fieldset = $this->fields[ $field ]; + $name = $fieldset[ 'name' ]; + $directory = $fieldset[ 'directory' ]; + return empty( $this->owner->$name ) ? NULL : '/storage/' . $directory . '/' . $this->owner->$name; + } } \ No newline at end of file diff --git a/common/behaviors/SaveMultipleFileBehavior.php b/common/behaviors/SaveMultipleFileBehavior.php new file mode 100644 index 0000000..d8b3351 --- /dev/null +++ b/common/behaviors/SaveMultipleFileBehavior.php @@ -0,0 +1,119 @@ + 'downloadFiles', + ActiveRecord::EVENT_AFTER_INSERT => 'downloadFiles', + ]; + } + + /** + * Save files to file table + * + * @param Event $event + */ + public function downloadFiles($event) + { + /** + * @var ActiveRecord $owner + */ + $owner = $this->owner; + $name = $this->name; + $owner->$name = UploadedFile::getInstances($owner, $name); + if(!empty( $files = $this->filesUpload() )) { + $model = $this->model; + $links = $this->links; + $column = $this->column; + foreach($files as $file) { + /** + * @var ActiveRecord $fileModel + */ + $fileModel = new $model(); + foreach($links as $link_owner => $link) { + $fileModel->$link = $owner->$link_owner; + } + $fileModel->$column = $file; + $fileModel->save(); + } + } + $this->detach(); + } + + /** + * Save files to file system + * + * @return array + */ + private function filesUpload() + { + $owner = $this->owner; + $name = $this->name; + $directory = $this->directory; + $fileDir = \Yii::getAlias('@storage/' . $directory . '/'); + if(!is_dir($fileDir)) { + mkdir($fileDir, 0755, true); + } + $files = []; + /** + * @var UploadedFile $file + */ + foreach($owner->$name as $file) { + $fileName = $file->baseName . '.' . $file->extension; + $i = 0; + while(file_exists(\Yii::getAlias($fileDir . $fileName))) { + $fileName = $file->baseName . '_' . ++$i . '.' . $file->extension; + } + $file->saveAs($fileDir . $fileName); + $files[] = $fileName; + } + return $files; + } + } \ No newline at end of file diff --git a/common/models/Articles.php b/common/models/Articles.php index ae7d2e1..6c5746c 100755 --- a/common/models/Articles.php +++ b/common/models/Articles.php @@ -1,8 +1,7 @@ [ + 'language' => [ 'class' => LanguageBehavior::className(), ], + 'images' => [ + 'class' => SaveMultipleFileBehavior::className(), + 'name' => 'imagesUpload', + 'directory' => 'projects', + 'column' => 'image', + 'links' => [ + 'project_id' => 'project_id', + ], + 'model' => ProjectImage::className(), + ], + 'multipleImage' => [ + 'class' => MultipleImgBehavior::className(), + 'links' => [ + 'project_id' => 'project_id', + ], + 'model' => ProjectImage::className(), + 'config' => [ + 'caption' => 'image', + 'delete_action' => '/project/delimg', + 'id' => 'project_image_id', + ], + ], ]; } @@ -112,74 +140,6 @@ ]; } - public function getImages() - { - return $this->hasMany(ProjectImage::className(), [ 'project_id' => 'project_id' ]); - } - - public function getImage() - { - return $this->hasOne(ProjectImage::className(), [ 'project_id' => 'project_id' ]); - } - - public function getImagesHTML() - { - $op = []; - if($this->images) { - foreach($this->images as $image) { - $op[] = \common\components\artboximage\ArtboxImageHelper::getImage($image->imageUrl, 'admin_thumb'); - } - } - return $op; - } - - public function getImagesConfig() - { - $op = []; - if($this->images) { - foreach($this->images as $image) { - $op[] = [ - 'caption' => $image->image, - 'width' => '120px', - 'url' => \yii\helpers\Url::to([ - '/project/delimg', - 'id' => $image->project_image_id, - ]), - 'key' => $image->project_image_id, - 'extra' => [ - 'id' => $image->project_image_id, - ], - ]; - } - } - return $op; - } - - public function imagesUpload() - { - if($this->validate()) { - $images = []; - foreach($this->imagesUpload as $image) { - $imageName = $image->baseName . '.' . $image->extension; - $i = 0; - while(file_exists(\Yii::getAlias('@storage/projects/' . $imageName))) { - $i++; - $imageName = $image->baseName . '_' . $i . '.' . $image->extension; - } - $imgDir = \Yii::getAlias('@storage/projects/'); - if(!is_dir($imgDir)) { - mkdir($imgDir, 0755, true); - } - - $image->saveAs($imgDir . $imageName); - $images[] = $imageName; - } - return $images; - } else { - return false; - } - } - public function getProductToProject() { return $this->hasMany(ProductToProject::className(), [ diff --git a/common/modules/product/controllers/ManageController.php b/common/modules/product/controllers/ManageController.php index 630be1e..18affef 100755 --- a/common/modules/product/controllers/ManageController.php +++ b/common/modules/product/controllers/ManageController.php @@ -8,7 +8,6 @@ use common\modules\product\models\Export; use common\modules\product\models\Import; use common\modules\product\models\ProductImage; - use common\modules\product\models\ProductVariant; use Yii; use common\modules\product\models\Product; use common\modules\product\models\ProductSearch; @@ -38,22 +37,6 @@ ]; } - public function actionDev() - { - foreach(ProductVariant::find() - ->where([ - '>', - 'price_old', - 0, - ]) - ->all() as $item) { - if($item->price >= $item->price_old || $item->price == 0) { - print $item->price . ' | ' . $item->price_old . "
\n"; - } - } - print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'; - } - /** * Lists all Product models. * @return mixed @@ -92,36 +75,11 @@ { $model = new Product(); $model->generateLangs(); - $product_spec = new ProductSpec(); $product_spec->generateLangs(); - if($model->load(Yii::$app->request->post())) { $model->loadLangs(\Yii::$app->request); - $model->imagesUpload = UploadedFile::getInstances($model, 'imagesUpload'); - $model->certificateUpload = UploadedFile::getInstances($model, 'certificateUpload'); - if($model->save() && $model->transactionStatus) { - - if(!empty( $model->imagesUpload ) && ( ( $images = $model->imagesUpload() ) !== false )) { - foreach($images as $image) { - $imageModel = new ProductImage(); - $imageModel->product_id = $model->product_id; - $imageModel->image = $image; - $imageModel->save(); - } - } - - if(!empty( $model->certificateUpload ) && ( ( $certificates = $model->certificateUpload() ) !== false )) { - foreach($certificates as $certificate) { - $certificateModel = new ProductCertificate([ - 'product_id' => $model->product_id, - 'link' => $certificate, - ]); - $certificateModel->save(false); - } - } - if($product_spec->load(Yii::$app->request->post())) { $product_spec->loadLangs(\Yii::$app->request); $product_spec->product_id = $model->product_id; @@ -168,26 +126,7 @@ $product_spec->generateLangs(); if($model->load(Yii::$app->request->post())) { $model->loadLangs(\Yii::$app->request); - $model->imagesUpload = UploadedFile::getInstances($model, 'imagesUpload'); - $model->certificateUpload = UploadedFile::getInstances($model, 'certificateUpload'); if($model->save() && $model->transactionStatus) { - if(!empty( $model->imagesUpload ) && ( ( $images = $model->imagesUpload() ) !== false )) { - foreach($images as $image) { - $imageModel = new ProductImage(); - $imageModel->product_id = $model->product_id; - $imageModel->image = $image; - $imageModel->save(); - } - } - if(!empty( $model->certificateUpload ) && ( ( $certificates = $model->certificateUpload() ) !== false )) { - foreach($certificates as $certificate) { - $certificateModel = new ProductCertificate([ - 'product_id' => $model->product_id, - 'link' => $certificate, - ]); - $certificateModel->save(false); - } - } if($product_spec->load(Yii::$app->request->post())) { $product_spec->loadLangs(\Yii::$app->request); $product_spec->techSpecFile = UploadedFile::getInstance($product_spec, 'techSpecFile'); @@ -225,7 +164,6 @@ { $this->findModel($id) ->delete(); - return $this->redirect([ 'index' ]); } diff --git a/common/modules/product/controllers/VariantController.php b/common/modules/product/controllers/VariantController.php index ccced1c..5fa2d40 100755 --- a/common/modules/product/controllers/VariantController.php +++ b/common/modules/product/controllers/VariantController.php @@ -9,11 +9,9 @@ use common\modules\product\models\ProductVariantListSearch; use common\modules\product\models\Stock; use Yii; - use yii\helpers\ArrayHelper; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; - use yii\web\UploadedFile; /** * ManageController implements the CRUD actions for ProductVariant model. @@ -83,30 +81,7 @@ $model->generateLangs(); if($model->load(Yii::$app->request->post())) { $model->loadLangs(\Yii::$app->request); - $model->imagesUpload = UploadedFile::getInstances($model, 'imagesUpload'); if($model->save() && $model->transactionStatus) { - if(( $image = UploadedFile::getInstance($model, 'image') )) { - $imageModel = ProductImage::find() - ->where([ 'product_variant_id' => $model->product_variant_id ]) - ->one(); - if($imageModel instanceof ProductImage) { - $imageModel->product_id = $model->product_id; - $imageModel->product_variant_id = $model->product_variant_id; - $imageModel->image = $image->name; - $imageModel->save(); - } else { - $imageModel = new ProductImage(); - $imageModel->product_id = $model->product_id; - $imageModel->product_variant_id = $model->product_variant_id; - $imageModel->image = $image->name; - $imageModel->save(); - } - $imgDir = Yii::getAlias('@storage/products/'); - if(!is_dir($imgDir)) { - mkdir($imgDir, 0755, true); - } - $image->saveAs(Yii::getAlias('@storage/products/' . $image->name)); - } $ProductStocks = Yii::$app->request->post('ProductStock'); $total_quantity = 0; if(!empty( $ProductStocks ) && is_array($ProductStocks)) { @@ -187,28 +162,6 @@ if($model->load(Yii::$app->request->post())) { $model->loadLangs(\Yii::$app->request); if($model->save() && $model->transactionStatus) { - if(( $image = UploadedFile::getInstance($model, 'image') )) { - $imageModel = ProductImage::find() - ->where([ 'product_variant_id' => $model->product_variant_id ]) - ->one(); - if($imageModel instanceof ProductImage) { - $imageModel->product_id = $model->product_id; - $imageModel->product_variant_id = $model->product_variant_id; - $imageModel->image = $image->name; - $imageModel->save(); - } else { - $imageModel = new ProductImage(); - $imageModel->product_id = $model->product_id; - $imageModel->product_variant_id = $model->product_variant_id; - $imageModel->image = $image->name; - $imageModel->save(); - } - $imgDir = Yii::getAlias('@storage/products/'); - if(!is_dir($imgDir)) { - mkdir($imgDir, 0755, true); - } - $image->saveAs(Yii::getAlias('@storage/products/' . $image->name)); - } $ProductStocks = Yii::$app->request->post('ProductStock'); $total_quantity = 0; if(!empty( $ProductStocks ) && is_array($ProductStocks)) { diff --git a/common/modules/product/models/Export.php b/common/modules/product/models/Export.php index d0b5dad..6c0a812 100755 --- a/common/modules/product/models/Export.php +++ b/common/modules/product/models/Export.php @@ -50,7 +50,7 @@ * @var Product[] $products */ $products = Product::find() - ->with('variantsWithFilters', 'brand.lang', 'categories.lang', 'filters') + ->with('variantsWithFilters', 'brand.lang', 'categories.lang', 'filters', 'images') ->joinWith('lang', true, 'INNER JOIN') ->limit($limit) ->offset($from) @@ -71,6 +71,9 @@ } $fotos = []; + foreach($product->images as $image) { + $fotos[] = $image->image; + } $categories = []; foreach($product->categories as $value) { @@ -80,20 +83,20 @@ $categories = implode(',', $categories); $list = [ - $categories, - ( ( !empty( $product->brand ) ) ? $product->brand->lang->name.$this->generateID($product->brand->remote_id) : '' ), - $product->lang->name.$this->generateID($product->remote_id), - '', - ( ( !empty( $product->lang->description ) ) ? $product->lang->description : '' ), - $filterString, - ( !empty( $product->variant ) ) ? $product->variant->price_old : '', - ( !empty( $product->variant ) ) ? $product->variant->price : '', - intval($product->akciya), - '', - intval($product->is_new), - intval($product->is_top), - $product->video, - implode(',', $fotos), + $categories, //A - категории через запятую Название(remote_id) + ( ( !empty( $product->brand ) ) ? $product->brand->lang->name.$this->generateID($product->brand->remote_id) : '' ), //B - бренд Название(remote_id) + $product->lang->name.$this->generateID($product->remote_id), //C - название товара Название(remote_id) + ( ( !empty( $product->lang->description ) ) ? $product->lang->description : '' ), //D - описание товара Описание(remote_id) + $filterString, //E - характеристики товара. Структура: [Группа1(remote_id):Характеристика11(remote_id),Характеристика12(remote_id)]*[Группа2(remote_id):Характеристика21(remote_id),Характеристика22(remote_id)] + ( !empty( $product->variant ) ) ? $product->variant->price_old : '', //F - страрая цена + ( !empty( $product->variant ) ) ? $product->variant->price : '', //G - новая цена + intval($product->akciya), //H - товар акционный (1/0) + '', //I - пустой + intval($product->is_new), //J - товар новинка + intval($product->is_top), //K - товар в топе + $product->video, //L - ссылка на видео (iframe) + implode(',', $fotos), //M - название файлов через запятую, картинки должны хранится в /storage/sync/product_images + // Все последующие модификации: SKU(remote_id)=[Группа1(remote_id):Характеристика11(remote_id),Характеристика12(remote_id)]*[Группа2(remote_id):Характеристика21(remote_id),Характеристика22(remote_id)]=Название=Изображение=Остаток ]; $to_write = array_merge($list, $mods); fputcsv($handle, $to_write, ';'); diff --git a/common/modules/product/models/Import.php b/common/modules/product/models/Import.php index d1f6c55..b111431 100755 --- a/common/modules/product/models/Import.php +++ b/common/modules/product/models/Import.php @@ -316,6 +316,10 @@ { if(!empty( $fotos )) { foreach($fotos as $foto) { + + if(empty($foto)){ + continue; + } $source_image = Yii::getAlias('@uploadDir') . '/product_images/' . urlencode($foto); if(file_exists($source_image)) { if(( $productImage = ProductImage::find() @@ -410,7 +414,6 @@ $_productVariant->save(false); $MOD_ARRAY[] = $_productVariant->product_variant_id; - $this->saveFotos([ $mod_image ], $product_id, $_productVariant->product_variant_id); } } @@ -444,6 +447,8 @@ while(( empty( $limit ) || $j++ < $limit ) && ( $data = fgetcsv($handle, 10000, ";") ) !== false) { try { + + foreach($data as &$value) { if(!$is_utf) { $value = iconv('windows-1251', "UTF-8//TRANSLIT//IGNORE", $value); @@ -528,7 +533,6 @@ $options = []; if(!empty ( $filters )) { - $options = $this->saveFilters($filters, 0, $categories); } $parsed_name = $this->parseName($product_name); diff --git a/common/modules/product/models/Product.php b/common/modules/product/models/Product.php index f764778..04b3b1e 100755 --- a/common/modules/product/models/Product.php +++ b/common/modules/product/models/Product.php @@ -2,6 +2,8 @@ namespace common\modules\product\models; + use common\behaviors\MultipleImgBehavior; + use common\behaviors\SaveMultipleFileBehavior; use common\models\ProductCertificate; use common\models\ProductSpec; use common\models\ProductToRating; @@ -24,8 +26,6 @@ * @property Category[] $categories * @property ProductVariant[] $variants * @property ProductVariant $variant - * @property ProductImage $image - * @property ProductImage[] $images * @property boolean $is_top * @property boolean $is_new * @property boolean $akciya @@ -39,25 +39,33 @@ * @property ProductSpec $productSpec * @property ProductCertificate[] $productCertificates * * From language behavior * - * @property ProductLang $lang - * @property ProductLang[] $langs - * @property ProductLang $object_lang - * @property string $ownerKey - * @property string $langKey - * @property ProductLang[] $model_langs - * @property bool $transactionStatus + * @property ProductLang $lang + * @property ProductLang[] $langs + * @property ProductLang $object_lang + * @property string $ownerKey + * @property string $langKey + * @property ProductLang[] $model_langs + * @property bool $transactionStatus * @method string getOwnerKey() - * @method void setOwnerKey(string $value) + * @method void setOwnerKey( string $value ) * @method string getLangKey() - * @method void setLangKey(string $value) + * @method void setLangKey( string $value ) * @method ActiveQuery getLangs() * @method ActiveQuery getLang( integer $language_id ) * @method ProductLang[] generateLangs() - * @method void loadLangs(Request $request) + * @method void loadLangs( Request $request ) * @method bool linkLangs() * @method bool saveLangs() * @method bool getTransactionStatus() * * End language behavior * + * * From multipleImage behavior + * @property ProductImage $image + * @property ProductImage[] $images + * @method ActiveQuery getImage() + * @method ActiveQuery getImages() + * @method array getImagesConfig() + * @method array getImagesHTML( string $preset ) + * * End multipleImage behavior */ class Product extends \yii\db\ActiveRecord { @@ -65,8 +73,7 @@ /** @var array $_variants */ public $_variants = []; - /** @var array $_images */ - public $imagesUpload = ''; + public $imagesUpload = []; public $certificateUpload = []; @@ -76,10 +83,42 @@ public function behaviors() { return [ + 'images' => [ + 'class' => SaveMultipleFileBehavior::className(), + 'name' => 'imagesUpload', + 'directory' => 'products', + 'column' => 'image', + 'links' => [ + 'product_id' => 'product_id', + ], + 'model' => ProductImage::className(), + ], + 'certificates' => [ + 'class' => SaveMultipleFileBehavior::className(), + 'name' => 'certificateUpload', + 'directory' => 'certificates', + 'column' => 'link', + 'links' => [ + 'product_id' => 'product_id', + ], + 'model' => ProductCertificate::className(), + ], + 'multipleImage' => [ + 'class' => MultipleImgBehavior::className(), + 'links' => [ + 'product_id' => 'product_id', + ], + 'model' => ProductImage::className(), + 'config' => [ + 'caption' => 'image', + 'delete_action' => '/product/manage/delimg', + 'id' => 'product_image_id', + ], + ], [ 'class' => FilterBehavior::className(), ], - 'language' => [ + 'language' => [ 'class' => LanguageBehavior::className(), ], ]; @@ -159,33 +198,6 @@ { return $this->hasOne(Brand::className(), [ 'brand_id' => 'brand_id' ]); } - - /** - * @return \yii\db\ActiveQuery - */ - public function getImage() - { - return $this->hasOne(ProductImage::className(), [ 'product_id' => 'product_id' ]); - } - - /** - * fetch stored image url - * @return string - */ - public function getImageUrl() - { - $image = empty( $this->variant ) ? NULL : $this->variant->image; - return !empty( $image ) ? $image->imageUrl : '/images/no_photo.png'; - } - - /** - * @return \yii\db\ActiveQuery - */ - public function getImages() - { - return $this->hasMany(ProductImage::className(), [ 'product_id' => 'product_id' ]) - ->where([ 'product_variant_id' => NULL ]); - } /** * @return \yii\db\ActiveQuery @@ -247,18 +259,15 @@ return empty( $this->brand ) ? $this->lang->name : $this->brand->lang->name . ' ' . $this->lang->name; } + /** + * @return ActiveQuery + */ public function getCategories() { return $this->hasMany(Category::className(), [ 'category_id' => 'category_id' ]) ->viaTable('product_category', [ 'product_id' => 'product_id' ]); } - public function getCategoriesWithName() - { - return $this->hasMany(Category::className(), [ 'category_id' => 'category_id' ]) - ->viaTable('product_category', [ 'product_id' => 'product_id' ]); - } - public function getCategoriesNames() { $result = []; @@ -347,135 +356,51 @@ ->where([ 'product_id' => $this->product_id ]) ->sum('quantity'); } - + public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); - - if(!empty($this->categories)){ + + if(!empty( $this->categories )) { $categories = Category::findAll($this->categories); $this->unlinkAll('categories', true); - foreach($categories as $category){ + foreach($categories as $category) { $this->link('categories', $category); } } - - if(!empty($this->options)){ + + if(!empty( $this->options )) { $options = TaxOption::findAll($this->options); - $this->unlinkAll('options',true); - foreach($options as $option){ + $this->unlinkAll('options', true); + foreach($options as $option) { $this->link('options', $option); } } - - - if (!empty($this->_variants)) { + + if(!empty( $this->_variants )) { $todel = []; - foreach ($this->variants ?: [] as $_variant) { - $todel[$_variant->product_variant_id] = $_variant->product_variant_id; + foreach($this->variants ? : [] as $_variant) { + $todel[ $_variant->product_variant_id ] = $_variant->product_variant_id; } - foreach ($this->_variants as $_variant) { - if (!is_array($_variant)) { + foreach($this->_variants as $_variant) { + if(!is_array($_variant)) { return; } - if (!empty($_variant['product_variant_id'])) { - unset($todel[$_variant['product_variant_id']]); - $model = ProductVariant::findOne($_variant['product_variant_id']); + 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]); + $_variant[ 'product_id' ] = $this->product_id; + $model->load([ 'ProductVariant' => $_variant ]); $model->product_id = $this->product_id; $model->save(); } - if (!empty($todel)) { - ProductVariant::deleteAll(['product_variant_id' => $todel]); - } - } - } - - public function imagesUpload() - { - if($this->validate()) { - $images = []; - foreach($this->imagesUpload as $image) { - $imageName = $image->baseName . '.' . $image->extension; - $i = 0; - while(file_exists(Yii::getAlias('@imagesDir/products/' . $imageName))) { - $i++; - $imageName = $image->baseName . '_' . $i . '.' . $image->extension; - } - $imgDir = Yii::getAlias('@imagesDir/products/'); - if(!is_dir($imgDir)) { - mkdir($imgDir, 0755, true); - } - - $image->saveAs($imgDir . $imageName); - $images[] = $imageName; - } - return $images; - } else { - return false; - } - } - - public function certificateUpload() - { - if($this->validate()) { - $certificates = []; - foreach($this->certificateUpload as $certificate) { - $certificateName = $certificate->baseName . '.' . $certificate->extension; - $i = 0; - while(file_exists(Yii::getAlias('@storage/certificates/' . $this->product_id . '/' . $certificateName))) { - $i++; - $certificateName = $certificate->baseName . '_' . $i . '.' . $certificate->extension; - } - $certificateDir = Yii::getAlias('@storage/certificates/' . $this->product_id . '/'); - if(!is_dir($certificateDir)) { - mkdir($certificateDir, 0755, true); - } - - $certificate->saveAs($certificateDir . $certificateName); - $certificates[] = $certificateName; - } - return $certificates; - } else { - return false; - } - } - - public function getImagesHTML() - { - $op = []; - if($this->images) { - foreach($this->images as $image) { - $op[] = \common\components\artboximage\ArtboxImageHelper::getImage($image->imageUrl, 'admin_thumb'); - } - } - return $op; - } - - public function getImagesConfig() - { - $op = []; - if($this->images) { - foreach($this->images as $image) { - $op[] = [ - 'caption' => $image->image, - 'width' => '120px', - 'url' => \yii\helpers\Url::to([ - '/product/manage/delimg', - 'id' => $image->product_image_id, - ]), - 'key' => $image->product_image_id, - 'extra' => [ - 'id' => $image->product_image_id, - ], - ]; + if(!empty( $todel )) { + ProductVariant::deleteAll([ 'product_variant_id' => $todel ]); } } - return $op; } public function recalculateRating() diff --git a/common/modules/product/models/ProductImage.php b/common/modules/product/models/ProductImage.php index 9c83b88..999a215 100755 --- a/common/modules/product/models/ProductImage.php +++ b/common/modules/product/models/ProductImage.php @@ -1,154 +1,121 @@ 255], - [['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'product_id']], - [['product_variant_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProductVariant::className(), 'targetAttribute' => ['product_variant_id' => 'product_variant_id']], - [['imageUpload'], 'safe'], - [['imageUpload'], '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'), - 'product_variant_id' => Yii::t('product', 'Product Variant ID'), - 'product' => Yii::t('product', 'Product'), - 'product_variant' => Yii::t('product', 'Product Variant'), - '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']); - if (empty($return)) { - $return = $this->productVariant->product_id; + + /** + * @inheritdoc + */ + public static function tableName() + { + return 'product_image'; } - return $return; - } - - /** - * @return \yii\db\ActiveQuery - */ - public function getProductVariant() - { - return $this->hasOne(Product::className(), ['product_variant_id' => 'product_variant_id']); - } - - /** - * fetch stored image file name with complete path - * @return string - */ - public function getImageFile() - { - return isset($this->image) ? '/storage/products/' . $this->image : null; - } - - /** - * fetch stored image url - * @return string - */ - public function getImageUrl() - { - // return a default image placeholder if your source image is not found - return isset($this->image) ? '/storage/products/'. $this->image : '/storage/no_photo.png'; - } - - /** - * 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, 'imageUpload'); - - // if no image was uploaded abort the upload - if (empty($image)) { - return false; + + public function behaviors() + { + return [ + [ + 'class' => ImageBehavior::className(), + 'link' => 'image', + 'directory' => 'products', + ], + ]; } - - // 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; + + /** + * @inheritdoc + */ + public function rules() + { + return [ + [ + [ 'product_id' ], + 'required', + ], + [ + [ + 'product_image_id', + 'product_id', + 'product_variant_id', + ], + 'integer', + ], + [ + [ + 'alt', + 'title', + 'image', + ], + 'string', + 'max' => 255, + ], + [ + [ 'product_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => Product::className(), + 'targetAttribute' => [ 'product_id' => 'product_id' ], + ], + [ + [ 'product_variant_id' ], + 'exist', + 'skipOnError' => true, + 'targetClass' => ProductVariant::className(), + 'targetAttribute' => [ 'product_variant_id' => 'product_variant_id' ], + ], + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'product_image_id' => Yii::t('product', 'Product Image ID'), + 'product_id' => Yii::t('product', 'Product ID'), + 'product_variant_id' => Yii::t('product', 'Product Variant ID'), + 'product' => Yii::t('product', 'Product'), + 'product_variant' => Yii::t('product', 'Product Variant'), + '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' ]); + if(empty( $return )) { + $return = $this->productVariant->product_id; + } + return $return; } - - // check if uploaded file can be deleted on server - if (!unlink($file)) { - return false; + + /** + * @return \yii\db\ActiveQuery + */ + public function getProductVariant() + { + return $this->hasOne(Product::className(), [ 'product_variant_id' => 'product_variant_id' ]); } - - // if deletion successful, reset your file attributes - $this->image = null; - $this->filename = null; - - return true; } -} diff --git a/common/modules/product/models/ProductSearch.php b/common/modules/product/models/ProductSearch.php index 201010d..2f9edf5 100755 --- a/common/modules/product/models/ProductSearch.php +++ b/common/modules/product/models/ProductSearch.php @@ -1,113 +1,142 @@ joinWith(['brand', 'categories', 'variant']); - - $query->groupBy(['product.product_id']); - - $dataProvider = new ActiveDataProvider([ - 'query' => $query, - ]); - - if ( !($this->load($params) && $this->validate()) ) { - return $dataProvider; + + public $brand_id; + + public $category_id; + + public $variant_sku; + + public function behaviors() + { + $behaviors = parent::behaviors(); + if(isset( $behaviors[ 'language' ] )) { + unset( $behaviors[ 'language' ] ); + } + return $behaviors; } - -// $dataProvider->setSort([ -// 'attributes' => [ -// 'brand_name' => [ -// 'asc' => ['brand.name' => SORT_ASC], -// 'desc' => ['brand.name' => SORT_DESC], -// 'default' => SORT_DESC, -// 'label' => 'Brand name', -// ], -// 'category_name', -// 'variant_sku', -// ] -// ]); - - if (isset($this->is_top)) { - $query->andFilterWhere([ - 'is_top' => (bool)$this->is_top, - ]); + + /** + * @inheritdoc + */ + public function rules() + { + return [ + [ + [ 'variant_sku' ], + 'safe', + ], + [ + [ + 'brand_id', + 'product_id', + 'category_id', + 'brand_id', + ], + 'integer', + ], + [ + [ + 'is_top', + 'is_new', + 'akciya', + ], + 'boolean', + ], + ]; } - if (isset($this->is_new)) { - $query->andFilterWhere([ - 'is_new' => (bool)$this->is_new, - ]); + + /** + * @inheritdoc + */ + public function scenarios() + { + // bypass scenarios() implementation in the parent class + return Model::scenarios(); } - if (isset($this->akciya)) { + + /** + * Creates data provider instance with search query applied + * + * @param array $params + * + * @return ActiveDataProvider + */ + public function search($params) + { + + $query = Product::find(); + + $query->joinWith([ + 'categories', + /*'variant'*/ + ]) + ->joinWith([ + 'brand' => function($query) { + /** + * @var ActiveQuery $query + */ + $query->joinWith('lang'); + }, + ]); + + $query->groupBy([ 'product.product_id' ]); + + $dataProvider = new ActiveDataProvider([ + 'query' => $query, + ]); + + if(!( $this->load($params) && $this->validate() )) { + return $dataProvider; + } + + $dataProvider->setSort([ + 'attributes' => [ + 'brand_id' => [ + 'asc' => [ 'brand_lang.name' => SORT_ASC ], + 'desc' => [ 'brand_lang.name' => SORT_DESC ], + 'default' => SORT_DESC, + 'label' => 'Brand name', + ], + ], + ]); + + if(isset( $this->is_top )) { + $query->andFilterWhere([ + 'is_top' => (bool) $this->is_top, + ]); + } + if(isset( $this->is_new )) { + $query->andFilterWhere([ + 'is_new' => (bool) $this->is_new, + ]); + } + if(isset( $this->akciya )) { + $query->andFilterWhere([ + 'akciya' => (bool) $this->akciya, + ]); + } $query->andFilterWhere([ - 'akciya' => (bool)$this->akciya, + 'product.brand_id' => $this->brand_id, + 'product.product_id' => $this->product_id, + 'product_category.category_id' => $this->category_id, ]); + + // $query->andFilterWhere(['ilike', 'brand.name', $this->brand_name]); + // $query->andFilterWhere(['ilike', 'product_variant.sku', $this->variant_sku]); + + return $dataProvider; } - $query->andFilterWhere([ - 'product.brand_id' => $this->brand_id, - 'product.product_id' => $this->product_id, -// 'product_category.category_id' => $this->category_id - ]); - -// $query->andFilterWhere(['ilike', 'brand.name', $this->brand_name]); -// $query->andFilterWhere(['ilike', 'category.name', $this->category_name]); -// $query->andFilterWhere(['ilike', 'product_variant.sku', $this->variant_sku]); - - return $dataProvider; } -} diff --git a/common/modules/product/models/ProductVariant.php b/common/modules/product/models/ProductVariant.php index 75fa08e..f3f9723 100755 --- a/common/modules/product/models/ProductVariant.php +++ b/common/modules/product/models/ProductVariant.php @@ -2,6 +2,8 @@ namespace common\modules\product\models; + use common\behaviors\MultipleImgBehavior; + use common\behaviors\SaveMultipleFileBehavior; use common\modules\language\behaviors\LanguageBehavior; use common\modules\rubrication\models\TaxGroup; use common\modules\rubrication\models\TaxOption; @@ -22,8 +24,6 @@ * @property double $price_old * @property double $stock * @property integer $product_unit_id - * @property ProductImage $image - * @property array $images * @property TaxOption[] $options * @property ProductUnit $productUnit * @property Product $product @@ -47,6 +47,14 @@ * @method bool saveLangs() * @method bool getTransactionStatus() * * End language behavior * + * * From multipleImage behavior + * @property ProductImage $image + * @property ProductImage[] $images + * @method ActiveQuery getImage() + * @method ActiveQuery getImages() + * @method array getImagesConfig() + * @method array getImagesHTML( string $preset ) + * * End multipleImage behavior */ class ProductVariant extends ActiveRecord { @@ -58,7 +66,7 @@ private $_options; /** @var array $_images */ - public $imagesUpload = ''; + public $imagesUpload = []; /** * @inheritdoc @@ -71,9 +79,32 @@ public function behaviors() { return [ - 'language' => [ + 'language' => [ 'class' => LanguageBehavior::className(), ], + 'images' => [ + 'class' => SaveMultipleFileBehavior::className(), + 'name' => 'imagesUpload', + 'directory' => 'products', + 'column' => 'image', + 'links' => [ + 'product_id' => 'product_id', + 'product_variant_id' => 'product_variant_id', + ], + 'model' => ProductImage::className(), + ], + 'multipleImage' => [ + 'class' => MultipleImgBehavior::className(), + 'links' => [ + 'product_variant_id' => 'product_variant_id', + ], + 'model' => ProductImage::className(), + 'config' => [ + 'caption' => 'image', + 'delete_action' => '/product/variant/delimg', + 'id' => 'product_image_id', + ], + ], ]; } @@ -115,7 +146,6 @@ [ [ 'options', - 'imagesUpload', ], 'safe', ], @@ -204,70 +234,11 @@ ->joinWith('lang', true, 'INNER JOIN'); } - /** - * @return \yii\db\ActiveQuery - */ - public function getImage() - { - return $this->hasOne(ProductImage::className(), [ 'product_variant_id' => 'product_variant_id' ]); - } - - /** - * fetch stored image url - * @return string - */ - public function getImageUrl() - { - // return a default image placeholder if your source image is not found - return !empty( $this->image ) ? $this->image->imageUrl : '/images/no_photo.png'; - } - public function getFullname() { return empty( $this->product ) ? NULL : ( $this->product->lang->name . ( empty( $this->lang->name ) ? '' : ' ' . $this->lang->name ) ); } - public function getImagesHTML() - { - $op = []; - if($this->images) { - foreach($this->images as $image) { - $op[] = \common\components\artboximage\ArtboxImageHelper::getImage($image->imageUrl, 'admin_thumb'); - } - } - return $op; - } - - public function getImagesConfig() - { - $op = []; - if($this->images) { - foreach($this->images as $image) { - $op[] = [ - 'caption' => $image->image, - 'width' => '120px', - 'url' => \yii\helpers\Url::to([ - '/product/manage/delimg', - 'id' => $image->product_image_id, - ]), - 'key' => $image->product_image_id, - 'extra' => [ - 'id' => $image->product_image_id, - ], - ]; - } - } - return $op; - } - - /** - * @return \yii\db\ActiveQuery - */ - public function getImages() - { - return $this->hasMany(ProductImage::className(), [ 'product_variant_id' => 'product_variant_id' ]); - } - public function setOptions($values) { $this->_options = $values; @@ -348,7 +319,6 @@ if(!empty( $this->stocks )) { ProductStock::deleteAll([ 'product_variant_id' => $this->product_variant_id ]); - $values = []; foreach($this->stocks as $id => $quantity) { $productStock = ProductStock::find() ->where([ @@ -362,24 +332,4 @@ } } - public function imagesUpload() - { - if($this->validate()) { - $images = []; - foreach($this->imagesUpload as $image) { - $imageName = $image->baseName . '.' . $image->extension; - $i = 0; - while(file_exists(Yii::getAlias('@imagesDir/products/' . $imageName))) { - $i++; - $imageName = $image->baseName . '_' . $i . '.' . $image->extension; - } - - $image->saveAs(Yii::getAlias('@imagesDir/products/' . $imageName)); - $images[] = $imageName; - } - return $images; - } else { - return false; - } - } } diff --git a/common/modules/product/views/manage/export-process.php b/common/modules/product/views/manage/export-process.php old mode 100644 new mode 100755 index ac250ed..ac250ed --- a/common/modules/product/views/manage/export-process.php +++ b/common/modules/product/views/manage/export-process.php diff --git a/common/modules/product/views/manage/export.php b/common/modules/product/views/manage/export.php old mode 100644 new mode 100755 index 7338173..7338173 --- a/common/modules/product/views/manage/export.php +++ b/common/modules/product/views/manage/export.php diff --git a/common/modules/product/views/manage/index.php b/common/modules/product/views/manage/index.php index a1d65f5..66ee363 100755 --- a/common/modules/product/views/manage/index.php +++ b/common/modules/product/views/manage/index.php @@ -1,139 +1,165 @@ title = Yii::t('product', 'Products'); -$this->params['breadcrumbs'][] = $this->title; + + use common\modules\product\models\Brand; + use common\modules\product\models\Category; + use common\modules\product\models\Product; + use yii\helpers\Html; + use yii\grid\GridView; + use kartik\select2\Select2; + use common\components\artboxtree\ArtboxTreeHelper; + use common\modules\product\helpers\ProductHelper; + + /* @var $this yii\web\View */ + /* @var $searchModel common\modules\product\models\ProductSearch */ + /* @var $dataProvider yii\data\ActiveDataProvider */ + + $this->title = Yii::t('product', 'Products'); + $this->params[ 'breadcrumbs' ][] = $this->title; ?>
- +

title) ?>

- +

- 'btn btn-success']) ?> + 'btn btn-success' ]) ?>

$dataProvider, - 'filterModel' => $searchModel, - 'columns' => [ - ['class' => 'yii\grid\SerialColumn'], + 'filterModel' => $searchModel, + 'columns' => [ 'product_id', -// [ -// 'label' => Yii::t('product', 'Brand'), -// 'attribute' => 'brand_name', -// 'value' => 'brand.name', -// 'format' => 'raw', -// 'filter' => Select2::widget([ -// 'model' => $searchModel, -// 'attribute' => 'brand_id', -// 'data' => ArrayHelper::map(ProductHelper::getBrands()->all(), 'brand_id', 'name'), -// 'language' => 'ru', -// 'options' => [ -// 'placeholder' => Yii::t('product', 'Select brand'), -// 'multiple' => false, -// ], -// 'pluginOptions' => [ -// 'allowClear' => true -// ], -// ]) -// ], -// [ -// 'label' => Yii::t('product', 'Category'), -// 'attribute' => 'category_name', -// 'value' => function($model) { -// $categories = []; -// foreach ($model->categories as $category) { -// $categories[] = $category->name; -// } -// return implode(", ", $categories); -// }, -// 'format' => 'raw', -// 'filter' => Select2::widget([ -// 'model' => $searchModel, -// 'attribute' => 'category_id', -// 'data' => ArtboxTreeHelper::treeMap(ProductHelper::getCategories(), 'category_id', 'name'), -// 'language' => 'ru', -// 'options' => [ -// 'placeholder' => Yii::t('product', 'Select category'), -// 'multiple' => false, -// ], -// 'pluginOptions' => [ -// 'allowClear' => true -// ], -// ]) -// ], -// [ -// 'label' => Yii::t('product', 'SKU'), -// 'attribute' => 'variant_sku', -// 'value' => 'variant.sku', -// ], -// 'variant.price', -// 'variant.price_old', -// [ -// 'label' => Yii::t('product', 'Stock'), -// 'attribute' => 'variant_stock', -// 'value' => 'variant.stock_caption', -// ], [ - 'class' => 'yii\grid\ActionColumn', - 'template' => '{items} {view} |{is_top} {is_new} {akciya} | {update} {delete}', - 'buttons' => [ - 'is_top' => function ($url, $model) { - return Html::a('', $url, [ - 'title' => Yii::t('product', ($model->is_top ? 'Set not is top' : 'Set is top')), + 'label' => Yii::t('product', 'Brand'), + 'attribute' => 'brand_id', + 'value' => 'brand.lang.name', + 'filter' => Select2::widget([ + 'model' => $searchModel, + 'attribute' => 'brand_id', + 'data' => Brand::find()->joinWith('lang')->select(['brand_lang.name', 'brand.brand_id'])->asArray()->indexBy('brand_id')->column(), + 'language' => 'ru', + 'options' => [ + 'placeholder' => Yii::t('product', 'Select brand'), + 'multiple' => false, + ], + 'pluginOptions' => [ + 'allowClear' => true, + ], + ]), + ], + [ + 'label' => Yii::t('product', 'Category'), + 'attribute' => 'category_id', + 'value' => function($model) { + /** + * @var Product $model + */ + $categories = []; + foreach($model->getCategories()->with('lang')->all() as $category) { + /** + * @var Category $category + */ + $categories[] = $category->lang->name; + } + return implode(", ", $categories); + }, + 'filter' => Select2::widget([ + 'model' => $searchModel, + 'attribute' => 'category_id', + 'data' => ArtboxTreeHelper::treeMap(ProductHelper::getCategories(), 'category_id', 'lang.name'), + 'language' => 'ru', + 'options' => [ + 'placeholder' => Yii::t('product', 'Select category'), + 'multiple' => false, + ], + 'pluginOptions' => [ + 'allowClear' => true, + ], + ]), + ], + // [ + // 'label' => Yii::t('product', 'SKU'), + // 'attribute' => 'variant_sku', + // 'value' => 'variant.sku', + // ], + // 'variant.price', + // 'variant.price_old', + // [ + // 'label' => Yii::t('product', 'Stock'), + // 'attribute' => 'variant_stock', + // 'value' => 'variant.stock_caption', + // ], + [ + 'class' => 'yii\grid\ActionColumn', + 'template' => '{items} {view} |{is_top} {is_new} {akciya} | {update} {delete}', + 'buttons' => [ + 'is_top' => function($url, $model) { + return Html::a('', $url, [ + 'title' => Yii::t('product', ( $model->is_top ? 'Set not is top' : 'Set is top' )), ]); }, - 'is_new' => function ($url, $model) { - return Html::a('', $url, [ - 'title' => Yii::t('product', ($model->is_new ? 'Set not is new' : 'Set is new')), + 'is_new' => function($url, $model) { + return Html::a('', $url, [ + 'title' => Yii::t('product', ( $model->is_new ? 'Set not is new' : 'Set is new' )), ]); }, - 'akciya' => function ($url, $model) { - return Html::a('', $url, [ - 'title' => Yii::t('product', ($model->akciya ? 'Set not is promotion' : 'Set is promotion')), + 'akciya' => function($url, $model) { + return Html::a('', $url, [ + 'title' => Yii::t('product', ( $model->akciya ? 'Set not is promotion' : 'Set is promotion' )), ]); }, - 'items' => function ($url, $model) { + 'items' => function($url, $model) { return Html::a('', $url, [ 'title' => Yii::t('product', 'Variants'), ]); }, - + ], - 'urlCreator' => function ($action, $model, $key, $index) { - switch ($action) { + 'urlCreator' => function($action, $model, $key, $index) { + switch($action) { case 'items': - return \yii\helpers\Url::to(['/product/variant', 'product_id' => $model->product_id]); + return \yii\helpers\Url::to([ + '/product/variant', + 'product_id' => $model->product_id, + ]); break; case 'is_top': - return \yii\helpers\Url::to(['manage/is_top', 'id' => $model->product_id]); + return \yii\helpers\Url::to([ + 'manage/is_top', + 'id' => $model->product_id, + ]); break; case 'is_new': - return \yii\helpers\Url::to(['manage/is_new', 'id' => $model->product_id]); + return \yii\helpers\Url::to([ + 'manage/is_new', + 'id' => $model->product_id, + ]); break; case 'akciya': - return \yii\helpers\Url::to(['manage/akciya', 'id' => $model->product_id]); + return \yii\helpers\Url::to([ + 'manage/akciya', + 'id' => $model->product_id, + ]); break; case 'view': - return \yii\helpers\Url::to(['manage/view', 'id' => $model->product_id]); + return \yii\helpers\Url::to([ + 'manage/view', + 'id' => $model->product_id, + ]); break; case 'update': - return \yii\helpers\Url::to(['manage/update', 'id' => $model->product_id]); + return \yii\helpers\Url::to([ + 'manage/update', + 'id' => $model->product_id, + ]); break; case 'delete': - return \yii\helpers\Url::to(['manage/delete', 'id' => $model->product_id]); + return \yii\helpers\Url::to([ + 'manage/delete', + 'id' => $model->product_id, + ]); break; } - } + }, ], ], ]); ?> diff --git a/common/modules/product/views/variant/_form.php b/common/modules/product/views/variant/_form.php index 68fdcb6..d953378 100755 --- a/common/modules/product/views/variant/_form.php +++ b/common/modules/product/views/variant/_form.php @@ -53,13 +53,12 @@ $(".dynamicform_wrapper").on("limitReached", function(e, item) { ->textarea(); ?> field($model, 'price_old') ->textarea(); ?> - field($model, 'image') + field($model, 'imagesUpload[]') ->widget(\kartik\file\FileInput::className(), [ - 'model' => $model, - 'attribute' => 'image', + 'language' => 'ru', 'options' => [ 'accept' => 'image/*', - 'multiple' => false, + 'multiple' => true, ], 'pluginOptions' => [ 'allowedFileExtensions' => [ @@ -67,10 +66,13 @@ $(".dynamicform_wrapper").on("limitReached", function(e, item) { 'gif', 'png', ], - 'initialPreview' => $model->imageUrl ? \common\components\artboximage\ArtboxImageHelper::getImage($model->imageUrl, 'products') : '', - 'overwriteInitial' => true, - 'showRemove' => true, + 'initialPreview' => !empty( $model->imagesHTML ) ? $model->imagesHTML : [], + 'initialPreviewConfig' => $model->imagesConfig, + 'overwriteInitial' => false, + 'showRemove' => false, 'showUpload' => false, + 'uploadAsync' => !empty( $model->product_variant_id ), + 'previewFileType' => 'image', ], ]); ?> diff --git a/console/migrations/m161011_104931_create_stock_lang_table.php b/console/migrations/m161011_104931_create_stock_lang_table.php old mode 100644 new mode 100755 index f47a7da..f47a7da --- a/console/migrations/m161011_104931_create_stock_lang_table.php +++ b/console/migrations/m161011_104931_create_stock_lang_table.php -- libgit2 0.21.4