From 4b9e5da72e7c36ece6bd9a840870e8a451844749 Mon Sep 17 00:00:00 2001
From: Carsten  Rose <carsten.rose@math.uzh.ch>
Date: Sun, 13 Nov 2022 22:17:49 +0100
Subject: [PATCH] Code reformat

---
 Documentation/Form.rst                       | 15 +++---
 extension/Classes/Core/AbstractBuildForm.php | 43 +++++++--------
 extension/Classes/Core/Save.php              | 57 +++++++++++---------
 3 files changed, 62 insertions(+), 53 deletions(-)

diff --git a/Documentation/Form.rst b/Documentation/Form.rst
index a9a331fb2..db44de583 100644
--- a/Documentation/Form.rst
+++ b/Documentation/Form.rst
@@ -1373,12 +1373,14 @@ General input for any text.
   * *hideZero* = 0|1 (optional): `with hideZero=1` a '0' in the value will be replaced by an empty string.
   * *emptyMeansNull* = [0|1] (optional): with `emptyMeansNull` or `emptyMeansNull=1` a NULL value will be written if
     the value is an empty string
-  * *inputType* = number (optional). Typically the HTML tag 'type' will be 'text', 'textarea' or 'number' (detected automatically).
-    If necessary, the HTML tag 'type' might be forced to a specific given value.
+  * *inputType* = number (optional). Typically the HTML tag 'type' will be 'text', 'textarea' or 'number' (detected
+    automatically). If necessary, the HTML tag 'type' might be forced to a specific given value.
   * *step* = Step size of the up/down buttons which increase/decrease the number of in the input field. Optional.
     Default 1. Only useful with `inputType=number` (defined explicit via `inputType` or detected automatically).
   * *textareaResize* = 0|1 (optional). Be default = 1 (=on). A textarea element is resizable by the user.
-  * *htmlAllow* = p,br,img,table,u,ol,b,h2,h3,h5,sup (optional). By default every html tag is allowed. Allow only specific html tags. If following tags are given in list then associated tags will be added automatically:
+  * *htmlAllow* = p,br,img,table,u,ol,b,h2,h3,h5,sup (optional). By default every html tag is allowed. Allow only specific
+    html tags. This option is only useful in case `encode` is not `specialchar` (cause otherwise there are no HTML tags).
+    If any of the following main tags (before colon) are given, the associated tags will be added automatically:
 
     * table: td, tr, th, tbody, thead
     * ol,ul: li
@@ -1565,7 +1567,7 @@ Type: editor
 
 .. important::
 
-    *FormElement.encode*: To save HTML code, incl. HTML tags (bold, table, lists, ...), the **htmspecialchar**
+    *FormElement.encode*: To save HTML code, incl. HTML tags (bold, table, lists, ...), the **htmlspecialchar**
     encoding can't be used, cause the HTML tags loose their meaning. Therefore **single tick** or **none**  is necessary.
 
 
@@ -1592,6 +1594,7 @@ Type: editor
     editor-plugins=code link lists searchreplace table textcolor textpattern visualchars
     editor-toolbar=code searchreplace undo redo | styleselect link table | bullist numlist outdent indent | forecolor backcolor bold italic editor-menubar=false
     editor-statusbar=false
+
 * To activate drag and drop option for images in TinyMCE add 'image,paste' to editor-plugins. Example: ::
 
     editor-plugins=code link lists searchreplace table textcolor textpattern visualchars image,paste
@@ -1621,8 +1624,8 @@ Type: editor
 
 * *FormElement.size* = <min_height>,<max_height>: in pixels, including top and bottom bars. E.g.: 300,600
 
-  Define allowed html tags. TinyMce Settings will be overwritten if this parameter is set.
-* Following tags are not used from tinyMce: u,del,ins,s. In this case use textDecoration to get comparable function and correct configuration. Example: ::
+  Define allowed html tags. TinyMCE settings will be overwritten if this parameter is set.
+* Following tags are not used from TinyMCE: u,del,ins,s. In this case use textDecoration to get comparable function and correct configuration. Example: ::
 
     htmlAllow = p,br,h1,h3,table,b,textDecoration,ul,img
 
diff --git a/extension/Classes/Core/AbstractBuildForm.php b/extension/Classes/Core/AbstractBuildForm.php
index 088834e79..6ef33a016 100644
--- a/extension/Classes/Core/AbstractBuildForm.php
+++ b/extension/Classes/Core/AbstractBuildForm.php
@@ -3412,11 +3412,11 @@ abstract class AbstractBuildForm {
             switch ($value) {
                 case 'a':
                     $htmlAllowArray[$htmlToken] = $url;
-                    self::setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'link');
+                    $this->setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'link');
                     break;
                 case 'table':
                     $htmlAllowArray[$htmlToken] = $table;
-                    self::setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'table');
+                    $this->setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'table');
                     break;
                 case 'textDecoration':
                 case 'u':
