Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PageTree controller class #6709

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading