123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- <?php
- /**
- * Copyright © 2017 Ihor Vansach (ihor@magefan.com). All rights reserved.
- * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
- *
- * Glory to Ukraine! Glory to the heroes!
- */
- namespace Magefan\Blog\Model;
- use Magefan\Blog\Model\Url;
- /**
- * Category model
- *
- * @method \Magefan\Blog\Model\ResourceModel\Category _getResource()
- * @method \Magefan\Blog\Model\ResourceModel\Category getResource()
- * @method int getStoreId()
- * @method $this setStoreId(int $value)
- * @method string getTitle()
- * @method $this setTitle(string $value)
- * @method string getMetaKeywords()
- * @method $this setMetaKeywords(string $value)
- * @method string getMetaDescription()
- * @method $this setMetaDescription(string $value)
- * @method string getIdentifier()
- * @method $this setIdentifier(string $value)
- */
- class Category extends \Magento\Framework\Model\AbstractModel
- {
- /**
- * Category's Statuses
- */
- const STATUS_ENABLED = 1;
- const STATUS_DISABLED = 0;
- /**
- * Prefix of model events names
- *
- * @var string
- */
- protected $_eventPrefix = 'magefan_blog_category';
- /**
- * Parameter name in event
- *
- * In observe method you can use $observer->getEvent()->getObject() in this case
- *
- * @var string
- */
- protected $_eventObject = 'blog_category';
- /**
- * @var \Magento\Framework\UrlInterface
- */
- protected $_url;
- /**
- * @var \Magefan\Blog\Model\ResourceModel\Post\CollectionFactory
- */
- protected $postCollectionFactory;
- /**
- * @var array
- */
- static private $loadedCategoriesRepository = [];
- /**
- * @var string
- */
- protected $controllerName;
- /**
- * Initialize dependencies.
- *
- * @param \Magento\Framework\Model\Context $context
- * @param \Magento\Framework\Registry $registry
- * @param \Magefan\Blog\Model\Url $url
- * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
- * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
- * @param array $data
- */
- public function __construct(
- \Magento\Framework\Model\Context $context,
- \Magento\Framework\Registry $registry,
- Url $url,
- \Magefan\Blog\Model\ResourceModel\Post\CollectionFactory $postCollectionFactory,
- \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
- \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
- array $data = []
- ) {
- $this->_url = $url;
- $this->postCollectionFactory = $postCollectionFactory;
- parent::__construct($context, $registry, $resource, $resourceCollection, $data);
- }
- /**
- * Initialize resource model
- *
- * @return void
- */
- protected function _construct()
- {
- $this->_init('Magefan\Blog\Model\ResourceModel\Category');
- $this->controllerName = URL::CONTROLLER_CATEGORY;
- }
- /**
- * Load object data
- *
- * @param integer $modelId
- * @param null|string $field
- * @return $this
- * @deprecated
- */
- public function load($modelId, $field = null)
- {
- $object = parent::load($modelId, $field);
- if (!isset(self::$loadedCategoriesRepository[$object->getId()])) {
- self::$loadedCategoriesRepository[$object->getId()] = $object;
- }
- return $object;
- }
- private function loadFromRepository($categoryId)
- {
- if (!isset(self::$loadedCategoriesRepository[$categoryId])) {
- $category = clone $this;
- $category->load($categoryId);
- }
- return self::$loadedCategoriesRepository[$categoryId];
- }
- /**
- * Retrieve controller name
- * @return string
- */
- public function getControllerName()
- {
- return $this->controllerName;
- }
- /**
- * Retrieve model title
- * @param boolean $plural
- * @return string
- */
- public function getOwnTitle($plural = false)
- {
- return $plural ? 'Categories' : 'Category';
- }
- /**
- * Retrieve true if category is active
- * @return boolean [description]
- */
- public function isActive()
- {
- return ($this->getStatus() == self::STATUS_ENABLED);
- }
- /**
- * Retrieve available category statuses
- * @return array
- */
- public function getAvailableStatuses()
- {
- return [self::STATUS_DISABLED => __('Disabled'), self::STATUS_ENABLED => __('Enabled')];
- }
- /**
- * Check if category identifier exist for specific store
- * return category id if category exists
- *
- * @param string $identifier
- * @param int $storeId
- * @return int
- */
- public function checkIdentifier($identifier, $storeId)
- {
- return $this->_getResource()->checkIdentifier($identifier, $storeId);
- }
- /**
- * Retrieve parent category ids
- * @return array
- */
- public function getParentIds()
- {
- $k = 'parent_ids';
- if (!$this->hasData($k)) {
- $this->setData($k,
- $this->getPath() ? explode('/', $this->getPath()) : []
- );
- }
- return $this->getData($k);
- }
- /**
- * Retrieve parent category id
- * @return array
- */
- public function getParentId()
- {
- $parentIds = $this->getParentIds();
- if ($parentIds) {
- return $parentIds[count($parentIds) - 1];
- }
- return 0;
- }
- /**
- * Retrieve parent category
- * @return self || false
- */
- public function getParentCategory()
- {
- $k = 'parent_category';
- if (null === $this->getData($k)) {
- $this->setData($k, false);
- if ($pId = $this->getParentId()) {
- $category = $this->loadFromRepository($pId);
- if ($category->getId()) {
- if ($category->isVisibleOnStore($this->getStoreId())) {
- $this->setData($k, $category);
- }
- }
- }
- }
- return $this->getData($k);
- }
- /**
- * Check if current category is parent category
- * @param self $category
- * @return boolean
- */
- public function isParent($category)
- {
- if (is_object($category)) {
- $category = $category->getId();
- }
- return in_array($category, $this->getParentIds());
- }
- /**
- * Retrieve children category ids
- * @return array
- */
- public function getChildrenIds()
- {
- $k = 'children_ids';
- if (!$this->hasData($k)) {
- $categories = \Magento\Framework\App\ObjectManager::getInstance()
- ->create($this->_collectionName);
- $ids = [];
- foreach($categories as $category) {
- if ($category->isParent($this)) {
- $ids[] = $category->getId();
- }
- }
- $this->setData($k,
- $ids
- );
- }
- return $this->getData($k);
- }
- /**
- * Check if current category is child category
- * @param self $category
- * @return boolean
- */
- public function isChild($category)
- {
- return $category->isParent($this);
- }
- /**
- * Retrieve category depth level
- * @return int
- */
- public function getLevel()
- {
- return count($this->getParentIds());
- }
- /**
- * Retrieve catgegory url route path
- * @return string
- */
- public function getUrl()
- {
- return $this->_url->getUrlPath($this->getIdentifier(), $this->controllerName);
- }
- /**
- * Retrieve category url
- * @return string
- */
- public function getCategoryUrl()
- {
- return $this->_url->getUrl($this, $this->controllerName);
- }
- /**
- * Retrieve catgegory canonical url
- * @return string
- */
- public function getCanonicalUrl()
- {
- return $this->_url->getCanonicalUrl($this);
- }
- /**
- * Retrieve meta title
- * @return string
- */
- public function getMetaTitle()
- {
- $title = $this->getData('meta_title');
- if (!$title) {
- $title = $this->getData('title');
- }
- return trim($title);
- }
- /**
- * Retrieve meta description
- * @return string
- */
- public function getMetaDescription()
- {
- $desc = $this->getData('meta_description');
- if (!$desc) {
- $desc = $this->getData('content');
- }
- $desc = strip_tags($desc);
- if (mb_strlen($desc) > 160) {
- $desc = mb_substr($desc, 0, 160);
- }
- return trim($desc);
- }
- /**
- * Retrieve if is visible on store
- * @return bool
- */
- public function isVisibleOnStore($storeId)
- {
- return $this->getIsActive() && array_intersect([0, $storeId], $this->getStoreIds());
- }
- /**
- * Retrieve number of posts in this category
- *
- * @return int
- */
- public function getPostsCount()
- {
- $key = 'posts_count';
- if (!$this->hasData($key)) {
- $posts = $this->postCollectionFactory->create()
- ->addActiveFilter()
- ->addStoreFilter($this->getStoreId())
- ->addCategoryFilter($this);
- $this->setData($key, (int)$posts->getSize());
- }
- return $this->getData($key);
- }
- /**
- * Prepare all additional data
- * @param string $format
- * @return self
- */
- public function initDinamicData()
- {
- $keys = [
- 'meta_description',
- 'meta_title',
- ];
- foreach ($keys as $key) {
- $method = 'get' . str_replace('_', '',
- ucwords($key, '_')
- );
- $this->$method();
- }
- return $this;
- }
- /**
- * Duplicate category and return new object
- * @return self
- */
- public function duplicate()
- {
- $object = clone $this;
- $object
- ->unsetData('category_id')
- ->setTitle($object->getTitle() . ' (' . __('Duplicated') . ')')
- ->setData('is_active', 0);
- return $object->save();
- }
- }
|