aboutsummaryrefslogtreecommitdiffstats
path: root/lib/public/Files/Template
diff options
context:
space:
mode:
Diffstat (limited to 'lib/public/Files/Template')
-rw-r--r--lib/public/Files/Template/BeforeGetTemplatesEvent.php52
-rw-r--r--lib/public/Files/Template/Field.php55
-rw-r--r--lib/public/Files/Template/FieldFactory.php32
-rw-r--r--lib/public/Files/Template/FieldType.php19
-rw-r--r--lib/public/Files/Template/Fields/CheckBoxField.php56
-rw-r--r--lib/public/Files/Template/Fields/RichTextField.php56
-rw-r--r--lib/public/Files/Template/FileCreatedFromTemplateEvent.php33
-rw-r--r--lib/public/Files/Template/ICustomTemplateProvider.php23
-rw-r--r--lib/public/Files/Template/ITemplateManager.php37
-rw-r--r--lib/public/Files/Template/InvalidFieldTypeException.php15
-rw-r--r--lib/public/Files/Template/RegisterTemplateCreatorEvent.php31
-rw-r--r--lib/public/Files/Template/Template.php57
-rw-r--r--lib/public/Files/Template/TemplateFileCreator.php28
13 files changed, 389 insertions, 105 deletions
diff --git a/lib/public/Files/Template/BeforeGetTemplatesEvent.php b/lib/public/Files/Template/BeforeGetTemplatesEvent.php
new file mode 100644
index 00000000000..9fb7453a50c
--- /dev/null
+++ b/lib/public/Files/Template/BeforeGetTemplatesEvent.php
@@ -0,0 +1,52 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCP\Files\Template;
+
+use OCP\EventDispatcher\Event;
+
+/**
+ * @since 30.0.0
+ */
+class BeforeGetTemplatesEvent extends Event {
+ /** @var array<Template> */
+ private array $templates;
+ /** @var bool */
+ private bool $withFields;
+
+ /**
+ * @param array<Template> $templates
+ *
+ * @since 30.0.0
+ */
+ public function __construct(array $templates, bool $withFields = false) {
+ parent::__construct();
+
+ $this->templates = $templates;
+ $this->withFields = $withFields;
+ }
+
+ /**
+ * @return array<Template>
+ *
+ * @since 30.0.0
+ */
+ public function getTemplates(): array {
+ return $this->templates;
+ }
+
+ /**
+ * @return bool
+ *
+ * @since 32.0.0
+ */
+ public function shouldGetFields(): bool {
+ return $this->withFields;
+ }
+}
diff --git a/lib/public/Files/Template/Field.php b/lib/public/Files/Template/Field.php
new file mode 100644
index 00000000000..e047e83a29e
--- /dev/null
+++ b/lib/public/Files/Template/Field.php
@@ -0,0 +1,55 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCP\Files\Template;
+
+/**
+ * @since 30.0.0
+ */
+abstract class Field implements \JsonSerializable {
+ public ?string $alias = null;
+ public ?string $tag = null;
+ public ?int $id = null;
+
+ /**
+ * @since 30.0.0
+ */
+ public function __construct(
+ private string $index,
+ private FieldType $type,
+ ) {
+ }
+
+ /**
+ * @since 30.0.0
+ */
+ abstract public function setValue(mixed $value): void;
+
+ /**
+ * @return array{
+ * index: string,
+ * type: string,
+ * alias: ?string,
+ * tag: ?string,
+ * id: ?int,
+ * content?: string,
+ * checked?: bool,
+ * }
+ * @since 30.0.0
+ */
+ public function jsonSerialize(): array {
+ return [
+ 'index' => $this->index,
+ 'type' => $this->type->value,
+ 'alias' => $this->alias,
+ 'tag' => $this->tag,
+ 'id' => $this->id,
+ ];
+ }
+}
diff --git a/lib/public/Files/Template/FieldFactory.php b/lib/public/Files/Template/FieldFactory.php
new file mode 100644
index 00000000000..f14d44a8573
--- /dev/null
+++ b/lib/public/Files/Template/FieldFactory.php
@@ -0,0 +1,32 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCP\Files\Template;
+
+use OCP\Files\Template\Fields\CheckBoxField;
+use OCP\Files\Template\Fields\RichTextField;
+
+/**
+ * @since 30.0.0
+ */
+class FieldFactory {
+ /**
+ * @since 30.0.0
+ */
+ public static function createField(
+ string $index,
+ FieldType $type,
+ ): Field {
+ return match ($type) {
+ FieldType::RichText => new RichTextField($index, $type),
+ FieldType::CheckBox => new CheckBoxField($index, $type),
+ default => throw new InvalidFieldTypeException(),
+ };
+ }
+}
diff --git a/lib/public/Files/Template/FieldType.php b/lib/public/Files/Template/FieldType.php
new file mode 100644
index 00000000000..2d059cadc17
--- /dev/null
+++ b/lib/public/Files/Template/FieldType.php
@@ -0,0 +1,19 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCP\Files\Template;
+
+/**
+ * @since 30.0.0
+ */
+enum FieldType: string {
+ case RichText = 'rich-text';
+ case CheckBox = 'checkbox';
+ case DropDownList = 'drop-down-list';
+ case Picture = 'picture';
+ case Date = 'date';
+}
diff --git a/lib/public/Files/Template/Fields/CheckBoxField.php b/lib/public/Files/Template/Fields/CheckBoxField.php
new file mode 100644
index 00000000000..6fab3ce66a6
--- /dev/null
+++ b/lib/public/Files/Template/Fields/CheckBoxField.php
@@ -0,0 +1,56 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCP\Files\Template\Fields;
+
+use OCP\Files\Template\Field;
+use OCP\Files\Template\FieldType;
+
+/**
+ * @since 30.0.0
+ */
+class CheckBoxField extends Field {
+ private bool $checked = false;
+
+ /**
+ * @since 30.0.0
+ */
+ public function __construct(string $index, FieldType $type) {
+ parent::__construct($index, $type);
+ }
+
+ /**
+ * @since 30.0.0
+ */
+ public function setValue(mixed $value): void {
+ if (!is_bool($value)) {
+ throw new \Exception('Invalid value for checkbox field type');
+ }
+
+ $this->checked = $value;
+ }
+
+ /**
+ * @return array{
+ * index: string,
+ * type: string,
+ * alias: ?string,
+ * tag: ?string,
+ * id: ?int,
+ * content?: string,
+ * checked?: bool,
+ * }
+ * @since 30.0.0
+ */
+ public function jsonSerialize(): array {
+ $jsonProperties = parent::jsonSerialize();
+
+ return array_merge($jsonProperties, ['checked' => $this->checked]);
+ }
+}
diff --git a/lib/public/Files/Template/Fields/RichTextField.php b/lib/public/Files/Template/Fields/RichTextField.php
new file mode 100644
index 00000000000..93ead68747c
--- /dev/null
+++ b/lib/public/Files/Template/Fields/RichTextField.php
@@ -0,0 +1,56 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCP\Files\Template\Fields;
+
+use OCP\Files\Template\Field;
+use OCP\Files\Template\FieldType;
+
+/**
+ * @since 30.0.0
+ */
+class RichTextField extends Field {
+ private string $content = '';
+
+ /**
+ * @since 30.0.0
+ */
+ public function __construct(string $index, FieldType $type) {
+ parent::__construct($index, $type);
+ }
+
+ /**
+ * @since 30.0.0
+ */
+ public function setValue(mixed $value): void {
+ if (!is_string($value)) {
+ throw new \Exception('Invalid value for rich-text field type');
+ }
+
+ $this->content = $value;
+ }
+
+ /**
+ * @return array{
+ * index: string,
+ * type: string,
+ * alias: ?string,
+ * tag: ?string,
+ * id: ?int,
+ * content?: string,
+ * checked?: bool,
+ * }
+ * @since 30.0.0
+ */
+ public function jsonSerialize(): array {
+ $jsonProperties = parent::jsonSerialize();
+
+ return array_merge($jsonProperties, ['content' => $this->content]);
+ }
+}
diff --git a/lib/public/Files/Template/FileCreatedFromTemplateEvent.php b/lib/public/Files/Template/FileCreatedFromTemplateEvent.php
index bb913f6f16a..0636d1dc251 100644
--- a/lib/public/Files/Template/FileCreatedFromTemplateEvent.php
+++ b/lib/public/Files/Template/FileCreatedFromTemplateEvent.php
@@ -3,25 +3,8 @@
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2021 Julius Härtl <jus@bitgrid.net>
- *
- * @author Julius Härtl <jus@bitgrid.net>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCP\Files\Template;
@@ -34,15 +17,17 @@ use OCP\Files\File;
class FileCreatedFromTemplateEvent extends Event {
private $template;
private $target;
+ private $templateFields;
/**
* @param File|null $template
* @param File $target
* @since 21.0.0
*/
- public function __construct(?File $template, File $target) {
+ public function __construct(?File $template, File $target, array $templateFields) {
$this->template = $template;
$this->target = $target;
+ $this->templateFields = $templateFields;
}
/**
@@ -54,6 +39,14 @@ class FileCreatedFromTemplateEvent extends Event {
}
/**
+ * @return array
+ * @since 30.0.0
+ */
+ public function getTemplateFields(): array {
+ return $this->templateFields;
+ }
+
+ /**
* @return File
* @since 21.0.0
*/
diff --git a/lib/public/Files/Template/ICustomTemplateProvider.php b/lib/public/Files/Template/ICustomTemplateProvider.php
index 81448a111de..6136bc4f1c0 100644
--- a/lib/public/Files/Template/ICustomTemplateProvider.php
+++ b/lib/public/Files/Template/ICustomTemplateProvider.php
@@ -3,25 +3,8 @@
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2021 Julius Härtl <jus@bitgrid.net>
- *
- * @author Julius Härtl <jus@bitgrid.net>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCP\Files\Template;
@@ -34,7 +17,7 @@ interface ICustomTemplateProvider {
/**
* Return a list of additional templates that the template provider is offering
*
- * @return File[]
+ * @return Template[]
* @since 21.0.0
*/
public function getCustomTemplates(string $mimetype): array;
diff --git a/lib/public/Files/Template/ITemplateManager.php b/lib/public/Files/Template/ITemplateManager.php
index c54c9716804..df81bc5604e 100644
--- a/lib/public/Files/Template/ITemplateManager.php
+++ b/lib/public/Files/Template/ITemplateManager.php
@@ -3,26 +3,8 @@
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2021 Julius Härtl <jus@bitgrid.net>
- *
- * @author John Molakvoæ <skjnldsv@protonmail.com>
- * @author Julius Härtl <jus@bitgrid.net>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCP\Files\Template;
@@ -51,12 +33,21 @@ interface ITemplateManager {
/**
* Get a list of available file creators and their offered templates
*
- * @return array
+ * @return list<array{app: string, label: string, extension: string, iconClass: ?string, iconSvgInline: ?string, mimetypes: list<string>, ratio: ?float, actionLabel: string, templates: list<Template>}>
* @since 21.0.0
*/
public function listTemplates(): array;
/**
+ * Get the fields for a given template
+ *
+ * @param int $fileId
+ * @return array
+ * @since 32.0.0
+ */
+ public function listTemplateFields(int $fileId): array;
+
+ /**
* @return bool
* @since 21.0.0
*/
@@ -85,9 +76,11 @@ interface ITemplateManager {
/**
* @param string $filePath
* @param string $templateId
+ * @param string $templateType
+ * @param array $templateFields Since 30.0.0
* @return array
* @throws GenericFileException
* @since 21.0.0
*/
- public function createFromTemplate(string $filePath, string $templateId = '', string $templateType = 'user'): array;
+ public function createFromTemplate(string $filePath, string $templateId = '', string $templateType = 'user', array $templateFields = []): array;
}
diff --git a/lib/public/Files/Template/InvalidFieldTypeException.php b/lib/public/Files/Template/InvalidFieldTypeException.php
new file mode 100644
index 00000000000..a0c5297526c
--- /dev/null
+++ b/lib/public/Files/Template/InvalidFieldTypeException.php
@@ -0,0 +1,15 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCP\Files\Template;
+
+/**
+ * Exception for invalid template field type
+ * @since 30.0.0
+ */
+class InvalidFieldTypeException extends \Exception {
+}
diff --git a/lib/public/Files/Template/RegisterTemplateCreatorEvent.php b/lib/public/Files/Template/RegisterTemplateCreatorEvent.php
new file mode 100644
index 00000000000..a9e7fa01252
--- /dev/null
+++ b/lib/public/Files/Template/RegisterTemplateCreatorEvent.php
@@ -0,0 +1,31 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCP\Files\Template;
+
+use OCP\EventDispatcher\Event;
+
+/**
+ * @since 30.0.0
+ */
+class RegisterTemplateCreatorEvent extends Event {
+
+ /**
+ * @since 30.0.0
+ */
+ public function __construct(
+ private ITemplateManager $templateManager,
+ ) {
+ }
+
+ /**
+ * @since 30.0.0
+ */
+ public function getTemplateManager(): ITemplateManager {
+ return $this->templateManager;
+ }
+}
diff --git a/lib/public/Files/Template/Template.php b/lib/public/Files/Template/Template.php
index 8fefc422c86..7f01c2afa48 100644
--- a/lib/public/Files/Template/Template.php
+++ b/lib/public/Files/Template/Template.php
@@ -3,25 +3,8 @@
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2021 Julius Härtl <jus@bitgrid.net>
- *
- * @author Julius Härtl <jus@bitgrid.net>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCP\Files\Template;
@@ -41,6 +24,8 @@ final class Template implements \JsonSerializable {
private $hasPreview = false;
/** @var string|null */
private $previewUrl = null;
+ /** @var list<Field> */
+ private $fields = [];
/**
* @since 21.0.0
@@ -66,6 +51,37 @@ final class Template implements \JsonSerializable {
}
/**
+ * @param list<Field> $fields
+ * @since 30.0.0
+ */
+ public function setFields(array $fields): void {
+ $this->fields = $fields;
+ }
+
+ /**
+ * @return array{
+ * templateType: string,
+ * templateId: string,
+ * basename: string,
+ * etag: string,
+ * fileid: int,
+ * filename: string,
+ * lastmod: int,
+ * mime: string,
+ * size: int|float,
+ * type: string,
+ * hasPreview: bool,
+ * previewUrl: ?string,
+ * fields: list<array{
+ * index: string,
+ * type: string,
+ * alias: ?string,
+ * tag: ?string,
+ * id: ?int,
+ * content?: string,
+ * checked?: bool,
+ * }>,
+ * }
* @since 21.0.0
*/
public function jsonSerialize(): array {
@@ -81,7 +97,8 @@ final class Template implements \JsonSerializable {
'size' => $this->file->getSize(),
'type' => $this->file->getType(),
'hasPreview' => $this->hasPreview,
- 'previewUrl' => $this->previewUrl
+ 'previewUrl' => $this->previewUrl,
+ 'fields' => array_map(static fn (Field $field) => $field->jsonSerialize(), $this->fields),
];
}
}
diff --git a/lib/public/Files/Template/TemplateFileCreator.php b/lib/public/Files/Template/TemplateFileCreator.php
index 40a68461bc1..809bd3d0bc2 100644
--- a/lib/public/Files/Template/TemplateFileCreator.php
+++ b/lib/public/Files/Template/TemplateFileCreator.php
@@ -3,26 +3,8 @@
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2021 Julius Härtl <jus@bitgrid.net>
- *
- * @author John Molakvoæ <skjnldsv@protonmail.com>
- * @author Julius Härtl <jus@bitgrid.net>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCP\Files\Template;
@@ -31,7 +13,7 @@ namespace OCP\Files\Template;
*/
final class TemplateFileCreator implements \JsonSerializable {
protected $appId;
- /** @var string[] $mimetypes */
+ /** @var list<string> $mimetypes */
protected $mimetypes = [];
protected $actionName;
protected $fileExtension;
@@ -52,7 +34,7 @@ final class TemplateFileCreator implements \JsonSerializable {
* @since 21.0.0
*/
public function __construct(
- string $appId, string $actionName, string $fileExtension
+ string $appId, string $actionName, string $fileExtension,
) {
$this->appId = $appId;
$this->actionName = $actionName;
@@ -139,7 +121,7 @@ final class TemplateFileCreator implements \JsonSerializable {
/**
* @since 21.0.0
- * @return array{app: string, label: string, extension: string, iconClass: ?string, iconSvgInline: ?string, mimetypes: string[], ratio: ?float, actionLabel: string}
+ * @return array{app: string, label: string, extension: string, iconClass: ?string, iconSvgInline: ?string, mimetypes: list<string>, ratio: ?float, actionLabel: string}
*/
public function jsonSerialize(): array {
return [