Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CategoryManagement.php
Go to the documentation of this file.
1 <?php
8 namespace Magento\Catalog\Model;
9 
10 use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
11 
13 {
18 
22  protected $categoryTree;
23 
27  private $scopeResolver;
28 
32  private $categoriesFactory;
33 
39  public function __construct(
41  \Magento\Catalog\Model\Category\Tree $categoryTree,
42  \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoriesFactory
43  ) {
44  $this->categoryRepository = $categoryRepository;
45  $this->categoryTree = $categoryTree;
46  $this->categoriesFactory = $categoriesFactory;
47  }
48 
52  public function getTree($rootCategoryId = null, $depth = null)
53  {
54  $category = null;
55  if ($rootCategoryId !== null) {
57  $category = $this->categoryRepository->get($rootCategoryId);
58  } elseif ($this->isAdminStore()) {
59  $category = $this->getTopLevelCategory();
60  }
61  $result = $this->categoryTree->getTree($this->categoryTree->getRootNode($category), $depth);
62  return $result;
63  }
64 
70  private function isAdminStore()
71  {
72  return $this->getScopeResolver()->getScope()->getCode() == \Magento\Store\Model\Store::ADMIN_CODE;
73  }
74 
80  private function getScopeResolver()
81  {
82  if ($this->scopeResolver == null) {
84  ->get(\Magento\Framework\App\ScopeResolverInterface::class);
85  }
86 
87  return $this->scopeResolver;
88  }
89 
95  private function getTopLevelCategory()
96  {
97  $categoriesCollection = $this->categoriesFactory->create();
98  return $categoriesCollection->addFilter('level', ['eq' => 0])->getFirstItem();
99  }
100 
104  public function move($categoryId, $parentId, $afterId = null)
105  {
106  $model = $this->categoryRepository->get($categoryId);
107  $parentCategory = $this->categoryRepository->get($parentId);
108 
109  if ($parentCategory->hasChildren()) {
110  $parentChildren = $parentCategory->getChildren();
111  $categoryIds = explode(',', $parentChildren);
112  $lastId = array_pop($categoryIds);
113  $afterId = ($afterId === null || $afterId > $lastId) ? $lastId : $afterId;
114  }
115 
116  if (strpos($parentCategory->getPath(), $model->getPath()) === 0) {
117  throw new \Magento\Framework\Exception\LocalizedException(
118  __('Operation do not allow to move a parent category to any of children category')
119  );
120  }
121  try {
122  $model->move($parentId, $afterId);
123  } catch (\Exception $e) {
124  throw new \Magento\Framework\Exception\LocalizedException(__('Could not move category'));
125  }
126  return true;
127  }
128 
132  public function getCount()
133  {
134  $categories = $this->categoriesFactory->create();
136  $categories->addAttributeToFilter('parent_id', ['gt' => 0]);
137  return $categories->getSize();
138  }
139 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository, \Magento\Catalog\Model\Category\Tree $categoryTree, \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoriesFactory)
getTree($rootCategoryId=null, $depth=null)
__()
Definition: __.php:13
move($categoryId, $parentId, $afterId=null)
$categories