Commit e2128676924b0a9f5c1c585154af6c9686e04f09

Authored by Karnovsky A
1 parent 85756013

Karnovsky 05052016 1600

backend/views/brand/_form.php
@@ -24,11 +24,10 @@ use yii\widgets\ActiveForm; @@ -24,11 +24,10 @@ use yii\widgets\ActiveForm;
24 ], 24 ],
25 'pluginOptions' => [ 25 'pluginOptions' => [
26 'allowedFileExtensions' => ['jpg','gif','png'], 26 'allowedFileExtensions' => ['jpg','gif','png'],
27 - 'initialPreview' => function() use ($model) {  
28 - if (!empty($model->imageUrl))  
29 - return Html::img($model->imageUrl);  
30 - },  
31 - 'overwriteInitial' => false, 27 + 'initialPreview' => $model->imageUrl ? Html::img($model->imageUrl) : '',
  28 + 'overwriteInitial' => true,
  29 + 'showRemove' => true,
  30 + 'showUpload' => false,
32 ], 31 ],
33 ]); ?> 32 ]); ?>
34 33
backend/views/category/_form.php
@@ -79,6 +79,10 @@ use kartik\select2\Select2; @@ -79,6 +79,10 @@ use kartik\select2\Select2;
79 ], 79 ],
80 'pluginOptions' => [ 80 'pluginOptions' => [
81 'allowedFileExtensions' => ['jpg','gif','png'], 81 'allowedFileExtensions' => ['jpg','gif','png'],
  82 + 'initialPreview' => $model->imageUrl ? Html::img($model->imageUrl) : '',
  83 + 'overwriteInitial' => true,
  84 + 'showRemove' => true,
  85 + 'showUpload' => false,
82 ], 86 ],
83 ]); ?> 87 ]); ?>
84 88
common/modules/product/controllers/ManageController.php
@@ -4,6 +4,7 @@ namespace common\modules\product\controllers; @@ -4,6 +4,7 @@ namespace common\modules\product\controllers;
4 4
5 use common\modules\product\helpers\ProductHelper; 5 use common\modules\product\helpers\ProductHelper;
6 use common\modules\product\models\Category; 6 use common\modules\product\models\Category;
  7 +use common\modules\product\models\ProductImage;
7 use common\modules\product\models\ProductVariant; 8 use common\modules\product\models\ProductVariant;
8 use common\modules\product\models\RemoteProductsSearch; 9 use common\modules\product\models\RemoteProductsSearch;
9 use Yii; 10 use Yii;
@@ -15,6 +16,7 @@ use yii\filters\VerbFilter; @@ -15,6 +16,7 @@ use yii\filters\VerbFilter;
15 use common\modules\product\models\Brand; 16 use common\modules\product\models\Brand;
16 use common\modules\product\models\BrandName; 17 use common\modules\product\models\BrandName;
17 use common\modules\product\models\RemoteProducts; 18 use common\modules\product\models\RemoteProducts;
  19 +use yii\web\UploadedFile;
