vendor/pimcore/pimcore/models/Document/Editable/Snippet.php line 29

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\Editable;
  15. use Pimcore\Cache;
  16. use Pimcore\Document\Editable\EditableHandler;
  17. use Pimcore\Model;
  18. use Pimcore\Model\Document;
  19. use Pimcore\Model\Site;
  20. use Pimcore\Targeting\Document\DocumentTargetingConfigurator;
  21. use Pimcore\Tool\DeviceDetector;
  22. /**
  23. * @method \Pimcore\Model\Document\Editable\Dao getDao()
  24. */
  25. class Snippet extends Model\Document\Editable implements IdRewriterInterface, EditmodeDataInterface, LazyLoadingInterface
  26. {
  27. /**
  28. * Contains the ID of the linked snippet
  29. *
  30. * @internal
  31. *
  32. * @var int|null
  33. */
  34. protected ?int $id = null;
  35. /**
  36. * Contains the object for the snippet
  37. *
  38. * @internal
  39. *
  40. * @var Document\Snippet|Model\Element\ElementDescriptor|null
  41. */
  42. protected $snippet = null;
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getType()
  47. {
  48. return 'snippet';
  49. }
  50. /**
  51. * @return mixed
  52. */
  53. public function getData()
  54. {
  55. return $this->id;
  56. }
  57. /**
  58. * @param int $id
  59. */
  60. public function setId($id)
  61. {
  62. $this->id = $id;
  63. }
  64. /**
  65. * @return int
  66. */
  67. public function getId()
  68. {
  69. return (int) $this->id;
  70. }
  71. /**
  72. * @return array|null
  73. */
  74. public function getDataEditmode() /** : mixed */
  75. {
  76. if ($this->snippet instanceof Document\Snippet) {
  77. return [
  78. 'id' => $this->id,
  79. 'path' => $this->snippet->getFullPath(),
  80. ];
  81. }
  82. return null;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function frontend()
  88. {
  89. // TODO inject services via DI when editables are built through container
  90. $container = \Pimcore::getContainer();
  91. $editableHandler = $container->get(EditableHandler::class);
  92. $targetingConfigurator = $container->get(DocumentTargetingConfigurator::class);
  93. if (!$this->snippet instanceof Document\Snippet) {
  94. return '';
  95. }
  96. if (!$this->snippet->isPublished()) {
  97. return '';
  98. }
  99. // apply best matching target group (if any)
  100. $targetingConfigurator->configureTargetGroup($this->snippet);
  101. $params = $this->config;
  102. $params['document'] = $this->snippet;
  103. // check if output-cache is enabled, if so, we're also using the cache here
  104. $cacheKey = null;
  105. $cacheConfig = \Pimcore\Tool\Frontend::isOutputCacheEnabled();
  106. if ((isset($params['cache']) && $params['cache'] === true) || $cacheConfig) {
  107. // cleanup params to avoid serializing Element\ElementInterface objects
  108. $cacheParams = $params;
  109. array_walk($cacheParams, function (&$value, $key) {
  110. if ($value instanceof Model\Element\ElementInterface) {
  111. $value = $value->getId();
  112. }
  113. });
  114. // TODO is this enough for cache or should we disable caching completely?
  115. if ($this->snippet->getUseTargetGroup()) {
  116. $cacheParams['target_group'] = $this->snippet->getUseTargetGroup();
  117. }
  118. if (Site::isSiteRequest()) {
  119. $cacheParams['siteId'] = Site::getCurrentSite()->getId();
  120. }
  121. $cacheKey = 'editable_snippet__' . md5(serialize($cacheParams));
  122. if ($content = Cache::load($cacheKey)) {
  123. return $content;
  124. }
  125. }
  126. $content = $editableHandler->renderAction($this->snippet->getController(), $params);
  127. // write contents to the cache, if output-cache is enabled
  128. if ($cacheConfig && !DeviceDetector::getInstance()->wasUsed()) {
  129. $cacheTags = ['output_inline'];
  130. $cacheTags[] = $cacheConfig['lifetime'] ? 'output_lifetime' : 'output';
  131. Cache::save($content, $cacheKey, $cacheTags, $cacheConfig['lifetime']);
  132. } elseif (isset($params['cache']) && $params['cache'] === true) {
  133. Cache::save($content, $cacheKey, ['output']);
  134. }
  135. return $content;
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function setDataFromResource($data)
  141. {
  142. if ((int)$data > 0) {
  143. $this->id = $data;
  144. $this->snippet = Document\Snippet::getById($this->id);
  145. }
  146. return $this;
  147. }
  148. /**
  149. * {@inheritdoc}
  150. */
  151. public function setDataFromEditmode($data)
  152. {
  153. if ((int)$data > 0) {
  154. $this->id = $data;
  155. $this->snippet = Document\Snippet::getById($this->id);
  156. }
  157. return $this;
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function isEmpty()
  163. {
  164. $this->load();
  165. if ($this->snippet instanceof Document\Snippet) {
  166. return false;
  167. }
  168. return true;
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function resolveDependencies()
  174. {
  175. $dependencies = [];
  176. if ($this->snippet instanceof Document\Snippet) {
  177. $key = 'document_' . $this->snippet->getId();
  178. $dependencies[$key] = [
  179. 'id' => $this->snippet->getId(),
  180. 'type' => 'document',
  181. ];
  182. }
  183. return $dependencies;
  184. }
  185. /**
  186. * {@inheritdoc}
  187. */
  188. public function __sleep()
  189. {
  190. $finalVars = [];
  191. $parentVars = parent::__sleep();
  192. $blockedVars = ['snippet'];
  193. foreach ($parentVars as $key) {
  194. if (!in_array($key, $blockedVars)) {
  195. $finalVars[] = $key;
  196. }
  197. }
  198. return $finalVars;
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. public function load() /** : void */
  204. {
  205. if (!$this->snippet && $this->id) {
  206. $this->snippet = Document\Snippet::getById($this->id);
  207. }
  208. }
  209. /**
  210. * {@inheritdoc}
  211. */
  212. public function rewriteIds($idMapping) /** : void */
  213. {
  214. $id = $this->getId();
  215. if (array_key_exists('document', $idMapping) && array_key_exists($id, $idMapping['document'])) {
  216. $this->id = $idMapping['document'][$id];
  217. }
  218. }
  219. /**
  220. * @param Document\Snippet $snippet
  221. */
  222. public function setSnippet($snippet)
  223. {
  224. if ($snippet instanceof Document\Snippet) {
  225. $this->id = $snippet->getId();
  226. $this->snippet = $snippet;
  227. }
  228. }
  229. /**
  230. * @return Document\Snippet|null
  231. */
  232. public function getSnippet()
  233. {
  234. $this->load();
  235. return $this->snippet;
  236. }
  237. }