Skip to content

Commit

Permalink
PageTree controller class
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed Oct 4, 2024
1 parent 2ccdc8f commit 0b11dff
Show file tree
Hide file tree
Showing 3 changed files with 353 additions and 73 deletions.
82 changes: 9 additions & 73 deletions config/areas/site/requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Kirby\Cms\App;
use Kirby\Cms\Find;
use Kirby\Panel\Controller\Changes;
use Kirby\Toolkit\I18n;
use Kirby\Panel\Controller\PageTree;

$files = require __DIR__ . '/../files/requests.php';

Expand Down Expand Up @@ -125,86 +125,22 @@
],

// Tree Navigation
// @codeCoverageIgnoreStart
// TODO: move to controller class and add unit tests
'tree' => [
'pattern' => 'site/tree',
'action' => function () {
$kirby = App::instance();
$request = $kirby->request();
$move = $request->get('move');
$move = $move ? Find::parent($move) : null;
$parent = $request->get('parent');

if ($parent === null) {
$site = $kirby->site();
$panel = $site->panel();
$uuid = $site->uuid()?->toString();
$url = $site->url();
$value = $uuid ?? '/';

return [
[
'children' => $panel->url(true),
'disabled' => $move?->isMovableTo($site) === false,
'hasChildren' => true,
'icon' => 'home',
'id' => '/',
'label' => I18n::translate('view.site'),
'open' => false,
'url' => $url,
'uuid' => $uuid,
'value' => $value
]
];
}

$parent = Find::parent($parent);
$pages = [];

foreach ($parent->childrenAndDrafts()->filterBy('isListable', true) as $child) {
$panel = $child->panel();
$uuid = $child->uuid()?->toString();
$url = $child->url();
$value = $uuid ?? $child->id();

$pages[] = [
'children' => $panel->url(true),
'disabled' => $move?->isMovableTo($child) === false,
'hasChildren' => $child->hasChildren() === true || $child->hasDrafts() === true,
'icon' => $panel->image()['icon'] ?? null,
'id' => $child->id(),
'open' => false,
'label' => $child->title()->value(),
'url' => $url,
'uuid' => $uuid,
'value' => $value
];
}

return $pages;
return (new PageTree())->children(
parent: App::instance()->request()->get('parent'),
moving: App::instance()->request()->get('move')
);
}
],
'tree.parents' => [
'pattern' => 'site/tree/parents',
'action' => function () {
$kirby = App::instance();
$request = $kirby->request();
$root = $request->get('root');
$page = $kirby->page($request->get('page'));
$parents = $page?->parents()->flip()->values(
fn ($parent) => $parent->uuid()?->toString() ?? $parent->id()
) ?? [];

// if root is included, add the site as top-level parent
if ($root === 'true') {
array_unshift($parents, $kirby->site()->uuid()?->toString() ?? '/');
}

return [
'data' => $parents
];
return (new PageTree())->parents(
page: App::instance()->request()->get('page'),
includeSite: App::instance()->request()->get('root') === 'true',
);
}
]
// @codeCoverageIgnoreEnd
];
113 changes: 113 additions & 0 deletions src/Panel/Controller/PageTree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace Kirby\Panel\Controller;

use Kirby\Cms\App;
use Kirby\Cms\Find;
use Kirby\Cms\Page;
use Kirby\Cms\Site;
use Kirby\Toolkit\I18n;

/**
* The PageTree controller takes care of the request logic
* for the `k-page-tree` component and similar
*
* @package Kirby Panel
* @author Nico Hoffmann <nico@getkirby.com>
* @link https://getkirby.com
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
*/
class PageTree
{
protected Site $site;

public function __construct(
) {
$this->site = App::instance()->site();
}

/**
* Returns children for the parent as entries
*/
public function children(
string|null $parent = null,
string|null $moving = null
): array {
if ($moving !== null) {
$moving = Find::parent($moving);
}

if ($parent === null) {
return [
$this->entry($this->site, $moving)
];
}

return Find::parent($parent)
->childrenAndDrafts()
->filterBy('isListable', true)
->values(
fn ($child) => $this->entry($child, $moving)
);
}

/**
* Returns the properties to display the site or page
* as an entry in the page tree component
*/
public function entry(
Site|Page $entry,
Page|null $moving = null
): array {
$panel = $entry->panel();
$id = $entry->id() ?? '/';
$uuid = $entry->uuid()?->toString();
$url = $entry->url();
$value = $uuid ?? $id;

return [
'children' => $panel->url(true),
'disabled' => $moving?->isMovableTo($entry) === false,
'hasChildren' =>
$entry->hasChildren() === true ||
$entry->hasDrafts() === true,
'icon' => match (true) {
$entry instanceof Site => 'home',
default => $panel->image()['icon'] ?? null
},
'id' => $id,
'open' => false,
'label' => match (true) {
$entry instanceof Site => I18n::translate('view.site'),
default => $entry->title()->value()
},
'url' => $url,
'uuid' => $uuid,
'value' => $value
];
}

/**
* Returns the UUIDs/ids for all parents of the page
*/
public function parents(
string|null $page = null,
bool $includeSite = false,
): array {
$page = $this->site->page($page);
$parents = $page?->parents()->flip();
$parents = $parents?->values(
fn ($parent) => $parent->uuid()?->toString() ?? $parent->id()
);
$parents ??= [];

if ($includeSite === true) {
array_unshift($parents, $this->site->uuid()?->toString() ?? '/');
}

return [
'data' => $parents
];
}
}
Loading

0 comments on commit 0b11dff

Please sign in to comment.