Skip to content

Commit

Permalink
Merge pull request #2805 from magento-tsg/2.2-develop-pr33
Browse files Browse the repository at this point in the history
[TSG] Backporting for 2.2 (pr33) (2.2.6)
  • Loading branch information
Alexander Akimov authored Jul 5, 2018
2 parents 03d5c35 + 642ac55 commit 38dfc6e
Show file tree
Hide file tree
Showing 34 changed files with 1,035 additions and 139 deletions.
83 changes: 56 additions & 27 deletions app/code/Magento/Catalog/Controller/Adminhtml/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,27 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Controller\Adminhtml;

use Magento\Store\Model\Store;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Stdlib\DateTime\Filter\Date;
use Magento\Catalog\Model\Category as CategoryModel;
use Magento\Backend\App\Action;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Registry;
use Magento\Cms\Model\Wysiwyg\Config;
use Magento\Backend\Model\View\Result\Page;
use Magento\Framework\Controller\Result\Json;
use Magento\Backend\Model\Auth\Session;
use Magento\Framework\DataObject;

/**
* Catalog category controller
*/
abstract class Category extends \Magento\Backend\App\Action
abstract class Category extends Action
{
/**
* Authorization level of a basic admin session
Expand All @@ -18,18 +33,16 @@ abstract class Category extends \Magento\Backend\App\Action
const ADMIN_RESOURCE = 'Magento_Catalog::categories';

/**
* @var \Magento\Framework\Stdlib\DateTime\Filter\Date
* @var Date
*/
protected $dateFilter;

/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Stdlib\DateTime\Filter\Date|null $dateFilter
* @param Context $context
* @param Date|null $dateFilter
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter = null
) {
public function __construct(Context $context, Date $dateFilter = null)
{
$this->dateFilter = $dateFilter;
parent::__construct($context);
}
Expand All @@ -39,20 +52,20 @@ public function __construct(
* Root category can be returned, if inappropriate store/category is specified
*
* @param bool $getRootInstead
* @return \Magento\Catalog\Model\Category|false
* @return CategoryModel|false
*/
protected function _initCategory($getRootInstead = false)
protected function _initCategory(bool $getRootInstead = false)
{
$categoryId = $this->resolveCategoryId();
$storeId = (int)$this->getRequest()->getParam('store');
$category = $this->_objectManager->create(\Magento\Catalog\Model\Category::class);
$storeId = $this->resolveStoreId();
$category = $this->_objectManager->create(CategoryModel::class);
$category->setStoreId($storeId);

if ($categoryId) {
$category->load($categoryId);
if ($storeId) {
$rootId = $this->_objectManager->get(
\Magento\Store\Model\StoreManagerInterface::class
StoreManagerInterface::class
)->getStore(
$storeId
)->getRootCategoryId();
Expand All @@ -67,9 +80,9 @@ protected function _initCategory($getRootInstead = false)
}
}

$this->_objectManager->get(\Magento\Framework\Registry::class)->register('category', $category);
$this->_objectManager->get(\Magento\Framework\Registry::class)->register('current_category', $category);
$this->_objectManager->get(\Magento\Cms\Model\Wysiwyg\Config::class)
$this->_objectManager->get(Registry::class)->register('category', $category);
$this->_objectManager->get(Registry::class)->register('current_category', $category);
$this->_objectManager->get(Config::class)
->setStoreId($this->getRequest()->getParam('store'));
return $category;
}
Expand All @@ -79,31 +92,46 @@ protected function _initCategory($getRootInstead = false)
*
* @return int
*/
private function resolveCategoryId()
private function resolveCategoryId(): int
{
$categoryId = (int)$this->getRequest()->getParam('id', false);

return $categoryId ?: (int)$this->getRequest()->getParam('entity_id', false);
}

/**
* Resolve store id
*
* Tries to take store id from store HTTP parameter
* @see Store
*
* @return int
*/
private function resolveStoreId(): int
{
$storeId = (int)$this->getRequest()->getParam('store', false);

return $storeId ?: (int)$this->getRequest()->getParam('store_id', Store::DEFAULT_STORE_ID);
}

/**
* Build response for ajax request
*
* @param \Magento\Catalog\Model\Category $category
* @param \Magento\Backend\Model\View\Result\Page $resultPage
* @param CategoryModel $category
* @param Page $resultPage
*
* @return \Magento\Framework\Controller\Result\Json
* @return Json
*
* @deprecated 101.0.0
*/
protected function ajaxRequestResponse($category, $resultPage)
protected function ajaxRequestResponse(CategoryModel $category, Page $resultPage): Json
{
// prepare breadcrumbs of selected category, if any
$breadcrumbsPath = $category->getPath();
if (empty($breadcrumbsPath)) {
// but if no category, and it is deleted - prepare breadcrumbs from path, saved in session
$breadcrumbsPath = $this->_objectManager->get(
\Magento\Backend\Model\Auth\Session::class
Session::class
)->getDeletedPath(
true
);
Expand All @@ -119,7 +147,7 @@ protected function ajaxRequestResponse($category, $resultPage)
}
}

$eventResponse = new \Magento\Framework\DataObject([
$eventResponse = new DataObject([
'content' => $resultPage->getLayout()->getUiComponent('category_form')->getFormHtml()
. $resultPage->getLayout()->getBlock('category.tree')
->getBreadcrumbsJavascript($breadcrumbsPath, 'editingCategoryBreadcrumbs'),
Expand All @@ -130,22 +158,23 @@ protected function ajaxRequestResponse($category, $resultPage)
'category_prepare_ajax_response',
['response' => $eventResponse, 'controller' => $this]
);
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->_objectManager->get(\Magento\Framework\Controller\Result\Json::class);
/** @var Json $resultJson */
$resultJson = $this->_objectManager->get(Json::class);
$resultJson->setHeader('Content-type', 'application/json', true);
$resultJson->setData($eventResponse->getData());

return $resultJson;
}

/**
* Datetime data preprocessing
*
* @param \Magento\Catalog\Model\Category $category
* @param CategoryModel $category
* @param array $postData
*
* @return array
*/
protected function dateTimePreprocessing($category, $postData)
protected function dateTimePreprocessing(CategoryModel $category, array $postData): array
{
$dateFieldFilters = [];
$attributes = $category->getAttributes();
Expand Down
23 changes: 5 additions & 18 deletions app/code/Magento/Catalog/Model/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,10 @@ protected function initializeProductData(array $productData, $createNew)
unset($productData['media_gallery']);
if ($createNew) {
$product = $this->productFactory->create();
$this->assignProductToWebsites($product);
if (isset($productData['price']) && !isset($productData['product_type'])) {
$product->setTypeId(Product\Type::TYPE_SIMPLE);
}
if ($this->storeManager->hasSingleStore()) {
$product->setWebsiteIds([$this->storeManager->getStore(true)->getWebsiteId()]);
}
} else {
if (!empty($productData['id'])) {
unset($this->instancesById[$productData['id']]);
Expand All @@ -341,31 +339,20 @@ protected function initializeProductData(array $productData, $createNew)
foreach ($productData as $key => $value) {
$product->setData($key, $value);
}
$this->assignProductToWebsites($product, $createNew);

return $product;
}

/**
* @param \Magento\Catalog\Model\Product $product
* @param bool $createNew
* @return void
*/
private function assignProductToWebsites(\Magento\Catalog\Model\Product $product, $createNew)
private function assignProductToWebsites(\Magento\Catalog\Model\Product $product)
{
$websiteIds = $product->getWebsiteIds();

if (!$this->storeManager->hasSingleStore()) {
$websiteIds = array_unique(
array_merge(
$websiteIds,
[$this->storeManager->getStore()->getWebsiteId()]
)
);
}

if ($createNew && $this->storeManager->getStore(true)->getCode() == \Magento\Store\Model\Store::ADMIN_CODE) {
if ($this->storeManager->getStore(true)->getCode() === \Magento\Store\Model\Store::ADMIN_CODE) {
$websiteIds = array_keys($this->storeManager->getWebsites());
} else {
$websiteIds = [$this->storeManager->getStore()->getWebsiteId()];
}

$product->setWebsiteIds($websiteIds);
Expand Down
Loading

0 comments on commit 38dfc6e

Please sign in to comment.