tagCollectionFactory = $tagCollectionFactory; $this->topicCollectionFactory = $topicCollectionFactory; $this->categoryCollectionFactory = $categoryCollectionFactory; $this->postCollectionFactory = $postCollectionFactory; $this->productCollectionFactory = $productCollectionFactory; $this->helperData = $helperData; $this->dateTime = $dateTime; $this->trafficFactory = $trafficFactory; parent::__construct($context, $registry, $resource, $resourceCollection, $data); } /** * Initialize resource model * * @return void */ protected function _construct() { $this->_init(PostResource::class); } /** * @inheritdoc */ public function afterSave() { if ($this->isObjectNew()) { $trafficModel = $this->trafficFactory->create() ->load($this->getId(), 'post_id'); if (!$trafficModel->getId()) { $trafficModel->setData([ 'post_id' => $this->getId(), 'numbers_view' => 0 ])->save(); } } return parent::afterSave(); } /** * @param bool $shorten * * @return mixed|string */ public function getShortDescription($shorten = false) { $shortDescription = $this->getData('short_description'); $maxLength = 20; $descArr = explode(' ', $shortDescription ?? ''); if ($shorten && count($descArr) > $maxLength) { $descArr = array_slice($descArr, 0, $maxLength); $shortDescription = implode(' ', $descArr) . '...'; } return $shortDescription; } /** * @param null $store * * @return string */ public function getUrl($store = null) { return $this->helperData->getBlogUrl($this, Data::TYPE_POST, $store); } /** * Get identities * * @return array */ public function getIdentities() { return [self::CACHE_TAG . '_' . $this->getId()]; } /** * get entity default values * * @return array */ public function getDefaultValues() { $values = []; $values['in_rss'] = '1'; $values['enabled'] = '1'; $values['allow_comment'] = '1'; $values['store_ids'] = '1'; return $values; } /** * @return ResourceModel\Tag\Collection */ public function getSelectedTagsCollection() { if ($this->tagCollection === null) { $collection = $this->tagCollectionFactory->create(); $collection->getSelect()->join( $this->getResource()->getTable('mageplaza_blog_post_tag'), 'main_table.tag_id=' . $this->getResource()->getTable('mageplaza_blog_post_tag') . '.tag_id AND ' . $this->getResource()->getTable('mageplaza_blog_post_tag') . '.post_id=' . $this->getId(), ['position'] )->where("main_table.enabled='1'"); $this->tagCollection = $collection; } return $this->tagCollection; } /** * @return ResourceModel\Topic\Collection */ public function getSelectedTopicsCollection() { if ($this->topicCollection === null) { $collection = $this->topicCollectionFactory->create(); $collection->join( $this->getResource()->getTable('mageplaza_blog_post_topic'), 'main_table.topic_id=' . $this->getResource()->getTable('mageplaza_blog_post_topic') . '.topic_id AND ' . $this->getResource()->getTable('mageplaza_blog_post_topic') . '.post_id=' . $this->getId(), ['position'] ); $this->topicCollection = $collection; } return $this->topicCollection; } /** * @return ResourceModel\Category\Collection */ public function getSelectedCategoriesCollection() { if ($this->categoryCollection === null) { $collection = $this->categoryCollectionFactory->create(); $collection->join( $this->getResource()->getTable('mageplaza_blog_post_category'), 'main_table.category_id=' . $this->getResource()->getTable('mageplaza_blog_post_category') . '.category_id AND ' . $this->getResource()->getTable('mageplaza_blog_post_category') . '.post_id="' . $this->getId() . '"', ['position'] ); $this->categoryCollection = $collection; } return $this->categoryCollection; } /** * @return array * @throws LocalizedException */ public function getCategoryIds() { if (!$this->hasData('category_ids')) { $ids = $this->_getResource()->getCategoryIds($this); $this->setData('category_ids', $ids); } return (array) $this->_getData('category_ids'); } /** * @return array * @throws LocalizedException */ public function getTagIds() { if (!$this->hasData('tag_ids')) { $ids = $this->_getResource()->getTagIds($this); $this->setData('tag_ids', $ids); } return (array) $this->_getData('tag_ids'); } /** * @return array * @throws LocalizedException */ public function getTopicIds() { if (!$this->hasData('topic_ids')) { $ids = $this->_getResource()->getTopicIds($this); $this->setData('topic_ids', $ids); } return (array) $this->_getData('topic_ids'); } /** * @return int * @throws LocalizedException */ public function getViewTraffic() { if (!$this->hasData('view_traffic')) { $traffic = $this->_getResource()->getViewTraffic($this); $this->setData('view_traffic', $traffic[0]); } return $this->_getData('view_traffic'); } /** * @return int * @throws LocalizedException */ public function getAuthorName() { if (!$this->hasData('author_name')) { $author = $this->_getResource()->getAuthor($this); $this->setData('author_name', $author['name']); } return $this->_getData('author_name'); } /** * @return int * @throws LocalizedException */ public function getAuthorUrl() { if (!$this->hasData('author_url')) { $author = $this->_getResource()->getAuthor($this); $this->setData('author_url', $this->helperData->getBlogUrl($author['url_key'], Data::TYPE_AUTHOR)); } return $this->_getData('author_url'); } /** * @return int * @throws LocalizedException */ public function getAuthorUrlKey() { if (!$this->hasData('author_url_key')) { $author = $this->_getResource()->getAuthor($this); $this->setData('author_url_key', $author['url_key']); } return $this->_getData('author_url_key'); } /** * @return mixed * @throws NoSuchEntityException */ public function getUrlImage() { $imageHelper = $this->helperData->getImageHelper(); $imageFile = $this->getImage() ? $imageHelper->getMediaPath($this->getImage(), 'post') : ''; $imageUrl = $imageFile ? $this->helperData->getImageHelper()->getMediaUrl($imageFile) : ''; $this->setData('image', $imageUrl); return $this->_getData('image'); } /** * @throws Exception */ public function updateViewTraffic() { if ($this->getId()) { $trafficModel = $this->trafficFactory->create()->load($this->getId(), 'post_id'); if ($trafficModel->getId()) { $trafficModel->setNumbersView($trafficModel->getNumbersView() + 1); $trafficModel->save(); } else { $traffic = $this->trafficFactory->create(); $traffic->addData(['post_id' => $this->getId(), 'numbers_view' => 1])->save(); } } } /** * @return Collection|null * @throws LocalizedException * @throws NoSuchEntityException */ public function getRelatedPostsCollection() { $topicIds = $this->_getResource()->getTopicIds($this); if (count($topicIds)) { $collection = $this->postCollectionFactory->create(); $collection->getSelect() ->join( ['topic' => $this->getResource()->getTable('mageplaza_blog_post_topic')], 'main_table.post_id=topic.post_id AND topic.post_id != "' . $this->getId() . '" AND topic.topic_id IN (' . implode(',', $topicIds) . ')', ['position'] )->group('main_table.post_id')->order('topic.position'); if ($limit = (int) $this->helperData->getPostViewPageConfig('related_post')) { $collection->getSelect() ->limit($limit); } $collection->addFieldToFilter('enabled', '1'); $this->helperData->addStoreFilter($collection); return $collection; } return null; } /** * @return ProductCollection */ public function getSelectedProductsCollection() { if ($this->productCollection === null) { $collection = $this->productCollectionFactory->create(); $collection->getSelect()->join( $this->getResource()->getTable('mageplaza_blog_post_product'), 'e.entity_id=' . $this->getResource()->getTable('mageplaza_blog_post_product') . '.entity_id AND ' . $this->getResource()->getTable('mageplaza_blog_post_product') . '.post_id=' . $this->getId(), ['position'] ); $this->productCollection = $collection; } return $this->productCollection; } /** * @return array|mixed */ public function getProductsPosition() { if (!$this->getId()) { return []; } $array = $this->getData('products_position'); if ($array === null) { $array = $this->getResource()->getProductsPosition($this); $this->setData('products_position', $array); } return $array; } /** * get previous post * @return Collection */ public function getPrevPost() { if ($this->prevPostCollection === null) { $collection = $this->postCollectionFactory->create(); $collection->addFieldToFilter('post_id', ['lt' => $this->getId()]) ->setOrder('post_id', 'DESC')->setPageSize(1)->setCurPage(1); $this->prevPostCollection = $collection; } return $this->prevPostCollection; } /** * get next post * @return Collection */ public function getNextPost() { if ($this->nextPostCollection === null) { $collection = $this->postCollectionFactory->create(); $collection->addFieldToFilter('post_id', ['gt' => $this->getId()]) ->setOrder('post_id', 'ASC')->setPageSize(1)->setCurPage(1); $this->nextPostCollection = $collection; } return $this->nextPostCollection; } }