aboutsummaryrefslogtreecommitdiffstats
path: root/lib/public/Files
diff options
context:
space:
mode:
authorElizabeth Danzberger <lizzy7128@tutanota.de>2024-08-15 14:38:02 -0400
committerElizabeth Danzberger <lizzy7128@tutanota.de>2024-08-27 14:41:58 -0400
commit79d005e11fbdb0587ffff2f437721218fb29bade (patch)
treec988388dfbdf8ef5e269e1177f2ed323ddbb3034 /lib/public/Files
parent95fff6bc72f1f05050d87ef5f7bfb7ce5c5214af (diff)
downloadnextcloud-server-79d005e11fbdb0587ffff2f437721218fb29bade.tar.gz
nextcloud-server-79d005e11fbdb0587ffff2f437721218fb29bade.zip
feat(templates): add support for checkboxes in template filler
Signed-off-by: Elizabeth Danzberger <lizzy7128@tutanota.de>
Diffstat (limited to 'lib/public/Files')
-rw-r--r--lib/public/Files/Template/Field.php30
-rw-r--r--lib/public/Files/Template/FieldFactory.php32
-rw-r--r--lib/public/Files/Template/Fields/CheckBoxField.php47
-rw-r--r--lib/public/Files/Template/Fields/RichTextField.php47
4 files changed, 140 insertions, 16 deletions
diff --git a/lib/public/Files/Template/Field.php b/lib/public/Files/Template/Field.php
index 9a847d29ce0..dd65ef857ea 100644
--- a/lib/public/Files/Template/Field.php
+++ b/lib/public/Files/Template/Field.php
@@ -12,37 +12,35 @@ namespace OCP\Files\Template;
/**
* @since 30.0.0
*/
-class Field implements \JsonSerializable {
- private string $index;
- private string $content;
- private FieldType $type;
- private ?string $alias;
- private ?int $id;
- private ?string $tag;
+abstract class Field implements \JsonSerializable {
+ public ?string $alias = null;
+ public ?string $tag = null;
+ public ?int $id = null;
/**
* @since 30.0.0
*/
- public function __construct(string $index, string $content, FieldType $type, ?string $alias = null, ?int $id = null, ?string $tag = null) {
- $this->index = $index;
- $this->alias = $alias;
- $this->type = $type;
- $this->id = $id;
- $this->tag = $tag;
- $this->content = $content;
+ public function __construct(
+ private string $index,
+ private FieldType $type
+ ) {
}
/**
* @since 30.0.0
*/
+ abstract public function setValue(mixed $value): void;
+
+ /**
+ * @since 30.0.0
+ */
public function jsonSerialize(): array {
return [
'index' => $this->index,
- 'content' => $this->content,
'type' => $this->type->value,
'alias' => $this->alias,
- 'id' => $this->id,
'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..4c0fff6b46f
--- /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/Fields/CheckBoxField.php b/lib/public/Files/Template/Fields/CheckBoxField.php
new file mode 100644
index 00000000000..48ccb04336a
--- /dev/null
+++ b/lib/public/Files/Template/Fields/CheckBoxField.php
@@ -0,0 +1,47 @@
+<?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;
+ }
+
+ /**
+ * @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..14539bc9dbd
--- /dev/null
+++ b/lib/public/Files/Template/Fields/RichTextField.php
@@ -0,0 +1,47 @@
+<?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;
+ }
+
+ /**
+ * @since 30.0.0
+ */
+ public function jsonSerialize(): array {
+ $jsonProperties = parent::jsonSerialize();
+
+ return array_merge($jsonProperties, ['content' => $this->content]);
+ }
+}