FlexPageObject.php 13 KB

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