init
This commit is contained in:
62
kirby/config/sections/fields.php
Normal file
62
kirby/config/sections/fields.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
use Kirby\Cms\Page;
|
||||
use Kirby\Cms\Site;
|
||||
use Kirby\Form\Form;
|
||||
|
||||
return [
|
||||
'props' => [
|
||||
'fields' => function (array $fields = []) {
|
||||
return $fields;
|
||||
}
|
||||
],
|
||||
'computed' => [
|
||||
'form' => function () {
|
||||
$fields = $this->fields;
|
||||
$disabled = $this->model->permissions()->update() === false;
|
||||
$lang = $this->model->kirby()->languageCode();
|
||||
$content = $this->model->content($lang)->toArray();
|
||||
|
||||
if ($disabled === true) {
|
||||
foreach ($fields as $key => $props) {
|
||||
$fields[$key]['disabled'] = true;
|
||||
}
|
||||
}
|
||||
|
||||
return new Form([
|
||||
'fields' => $fields,
|
||||
'values' => $content,
|
||||
'model' => $this->model,
|
||||
'strict' => true
|
||||
]);
|
||||
},
|
||||
'fields' => function () {
|
||||
$fields = $this->form->fields()->toArray();
|
||||
|
||||
if (
|
||||
$this->model instanceof Page ||
|
||||
$this->model instanceof Site
|
||||
) {
|
||||
// the title should never be updated directly via
|
||||
// fields section to avoid conflicts with the rename dialog
|
||||
unset($fields['title']);
|
||||
}
|
||||
|
||||
foreach ($fields as $index => $props) {
|
||||
unset($fields[$index]['value']);
|
||||
}
|
||||
|
||||
return $fields;
|
||||
}
|
||||
],
|
||||
'methods' => [
|
||||
'errors' => function () {
|
||||
return $this->form->errors();
|
||||
}
|
||||
],
|
||||
'toArray' => function () {
|
||||
return [
|
||||
'fields' => $this->fields,
|
||||
];
|
||||
}
|
||||
];
|
||||
249
kirby/config/sections/files.php
Normal file
249
kirby/config/sections/files.php
Normal file
@@ -0,0 +1,249 @@
|
||||
<?php
|
||||
|
||||
use Kirby\Cms\File;
|
||||
use Kirby\Cms\Files;
|
||||
use Kirby\Toolkit\I18n;
|
||||
|
||||
return [
|
||||
'mixins' => [
|
||||
'details',
|
||||
'empty',
|
||||
'headline',
|
||||
'help',
|
||||
'layout',
|
||||
'min',
|
||||
'max',
|
||||
'pagination',
|
||||
'parent',
|
||||
'search',
|
||||
'sort'
|
||||
],
|
||||
'props' => [
|
||||
/**
|
||||
* Filters pages by a query. Sorting will be disabled
|
||||
*/
|
||||
'query' => function (string|null $query = null) {
|
||||
return $query;
|
||||
},
|
||||
/**
|
||||
* Filters all files by template and also sets the template, which will be used for all uploads
|
||||
*/
|
||||
'template' => function (string $template = null) {
|
||||
return $template;
|
||||
},
|
||||
/**
|
||||
* Setup for the main text in the list or cards. By default this will display the filename.
|
||||
*/
|
||||
'text' => function ($text = '{{ file.filename }}') {
|
||||
return I18n::translate($text, $text);
|
||||
}
|
||||
],
|
||||
'computed' => [
|
||||
'accept' => function () {
|
||||
if ($this->template) {
|
||||
$file = new File([
|
||||
'filename' => 'tmp',
|
||||
'parent' => $this->model(),
|
||||
'template' => $this->template
|
||||
]);
|
||||
|
||||
return $file->blueprint()->acceptAttribute();
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
'parent' => function () {
|
||||
return $this->parentModel();
|
||||
},
|
||||
'models' => function () {
|
||||
if ($this->query !== null) {
|
||||
$files = $this->parent->query($this->query, Files::class) ?? new Files([]);
|
||||
} else {
|
||||
$files = $this->parent->files();
|
||||
}
|
||||
|
||||
// filter files by template
|
||||
$files = $files->template($this->template);
|
||||
|
||||
// filter out all protected and hidden files
|
||||
$files = $files->filter('isListable', true);
|
||||
|
||||
// search
|
||||
if ($this->search === true && empty($this->searchterm()) === false) {
|
||||
$files = $files->search($this->searchterm());
|
||||
|
||||
// disable flip and sortBy while searching
|
||||
// to show most relevant results
|
||||
$this->flip = false;
|
||||
$this->sortBy = null;
|
||||
}
|
||||
|
||||
// sort
|
||||
if ($this->sortBy) {
|
||||
$files = $files->sort(...$files::sortArgs($this->sortBy));
|
||||
} else {
|
||||
$files = $files->sorted();
|
||||
}
|
||||
|
||||
// flip
|
||||
if ($this->flip === true) {
|
||||
$files = $files->flip();
|
||||
}
|
||||
|
||||
// apply the default pagination
|
||||
$files = $files->paginate([
|
||||
'page' => $this->page,
|
||||
'limit' => $this->limit,
|
||||
'method' => 'none' // the page is manually provided
|
||||
]);
|
||||
|
||||
return $files;
|
||||
},
|
||||
'files' => function () {
|
||||
return $this->models;
|
||||
},
|
||||
'data' => function () {
|
||||
$data = [];
|
||||
|
||||
// the drag text needs to be absolute when the files come from
|
||||
// a different parent model
|
||||
$dragTextAbsolute = $this->model->is($this->parent) === false;
|
||||
|
||||
foreach ($this->models as $file) {
|
||||
$panel = $file->panel();
|
||||
|
||||
$item = [
|
||||
'dragText' => $panel->dragText('auto', $dragTextAbsolute),
|
||||
'extension' => $file->extension(),
|
||||
'filename' => $file->filename(),
|
||||
'id' => $file->id(),
|
||||
'image' => $panel->image(
|
||||
$this->image,
|
||||
$this->layout === 'table' ? 'list' : $this->layout
|
||||
),
|
||||
'info' => $file->toSafeString($this->info ?? false),
|
||||
'link' => $panel->url(true),
|
||||
'mime' => $file->mime(),
|
||||
'parent' => $file->parent()->panel()->path(),
|
||||
'template' => $file->template(),
|
||||
'text' => $file->toSafeString($this->text),
|
||||
'url' => $file->url(),
|
||||
];
|
||||
|
||||
if ($this->layout === 'table') {
|
||||
$item = $this->columnsValues($item, $file);
|
||||
}
|
||||
|
||||
$data[] = $item;
|
||||
}
|
||||
|
||||
return $data;
|
||||
},
|
||||
'total' => function () {
|
||||
return $this->models->pagination()->total();
|
||||
},
|
||||
'errors' => function () {
|
||||
$errors = [];
|
||||
|
||||
if ($this->validateMax() === false) {
|
||||
$errors['max'] = I18n::template('error.section.files.max.' . I18n::form($this->max), [
|
||||
'max' => $this->max,
|
||||
'section' => $this->headline
|
||||
]);
|
||||
}
|
||||
|
||||
if ($this->validateMin() === false) {
|
||||
$errors['min'] = I18n::template('error.section.files.min.' . I18n::form($this->min), [
|
||||
'min' => $this->min,
|
||||
'section' => $this->headline
|
||||
]);
|
||||
}
|
||||
|
||||
if (empty($errors) === true) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
$this->name => [
|
||||
'label' => $this->headline,
|
||||
'message' => $errors,
|
||||
]
|
||||
];
|
||||
},
|
||||
'pagination' => function () {
|
||||
return $this->pagination();
|
||||
},
|
||||
'upload' => function () {
|
||||
if ($this->isFull() === true) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// count all uploaded files
|
||||
$max = $this->max ? $this->max - $this->total : null;
|
||||
|
||||
if ($this->max && $this->total === $this->max - 1) {
|
||||
$multiple = false;
|
||||
} else {
|
||||
$multiple = true;
|
||||
}
|
||||
|
||||
$template = $this->template === 'default' ? null : $this->template;
|
||||
|
||||
return [
|
||||
'accept' => $this->accept,
|
||||
'multiple' => $multiple,
|
||||
'max' => $max,
|
||||
'api' => $this->parent->apiUrl(true) . '/files',
|
||||
'attributes' => [
|
||||
// TODO: an edge issue that needs to be solved:
|
||||
// if multiple users load the same section
|
||||
// at the same time and upload a file,
|
||||
// uploaded files have the same sort number
|
||||
'sort' => $this->sortable === true ? $this->total + 1 : null,
|
||||
'template' => $template
|
||||
]
|
||||
];
|
||||
}
|
||||
],
|
||||
// @codeCoverageIgnoreStart
|
||||
'api' => function () {
|
||||
return [
|
||||
[
|
||||
'pattern' => 'sort',
|
||||
'method' => 'PATCH',
|
||||
'action' => function () {
|
||||
$this->section()->model()->files()->changeSort(
|
||||
$this->requestBody('files'),
|
||||
$this->requestBody('index')
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
]
|
||||
];
|
||||
},
|
||||
// @codeCoverageIgnoreEnd
|
||||
'toArray' => function () {
|
||||
return [
|
||||
'data' => $this->data,
|
||||
'errors' => $this->errors,
|
||||
'options' => [
|
||||
'accept' => $this->accept,
|
||||
'apiUrl' => $this->parent->apiUrl(true) . '/sections/' . $this->name,
|
||||
'columns' => $this->columnsWithTypes(),
|
||||
'empty' => $this->empty,
|
||||
'headline' => $this->headline,
|
||||
'help' => $this->help,
|
||||
'layout' => $this->layout,
|
||||
'link' => $this->link(),
|
||||
'max' => $this->max,
|
||||
'min' => $this->min,
|
||||
'search' => $this->search,
|
||||
'size' => $this->size,
|
||||
'sortable' => $this->sortable,
|
||||
'upload' => $this->upload
|
||||
],
|
||||
'pagination' => $this->pagination
|
||||
];
|
||||
}
|
||||
];
|
||||
37
kirby/config/sections/info.php
Normal file
37
kirby/config/sections/info.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
use Kirby\Toolkit\I18n;
|
||||
|
||||
return [
|
||||
'mixins' => [
|
||||
'headline',
|
||||
],
|
||||
'props' => [
|
||||
'icon' => function (string $icon = null) {
|
||||
return $icon;
|
||||
},
|
||||
'text' => function ($text = null) {
|
||||
return I18n::translate($text, $text);
|
||||
},
|
||||
'theme' => function (string $theme = null) {
|
||||
return $theme;
|
||||
}
|
||||
],
|
||||
'computed' => [
|
||||
'text' => function () {
|
||||
if ($this->text) {
|
||||
$text = $this->model()->toSafeString($this->text);
|
||||
$text = $this->kirby()->kirbytext($text);
|
||||
return $text;
|
||||
}
|
||||
},
|
||||
],
|
||||
'toArray' => function () {
|
||||
return [
|
||||
'icon' => $this->icon,
|
||||
'label' => $this->headline,
|
||||
'text' => $this->text,
|
||||
'theme' => $this->theme
|
||||
];
|
||||
}
|
||||
];
|
||||
36
kirby/config/sections/mixins/details.php
Normal file
36
kirby/config/sections/mixins/details.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Kirby\Toolkit\I18n;
|
||||
|
||||
return [
|
||||
'props' => [
|
||||
/**
|
||||
* Image options to control the source and look of preview
|
||||
*/
|
||||
'image' => function ($image = null) {
|
||||
return $image ?? [];
|
||||
},
|
||||
/**
|
||||
* Optional info text setup. Info text is shown on the right (lists, cardlets) or below (cards) the title.
|
||||
*/
|
||||
'info' => function ($info = null) {
|
||||
return I18n::translate($info, $info);
|
||||
},
|
||||
/**
|
||||
* Setup for the main text in the list or cards. By default this will display the title.
|
||||
*/
|
||||
'text' => function ($text = '{{ model.title }}') {
|
||||
return I18n::translate($text, $text);
|
||||
}
|
||||
],
|
||||
'methods' => [
|
||||
'link' => function () {
|
||||
$modelLink = $this->model->panel()->url(true);
|
||||
$parentLink = $this->parent->panel()->url(true);
|
||||
|
||||
if ($modelLink !== $parentLink) {
|
||||
return $parentLink;
|
||||
}
|
||||
}
|
||||
]
|
||||
];
|
||||
21
kirby/config/sections/mixins/empty.php
Normal file
21
kirby/config/sections/mixins/empty.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
use Kirby\Toolkit\I18n;
|
||||
|
||||
return [
|
||||
'props' => [
|
||||
/**
|
||||
* Sets the text for the empty state box
|
||||
*/
|
||||
'empty' => function ($empty = null) {
|
||||
return I18n::translate($empty, $empty);
|
||||
}
|
||||
],
|
||||
'computed' => [
|
||||
'empty' => function () {
|
||||
if ($this->empty) {
|
||||
return $this->model()->toSafeString($this->empty);
|
||||
}
|
||||
}
|
||||
]
|
||||
];
|
||||
36
kirby/config/sections/mixins/headline.php
Normal file
36
kirby/config/sections/mixins/headline.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
use Kirby\Toolkit\I18n;
|
||||
|
||||
return [
|
||||
'props' => [
|
||||
/**
|
||||
* The headline for the section. This can be a simple string or a template with additional info from the parent page.
|
||||
* @deprecated 3.8.0 Use `label` instead
|
||||
*/
|
||||
'headline' => function ($headline = null) {
|
||||
return I18n::translate($headline, $headline);
|
||||
},
|
||||
/**
|
||||
* The label for the section. This can be a simple string or
|
||||
* a template with additional info from the parent page.
|
||||
* Replaces the `headline` prop.
|
||||
*/
|
||||
'label' => function ($label = null) {
|
||||
return I18n::translate($label, $label);
|
||||
}
|
||||
],
|
||||
'computed' => [
|
||||
'headline' => function () {
|
||||
if ($this->label) {
|
||||
return $this->model()->toString($this->label);
|
||||
}
|
||||
|
||||
if ($this->headline) {
|
||||
return $this->model()->toString($this->headline);
|
||||
}
|
||||
|
||||
return ucfirst($this->name);
|
||||
}
|
||||
]
|
||||
];
|
||||
23
kirby/config/sections/mixins/help.php
Normal file
23
kirby/config/sections/mixins/help.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
use Kirby\Toolkit\I18n;
|
||||
|
||||
return [
|
||||
'props' => [
|
||||
/**
|
||||
* Sets the help text
|
||||
*/
|
||||
'help' => function ($help = null) {
|
||||
return I18n::translate($help, $help);
|
||||
}
|
||||
],
|
||||
'computed' => [
|
||||
'help' => function () {
|
||||
if ($this->help) {
|
||||
$help = $this->model()->toSafeString($this->help);
|
||||
$help = $this->kirby()->kirbytext($help);
|
||||
return $help;
|
||||
}
|
||||
}
|
||||
]
|
||||
];
|
||||
155
kirby/config/sections/mixins/layout.php
Normal file
155
kirby/config/sections/mixins/layout.php
Normal file
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
use Kirby\Cms\ModelWithContent;
|
||||
use Kirby\Form\Form;
|
||||
use Kirby\Toolkit\I18n;
|
||||
use Kirby\Toolkit\Str;
|
||||
|
||||
return [
|
||||
'props' => [
|
||||
/**
|
||||
* Columns config for `layout: table`
|
||||
*/
|
||||
'columns' => function (array $columns = null) {
|
||||
return $columns ?? [];
|
||||
},
|
||||
/**
|
||||
* Section layout.
|
||||
* Available layout methods: `list`, `cardlets`, `cards`, `table`.
|
||||
*/
|
||||
'layout' => function (string $layout = 'list') {
|
||||
$layouts = ['list', 'cardlets', 'cards', 'table'];
|
||||
return in_array($layout, $layouts) ? $layout : 'list';
|
||||
},
|
||||
/**
|
||||
* The size option controls the size of cards. By default cards are auto-sized and the cards grid will always fill the full width. With a size you can disable auto-sizing. Available sizes: `tiny`, `small`, `medium`, `large`, `huge`, `full`
|
||||
*/
|
||||
'size' => function (string $size = 'auto') {
|
||||
return $size;
|
||||
},
|
||||
],
|
||||
'computed' => [
|
||||
'columns' => function () {
|
||||
$columns = [];
|
||||
|
||||
if ($this->layout !== 'table') {
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($this->image !== false) {
|
||||
$columns['image'] = [
|
||||
'label' => ' ',
|
||||
'mobile' => true,
|
||||
'type' => 'image',
|
||||
'width' => 'var(--table-row-height)'
|
||||
];
|
||||
}
|
||||
|
||||
if ($this->text) {
|
||||
$columns['title'] = [
|
||||
'label' => I18n::translate('title'),
|
||||
'mobile' => true,
|
||||
'type' => 'url',
|
||||
];
|
||||
}
|
||||
|
||||
if ($this->info) {
|
||||
$columns['info'] = [
|
||||
'label' => I18n::translate('info'),
|
||||
'type' => 'text',
|
||||
];
|
||||
}
|
||||
|
||||
foreach ($this->columns as $columnName => $column) {
|
||||
if ($column === true) {
|
||||
$column = [];
|
||||
}
|
||||
|
||||
if ($column === false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// fallback for labels
|
||||
$column['label'] ??= Str::ucfirst($columnName);
|
||||
|
||||
// make sure to translate labels
|
||||
$column['label'] = I18n::translate($column['label'], $column['label']);
|
||||
|
||||
// keep the original column name as id
|
||||
$column['id'] = $columnName;
|
||||
|
||||
// add the custom column to the array
|
||||
// allowing to extend/overwrite existing columns
|
||||
$columns[$columnName] = [
|
||||
...$columns[$columnName] ?? [],
|
||||
...$column
|
||||
];
|
||||
}
|
||||
|
||||
if ($this->type === 'pages') {
|
||||
$columns['flag'] = [
|
||||
'label' => ' ',
|
||||
'mobile' => true,
|
||||
'type' => 'flag',
|
||||
'width' => 'var(--table-row-height)',
|
||||
];
|
||||
}
|
||||
|
||||
return $columns;
|
||||
},
|
||||
],
|
||||
'methods' => [
|
||||
'columnsWithTypes' => function () {
|
||||
$columns = $this->columns;
|
||||
|
||||
// add the type to the columns for the table layout
|
||||
if ($this->layout === 'table') {
|
||||
$blueprint = $this->models->first()?->blueprint();
|
||||
|
||||
if ($blueprint === null) {
|
||||
return $columns;
|
||||
}
|
||||
|
||||
foreach ($columns as $columnName => $column) {
|
||||
if ($id = $column['id'] ?? null) {
|
||||
$columns[$columnName]['type'] ??= $blueprint->field($id)['type'] ?? null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $columns;
|
||||
},
|
||||
'columnsValues' => function (array $item, ModelWithContent $model) {
|
||||
$item['title'] = [
|
||||
// override toSafeString() coming from `$item`
|
||||
// because the table cells don't use v-html
|
||||
'text' => $model->toString($this->text),
|
||||
'href' => $model->panel()->url(true)
|
||||
];
|
||||
|
||||
if ($this->info) {
|
||||
// override toSafeString() coming from `$item`
|
||||
// because the table cells don't use v-html
|
||||
$item['info'] = $model->toString($this->info);
|
||||
}
|
||||
|
||||
// Use form to get the proper values for the columns
|
||||
$form = Form::for($model)->values();
|
||||
|
||||
foreach ($this->columns as $columnName => $column) {
|
||||
$item[$columnName] = match (empty($column['value'])) {
|
||||
// if column value defined, resolve the query
|
||||
false => $model->toString($column['value']),
|
||||
// otherwise use the form value,
|
||||
// but don't overwrite columns
|
||||
default =>
|
||||
$item[$columnName] ??
|
||||
$form[$column['id'] ?? $columnName] ??
|
||||
null,
|
||||
};
|
||||
}
|
||||
|
||||
return $item;
|
||||
}
|
||||
],
|
||||
];
|
||||
28
kirby/config/sections/mixins/max.php
Normal file
28
kirby/config/sections/mixins/max.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'props' => [
|
||||
/**
|
||||
* Sets the maximum number of allowed entries in the section
|
||||
*/
|
||||
'max' => function (int $max = null) {
|
||||
return $max;
|
||||
}
|
||||
],
|
||||
'methods' => [
|
||||
'isFull' => function () {
|
||||
if ($this->max) {
|
||||
return $this->total >= $this->max;
|
||||
}
|
||||
|
||||
return false;
|
||||
},
|
||||
'validateMax' => function () {
|
||||
if ($this->max && $this->total > $this->max) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
]
|
||||
];
|
||||
21
kirby/config/sections/mixins/min.php
Normal file
21
kirby/config/sections/mixins/min.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'props' => [
|
||||
/**
|
||||
* Sets the minimum number of required entries in the section
|
||||
*/
|
||||
'min' => function (int $min = null) {
|
||||
return $min;
|
||||
}
|
||||
],
|
||||
'methods' => [
|
||||
'validateMin' => function () {
|
||||
if ($this->min && $this->min > $this->total) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
]
|
||||
];
|
||||
37
kirby/config/sections/mixins/pagination.php
Normal file
37
kirby/config/sections/mixins/pagination.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Toolkit\Pagination;
|
||||
|
||||
return [
|
||||
'props' => [
|
||||
/**
|
||||
* Sets the number of items per page. If there are more items the pagination navigation will be shown at the bottom of the section.
|
||||
*/
|
||||
'limit' => function (int $limit = 20) {
|
||||
return $limit;
|
||||
},
|
||||
/**
|
||||
* Sets the default page for the pagination. This will overwrite default pagination.
|
||||
*/
|
||||
'page' => function (int $page = null) {
|
||||
return App::instance()->request()->get('page', $page);
|
||||
},
|
||||
],
|
||||
'methods' => [
|
||||
'pagination' => function () {
|
||||
$pagination = new Pagination([
|
||||
'limit' => $this->limit,
|
||||
'page' => $this->page,
|
||||
'total' => $this->total
|
||||
]);
|
||||
|
||||
return [
|
||||
'limit' => $pagination->limit(),
|
||||
'offset' => $pagination->offset(),
|
||||
'page' => $pagination->page(),
|
||||
'total' => $pagination->total(),
|
||||
];
|
||||
}
|
||||
]
|
||||
];
|
||||
47
kirby/config/sections/mixins/parent.php
Normal file
47
kirby/config/sections/mixins/parent.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
use Kirby\Cms\File;
|
||||
use Kirby\Cms\Page;
|
||||
use Kirby\Cms\Site;
|
||||
use Kirby\Cms\User;
|
||||
use Kirby\Exception\Exception;
|
||||
|
||||
return [
|
||||
'props' => [
|
||||
/**
|
||||
* Sets the query to a parent to find items for the list
|
||||
*/
|
||||
'parent' => function (string $parent = null) {
|
||||
return $parent;
|
||||
}
|
||||
],
|
||||
'methods' => [
|
||||
'parentModel' => function () {
|
||||
$parent = $this->parent;
|
||||
|
||||
if (is_string($parent) === true) {
|
||||
$query = $parent;
|
||||
$parent = $this->model->query($query);
|
||||
|
||||
if (!$parent) {
|
||||
throw new Exception('The parent for the query "' . $query . '" cannot be found in the section "' . $this->name() . '"');
|
||||
}
|
||||
|
||||
if (
|
||||
$parent instanceof Page === false &&
|
||||
$parent instanceof Site === false &&
|
||||
$parent instanceof File === false &&
|
||||
$parent instanceof User === false
|
||||
) {
|
||||
throw new Exception('The parent for the section "' . $this->name() . '" has to be a page, site or user object');
|
||||
}
|
||||
}
|
||||
|
||||
if ($parent === null) {
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
return $parent;
|
||||
}
|
||||
]
|
||||
];
|
||||
19
kirby/config/sections/mixins/search.php
Normal file
19
kirby/config/sections/mixins/search.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
use Kirby\Cms\App;
|
||||
|
||||
return [
|
||||
'props' => [
|
||||
/**
|
||||
* Enable/disable the search in the sections
|
||||
*/
|
||||
'search' => function (bool $search = false): bool {
|
||||
return $search;
|
||||
}
|
||||
],
|
||||
'methods' => [
|
||||
'searchterm' => function (): string|null {
|
||||
return App::instance()->request()->get('searchterm');
|
||||
}
|
||||
]
|
||||
];
|
||||
57
kirby/config/sections/mixins/sort.php
Normal file
57
kirby/config/sections/mixins/sort.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'props' => [
|
||||
/**
|
||||
* Enables/disables reverse sorting
|
||||
*/
|
||||
'flip' => function (bool $flip = false) {
|
||||
return $flip;
|
||||
},
|
||||
/**
|
||||
* Enables/disables manual sorting
|
||||
*/
|
||||
'sortable' => function (bool $sortable = true) {
|
||||
return $sortable;
|
||||
},
|
||||
/**
|
||||
* Overwrites manual sorting and sorts by the given field and sorting direction (i.e. `date desc`)
|
||||
*/
|
||||
'sortBy' => function (string $sortBy = null) {
|
||||
return $sortBy;
|
||||
},
|
||||
],
|
||||
'computed' => [
|
||||
'sortable' => function () {
|
||||
if ($this->sortable === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (
|
||||
$this->type === 'pages' &&
|
||||
in_array($this->status, ['listed', 'published', 'all']) === false
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// don't allow sorting while search filter is active
|
||||
if (empty($this->searchterm()) === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->query !== null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->sortBy !== null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->flip === true) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
]
|
||||
];
|
||||
337
kirby/config/sections/pages.php
Normal file
337
kirby/config/sections/pages.php
Normal file
@@ -0,0 +1,337 @@
|
||||
<?php
|
||||
|
||||
use Kirby\Cms\Blueprint;
|
||||
use Kirby\Cms\Page;
|
||||
use Kirby\Cms\Pages;
|
||||
use Kirby\Cms\Site;
|
||||
use Kirby\Exception\InvalidArgumentException;
|
||||
use Kirby\Toolkit\A;
|
||||
use Kirby\Toolkit\I18n;
|
||||
|
||||
return [
|
||||
'mixins' => [
|
||||
'details',
|
||||
'empty',
|
||||
'headline',
|
||||
'help',
|
||||
'layout',
|
||||
'min',
|
||||
'max',
|
||||
'pagination',
|
||||
'parent',
|
||||
'search',
|
||||
'sort'
|
||||
],
|
||||
'props' => [
|
||||
/**
|
||||
* Optional array of templates that should only be allowed to add
|
||||
* or `false` to completely disable page creation
|
||||
*/
|
||||
'create' => function ($create = null) {
|
||||
return $create;
|
||||
},
|
||||
/**
|
||||
* Filters pages by a query. Sorting will be disabled
|
||||
*/
|
||||
'query' => function (string|null $query = null) {
|
||||
return $query;
|
||||
},
|
||||
/**
|
||||
* Filters pages by their status. Available status settings: `draft`, `unlisted`, `listed`, `published`, `all`.
|
||||
*/
|
||||
'status' => function (string $status = '') {
|
||||
if ($status === 'drafts') {
|
||||
$status = 'draft';
|
||||
}
|
||||
|
||||
if (in_array($status, ['all', 'draft', 'published', 'listed', 'unlisted']) === false) {
|
||||
$status = 'all';
|
||||
}
|
||||
|
||||
return $status;
|
||||
},
|
||||
/**
|
||||
* Filters the list by single template.
|
||||
*/
|
||||
'template' => function (string|array $template = null) {
|
||||
return $template;
|
||||
},
|
||||
/**
|
||||
* Filters the list by templates and sets template options when adding new pages to the section.
|
||||
*/
|
||||
'templates' => function ($templates = null) {
|
||||
return A::wrap($templates ?? $this->template);
|
||||
},
|
||||
/**
|
||||
* Excludes the selected templates.
|
||||
*/
|
||||
'templatesIgnore' => function ($templates = null) {
|
||||
return A::wrap($templates);
|
||||
}
|
||||
],
|
||||
'computed' => [
|
||||
'parent' => function () {
|
||||
$parent = $this->parentModel();
|
||||
|
||||
if (
|
||||
$parent instanceof Site === false &&
|
||||
$parent instanceof Page === false
|
||||
) {
|
||||
throw new InvalidArgumentException('The parent is invalid. You must choose the site or a page as parent.');
|
||||
}
|
||||
|
||||
return $parent;
|
||||
},
|
||||
'models' => function () {
|
||||
if ($this->query !== null) {
|
||||
$pages = $this->parent->query($this->query, Pages::class) ?? new Pages([]);
|
||||
} else {
|
||||
$pages = match ($this->status) {
|
||||
'draft' => $this->parent->drafts(),
|
||||
'listed' => $this->parent->children()->listed(),
|
||||
'published' => $this->parent->children(),
|
||||
'unlisted' => $this->parent->children()->unlisted(),
|
||||
default => $this->parent->childrenAndDrafts()
|
||||
};
|
||||
}
|
||||
|
||||
// filters pages that are protected and not in the templates list
|
||||
// internal `filter()` method used instead of foreach loop that previously included `unset()`
|
||||
// because `unset()` is updating the original data, `filter()` is just filtering
|
||||
// also it has been tested that there is no performance difference
|
||||
// even in 0.1 seconds on 100k virtual pages
|
||||
$pages = $pages->filter(function ($page) {
|
||||
// remove all protected and hidden pages
|
||||
if ($page->isListable() === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$intendedTemplate = $page->intendedTemplate()->name();
|
||||
|
||||
// filter by all set templates
|
||||
if (
|
||||
$this->templates &&
|
||||
in_array($intendedTemplate, $this->templates) === false
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// exclude by all ignored templates
|
||||
if (
|
||||
$this->templatesIgnore &&
|
||||
in_array($intendedTemplate, $this->templatesIgnore) === true
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
|
||||
// search
|
||||
if ($this->search === true && empty($this->searchterm()) === false) {
|
||||
$pages = $pages->search($this->searchterm());
|
||||
|
||||
// disable flip and sortBy while searching
|
||||
// to show most relevant results
|
||||
$this->flip = false;
|
||||
$this->sortBy = null;
|
||||
}
|
||||
|
||||
// sort
|
||||
if ($this->sortBy) {
|
||||
$pages = $pages->sort(...$pages::sortArgs($this->sortBy));
|
||||
}
|
||||
|
||||
// flip
|
||||
if ($this->flip === true) {
|
||||
$pages = $pages->flip();
|
||||
}
|
||||
|
||||
// pagination
|
||||
$pages = $pages->paginate([
|
||||
'page' => $this->page,
|
||||
'limit' => $this->limit,
|
||||
'method' => 'none' // the page is manually provided
|
||||
]);
|
||||
|
||||
return $pages;
|
||||
},
|
||||
'pages' => function () {
|
||||
return $this->models;
|
||||
},
|
||||
'total' => function () {
|
||||
return $this->models->pagination()->total();
|
||||
},
|
||||
'data' => function () {
|
||||
$data = [];
|
||||
|
||||
foreach ($this->models as $page) {
|
||||
$panel = $page->panel();
|
||||
$permissions = $page->permissions();
|
||||
|
||||
$item = [
|
||||
'dragText' => $panel->dragText(),
|
||||
'id' => $page->id(),
|
||||
'image' => $panel->image(
|
||||
$this->image,
|
||||
$this->layout === 'table' ? 'list' : $this->layout
|
||||
),
|
||||
'info' => $page->toSafeString($this->info ?? false),
|
||||
'link' => $panel->url(true),
|
||||
'parent' => $page->parentId(),
|
||||
'permissions' => [
|
||||
'sort' => $permissions->can('sort'),
|
||||
'changeSlug' => $permissions->can('changeSlug'),
|
||||
'changeStatus' => $permissions->can('changeStatus'),
|
||||
'changeTitle' => $permissions->can('changeTitle'),
|
||||
],
|
||||
'status' => $page->status(),
|
||||
'template' => $page->intendedTemplate()->name(),
|
||||
'text' => $page->toSafeString($this->text),
|
||||
];
|
||||
|
||||
if ($this->layout === 'table') {
|
||||
$item = $this->columnsValues($item, $page);
|
||||
}
|
||||
|
||||
$data[] = $item;
|
||||
}
|
||||
|
||||
return $data;
|
||||
},
|
||||
'errors' => function () {
|
||||
$errors = [];
|
||||
|
||||
if ($this->validateMax() === false) {
|
||||
$errors['max'] = I18n::template('error.section.pages.max.' . I18n::form($this->max), [
|
||||
'max' => $this->max,
|
||||
'section' => $this->headline
|
||||
]);
|
||||
}
|
||||
|
||||
if ($this->validateMin() === false) {
|
||||
$errors['min'] = I18n::template('error.section.pages.min.' . I18n::form($this->min), [
|
||||
'min' => $this->min,
|
||||
'section' => $this->headline
|
||||
]);
|
||||
}
|
||||
|
||||
if (empty($errors) === true) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
$this->name => [
|
||||
'label' => $this->headline,
|
||||
'message' => $errors,
|
||||
]
|
||||
];
|
||||
},
|
||||
'add' => function () {
|
||||
if ($this->create === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->isFull() === true) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// form here on, we need to check with which status
|
||||
// the pages are created and if the section can show
|
||||
// these newly created pages
|
||||
|
||||
// if the section shows pages no matter what status they have,
|
||||
// we can always show the add button
|
||||
if ($this->status === 'all') {
|
||||
return true;
|
||||
}
|
||||
|
||||
// collect all statuses of the blueprints
|
||||
// that are allowed to be created
|
||||
$statuses = [];
|
||||
|
||||
foreach ($this->blueprintNames() as $blueprint) {
|
||||
try {
|
||||
$props = Blueprint::load('pages/' . $blueprint);
|
||||
$statuses[] = $props['create']['status'] ?? 'draft';
|
||||
} catch (Throwable) {
|
||||
$statuses[] = 'draft'; // @codeCoverageIgnore
|
||||
}
|
||||
}
|
||||
|
||||
$statuses = array_unique($statuses);
|
||||
|
||||
// if there are multiple statuses or if the section is showing
|
||||
// a different status than new pages would be created with,
|
||||
// we cannot show the add button
|
||||
if (count($statuses) > 1 || $this->status !== $statuses[0]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
'pagination' => function () {
|
||||
return $this->pagination();
|
||||
}
|
||||
],
|
||||
'methods' => [
|
||||
'blueprints' => function () {
|
||||
$blueprints = [];
|
||||
|
||||
// convert every template to a usable option array
|
||||
// for the template select box
|
||||
foreach ($this->blueprintNames() as $blueprint) {
|
||||
try {
|
||||
$props = Blueprint::load('pages/' . $blueprint);
|
||||
|
||||
$blueprints[] = [
|
||||
'name' => basename($props['name']),
|
||||
'title' => $props['title'],
|
||||
];
|
||||
} catch (Throwable) {
|
||||
$blueprints[] = [
|
||||
'name' => basename($blueprint),
|
||||
'title' => ucfirst($blueprint),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $blueprints;
|
||||
},
|
||||
'blueprintNames' => function () {
|
||||
$blueprints = empty($this->create) === false ? A::wrap($this->create) : $this->templates;
|
||||
|
||||
if (empty($blueprints) === true) {
|
||||
$blueprints = $this->kirby()->blueprints();
|
||||
}
|
||||
|
||||
// excludes ignored templates
|
||||
if ($templatesIgnore = $this->templatesIgnore) {
|
||||
$blueprints = array_diff($blueprints, $templatesIgnore);
|
||||
}
|
||||
|
||||
return $blueprints;
|
||||
},
|
||||
],
|
||||
'toArray' => function () {
|
||||
return [
|
||||
'data' => $this->data,
|
||||
'errors' => $this->errors,
|
||||
'options' => [
|
||||
'add' => $this->add,
|
||||
'columns' => $this->columnsWithTypes(),
|
||||
'empty' => $this->empty,
|
||||
'headline' => $this->headline,
|
||||
'help' => $this->help,
|
||||
'layout' => $this->layout,
|
||||
'link' => $this->link(),
|
||||
'max' => $this->max,
|
||||
'min' => $this->min,
|
||||
'search' => $this->search,
|
||||
'size' => $this->size,
|
||||
'sortable' => $this->sortable
|
||||
],
|
||||
'pagination' => $this->pagination,
|
||||
];
|
||||
}
|
||||
];
|
||||
68
kirby/config/sections/stats.php
Normal file
68
kirby/config/sections/stats.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
use Kirby\Toolkit\I18n;
|
||||
|
||||
return [
|
||||
'mixins' => [
|
||||
'headline',
|
||||
],
|
||||
'props' => [
|
||||
/**
|
||||
* Array or query string for reports. Each report needs a `label` and `value` and can have additional `info`, `link`, `icon` and `theme` settings.
|
||||
*/
|
||||
'reports' => function ($reports = null) {
|
||||
if ($reports === null) {
|
||||
return [];
|
||||
}
|
||||
|
||||
if (is_string($reports) === true) {
|
||||
$reports = $this->model()->query($reports);
|
||||
}
|
||||
|
||||
if (is_array($reports) === false) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $reports;
|
||||
},
|
||||
/**
|
||||
* The size of the report cards. Available sizes: `tiny`, `small`, `medium`, `large`
|
||||
*/
|
||||
'size' => function (string $size = 'large') {
|
||||
return $size;
|
||||
}
|
||||
],
|
||||
'computed' => [
|
||||
'reports' => function () {
|
||||
$reports = [];
|
||||
$model = $this->model();
|
||||
$toString = fn ($value) => $value === null ? null : $model->toString($value);
|
||||
|
||||
foreach ($this->reports as $report) {
|
||||
if (is_string($report) === true) {
|
||||
$report = $model->query($report);
|
||||
}
|
||||
|
||||
if (is_array($report) === false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$info = $report['info'] ?? null;
|
||||
$label = $report['label'] ?? null;
|
||||
$link = $report['link'] ?? null;
|
||||
$value = $report['value'] ?? null;
|
||||
|
||||
$reports[] = [
|
||||
'icon' => $toString($report['icon'] ?? null),
|
||||
'info' => $toString(I18n::translate($info, $info)),
|
||||
'label' => $toString(I18n::translate($label, $label)),
|
||||
'link' => $toString(I18n::translate($link, $link)),
|
||||
'theme' => $toString($report['theme'] ?? null),
|
||||
'value' => $toString(I18n::translate($value, $value))
|
||||
];
|
||||
}
|
||||
|
||||
return $reports;
|
||||
}
|
||||
]
|
||||
];
|
||||
Reference in New Issue
Block a user