@@ -3425,13 +3425,13 @@ abstract class AbstractBuildForm {
                 case 'del':
                     if (!$decorationFlag) {
                         $htmlAllowArray[$htmlToken] = $textDecoration;
-                        self::setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'textDecoration', 'underline strikethrough');
+                        $this->setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'textDecoration', 'underline strikethrough');
                         $decorationFlag = true;
                     }
                     break;
                 case 'img':
                     $htmlAllowArray[$htmlToken] = $imgToken;
-                    self::setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'image');
+                    $this->setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'image');
                     break;
                 case 'ul':
                     if (!$listFlag) {
@@ -3440,7 +3440,7 @@ abstract class AbstractBuildForm {
                     } else {
                         $htmlAllowArray[$htmlToken] = 'ul';
                     }
-                    self::setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'bullist');
+                    $this->setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'bullist');
                     break;
                 case 'ol':
                     if (!$listFlag) {
@@ -3449,22 +3449,22 @@ abstract class AbstractBuildForm {
                     } else {
                         $htmlAllowArray[$htmlToken] = 'ol';
                     }
-                    self::setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'numlist');
+                    $this->setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'numlist');
                     break;
                 case 'b':
                 case 'strong':
                     $htmlAllowArray[$htmlToken] = $strong;
-                    self::setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'bold');
-                break;
+                    $this->setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'bold');
+                    break;
                 case 'i':
                 case 'em':
-                    self::setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'italic');
-                break;
+                    $this->setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'italic');
+                    break;
                 case 'sub':
-                    self::setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'subscript');
+                    $this->setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'subscript');
                     break;
                 case 'sup':
-                    self::setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'supscript');
+                    $this->setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'supscript');
                     break;
                 case 'h1':
                 case 'h2':
@@ -3475,20 +3475,20 @@ abstract class AbstractBuildForm {
                 case 'p':
                 case 'div':
                 case 'pre':
-                if ($value === 'p') {
-                    $htmlAllowArray[$htmlToken] = $paragraphToken;
-                }
-                    self::setTinymceBlockFormats($value, $formatDropdownElements);
-                if (!$formatDropdownFlag) {
-                    $formatDropdownFlag = true;
-                }
-                break;
+                    if ($value === 'p') {
+                        $htmlAllowArray[$htmlToken] = $paragraphToken;
+                    }
+                    $this->setTinymceBlockFormats($value, $formatDropdownElements);
+                    if (!$formatDropdownFlag) {
+                        $formatDropdownFlag = true;
+                    }
+                    break;
             }
         }
 
         // set format dropdown at the end of the toolbar if its used
         if ($formatDropdownFlag) {
-            self::setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'formatselect');
+            $this->setTinymceEditorToolbarAttributes($customEditorToolbarFlags, $customEditorToolbar, 'formatselect');
         }
 
         // If htmlAllow is used: toolbar will be overwritten with the customized one
