PageIndex.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @package Grav\Common\Flex
  5. *
  6. * @copyright Copyright (c) 2015 - 2021 Trilby Media, LLC. All rights reserved.
  7. * @license MIT License; see LICENSE file for details.
  8. */
  9. namespace Grav\Common\Flex\Types\Pages;
  10. use Exception;
  11. use Grav\Common\Debugger;
  12. use Grav\Common\File\CompiledJsonFile;
  13. use Grav\Common\File\CompiledYamlFile;
  14. use Grav\Common\Flex\Traits\FlexGravTrait;
  15. use Grav\Common\Flex\Traits\FlexIndexTrait;
  16. use Grav\Common\Grav;
  17. use Grav\Common\Page\Header;
  18. use Grav\Common\Page\Interfaces\PageCollectionInterface;
  19. use Grav\Common\Page\Interfaces\PageInterface;
  20. use Grav\Common\User\Interfaces\UserInterface;
  21. use Grav\Common\Utils;
  22. use Grav\Framework\Flex\FlexDirectory;
  23. use Grav\Framework\Flex\Interfaces\FlexStorageInterface;
  24. use Grav\Framework\Flex\Pages\FlexPageIndex;
  25. use InvalidArgumentException;
  26. use RuntimeException;
  27. use function array_slice;
  28. use function count;
  29. use function in_array;
  30. use function is_array;
  31. use function is_string;
  32. /**
  33. * Class GravPageObject
  34. * @package Grav\Plugin\FlexObjects\Types\GravPages
  35. *
  36. * @extends FlexPageIndex<string,PageObject,PageCollection>
  37. *
  38. * @method PageIndex withModules(bool $bool = true)
  39. * @method PageIndex withPages(bool $bool = true)
  40. * @method PageIndex withTranslation(bool $bool = true, string $languageCode = null, bool $fallback = null)
  41. */
  42. class PageIndex extends FlexPageIndex implements PageCollectionInterface
  43. {
  44. use FlexGravTrait;
  45. use FlexIndexTrait;
  46. public const VERSION = parent::VERSION . '.5';
  47. public const ORDER_LIST_REGEX = '/(\/\d+)\.[^\/]+/u';
  48. public const PAGE_ROUTE_REGEX = '/\/\d+\./u';
  49. /** @var PageObject|array */
  50. protected $_root;
  51. /** @var array|null */
  52. protected $_params;
  53. /**
  54. * @param array $entries
  55. * @param FlexDirectory|null $directory
  56. */
  57. public function __construct(array $entries = [], FlexDirectory $directory = null)
  58. {
  59. // Remove root if it's taken.
  60. if (isset($entries[''])) {
  61. $this->_root = $entries[''];
  62. unset($entries['']);
  63. }
  64. parent::__construct($entries, $directory);
  65. }
  66. /**
  67. * @param FlexStorageInterface $storage
  68. * @return array
  69. */
  70. public static function loadEntriesFromStorage(FlexStorageInterface $storage): array
  71. {
  72. // Load saved index.
  73. $index = static::loadIndex($storage);
  74. $version = $index['version'] ?? 0;
  75. $force = static::VERSION !== $version;
  76. // TODO: Following check flex index to be out of sync after some saves, disabled until better solution is found.
  77. //$timestamp = $index['timestamp'] ?? 0;
  78. //if (!$force && $timestamp && $timestamp > time() - 1) {
  79. // return $index['index'];
  80. //}
  81. // Load up to date index.
  82. $entries = parent::loadEntriesFromStorage($storage);
  83. return static::updateIndexFile($storage, $index['index'], $entries, ['include_missing' => true, 'force_update' => $force]);
  84. }
  85. /**
  86. * @param string $key
  87. * @return PageObject|null
  88. */
  89. public function get($key)
  90. {
  91. if (mb_strpos($key, '|') !== false) {
  92. [$key, $params] = explode('|', $key, 2);
  93. }
  94. $element = parent::get($key);
  95. if (isset($params)) {
  96. $element = $element->getTranslation(ltrim($params, '.'));
  97. }
  98. return $element;
  99. }
  100. /**
  101. * @return PageObject
  102. */
  103. public function getRoot()
  104. {
  105. $root = $this->_root;
  106. if (is_array($root)) {
  107. $directory = $this->getFlexDirectory();
  108. $storage = $directory->getStorage();
  109. $defaults = [
  110. 'header' => [
  111. 'routable' => false,
  112. 'permissions' => [
  113. 'inherit' => false
  114. ]
  115. ]
  116. ];
  117. $row = $storage->readRows(['' => null])[''] ?? null;
  118. if (null !== $row) {
  119. if (isset($row['__ERROR'])) {
  120. /** @var Debugger $debugger */
  121. $debugger = Grav::instance()['debugger'];
  122. $message = sprintf('Flex Pages: root page is broken in storage: %s', $row['__ERROR']);
  123. $debugger->addException(new RuntimeException($message));
  124. $debugger->addMessage($message, 'error');
  125. $row = ['__META' => $root];
  126. }
  127. } else {
  128. $row = ['__META' => $root];
  129. }
  130. $row = array_merge_recursive($defaults, $row);
  131. /** @var PageObject $root */
  132. $root = $this->getFlexDirectory()->createObject($row, '/', false);
  133. $root->name('root.md');
  134. $root->root(true);
  135. $this->_root = $root;
  136. }
  137. return $root;
  138. }
  139. /**
  140. * Get the collection params
  141. *
  142. * @return array
  143. */
  144. public function getParams(): array
  145. {
  146. return $this->_params ?? [];
  147. }
  148. /**
  149. * Set parameters to the Collection
  150. *
  151. * @param array $params
  152. * @return $this
  153. */
  154. public function setParams(array $params)
  155. {
  156. $this->_params = $this->_params ? array_merge($this->_params, $params) : $params;
  157. return $this;
  158. }
  159. /**
  160. * Get the collection params
  161. *
  162. * @return array
  163. */
  164. public function params(): array
  165. {
  166. return $this->getParams();
  167. }
  168. /**
  169. * Filter pages by given filters.
  170. *
  171. * - search: string
  172. * - page_type: string|string[]
  173. * - modular: bool
  174. * - visible: bool
  175. * - routable: bool
  176. * - published: bool
  177. * - page: bool
  178. * - translated: bool
  179. *
  180. * @param array $filters
  181. * @param bool $recursive
  182. * @return static
  183. */
  184. public function filterBy(array $filters, bool $recursive = false)
  185. {
  186. if (!$filters) {
  187. return $this;
  188. }
  189. if ($recursive) {
  190. return $this->__call('filterBy', [$filters, true]);
  191. }
  192. $list = [];
  193. $index = $this;
  194. foreach ($filters as $key => $value) {
  195. switch ($key) {
  196. case 'search':
  197. $index = $index->search((string)$value);
  198. break;
  199. case 'page_type':
  200. if (!is_array($value)) {
  201. $value = is_string($value) && $value !== '' ? explode(',', $value) : [];
  202. }
  203. $index = $index->ofOneOfTheseTypes($value);
  204. break;
  205. case 'routable':
  206. $index = $index->withRoutable((bool)$value);
  207. break;
  208. case 'published':
  209. $index = $index->withPublished((bool)$value);
  210. break;
  211. case 'visible':
  212. $index = $index->withVisible((bool)$value);
  213. break;
  214. case 'module':
  215. $index = $index->withModules((bool)$value);
  216. break;
  217. case 'page':
  218. $index = $index->withPages((bool)$value);
  219. break;
  220. case 'folder':
  221. $index = $index->withPages(!$value);
  222. break;
  223. case 'translated':
  224. $index = $index->withTranslation((bool)$value);
  225. break;
  226. default:
  227. $list[$key] = $value;
  228. }
  229. }
  230. return $list ? $index->filterByParent($list) : $index;
  231. }
  232. /**
  233. * @param array $filters
  234. * @return static
  235. */
  236. protected function filterByParent(array $filters)
  237. {
  238. return parent::filterBy($filters);
  239. }
  240. /**
  241. * @param array $options
  242. * @return array
  243. */
  244. public function getLevelListing(array $options): array
  245. {
  246. // Undocumented B/C
  247. $order = $options['order'] ?? 'asc';
  248. if ($order === SORT_ASC) {
  249. $options['order'] = 'asc';
  250. } elseif ($order === SORT_DESC) {
  251. $options['order'] = 'desc';
  252. }
  253. $options += [
  254. 'field' => null,
  255. 'route' => null,
  256. 'leaf_route' => null,
  257. 'sortby' => null,
  258. 'order' => 'asc',
  259. 'lang' => null,
  260. 'filters' => [],
  261. ];
  262. $options['filters'] += [
  263. 'type' => ['root', 'dir'],
  264. ];
  265. $key = 'page.idx.lev.' . sha1(json_encode($options, JSON_THROW_ON_ERROR) . $this->getCacheKey());
  266. $checksum = $this->getCacheChecksum();
  267. $cache = $this->getCache('object');
  268. /** @var Debugger $debugger */
  269. $debugger = Grav::instance()['debugger'];
  270. $result = null;
  271. try {
  272. $cached = $cache->get($key);
  273. $test = $cached[0] ?? null;
  274. $result = $test === $checksum ? ($cached[1] ?? null) : null;
  275. } catch (\Psr\SimpleCache\InvalidArgumentException $e) {
  276. $debugger->addException($e);
  277. }
  278. try {
  279. if (null === $result) {
  280. $result = $this->getLevelListingRecurse($options);
  281. $cache->set($key, [$checksum, $result]);
  282. }
  283. } catch (\Psr\SimpleCache\InvalidArgumentException $e) {
  284. $debugger->addException($e);
  285. }
  286. return $result;
  287. }
  288. /**
  289. * @param array $entries
  290. * @param string|null $keyField
  291. * @return static
  292. */
  293. protected function createFrom(array $entries, string $keyField = null)
  294. {
  295. /** @var static $index */
  296. $index = parent::createFrom($entries, $keyField);
  297. $index->_root = $this->getRoot();
  298. return $index;
  299. }
  300. /**
  301. * @param array $options
  302. * @return array
  303. */
  304. protected function getLevelListingRecurse(array $options): array
  305. {
  306. $filters = $options['filters'] ?? [];
  307. $field = $options['field'];
  308. $route = $options['route'];
  309. $leaf_route = $options['leaf_route'];
  310. $sortby = $options['sortby'];
  311. $order = $options['order'];
  312. $language = $options['lang'];
  313. $status = 'error';
  314. $msg = null;
  315. $response = [];
  316. $children = null;
  317. $sub_route = null;
  318. $extra = null;
  319. // Handle leaf_route
  320. $leaf = null;
  321. if ($leaf_route && $route !== $leaf_route) {
  322. $nodes = explode('/', $leaf_route);
  323. $sub_route = '/' . implode('/', array_slice($nodes, 1, $options['level']++));
  324. $options['route'] = $sub_route;
  325. [$status,,$leaf,$extra] = $this->getLevelListingRecurse($options);
  326. }
  327. // Handle no route, assume page tree root
  328. if (!$route) {
  329. $page = $this->getRoot();
  330. } else {
  331. $page = $this->get(trim($route, '/'));
  332. }
  333. $path = $page ? $page->path() : null;
  334. if ($field) {
  335. // Get forced filters from the field.
  336. $blueprint = $page ? $page->getBlueprint() : $this->getFlexDirectory()->getBlueprint();
  337. $settings = $blueprint->schema()->getProperty($field);
  338. $filters = array_merge([], $filters, $settings['filters'] ?? []);
  339. }
  340. // Clean up filter.
  341. $filter_type = (array)($filters['type'] ?? []);
  342. unset($filters['type']);
  343. $filters = array_filter($filters, static function($val) { return $val !== null && $val !== ''; });
  344. if ($page) {
  345. if ($page->root() && (!$filter_type || in_array('root', $filter_type, true))) {
  346. if ($field) {
  347. $response[] = [
  348. 'name' => '<root>',
  349. 'value' => '/',
  350. 'item-key' => '',
  351. 'filename' => '.',
  352. 'extension' => '',
  353. 'type' => 'root',
  354. 'modified' => $page->modified(),
  355. 'size' => 0,
  356. 'symlink' => false,
  357. 'has-children' => false
  358. ];
  359. } else {
  360. $response[] = [
  361. 'item-key' => '-root-',
  362. 'icon' => 'root',
  363. 'title' => 'Root', // FIXME
  364. 'route' => [
  365. 'display' => '&lt;root&gt;', // FIXME
  366. 'raw' => '_root',
  367. ],
  368. 'modified' => $page->modified(),
  369. 'extras' => [
  370. 'template' => $page->template(),
  371. //'lang' => null,
  372. //'translated' => null,
  373. 'langs' => [],
  374. 'published' => false,
  375. 'visible' => false,
  376. 'routable' => false,
  377. 'tags' => ['root', 'non-routable'],
  378. 'actions' => ['edit'], // FIXME
  379. ]
  380. ];
  381. }
  382. }
  383. $status = 'success';
  384. $msg = 'PLUGIN_ADMIN.PAGE_ROUTE_FOUND';
  385. /** @var PageIndex $children */
  386. $children = $page->children()->getIndex();
  387. $selectedChildren = $children->filterBy($filters, true);
  388. /** @var Header $header */
  389. $header = $page->header();
  390. if (!$field && $header->get('admin.children_display_order') === 'collection' && ($orderby = $header->get('content.order.by'))) {
  391. // Use custom sorting by page header.
  392. $sortby = $orderby;
  393. $order = $header->get('content.order.dir', $order);
  394. $custom = $header->get('content.order.custom');
  395. }
  396. if ($sortby) {
  397. // Sort children.
  398. $selectedChildren = $selectedChildren->order($sortby, $order, $custom ?? null);
  399. }
  400. /** @var UserInterface|null $user */
  401. $user = Grav::instance()['user'] ?? null;
  402. /** @var PageObject $child */
  403. foreach ($selectedChildren as $child) {
  404. $selected = $child->path() === $extra;
  405. $includeChildren = is_array($leaf) && !empty($leaf) && $selected;
  406. if ($field) {
  407. $child_count = count($child->children());
  408. $payload = [
  409. 'name' => $child->menu(),
  410. 'value' => $child->rawRoute(),
  411. 'item-key' => basename($child->rawRoute() ?? ''),
  412. 'filename' => $child->folder(),
  413. 'extension' => $child->extension(),
  414. 'type' => 'dir',
  415. 'modified' => $child->modified(),
  416. 'size' => $child_count,
  417. 'symlink' => false,
  418. 'has-children' => $child_count > 0
  419. ];
  420. } else {
  421. $lang = $child->findTranslation($language) ?? 'n/a';
  422. /** @var PageObject $child */
  423. $child = $child->getTranslation($language) ?? $child;
  424. // TODO: all these features are independent from each other, we cannot just have one icon/color to catch all.
  425. // TODO: maybe icon by home/modular/page/folder (or even from blueprints) and color by visibility etc..
  426. if ($child->home()) {
  427. $icon = 'home';
  428. } elseif ($child->isModule()) {
  429. $icon = 'modular';
  430. } elseif ($child->visible()) {
  431. $icon = 'visible';
  432. } elseif ($child->isPage()) {
  433. $icon = 'page';
  434. } else {
  435. // TODO: add support
  436. $icon = 'folder';
  437. }
  438. $tags = [
  439. $child->published() ? 'published' : 'non-published',
  440. $child->visible() ? 'visible' : 'non-visible',
  441. $child->routable() ? 'routable' : 'non-routable'
  442. ];
  443. $extras = [
  444. 'template' => $child->template(),
  445. 'lang' => $lang ?: null,
  446. 'translated' => $lang ? $child->hasTranslation($language, false) : null,
  447. 'langs' => $child->getAllLanguages(true) ?: null,
  448. 'published' => $child->published(),
  449. 'published_date' => $this->jsDate($child->publishDate()),
  450. 'unpublished_date' => $this->jsDate($child->unpublishDate()),
  451. 'visible' => $child->visible(),
  452. 'routable' => $child->routable(),
  453. 'tags' => $tags,
  454. 'actions' => $this->getListingActions($child, $user),
  455. ];
  456. $extras = array_filter($extras, static function ($v) {
  457. return $v !== null;
  458. });
  459. $tmp = $child->children()->getIndex();
  460. $child_count = $tmp->count();
  461. $count = $filters ? $tmp->filterBy($filters, true)->count() : null;
  462. $payload = [
  463. 'item-key' => basename($child->rawRoute() ?? $child->getKey()),
  464. 'icon' => $icon,
  465. 'title' => htmlspecialchars($child->menu()),
  466. 'route' => [
  467. 'display' => $child->getRoute()->toString(false) ?: '/',
  468. 'raw' => $child->rawRoute(),
  469. ],
  470. 'modified' => $this->jsDate($child->modified()),
  471. 'child_count' => $child_count ?: null,
  472. 'count' => $count ?? null,
  473. 'filters_hit' => $filters ? ($child->filterBy($filters, false) ?: null) : null,
  474. 'extras' => $extras
  475. ];
  476. $payload = array_filter($payload, static function ($v) {
  477. return $v !== null;
  478. });
  479. }
  480. // Add children if any
  481. if ($includeChildren) {
  482. $payload['children'] = array_values($leaf);
  483. }
  484. $response[] = $payload;
  485. }
  486. } else {
  487. $msg = 'PLUGIN_ADMIN.PAGE_ROUTE_NOT_FOUND';
  488. }
  489. if ($field) {
  490. $temp_array = [];
  491. foreach ($response as $index => $item) {
  492. $temp_array[$item['type']][$index] = $item;
  493. }
  494. $sorted = Utils::sortArrayByArray($temp_array, $filter_type);
  495. $response = Utils::arrayFlatten($sorted);
  496. }
  497. return [$status, $msg ?? 'PLUGIN_ADMIN.NO_ROUTE_PROVIDED', $response, $path];
  498. }
  499. /**
  500. * @param PageObject $object
  501. * @param UserInterface $user
  502. * @return array
  503. */
  504. protected function getListingActions(PageObject $object, UserInterface $user): array
  505. {
  506. $actions = [];
  507. if ($object->isAuthorized('read', null, $user)) {
  508. $actions[] = 'preview';
  509. $actions[] = 'edit';
  510. }
  511. if ($object->isAuthorized('update', null, $user)) {
  512. $actions[] = 'copy';
  513. $actions[] = 'move';
  514. }
  515. if ($object->isAuthorized('delete', null, $user)) {
  516. $actions[] = 'delete';
  517. }
  518. return $actions;
  519. }
  520. /**
  521. * @param FlexStorageInterface $storage
  522. * @return CompiledJsonFile|CompiledYamlFile|null
  523. */
  524. protected static function getIndexFile(FlexStorageInterface $storage)
  525. {
  526. if (!method_exists($storage, 'isIndexed') || !$storage->isIndexed()) {
  527. return null;
  528. }
  529. // Load saved index file.
  530. $grav = Grav::instance();
  531. $locator = $grav['locator'];
  532. $filename = $locator->findResource('user-data://flex/indexes/pages.json', true, true);
  533. return CompiledJsonFile::instance($filename);
  534. }
  535. /**
  536. * @param int|null $timestamp
  537. * @return string|null
  538. */
  539. private function jsDate(int $timestamp = null): ?string
  540. {
  541. if (!$timestamp) {
  542. return null;
  543. }
  544. $config = Grav::instance()['config'];
  545. $dateFormat = $config->get('system.pages.dateformat.long');
  546. return date($dateFormat, $timestamp) ?: null;
  547. }
  548. /**
  549. * Add a single page to a collection
  550. *
  551. * @param PageInterface $page
  552. * @return PageCollection
  553. */
  554. public function addPage(PageInterface $page)
  555. {
  556. return $this->getCollection()->addPage($page);
  557. }
  558. /**
  559. *
  560. * Create a copy of this collection
  561. *
  562. * @return static
  563. */
  564. public function copy()
  565. {
  566. return clone $this;
  567. }
  568. /**
  569. *
  570. * Merge another collection with the current collection
  571. *
  572. * @param PageCollectionInterface $collection
  573. * @return PageCollection
  574. */
  575. public function merge(PageCollectionInterface $collection)
  576. {
  577. return $this->getCollection()->merge($collection);
  578. }
  579. /**
  580. * Intersect another collection with the current collection
  581. *
  582. * @param PageCollectionInterface $collection
  583. * @return PageCollection
  584. */
  585. public function intersect(PageCollectionInterface $collection)
  586. {
  587. return $this->getCollection()->intersect($collection);
  588. }
  589. /**
  590. * Split collection into array of smaller collections.
  591. *
  592. * @param int $size
  593. * @return PageCollection[]
  594. */
  595. public function batch($size)
  596. {
  597. return $this->getCollection()->batch($size);
  598. }
  599. /**
  600. * Remove item from the list.
  601. *
  602. * @param PageInterface|string|null $key
  603. *
  604. * @return $this
  605. * @throws InvalidArgumentException
  606. */
  607. public function remove($key = null)
  608. {
  609. return $this->getCollection()->remove($key);
  610. }
  611. /**
  612. * Reorder collection.
  613. *
  614. * @param string $by
  615. * @param string $dir
  616. * @param array $manual
  617. * @param string $sort_flags
  618. * @return static
  619. */
  620. public function order($by, $dir = 'asc', $manual = null, $sort_flags = null)
  621. {
  622. /** @var PageCollectionInterface $collection */
  623. $collection = $this->__call('order', [$by, $dir, $manual, $sort_flags]);
  624. return $collection;
  625. }
  626. /**
  627. * Check to see if this item is the first in the collection.
  628. *
  629. * @param string $path
  630. * @return bool True if item is first.
  631. */
  632. public function isFirst($path): bool
  633. {
  634. /** @var bool $result */
  635. $result = $this->__call('isFirst', [$path]);
  636. return $result;
  637. }
  638. /**
  639. * Check to see if this item is the last in the collection.
  640. *
  641. * @param string $path
  642. * @return bool True if item is last.
  643. */
  644. public function isLast($path): bool
  645. {
  646. /** @var bool $result */
  647. $result = $this->__call('isLast', [$path]);
  648. return $result;
  649. }
  650. /**
  651. * Gets the previous sibling based on current position.
  652. *
  653. * @param string $path
  654. * @return PageObject|null The previous item.
  655. */
  656. public function prevSibling($path)
  657. {
  658. /** @var PageObject|null $result */
  659. $result = $this->__call('prevSibling', [$path]);
  660. return $result;
  661. }
  662. /**
  663. * Gets the next sibling based on current position.
  664. *
  665. * @param string $path
  666. * @return PageObject|null The next item.
  667. */
  668. public function nextSibling($path)
  669. {
  670. /** @var PageObject|null $result */
  671. $result = $this->__call('nextSibling', [$path]);
  672. return $result;
  673. }
  674. /**
  675. * Returns the adjacent sibling based on a direction.
  676. *
  677. * @param string $path
  678. * @param int $direction either -1 or +1
  679. * @return PageObject|false The sibling item.
  680. */
  681. public function adjacentSibling($path, $direction = 1)
  682. {
  683. /** @var PageObject|false $result */
  684. $result = $this->__call('adjacentSibling', [$path, $direction]);
  685. return $result;
  686. }
  687. /**
  688. * Returns the item in the current position.
  689. *
  690. * @param string $path the path the item
  691. * @return int|null The index of the current page, null if not found.
  692. */
  693. public function currentPosition($path): ?int
  694. {
  695. /** @var int|null $result */
  696. $result = $this->__call('currentPosition', [$path]);
  697. return $result;
  698. }
  699. /**
  700. * Returns the items between a set of date ranges of either the page date field (default) or
  701. * an arbitrary datetime page field where end date is optional
  702. * Dates can be passed in as text that strtotime() can process
  703. * http://php.net/manual/en/function.strtotime.php
  704. *
  705. * @param string $startDate
  706. * @param bool $endDate
  707. * @param string|null $field
  708. * @return static
  709. * @throws Exception
  710. */
  711. public function dateRange($startDate, $endDate = false, $field = null)
  712. {
  713. $collection = $this->__call('dateRange', [$startDate, $endDate, $field]);
  714. return $collection;
  715. }
  716. /**
  717. * Mimicks Pages class.
  718. *
  719. * @return $this
  720. * @deprecated 1.7 Not needed anymore in Flex Pages (does nothing).
  721. */
  722. public function all()
  723. {
  724. return $this;
  725. }
  726. /**
  727. * Creates new collection with only visible pages
  728. *
  729. * @return static The collection with only visible pages
  730. */
  731. public function visible()
  732. {
  733. $collection = $this->__call('visible', []);
  734. return $collection;
  735. }
  736. /**
  737. * Creates new collection with only non-visible pages
  738. *
  739. * @return static The collection with only non-visible pages
  740. */
  741. public function nonVisible()
  742. {
  743. $collection = $this->__call('nonVisible', []);
  744. return $collection;
  745. }
  746. /**
  747. * Creates new collection with only non-modular pages
  748. *
  749. * @return static The collection with only non-modular pages
  750. */
  751. public function pages()
  752. {
  753. $collection = $this->__call('pages', []);
  754. return $collection;
  755. }
  756. /**
  757. * Creates new collection with only modular pages
  758. *
  759. * @return static The collection with only modular pages
  760. */
  761. public function modules()
  762. {
  763. $collection = $this->__call('modules', []);
  764. return $collection;
  765. }
  766. /**
  767. * Creates new collection with only modular pages
  768. *
  769. * @return static The collection with only modular pages
  770. */
  771. public function modular()
  772. {
  773. return $this->modules();
  774. }
  775. /**
  776. * Creates new collection with only non-modular pages
  777. *
  778. * @return static The collection with only non-modular pages
  779. */
  780. public function nonModular()
  781. {
  782. return $this->pages();
  783. }
  784. /**
  785. * Creates new collection with only published pages
  786. *
  787. * @return static The collection with only published pages
  788. */
  789. public function published()
  790. {
  791. $collection = $this->__call('published', []);
  792. return $collection;
  793. }
  794. /**
  795. * Creates new collection with only non-published pages
  796. *
  797. * @return static The collection with only non-published pages
  798. */
  799. public function nonPublished()
  800. {
  801. $collection = $this->__call('nonPublished', []);
  802. return $collection;
  803. }
  804. /**
  805. * Creates new collection with only routable pages
  806. *
  807. * @return static The collection with only routable pages
  808. */
  809. public function routable()
  810. {
  811. $collection = $this->__call('routable', []);
  812. return $collection;
  813. }
  814. /**
  815. * Creates new collection with only non-routable pages
  816. *
  817. * @return static The collection with only non-routable pages
  818. */
  819. public function nonRoutable()
  820. {
  821. $collection = $this->__call('nonRoutable', []);
  822. return $collection;
  823. }
  824. /**
  825. * Creates new collection with only pages of the specified type
  826. *
  827. * @param string $type
  828. * @return static The collection
  829. */
  830. public function ofType($type)
  831. {
  832. $collection = $this->__call('ofType', []);
  833. return $collection;
  834. }
  835. /**
  836. * Creates new collection with only pages of one of the specified types
  837. *
  838. * @param string[] $types
  839. * @return static The collection
  840. */
  841. public function ofOneOfTheseTypes($types)
  842. {
  843. $collection = $this->__call('ofOneOfTheseTypes', []);
  844. return $collection;
  845. }
  846. /**
  847. * Creates new collection with only pages of one of the specified access levels
  848. *
  849. * @param array $accessLevels
  850. * @return static The collection
  851. */
  852. public function ofOneOfTheseAccessLevels($accessLevels)
  853. {
  854. $collection = $this->__call('ofOneOfTheseAccessLevels', []);
  855. return $collection;
  856. }
  857. /**
  858. * Converts collection into an array.
  859. *
  860. * @return array
  861. */
  862. public function toArray()
  863. {
  864. return $this->getCollection()->toArray();
  865. }
  866. /**
  867. * Get the extended version of this Collection with each page keyed by route
  868. *
  869. * @return array
  870. * @throws Exception
  871. */
  872. public function toExtendedArray()
  873. {
  874. return $this->getCollection()->toExtendedArray();
  875. }
  876. }