From 0856b7c49738c7a38c40910013dd773a49f19d81 Mon Sep 17 00:00:00 2001
From: Carsten  Rose <carsten.rose@math.uzh.ch>
Date: Mon, 27 Feb 2017 12:29:40 +0100
Subject: [PATCH] #3253: STORE_TYPO3: steht waehrend Update / Save nicht zur
 Verfuegung - wird aber benoetigt. Bsp: feUser, beUser, pageId, ...

CODING.md: described implementation.
Store.php: fixed setting of STORE_SYSTEM / showDebugInfo during API call.
---
 doc/CODING.md                     | 35 +++++++++------
 extension/qfq/qfq/store/Store.php | 74 ++++++++++++++++++++++---------
 2 files changed, 74 insertions(+), 35 deletions(-)

diff --git a/doc/CODING.md b/doc/CODING.md
index 4345a7e60..373cc8100 100644
--- a/doc/CODING.md
+++ b/doc/CODING.md
@@ -34,7 +34,7 @@ LOAD
   * active/valid formname: [$this->store->setVar(SYSTEM_FORM, $formName, STORE_SYSTEM);]
   * SIP: [$this->store->getVar('form', STORE_SIP)]
   * All parameters from active SIP:  [$this->store->getStore(STORE_SIP)]
-  * Check Contstants.php for known Store members
+  * Check Contstants.php for known Store members: 
   
 * In QuickFormQuery.php the whole Form will be copied to `$this->formSpec` and depending on further processing, the 
   elements are available in `$this->feNative` and `$this->feAction`.
@@ -50,18 +50,27 @@ LOAD
       the record again and again. Unfortunately, the initial created SIP (before 'form load') is not uniqe anymore (multiple 
       tabs might  contain a saved 'new record'). To guarantee correct saving of r=0 records, a unique on the fly generated SIP 
       is creatd during form load - individually per browser tab.  
-
- * Formular zusammenbausen
-    * QuickFormQuery: doForm > loadFormSpecification  - laedt alle Elemente die nicht genested sind: native, pill, fieldset, templateGroup >> $his->formNative
-    * Damit wird '(BuildFormBootstrap / AbstractBuildForm) > process()' aufgerufen.
-      * Hier wird AbstractBuildForm->elements() aufgerufen (ein Aufruf fuer alle root elemente).
-      * Pro native Element (inkl. pill, fieldset, templateGroup) wird $builElementFunctionName aufgerufen. 
-        - buildText()
-        - ....
-        - buildFieldSet()       << von hier werden alle zum aktuellen 'FieldSet' gehoerenden SubElemente abgearbeitet - via AbstractBuildForm->elements()  (damit schliesst sich der Kreis und wird rekursiv)
-        - buildPill()           << von hier werden alle zum aktuellen 'Pill' gehoerenden SubElemente abgearbeitet - via AbstractBuildForm->elements()  (damit schliesst sich der Kreis und wird rekursiv)
-        - buildTemplateGroup()  << von hier werden alle zum aktuellen 'Pill' gehoerenden SubElemente abgearbeitet - via AbstractBuildForm->elements()  (damit schliesst sich der Kreis und wird rekursiv)
-        >>  
+* Faking the STORE_TYPO3 for API calls:
+  * The PHP code api/save.php, api/load.php is called directly, without any TYPO3 Framework. Therefore the Typo3 information 
+    'pageId', 'feUser*', 'beUser*',  'ttContentUid', ... is not directly available.
+  * *Form load*: an additional hidden Formelement '_sipForTypo3Vars' will be created with a subset of the current 
+    STORE_TYPO3 values. The workaround with the SIP is usefull, cause the same form can be shown on different places (QFQ records) -
+    this is not very likely, but might happen. The 'on the fly rendered' SIP helps to deliver the status.
+    AbstractBuildForm.php: process() > prepareT3VarsForSave() > Store.php: copyT3VarsToSip();
+  * *Form save*:  FillStoreForm.php: process() > Store: fillTypo3StoreFromSip()   
+
+* Formular zusammenbauen
+  * QuickFormQuery: doForm > loadFormSpecification  - laedt alle Elemente die nicht genested sind: native, pill, fieldset, templateGroup >> $his->formNative
+  * Damit wird '(BuildFormBootstrap / AbstractBuildForm) > process()' aufgerufen.
+    * Hier wird AbstractBuildForm->elements() aufgerufen (ein Aufruf fuer alle root elemente).
+    * Pro native Element (inkl. pill, fieldset, templateGroup) wird $builElementFunctionName aufgerufen. 
+      - buildText()
+      - ....
+      - buildFieldSet()       << von hier werden alle zum aktuellen 'FieldSet' gehoerenden SubElemente abgearbeitet - via AbstractBuildForm->elements()  (damit schliesst sich der Kreis und wird rekursiv)
+      - buildPill()           << von hier werden alle zum aktuellen 'Pill' gehoerenden SubElemente abgearbeitet - via AbstractBuildForm->elements()  (damit schliesst sich der Kreis und wird rekursiv)
+      - buildTemplateGroup()  << von hier werden alle zum aktuellen 'Pill' gehoerenden SubElemente abgearbeitet - via AbstractBuildForm->elements()  (damit schliesst sich der Kreis und wird rekursiv)
+      >>
+        
 SAVE
 ----
 * Via wrapper api/save.php
