Add pages form field
authorCyperghost <olaf_schmitz_1@t-online.de>
Tue, 19 Nov 2024 09:27:39 +0000 (10:27 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Tue, 19 Nov 2024 09:27:39 +0000 (10:27 +0100)
com.woltlab.wcf/templates/shared_multiplePagesSelectionFormField.tpl [new file with mode: 0644]
wcfsetup/install/files/lib/system/form/builder/field/PagesFormField.class.php [new file with mode: 0644]

diff --git a/com.woltlab.wcf/templates/shared_multiplePagesSelectionFormField.tpl b/com.woltlab.wcf/templates/shared_multiplePagesSelectionFormField.tpl
new file mode 100644 (file)
index 0000000..8d44f89
--- /dev/null
@@ -0,0 +1,19 @@
+{include file="shared_multipleSelectionFormField"}
+
+{if $field->getVisibleEverywhereFieldId() !== null}
+       <script data-relocate="true">
+               const label = document.querySelector('label[for="{$field->getPrefixedId()}"]');
+
+               document.querySelectorAll('input[name="{$field->getVisibleEverywhereFieldId()}"]').forEach((input) => {
+                       input.addEventListener("change", () => {
+                               setLabelText(input.value);
+                       });
+               });
+
+               function setLabelText (value) {
+                       label.innerHTML = parseInt(value) === 0 ? '{unsafe:$field->getLabel()|encodeJS}' : '{unsafe:$field->getInvertedLabel()|encodeJS}';
+               }
+
+               setLabelText(document.querySelector('input[name="{$field->getVisibleEverywhereFieldId()}"]:checked').value);
+       </script>
+{/if}
diff --git a/wcfsetup/install/files/lib/system/form/builder/field/PagesFormField.class.php b/wcfsetup/install/files/lib/system/form/builder/field/PagesFormField.class.php
new file mode 100644 (file)
index 0000000..601ced0
--- /dev/null
@@ -0,0 +1,64 @@
+<?php
+
+namespace wcf\system\form\builder\field;
+
+use wcf\data\page\PageNodeTree;
+use wcf\system\WCF;
+
+/**
+ * Implementation of a form field for selecting multiple pages.
+ *
+ * @author      Olaf Braun
+ * @copyright   2001-2024 WoltLab GmbH
+ * @license     GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since       6.2
+ */
+final class PagesFormField extends MultipleSelectionFormField
+{
+    use TDefaultIdFormField;
+
+    /**
+     * @inheritDoc
+     */
+    protected $templateName = 'shared_multiplePagesSelectionFormField';
+
+    private ?string $visibleEverywhereFieldId = null;
+    private string $invertedLabel = '';
+
+    public function __construct()
+    {
+        $this
+            ->options((new PageNodeTree())->getNodeList(), true)
+            ->filterable();
+    }
+
+    #[\Override]
+    protected static function getDefaultId()
+    {
+        return 'pageIDs';
+    }
+
+    public function getVisibleEverywhereFieldId(): ?string
+    {
+        return $this->visibleEverywhereFieldId;
+    }
+
+    public function visibleEverywhereFieldId(?string $visibleEverywhereFieldId): self
+    {
+        $this->visibleEverywhereFieldId = $visibleEverywhereFieldId;
+
+        return $this;
+    }
+
+    public function getInvertedLabel(): string
+    {
+        return $this->invertedLabel;
+    }
+
+    public function invertedLabel(string $languageItem, array $variables = []): self
+    {
+        $this->invertedLabel = WCF::getLanguage()->getDynamicVariable($languageItem, $variables);
+
+        return $this;
+    }
+}