From fa3aa5535b7ca35b355a01da090f3d0581960413 Mon Sep 17 00:00:00 2001
From: Carsten  Rose <carsten.rose@math.uzh.ch>
Date: Sat, 30 Jan 2016 17:48:49 +0100
Subject: [PATCH] Form: changed detection of load/save. now based on GET/POST.
 Catch exception in __construct().

---
 qfq/Form.php | 33 +++++++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 6 deletions(-)

diff --git a/qfq/Form.php b/qfq/Form.php
index 5195bd4fe..2e03b05d4 100644
--- a/qfq/Form.php
+++ b/qfq/Form.php
@@ -84,14 +84,35 @@ class Form {
 //    protected $formElements = null;
 //    protected $userLog = null;
 
-    /*
+    /**
+     * Construct the Form Class and Store too. This is the base initialization moment.
+     *
+     * As a result of instantiating of Form, the class Store will initially called the first time and therefore instantiated automatically.
+     * Store might throw an exception, in case the URL-passed SIP is invalid.
      *
+     * @param string $bodytext
      */
     public function __construct($bodytext = '') {
 
-        $this->store = Store::getInstance($bodytext);
-        $this->db = new Database();
-        $this->eval = new Evaluate($this->store, $this->db);
+        try {
+            $this->store = Store::getInstance($bodytext);
+            $this->db = new Database();
+            $this->eval = new Evaluate($this->store, $this->db);
+        } catch (UserException $e) {
+            echo $e->formatMessage();
+            exit;
+        } catch (CodeException $e) {
+            echo $e->formatMessage();
+            exit;
+        } catch (DbException $e) {
+            echo $e->formatMessage();
+            exit;
+        } catch (\Exception $e) {
+            echo "Generic Exception: " . $e->getMessage();
+            exit;
+        }
+
+
     }
 
     /**
@@ -109,8 +130,6 @@ class Form {
 //        multimode: none
 
         try {
-            // Form action: load or save?
-            $mode = ($this->store->getVar(CLIENT_POST_SIP, STORE_CLIENT) === false) ? FORM_LOAD : FORM_SAVE;
 
             $formName = $this->loadFormSpecification();
 
@@ -135,6 +154,8 @@ class Form {
                     throw new CodeException("This statement should never be reached", ERROR_CODE_SHOULD_NOT_HAPPEN);
             }
 
+            // Form action: load or save?
+            $mode = isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'POST' ? FORM_SAVE : FORM_LOAD;
             switch ($mode) {
                 case FORM_LOAD:
                     $html .= $build->process();
-- 
GitLab