@@ -3549,6 +3549,7 @@ abstract class AbstractBuildForm {
      * @return void
      */
     private function setTinymceBlockFormats($format, &$formatDropdownElements) {
+
         switch ($format) {
             case 'h1':
                 $formatDropdownElements[] = 'Heading 1=h1';
diff --git a/extension/Classes/Core/Save.php b/extension/Classes/Core/Save.php
index 688946540..8ebff5905 100644
--- a/extension/Classes/Core/Save.php
+++ b/extension/Classes/Core/Save.php
@@ -8,7 +8,6 @@
 
 namespace IMATHUZH\Qfq\Core;
 
-use DOMDocument;
 use HTMLPurifier;
 use IMATHUZH\Qfq\Core\Database\Database;
 use IMATHUZH\Qfq\Core\Form\FormAction;
@@ -408,7 +407,7 @@ class Save {
         $formValues = $this->createEmptyTemplateGroupElements($formValues);
 
         $feColumnTypes = array();
-        foreach($this->feSpecNative as $fe) {
+        foreach ($this->feSpecNative as $fe) {
             $feColumnTypes[$fe['name']] = $fe['type'];
         }
 
@@ -448,19 +447,19 @@ class Save {
             $this->store->setVar(SYSTEM_FORM_ELEMENT, "Column: $column", STORE_SYSTEM);
 
             // Convert time to datetime if mysql column is datetime, keep date if given
-            if($tableColumnTypes[$column] === DB_COLUMN_TYPE_DATETIME && $feColumnTypes[$column] === FE_TYPE_TIME) {
+            if ($tableColumnTypes[$column] === DB_COLUMN_TYPE_DATETIME && $feColumnTypes[$column] === FE_TYPE_TIME) {
                 $actualDate = explode(' ', $this->store->getVar($column, STORE_RECORD), 2)[0];
-                if($actualDate === '0000-00-00' || !isset($actualDate)) {
+                if ($actualDate === '0000-00-00' || !isset($actualDate)) {
                     $formValues[$column] = '2000-01-01 ' . $formValues[$column];
                 } else {
-                    $formValues[$column] = $actualDate.' '.$formValues[$column];
+                    $formValues[$column] = $actualDate . ' ' . $formValues[$column];
                 }
             }
 
             // Convert date to datetime if mysql column is datetime, keep time if given
-            if($tableColumnTypes[$column] === DB_COLUMN_TYPE_DATETIME && $feColumnTypes[$column] === FE_TYPE_DATE) {
+            if ($tableColumnTypes[$column] === DB_COLUMN_TYPE_DATETIME && $feColumnTypes[$column] === FE_TYPE_DATE) {
                 $actualTime = explode(' ', $this->store->getVar($column, STORE_RECORD), 2)[1];
-                $formValues[$column] = $formValues[$column].' '.$actualTime;
+                $formValues[$column] = $formValues[$column] . ' ' . $actualTime;
 
             }
 
@@ -472,7 +471,7 @@ class Save {
             }
 
             // Check for existing htmlAllow and strip tags, purify html result to prevent XSS
-            if(isset($feSpecsTags[$column]) && $feSpecsTags[$column] !== '') {
+            if (isset($feSpecsTags[$column]) && $feSpecsTags[$column] !== '') {
                 $formValues[$column] = $this->custom_strip_tags($formValues[$column], $feSpecsTags[$column]);
                 $formValues[$column] = $this->purifierHtml($formValues[$column]);
             }
@@ -510,26 +509,32 @@ class Save {
 
     /**
      * Get for every formElement htmlAllow tags from parameter
+     *
      * @param $feSpecNative
      * @param $formValues
      * @return array
      */
     private function getHtmlAllowTags($feSpecNative, $formValues): array {
+
         $feSpecsTags = array();
+
         foreach ($feSpecNative as $formElement) {
             foreach ($formValues as $keyName => $keyValue) {
-                if($formElement[FE_NAME] === $keyName) {
-                    if(isset($formElement[FE_HTML_ALLOW]) && $formElement[FE_HTML_ALLOW] !== '') {
+                if ($formElement[FE_NAME] === $keyName) {
+                    if (isset($formElement[FE_HTML_ALLOW]) && $formElement[FE_HTML_ALLOW] !== '') {
                         $feSpecsTags[$keyName] = $formElement[FE_HTML_ALLOW];
                     }
                 }
             }
         }
+
         return $feSpecsTags;
     }
 
     /**
-     * For tinyMce there are specific tags needed for lists and text decoration (underline, strikethrough). These tags should be added here.
+     * For TinyMCE there are specific tags needed for lists and text decoration (underline, strikethrough).
+     * These tags should be added here.
+     *
      * @param $feSpecsTags
      * @return array
      */
@@ -539,7 +544,7 @@ class Save {
         $tableFlag = false;
         $strongFlag = false;
         foreach ($feSpecsTags as $key => $value) {
-            $feSpecsTagArray[$key] = explode(',',$value);
+            $feSpecsTagArray[$key] = explode(',', $value);
             foreach ($feSpecsTagArray[$key] as $key2 => $tag) {
                 switch ($tag) {
                     case 'ul':
@@ -552,38 +557,38 @@ class Save {
                     case 'del':
                     case 's':
                         $decorationFlag = true;
-                    break;
+                        break;
                     case 'table':
                         $tableFlag = true;
-                    break;
+                        break;
                     case 'b':
                         $strongFlag = true;
-                    break;
+                        break;
                     default:
                         $feSpecsTagArray[$key][$key2] = $tag;
-                    break;
+                        break;
                 }
             }
 
-            if($listFlag) {
+            if ($listFlag) {
                 $feSpecsTagArray[$key][] = "li";
                 $listFlag = false;
             }
 
-            // In case of tinyMce span is automatically used for underline and strikethrough
-            if($decorationFlag) {
+            // In case of TinyMCE span is automatically used for underline and strikethrough
+            if ($decorationFlag) {
                 $feSpecsTagArray[$key][] = "span";
                 $decorationFlag = false;
             }
 
 
-            if($strongFlag) {
+            if ($strongFlag) {
                 $feSpecsTagArray[$key][] = "strong";
                 $strongFlag = false;
             }
 
-            if($tableFlag) {
-                array_push($feSpecsTagArray[$key],"th","td","tr","tbody","thead");
+            if ($tableFlag) {
+                array_push($feSpecsTagArray[$key], "th", "td", "tr", "tbody", "thead");
                 $tableFlag = false;
             }
 
@@ -1766,14 +1771,14 @@ class Save {
      * @return string
      */
     function custom_strip_tags($html, string $allowedTags) {
-        $allowed_tags = explode(',',$allowedTags);
-        $allowed_tags = array_map(strtolower,$allowed_tags);
+        $allowed_tags = explode(',', $allowedTags);
+        $allowed_tags = array_map(strtolower, $allowed_tags);
         $regex_tags = '/<\/?([^>\s]+)[^>]*>/i';
         $matches = array();
         preg_match_all($regex_tags, $html, $matches);
         $rhtml = preg_replace_callback($regex_tags, function ($matches) use (&$allowed_tags) {
-            return in_array(strtolower($matches[1]),$allowed_tags)?$matches[0]:'';
-        },$html);
+            return in_array(strtolower($matches[1]), $allowed_tags) ? $matches[0] : '';
+        }, $html);
         return $rhtml;
     }
 
-- 
GitLab