Skip to content
Draft
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
94 changes: 32 additions & 62 deletions config/fields/files.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php

use Kirby\Cms\ModelWithContent;
use Kirby\Data\Data;
use Kirby\Toolkit\A;
use Kirby\Panel\Controller\Dialog\FilesPickerDialogController;

return [
'mixins' => [
'filepicker',
'layout',
'min',
'picker',
Expand All @@ -23,7 +21,8 @@
'placeholder' => null,

/**
* Sets the file(s), which are selected by default when a new page is created
* Sets the file(s), which are selected by default
* when a new page is created
*/
'default' => function ($default = null) {
return $default;
Expand All @@ -50,89 +49,60 @@
'parent' => function () {
return $this->parentModel->apiUrl(true);
},
'query' => function () {
return $this->query ?? $this->parentModel::CLASS_ALIAS . '.files';
},
'default' => function () {
return $this->toFiles($this->default);
return $this->toFormValues($this->default);
},
'value' => function () {
return $this->toFiles($this->value);
return $this->toFormValues($this->value);
},
],
'methods' => [
'fileResponse' => function ($file) {
return $file->panel()->pickerData([
'image' => $this->image,
'info' => $this->info ?? false,
'layout' => $this->layout,
'model' => $this->model(),
'text' => $this->text,
]);
},
'toFiles' => function ($value = null) {
$files = [];

foreach (Data::decode($value, 'yaml') as $id) {
if (is_array($id) === true) {
$id = $id['uuid'] ?? $id['id'] ?? null;
}

if (
$id !== null &&
($file = $this->kirby()->file($id, $this->model()))
) {
$files[] = $this->fileResponse($file);
}
}

return $files;
'toModel' => function (string $id) {
return $this->kirby()->file($id, $this->model);
}
],
'api' => function () {
return [
[
'pattern' => '/',
'action' => function () {
$field = $this->field();

return $field->filepicker([
'image' => $field->image(),
'info' => $field->info(),
'layout' => $field->layout(),
'limit' => $field->limit(),
'page' => $this->requestQuery('page'),
'query' => $field->query(),
'search' => $this->requestQuery('search'),
'text' => $field->text()
]);
}
'pattern' => 'items',
'method' => 'GET',
'action' => fn () => $this->field()->itemsFromRequest()
],
[
'pattern' => 'upload',
'method' => 'POST',
'action' => function () {
$field = $this->field();
$uploads = $field->uploads();
$field = $this->field();

// move_uploaded_file() not working with unit test
// @codeCoverageIgnoreStart
return $field->upload($this, $uploads, function ($file, $parent) use ($field) {
return $file->panel()->pickerData([
'image' => $field->image(),
'info' => $field->info(),
'layout' => $field->layout(),
'model' => $field->model(),
'text' => $field->text(),
]);
});
return $field->upload(
$this,
$field->uploads(),
fn ($file, $parent) => $field->toItem($file)
);
// @codeCoverageIgnoreEnd
}
]
];
},
'dialogs' => fn () => [
'picker' => fn () => new FilesPickerDialogController(...[
'model' => $this->model(),
'hasSearch' => $this->search,
'image' => $this->image,
'info' => $this->info ?? false,
'limit' => $this->limit,
'max' => $this->max,
'multiple' => $this->multiple,
'query' => $this->query,
'text' => $this->text,
'uploads' => $this->uploads,
...$this->picker
])
],
'save' => function ($value = null) {
return A::pluck($value, $this->store);
return $this->toStoredValues($value);
},
'validations' => [
'max',
Expand Down
14 changes: 0 additions & 14 deletions config/fields/mixins/filepicker.php

This file was deleted.

14 changes: 0 additions & 14 deletions config/fields/mixins/pagepicker.php

This file was deleted.

69 changes: 69 additions & 0 deletions config/fields/mixins/picker.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

use Kirby\Cms\ModelWithContent;
use Kirby\Data\Data;
use Kirby\Exception\Exception;
use Kirby\Toolkit\A;
use Kirby\Toolkit\I18n;
use Kirby\Toolkit\Str;
use Kirby\Uuid\Uuids;
Expand Down Expand Up @@ -55,6 +59,15 @@
return $multiple;
},

/**
* Additional picker dialog props
* @since 6.0.0
* @values { image, layout, size }
*/
'picker' => function (array $picker = []) {
return $picker;
},

/**
* Query for the items to be included in the picker
*/
Expand Down Expand Up @@ -90,4 +103,60 @@
return $text;
},
],
'methods' => [
'getIdFromArray' => function (array $array) {
return $array['uuid'] ?? $array['id'] ?? null;
},
'itemsFromRequest' => function () {
$ids = $this->kirby()->request()->get('items', '');
$ids = Str::split($ids);
$models = $this->toModels($ids);
return $this->toItems($models);
},
'toItem' => function (ModelWithContent $model) {
return $model->panel()->pickerData([
'image' => $this->image,
'info' => $this->info ?? false,
'layout' => $this->layout,
'model' => $this->model(),
'text' => $this->text,
]);
},
'toItems' => function (array $models = []) {
return A::map(
$models,
fn ($model) => $this->toItem($model)
);
},
'toModel' => function (string $id) {
throw new Exception(message: 'toModel() is not implemented on ' . $this->type() . ' field');
},
'toModels' => function (array $ids = []) {
return A::map(
$ids,
fn ($id) => $this->toModel($id)
);
},
'toFormValues' => function ($value = null) {
$ids = [];

foreach (Data::decode($value, 'yaml') as $id) {
if (is_array($id) === true) {
$id = $this->getIdFromArray($id);
}

if ($id !== null && ($model = $this->toModel($id))) {
$ids[] = $model->id();
}
}

return $ids;
},
'toStoredValues' => function ($value = null) {
return A::map(
$value ?? [],
fn (string $id) => (string)$this->toModel($id)?->{$this->store}()
);
}
]
];
2 changes: 2 additions & 0 deletions config/fields/mixins/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
$uploads['accept'] = $file->blueprint()->acceptAttribute();
}

