/pimcore/models/Document/Link.php

https://github.com/ngocanh/pimcore · PHP · 467 lines · 199 code · 59 blank · 209 comment · 28 complexity · 3aebd80c530714671b55fcd3edfd2732 MD5 · raw file

  1. <?php
  2. /**
  3. * Pimcore
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.pimcore.org/license
  11. *
  12. * @category Pimcore
  13. * @package Document
  14. * @copyright Copyright (c) 2009-2010 elements.at New Media Solutions GmbH (http://www.elements.at)
  15. * @license http://www.pimcore.org/license New BSD License
  16. */
  17. class Document_Link extends Document
  18. {
  19. /**
  20. * Contains the ID of the internal ID
  21. *
  22. * @var integer
  23. */
  24. public $internal;
  25. /**
  26. * Contains the ID of the internal ID
  27. *
  28. * @var string
  29. */
  30. public $internalType;
  31. /**
  32. * Contains object of linked Document|Asset
  33. *
  34. * @var Document | Asset
  35. */
  36. public $object;
  37. /**
  38. * Contains the direkt link as plain text
  39. *
  40. * @var string
  41. */
  42. public $direct = "";
  43. /**
  44. * Type of the link (internal/direct)
  45. *
  46. * @var string
  47. */
  48. public $linktype = "internal";
  49. /**
  50. * static type of this object
  51. *
  52. * @var string
  53. */
  54. public $type = "link";
  55. /**
  56. * path of the link
  57. *
  58. * @var string
  59. */
  60. public $href = "";
  61. /**
  62. * @see Document::resolveDependencies
  63. * @return array
  64. */
  65. public function resolveDependencies()
  66. {
  67. $dependencies = parent::resolveDependencies();
  68. if ($this->getLinktype() == "internal") {
  69. if ($this->getObject() instanceof Document || $this->getObject() instanceof Asset) {
  70. $key = $this->getInternalType() . "_" . $this->getObject()->getId();
  71. $dependencies[$key] = array(
  72. "id" => $this->getObject()->getId(),
  73. "type" => $this->getInternalType()
  74. );
  75. }
  76. }
  77. return $dependencies;
  78. }
  79. /**
  80. * Resolves dependencies and create tags for caching out of them
  81. *
  82. * @return array
  83. */
  84. public function getCacheTags($tags = array())
  85. {
  86. $tags = is_array($tags) ? $tags : array();
  87. $tags = parent::getCacheTags($tags);
  88. if ($this->getLinktype() == "internal") {
  89. if ($this->getObject() instanceof Document || $this->getObject() instanceof Asset) {
  90. if ($this->getObject()->getId() != $this->getId() and !array_key_exists($this->getObject()->getCacheTag(), $tags)) {
  91. $tags = $this->getObject()->getCacheTags($tags);
  92. }
  93. }
  94. }
  95. return $tags;
  96. }
  97. /**
  98. * Returns the plain text path of the link
  99. *
  100. * @return string
  101. */
  102. public function getHref()
  103. {
  104. $path = "";
  105. if ($this->getLinktype() == "internal") {
  106. if ($this->getObject() instanceof Document || $this->getObject() instanceof Asset) {
  107. $path = $this->getObject()->getFullPath();
  108. }
  109. }
  110. else {
  111. $path = $this->getDirect();
  112. }
  113. $this->href = $path;
  114. return $path;
  115. }
  116. /**
  117. * Returns the path of the link including the anchor and parameters
  118. *
  119. * @return string
  120. */
  121. public function getLink () {
  122. $path = $this->getHref();
  123. if (strlen($this->getParameters()) > 0) {
  124. $path .= "?" . str_replace("?", "", $this->getParameters());
  125. }
  126. if (strlen($this->getAnchor()) > 0) {
  127. $path .= "#" . str_replace("#", "", $this->getAnchor());
  128. }
  129. return $path;
  130. }
  131. /**
  132. * getProperty method should be used instead
  133. *
  134. * @deprecated
  135. * @return string
  136. */
  137. public function getTarget()
  138. {
  139. return $this->getProperty("navigation_target");
  140. }
  141. /**
  142. * setProperty method should be used instead
  143. *
  144. * @deprecated
  145. * @param string $target
  146. * @return string
  147. */
  148. public function setTarget($target)
  149. {
  150. $this->setProperty("navigation_target", "text", $target, false);
  151. }
  152. /**
  153. * Returns the id of the internal document|asset which is linked
  154. *
  155. * @return integer
  156. */
  157. public function getInternal()
  158. {
  159. return $this->internal;
  160. }
  161. /**
  162. * Returns the direct link (eg. http://www.pimcore.org/test)
  163. *
  164. * @return string
  165. */
  166. public function getDirect()
  167. {
  168. return $this->direct;
  169. }
  170. /**
  171. * Returns the type of the link (internal/direct)
  172. *
  173. * @return string
  174. */
  175. public function getLinktype()
  176. {
  177. return $this->linktype;
  178. }
  179. /**
  180. * @param integer $internal
  181. * @return void
  182. */
  183. public function setInternal($internal)
  184. {
  185. if (!empty($internal)) {
  186. $this->internal = $internal;
  187. $this->setObjectFromId();
  188. }
  189. else {
  190. $this->internal = null;
  191. }
  192. }
  193. /**
  194. * @param string $direct
  195. * @return void
  196. */
  197. public function setDirect($direct)
  198. {
  199. $this->direct = $direct;
  200. }
  201. /**
  202. * @param string $linktype
  203. * @return void
  204. */
  205. public function setLinktype($linktype)
  206. {
  207. $this->linktype = $linktype;
  208. }
  209. /**
  210. * getProperty method should be used instead
  211. *
  212. * @deprecated
  213. * @return string
  214. */
  215. public function getName()
  216. {
  217. return $this->getProperty("navigation_name");
  218. }
  219. /**
  220. * setProperty method should be used instead
  221. *
  222. * @deprecated
  223. * @param string $name
  224. * @return void
  225. */
  226. public function setName($name)
  227. {
  228. $this->setProperty("navigation_name", "text", $name, false);
  229. }
  230. /**
  231. * @return string
  232. */
  233. public function getInternalType()
  234. {
  235. return $this->internalType;
  236. }
  237. /**
  238. * @param string $type
  239. * @return void
  240. */
  241. public function setInternalType($type)
  242. {
  243. $this->internalType = $type;
  244. }
  245. /**
  246. * @return Document|Asset
  247. */
  248. public function getObject()
  249. {
  250. if ($this->object instanceof Document || $this->object instanceof Asset) {
  251. return $this->object;
  252. }
  253. else {
  254. if ($this->setObjectFromId()) {
  255. return $this->object;
  256. }
  257. }
  258. return false;
  259. }
  260. /**
  261. * @return void
  262. */
  263. public function setObject($object)
  264. {
  265. $this->object = $object;
  266. }
  267. /**
  268. * @return void
  269. */
  270. public function setObjectFromId()
  271. {
  272. if ($this->internalType == "document") {
  273. $this->object = Document::getById($this->internal);
  274. }
  275. else if ($this->internalType == "asset") {
  276. $this->object = Asset::getById($this->internal);
  277. }
  278. return $this->object;
  279. }
  280. /**
  281. * getProperty method should be used instead
  282. *
  283. * @deprecated
  284. * @return string
  285. */
  286. public function getParameters()
  287. {
  288. return $this->getProperty("navigation_parameters");
  289. }
  290. /**
  291. * setProperty method should be used instead
  292. *
  293. * @deprecated
  294. * @param string $parameters
  295. */
  296. public function setParameters($parameters)
  297. {
  298. $this->setProperty("navigation_parameters", "text", $parameters, false);
  299. }
  300. /**
  301. * getProperty method should be used instead
  302. *
  303. * @deprecated
  304. * @return string
  305. */
  306. public function getAnchor()
  307. {
  308. return $this->getProperty("navigation_anchor");
  309. }
  310. /**
  311. * setProperty method should be used instead
  312. *
  313. * @deprecated
  314. * @param string $anchor
  315. */
  316. public function setAnchor($anchor)
  317. {
  318. $this->setProperty("navigation_anchor", "text", $anchor, false);
  319. }
  320. /**
  321. * @return string
  322. */
  323. public function getTitle()
  324. {
  325. return $this->title;
  326. }
  327. /**
  328. * @param string $title
  329. */
  330. public function setTitle($title)
  331. {
  332. $this->title = $title;
  333. }
  334. /**
  335. * getProperty method should be used instead
  336. *
  337. * @deprecated
  338. * @return string
  339. */
  340. public function getAccesskey()
  341. {
  342. return $this->getProperty("accesskey");
  343. }
  344. /**
  345. * setProperty method should be used instead
  346. *
  347. * @deprecated
  348. * @param string $accesskey
  349. */
  350. public function setAccesskey($accesskey)
  351. {
  352. $this->setProperty("accesskey", "text", $accesskey, false);
  353. }
  354. /**
  355. * getProperty method should be used instead
  356. *
  357. * @deprecated
  358. * @return string
  359. */
  360. public function getRel()
  361. {
  362. return $this->getProperty("navigation_relation");
  363. }
  364. /**
  365. * setProperty method should be used instead
  366. *
  367. * @deprecated
  368. * @param string $rel
  369. */
  370. public function setRel($rel)
  371. {
  372. $this->setProperty("navigation_relation", "text", $rel, false);
  373. }
  374. /**
  375. * getProperty method should be used instead
  376. *
  377. * @deprecated
  378. * @return string
  379. */
  380. public function getTabindex()
  381. {
  382. return $this->getProperty("tabindex");
  383. }
  384. /**
  385. * setProperty method should be used instead
  386. *
  387. * @deprecated
  388. * @param string $tabindex
  389. */
  390. public function setTabindex($tabindex)
  391. {
  392. $this->setProperty("navigation_tabindex", "text", $tabindex, false);
  393. }
  394. /**
  395. * returns the ready-use html for this link
  396. *
  397. * @return string
  398. */
  399. public function getHtml()
  400. {
  401. $attributes = array("rel", "tabindex", "accesskey", "title", "name", "target");
  402. $attribs = array();
  403. foreach ($attributes as $a) {
  404. $attribs[] = $a . '="' . $this->$a . '"';
  405. }
  406. return '<a href="' . $this->getLink() . '" ' . implode(" ", $attribs) . '>' . htmlspecialchars($this->getProperty("navigation_name")) . '</a>';
  407. }
  408. }