From f1f32845d30de87ccd37c367488ac4d1a4a64244 Mon Sep 17 00:00:00 2001
From: Henning Leutz <leutz@pcsg.de>
Date: Wed, 7 Oct 2020 14:48:28 +0200
Subject: [PATCH] refactor: bricks can no determine if the brick is cacheable
 or not - simple contact is not cachable

---
 bricks.xml                                |  2 +-
 src/QUI/Bricks/Brick.php                  | 47 ++++++++++++++---------
 src/QUI/Bricks/Controls/SimpleContact.php |  2 +
 src/QUI/Bricks/Utils.php                  |  7 ++++
 4 files changed, 38 insertions(+), 20 deletions(-)

diff --git a/bricks.xml b/bricks.xml
index 01a81c9..781cba7 100644
--- a/bricks.xml
+++ b/bricks.xml
@@ -1264,4 +1264,4 @@
         </brick>
 
     </bricks>
-</quiqqer>
\ No newline at end of file
+</quiqqer>
diff --git a/src/QUI/Bricks/Brick.php b/src/QUI/Bricks/Brick.php
index f1bf26b..5193a5f 100644
--- a/src/QUI/Bricks/Brick.php
+++ b/src/QUI/Bricks/Brick.php
@@ -85,7 +85,8 @@ public function __construct($params = [])
             'width'         => '',
             'classes'       => '',
             'frontendTitle' => '',
-            'hasContent'    => 1
+            'hasContent'    => 1,
+            'cacheable'     => 1    // if the brick is cacheable or not
         ];
 
         $this->setAttributes($default);
@@ -276,27 +277,29 @@ public function create()
                      .'/'
                      .$this->hash;
 
-        try {
-            $data       = QUI\Cache\Manager::get($cacheName);
-            $cssFiles   = $data['cssFiles'];
-            $cssClasses = $data['cssClasses'];
+        if ($this->getAttribute('cacheable')) {
+            try {
+                $data       = QUI\Cache\Manager::get($cacheName);
+                $cssFiles   = $data['cssFiles'];
+                $cssClasses = $data['cssClasses'];
 
-            if (\is_array($cssClasses)) {
-                foreach ($cssClasses as $cssClass) {
-                    $this->addCSSClass($cssClass);
+                if (\is_array($cssClasses)) {
+                    foreach ($cssClasses as $cssClass) {
+                        $this->addCSSClass($cssClass);
+                    }
                 }
-            }
 
-            if (\is_array($cssFiles)) {
-                foreach ($cssFiles as $cssFile) {
-                    QUI\Control\Manager::addCSSFile($cssFile);
+                if (\is_array($cssFiles)) {
+                    foreach ($cssFiles as $cssFile) {
+                        QUI\Control\Manager::addCSSFile($cssFile);
+                    }
                 }
-            }
 
-            if (!empty($data['html'])) {
-                return $data['html'];
+                if (!empty($data['html'])) {
+                    return $data['html'];
+                }
+            } catch (\Exception $Exception) {
             }
-        } catch (\Exception $Exception) {
         }
 
         if ($this->getAttribute('type') == 'content') {
@@ -336,9 +339,7 @@ public function create()
             }
 
             foreach ($classes as $class) {
-                $class = \trim($class);
-
-                $_classes[] = $class;
+                $_classes[] = \trim($class);
             }
 
             $_classes   = \array_unique($_classes);
@@ -375,6 +376,10 @@ public function create()
             $Control->addCSSClass($this->getAttribute('classes'));
         }
 
+        if ($Control->existsAttribute('cacheable')) {
+            $Control->setAttribute('cacheable', $Control->getAttribute('cacheable'));
+        }
+
         // workaround wegen title bug
         // @todo backendTitle einführen und title als frontend Title nutzen (Versionssprung)
         $Control->setAttribute('title', $this->getAttribute('frontendTitle'));
@@ -447,6 +452,10 @@ protected function getControl()
 
         $this->Control = $Control;
 
+        if ($this->Control->existsAttribute('cacheable')) {
+            $this->setAttribute('cacheable', $this->Control->getAttribute('cacheable'));
+        }
+
         return $Control;
     }
 
diff --git a/src/QUI/Bricks/Controls/SimpleContact.php b/src/QUI/Bricks/Controls/SimpleContact.php
index 985eccd..86a1475 100644
--- a/src/QUI/Bricks/Controls/SimpleContact.php
+++ b/src/QUI/Bricks/Controls/SimpleContact.php
@@ -37,6 +37,8 @@ public function __construct($attributes = [])
 
         parent::__construct($attributes);
 
+        $this->setAttribute('cacheable', 0);
+
         if (!isset($attributes['showPrivacyPolicyCheckbox'])) {
             $Site = $this->getSite();
 
diff --git a/src/QUI/Bricks/Utils.php b/src/QUI/Bricks/Utils.php
index 850a486..a2b388a 100644
--- a/src/QUI/Bricks/Utils.php
+++ b/src/QUI/Bricks/Utils.php
@@ -125,12 +125,18 @@ public static function parseAreaToArray(\DOMElement $Brick, \DOMXPath $Path)
         $name    = $Brick->getAttribute('name');
 
         $hasContent = 1;
+        $cacheable  = 1;
 
         if ($Brick->hasAttribute('hasContent')
             && (int)$Brick->getAttribute('hasContent') === 0) {
             $hasContent = 0;
         }
 
+        if ($Brick->hasAttribute('cacheable')
+            && (int)$Brick->getAttribute('cacheable') === 0) {
+            $cacheable = 0;
+        }
+
         $title       = [];
         $description = [];
 
@@ -154,6 +160,7 @@ public static function parseAreaToArray(\DOMElement $Brick, \DOMXPath $Path)
         return [
             'control'     => $control,
             'hasContent'  => $hasContent,
+            'cacheable'   => $cacheable,
             'name'        => $name,
             'title'       => $title,
             'description' => $description,
-- 
GitLab