PageRoutableTrait.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\Interfaces\PageCollectionInterface;
  12. use Grav\Common\Page\Interfaces\PageInterface;
  13. use Grav\Common\Page\Pages;
  14. use Grav\Common\Uri;
  15. use Grav\Common\Utils;
  16. use Grav\Framework\Filesystem\Filesystem;
  17. use RuntimeException;
  18. /**
  19. * Implements PageRoutableInterface.
  20. */
  21. trait PageRoutableTrait
  22. {
  23. /**
  24. * Gets and Sets the parent object for this page
  25. *
  26. * @param PageInterface|null $var the parent page object
  27. * @return PageInterface|null the parent page object if it exists.
  28. */
  29. public function parent(PageInterface $var = null)
  30. {
  31. if (Utils::isAdminPlugin()) {
  32. return parent::parent();
  33. }
  34. if (null !== $var) {
  35. throw new RuntimeException('Not Implemented');
  36. }
  37. if ($this->root()) {
  38. return null;
  39. }
  40. /** @var Pages $pages */
  41. $pages = Grav::instance()['pages'];
  42. $filesystem = Filesystem::getInstance(false);
  43. // FIXME: this does not work, needs to use $pages->get() with cached parent id!
  44. $key = $this->getKey();
  45. $parent_route = $filesystem->dirname('/' . $key);
  46. return $parent_route !== '/' ? $pages->find($parent_route) : $pages->root();
  47. }
  48. /**
  49. * Returns the item in the current position.
  50. *
  51. * @return int|null the index of the current page.
  52. */
  53. public function currentPosition(): ?int
  54. {
  55. $path = $this->path();
  56. $parent = $this->parent();
  57. $collection = $parent ? $parent->collection('content', false) : null;
  58. if (null !== $path && $collection instanceof PageCollectionInterface) {
  59. return $collection->currentPosition($path);
  60. }
  61. return 1;
  62. }
  63. /**
  64. * Returns whether or not this page is the currently active page requested via the URL.
  65. *
  66. * @return bool True if it is active
  67. */
  68. public function active(): bool
  69. {
  70. $grav = Grav::instance();
  71. $uri_path = rtrim(urldecode($grav['uri']->path()), '/') ?: '/';
  72. $routes = $grav['pages']->routes();
  73. return isset($routes[$uri_path]) && $routes[$uri_path] === $this->path();
  74. }
  75. /**
  76. * Returns whether or not this URI's URL contains the URL of the active page.
  77. * Or in other words, is this page's URL in the current URL
  78. *
  79. * @return bool True if active child exists
  80. */
  81. public function activeChild(): bool
  82. {
  83. $grav = Grav::instance();
  84. /** @var Uri $uri */
  85. $uri = $grav['uri'];
  86. /** @var Pages $pages */
  87. $pages = $grav['pages'];
  88. $uri_path = rtrim(urldecode($uri->path()), '/');
  89. $routes = $pages->routes();
  90. if (isset($routes[$uri_path])) {
  91. $page = $pages->find($uri->route());
  92. /** @var PageInterface|null $child_page */
  93. $child_page = $page ? $page->parent() : null;
  94. while ($child_page && !$child_page->root()) {
  95. if ($this->path() === $child_page->path()) {
  96. return true;
  97. }
  98. $child_page = $child_page->parent();
  99. }
  100. }
  101. return false;
  102. }
  103. }