Skip to content

Commit

Permalink
Merge pull request #461 from magento-troll/MAGETWO-49154-updated
Browse files Browse the repository at this point in the history
[Troll] EAV and Store usage changes
  • Loading branch information
slopukhov committed Mar 22, 2016
2 parents 0cd6b3a + db5a937 commit 28f2d03
Show file tree
Hide file tree
Showing 61 changed files with 1,445 additions and 574 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ public function __construct(
public function execute()
{
$storeId = (int)$this->getRequest()->getParam('store');
$store = $this->storeManager->getStore($storeId);
$this->storeManager->setCurrentStore($store->getCode());

$categoryId = (int)$this->getRequest()->getParam('id');

if (!$categoryId) {
Expand Down
15 changes: 14 additions & 1 deletion app/code/Magento/Catalog/Controller/Adminhtml/Category/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
namespace Magento\Catalog\Controller\Adminhtml\Category;

use Magento\Store\Model\StoreManagerInterface;

/**
* Class Save
*
Expand Down Expand Up @@ -43,24 +45,32 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Category
]
];

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* Constructor
*
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
* @param \Magento\Framework\View\LayoutFactory $layoutFactory
* @param StoreManagerInterface $storeManager
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
\Magento\Framework\View\LayoutFactory $layoutFactory
\Magento\Framework\View\LayoutFactory $layoutFactory,
StoreManagerInterface $storeManager
) {
parent::__construct($context);
$this->resultRawFactory = $resultRawFactory;
$this->resultJsonFactory = $resultJsonFactory;
$this->layoutFactory = $layoutFactory;
$this->storeManager = $storeManager;
}

/**
Expand Down Expand Up @@ -104,6 +114,8 @@ public function execute()
$data = $this->stringToBoolConverting($data);
$data = $this->imagePreprocessing($data);
$storeId = isset($data['general']['store_id']) ? $data['general']['store_id'] : null;
$store = $this->storeManager->getStore($storeId);
$this->storeManager->setCurrentStore($store->getCode());
$parentId = isset($data['general']['parent']) ? $data['general']['parent'] : null;
if ($data['general']) {
$category->addData($this->_filterCategoryPostData($data['general']));
Expand Down Expand Up @@ -163,6 +175,7 @@ public function execute()
if ($category->hasCustomDesignTo()) {
$categoryResource->getAttribute('custom_design_from')->setMaxValue($category->getCustomDesignTo());
}

$validate = $category->validate();
if ($validate !== true) {
foreach ($validate as $code => $error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public function __construct(
*/
public function execute()
{
/** @var \Magento\Store\Model\StoreManagerInterface $storeManager */
$storeManager = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface');
$storeId = (int) $this->getRequest()->getParam('store', 0);
$store = $storeManager->getStore($storeId);
$storeManager->setCurrentStore($store->getCode());
$productId = (int) $this->getRequest()->getParam('id');
$product = $this->productBuilder->build($this->getRequest());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function initialize(\Magento\Catalog\Model\Product $product)
$product->lockAttribute('media');
}

if ($this->storeManager->hasSingleStore()) {
if ($this->storeManager->hasSingleStore() && empty($product->getWebsiteIds())) {
$product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsite()->getId()]);
}

Expand Down
17 changes: 15 additions & 2 deletions app/code/Magento/Catalog/Controller/Adminhtml/Product/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Magento\Backend\App\Action;
use Magento\Catalog\Controller\Adminhtml\Product;
use Magento\Store\Model\StoreManagerInterface;

class Save extends \Magento\Catalog\Controller\Adminhtml\Product
{
Expand Down Expand Up @@ -37,13 +38,21 @@ class Save extends \Magento\Catalog\Controller\Adminhtml\Product
protected $productRepository;

/**
* @var StoreManagerInterface
*/
private $storeManager;

/**
* Save constructor.
*
* @param Action\Context $context
* @param Builder $productBuilder
* @param Initialization\Helper $initializationHelper
* @param \Magento\Catalog\Model\Product\Copier $productCopier
* @param \Magento\Catalog\Model\Product\TypeTransitionManager $productTypeManager
* @param \Magento\Catalog\Api\CategoryLinkManagementInterface $categoryLinkManagement
* @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
* @param StoreManagerInterface $storeManager
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
Expand All @@ -52,13 +61,15 @@ public function __construct(
\Magento\Catalog\Model\Product\Copier $productCopier,
\Magento\Catalog\Model\Product\TypeTransitionManager $productTypeManager,
\Magento\Catalog\Api\CategoryLinkManagementInterface $categoryLinkManagement,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
StoreManagerInterface $storeManager
) {
$this->initializationHelper = $initializationHelper;
$this->productCopier = $productCopier;
$this->productTypeManager = $productTypeManager;
$this->categoryLinkManagement = $categoryLinkManagement;
$this->productRepository = $productRepository;
$this->storeManager = $storeManager;
parent::__construct($context, $productBuilder);
}

Expand All @@ -71,7 +82,9 @@ public function __construct(
*/
public function execute()
{
$storeId = $this->getRequest()->getParam('store');
$storeId = $this->getRequest()->getParam('store', 0);
$store = $this->storeManager->getStore($storeId);
$this->storeManager->setCurrentStore($store->getCode());
$redirectBack = $this->getRequest()->getParam('back', false);
$productId = $this->getRequest()->getParam('id');
$resultRedirect = $this->resultRedirectFactory->create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ public function afterLoad($object)
if ($attributeCode == 'available_sort_by') {
$data = $object->getData($attributeCode);
if ($data) {
$object->setData($attributeCode, explode(',', $data));
if (!is_array($data)) {
$object->setData($attributeCode, explode(',', $data));
} else {
$object->setData($attributeCode, $data);
}

}
}
return $this;
Expand Down
124 changes: 19 additions & 105 deletions app/code/Magento/Catalog/Model/ResourceModel/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public function __construct(
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Catalog\Model\ResourceModel\Category\TreeFactory $categoryTreeFactory,
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
EntityManager $entityManager,
Category\AggregateCount $aggregateCount,
EntityManager $entityManager,
$data = []
) {
parent::__construct(
Expand Down Expand Up @@ -990,96 +990,19 @@ public function countVisible()
/**
* Reset firstly loaded attributes
*
* @param \Magento\Framework\Model\AbstractModel $object
* @param \Magento\Framework\DataObject $object
* @param integer $entityId
* @param array|null $attributes
* @return $this
*/
public function load($object, $entityId, $attributes = [])
{
\Magento\Framework\Profiler::start('EAV:load_entity');
/**
* Load object base row data
*/
$this->entityManager->load(CategoryInterface::class, $object, $entityId);

$this->_attributes = [];
$this->loadAttributesMetadata($attributes);
$object = $this->entityManager->load(CategoryInterface::class, $object, $entityId);
if (!$this->entityManager->has(\Magento\Catalog\Api\Data\CategoryInterface::class, $entityId)) {
$object->isObjectNew(true);
}

$this->loadAttributesMetadata($attributes);

$this->_loadModelAttributes($object);

$object->setOrigData();

$this->_afterLoad($object);

\Magento\Framework\Profiler::stop('EAV:load_entity');
return $this;
}

/**
* Save object collected data
*
* @param array $saveData array('newObject', 'entityRow', 'insert', 'update', 'delete')
* @return $this
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function _processSaveData($saveData)
{
extract($saveData, EXTR_SKIP);
/**
* Import variables into the current symbol table from save data array
*
* @see \Magento\Eav\Model\Entity\AbstractEntity::_collectSaveData()
*
* @var array $entityRow
* @var \Magento\Framework\Model\AbstractModel $newObject
* @var array $insert
* @var array $update
* @var array $delete
*/

/**
* Process base row
*/
$this->entityManager->save(CategoryInterface::class, $newObject);

/**
* insert attribute values
*/
if (!empty($insert)) {
foreach ($insert as $attributeId => $value) {
$attribute = $this->getAttribute($attributeId);
$this->_insertAttribute($newObject, $attribute, $value);
}
}

/**
* update attribute values
*/
if (!empty($update)) {
foreach ($update as $attributeId => $v) {
$attribute = $this->getAttribute($attributeId);
$this->_updateAttribute($newObject, $attribute, $v['value_id'], $v['value']);
}
}

/**
* delete empty attribute values
*/
if (!empty($delete)) {
foreach ($delete as $table => $values) {
$this->_deleteAttributes($newObject, $table, $values);
}
}

$this->_processAttributeValues();

$newObject->isObjectNew(false);

return $this;
}

Expand All @@ -1088,33 +1011,24 @@ protected function _processSaveData($saveData)
*/
public function delete($object)
{
try {
$this->transactionManager->start($this->getConnection());
if (is_numeric($object)) {
} elseif ($object instanceof \Magento\Framework\Model\AbstractModel) {
$object->beforeDelete();
}
$this->_beforeDelete($object);
$this->entityManager->delete(\Magento\Catalog\Api\Data\CategoryInterface::class, $object);

$this->_afterDelete($object);

if ($object instanceof \Magento\Framework\Model\AbstractModel) {
$object->isDeleted(true);
$object->afterDelete();
}
$this->transactionManager->commit();
if ($object instanceof \Magento\Framework\Model\AbstractModel) {
$object->afterDeleteCommit();
}
} catch (\Exception $e) {
$this->transactionManager->rollBack();
throw $e;
}
$this->entityManager->delete(CategoryInterface::class, $object);
$this->_eventManager->dispatch(
'catalog_category_delete_after_done',
['product' => $object]
);
return $this;
}

/**
* Save entity's attributes into the object's resource
*
* @param \Magento\Framework\Model\AbstractModel $object
* @return $this
* @throws \Exception
*/
public function save(\Magento\Framework\Model\AbstractModel $object)
{
$this->entityManager->save(CategoryInterface::class, $object);
return $this;
}
}
Loading

0 comments on commit 28f2d03

Please sign in to comment.