Add `TemplateFormNode`
authorMatthias Schmidt <gravatronics@live.com>
Sat, 6 Apr 2019 10:51:41 +0000 (12:51 +0200)
committerMatthias Schmidt <gravatronics@live.com>
Sat, 6 Apr 2019 10:51:41 +0000 (12:51 +0200)
See #2509

wcfsetup/install/files/lib/system/form/builder/TemplateFormNode.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/lib/system/form/builder/TemplateFormNode.class.php b/wcfsetup/install/files/lib/system/form/builder/TemplateFormNode.class.php
new file mode 100644 (file)
index 0000000..45e9be7
--- /dev/null
@@ -0,0 +1,137 @@
+<?php
+namespace wcf\system\form\builder;
+use wcf\system\application\ApplicationHandler;
+use wcf\system\WCF;
+
+/**
+ * Form node that shows the contents of a specific template.
+ * 
+ * @author     Matthias Schmidt
+ * @copyright  2001-2019 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    WoltLabSuite\Core\System\Form\Builder
+ * @since      5.2
+ */
+class TemplateFormNode implements IFormChildNode {
+       use TFormChildNode;
+       use TFormNode;
+       
+       /**
+        * application of the template with the contents of the form node
+        * @var null|string
+        */
+       protected $application;
+       
+       /**
+        * name of the template with the contents of the form node
+        * @var null|string
+        */
+       protected $templateName;
+       
+       /**
+        * template variables passed to the template
+        * @var array
+        */
+       protected $variables = [];
+       
+       /**
+        * Sets the application of the template with the contents of the form node and returns this
+        * form node.
+        * 
+        * @param       string          $application    application abbreviation
+        * @return      static                          this form node
+        * 
+        * @throws      \InvalidArgumentException       if no application with the given abbreviation exists
+        */
+       public function application($application) {
+               if (ApplicationHandler::getInstance()->getApplication($application) === null) {
+                       throw new \InvalidArgumentException("Unknown application with abbreviation '{$application}'.");
+               }
+               
+               $this->application = $application;
+               
+               return $this;
+       }
+       
+       /**
+        * Returns the application of the template with the contents of the form node.
+        * 
+        * If no application has been set, `wcf` will be set and return.
+        * 
+        * @return      string          application abbreviation
+        */
+       public function getApplication() {
+               if ($this->application === null) {
+                       $this->application = 'wcf';
+               }
+               
+               return $this->application;
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function getHtml() {
+               return WCF::getTPL()->fetch(
+                       $this->getTemplateName(),
+                       $this->getApplication(),
+                       $this->getVariables(),
+                       true
+               );
+       }
+       
+       /**
+        * Returns the name of the template with the contents of the form node.
+        * 
+        * @return      string                          name of template with form node contents
+        * 
+        * @throws      \BadMethodCallException         if template name has not been set yet
+        */
+       public function getTemplateName() {
+               if ($this->templateName === null) {
+                       throw new \BadMethodCallException("Template name has not been set yet.");
+               }
+               
+               return $this->templateName;
+       }
+       
+       /**
+        * Returns the template variables passed to the template.
+        * 
+        * @return      array                   template variables passed to template
+        */
+       public function getVariables() {
+               return $this->variables;
+       }
+       
+       /**
+        * Sets the name of the template with the contents of the form node and returns this form node.
+        * 
+        * @param       string          $templateName   name of template with form node contents
+        * @return      static                          this form node
+        */
+       public function templateName($templateName) {
+               $this->templateName = $templateName;
+               
+               return $this;
+       }
+       
+       /**
+        * @inheritDoc
+        */
+       public function validate() {
+               // does nothing
+       }
+       
+       /**
+        * Sets the template variables passed to the template and returns this form node.
+        * 
+        * @param       array   $variables      template variables passed to template
+        * @return      static                  this form node
+        */
+       public function variables(array $variables) {
+               $this->variables = $variables;
+               
+               return $this;
+       }
+}