Commit e2128676924b0a9f5c1c585154af6c9686e04f09
1 parent
85756013
Karnovsky 05052016 1600
Showing
11 changed files
with
151 additions
and
32 deletions
Show diff stats
backend/views/brand/_form.php
... | ... | @@ -24,11 +24,10 @@ use yii\widgets\ActiveForm; |
24 | 24 | ], |
25 | 25 | 'pluginOptions' => [ |
26 | 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 | 79 | ], |
80 | 80 | 'pluginOptions' => [ |
81 | 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 | 4 | |
5 | 5 | use common\modules\product\helpers\ProductHelper; |
6 | 6 | use common\modules\product\models\Category; |
7 | +use common\modules\product\models\ProductImage; | |
7 | 8 | use common\modules\product\models\ProductVariant; |
8 | 9 | use common\modules\product\models\RemoteProductsSearch; |
9 | 10 | use Yii; |
... | ... | @@ -15,6 +16,7 @@ use yii\filters\VerbFilter; |
15 | 16 | use common\modules\product\models\Brand; |
16 | 17 | use common\modules\product\models\BrandName; |
17 | 18 | use common\modules\product\models\RemoteProducts; |
19 | +use yii\web\UploadedFile; | |
18 | 20 | |
19 | 21 | /** |
20 | 22 | * ManageController implements the CRUD actions for Product model. |
... | ... | @@ -122,8 +124,26 @@ class ManageController extends Controller |
122 | 124 | { |
123 | 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 | 145 | return $this->redirect(['view', 'id' => $model->product_id]); |
146 | + | |
127 | 147 | } else { |
128 | 148 | return $this->render('create', [ |
129 | 149 | 'model' => $model, |
... | ... | @@ -141,7 +161,24 @@ class ManageController extends Controller |
141 | 161 | { |
142 | 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 | 182 | return $this->redirect(['view', 'id' => $model->product_id]); |
146 | 183 | } else { |
147 | 184 | $groups = $model->category->getTaxGroups(); | ... | ... |
common/modules/product/models/Brand.php
... | ... | @@ -41,7 +41,7 @@ class Brand extends \yii\db\ActiveRecord |
41 | 41 | 'valueKeyName' => 'value', |
42 | 42 | 'slugKeyName' => 'alias', |
43 | 43 | 'translit' => true |
44 | - ] | |
44 | + ], | |
45 | 45 | ], |
46 | 46 | ]; |
47 | 47 | } |
... | ... | @@ -119,6 +119,10 @@ class Brand extends \yii\db\ActiveRecord |
119 | 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 | 126 | public function getImageUrl() { |
123 | 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 | 7 | use Yii; |
8 | 8 | use common\modules\relation\relationBehavior; |
9 | 9 | use yii\db\ActiveQuery; |
10 | +use yii\helpers\Html; | |
11 | +use yii\web\UploadedFile; | |
10 | 12 | |
11 | 13 | /** |
12 | 14 | * This is the model class for table "{{%product}}". |
... | ... | @@ -68,8 +70,8 @@ class Product extends \yii\db\ActiveRecord |
68 | 70 | [['brand_id'], 'integer'], |
69 | 71 | [['name'], 'string', 'max' => 150], |
70 | 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 | 75 | [['description', 'video'], 'safe'], |
74 | 76 | [['is_top', 'is_new'], 'boolean'], |
75 | 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 | 189 | { |
188 | 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 | 206 | $todel = []; |
... | ... | @@ -213,4 +225,26 @@ class Product extends \yii\db\ActiveRecord |
213 | 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 | 10 | * |
11 | 11 | * @property integer $product_image_id |
12 | 12 | * @property integer $product_id |
13 | + * @property integer $product_variant_id | |
13 | 14 | * @property string $image |
14 | 15 | * @property string $alt |
15 | 16 | * @property string $title |
16 | - * | |
17 | 17 | * @property Product $product |
18 | + * @property ProductVariant $productVariant | |
18 | 19 | */ |
19 | 20 | class ProductImage extends \yii\db\ActiveRecord |
20 | 21 | { |
21 | - public $image; | |
22 | + public $imageUpload; | |
22 | 23 | /** |
23 | 24 | * @inheritdoc |
24 | 25 | */ |
... | ... | @@ -33,12 +34,13 @@ class ProductImage extends \yii\db\ActiveRecord |
33 | 34 | public function rules() |
34 | 35 | { |
35 | 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 | 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 | 52 | return [ |
51 | 53 | 'product_image_id' => Yii::t('product', 'Product Image ID'), |
52 | 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 | 59 | 'image' => Yii::t('product', 'Image'), |
54 | 60 | 'alt' => Yii::t('product', 'Alt'), |
55 | 61 | 'title' => Yii::t('product', 'Title'), |
... | ... | @@ -61,7 +67,19 @@ class ProductImage extends \yii\db\ActiveRecord |
61 | 67 | */ |
62 | 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 | 108 | { |
91 | 109 | // return a default image placeholder if your source image is not found |
92 | 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 | 120 | // get the uploaded file instance. for multiple file uploads |
103 | 121 | // the following data will return an array (you may need to use |
104 | 122 | // getInstances method) |
105 | - $image = UploadedFile::getInstance($this, 'image'); | |
123 | + $image = UploadedFile::getInstance($this, 'imageUpload'); | |
106 | 124 | |
107 | 125 | // if no image was uploaded abort the upload |
108 | 126 | if (empty($image)) { | ... | ... |
common/modules/product/views/manage/_form.php
... | ... | @@ -17,7 +17,9 @@ use kartik\select2\Select2; |
17 | 17 | |
18 | 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 | 24 | <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?> |
23 | 25 | |
... | ... | @@ -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 | 51 | 'options' => [ |
51 | 52 | 'accept' => 'image/*', |
52 | 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 | 66 | <?= $form->field($model, 'variants')->widget(MultipleInput::className(), [ |
57 | 67 | 'columns' => [ | ... | ... |
common/modules/product/widgets/views/brandsCarousel.php
... | ... | @@ -3,7 +3,7 @@ |
3 | 3 | <div class="prods_carousel"> |
4 | 4 | <ul> |
5 | 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 | 7 | <?php endforeach?> |
8 | 8 | </ul> |
9 | 9 | </div> | ... | ... |
common/modules/product/widgets/views/submenu.php
... | ... | @@ -12,7 +12,7 @@ |
12 | 12 | <?php if (empty($_item->image)) :?> |
13 | 13 | <img valign="top" src="/images/no_photo.png"> |
14 | 14 | <?php else :?> |
15 | - <img valign="top" src="<?= $_item->imageUrl?>"> | |
15 | + <?= $_item->imageUrl ? Yii::$app->imageCache->thumb($_item->imageUrl, 'mainmenu') : ''?> | |
16 | 16 | <?php endif?> |
17 | 17 | </div> |
18 | 18 | <div class="title"><?= $_item->categoryName->value?></div> |
... | ... | @@ -34,7 +34,7 @@ |
34 | 34 | <?php if (empty($_item['item']->image)) :?> |
35 | 35 | <img valign="top" src="/images/no_photo.png"> |
36 | 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 | 38 | <?php endif?> |
39 | 39 | </div> |
40 | 40 | <div class="title"><?= $_item['item']->categoryName->value?></div> | ... | ... |
frontend/config/main.php
... | ... | @@ -41,7 +41,19 @@ return [ |
41 | 41 | 'errorHandler' => [ |
42 | 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 | 57 | 'urlManager' => [ |
46 | 58 | 'baseUrl' => '/', |
47 | 59 | 'enablePrettyUrl' => true, |
... | ... | @@ -58,6 +70,7 @@ return [ |
58 | 70 | 'page/<translit:[A-Za-z0-9_-]++>'=>'page/show', |
59 | 71 | 'event/view/<alias:[A-Za-z0-9_-]+>'=>'event/view', |
60 | 72 | 'service/view/<alias:[A-Za-z0-9_-]+>'=>'service/view', |
73 | + 'thumbs/<path:.*>' => 'site/thumb', | |
61 | 74 | //// 'catalog' => 'catalog/category', |
62 | 75 | // 'catalog/<alias:[A-Za-z0-9_-]+>' => 'catalog/category', |
63 | 76 | // 'catalog/<alias:[A-Za-z0-9_-]+>/<filter>' => 'catalog/category', | ... | ... |
frontend/controllers/SiteController.php
... | ... | @@ -21,7 +21,6 @@ use yii\widgets\ActiveForm; |
21 | 21 | */ |
22 | 22 | class SiteController extends Controller |
23 | 23 | { |
24 | - | |
25 | 24 | /** |
26 | 25 | * @inheritdoc |
27 | 26 | */ |
... | ... | @@ -78,6 +77,7 @@ class SiteController extends Controller |
78 | 77 | 'class' => 'yii\captcha\CaptchaAction', |
79 | 78 | 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, |
80 | 79 | ], |
80 | + 'thumb' => 'iutbay\yii2imagecache\ThumbAction', | |
81 | 81 | ]; |
82 | 82 | } |
83 | 83 | ... | ... |