18 20
19 /** 21 /**
20 * ManageController implements the CRUD actions for Product model. 22 * ManageController implements the CRUD actions for Product model.
@@ -122,8 +124,26 @@ class ManageController extends Controller @@ -122,8 +124,26 @@ class ManageController extends Controller
122 { 124 {
123 $model = new Product(); 125 $model = new Product();
124 126
125 - if ($model->load(Yii::$app->request->post()) && $model->save()) { 127 + if ($model->load(Yii::$app->request->post())) {
  128 + $model->imagesUpload = UploadedFile::getInstances($model, 'imagesUpload');
  129 +
  130 + $model->save();
  131 +
  132 + foreach($model->images as $image) {
  133 + $image->delete();
  134 + }
  135 +
  136 + if ($model->imagesUpload) {
  137 + foreach ($model->imagesUpload as $image) {
  138 + $imageModel = new ProductImage();
  139 + $imageModel->product_id = $model->product_id;
  140 + $imageModel->image = $image->name;
  141 + $imageModel->save();
  142 + }
  143 + }
  144 +
126 return $this->redirect(['view', 'id' => $model->product_id]); 145 return $this->redirect(['view', 'id' => $model->product_id]);
  146 +
127 } else { 147 } else {
128 return $this->render('create', [ 148 return $this->render('create', [
129 'model' => $model, 149 'model' => $model,
@@ -141,7 +161,24 @@ class ManageController extends Controller @@ -141,7 +161,24 @@ class ManageController extends Controller
141 { 161 {
142 $model = $this->findModel($id); 162 $model = $this->findModel($id);
143 163
144 - if ($model->load(Yii::$app->request->post()) && $model->save()) { 164 + if ($model->load(Yii::$app->request->post())) {
  165 + $model->imagesUpload = UploadedFile::getInstances($model, 'imagesUpload');
  166 +
  167 + $model->save();
  168 +
  169 + foreach($model->images as $image) {
  170 + $image->delete();
  171 + }
  172 +
  173 + if ($model->imagesUpload) {
  174 + foreach ($model->imagesUpload as $image) {
  175 + $imageModel = new ProductImage();
  176 + $imageModel->product_id = $model->product_id;
  177 + $imageModel->image = $image->name;
  178 + $imageModel->save();
  179 + }
  180 + }
  181 +
145 return $this->redirect(['view', 'id' => $model->product_id]); 182 return $this->redirect(['view', 'id' => $model->product_id]);
146 } else { 183 } else {
147 $groups = $model->category->getTaxGroups(); 184 $groups = $model->category->getTaxGroups();
common/modules/product/models/Brand.php
@@ -41,7 +41,7 @@ class Brand extends \yii\db\ActiveRecord @@ -41,7 +41,7 @@ class Brand extends \yii\db\ActiveRecord
41 'valueKeyName' => 'value', 41 'valueKeyName' => 'value',
42 'slugKeyName' => 'alias', 42 'slugKeyName' => 'alias',
43 'translit' => true 43 'translit' => true
44 - ] 44 + ],
45 ], 45 ],
46 ]; 46 ];
47 } 47 }
@@ -119,6 +119,10 @@ class Brand extends \yii\db\ActiveRecord @@ -119,6 +119,10 @@ class Brand extends \yii\db\ActiveRecord
119 return empty($this->brand_name_id) ? null : $this->brandName->value; 119 return empty($this->brand_name_id) ? null : $this->brandName->value;
120 } 120 }
121 121
  122 + public function getImageFile() {
  123 + return empty($this->image) ? null : '/images/brand/'. $this->image;
  124 + }
  125 +
122 public function getImageUrl() { 126 public function getImageUrl() {
123 return empty($this->image) ? null : '/images/brand/'. $this->image; 127 return empty($this->image) ? null : '/images/brand/'. $this->image;
124 } 128 }
common/modules/product/models/Product.php
@@ -7,6 +7,8 @@ use common\modules\rubrication\models\TaxOption; @@ -7,6 +7,8 @@ use common\modules\rubrication\models\TaxOption;
7 use Yii; 7 use Yii;
8 use common\modules\relation\relationBehavior; 8 use common\modules\relation\relationBehavior;
9 use yii\db\ActiveQuery; 9 use yii\db\ActiveQuery;
  10 +use yii\helpers\Html;
  11 +use yii\web\UploadedFile;
10 12
11 /** 13 /**
12 * This is the model class for table "{{%product}}". 14 * This is the model class for table "{{%product}}".
@@ -68,8 +70,8 @@ class Product extends \yii\db\ActiveRecord @@ -68,8 +70,8 @@ class Product extends \yii\db\ActiveRecord
68 [['brand_id'], 'integer'], 70 [['brand_id'], 'integer'],
69 [['name'], 'string', 'max' => 150], 71 [['name'], 'string', 'max' => 150],
70 [['alias'], 'string', 'max' => 250], 72 [['alias'], 'string', 'max' => 250],
71 - [['categories', 'variants', 'options'], 'safe'],  
72 -// [['imagesUpload'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, gif'], 73 + [['categories', 'variants', 'options', 'imagesUpload'], 'safe'],
  74 + [['imagesUpload'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg, gif', 'maxFiles' => 50],
73 [['description', 'video'], 'safe'], 75 [['description', 'video'], 'safe'],
74 [['is_top', 'is_new'], 'boolean'], 76 [['is_top', 'is_new'], 'boolean'],
75 // [['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'product_id']], 77 // [['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'product_id']],
@@ -187,8 +189,18 @@ class Product extends \yii\db\ActiveRecord @@ -187,8 +189,18 @@ class Product extends \yii\db\ActiveRecord
187 { 189 {
188 parent::afterSave($insert, $changedAttributes); 190 parent::afterSave($insert, $changedAttributes);
189 191
190 -// foreach($this->imagesUpload as $image) {  
191 -// $image->saveAs('/images/items/' . $image->baseName .'_'. uniqid() . '.' . $image->extension); 192 +// $images = UploadedFile::getInstance($this, 'imagesUpload');
  193 +// var_dump($images);exit;
  194 +
  195 +// if (!empty($this->imagesUpload)) {
  196 +// if (!is_array($this->imagesUpload)) {
  197 +// $this->imagesUpload = [$this->imagesUpload];
  198 +// }
  199 +// foreach($this->imagesUpload as $image) {
  200 +// $image->saveAs((Yii::getAlias('@frontend/web/images/products/original/' . $image->baseName .'_'. uniqid() . '.' . $image->extension)));
  201 +// }
  202 +//
  203 +//
192 // } 204 // }
193 205
194 $todel = []; 206 $todel = [];
@@ -213,4 +225,26 @@ class Product extends \yii\db\ActiveRecord @@ -213,4 +225,26 @@ class Product extends \yii\db\ActiveRecord
213 ProductVariant::deleteAll(['product_variant_id' => $todel]); 225 ProductVariant::deleteAll(['product_variant_id' => $todel]);
214 } 226 }
215 } 227 }
  228 +
  229 + public function imagesUpload()
  230 + {
  231 + if ($this->validate()) {
  232 + foreach ($this->imagesUpload as $image) {
  233 + $image->saveAs(Yii::getAlias('@frontend/web/images/products/original/' . $image->baseName . '.' . $image->extension));
  234 + }
  235 + return true;
  236 + } else {
  237 + return false;
  238 + }
  239 + }
  240 +
  241 + public function getImagesHTML() {
  242 + $op = [];
  243 + if ($this->images) {
  244 + foreach ($this->images as $image) {
  245 + $op[] = Html::img($image->imageUrl);
  246 + }
  247 + }
  248 + return $op;
  249 + }
216 } 250 }
common/modules/product/models/ProductImage.php
@@ -10,15 +10,16 @@ use yii\web\UploadedFile; @@ -10,15 +10,16 @@ use yii\web\UploadedFile;
10 * 10 *
11 * @property integer $product_image_id 11 * @property integer $product_image_id
12 * @property integer $product_id 12 * @property integer $product_id
  13 + * @property integer $product_variant_id
13 * @property string $image 14 * @property string $image
14 * @property string $alt 15 * @property string $alt
15 * @property string $title 16 * @property string $title
16 - *  
17 * @property Product $product 17 * @property Product $product
  18 + * @property ProductVariant $productVariant
18 */ 19 */
19 class ProductImage extends \yii\db\ActiveRecord 20 class ProductImage extends \yii\db\ActiveRecord
20 { 21 {
21 - public $image; 22 + public $imageUpload;
22 /** 23 /**
23 * @inheritdoc 24 * @inheritdoc
24 */ 25 */
@@ -33,12 +34,13 @@ class ProductImage extends \yii\db\ActiveRecord @@ -33,12 +34,13 @@ class ProductImage extends \yii\db\ActiveRecord
33 public function rules() 34 public function rules()
34 { 35 {
35 return [ 36 return [
36 - [['product_image_id', 'product_id'], 'required'],  
37 - [['product_image_id', 'product_id'], 'integer'],  
38 - [['alt', 'title'], 'string', 'max' => 255],  
39 - [['image'], 'safe'], 37 + [['product_id'], 'required'],
  38 + [['product_image_id', 'product_id', 'product_variant_id'], 'integer'],
  39 + [['alt', 'title', 'image'], 'string', 'max' => 255],
40 [['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'product_id']], 40 [['product_id'], 'exist', 'skipOnError' => true, 'targetClass' => Product::className(), 'targetAttribute' => ['product_id' => 'product_id']],
41 - [['image'], 'file', 'extensions'=>'jpg, gif, png'], 41 + [['product_variant_id'], 'exist', 'skipOnError' => true, 'targetClass' => ProductVariant::className(), 'targetAttribute' => ['product_variant_id' => 'product_variant_id']],
  42 + [['imageUpload'], 'safe'],
  43 + [['imageUpload'], 'file', 'extensions' => 'jpg, gif, png'],
42 ]; 44 ];
43 } 45 }
44 46
@@ -50,6 +52,10 @@ class ProductImage extends \yii\db\ActiveRecord @@ -50,6 +52,10 @@ class ProductImage extends \yii\db\ActiveRecord
50 return [ 52 return [
51 'product_image_id' => Yii::t('product', 'Product Image ID'), 53 'product_image_id' => Yii::t('product', 'Product Image ID'),
52 'product_id' => Yii::t('product', 'Product ID'), 54 'product_id' => Yii::t('product', 'Product ID'),
  55 + 'product_variant_id' => Yii::t('product', 'Product Variant ID'),
  56 + 'product' => Yii::t('product', 'Product'),
  57 + 'product' => Yii::t('product', 'Product'),
  58 + 'product_variant' => Yii::t('product', 'Product Variant'),
53 'image' => Yii::t('product', 'Image'), 59 'image' => Yii::t('product', 'Image'),
54 'alt' => Yii::t('product', 'Alt'), 60 'alt' => Yii::t('product', 'Alt'),
55 'title' => Yii::t('product', 'Title'), 61 'title' => Yii::t('product', 'Title'),
@@ -61,7 +67,19 @@ class ProductImage extends \yii\db\ActiveRecord @@ -61,7 +67,19 @@ class ProductImage extends \yii\db\ActiveRecord
61 */ 67 */
62 public function getProduct() 68 public function getProduct()
63 { 69 {
64 - return $this->hasOne(Product::className(), ['product_id' => 'product_id']); 70 + $return = $this->hasOne(Product::className(), ['product_id' => 'product_id']);
  71 + if (empty($return)) {
  72 + $return = $this->productVariant->product_id;
  73 + }
  74 + return $return;
  75 + }
  76 +
  77 + /**
  78 + * @return \yii\db\ActiveQuery
  79 + */
  80 + public function getProductVariant()
  81 + {
  82 + return $this->hasOne(Product::className(), ['product_variant_id' => 'product_variant_id']);
65 } 83 }
66 84
67 /** 85 /**
@@ -90,7 +108,7 @@ class ProductImage extends \yii\db\ActiveRecord @@ -90,7 +108,7 @@ class ProductImage extends \yii\db\ActiveRecord
90 { 108 {
91 // return a default image placeholder if your source image is not found 109 // return a default image placeholder if your source image is not found
92 $image = isset($this->image) ? $this->image : 'default.jpg'; 110 $image = isset($this->image) ? $this->image : 'default.jpg';
93 - return Yii::$app->params['uploadUrl'] . $image; 111 + return Yii::getAlias('/images/products/original/' . $image);
94 } 112 }
95 113
96 /** 114 /**
@@ -102,7 +120,7 @@ class ProductImage extends \yii\db\ActiveRecord @@ -102,7 +120,7 @@ class ProductImage extends \yii\db\ActiveRecord
102 // get the uploaded file instance. for multiple file uploads 120 // get the uploaded file instance. for multiple file uploads
103 // the following data will return an array (you may need to use 121 // the following data will return an array (you may need to use
104 // getInstances method) 122 // getInstances method)
105 - $image = UploadedFile::getInstance($this, 'image'); 123 + $image = UploadedFile::getInstance($this, 'imageUpload');
106 124
107 // if no image was uploaded abort the upload 125 // if no image was uploaded abort the upload
108 if (empty($image)) { 126 if (empty($image)) {
common/modules/product/views/manage/_form.php
@@ -17,7 +17,9 @@ use kartik\select2\Select2; @@ -17,7 +17,9 @@ use kartik\select2\Select2;
17 17
18 <div class="product-form"> 18 <div class="product-form">
19 19
20 - <?php $form = ActiveForm::begin(); ?> 20 + <?php $form = ActiveForm::begin([
  21 + 'options' => ['enctype' => 'multipart/form-data']
  22 + ]); ?>
21 23
22 <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?> 24 <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
23 25
@@ -44,14 +46,22 @@ use kartik\select2\Select2; @@ -44,14 +46,22 @@ use kartik\select2\Select2;
44 ] 46 ]
45 ) ?> 47 ) ?>
46 48
47 -  
48 -  
49 - <?php /*= $form->field($model, 'imagesUpload[]')->widget(FileInput::classname(), [ 49 + <?= $form->field($model, 'imagesUpload[]')->widget(\kartik\file\FileInput::classname(), [
  50 + 'language' => 'ru',
50 'options' => [ 51 'options' => [
51 'accept' => 'image/*', 52 'accept' => 'image/*',
52 'multiple' => true, 53 'multiple' => true,
53 ], 54 ],
54 - ]);*/?> 55 + 'pluginOptions' => [
  56 + 'allowedFileExtensions' => ['jpg', 'gif', 'png'],
  57 + 'initialPreview' => !empty($model->imagesHTML) ? $model->imagesHTML : [],
  58 + 'overwriteInitial' => false,
  59 + 'showRemove' => true,
  60 + 'showUpload' => false,
  61 + 'previewFileType' => 'image',
  62 +// 'uploadUrl' => \yii\helpers\Url::to(['/site/file-upload'])
  63 + ],
  64 + ]); ?>
55 65
56 <?= $form->field($model, 'variants')->widget(MultipleInput::className(), [ 66 <?= $form->field($model, 'variants')->widget(MultipleInput::className(), [
57 'columns' => [ 67 'columns' => [
common/modules/product/widgets/views/brandsCarousel.php
@@ -3,7 +3,7 @@ @@ -3,7 +3,7 @@
3 <div class="prods_carousel"> 3 <div class="prods_carousel">
4 <ul> 4 <ul>
5 <?php foreach($brands as $brand) :?> 5 <?php foreach($brands as $brand) :?>
6 - <li><span><a href="<?= \yii\helpers\Url::to('/brands/'. $brand->alias)?>" title="<?= $brand->name?>"><img src="<?= $brand->imageUrl?>"></a></span></li> 6 + <li><span><a href="<?= \yii\helpers\Url::to('/brands/'. $brand->alias)?>" title="<?= $brand->name?>"><?= $brand->imageFile ? Yii::$app->imageCache->thumb($brand->imageFile, 'brandlist') : ''?></a></span></li>
7 <?php endforeach?> 7 <?php endforeach?>
8 </ul> 8 </ul>
9 </div> 9 </div>
common/modules/product/widgets/views/submenu.php
@@ -12,7 +12,7 @@ @@ -12,7 +12,7 @@
12 <?php if (empty($_item->image)) :?> 12 <?php if (empty($_item->image)) :?>
13 <img valign="top" src="/images/no_photo.png"> 13 <img valign="top" src="/images/no_photo.png">
14 <?php else :?> 14 <?php else :?>
15 - <img valign="top" src="<?= $_item->imageUrl?>"> 15 + <?= $_item->imageUrl ? Yii::$app->imageCache->thumb($_item->imageUrl, 'mainmenu') : ''?>
16 <?php endif?> 16 <?php endif?>
17 </div> 17 </div>
18 <div class="title"><?= $_item->categoryName->value?></div> 18 <div class="title"><?= $_item->categoryName->value?></div>
@@ -34,7 +34,7 @@ @@ -34,7 +34,7 @@
34 <?php if (empty($_item['item']->image)) :?> 34 <?php if (empty($_item['item']->image)) :?>
35 <img valign="top" src="/images/no_photo.png"> 35 <img valign="top" src="/images/no_photo.png">
36 <?php else :?> 36 <?php else :?>
37 - <img valign="top" src="<?= $_item['item']->imageUrl?>" alt="<?= $_item['item']->categoryName->value?>"> 37 + <?= $_item['item']->imageUrl ? Yii::$app->imageCache->thumb($_item['item']->imageUrl, 'mainmenu') : ''?>
38 <?php endif?> 38 <?php endif?>
39 </div> 39 </div>
40 <div class="title"><?= $_item['item']->categoryName->value?></div> 40 <div class="title"><?= $_item['item']->categoryName->value?></div>
frontend/config/main.php
@@ -41,7 +41,19 @@ return [ @@ -41,7 +41,19 @@ return [
41 'errorHandler' => [ 41 'errorHandler' => [
42 'errorAction' => 'site/error', 42 'errorAction' => 'site/error',
43 ], 43 ],
44 - 44 + 'imageCache' => [
  45 + 'class' => 'iutbay\yii2imagecache\ImageCache',
  46 + 'sourcePath' => '@app/web/images',
  47 + 'sourceUrl' => '@web/images',
  48 + 'sizes' => [
  49 + 'brandlist' => [130, 70],
  50 + 'product_item' => [130, 70],
  51 + 'product_list' => [130, 70],
  52 + 'product_list2' => [130, 70],
  53 + 'mainmenu' => [160, 170],
  54 + 'large' => [600, 600],
  55 + ],
  56 + ],
45 'urlManager' => [ 57 'urlManager' => [
46 'baseUrl' => '/', 58 'baseUrl' => '/',
47 'enablePrettyUrl' => true, 59 'enablePrettyUrl' => true,
@@ -58,6 +70,7 @@ return [ @@ -58,6 +70,7 @@ return [
58 'page/<translit:[A-Za-z0-9_-]++>'=>'page/show', 70 'page/<translit:[A-Za-z0-9_-]++>'=>'page/show',
59 'event/view/<alias:[A-Za-z0-9_-]+>'=>'event/view', 71 'event/view/<alias:[A-Za-z0-9_-]+>'=>'event/view',
60 'service/view/<alias:[A-Za-z0-9_-]+>'=>'service/view', 72 'service/view/<alias:[A-Za-z0-9_-]+>'=>'service/view',
  73 + 'thumbs/<path:.*>' => 'site/thumb',
61 //// 'catalog' => 'catalog/category', 74 //// 'catalog' => 'catalog/category',
62 // 'catalog/<alias:[A-Za-z0-9_-]+>' => 'catalog/category', 75 // 'catalog/<alias:[A-Za-z0-9_-]+>' => 'catalog/category',
63 // 'catalog/<alias:[A-Za-z0-9_-]+>/<filter>' => 'catalog/category', 76 // 'catalog/<alias:[A-Za-z0-9_-]+>/<filter>' => 'catalog/category',
frontend/controllers/SiteController.php
@@ -21,7 +21,6 @@ use yii\widgets\ActiveForm; @@ -21,7 +21,6 @@ use yii\widgets\ActiveForm;
21 */ 21 */
22 class SiteController extends Controller 22 class SiteController extends Controller
23 { 23 {
24 -  
25 /** 24 /**
26 * @inheritdoc 25 * @inheritdoc
27 */ 26 */
@@ -78,6 +77,7 @@ class SiteController extends Controller @@ -78,6 +77,7 @@ class SiteController extends Controller
78 'class' => 'yii\captcha\CaptchaAction', 77 'class' => 'yii\captcha\CaptchaAction',
79 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, 78 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
80 ], 79 ],
  80 + 'thumb' => 'iutbay\yii2imagecache\ThumbAction',
81 ]; 81 ];
82 } 82 }
83 83