FlexPageObject.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. <?php
  2. /**
  3. * @package Grav\Framework\Flex
  4. *
  5. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Framework\Flex\Pages;
  9. use DateTime;
  10. use Exception;
  11. use Grav\Common\Debugger;
  12. use Grav\Common\Grav;
  13. use Grav\Common\Page\Interfaces\PageInterface;
  14. use Grav\Common\Page\Traits\PageFormTrait;
  15. use Grav\Common\User\Interfaces\UserCollectionInterface;
  16. use Grav\Framework\Flex\FlexObject;
  17. use Grav\Framework\Flex\Interfaces\FlexObjectInterface;
  18. use Grav\Framework\Flex\Interfaces\FlexTranslateInterface;
  19. use Grav\Framework\Flex\Pages\Traits\PageAuthorsTrait;
  20. use Grav\Framework\Flex\Pages\Traits\PageContentTrait;
  21. use Grav\Framework\Flex\Pages\Traits\PageLegacyTrait;
  22. use Grav\Framework\Flex\Pages\Traits\PageRoutableTrait;
  23. use Grav\Framework\Flex\Pages\Traits\PageTranslateTrait;
  24. use Grav\Framework\Flex\Traits\FlexMediaTrait;
  25. use RuntimeException;
  26. use stdClass;
  27. use function array_key_exists;
  28. use function is_array;
  29. /**
  30. * Class FlexPageObject
  31. * @package Grav\Plugin\FlexObjects\Types\FlexPages
  32. */
  33. class FlexPageObject extends FlexObject implements PageInterface, FlexTranslateInterface
  34. {
  35. use PageAuthorsTrait;
  36. use PageContentTrait;
  37. use PageFormTrait;
  38. use PageLegacyTrait;
  39. use PageRoutableTrait;
  40. use PageTranslateTrait;
  41. use FlexMediaTrait;
  42. public const PAGE_ORDER_REGEX = '/^(\d+)\.(.*)$/u';
  43. public const PAGE_ORDER_PREFIX_REGEX = '/^[0-9]+\./u';
  44. /** @var array|null */
  45. protected $_reorder;
  46. /** @var FlexPageObject|null */
  47. protected $_originalObject;
  48. /**
  49. * Clone page.
  50. */
  51. #[\ReturnTypeWillChange]
  52. public function __clone()
  53. {
  54. parent::__clone();
  55. if (isset($this->header)) {
  56. $this->header = clone($this->header);
  57. }
  58. }
  59. /**
  60. * @return array
  61. */
  62. public static function getCachedMethods(): array
  63. {
  64. return [
  65. // Page Content Interface
  66. 'header' => false,
  67. 'summary' => true,
  68. 'content' => true,
  69. 'value' => false,
  70. 'media' => false,
  71. 'title' => true,
  72. 'menu' => true,
  73. 'visible' => true,
  74. 'published' => true,
  75. 'publishDate' => true,
  76. 'unpublishDate' => true,
  77. 'process' => true,
  78. 'slug' => true,
  79. 'order' => true,
  80. 'id' => true,
  81. 'modified' => true,
  82. 'lastModified' => true,
  83. 'folder' => true,
  84. 'date' => true,
  85. 'dateformat' => true,
  86. 'taxonomy' => true,
  87. 'shouldProcess' => true,
  88. 'isPage' => true,
  89. 'isDir' => true,
  90. 'folderExists' => true,
  91. // Page
  92. 'isPublished' => true,
  93. 'isOrdered' => true,
  94. 'isVisible' => true,
  95. 'isRoutable' => true,
  96. 'getCreated_Timestamp' => true,
  97. 'getPublish_Timestamp' => true,
  98. 'getUnpublish_Timestamp' => true,
  99. 'getUpdated_Timestamp' => true,
  100. ] + parent::getCachedMethods();
  101. }
  102. /**
  103. * @param bool $test
  104. * @return bool
  105. */
  106. public function isPublished(bool $test = true): bool
  107. {
  108. $time = time();
  109. $start = $this->getPublish_Timestamp();
  110. $stop = $this->getUnpublish_Timestamp();
  111. return $this->published() && $start <= $time && (!$stop || $time <= $stop) === $test;
  112. }
  113. /**
  114. * @param bool $test
  115. * @return bool
  116. */
  117. public function isOrdered(bool $test = true): bool
  118. {
  119. return ($this->order() !== false) === $test;
  120. }
  121. /**
  122. * @param bool $test
  123. * @return bool
  124. */
  125. public function isVisible(bool $test = true): bool
  126. {
  127. return $this->visible() === $test;
  128. }
  129. /**
  130. * @param bool $test
  131. * @return bool
  132. */
  133. public function isRoutable(bool $test = true): bool
  134. {
  135. return $this->routable() === $test;
  136. }
  137. /**
  138. * @return int
  139. */
  140. public function getCreated_Timestamp(): int
  141. {
  142. return $this->getFieldTimestamp('created_date') ?? 0;
  143. }
  144. /**
  145. * @return int
  146. */
  147. public function getPublish_Timestamp(): int
  148. {
  149. return $this->getFieldTimestamp('publish_date') ?? $this->getCreated_Timestamp();
  150. }
  151. /**
  152. * @return int|null
  153. */
  154. public function getUnpublish_Timestamp(): ?int
  155. {
  156. return $this->getFieldTimestamp('unpublish_date');
  157. }
  158. /**
  159. * @return int
  160. */
  161. public function getUpdated_Timestamp(): int
  162. {
  163. return $this->getFieldTimestamp('updated_date') ?? $this->getPublish_Timestamp();
  164. }
  165. /**
  166. * @inheritdoc
  167. */
  168. public function getFormValue(string $name, $default = null, string $separator = null)
  169. {
  170. $test = new stdClass();
  171. $value = $this->pageContentValue($name, $test);
  172. if ($value !== $test) {
  173. return $value;
  174. }
  175. switch ($name) {
  176. case 'name':
  177. return $this->getProperty('template');
  178. case 'route':
  179. return $this->hasKey() ? '/' . $this->getKey() : null;
  180. case 'header.permissions.groups':
  181. $encoded = json_encode($this->getPermissions());
  182. if ($encoded === false) {
  183. throw new RuntimeException('json_encode(): failed to encode group permissions');
  184. }
  185. return json_decode($encoded, true);
  186. }
  187. return parent::getFormValue($name, $default, $separator);
  188. }
  189. /**
  190. * Get master storage key.
  191. *
  192. * @return string
  193. * @see FlexObjectInterface::getStorageKey()
  194. */
  195. public function getMasterKey(): string
  196. {
  197. $key = (string)($this->storage_key ?? $this->getMetaData()['storage_key'] ?? null);
  198. if (($pos = strpos($key, '|')) !== false) {
  199. $key = substr($key, 0, $pos);
  200. }
  201. return $key;
  202. }
  203. /**
  204. * {@inheritdoc}
  205. * @see FlexObjectInterface::getCacheKey()
  206. */
  207. public function getCacheKey(): string
  208. {
  209. return $this->hasKey() ? $this->getTypePrefix() . $this->getFlexType() . '.' . $this->getKey() . '.' . $this->getLanguage() : '';
  210. }
  211. /**
  212. * @param string|null $key
  213. * @return FlexObjectInterface
  214. */
  215. public function createCopy(string $key = null)
  216. {
  217. $this->copy();
  218. return parent::createCopy($key);
  219. }
  220. /**
  221. * @param array|bool $reorder
  222. * @return FlexObject|FlexObjectInterface
  223. */
  224. public function save($reorder = true)
  225. {
  226. return parent::save();
  227. }
  228. /**
  229. * Gets the Page Unmodified (original) version of the page.
  230. *
  231. * Assumes that object has been cloned before modifying it.
  232. *
  233. * @return FlexPageObject|null The original version of the page.
  234. */
  235. public function getOriginal()
  236. {
  237. return $this->_originalObject;
  238. }
  239. /**
  240. * Store the Page Unmodified (original) version of the page.
  241. *
  242. * Can be called multiple times, only the first call matters.
  243. *
  244. * @return void
  245. */
  246. public function storeOriginal(): void
  247. {
  248. if (null === $this->_originalObject) {
  249. $this->_originalObject = clone $this;
  250. }
  251. }
  252. /**
  253. * Get display order for the associated media.
  254. *
  255. * @return array
  256. */
  257. public function getMediaOrder(): array
  258. {
  259. $order = $this->getNestedProperty('header.media_order');
  260. if (is_array($order)) {
  261. return $order;
  262. }
  263. if (!$order) {
  264. return [];
  265. }
  266. return array_map('trim', explode(',', $order));
  267. }
  268. // Overrides for header properties.
  269. /**
  270. * Common logic to load header properties.
  271. *
  272. * @param string $property
  273. * @param mixed $var
  274. * @param callable $filter
  275. * @return mixed|null
  276. */
  277. protected function loadHeaderProperty(string $property, $var, callable $filter)
  278. {
  279. // We have to use parent methods in order to avoid loops.
  280. $value = null === $var ? parent::getProperty($property) : null;
  281. if (null === $value) {
  282. $value = $filter($var ?? $this->getProperty('header')->get($property));
  283. parent::setProperty($property, $value);
  284. if ($this->doHasProperty($property)) {
  285. $value = parent::getProperty($property);
  286. }
  287. }
  288. return $value;
  289. }
  290. /**
  291. * Common logic to load header properties.
  292. *
  293. * @param string $property
  294. * @param mixed $var
  295. * @param callable $filter
  296. * @return mixed|null
  297. */
  298. protected function loadProperty(string $property, $var, callable $filter)
  299. {
  300. // We have to use parent methods in order to avoid loops.
  301. $value = null === $var ? parent::getProperty($property) : null;
  302. if (null === $value) {
  303. $value = $filter($var);
  304. parent::setProperty($property, $value);
  305. if ($this->doHasProperty($property)) {
  306. $value = parent::getProperty($property);
  307. }
  308. }
  309. return $value;
  310. }
  311. /**
  312. * @param string $property
  313. * @param mixed $default
  314. * @return mixed
  315. */
  316. public function getProperty($property, $default = null)
  317. {
  318. $method = static::$headerProperties[$property] ?? static::$calculatedProperties[$property] ?? null;
  319. if ($method && method_exists($this, $method)) {
  320. return $this->{$method}();
  321. }
  322. return parent::getProperty($property, $default);
  323. }
  324. /**
  325. * @param string $property
  326. * @param mixed $value
  327. * @return $this
  328. */
  329. public function setProperty($property, $value)
  330. {
  331. $method = static::$headerProperties[$property] ?? static::$calculatedProperties[$property] ?? null;
  332. if ($method && method_exists($this, $method)) {
  333. $this->{$method}($value);
  334. return $this;
  335. }
  336. parent::setProperty($property, $value);
  337. return $this;
  338. }
  339. /**
  340. * @param string $property
  341. * @param mixed $value
  342. * @param string|null $separator
  343. * @return $this
  344. */
  345. public function setNestedProperty($property, $value, $separator = null)
  346. {
  347. $separator = $separator ?: '.';
  348. if (strpos($property, 'header' . $separator) === 0) {
  349. $this->getProperty('header')->set(str_replace('header' . $separator, '', $property), $value, $separator);
  350. return $this;
  351. }
  352. parent::setNestedProperty($property, $value, $separator);
  353. return $this;
  354. }
  355. /**
  356. * @param string $property
  357. * @param string|null $separator
  358. * @return $this
  359. */
  360. public function unsetNestedProperty($property, $separator = null)
  361. {
  362. $separator = $separator ?: '.';
  363. if (strpos($property, 'header' . $separator) === 0) {
  364. $this->getProperty('header')->undef(str_replace('header' . $separator, '', $property), $separator);
  365. return $this;
  366. }
  367. parent::unsetNestedProperty($property, $separator);
  368. return $this;
  369. }
  370. /**
  371. * @param array $elements
  372. * @param bool $extended
  373. * @return void
  374. */
  375. protected function filterElements(array &$elements, bool $extended = false): void
  376. {
  377. // Markdown storage conversion to page structure.
  378. if (array_key_exists('content', $elements)) {
  379. $elements['markdown'] = $elements['content'];
  380. unset($elements['content']);
  381. }
  382. if (!$extended) {
  383. $folder = !empty($elements['folder']) ? trim($elements['folder']) : '';
  384. if ($folder) {
  385. $order = !empty($elements['order']) ? (int)$elements['order'] : null;
  386. // TODO: broken
  387. $elements['storage_key'] = $order ? sprintf('%02d.%s', $order, $folder) : $folder;
  388. }
  389. }
  390. parent::filterElements($elements);
  391. }
  392. /**
  393. * @param string $field
  394. * @return int|null
  395. */
  396. protected function getFieldTimestamp(string $field): ?int
  397. {
  398. $date = $this->getFieldDateTime($field);
  399. return $date ? $date->getTimestamp() : null;
  400. }
  401. /**
  402. * @param string $field
  403. * @return DateTime|null
  404. */
  405. protected function getFieldDateTime(string $field): ?DateTime
  406. {
  407. try {
  408. $value = $this->getProperty($field);
  409. if (is_numeric($value)) {
  410. $value = '@' . $value;
  411. }
  412. $date = $value ? new DateTime($value) : null;
  413. } catch (Exception $e) {
  414. /** @var Debugger $debugger */
  415. $debugger = Grav::instance()['debugger'];
  416. $debugger->addException($e);
  417. $date = null;
  418. }
  419. return $date;
  420. }
  421. /**
  422. * @return UserCollectionInterface|null
  423. * @internal
  424. */
  425. protected function loadAccounts()
  426. {
  427. return Grav::instance()['accounts'] ?? null;
  428. }
  429. }