categoryFactory = $categoryFactory; $this->postCollectionFactory = $postCollectionFactory; $this->categoryCollectionFactory = $categoryCollectionFactory; parent::__construct($context, $registry, $resource, $resourceCollection, $data); } /** * Initialize resource model * * @return void */ protected function _construct() { $this->_init(ResourceModel\Category::class); } /** * Get identities * * @return array */ public function getIdentities() { return [self::CACHE_TAG . '_' . $this->getId()]; } /** * get entity default values * * @return array */ public function getDefaultValues() { $values = []; $values['store_ids'] = '1'; $values['enabled'] = '1'; return $values; } /** * get tree path ids * * @return array */ public function getPathIds() { $ids = $this->getData('path_ids'); if ($ids === null) { $ids = explode('/', (string) $this->getPath()); $this->setData('path_ids', $ids); } return $ids; } /** * get all parent ids * * @return array */ public function getParentIds() { return array_diff($this->getPathIds(), [$this->getId()]); } /** * move Blog Category in tree * * @param $parentId * @param $afterCategoryId * * @return $this * @throws LocalizedException * @throws Exception */ public function move($parentId, $afterCategoryId) { try { $parent = $this->categoryFactory->create()->load($parentId); } catch (NoSuchEntityException $e) { throw new LocalizedException( __('Sorry, but we can\'t move the Blog Category because we can\'t find the new parent Blog Category you selected.'), $e ); } if (!$this->getId()) { throw new LocalizedException( __('Sorry, but we can\'t move the Blog Category because we can\'t find the new parent Blog Category you selected.') ); } if ($parent->getId() == $this->getId()) { throw new LocalizedException( __('We can\'t perform this Blog Category move operation because the parent Blog Category matches the child Blog Category.') ); } $this->setMovedCategoryId($this->getId()); $oldParentId = $this->getParentId(); $eventParams = [ $this->_eventObject => $this, 'parent' => $parent, 'category_id' => $this->getId(), 'prev_parent_id' => $oldParentId, 'parent_id' => $parentId, ]; $this->_getResource()->beginTransaction(); try { $this->_eventManager->dispatch($this->_eventPrefix . '_move_before', $eventParams); $this->getResource()->changeParent($this, $parent, $afterCategoryId); $this->_eventManager->dispatch($this->_eventPrefix . '_move_after', $eventParams); $this->_getResource()->commit(); // Set data for indexer $this->setAffectedCategoryIds([$this->getId(), $oldParentId, $parentId]); } catch (Exception $e) { $this->_getResource()->rollBack(); throw $e; } $this->_eventManager->dispatch($this->_eventPrefix . '_move', $eventParams); $this->_cacheManager->clean([self::CACHE_TAG]); return $this; } /** * @return array|mixed */ public function getPostsPosition() { if (!$this->getId()) { return []; } $array = $this->getData('posts_position'); if ($array === null) { $array = $this->getResource()->getPostsPosition($this); $this->setData('posts_position', $array); } return $array; } /** * @return Collection */ public function getSelectedPostsCollection() { if (!$this->postCollection) { $collection = $this->postCollectionFactory->create(); $collection->join( ['cat' => $this->getResource()->getTable('mageplaza_blog_post_category')], 'main_table.post_id=cat.post_id AND cat.category_id=' . (int) $this->getId(), ['position'] ); $collection->setOrder('post_id','DESC'); $this->postCollection = $collection; } return $this->postCollection; } }