Skip to content

Commit

Permalink
fix #2440 Массовое редактирование свойств товаров
Browse files Browse the repository at this point in the history
  • Loading branch information
sabian committed Nov 19, 2017
1 parent e90b6c6 commit 36a83d8
Show file tree
Hide file tree
Showing 8 changed files with 551 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@
--------------------------

- #1865: Загрузка изображений в товар (@sabian)
- #2440: Массовое редактирование свойств товаров (@sabian, @samox1n)
- #2606: Дебаг-панель не отображалась, если включить на сайте другой язык (@fazliddin, @sabian)
- #2615: Текст уведомления при создании карты сайта (@dzhedai, @sabian)
- #2616: Очистка кеша после изменения блока контента (@tmsk70, @sabian)
Expand Down
57 changes: 30 additions & 27 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions protected/modules/store/components/helpers/ProductBatchHelper.php
@@ -0,0 +1,55 @@
<?php

/**
* Class ProductBatchHelper
*/
class ProductBatchHelper
{
/**
*
*/
const PRICE_EQUAL = 0;

/**
*
*/
const PRICE_ADD = 1;

/**
*
*/
const PRICE_SUB = 2;

/**
*
*/
const OP_UNIT = 0;

/**
*
*/
const OP_PERCENT = 1;

/**
* @return array
*/
public static function getPericeOpList()
{
return [
self::PRICE_EQUAL => Yii::t('StoreModule.store', 'equal'),
self::PRICE_ADD => Yii::t('StoreModule.store', 'increase'),
self::PRICE_SUB => Yii::t('StoreModule.store', 'decrease'),
];
}

/**
* @return array
*/
public static function getOpUnits()
{
return [
self::OP_UNIT => Yii::t('StoreModule.store', 'unit'),
self::OP_PERCENT => Yii::t('StoreModule.store', '%'),
];
}
}
Expand Up @@ -391,4 +391,67 @@ public function getLinkedProductsDataProvider(Product $product, $typeCode = null
]
);
}

/**
* @param ProductBatchForm $form
* @param array $ids
* @return int
*/
public function batchUpdate(ProductBatchForm $form, array $ids)
{
$attributes = $form->loadQueryAttributes();

if (null !== $form->price) {
$attributes['price'] = $this->getPriceQuery(
'price',
$form->price,
(int)$form->price_op,
(int)$form->price_op_unit
);
}

if (null !== $form->discount_price) {
$attributes['discount_price'] = $this->getPriceQuery(
'discount_price',
$form->discount_price,
(int)$form->discount_price_op,
(int)$form->discount_price_op_unit
);
}

if (count($attributes) === 0) {
return true;
}

$criteria = new CDbCriteria();
$criteria->addInCondition('id', $ids);

return Product::model()->updateAll($attributes, $criteria);
}

/**
* @param $field
* @param $price
* @param $operation
* @param $unit
* @return float|CDbExpression
*/
private function getPriceQuery($field, $price, $operation, $unit)
{
if (ProductBatchHelper::PRICE_EQUAL === $operation) {
return $price;
}

$sign = ProductBatchHelper::PRICE_ADD === $operation ? '+' : '-';

if (ProductBatchHelper::OP_PERCENT === $unit) {
return new CDbExpression(sprintf('%s %s ((%s / 100) * :percent)', $field, $sign, $field), [
':percent' => $price
]);
}

return new CDbExpression(sprintf('%s %s :price', $field, $sign), [
':price' => $price
]);
}
}
28 changes: 25 additions & 3 deletions protected/modules/store/controllers/ProductBackendController.php
Expand Up @@ -61,10 +61,10 @@ public function accessRules()
['allow', 'roles' => ['admin'],],
['allow', 'actions' => ['index'], 'roles' => ['Store.ProductBackend.Index'],],
['allow', 'actions' => ['view'], 'roles' => ['Store.ProductBackend.View'],],
['allow', 'actions' => ['create', 'copy'], 'roles' => ['Store.ProductBackend.Create'],],
['allow', 'actions' => ['create', 'copy', 'batch'], 'roles' => ['Store.ProductBackend.Create'],],
[
'allow',
'actions' => ['update', 'inline', 'sortable', 'deleteImage'],
'actions' => ['update', 'inline', 'sortable', 'deleteImage', 'batch'],
'roles' => ['Store.ProductBackend.Update'],
],
['allow', 'actions' => ['delete', 'multiaction'], 'roles' => ['Store.ProductBackend.Delete'],],
Expand Down Expand Up @@ -306,7 +306,10 @@ public function actionIndex()
Yii::app()->getRequest()->getQuery('Product')
);
}
$this->render('index', ['model' => $model]);
$this->render('index', [
'model' => $model,
'batchModel' => new ProductBatchForm(),
]);
}

/**
Expand Down Expand Up @@ -408,4 +411,23 @@ public function actionCopy()
Yii::app()->ajax->success();
}
}

/**
*
*/
public function actionBatch()
{
$form = new ProductBatchForm();
$form->setAttributes(Yii::app()->getRequest()->getPost('ProductBatchForm'));

if ($form->validate() === false) {
Yii::app()->ajax->failure(Yii::t('StoreModule.store', 'Wrong data'));
}

if ($this->productRepository->batchUpdate($form, explode(',', Yii::app()->getRequest()->getPost('ids')))) {
Yii::app()->ajax->success('ok');
}

Yii::app()->ajax->failure();
}
}

0 comments on commit 36a83d8

Please sign in to comment.