$uploads['url'] ??= $this->model()->panel()->url(true) . '/fields/' . $this->name() . '/upload';

return $uploads;
},
],
Expand Down
13 changes: 0 additions & 13 deletions config/fields/mixins/userpicker.php

This file was deleted.

71 changes: 24 additions & 47 deletions config/fields/pages.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
<?php

use Kirby\Cms\App;
use Kirby\Data\Data;
use Kirby\Toolkit\A;
use Kirby\Panel\Controller\Dialog\PagesPickerDialogController;

return [
'mixins' => [
'layout',
'min',
'pagepicker',
'picker',
],
'props' => [
Expand All @@ -25,7 +22,7 @@
* Default selected page(s) when a new page/file/user is created
*/
'default' => function ($default = null) {
return $this->toPages($default);
return $this->toFormValues($default);
},

/**
Expand All @@ -43,7 +40,7 @@
},

'value' => function ($value = null) {
return $this->toPages($value);
return $this->toFormValues($value);
},
],
'computed' => [
Expand All @@ -53,56 +50,36 @@
'default' => null
],
'methods' => [
'pageResponse' => function ($page) {
return $page->panel()->pickerData([
'image' => $this->image,
'info' => $this->info,
'layout' => $this->layout,
'text' => $this->text,
]);
},
'toPages' => function ($value = null) {
$pages = [];
$kirby = App::instance();

foreach (Data::decode($value, 'yaml') as $id) {
if (is_array($id) === true) {
$id = $id['uuid'] ?? $id['id'] ?? null;
}

if ($id !== null && ($page = $kirby->page($id))) {
$pages[] = $this->pageResponse($page);
}
}

return $pages;
'toModel' => function (string $id) {
return $this->kirby()->page($id);
}
],
'api' => function () {
return [
[
'pattern' => '/',
'action' => function () {
$field = $this->field();

return $field->pagepicker([
'image' => $field->image(),
'info' => $field->info(),
'layout' => $field->layout(),
'limit' => $field->limit(),
'page' => $this->requestQuery('page'),
'parent' => $this->requestQuery('parent'),
'query' => $field->query(),
'search' => $this->requestQuery('search'),
'subpages' => $field->subpages(),
'text' => $field->text()
]);
}
'pattern' => 'items',
'method' => 'GET',
'action' => fn () => $this->field()->itemsFromRequest()
]
];
},
'dialogs' => fn () => [
'picker' => fn () => new PagesPickerDialogController(...[
'model' => $this->model(),
'hasSearch' => $this->search,
'image' => $this->image,
'info' => $this->info ?? false,
'limit' => $this->limit,
'max' => $this->max,
'multiple' => $this->multiple,
'query' => $this->query,
'subpages' => $this->subpages,
'text' => $this->text,
...$this->picker
])
],
'save' => function ($value = null) {
return A::pluck($value, $this->store);
return $this->toStoredValues($value);
},
'validations' => [
'max',
Expand Down
Loading
Loading