diff --git a/extension/qfq/qfq/store/Store.php b/extension/qfq/qfq/store/Store.php
index 24dbd9ca1..9a4ebfbc2 100644
--- a/extension/qfq/qfq/store/Store.php
+++ b/extension/qfq/qfq/store/Store.php
@@ -300,7 +300,10 @@ class Store {
     private static function adjustConfig(array $config) {
         // Adjust config
         if ($config[SYSTEM_SHOW_DEBUG_INFO] === 'auto') {
-            $config[SYSTEM_SHOW_DEBUG_INFO] = (isset($GLOBALS["TSFE"]->beUserLogin) && $GLOBALS["TSFE"]->beUserLogin === true) ? 'yes' : 'no';
+            $rc = self::beUserLoggdIn();
+            if ($rc !== false) {
+                $config[SYSTEM_SHOW_DEBUG_INFO] = $rc;
+            }
         }
 
         // make SQL PATH absolute. This is necessary to work in different directories correctly.
@@ -311,6 +314,20 @@ class Store {
         return $config;
     }
 
+    /**
+     * @return bool|string
+     */
+    private static function beUserLoggdIn() {
+
+        if (isset($GLOBALS["TSFE"])) {
+            $rc = (isset($GLOBALS["TSFE"]->beUserLogin) && $GLOBALS["TSFE"]->beUserLogin === true) ? 'yes' : 'no';
+        } else {
+            $rc = false;
+        }
+
+        return $rc;
+    }
+
     /**
      * Iterate over all Parameter which have to exist in the config. Throw an array if any is missing.
      *
@@ -716,6 +733,29 @@ class Store {
         return $tmpParam;
     }
 
+    /**
+     * Returns a complete $store.
+     *
+     * @param $store
+     * @return array
+     * @throws UserFormException
+     * @throws \qfq\CodeException
+     */
+    public static function getStore($store) {
+        // Check valid Storename
+        if (!isset(self::$sanitizeStore[$store]))
+            throw new UserFormException("Unknown Store: $store", ERROR_UNNOWN_STORE);
+
+        if ($store === STORE_ZERO)
+            throw new CodeException("getStore() for STORE_ZERO is impossible - there are no values saved.", ERROR_GET_STORE_ZERO);
+
+        if (isset(self::$raw[$store])) {
+            return self::$raw[$store];
+        }
+
+        return array();
+    }
+
     /**
      * Returns a pointer to this class.
      *
@@ -741,6 +781,10 @@ class Store {
     }
 
     /**
+     * Saves a subset of STORE_TYPO3 vars as a SIP. The SIP will be transmitted as hidden form element.
+     *
+     * More docs: CODING.md > 'Faking the STORE_TYPO3 for API calls'
+     *
      * @throws CodeException
      * @throws UserFormException
      */
@@ -763,6 +807,9 @@ class Store {
     }
 
     /**
+     * Get stored STORE_TYPO3 vars from SIP and restore the store.
+     *
+     * More docs: CODING.md > 'Faking the STORE_TYPO3 for API calls'
      *
      * @param string $sipTypo3Vars
      */
@@ -777,29 +824,12 @@ class Store {
         $typo3VarsArray = self::$sip->getVarsFromSip($sipTypo3Vars);
 
         self::setStore($typo3VarsArray, STORE_TYPO3, true);
-    }
-
-    /**
-     * Returns a complete $store.
-     *
-     * @param $store
-     * @return array
-     * @throws UserFormException
-     * @throws \qfq\CodeException
-     */
-    public static function getStore($store) {
-        // Check valid Storename
-        if (!isset(self::$sanitizeStore[$store]))
-            throw new UserFormException("Unknown Store: $store", ERROR_UNNOWN_STORE);
 
-        if ($store === STORE_ZERO)
-            throw new CodeException("getStore() for STORE_ZERO is impossible - there are no values saved.", ERROR_GET_STORE_ZERO);
-
-        if (isset(self::$raw[$store])) {
-            return self::$raw[$store];
+        // If necessary, update SYSTEM_SHOW_DEBUG_INFO
+        if (self::getVar(SYSTEM_SHOW_DEBUG_INFO, STORE_SYSTEM) == 'auto') {
+            $val = ($typo3VarsArray[TYPO3_BE_USER_LOGGED_IN] == 'yes') ? 'yes' : 'no';
+            self::setVar(SYSTEM_SHOW_DEBUG_INFO, $val, STORE_SYSTEM);
         }
-
-        return array();
     }
 
 }
-- 
GitLab