vendor/pimcore/pimcore/models/Document/Page.php line 25

Open in your IDE?
  1. <?php
  2. /**
  3. * Pimcore
  4. *
  5. * This source file is available under two different licenses:
  6. * - GNU General Public License version 3 (GPLv3)
  7. * - Pimcore Commercial License (PCL)
  8. * Full copyright and license information is available in
  9. * LICENSE.md which is distributed with this source code.
  10. *
  11. * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12. * @license http://www.pimcore.org/license GPLv3 and PCL
  13. */
  14. namespace Pimcore\Model\Document;
  15. use Pimcore\Model\Redirect;
  16. use Pimcore\Model\Site;
  17. use Pimcore\Model\Tool\Targeting\TargetGroup;
  18. /**
  19. * @method \Pimcore\Model\Document\Page\Dao getDao()
  20. */
  21. class Page extends TargetingDocument
  22. {
  23. /**
  24. * Contains the title of the page (meta-title)
  25. *
  26. * @internal
  27. *
  28. * @var string
  29. */
  30. protected $title = '';
  31. /**
  32. * Contains the description of the page (meta-description)
  33. *
  34. * @internal
  35. *
  36. * @var string
  37. */
  38. protected $description = '';
  39. /**
  40. * @internal
  41. *
  42. * @var array
  43. */
  44. protected $metaData = [];
  45. /**
  46. * {@inheritdoc}
  47. */
  48. protected string $type = 'page';
  49. /**
  50. * @internal
  51. *
  52. * @var string|null
  53. */
  54. protected $prettyUrl;
  55. /**
  56. * Comma separated IDs of target groups
  57. *
  58. * @internal
  59. *
  60. * @var string
  61. */
  62. protected $targetGroupIds = '';
  63. /**
  64. * {@inheritdoc}
  65. */
  66. protected function doDelete()
  67. {
  68. // check for redirects pointing to this document, and delete them too
  69. $redirects = new Redirect\Listing();
  70. $redirects->setCondition('target = ?', $this->getId());
  71. $redirects->load();
  72. foreach ($redirects->getRedirects() as $redirect) {
  73. $redirect->delete();
  74. }
  75. if ($site = Site::getByRootId($this->getId())) {
  76. $site->delete();
  77. }
  78. parent::doDelete();
  79. }
  80. /**
  81. * @return string
  82. */
  83. public function getDescription()
  84. {
  85. return $this->description;
  86. }
  87. /**
  88. * @return string
  89. */
  90. public function getTitle()
  91. {
  92. return \Pimcore\Tool\Text::removeLineBreaks($this->title);
  93. }
  94. /**
  95. * @param string $description
  96. *
  97. * @return $this
  98. */
  99. public function setDescription($description)
  100. {
  101. $this->description = str_replace("\n", ' ', $description);
  102. return $this;
  103. }
  104. /**
  105. * @param string $title
  106. *
  107. * @return $this
  108. */
  109. public function setTitle($title)
  110. {
  111. $this->title = $title;
  112. return $this;
  113. }
  114. /**
  115. * @param array $metaData
  116. *
  117. * @return $this
  118. */
  119. public function setMetaData($metaData)
  120. {
  121. $this->metaData = $metaData;
  122. return $this;
  123. }
  124. /**
  125. * @return array
  126. */
  127. public function getMetaData()
  128. {
  129. return $this->metaData;
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function getFullPath(bool $force = false)
  135. {
  136. $path = parent::getFullPath($force);
  137. // do not use pretty url's when in admin, the current document is wrapped by a hardlink or this document isn't in the current site
  138. if (!\Pimcore::inAdmin() && !($this instanceof Hardlink\Wrapper\WrapperInterface) && \Pimcore\Tool\Frontend::isDocumentInCurrentSite($this)) {
  139. // check for a pretty url
  140. $prettyUrl = $this->getPrettyUrl();
  141. if (!empty($prettyUrl) && strlen($prettyUrl) > 1) {
  142. return $prettyUrl;
  143. }
  144. }
  145. return $path;
  146. }
  147. /**
  148. * @param string $prettyUrl
  149. *
  150. * @return $this
  151. */
  152. public function setPrettyUrl($prettyUrl)
  153. {
  154. $this->prettyUrl = '/' . trim($prettyUrl, ' /');
  155. if (strlen($this->prettyUrl) < 2) {
  156. $this->prettyUrl = null;
  157. }
  158. return $this;
  159. }
  160. /**
  161. * @return string|null
  162. */
  163. public function getPrettyUrl()
  164. {
  165. return $this->prettyUrl;
  166. }
  167. /**
  168. * Set linked Target Groups as set in properties panel as list of IDs
  169. *
  170. * @param string|array $targetGroupIds
  171. */
  172. public function setTargetGroupIds($targetGroupIds)
  173. {
  174. if (is_array($targetGroupIds)) {
  175. $targetGroupIds = implode(',', $targetGroupIds);
  176. }
  177. $targetGroupIds = trim($targetGroupIds, ' ,');
  178. if (!empty($targetGroupIds)) {
  179. $targetGroupIds = ',' . $targetGroupIds . ',';
  180. }
  181. $this->targetGroupIds = $targetGroupIds;
  182. }
  183. /**
  184. * Get serialized list of Target Group IDs
  185. *
  186. * @return string
  187. */
  188. public function getTargetGroupIds(): string
  189. {
  190. return $this->targetGroupIds;
  191. }
  192. /**
  193. * Set assigned target groups
  194. *
  195. * @param TargetGroup[]|int[] $targetGroups
  196. */
  197. public function setTargetGroups(array $targetGroups)
  198. {
  199. $ids = array_map(function ($targetGroup) {
  200. if (is_numeric($targetGroup)) {
  201. return (int)$targetGroup;
  202. } elseif ($targetGroup instanceof TargetGroup) {
  203. return $targetGroup->getId();
  204. }
  205. return null;
  206. }, $targetGroups);
  207. $ids = array_filter($ids, function ($id) {
  208. return null !== $id && $id > 0;
  209. });
  210. $this->setTargetGroupIds($ids);
  211. }
  212. /**
  213. * Return list of assigned target groups (via properties panel)
  214. *
  215. * @return TargetGroup[]
  216. */
  217. public function getTargetGroups(): array
  218. {
  219. $ids = explode(',', $this->targetGroupIds);
  220. $targetGroups = array_map(function ($id) {
  221. $id = trim($id);
  222. if (!empty($id)) {
  223. $targetGroup = TargetGroup::getById($id);
  224. if ($targetGroup) {
  225. return $targetGroup;
  226. }
  227. }
  228. }, $ids);
  229. $targetGroups = array_filter($targetGroups);
  230. return $targetGroups;
  231. }
  232. /**
  233. * @param bool $hdpi
  234. *
  235. * @return string
  236. */
  237. public function getPreviewImageFilesystemPath($hdpi = false)
  238. {
  239. $suffix = '';
  240. if ($hdpi) {
  241. $suffix = '@2x';
  242. }
  243. return PIMCORE_SYSTEM_TEMP_DIRECTORY . '/document-page-previews/document-page-screenshot-' . $this->getId() . $suffix . '.jpg';
  244. }
  245. }