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

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. * @deprecated
  113. *
  114. * @param array $metaData
  115. *
  116. * @return $this
  117. */
  118. public function setMetaData($metaData)
  119. {
  120. trigger_deprecation(
  121. 'pimcore/pimcore',
  122. '10.6.0',
  123. sprintf('%s is deprecated and will be removed in Pimcore 11.', __METHOD__)
  124. );
  125. $this->metaData = $metaData;
  126. return $this;
  127. }
  128. /**
  129. * @deprecated
  130. *
  131. * @return array
  132. */
  133. public function getMetaData()
  134. {
  135. trigger_deprecation(
  136. 'pimcore/pimcore',
  137. '10.6.0',
  138. sprintf('%s is deprecated and will be removed in Pimcore 11.', __METHOD__)
  139. );
  140. return $this->metaData;
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. public function getFullPath(bool $force = false)
  146. {
  147. $path = parent::getFullPath($force);
  148. // 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
  149. if (!\Pimcore::inAdmin() && !($this instanceof Hardlink\Wrapper\WrapperInterface) && \Pimcore\Tool\Frontend::isDocumentInCurrentSite($this)) {
  150. // check for a pretty url
  151. $prettyUrl = $this->getPrettyUrl();
  152. if (!empty($prettyUrl) && strlen($prettyUrl) > 1) {
  153. return $prettyUrl;
  154. }
  155. }
  156. return $path;
  157. }
  158. /**
  159. * @param string|null $prettyUrl
  160. *
  161. * @return $this
  162. */
  163. public function setPrettyUrl($prettyUrl)
  164. {
  165. if (!$prettyUrl) {
  166. $this->prettyUrl = null;
  167. } else {
  168. $this->prettyUrl = '/' . trim($prettyUrl, ' /');
  169. if (strlen($this->prettyUrl) < 2) {
  170. $this->prettyUrl = null;
  171. }
  172. }
  173. return $this;
  174. }
  175. /**
  176. * @return string|null
  177. */
  178. public function getPrettyUrl()
  179. {
  180. return $this->prettyUrl;
  181. }
  182. /**
  183. * Set linked Target Groups as set in properties panel as list of IDs
  184. *
  185. * @param string|array $targetGroupIds
  186. */
  187. public function setTargetGroupIds($targetGroupIds)
  188. {
  189. if (is_array($targetGroupIds)) {
  190. $targetGroupIds = implode(',', $targetGroupIds);
  191. }
  192. $targetGroupIds = trim($targetGroupIds, ' ,');
  193. if (!empty($targetGroupIds)) {
  194. $targetGroupIds = ',' . $targetGroupIds . ',';
  195. }
  196. $this->targetGroupIds = $targetGroupIds;
  197. }
  198. /**
  199. * Get serialized list of Target Group IDs
  200. *
  201. * @return string
  202. */
  203. public function getTargetGroupIds(): string
  204. {
  205. return $this->targetGroupIds;
  206. }
  207. /**
  208. * Set assigned target groups
  209. *
  210. * @param TargetGroup[]|int[] $targetGroups
  211. */
  212. public function setTargetGroups(array $targetGroups)
  213. {
  214. $ids = array_map(function ($targetGroup) {
  215. if (is_numeric($targetGroup)) {
  216. return (int)$targetGroup;
  217. } elseif ($targetGroup instanceof TargetGroup) {
  218. return $targetGroup->getId();
  219. }
  220. }, $targetGroups);
  221. $ids = array_filter($ids, function ($id) {
  222. return null !== $id && $id > 0;
  223. });
  224. $this->setTargetGroupIds($ids);
  225. }
  226. /**
  227. * Return list of assigned target groups (via properties panel)
  228. *
  229. * @return TargetGroup[]
  230. */
  231. public function getTargetGroups(): array
  232. {
  233. $ids = explode(',', $this->targetGroupIds);
  234. $targetGroups = array_map(function ($id) {
  235. $id = trim($id);
  236. if (!empty($id)) {
  237. $targetGroup = TargetGroup::getById((int) $id);
  238. if ($targetGroup) {
  239. return $targetGroup;
  240. }
  241. }
  242. }, $ids);
  243. $targetGroups = array_filter($targetGroups);
  244. return $targetGroups;
  245. }
  246. /**
  247. * @return string
  248. */
  249. public function getPreviewImageFilesystemPath()
  250. {
  251. return PIMCORE_SYSTEM_TEMP_DIRECTORY . '/document-page-previews/document-page-screenshot-' . $this->getId() . '@2x.jpg';
  252. }
  253. public function save()
  254. {
  255. $response = parent::save(...func_get_args());
  256. // Dispatch page preview message, if preview is enabled.
  257. $documentsConfig = \Pimcore\Config::getSystemConfiguration('documents');
  258. if ($documentsConfig['generate_preview'] ?? false) {
  259. \Pimcore::getContainer()->get('messenger.bus.pimcore-core')->dispatch(
  260. new GeneratePagePreviewMessage($this->getId(), \Pimcore\Tool::getHostUrl())
  261. );
  262. }
  263. return $response;
  264. }
  265. }