PageLegacyTrait.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @package Grav\Common\Flex
  5. *
  6. * @copyright Copyright (c) 2015 - 2022 Trilby Media, LLC. All rights reserved.
  7. * @license MIT License; see LICENSE file for details.
  8. */
  9. namespace Grav\Common\Flex\Types\Pages\Traits;
  10. use Grav\Common\Grav;
  11. use Grav\Common\Page\Collection;
  12. use Grav\Common\Page\Interfaces\PageCollectionInterface;
  13. use Grav\Common\Page\Interfaces\PageInterface;
  14. use Grav\Common\Page\Pages;
  15. use Grav\Common\Utils;
  16. use Grav\Framework\Flex\Interfaces\FlexIndexInterface;
  17. use InvalidArgumentException;
  18. use function is_array;
  19. use function is_string;
  20. /**
  21. * Implements PageLegacyInterface.
  22. */
  23. trait PageLegacyTrait
  24. {
  25. /**
  26. * Returns children of this page.
  27. *
  28. * @return FlexIndexInterface|PageCollectionInterface|Collection
  29. */
  30. public function children()
  31. {
  32. if (Utils::isAdminPlugin()) {
  33. return parent::children();
  34. }
  35. /** @var Pages $pages */
  36. $pages = Grav::instance()['pages'];
  37. $path = $this->path() ?? '';
  38. return $pages->children($path);
  39. }
  40. /**
  41. * Check to see if this item is the first in an array of sub-pages.
  42. *
  43. * @return bool True if item is first.
  44. */
  45. public function isFirst(): bool
  46. {
  47. if (Utils::isAdminPlugin()) {
  48. return parent::isFirst();
  49. }
  50. $path = $this->path();
  51. $parent = $this->parent();
  52. $collection = $parent ? $parent->collection('content', false) : null;
  53. if (null !== $path && $collection instanceof PageCollectionInterface) {
  54. return $collection->isFirst($path);
  55. }
  56. return true;
  57. }
  58. /**
  59. * Check to see if this item is the last in an array of sub-pages.
  60. *
  61. * @return bool True if item is last
  62. */
  63. public function isLast(): bool
  64. {
  65. if (Utils::isAdminPlugin()) {
  66. return parent::isLast();
  67. }
  68. $path = $this->path();
  69. $parent = $this->parent();
  70. $collection = $parent ? $parent->collection('content', false) : null;
  71. if (null !== $path && $collection instanceof PageCollectionInterface) {
  72. return $collection->isLast($path);
  73. }
  74. return true;
  75. }
  76. /**
  77. * Returns the adjacent sibling based on a direction.
  78. *
  79. * @param int $direction either -1 or +1
  80. * @return PageInterface|false the sibling page
  81. */
  82. public function adjacentSibling($direction = 1)
  83. {
  84. if (Utils::isAdminPlugin()) {
  85. return parent::adjacentSibling($direction);
  86. }
  87. $path = $this->path();
  88. $parent = $this->parent();
  89. $collection = $parent ? $parent->collection('content', false) : null;
  90. if (null !== $path && $collection instanceof PageCollectionInterface) {
  91. $child = $collection->adjacentSibling($path, $direction);
  92. if ($child instanceof PageInterface) {
  93. return $child;
  94. }
  95. }
  96. return false;
  97. }
  98. /**
  99. * Helper method to return an ancestor page.
  100. *
  101. * @param string|null $lookup Name of the parent folder
  102. * @return PageInterface|null page you were looking for if it exists
  103. */
  104. public function ancestor($lookup = null)
  105. {
  106. if (Utils::isAdminPlugin()) {
  107. return parent::ancestor($lookup);
  108. }
  109. /** @var Pages $pages */
  110. $pages = Grav::instance()['pages'];
  111. return $pages->ancestor($this->getProperty('parent_route'), $lookup);
  112. }
  113. /**
  114. * Method that contains shared logic for inherited() and inheritedField()
  115. *
  116. * @param string $field Name of the parent folder
  117. * @return array
  118. */
  119. protected function getInheritedParams($field): array
  120. {
  121. if (Utils::isAdminPlugin()) {
  122. return parent::getInheritedParams($field);
  123. }
  124. /** @var Pages $pages */
  125. $pages = Grav::instance()['pages'];
  126. $inherited = $pages->inherited($this->getProperty('parent_route'), $field);
  127. $inheritedParams = $inherited ? (array)$inherited->value('header.' . $field) : [];
  128. $currentParams = (array)$this->getFormValue('header.' . $field);
  129. if ($inheritedParams && is_array($inheritedParams)) {
  130. $currentParams = array_replace_recursive($inheritedParams, $currentParams);
  131. }
  132. return [$inherited, $currentParams];
  133. }
  134. /**
  135. * Helper method to return a page.
  136. *
  137. * @param string $url the url of the page
  138. * @param bool $all
  139. * @return PageInterface|null page you were looking for if it exists
  140. */
  141. public function find($url, $all = false)
  142. {
  143. if (Utils::isAdminPlugin()) {
  144. return parent::find($url, $all);
  145. }
  146. /** @var Pages $pages */
  147. $pages = Grav::instance()['pages'];
  148. return $pages->find($url, $all);
  149. }
  150. /**
  151. * Get a collection of pages in the current context.
  152. *
  153. * @param string|array $params
  154. * @param bool $pagination
  155. * @return PageCollectionInterface|Collection
  156. * @throws InvalidArgumentException
  157. */
  158. public function collection($params = 'content', $pagination = true)
  159. {
  160. if (Utils::isAdminPlugin()) {
  161. return parent::collection($params, $pagination);
  162. }
  163. if (is_string($params)) {
  164. // Look into a page header field.
  165. $params = (array)$this->getFormValue('header.' . $params);
  166. } elseif (!is_array($params)) {
  167. throw new InvalidArgumentException('Argument should be either header variable name or array of parameters');
  168. }
  169. $context = [
  170. 'pagination' => $pagination,
  171. 'self' => $this
  172. ];
  173. /** @var Pages $pages */
  174. $pages = Grav::instance()['pages'];
  175. return $pages->getCollection($params, $context);
  176. }
  177. /**
  178. * @param string|array $value
  179. * @param bool $only_published
  180. * @return PageCollectionInterface|Collection
  181. */
  182. public function evaluate($value, $only_published = true)
  183. {
  184. if (Utils::isAdminPlugin()) {
  185. return parent::collection($value, $only_published);
  186. }
  187. $params = [
  188. 'items' => $value,
  189. 'published' => $only_published
  190. ];
  191. $context = [
  192. 'event' => false,
  193. 'pagination' => false,
  194. 'url_taxonomy_filters' => false,
  195. 'self' => $this
  196. ];
  197. /** @var Pages $pages */
  198. $pages = Grav::instance()['pages'];
  199. return $pages->getCollection($params, $context);
  200. }
  201. }