Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #1865 Загрузка изображений в товар
- Loading branch information
Showing
14 changed files
with
748 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
194 changes: 194 additions & 0 deletions
194
protected/modules/store/controllers/ProductImageBackendController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
<?php | ||
|
||
use yupe\components\controllers\BackController; | ||
|
||
/** | ||
* Class ProductImageBackendController | ||
*/ | ||
class ProductImageBackendController extends BackController | ||
{ | ||
/** | ||
* @var ProductRepository | ||
*/ | ||
private $productRepository; | ||
|
||
/** | ||
* | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
|
||
$this->productRepository = Yii::app()->getComponent('productRepository'); | ||
} | ||
|
||
/** | ||
* @return array|mixed | ||
*/ | ||
public function filters() | ||
{ | ||
return CMap::mergeArray( | ||
parent::filters(), | ||
[ | ||
'postOnly + delete, addImages', | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @param integer $id Product Id | ||
* @throws CHttpException | ||
*/ | ||
public function actionIndex($id) | ||
{ | ||
$product = $this->productRepository->getById($id, []); | ||
$model = new ProductImage(); | ||
|
||
if (null === $product) { | ||
throw new CHttpException(404); | ||
} | ||
|
||
$this->render('index', [ | ||
'product' => $product, | ||
'model' => $model, | ||
]); | ||
} | ||
|
||
/** | ||
* Функция добавления группы изображений | ||
* | ||
* @param int $id - id-галереи | ||
* | ||
* @return void | ||
* | ||
* @throws CHttpException | ||
* @throws CException | ||
*/ | ||
public function actionAddImages($id) | ||
{ | ||
$product = $this->productRepository->getById($id, []); | ||
$image = new ProductImage(); | ||
$image->product_id = $product->id; | ||
|
||
if (null === $product) { | ||
throw new CHttpException(404); | ||
} | ||
|
||
if (($imageData = Yii::app()->getRequest()->getPost('ProductImage')) !== null) { | ||
$imageData = $imageData[$_FILES['ProductImage']['name']['name']]; | ||
|
||
$this->_addImage($image, $imageData, $product); | ||
if ($image->hasErrors()) { | ||
$data[] = ['error' => $image->getErrors()]; | ||
} else { | ||
$data[] = [ | ||
'name' => $image->name, | ||
'type' => $_FILES['ProductImage']['type']['name'], | ||
'size' => $_FILES['ProductImage']['size']['name'], | ||
'url' => $image->getImageUrl(), | ||
'thumbnail_url' => $image->getImageUrl(80, 80), | ||
'delete_url' => $this->createUrl( | ||
'/store/productImageBackend/deleteImage', | ||
[ | ||
'id' => $image->id, | ||
'method' => 'uploader' | ||
] | ||
), | ||
'delete_type' => 'GET' | ||
]; | ||
} | ||
|
||
Yii::app()->ajax->raw($data); | ||
} else { | ||
throw new CHttpException( | ||
404, | ||
Yii::t('StoreModule.store', 'Page not found!') | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Метод добавления одной фотографии: | ||
* | ||
* @param ProductImage $image - инстанс изображения | ||
* @param mixed $imageData - POST-массив данных | ||
* @param Product $product - инстанс галереи | ||
* | ||
* @return void | ||
* | ||
*/ | ||
private function _addImage(ProductImage $image, array $imageData, Product $product) | ||
{ | ||
try { | ||
$transaction = Yii::app()->getDb()->beginTransaction(); | ||
$image->setAttributes($imageData); | ||
|
||
if ($image->save()) { | ||
|
||
$transaction->commit(); | ||
|
||
if (Yii::app()->getRequest()->getPost('ajax') === null) { | ||
Yii::app()->getUser()->setFlash( | ||
yupe\widgets\YFlashMessages::SUCCESS_MESSAGE, | ||
Yii::t('StoreModule.store', 'Image created') | ||
); | ||
$this->redirect(['/store/productImageBackend/index', 'id' => $product->id]); | ||
} | ||
} | ||
} catch (Exception $e) { | ||
|
||
$transaction->rollback(); | ||
|
||
Yii::app()->getUser()->setFlash( | ||
yupe\widgets\YFlashMessages::ERROR_MESSAGE, | ||
$e->getMessage() | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Ajax/Get-обёртка для удаления изображения: | ||
* | ||
* @param int $id - id-изображения | ||
* @param int $product - id-товара | ||
* | ||
* @return void | ||
* | ||
* @throws CHttpException | ||
*/ | ||
public function actionDeleteImage($id, $product) | ||
{ | ||
$image = ProductImage::model()->findByPk($id); | ||
|
||
if ($image === null) { | ||
throw new CHttpException( | ||
404, | ||
Yii::t('StoreModule.store', 'Page not found!') | ||
); | ||
} | ||
|
||
$message = Yii::t( | ||
'StoreModule.store', | ||
'Image #{id} {result} deleted', | ||
[ | ||
'{id}' => $id, | ||
'{result}' => ($result = $image->delete()) | ||
? Yii::t('StoreModule.store', 'success') | ||
: Yii::t('StoreModule.store', 'not') | ||
] | ||
); | ||
|
||
if (Yii::app()->getRequest()->getIsPostRequest() && Yii::app()->getRequest()->getIsAjaxRequest) { | ||
$result === true | ||
? Yii::app()->ajax->success($message) | ||
: Yii::app()->ajax->failure($message); | ||
} | ||
|
||
Yii::app()->getUser()->setFlash( | ||
$result ? yupe\widgets\YFlashMessages::SUCCESS_MESSAGE : yupe\widgets\YFlashMessages::ERROR_MESSAGE, | ||
$message | ||
); | ||
|
||
$this->redirect(['/store/productImageBackend/index', 'id' => $product]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
protected/modules/store/views/productImageBackend/_add.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
$this->widget( | ||
'bootstrap.widgets.TbFileUpload', | ||
[ | ||
'id' => 'fileUploader', | ||
'url' => $this->createUrl("/store/productImageBackend/addImages", ['id' => $product->id]), | ||
'model' => $model, | ||
'attribute' => 'name', | ||
'multiple' => true, | ||
'formView' => 'store.views.productImageBackend._tform', | ||
'uploadView' => 'store.views.productImageBackend._upload', | ||
'options' => [ | ||
'maxFileSize' => Yii::app()->getModule('image')->maxSize, | ||
'acceptFileTypes' => 'js:/(\.|\/)(' . implode( | ||
'|', | ||
Yii::app()->getModule('image')->allowedExtensions() | ||
) . ')$/i', | ||
] | ||
] | ||
); |
26 changes: 26 additions & 0 deletions
26
protected/modules/store/views/productImageBackend/_image.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<div class="image-wrapper"> | ||
<div class="gallery-thumbnail"> | ||
<?= CHtml::link( | ||
CHtml::image($image->getImageUrl(190, 190), $image->alt), | ||
$image->getImageUrl(), | ||
[ | ||
'class' => 'gallery-image', | ||
'title' => $image->title, | ||
] | ||
); ?> | ||
</div> | ||
<div class="btn-group btn-group-xs" role="group"> | ||
<?= CHtml::link( | ||
'<i class="fa fa-fw fa-times"></i>', | ||
Yii::app()->createAbsoluteUrl('store/productImageBackend/deleteImage', [ | ||
'id' => $image->id, | ||
'product' => $product->id, | ||
]), | ||
[ | ||
'class' => 'btn btn-danger', | ||
'data-toggle' => 'tooltip', | ||
'title' => Yii::t('StoreModule.store', 'Delete'), | ||
] | ||
); ?> | ||
</div> | ||
</div> |
Oops, something went wrong.