123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803 |
- <?php
- /**
- * Copyright © 2015-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;
- /**
- * Post model
- *
- * @method \Magefan\Blog\Model\ResourceModel\Post _getResource()
- * @method \Magefan\Blog\Model\ResourceModel\Post 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)
- * @method string getContent()
- * @method string getShortContent()
- * @method $this setContent(string $value)
- * @method string getContentHeading()
- * @method $this setContentHeading(string $value)
- */
- class Post extends \Magento\Framework\Model\AbstractModel
- {
- /**
- * Posts's Statuses
- */
- const STATUS_ENABLED = 1;
- const STATUS_DISABLED = 0;
- /**
- * Gallery images separator
- */
- const GALLERY_IMAGES_SEPARATOR = ';';
- /**
- * Base media folder path
- */
- const BASE_MEDIA_PATH = 'magefan_blog';
- /**
- * Prefix of model events names
- *
- * @var string
- */
- protected $_eventPrefix = 'magefan_blog_post';
- /**
- * Parameter name in event
- *
- * In observe method you can use $observer->getEvent()->getObject() in this case
- *
- * @var string
- */
- protected $_eventObject = 'blog_post';
- /**
- * @var \Magento\Framework\Math\Random
- */
- protected $random;
- /**
- * @var \Magento\Cms\Model\Template\FilterProvider
- */
- protected $filterProvider;
- /**
- * @var \Magento\Framework\App\Config\ScopeConfigInterface
- */
- protected $scopeConfig;
- /**
- * @var \Magefan\Blog\Model\Url
- */
- protected $_url;
- /**
- * @var \Magefan\Blog\Model\AuthorFactory
- */
- protected $_authorFactory;
- /**
- * @var \Magefan\Blog\Model\ResourceModel\Category\CollectionFactory
- */
- protected $_categoryCollectionFactory;
- /**
- * @var \Magefan\Blog\Model\ResourceModel\Tag\CollectionFactory
- */
- protected $_tagCollectionFactory;
- /**
- * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
- */
- protected $_productCollectionFactory;
- /**
- * @var \Magefan\Blog\Model\ResourceModel\Category\Collection
- */
- protected $_parentCategories;
- /**
- * @var \Magefan\Blog\Model\ResourceModel\Tag\Collection
- */
- protected $_relatedTags;
- /**
- * @var \Magefan\Blog\Model\ResourceModel\Post\Collection
- */
- protected $_relatedPostsCollection;
- /**
- * @var \Magefan\Blog\Model\ImageFactory
- */
- protected $imageFactory;
- /**
- * @var string
- */
- protected $controllerName;
- /**
- * Initialize dependencies.
- *
- * @param \Magento\Framework\Model\Context $context
- * @param \Magento\Framework\Registry $registry
- * @param \Magento\Framework\Math\Random $random
- * @param \Magento\Cms\Model\Template\FilterProvider $filterProvider
- * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
- * @param \Magefan\Blog\Model\Url $url
- * @param \Magefan\Blog\Model\AuthorFactory $authorFactory
- * @param \Magefan\Blog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory
- * @param \Magefan\Blog\Model\ResourceModel\Tag\CollectionFactory $tagCollectionFactory
- * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
- * @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,
- \Magento\Framework\Math\Random $random,
- \Magento\Cms\Model\Template\FilterProvider $filterProvider,
- \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
- Url $url,
- \Magefan\Blog\Model\ImageFactory $imageFactory,
- \Magefan\Blog\Model\AuthorFactory $authorFactory,
- \Magefan\Blog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
- \Magefan\Blog\Model\ResourceModel\Tag\CollectionFactory $tagCollectionFactory,
- \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
- \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
- \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
- array $data = []
- ) {
- parent::__construct($context, $registry, $resource, $resourceCollection, $data);
- $this->filterProvider = $filterProvider;
- $this->random = $random;
- $this->scopeConfig = $scopeConfig;
- $this->_url = $url;
- $this->imageFactory = $imageFactory;
- $this->_authorFactory = $authorFactory;
- $this->_categoryCollectionFactory = $categoryCollectionFactory;
- $this->_tagCollectionFactory = $tagCollectionFactory;
- $this->_productCollectionFactory = $productCollectionFactory;
- $this->_relatedPostsCollection = clone($this->getCollection());
- }
- /**
- * Initialize resource model
- *
- * @return void
- */
- protected function _construct()
- {
- $this->_init('Magefan\Blog\Model\ResourceModel\Post');
- $this->controllerName = URL::CONTROLLER_POST;
- }
- /**
- * 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 ? 'Posts' : 'Post';
- }
- /**
- * Retrieve true if post is active
- * @return boolean [description]
- */
- public function isActive()
- {
- return ($this->getStatus() == self::STATUS_ENABLED);
- }
- /**
- * Retrieve available post statuses
- * @return array
- */
- public function getAvailableStatuses()
- {
- return [self::STATUS_DISABLED => __('Disabled'), self::STATUS_ENABLED => __('Enabled')];
- }
- /**
- * Check if post identifier exist for specific store
- * return post id if post exists
- *
- * @param string $identifier
- * @param int $storeId
- * @return int
- */
- public function checkIdentifier($identifier, $storeId)
- {
- return $this->_getResource()->checkIdentifier($identifier, $storeId);
- }
- /**
- * Retrieve post url path
- * @return string
- */
- public function getUrl()
- {
- return $this->_url->getUrlPath($this->getIdentifier(), $this->controllerName);
- }
- /**
- * Retrieve post url
- * @return string
- */
- public function getPostUrl()
- {
- if (!$this->hasData('post_url')) {
- $url = $this->_url->getUrl($this, $this->controllerName);
- $this->setData('post_url', $url);
- }
- return $this->getData('post_url');
- }
- /**
- * Retrieve post canonical url
- * @return string
- */
- public function getCanonicalUrl()
- {
- return $this->_url->getCanonicalUrl($this);
- }
- /**
- * Retrieve featured image url
- * @return string
- */
- public function getFeaturedImage()
- {
- if (!$this->hasData('featured_image')) {
- if ($file = $this->getData('featured_img')) {
- $image = $this->_url->getMediaUrl($file);
- } else {
- $image = false;
- }
- $this->setData('featured_image', $image);
- }
- return $this->getData('featured_image');
- }
- /**
- * Set media gallery images url
- *
- * @param array $images
- * @return this
- */
- public function setGalleryImages(array $images)
- {
- $this->setData('media_gallery',
- implode(
- self::GALLERY_IMAGES_SEPARATOR,
- $images
- )
- );
- /* Reinit Media Gallery Images */
- $this->unsetData('gallery_images');
- $this->getGalleryImages();
- return $this;
- }
- /**
- * Retrieve media gallery images url
- * @return string
- */
- public function getGalleryImages()
- {
- if (!$this->hasData('gallery_images')) {
- $images = array();
- $gallery = explode(
- self::GALLERY_IMAGES_SEPARATOR,
- $this->getData('media_gallery')
- );
- if (!empty($gallery)) {
- foreach ($gallery as $file) {
- if ($file) {
- $images[] = $this->imageFactory->create()
- ->setFile($file);
- }
- }
- }
- $this->setData('gallery_images', $images);
- }
- return $this->getData('gallery_images');
- }
- /**
- * Retrieve first image url
- * @return string
- */
- public function getFirstImage()
- {
- if (!$this->hasData('first_image')) {
- $image = $this->getFeaturedImage();
- if (!$image) {
- $content = $this->getFilteredContent();
- $match = null;
- preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $content, $match);
- if (!empty($match['src'])) {
- $image = $match['src'];
- }
- }
- $this->setData('first_image', $image);
- }
- return $this->getData('first_image');
- }
- /**
- * Retrieve filtered content
- *
- * @return string
- */
- public function getFilteredContent()
- {
- $key = 'filtered_content';
- if (!$this->hasData($key)) {
- $content = $this->filterProvider->getPageFilter()->filter(
- $this->getContent()
- );
- $this->setData($key, $content);
- }
- return $this->getData($key);
- }
- /**
- * Retrieve short filtered content
- *
- * @return string
- */
- public function getShortFilteredContent()
- {
- $key = 'short_filtered_content';
- if (!$this->hasData($key)) {
- if ($this->getShortContent()) {
- $content = $this->filterProvider->getPageFilter()->filter(
- $this->getShortContent()
- );
- } else {
- $content = $this->getFilteredContent();
- $pageBraker = '<!-- pagebreak -->';
- $p = mb_strpos($content, $pageBraker);
- if (!$p) {
- $p = (int)$this->scopeConfig->getValue(
- 'mfblog/post_list/shortcotent_length',
- \Magento\Store\Model\ScopeInterface::SCOPE_STORE
- );
- }
- if ($p) {
- $content = mb_substr($content, 0, $p);
- try {
- libxml_use_internal_errors(true);
- $dom = new \DOMDocument();
- $dom->loadHTML('<?xml encoding="UTF-8">' . $content);
- $body = $dom->getElementsByTagName('body');
- if ($body && $body->length > 0) {
- $body = $body->item(0);
- $_content = new \DOMDocument;
- foreach ($body->childNodes as $child) {
- $_content->appendChild($_content->importNode($child, true));
- }
- $content = $_content->saveHTML();
- }
- } catch (\Exception $e) {
- }
- }
- }
- $this->setData($key, $content);
- }
- return $this->getData($key);;
- }
- /**
- * 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 og title
- * @return string
- */
- public function getOgTitle()
- {
- $title = $this->getData('og_title');
- if (!$title) {
- $title = $this->getMetaTitle();
- }
- return trim($title);
- }
- /**
- * Retrieve og description
- * @return string
- */
- public function getOgDescription()
- {
- $desc = $this->getData('og_description');
- if (!$desc) {
- $desc = $this->getMetaDescription();
- } else {
- $desc = strip_tags($desc);
- if (mb_strlen($desc) > 160) {
- $desc = mb_substr($desc, 0, 160);
- }
- }
- return trim($desc);
- }
- /**
- * Retrieve og type
- * @return string
- */
- public function getOgType()
- {
- $type = $this->getData('og_type');
- if (!$type) {
- $type = 'article';
- }
- return trim($type);
- }
- /**
- * Retrieve og image url
- * @return string
- */
- public function getOgImage()
- {
- if (!$this->hasData('og_image')) {
- if ($file = $this->getData('og_img')) {
- $image = $this->_url->getMediaUrl($file);
- } else {
- $image = false;
- }
- $this->setData('og_image', $image);
- }
- return $this->getData('og_image');
- }
- /**
- * Retrieve post parent categories
- * @return \Magefan\Blog\Model\ResourceModel\Category\Collection
- */
- public function getParentCategories()
- {
- if (is_null($this->_parentCategories)) {
- $this->_parentCategories = $this->_categoryCollectionFactory->create()
- ->addFieldToFilter('category_id', ['in' => $this->getCategories()])
- ->addStoreFilter($this->getStoreId())
- ->addActiveFilter()
- ->setOrder('position');
- }
- return $this->_parentCategories;
- }
- /**
- * Retrieve parent category
- * @return \Magefan\Blog\Model\Category || false
- */
- public function getParentCategory()
- {
- $k = 'parent_category';
- if (null === $this->getData($k)) {
- $this->setData($k, false);
- foreach ($this->getParentCategories() as $category) {
- if ($category->isVisibleOnStore($this->getStoreId())) {
- $this->setData($k, $category);
- break;
- }
- }
- }
- return $this->getData($k);
- }
- /**
- * Retrieve post parent categories count
- * @return int
- */
- public function getCategoriesCount()
- {
- return count($this->getParentCategories());
- }
- /**
- * Retrieve post tags
- * @return \Magefan\Blog\Model\ResourceModel\Tag\Collection
- */
- public function getRelatedTags()
- {
- if (is_null($this->_relatedTags)) {
- $this->_relatedTags = $this->_tagCollectionFactory->create()
- ->addFieldToFilter('tag_id', ['in' => $this->getTags()])
- ->setOrder('title');
- }
- return $this->_relatedTags;
- }
- /**
- * Retrieve post tags count
- * @return int
- */
- public function getTagsCount()
- {
- return count($this->getRelatedTags());
- }
- /**
- * Retrieve post related posts
- * @return \Magefan\Blog\Model\ResourceModel\Post\Collection
- */
- public function getRelatedPosts()
- {
- if (!$this->hasData('related_posts')) {
- $collection = $this->_relatedPostsCollection
- ->addFieldToFilter('post_id', ['neq' => $this->getId()])
- ->addStoreFilter($this->getStoreId());
- $collection->getSelect()->joinLeft(
- ['rl' => $this->getResource()->getTable('magefan_blog_post_relatedpost')],
- 'main_table.post_id = rl.related_id',
- ['position']
- )->where(
- 'rl.post_id = ?',
- $this->getId()
- );
- $this->setData('related_posts', $collection);
- }
- return $this->getData('related_posts');
- }
- /**
- * Retrieve post related products
- * @return \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
- */
- public function getRelatedProducts()
- {
- if (!$this->hasData('related_products')) {
- $collection = $this->_productCollectionFactory->create();
- if ($this->getStoreId()) {
- $collection->addStoreFilter($this->getStoreId());
- }
- $collection->getSelect()->joinLeft(
- ['rl' => $this->getResource()->getTable('magefan_blog_post_relatedproduct')],
- 'e.entity_id = rl.related_id',
- ['position']
- )->where(
- 'rl.post_id = ?',
- $this->getId()
- );
- $this->setData('related_products', $collection);
- }
- return $this->getData('related_products');
- }
- /**
- * Retrieve post author
- * @return \Magefan\Blog\Model\Author | false
- */
- public function getAuthor()
- {
- if (!$this->hasData('author')) {
- $author = false;
- if ($authorId = $this->getData('author_id')) {
- $_author = $this->_authorFactory->create();
- $_author->load($authorId);
- if ($_author->getId()) {
- $author = $_author;
- }
- }
- $this->setData('author', $author);
- }
- return $this->getData('author');
- }
- /**
- * Retrieve if is visible on store
- * @return bool
- */
- public function isVisibleOnStore($storeId)
- {
- return $this->getIsActive()
- && $this->getData('publish_time') <= $this->getResource()->getDate()->gmtDate()
- && array_intersect([0, $storeId], $this->getStoreIds());
- }
- /**
- * Retrieve if is preview secret is valid
- * @return bool
- */
- public function isValidSecret($secret)
- {
- return ($secret && $this->getSecret() === $secret);
- }
- /**
- * Retrieve post publish date using format
- * @param string $format
- * @return string
- */
- public function getPublishDate($format = 'Y-m-d H:i:s')
- {
- return \Magefan\Blog\Helper\Data::getTranslatedDate(
- $format,
- $this->getData('publish_time')
- );
- }
- /**
- * Retrieve post publish date using format
- * @param string $format
- * @return string
- */
- public function getUpdateDate($format = 'Y-m-d H:i:s')
- {
- return \Magefan\Blog\Helper\Data::getTranslatedDate(
- $format,
- $this->getData('update_time')
- );
- }
- /**
- * Temporary method to get images from some custom blog version. Do not use this method.
- * @param string $format
- * @return string
- */
- public function getPostImage()
- {
- $image = $this->getData('featured_img');
- if (!$image) {
- $image = $this->getData('post_image');
- }
- return $image;
- }
- /**
- * Prepare all additional data
- * @param string $format
- * @return self
- */
- public function initDinamicData()
- {
- $keys = [
- 'og_image',
- 'og_type',
- 'og_description',
- 'og_title',
- 'meta_description',
- 'meta_title',
- 'short_filtered_content',
- 'filtered_content',
- 'first_image',
- 'featured_image',
- 'post_url',
- ];
- foreach ($keys as $key) {
- $method = 'get' . str_replace('_', '',
- ucwords($key, '_')
- );
- $this->$method();
- }
- return $this;
- }
- /**
- * Duplicate post and return new object
- * @return self
- */
- public function duplicate()
- {
- $object = clone $this;
- $object
- ->unsetData('post_id')
- ->setTitle($object->getTitle() . ' (' . __('Duplicated') . ')')
- ->setData('is_active', 0);
- $relatedProductIds = $this->getRelatedProducts()->getAllIds();
- $relatedPpostIds = $this->getRelatedPosts()->getAllIds();
- $object->setData(
- 'links',
- [
- 'product' => array_combine($relatedProductIds, $relatedProductIds),
- 'post' => array_combine($relatedPpostIds, $relatedPpostIds),
- ]
- );
- return $object->save();
- }
- /**
- * Retrieve secret key of post, it can be used during preview
- * @return string
- */
- public function getSecret()
- {
- if ($this->getId() && !$this->getData('secret')) {
- $this->setData(
- 'secret',
- $this->random->getRandomString(32)
- );
- $this->save();
- }
- return $this->getData('secret');
- }
- }
|