Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Category.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
11 
15 abstract class Category extends \Magento\Backend\App\Action
16 {
22  const ADMIN_RESOURCE = 'Magento_Catalog::categories';
23 
27  protected $dateFilter;
28 
33  public function __construct(
34  \Magento\Backend\App\Action\Context $context,
35  \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter = null
36  ) {
37  $this->dateFilter = $dateFilter;
38  parent::__construct($context);
39  }
40 
48  protected function _initCategory($getRootInstead = false)
49  {
50  $categoryId = $this->resolveCategoryId();
51  $storeId = $this->resolveStoreId();
52  $category = $this->_objectManager->create(\Magento\Catalog\Model\Category::class);
53  $category->setStoreId($storeId);
54 
55  if ($categoryId) {
56  $category->load($categoryId);
57  if ($storeId) {
58  $rootId = $this->_objectManager->get(
59  \Magento\Store\Model\StoreManagerInterface::class
60  )->getStore(
61  $storeId
62  )->getRootCategoryId();
63  if (!in_array($rootId, $category->getPathIds())) {
64  // load root category instead wrong one
65  if ($getRootInstead) {
66  $category->load($rootId);
67  } else {
68  return false;
69  }
70  }
71  }
72  }
73 
74  $this->_objectManager->get(\Magento\Framework\Registry::class)->register('category', $category);
75  $this->_objectManager->get(\Magento\Framework\Registry::class)->register('current_category', $category);
76  $this->_objectManager->get(\Magento\Cms\Model\Wysiwyg\Config::class)
77  ->setStoreId($storeId);
78  return $category;
79  }
80 
86  private function resolveCategoryId() : int
87  {
88  $categoryId = (int)$this->getRequest()->getParam('id', false);
89 
90  return $categoryId ?: (int)$this->getRequest()->getParam('entity_id', false);
91  }
92 
101  private function resolveStoreId() : int
102  {
103  $storeId = (int)$this->getRequest()->getParam('store', false);
104 
105  return $storeId ?: (int)$this->getRequest()->getParam('store_id', Store::DEFAULT_STORE_ID);
106  }
107 
118  protected function ajaxRequestResponse($category, $resultPage)
119  {
120  // prepare breadcrumbs of selected category, if any
121  $breadcrumbsPath = $category->getPath();
122  if (empty($breadcrumbsPath)) {
123  // but if no category, and it is deleted - prepare breadcrumbs from path, saved in session
124  $breadcrumbsPath = $this->_objectManager->get(
125  \Magento\Backend\Model\Auth\Session::class
126  )->getDeletedPath(
127  true
128  );
129  if (!empty($breadcrumbsPath)) {
130  $breadcrumbsPath = explode('/', $breadcrumbsPath);
131  // no need to get parent breadcrumbs if deleting category level 1
132  if (count($breadcrumbsPath) <= 1) {
133  $breadcrumbsPath = '';
134  } else {
135  array_pop($breadcrumbsPath);
136  $breadcrumbsPath = implode('/', $breadcrumbsPath);
137  }
138  }
139  }
140 
141  $eventResponse = new \Magento\Framework\DataObject([
142  'content' => $resultPage->getLayout()->getUiComponent('category_form')->getFormHtml()
143  . $resultPage->getLayout()->getBlock('category.tree')
144  ->getBreadcrumbsJavascript($breadcrumbsPath, 'editingCategoryBreadcrumbs'),
145  'messages' => $resultPage->getLayout()->getMessagesBlock()->getGroupedHtml(),
146  'toolbar' => $resultPage->getLayout()->getBlock('page.actions.toolbar')->toHtml()
147  ]);
148  $this->_eventManager->dispatch(
149  'category_prepare_ajax_response',
150  ['response' => $eventResponse, 'controller' => $this]
151  );
153  $resultJson = $this->_objectManager->get(\Magento\Framework\Controller\Result\Json::class);
154  $resultJson->setHeader('Content-type', 'application/json', true);
155  $resultJson->setData($eventResponse->getData());
156  return $resultJson;
157  }
158 
168  {
169  $dateFieldFilters = [];
170  $attributes = $category->getAttributes();
171  foreach ($attributes as $attrKey => $attribute) {
172  if ($attribute->getBackend()->getType() == 'datetime') {
173  if (array_key_exists($attrKey, $postData) && $postData[$attrKey] != '') {
174  $dateFieldFilters[$attrKey] = $this->dateFilter;
175  }
176  }
177  }
178  $inputFilter = new \Zend_Filter_Input($dateFieldFilters, [], $postData);
179  return $inputFilter->getUnescaped();
180  }
181 }
__construct(\Magento\Backend\App\Action\Context $context, \Magento\Framework\Stdlib\DateTime\Filter\Date $dateFilter=null)
Definition: Category.php:33
$attributes
Definition: matrix.phtml:13