/library/massiveart/locator/UniformResourceLocator.php

https://github.com/massiveart-webservices/ZOOLU · PHP · 465 lines · 208 code · 69 blank · 188 comment · 26 complexity · cfb983a779d288293dfb6a5273eabc49 MD5 · raw file

  1. <?php
  2. /**
  3. * ZOOLU - Content Management System
  4. * Copyright (c) 2008-2012 HID GmbH (http://www.hid.ag)
  5. *
  6. * LICENSE
  7. *
  8. * This file is part of ZOOLU.
  9. *
  10. * ZOOLU is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * ZOOLU is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with ZOOLU. If not, see http://www.gnu.org/licenses/gpl-3.0.html.
  22. *
  23. * For further information visit our website www.getzoolu.org
  24. * or contact us at zoolu@getzoolu.org
  25. *
  26. * @category ZOOLU
  27. * @package library.massiveart.command
  28. * @copyright Copyright (c) 2008-2012 HID GmbH (http://www.hid.ag)
  29. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, Version 3
  30. * @version $Id: version.php
  31. */
  32. /**
  33. * HandleUndefinedMethod
  34. *
  35. *
  36. * Version history (please keep backward compatible):
  37. * 1.0, 2012-11-13: Thomas Schedler
  38. *
  39. * @author Thomas Schedler <tsh@massiveart.com>
  40. * @version 1.0
  41. * @package massiveart.locator
  42. * @subpackage Url
  43. */
  44. class UniformResourceLocator
  45. {
  46. const LAYOUT_TREE = 'tree';
  47. const LAYOUT_SHORT = 'short';
  48. /**
  49. * @var Model_Urls
  50. */
  51. private $modelUrls;
  52. /**
  53. * @var int
  54. */
  55. protected $rootLevelId;
  56. /**
  57. * @var string
  58. */
  59. protected $formType;
  60. /**
  61. * @var int
  62. */
  63. protected $type;
  64. /**
  65. * @var string
  66. */
  67. private $url;
  68. /**
  69. * @var string
  70. */
  71. protected $layout;
  72. /**
  73. * @var string
  74. */
  75. protected $path;
  76. /**
  77. * @var string
  78. */
  79. protected $prefix;
  80. /**
  81. * @var string
  82. */
  83. protected $languageCode;
  84. /**
  85. * @var int
  86. */
  87. protected $parentId;
  88. /**
  89. * @var array
  90. */
  91. protected $parents;
  92. /**
  93. * @var array
  94. */
  95. protected $replacers = array();
  96. /**
  97. * @var bool
  98. */
  99. protected $isStartElement;
  100. /**
  101. * @param $layout
  102. * @param $modelUrls
  103. */
  104. public function __construct($layout, $modelUrls)
  105. {
  106. $this->layout = $layout;
  107. $this->modelUrls = $modelUrls;
  108. }
  109. /**
  110. * @param bool $makeUnique
  111. */
  112. public function build($makeUnique = true)
  113. {
  114. switch ($this->layout) {
  115. case self::LAYOUT_SHORT:
  116. $this->url = $this->makeConform($this->path);
  117. if ($makeUnique) {
  118. if (!$this->checkUniqueness($this->url)) {
  119. $this->url = $this->makeShortUnique($this->url);
  120. }
  121. }
  122. break;
  123. case self::LAYOUT_TREE:
  124. default:
  125. if (count($this->parents) > 1 || (count($this->parents) > 0 && !$this->isStartElement)) {
  126. $this->url = $this->makeConform($this->prefix . $this->path);
  127. } else {
  128. $this->url = $this->makeConform($this->path);
  129. }
  130. if ($makeUnique) {
  131. $this->url = $this->makeUnique($this->url);
  132. }
  133. }
  134. }
  135. /**
  136. * @param $url
  137. * @param int $addon
  138. * @return string
  139. */
  140. public function makeUnique($url, $addon = 0)
  141. {
  142. if (rtrim($url, '/') != $url) {
  143. $newUrl = ($addon > 0) ? rtrim($url, '/') . '-' . $addon . '/' : $url;
  144. } else {
  145. $newUrl = ($addon > 0) ? $url . '-' . $addon : $url;
  146. }
  147. if (!$this->checkUniqueness($newUrl)) {
  148. return $this->makeUnique($url, $addon + 1);
  149. } else {
  150. return $newUrl;
  151. }
  152. }
  153. /**
  154. * @param $url
  155. * @return string
  156. */
  157. public function makeShortUnique($url)
  158. {
  159. $first = true;
  160. foreach ($this->parents as $parentFolder) {
  161. if (!($first && $this->isStartElement)) {
  162. $url = $this->makeConform(strtolower($parentFolder->title)) . '/' . $url;
  163. if ($this->checkUniqueness($url)) {
  164. break;
  165. }
  166. }
  167. $first = false;
  168. }
  169. if (!$this->checkUniqueness($url)) {
  170. $url = $this->makeUnique($url);
  171. }
  172. return $url;
  173. }
  174. /**
  175. * @param $url
  176. * @return bool
  177. */
  178. public function checkUniqueness($url)
  179. {
  180. $urls = $this->modelUrls->loadByUrl($this->rootLevelId, $url, $this->formType);
  181. if (isset($urls->url) && count($urls->url) > 0) {
  182. return false;
  183. } else {
  184. return true;
  185. }
  186. }
  187. /**
  188. * @param $path
  189. * @return mixed
  190. */
  191. public function makeConform($path)
  192. {
  193. $path = strtolower($path);
  194. // replace problematic characters
  195. if (count($this->replacers) > 0) {
  196. foreach ($this->replacers as $rePlacer) {
  197. $path = str_replace($rePlacer->from, $rePlacer->to, $path);
  198. }
  199. }
  200. $path = strtolower($path);
  201. // delete problematic characters
  202. $path = str_replace('%2F', '/', urlencode(preg_replace('/([^A-za-z0-9\s-_\/])/', '', $path)));
  203. $path = str_replace('+', '-', $path);
  204. // replace multiple minus with one
  205. $path = preg_replace('/([-]+)/', '-', $path);
  206. // delete minus at the beginning or end
  207. $path = preg_replace('/^([-])/', '', $path);
  208. $path = preg_replace('/([-])$/', '', $path);
  209. return $path;
  210. }
  211. /**
  212. * @param bool $makeUnique
  213. * @param bool $withLanguageCode
  214. * @return string
  215. */
  216. public function get($makeUnique = true, $withLanguageCode = false)
  217. {
  218. // is folder
  219. if ($this->isStartElement && !empty($this->parentId)) {
  220. $this->path = rtrim($this->path, '/') . '/';
  221. }
  222. $this->build($makeUnique);
  223. if (!empty($this->languageCode) && $withLanguageCode) {
  224. return $this->languageCode . '/' . $this->url;
  225. } else {
  226. return $this->url;
  227. }
  228. }
  229. /**
  230. * @return string
  231. */
  232. public function getLayout()
  233. {
  234. return $this->layout;
  235. }
  236. /**
  237. * @param string $path
  238. * @return UniformResourceLocator
  239. */
  240. public function setPath($path)
  241. {
  242. $this->path = $path;
  243. return $this;
  244. }
  245. /**
  246. * @return string
  247. */
  248. public function getPath()
  249. {
  250. return $this->path;
  251. }
  252. /**
  253. * @param string $prefix
  254. * @return UniformResourceLocator
  255. */
  256. public function setPrefix($prefix)
  257. {
  258. $this->prefix = $prefix;
  259. return $this;
  260. }
  261. /**
  262. * @return string
  263. */
  264. public function getPrefix()
  265. {
  266. return $this->prefix;
  267. }
  268. /**
  269. * @param string $languageCode
  270. * @return UniformResourceLocator
  271. */
  272. public function setLanguageCode($languageCode)
  273. {
  274. $this->languageCode = $languageCode;
  275. return $this;
  276. }
  277. /**
  278. * @return string
  279. */
  280. public function getLanguageCode()
  281. {
  282. return $this->languageCode;
  283. }
  284. /**
  285. * @param array $parents
  286. * @return UniformResourceLocator
  287. */
  288. public function setParents($parents)
  289. {
  290. $this->parents = $parents;
  291. return $this;
  292. }
  293. /**
  294. * @return array
  295. */
  296. public function getParents()
  297. {
  298. return $this->parents;
  299. }
  300. /**
  301. * @param int $parentId
  302. * @return UniformResourceLocator
  303. */
  304. public function setParentId($parentId)
  305. {
  306. $this->parentId = $parentId;
  307. return $this;
  308. }
  309. /**
  310. * @return int
  311. */
  312. public function getParentId()
  313. {
  314. return $this->parentId;
  315. }
  316. /**
  317. * @param bool $isStartElement
  318. * @return UniformResourceLocator
  319. */
  320. public function setIsStartElement($isStartElement)
  321. {
  322. $this->isStartElement = $isStartElement;
  323. return $this;
  324. }
  325. /**
  326. * @return bool
  327. */
  328. public function getIsStartElement()
  329. {
  330. return $this->isStartElement;
  331. }
  332. /**
  333. * @param array $replacers
  334. * @return UniformResourceLocator
  335. */
  336. public function setReplacers($replacers)
  337. {
  338. $this->replacers = $replacers;
  339. return $this;
  340. }
  341. /**
  342. * @return array
  343. */
  344. public function getReplacers()
  345. {
  346. return $this->replacers;
  347. }
  348. /**
  349. * @param int $rootLevelId
  350. * @return UniformResourceLocator
  351. */
  352. public function setRootLevelId($rootLevelId)
  353. {
  354. $this->rootLevelId = $rootLevelId;
  355. return $this;
  356. }
  357. /**
  358. * @return int
  359. */
  360. public function getRootLevelId()
  361. {
  362. return $this->rootLevelId;
  363. }
  364. /**
  365. * @param int $type
  366. * @return UniformResourceLocator
  367. */
  368. public function setType($type)
  369. {
  370. $this->type = $type;
  371. return $this;
  372. }
  373. /**
  374. * @return int
  375. */
  376. public function getType()
  377. {
  378. return $this->type;
  379. }
  380. /**
  381. * @param string $formType
  382. * @return UniformResourceLocator
  383. */
  384. public function setFormType($formType)
  385. {
  386. $this->formType = $formType;
  387. return $this;
  388. }
  389. /**
  390. * @return string
  391. */
  392. public function getFormType()
  393. {
  394. return $this->formType;
  395. }
  396. }