Form.php 5 KB
Newer Older
1
2
3
4
5
6
7
8
<?php
/**
 * Created by PhpStorm.
 * User: ep
 * Date: 12/23/15
 * Time: 6:33 PM
 */

Carsten  Rose's avatar
Carsten Rose committed
9

10
11
namespace qfq;

Carsten  Rose's avatar
Carsten Rose committed
12
use qfq;
Carsten  Rose's avatar
Carsten Rose committed
13
use qfq\exceptions\UserException;
14
15
use qfq\exceptions\CodeException;
use qfq\exceptions\DbException;
16
use qfq\helper;
17
use qfq\store;
Carsten  Rose's avatar
Carsten Rose committed
18

19
require_once(__DIR__ . '/../qfq/store/Store.php');
Carsten  Rose's avatar
Carsten Rose committed
20
require_once(__DIR__ . '/../qfq/Constants.php');
Carsten  Rose's avatar
Carsten Rose committed
21
require_once(__DIR__ . '/../qfq/helper/KeyValueStringParser.php');
Carsten  Rose's avatar
Carsten Rose committed
22
require_once(__DIR__ . '/../qfq/exceptions/UserException.php');
23
24
require_once(__DIR__ . '/../qfq/exceptions/CodeException.php');
require_once(__DIR__ . '/../qfq/exceptions/DbException.php');
25
require_once(__DIR__ . '/../qfq/Database.php');
26
require_once(__DIR__ . '/../qfq/FormBuildPlain.php');
27

28
class Form {
Carsten  Rose's avatar
Carsten Rose committed
29

30
    protected $config = null;
Carsten  Rose's avatar
Carsten Rose committed
31
    protected $store = null;
32
33
34
35
36
37
38
    protected $db = null;
    protected $formDef = array();  // copy of the loaded form
    protected $feDefAction = array(); // copy of all formElement.class='action' of the loaded form
    protected $feDefNative = array(); // copy of all formElement.class='native' of the loaded form
    protected $fe = array(); // current processed formElement


39
40
41
42
43
44
//    protected $formElements = null;
//    protected $userLog = null;

    /*
     *
     */
45
    public function __construct($bodytext = '') {
46
47
48
49
50
51
52
53
54
55
56
57

        /*
         * TODO:
         *  Preparation: setup logging, database access, record locking
         *  fill stores
         *  Check permission_create / permission_update
         *  Multi: iterate over all records, Single: activate record
         *      Check mode: Load | Save
         *      doActions 'Before'
         *      Do all FormElements
         *      doActions 'After'
         */
Carsten  Rose's avatar
Carsten Rose committed
58

59
        $this->store = \qfq\store\Store::getInstance($bodytext);
60
        $this->db = new Database();
Carsten  Rose's avatar
Carsten Rose committed
61
62
    }

Carsten  Rose's avatar
Carsten Rose committed
63
64
65
66
    /*
     *
     */

67
    public function process() {
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
        $html = '';
        $build = null;

//        render:
//        plain
//        multimode: none

        try {
            // Form action: load or save?
            $mode = ($this->store->getVar(CLIENT_POST_SIP, STORE_CLIENT) === false) ? FORM_LOAD : FORM_SAVE;

            $this->loadFormDefinition();

            // TODO: replace switch() by Marschaller
            // render: FormPlain | FormBootstrap
            switch ($this->formDef['render']) {
                case 'Plain':
                    $build = new FormBuildPlain($this->formDef, $this->feDefAction, $this->feDefNative);
                    break;
                case 'Bootstrap':
                    //TODO: implement bootstrap rendering: FormBuildBootstrap
                    break;
                default:
                    throw new CodeException("this statement should never be reached", ERROR_CODE_SHOULD_NOT_HAPPEN);
            }

            switch ($mode) {
                case FORM_LOAD:
//                    $this->formActionBefore();
                    $html .= $build->head();
                    $html .= $build->elements();
                    $html .= $build->tail();
//                    $this->formActionAfter();
                    break;
                case FORM_SAVE:
                    break;
                default:
                    throw new CodeException("this statement should never be reached", ERROR_CODE_SHOULD_NOT_HAPPEN);
            }
            return $html;

        } catch (UserException $e) {
            echo $e->formatMessage();
        } catch (CodeException $e) {
            echo $e->formatMessage();
        } catch (DbException $e) {
            echo $e->formatMessage();
        } catch (\Exception $e) {
            echo "Generic Exception: " . $e->getMessage();
        }
Carsten  Rose's avatar
Carsten Rose committed
118

Carsten  Rose's avatar
Carsten Rose committed
119

120
121
    }

122
123
124
    /**
     * @throws DbException
     */
Carsten  Rose's avatar
Carsten Rose committed
125
    private function loadFormDefinition() {
Carsten  Rose's avatar
Carsten Rose committed
126

Carsten  Rose's avatar
Carsten Rose committed
127
        $formName = $this->getFormName();
128
        $this->formDef = $this->db->sql("SELECT * FROM Form AS f WHERE f.name LIKE ? AND f.deleted='no'", ROW_EXACT_1, [$formName]);
Carsten  Rose's avatar
Carsten Rose committed
129

130
131
132
        $sql = "SELECT * FROM FormElement AS fe WHERE fe.formId = ? AND fe.deleted='no' AND fe.class = ? AND fe.enabled='yes' ORDER BY fe.order, fe.id";
        $this->feDefAction = $this->db->sql($sql, ROW_REGULAR, [$this->formDef["id"], 'action']);
        $this->feDefNative = $this->db->sql($sql, ROW_REGULAR, [$this->formDef["id"], 'native']);
Carsten  Rose's avatar
Carsten Rose committed
133
134
    }

Carsten  Rose's avatar
Carsten Rose committed
135
136
137
138
    /**
     * @return string
     * @throws UserException
     * @throws exceptions\CodeException
Carsten  Rose's avatar
Carsten Rose committed
139
140
     */

Carsten  Rose's avatar
Carsten Rose committed
141
142
    private function getFormName() {

143
        // store: T3 Bodytext
Carsten  Rose's avatar
Carsten Rose committed
144
145
146
147
        $formName = $this->store->getVar(T3_BODYTEXT_FORM, STORE_T3_BODYTEXT);
        if ($formName !== false)
            return $formName;

148
        // store: SIP
Carsten  Rose's avatar
Carsten Rose committed
149
150
151
152
//        $fomName = $this->store->getVar(????_FORM, STORE_SIP);
//        if($formName !== false)
//            return $formName;

153
        // store: CLIENT
Carsten  Rose's avatar
Carsten Rose committed
154
        $formName = $this->store->getVar(CLIENT_FORM, STORE_CLIENT);
Carsten  Rose's avatar
Carsten Rose committed
155
156
        if ($formName !== false)
            return $formName;
Carsten  Rose's avatar
Carsten Rose committed
157

158
159
        // TODO: phpunit tests all - missing formname always fires this exception
//        throw new UserException("Missing form name. Not found in T3_BODYTEXT / SIP / GET", ERROR_MISSING_FORM_NAME);
160
    }
Carsten  Rose's avatar
Carsten Rose committed
161

162
}