|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Vuefront\Vuefront\Model\Api\Model\Common; |
| 4 | + |
| 5 | +use \Vuefront\Vuefront\Model\Api\System\Engine\Model; |
| 6 | +use Magefan\Blog\Model\Url; |
| 7 | + |
| 8 | + |
| 9 | +class Seo extends Model |
| 10 | +{ |
| 11 | + private $_categoryFactory; |
| 12 | + private $_categoryCollectionFactory; |
| 13 | + private $_blogCategoryCollectionFactory; |
| 14 | + private $_pageCollectionFactory; |
| 15 | + private $_postCollectionFactory; |
| 16 | + private $_productCollectionFactory; |
| 17 | + private $_manufacturerCollectionFactory; |
| 18 | + private $_scopeConfig; |
| 19 | + private $_suffix; |
| 20 | + private $_url; |
| 21 | + |
| 22 | + public function __construct( |
| 23 | + \Magento\Catalog\Model\CategoryFactory $categoryFactory, |
| 24 | + \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory, |
| 25 | + \Magento\Cms\Model\ResourceModel\Page\CollectionFactory $pageCollectionFactory, |
| 26 | + \Magefan\Blog\Model\ResourceModel\Post\CollectionFactory $postCollectionFactory, |
| 27 | + \Magefan\Blog\Model\ResourceModel\Category\CollectionFactory $blogCategoryCollectionFactory, |
| 28 | + \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, |
| 29 | + \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory, |
| 30 | + \Ves\Brand\Model\ResourceModel\Brand\CollectionFactory $manufacturerCollectionFactory, |
| 31 | + Url $url |
| 32 | + ) { |
| 33 | + $this->_blogCategoryCollectionFactory = $blogCategoryCollectionFactory; |
| 34 | + $this->_url = $url; |
| 35 | + $this->_categoryCollectionFactory = $categoryCollectionFactory; |
| 36 | + $this->_categoryFactory = $categoryFactory; |
| 37 | + $this->_scopeConfig = $scopeConfig; |
| 38 | + $this->_pageCollectionFactory = $pageCollectionFactory; |
| 39 | + $this->_postCollectionFactory = $postCollectionFactory; |
| 40 | + $this->_productCollectionFactory = $productCollectionFactory; |
| 41 | + $this->_manufacturerCollectionFactory = $manufacturerCollectionFactory; |
| 42 | + $this->_suffix = $this->_scopeConfig->getValue('catalog/seo/category_url_suffix'); |
| 43 | + } |
| 44 | + |
| 45 | + public function searchKeyword($keyword) |
| 46 | + { |
| 47 | + $url_key = str_replace($this->_suffix, '', $keyword); |
| 48 | + $url_key = ltrim($url_key, '/'); |
| 49 | + |
| 50 | + $result = $this->_categoryCollectionFactory->create()->addAttributeToFilter('url_key', $url_key)->load(); |
| 51 | + |
| 52 | + if (count($result->getItems()) > 0) { |
| 53 | + $category = $result->getFirstItem(); |
| 54 | + return [ |
| 55 | + 'type' => 'category', |
| 56 | + 'id' => $category->getId(), |
| 57 | + 'url' => $keyword |
| 58 | + ]; |
| 59 | + } |
| 60 | + |
| 61 | + $result = $this->_productCollectionFactory->create()->addAttributeToFilter('url_key', $url_key)->load(); |
| 62 | + |
| 63 | + if (count($result->getItems()) > 0) { |
| 64 | + $product = $result->getFirstItem(); |
| 65 | + return [ |
| 66 | + 'type' => 'product', |
| 67 | + 'id' => $product->getId(), |
| 68 | + 'url' => $keyword |
| 69 | + ]; |
| 70 | + } |
| 71 | + |
| 72 | + $result = $this->_pageCollectionFactory->create() |
| 73 | + ->addFieldToFilter('identifier', ['like' => $url_key])->load(); |
| 74 | + |
| 75 | + if (count($result->getItems()) > 0) { |
| 76 | + $page = $result->getFirstItem(); |
| 77 | + return [ |
| 78 | + 'type' => 'page', |
| 79 | + 'id' => $page->getId(), |
| 80 | + 'url' => $keyword |
| 81 | + ]; |
| 82 | + } |
| 83 | + |
| 84 | + $result = $this->_manufacturerCollectionFactory->create() |
| 85 | + ->addFieldToFilter('url_key', ['like' => $url_key])->load(); |
| 86 | + |
| 87 | + if (count($result->getItems()) > 0) { |
| 88 | + $manufacturer = $result->getFirstItem(); |
| 89 | + return [ |
| 90 | + 'type' => 'manufacturer', |
| 91 | + 'id' => $manufacturer->getId(), |
| 92 | + 'url' => $keyword |
| 93 | + ]; |
| 94 | + } |
| 95 | + |
| 96 | + $blog_post_url = ltrim($keyword, '/'.$this->_url->getRoute()); |
| 97 | + $blog_post_url = ltrim($blog_post_url, '/'.$this->_url->getRoute('post')); |
| 98 | + $blog_post_url = rtrim($blog_post_url, $this->_url->getUrlSufix('post')); |
| 99 | + $blog_post_url = ltrim($blog_post_url, '/'); |
| 100 | + |
| 101 | + $result = $this->_postCollectionFactory->create() |
| 102 | + ->addFieldToFilter('identifier', ['like' => $blog_post_url])->load(); |
| 103 | + |
| 104 | + if (count($result->getItems()) > 0) { |
| 105 | + $post = $result->getFirstItem(); |
| 106 | + return [ |
| 107 | + 'type' => 'blog-post', |
| 108 | + 'id' => $post->getId(), |
| 109 | + 'url' => $keyword |
| 110 | + ]; |
| 111 | + } |
| 112 | + |
| 113 | + $blog_category_url = ltrim($keyword, '/'.$this->_url->getRoute()); |
| 114 | + $blog_category_url = ltrim($blog_category_url, '/'.$this->_url->getRoute('category')); |
| 115 | + $blog_category_url = rtrim($blog_category_url, $this->_url->getUrlSufix('category')); |
| 116 | + $blog_category_url = ltrim($blog_category_url, '/'); |
| 117 | + |
| 118 | + $result = $this->_blogCategoryCollectionFactory->create() |
| 119 | + ->addFieldToFilter('identifier', ['like' => $blog_category_url])->load(); |
| 120 | + |
| 121 | + if (count($result->getItems()) > 0) { |
| 122 | + $blogCategory = $result->getFirstItem(); |
| 123 | + return [ |
| 124 | + 'type' => 'blog-category', |
| 125 | + 'id' => $blogCategory->getId(), |
| 126 | + 'url' => $keyword |
| 127 | + ]; |
| 128 | + } |
| 129 | + |
| 130 | + return [ |
| 131 | + 'type' => '', |
| 132 | + 'id' => 0, |
| 133 | + 'url' => $keyword |
| 134 | + ]; |
| 135 | + } |
| 136 | +} |
0 commit comments