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\Messenger\GeneratePagePreviewMessage;
  16. use Pimcore\Model\Redirect;
  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. parent::doDelete();
  76. }
  77. /**
  78. * @return string
  79. */
  80. public function getDescription()
  81. {
  82. return $this->description;
  83. }
  84. /**
  85. * @return string
  86. */
  87. public function getTitle()
  88. {
  89. return \Pimcore\Tool\Text::removeLineBreaks($this->title);
  90. }
  91. /**
  92. * @param string $description
  93. *
  94. * @return $this
  95. */
  96. public function setDescription($description)
  97. {
  98. $this->description = str_replace("\n", ' ', $description);
  99. return $this;
  100. }
  101. /**
  102. * @param string $title
  103. *
  104. * @return $this
  105. */
  106. public function setTitle($title)
  107. {
  108. $this->title = $title;
  109. return $this;
  110. }
  111. /**
  112. * @param array $metaData
  113. *
  114. * @return $this
  115. */
  116. public function setMetaData($metaData)
  117. {
  118. $this->metaData = $metaData;
  119. return $this;
  120. }
  121. /**
  122. * @return array
  123. */
  124. public function getMetaData()
  125. {
  126. return $this->metaData;
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function getFullPath(bool $force = false)
  132. {
  133. $path = parent::getFullPath($force);
  134. // 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
  135. if (!\Pimcore::inAdmin() && !($this instanceof Hardlink\Wrapper\WrapperInterface) && \Pimcore\Tool\Frontend::isDocumentInCurrentSite($this)) {
  136. // check for a pretty url
  137. $prettyUrl = $this->getPrettyUrl();
  138. if (!empty($prettyUrl) && strlen($prettyUrl) > 1) {
  139. return $prettyUrl;
  140. }
  141. }
  142. return $path;
  143. }
  144. /**
  145. * @param string|null $prettyUrl
  146. *
  147. * @return $this
  148. */
  149. public function setPrettyUrl($prettyUrl)
  150. {
  151. if (!$prettyUrl) {
  152. $this->prettyUrl = null;
  153. } else {
  154. $this->prettyUrl = '/' . trim($prettyUrl, ' /');
  155. if (strlen($this->prettyUrl) < 2) {
  156. $this->prettyUrl = null;
  157. }
  158. }
  159. return $this;
  160. }
  161. /**
  162. * @return string|null
  163. */
  164. public function getPrettyUrl()
  165. {
  166. return $this->prettyUrl;
  167. }
  168. /**
  169. * Set linked Target Groups as set in properties panel as list of IDs
  170. *
  171. * @param string|array $targetGroupIds
  172. */
  173. public function setTargetGroupIds($targetGroupIds)
  174. {
  175. if (is_array($targetGroupIds)) {
  176. $targetGroupIds = implode(',', $targetGroupIds);
  177. }
  178. $targetGroupIds = trim($targetGroupIds, ' ,');
  179. if (!empty($targetGroupIds)) {
  180. $targetGroupIds = ',' . $targetGroupIds . ',';
  181. }
  182. $this->targetGroupIds = $targetGroupIds;
  183. }
  184. /**
  185. * Get serialized list of Target Group IDs
  186. *
  187. * @return string
  188. */
  189. public function getTargetGroupIds(): string
  190. {
  191. return $this->targetGroupIds;
  192. }
  193. /**
  194. * Set assigned target groups
  195. *
  196. * @param TargetGroup[]|int[] $targetGroups
  197. */
  198. public function setTargetGroups(array $targetGroups)
  199. {
  200. $ids = array_map(function ($targetGroup) {
  201. if (is_numeric($targetGroup)) {
  202. return (int)$targetGroup;
  203. } elseif ($targetGroup instanceof TargetGroup) {
  204. return $targetGroup->getId();
  205. }
  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((int) $id);
  224. if ($targetGroup) {
  225. return $targetGroup;
  226. }
  227. }
  228. }, $ids);
  229. $targetGroups = array_filter($targetGroups);
  230. return $targetGroups;
  231. }
  232. /**
  233. * @return string
  234. */
  235. public function getPreviewImageFilesystemPath()
  236. {
  237. return PIMCORE_SYSTEM_TEMP_DIRECTORY . '/document-page-previews/document-page-screenshot-' . $this->getId() . '@2x.jpg';
  238. }
  239. public function save()
  240. {
  241. $response = parent::save(...func_get_args());
  242. // Dispatch page preview message, if preview is enabled.
  243. $documentsConfig = \Pimcore\Config::getSystemConfiguration('documents');
  244. if ($documentsConfig['generate_preview'] ?? false) {
  245. \Pimcore::getContainer()->get('messenger.bus.pimcore-core')->dispatch(
  246. new GeneratePagePreviewMessage($this->getId(), \Pimcore\Tool::getHostUrl())
  247. );
  248. }
  249. return $response;
  250. }
  251. }