Skip to content

Commit

Permalink
Merge pull request #618 from magento-troll/MAGETWO-42357
Browse files Browse the repository at this point in the history
[Merchant Beta][MX] Bugfixes
  • Loading branch information
slopukhov committed Sep 18, 2015
2 parents 80c8dc8 + 0b9310f commit ef89599
Show file tree
Hide file tree
Showing 10 changed files with 360 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,44 @@
*/
namespace Magento\Catalog\Controller\Adminhtml\Product\Action\Attribute;

use Magento\Ui\Component\MassAction\Filter;
use Magento\Catalog\Model\Resource\Product\CollectionFactory;

class Edit extends \Magento\Catalog\Controller\Adminhtml\Product\Action\Attribute
{
/**
* @var \Magento\Framework\View\Result\PageFactory
*/
protected $resultPageFactory;

/**
* MassActions filter
*
* @var Filter
*/
protected $filter;

/**
* @var CollectionFactory
*/
protected $collectionFactory;

/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Catalog\Helper\Product\Edit\Action\Attribute $attributeHelper
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
* @param Filter $filter
* @param CollectionFactory $collectionFactory
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Catalog\Helper\Product\Edit\Action\Attribute $attributeHelper,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
Filter $filter,
CollectionFactory $collectionFactory
) {
$this->filter = $filter;
$this->collectionFactory = $collectionFactory;
parent::__construct($context, $attributeHelper);
$this->resultPageFactory = $resultPageFactory;
}
Expand All @@ -32,6 +53,9 @@ public function __construct(
*/
public function execute()
{
$collection = $this->filter->getCollection($this->collectionFactory->create());
$this->attributeHelper->setProductIds($collection->getAllIds());

if (!$this->_validateProducts()) {
return $this->resultRedirectFactory->create()->setPath('catalog/product/', ['_current' => true]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,69 +6,58 @@
*/
namespace Magento\Catalog\Controller\Adminhtml\Product;

use Magento\Catalog\Model\Resource\Product\Collection;
use Magento\Framework\Controller\ResultFactory;
use Magento\Catalog\Controller\Adminhtml\Product\Builder;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Catalog\Model\Resource\Product\CollectionFactory;

class MassDelete extends \Magento\Catalog\Controller\Adminhtml\Product
{
/**
* Field id
* Massactions filter
*
* @var Filter
*/
const ID_FIELD = 'entity_id';
protected $filter;

/**
* Redirect url
* @var CollectionFactory
*/
const REDIRECT_URL = 'catalog/*/index';
protected $collectionFactory;

/**
* Resource collection
*
* @var string
* @param Context $context
* @param Builder $productBuilder
* @param Filter $filter
* @param CollectionFactory $collectionFactory
*/
protected $collection = 'Magento\Catalog\Model\Resource\Product\Collection';
public function __construct(
Context $context,
Builder $productBuilder,
Filter $filter,
CollectionFactory $collectionFactory
) {
$this->filter = $filter;
$this->collectionFactory = $collectionFactory;
parent::__construct($context, $productBuilder);
}

/**
* @return \Magento\Backend\Model\View\Result\Redirect
*/
public function execute()
{
$selected = $this->getRequest()->getParam('selected');
$excluded = $this->getRequest()->getParam('excluded');

$collection = $this->_objectManager->create($this->collection);
try {
if (!empty($excluded)) {
$collection->addFieldToFilter(static::ID_FIELD, ['nin' => $excluded]);
$this->massAction($collection);
} elseif (!empty($selected)) {
$collection->addFieldToFilter(static::ID_FIELD, ['in' => $selected]);
$this->massAction($collection);
} else {
$this->messageManager->addError(__('Please select product(s).'));
}
} catch (\Exception $e) {
$this->messageManager->addError($e->getMessage());
}

/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
return $resultRedirect->setPath(static::REDIRECT_URL);
}

/**
* Cancel selected orders
*
* @param Collection $collection
* @return void
*/
protected function massAction($collection)
{
$count = 0;
$collection = $this->filter->getCollection($this->collectionFactory->create());
$productDeleted = 0;
foreach ($collection->getItems() as $product) {
$product->delete();
++$count;
$productDeleted++;
}
$this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $count));
$this->messageManager->addSuccess(
__('A total of %1 record(s) have been deleted.', $productDeleted)
);

return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('catalog/*/index');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Magento\Backend\App\Action;
use Magento\Catalog\Controller\Adminhtml\Product;
use Magento\Framework\Controller\ResultFactory;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Catalog\Model\Resource\Product\CollectionFactory;

class MassStatus extends \Magento\Catalog\Controller\Adminhtml\Product
{
Expand All @@ -17,16 +19,34 @@ class MassStatus extends \Magento\Catalog\Controller\Adminhtml\Product
*/
protected $_productPriceIndexerProcessor;

/**
* MassActions filter
*
* @var Filter
*/
protected $filter;

/**
* @var CollectionFactory
*/
protected $collectionFactory;

/**
* @param Action\Context $context
* @param Builder $productBuilder
* @param \Magento\Catalog\Model\Indexer\Product\Price\Processor $productPriceIndexerProcessor
* @param Filter $filter
* @param CollectionFactory $collectionFactory
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
Product\Builder $productBuilder,
\Magento\Catalog\Model\Indexer\Product\Price\Processor $productPriceIndexerProcessor
\Magento\Catalog\Model\Indexer\Product\Price\Processor $productPriceIndexerProcessor,
Filter $filter,
CollectionFactory $collectionFactory
) {
$this->filter = $filter;
$this->collectionFactory = $collectionFactory;
$this->_productPriceIndexerProcessor = $productPriceIndexerProcessor;
parent::__construct($context, $productBuilder);
}
Expand Down Expand Up @@ -57,7 +77,8 @@ public function _validateMassStatus(array $productIds, $status)
*/
public function execute()
{
$productIds = (array) $this->getRequest()->getParam('product');
$collection = $this->filter->getCollection($this->collectionFactory->create());
$productIds = $collection->getAllIds();
$storeId = (int) $this->getRequest()->getParam('store', 0);
$status = (int) $this->getRequest()->getParam('status');

Expand Down
16 changes: 12 additions & 4 deletions app/code/Magento/Catalog/Helper/Product/Edit/Action/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,25 @@ public function getProducts()
return $this->_products;
}

/**
* Set array of selected product
*
* @param array $productIds
*
* @return void
*/
public function setProductIds($productIds)
{
$this->_session->setProductIds($productIds);
}

/**
* Return array of selected product ids from post or session
*
* @return array|null
*/
public function getProductIds()
{
if ($this->_getRequest()->isPost() && $this->_getRequest()->getActionName() == 'edit') {
$this->_session->setProductIds($this->_getRequest()->getParam('selected', null));
}

return $this->_session->getProductIds();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class Collection extends \Magento\Catalog\Model\Resource\Collection\AbstractColl
*/
const MAIN_TABLE_ALIAS = 'e';

/**
* @var string
*/
protected $_idFieldName = 'entity_id';

/**
* Catalog Product Flat is enabled cache per store
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ protected function setUp()
->disableOriginalConstructor()->getMock();

$productBuilder = $this->getMockBuilder('Magento\Catalog\Controller\Adminhtml\Product\Builder')->setMethods([
'build',
])->disableOriginalConstructor()->getMock();
'build',
])->disableOriginalConstructor()->getMock();

$product = $this->getMockBuilder('\Magento\Catalog\Model\Product')->disableOriginalConstructor()
->setMethods(['getTypeId', 'getStoreId', '__sleep', '__wakeup'])->getMock();
Expand All @@ -43,11 +43,37 @@ protected function setUp()
->with(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT)
->willReturn($this->resultRedirect);

$abstractDbMock = $this->getMockBuilder('Magento\Framework\Data\Collection\AbstractDb')
->disableOriginalConstructor()
->setMethods(['getAllIds', 'getResource'])
->getMock();
$abstractDbMock->expects($this->any())
->method('getAllIds')
->willReturn([]);

$filterMock = $this->getMockBuilder('Magento\Ui\Component\MassAction\Filter')
->disableOriginalConstructor()
->setMethods(['getCollection'])
->getMock();
$filterMock->expects($this->any())
->method('getCollection')
->willReturn($abstractDbMock);

$collectionFactoryMock = $this->getMockBuilder('Magento\Catalog\Model\Resource\Product\CollectionFactory')
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();
$collectionFactoryMock->expects($this->any())
->method('create')
->willReturn($abstractDbMock);

$additionalParams = ['resultFactory' => $resultFactory];
$this->action = new \Magento\Catalog\Controller\Adminhtml\Product\MassStatus(
$this->initContext($additionalParams),
$productBuilder,
$this->priceProcessor
$this->priceProcessor,
$filterMock,
$collectionFactoryMock
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,17 @@
<item name="label" xsi:type="string" translate="true">Delete</item>
<item name="url" xsi:type="string">catalog/product/massDelete</item>
</item>
<item name="enable" xsi:type="array">
<item name="type" xsi:type="string">enable</item>
<item name="label" xsi:type="string" translate="true">Status - Enable</item>
<item name="url" xsi:type="string">catalog/product/massStatus/status/1</item>
</item>
<item name="disable" xsi:type="array">
<item name="type" xsi:type="string">status</item>
<item name="label" xsi:type="string" translate="true">Change status</item>
<item name="url" xsi:type="string">catalog/*/massStatus</item>
<item name="type" xsi:type="string">disable</item>
<item name="label" xsi:type="string" translate="true">Status - Disable</item>
<item name="url" xsi:type="string">catalog/product/massStatus/status/2</item>
</item>
<item name="enable" xsi:type="array">
<item name="attributes" xsi:type="array">
<item name="type" xsi:type="string">attributes</item>
<item name="label" xsi:type="string" translate="true">Update attributes</item>
<item name="url" xsi:type="string">catalog/product_action_attribute/edit</item>
Expand Down
Loading

0 comments on commit ef89599

Please sign in to comment.