Url.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. /**
  3. * Copyright © 2015-2017 Ihor Vansach (ihor@magefan.com). All rights reserved.
  4. * See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
  5. *
  6. * Glory to Ukraine! Glory to the heroes!
  7. */
  8. namespace Magefan\Blog\Model;
  9. /**
  10. * Blog url model
  11. */
  12. class Url
  13. {
  14. /**
  15. * Permalink Types
  16. */
  17. const PERMALINK_TYPE_DEFAULT = 'default';
  18. const PERMALINK_TYPE_SHORT = 'short';
  19. /**
  20. * Objects Types
  21. */
  22. const CONTROLLER_POST = 'post';
  23. const CONTROLLER_CATEGORY = 'category';
  24. const CONTROLLER_ARCHIVE = 'archive';
  25. const CONTROLLER_AUTHOR = 'author';
  26. const CONTROLLER_SEARCH = 'search';
  27. const CONTROLLER_RSS = 'rss';
  28. const CONTROLLER_TAG = 'tag';
  29. /**
  30. * @var \Magento\Framework\Registry
  31. */
  32. protected $_registry;
  33. /**
  34. * @var \Magento\Framework\UrlInterface
  35. */
  36. protected $_url;
  37. /**
  38. * @var \Magento\Store\Model\StoreManagerInterface
  39. */
  40. protected $_storeManager;
  41. /**
  42. * Core store config
  43. *
  44. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  45. */
  46. protected $_scopeConfig;
  47. /**
  48. * Store id
  49. * @var int | null
  50. */
  51. protected $storeId;
  52. /**
  53. * Initialize dependencies.
  54. *
  55. * @param \Magento\Framework\Registry $registry
  56. * @param \Magento\Framework\UrlInterface $url
  57. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  58. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  59. */
  60. public function __construct(
  61. \Magento\Framework\Registry $registry,
  62. \Magento\Framework\UrlInterface $url,
  63. \Magento\Store\Model\StoreManagerInterface $storeManager,
  64. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  65. ) {
  66. $this->_registry = $registry;
  67. $this->_url = $url;
  68. $this->_storeManager = $storeManager;
  69. $this->_scopeConfig = $scopeConfig;
  70. }
  71. /**
  72. * Retrieve permalink type
  73. * @return string
  74. */
  75. public function getPermalinkType()
  76. {
  77. return $this->_getConfig('type');
  78. }
  79. /**
  80. * Retrieve route name by controller
  81. * @param string $controllerName
  82. * @param boolean $skip
  83. * @return string || null
  84. */
  85. public function getRoute($controllerName = null, $skip = true)
  86. {
  87. if ($controllerName) {
  88. $controllerName .= '_';
  89. }
  90. if ($route = $this->_getConfig($controllerName . 'route')) {
  91. return $route;
  92. } else {
  93. return $skip ? $controllerName : null;
  94. }
  95. }
  96. /**
  97. * Retrieve controller name by route
  98. * @param string $route
  99. * @param boolean $skip
  100. * @return string || null
  101. */
  102. public function getControllerName($route, $skip = true)
  103. {
  104. foreach([
  105. self::CONTROLLER_POST,
  106. self::CONTROLLER_CATEGORY,
  107. self::CONTROLLER_ARCHIVE,
  108. self::CONTROLLER_AUTHOR,
  109. self::CONTROLLER_TAG,
  110. self::CONTROLLER_SEARCH
  111. ] as $controllerName) {
  112. if ($this->getRoute($controllerName) == $route) {
  113. return $controllerName;
  114. }
  115. }
  116. return $skip ? $route : null;
  117. }
  118. /**
  119. * Retrieve blog base url
  120. * @return string
  121. */
  122. public function getBaseUrl()
  123. {
  124. return $this->_url->getUrl($this->getRoute());
  125. }
  126. /**
  127. * Retrieve blog page url
  128. * @param string $identifier
  129. * @param string $controllerName
  130. * @return string
  131. */
  132. public function getUrl($identifier, $controllerName)
  133. {
  134. $url = $this->_url->getUrl('', [
  135. '_direct' => $this->getUrlPath($identifier, $controllerName)
  136. ]);
  137. return $url;
  138. }
  139. /**
  140. * Retrieve canonical url
  141. * @param \Magento\Framework\Model\AbstractModel $object
  142. * @return string
  143. */
  144. public function getCanonicalUrl(\Magento\Framework\Model\AbstractModel $object)
  145. {
  146. $storeIds = $object->getStoreIds();
  147. $useDefaultStore = false;
  148. $currentStore = $this->_storeManager->getStore($object->getStoreId());
  149. if (is_array($storeIds)) {
  150. if (0 == array_values($storeIds)[0]) {
  151. $useDefaultStore = true;
  152. } elseif (count($storeIds > 1)) {
  153. foreach ($storeIds as $storeId) {
  154. if ($storeId != $currentStore->getId()) {
  155. $store = $this->_storeManager->getStore($storeId);
  156. if ($store->getGroupId() == $currentStore->getGroupId()) {
  157. $useDefaultStore = true;
  158. break;
  159. }
  160. }
  161. }
  162. }
  163. }
  164. $storeChanged = false;
  165. if ($useDefaultStore) {
  166. $newStore = $currentStore->getGroup()->getDefaultStore();
  167. $origStore = $this->_url->getScope();
  168. if ($newStore->getId() != $origStore->getId()) {
  169. $this->_url->setScope($newStore);
  170. $storeChanged = true;
  171. }
  172. }
  173. $url = $this->getUrl($object->getIdentifier(), $object->getControllerName());
  174. if ($storeChanged) {
  175. $this->_url->setScope($origStore);
  176. }
  177. return $url;
  178. }
  179. /**
  180. * Retrieve blog url path
  181. * @param string $identifier
  182. * @param string $controllerName
  183. * @return string
  184. */
  185. public function getUrlPath($identifier, $controllerName)
  186. {
  187. $identifier = $this->getExpandedItentifier($identifier);
  188. switch ($this->getPermalinkType()) {
  189. case self::PERMALINK_TYPE_DEFAULT :
  190. $path = $this->getRoute() . '/' . $this->getRoute($controllerName) . '/' . $identifier . ( $identifier ? '/' : '');
  191. break;
  192. case self::PERMALINK_TYPE_SHORT :
  193. if ($controllerName == self::CONTROLLER_SEARCH
  194. || $controllerName == self::CONTROLLER_AUTHOR
  195. || $controllerName == self::CONTROLLER_TAG
  196. ) {
  197. $path = $this->getRoute() . '/' . $this->getRoute($controllerName) . '/' . $identifier . ( $identifier ? '/' : '');
  198. } else {
  199. $path = $this->getRoute() . '/' . $identifier . ( $identifier ? '/' : '');
  200. }
  201. break;
  202. }
  203. $path = $this->addUrlSufix($path, $controllerName);
  204. return $path;
  205. }
  206. /**
  207. * Retrieve itentifier what include parent categories itentifier
  208. * @param \Magento\Framework\Model\AbstractModel || string $identifier
  209. * @return string
  210. */
  211. protected function getExpandedItentifier($identifier)
  212. {
  213. if (is_object($identifier)) {
  214. $object = $identifier;
  215. $identifier = $identifier->getIdentifier();
  216. $controllerName = $object->getControllerName();
  217. if ($this->_getConfig($controllerName . '_use_categories')
  218. ) {
  219. if ($parentCategory = $object->getParentCategory()) {
  220. if ($parentIdentifier = $this->getExpandedItentifier($parentCategory)) {
  221. $identifier = $parentIdentifier . '/' . $identifier;
  222. }
  223. }
  224. }
  225. }
  226. return $identifier;
  227. }
  228. /**
  229. * Add url sufix
  230. * @param string $url
  231. * @param string $controllerName
  232. * @return string
  233. */
  234. protected function addUrlSufix($url, $controllerName)
  235. {
  236. if (in_array($controllerName, [self::CONTROLLER_POST, self::CONTROLLER_CATEGORY])) {
  237. if ($sufix = $this->getUrlSufix($controllerName)) {
  238. $char = false;
  239. foreach(['#', '?'] as $ch) {
  240. if (false !== strpos($url, $ch)) {
  241. $char = $ch;
  242. }
  243. }
  244. if ($char) {
  245. $data = explode($char, $url);
  246. $data[0] = trim($data[0], '/') . $sufix;
  247. $url = implode($char, $url);
  248. } else {
  249. $url = trim($url, '/') . $sufix;
  250. }
  251. }
  252. }
  253. return $url;
  254. }
  255. /**
  256. * Retrieve trimmed url without sufix
  257. * @param string $identifier
  258. * @param string $sufix
  259. * @return string
  260. */
  261. public function trimSufix($identifier, $sufix)
  262. {
  263. if ($sufix) {
  264. $p = mb_strrpos($identifier, $sufix);
  265. if (false !== $p) {
  266. $li = mb_strlen($identifier);
  267. $ls = mb_strlen($sufix);
  268. if ($p + $ls == $li) {
  269. $identifier = mb_substr($identifier, 0, $p);
  270. }
  271. }
  272. }
  273. return $identifier;
  274. }
  275. /**
  276. * Retrieve post url sufix
  277. * @return string
  278. */
  279. public function getUrlSufix($controllerName)
  280. {
  281. return trim($this->_getConfig($controllerName . '_sufix'));
  282. }
  283. /**
  284. * Retrieve media url
  285. * @param string $file
  286. * @return string
  287. */
  288. public function getMediaUrl($file)
  289. {
  290. return $this->_storeManager->getStore()
  291. ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . $file;
  292. }
  293. /**
  294. * Retrieve blog permalink config value
  295. * @param string $key
  296. * @return string || null || int
  297. */
  298. protected function _getConfig($key)
  299. {
  300. return $this->_scopeConfig->getValue(
  301. 'mfblog/permalink/'.$key,
  302. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  303. $this->storeId
  304. );
  305. }
  306. }