Form.php 8.66 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;
13
14
15
use qfq\BuildFormPlain;
use qfq\BuildFormTable;
use qfq\BuildFormBootstrap;
Carsten  Rose's avatar
Carsten Rose committed
16
use qfq\exceptions\UserException;
17
18
use qfq\exceptions\CodeException;
use qfq\exceptions\DbException;
19
20
//use qfq\helper;
use qfq\Store;
Carsten  Rose's avatar
Carsten Rose committed
21

Carsten  Rose's avatar
Carsten Rose committed
22

23
require_once(__DIR__ . '/../qfq/store/Store.php');
Carsten  Rose's avatar
Carsten Rose committed
24
require_once(__DIR__ . '/../qfq/Constants.php');
Carsten  Rose's avatar
Carsten Rose committed
25
require_once(__DIR__ . '/../qfq/helper/KeyValueStringParser.php');
26
require_once(__DIR__ . '/../qfq/helper/HelperFormElement.php');
Carsten  Rose's avatar
Carsten Rose committed
27
require_once(__DIR__ . '/../qfq/exceptions/UserException.php');
28
29
require_once(__DIR__ . '/../qfq/exceptions/CodeException.php');
require_once(__DIR__ . '/../qfq/exceptions/DbException.php');
30
require_once(__DIR__ . '/../qfq/Database.php');
Carsten  Rose's avatar
Carsten Rose committed
31
require_once(__DIR__ . '/../qfq/Evaluate.php');
32
33
34
require_once(__DIR__ . '/../qfq/BuildFormPlain.php');
require_once(__DIR__ . '/../qfq/BuildFormTable.php');
require_once(__DIR__ . '/../qfq/BuildFormBootstrap.php');
35

36
37
38
39
40
41
42
43
44
45
46
47
/*
 * Form will be called
 * a) with a SIP identifier, or
 * b) without a SIP identifier (form setting has to allow this) and will create on the fly a new SIP.
 *
 * The SIP-Store stores:
 *  form=<formname>
 *  r=<record id>  (table.id for a single record form)
 *  keySemId,keySemIduser
 *  <further individual variables>
 */

Carsten  Rose's avatar
Carsten Rose committed
48
49
50
51
/**
 * Class Form
 * @package qfq
 */
52
class Form {
Carsten  Rose's avatar
Carsten Rose committed
53

54
    /**
55
     * @var \qfq\Store instantiated class
56
     */
Carsten  Rose's avatar
Carsten Rose committed
57
    protected $store = null;
58
59
60
    /**
     * @var Database instantiated class
     */
61
    protected $db = null;
62
63
64
65
66
    /**
     * @var Evaluate instantiated class
     */
    protected $eval = null;

67
68
69
    protected $formSpec = array();  // Form Definition: copy of the loaded form
    protected $feSpecAction = array(); // FormEelement Definition: all formElement.class='action' of the loaded form
    protected $feSpecNative = array(); // FormEelement Definition: all formElement.class='native' of the loaded form
70
71
72
73
74
75
76
77
78
79
80
81

    /*
     * 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'
     */
82
83


84
85
86
87
88
89
//    protected $formElements = null;
//    protected $userLog = null;

    /*
     *
     */
90
    public function __construct($bodytext = '') {
91

Carsten  Rose's avatar
Carsten Rose committed
92
        $this->store = Store::getInstance($bodytext);
93
        $this->db = new Database();
94
        $this->eval = new Evaluate($this->store, $this->db);
Carsten  Rose's avatar
Carsten Rose committed
95
96
    }

97
98
    /**
     * @return string
Carsten  Rose's avatar
Carsten Rose committed
99
     */
100
    public function process() {
101
102
        $html = '';
        $build = null;
103
        $master = null;
104

Carsten  Rose's avatar
Carsten Rose committed
105
106
        mb_internal_encoding("UTF-8");

107
108
109
110
111
112
113
114
//        render:
//        plain
//        multimode: none

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

115
            $formName = $this->loadFormSpecification();
116

Carsten  Rose's avatar
Carsten Rose committed
117
            $sipFound = $this->validateForm();
118
            if (!$sipFound) {
Carsten  Rose's avatar
Carsten Rose committed
119
                $this->store->createSipAfterFormLoad($formName);
120
            }
121

122
            $this->store->fillStoreTableDefaultColumnType($this->formSpec['tableName']);
123
124

            switch ($this->formSpec['render']) {
125
                case 'plain':
126
127
                    $build = new BuildFormPlain($this->formSpec, $this->feSpecAction, $this->feSpecNative);
                    break;
128
                case 'table':
129
                    $build = new BuildFormTable($this->formSpec, $this->feSpecAction, $this->feSpecNative);
130
131
                    break;
                case 'bootstrap':
132
                    $build = new BuildFormBootstrap($this->formSpec, $this->feSpecAction, $this->feSpecNative);
133
134
                    break;
                default:
135
                    throw new CodeException("This statement should never be reached", ERROR_CODE_SHOULD_NOT_HAPPEN);
136
137
138
139
            }

            switch ($mode) {
                case FORM_LOAD:
140
                    $html .= $build->process();
141
142
143
144
                    break;
                case FORM_SAVE:
                    break;
                default:
145
                    throw new CodeException("This statement should never be reached", ERROR_CODE_SHOULD_NOT_HAPPEN);
146
147
148
149
150
151
152
153
154
155
156
157
            }
            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
158

159
        return $html;
160
161
    }

162
    /**
163
164
165
166
167
168
169
     * Loads specification of recent form.
     * Evaluates Form and all FormElements.
     *
     * Loaded Form is in  $this->formSpec
     * Loaded 'action' FormElements are in $this->feSpecAction
     * Loaded 'native' FormElements are in $this->feSpecNative
     *
170
     * @return string formName
171
     * @throws DbException
172
     * @throws UserException
173
     */
174
    private function loadFormSpecification() {
Carsten  Rose's avatar
Carsten Rose committed
175

Carsten  Rose's avatar
Carsten Rose committed
176
        $formName = $this->getFormName();
177
        $this->store->setVar(SYSTEM_FORM, $formName, STORE_SYSTEM);
Carsten  Rose's avatar
Carsten Rose committed
178

179
        $this->formSpec = $this->eval->parseArray($this->db->sql("SELECT * FROM Form AS f WHERE f.name LIKE ? AND f.deleted='no'", ROW_EXACT_1, [$formName], 'Form not found or multiple forms with the same name.'));
Carsten  Rose's avatar
Carsten Rose committed
180
        HelperFormElement::explodeParameter($this->formSpec);
181

Carsten  Rose's avatar
Carsten Rose committed
182
        // Clear
183
184
        $this->store->setVar(SYSTEM_FORM_ELEMENT, '', STORE_SYSTEM);

185
186
187
188
189
190
191
192
193
194
        // "SELECT *, ? AS 'nestedInFieldSet' FROM FormElement AS fe WHERE fe.formId = ? AND fe.deleted = 'no' AND FIND_IN_SET(fe.class, ? ) AND fe.feIdContainer = ? AND fe.enabled='yes' ORDER BY fe.ord, fe.id";
        $sql = SQL_FORM_ELEMENT;

        // FE: Action
        $this->feSpecAction = $this->eval->parseArray($this->db->sql($sql, ROW_REGULAR, ['no', $this->formSpec["id"], 'action', 0]));
        HelperFormElement::explodeParameter($this->feSpecAction);

        // FE: Native & Container
        $this->feSpecNative = $this->db->sql($sql, ROW_REGULAR, ['no', $this->formSpec["id"], 'native,container', 0]);
        HelperFormElement::explodeParameter($this->feSpecNative);
195
196

        return $formName;
Carsten  Rose's avatar
Carsten Rose committed
197
198
    }

Carsten  Rose's avatar
Carsten Rose committed
199
200
201
202
    /**
     * @return string
     * @throws UserException
     * @throws exceptions\CodeException
Carsten  Rose's avatar
Carsten Rose committed
203
204
     */

Carsten  Rose's avatar
Carsten Rose committed
205
206
    private function getFormName() {

207
        $formName = $this->store->getVar(SIP_FORM, STORE_TYPO3 . STORE_SIP . STORE_CLIENT);
Carsten  Rose's avatar
Carsten Rose committed
208
209
210
        if ($formName !== false)
            return $formName;

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

215
216
217
218
219
    /**
     * @return bool - 'true' if SIP exists, else 'false'
     * @throws CodeException
     * @throws UserException
     */
Carsten  Rose's avatar
Carsten Rose committed
220
    private function validateForm() {
221
222
223
224
225

        // Retrieve record_id either from SIP (prefered) or via URL
        $r = $this->store->getVar(SIP_RECORD_ID, STORE_SIP . STORE_CLIENT);

        // If there is a record_id>0: EDIT else NEW
226
        $mode = ($r > 0) ? $this->formSpec['permitEdit'] : $this->formSpec['permitNew'];
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

        $feUserLoggedIn = isset($GLOBALS["TSFE"]->fe_user->user["uid"]) && $GLOBALS["TSFE"]->fe_user->user["uid"] > 0;

        $sipFound = $this->store->getVar(SIP_SIP, STORE_SIP) !== false;

        switch ($mode) {
            case  FORM_PERMISSION_SIP:
                if (!$sipFound) {
                    throw new UserException("SIP Parameter needed for this form.", ERROR_SIP_NEEDED_FOR_THIS_FORM);
                }
                break;
            case  FORM_PERMISSION_LOGGED_IN:
                if (!$feUserLoggedIn) {
                    throw new UserException("User not logged in.", ERROR_USER_NOT_LOGGED_IN);
                }
                break;
            case FORM_PERMISSION_LOGGED_OUT:
                if ($feUserLoggedIn) {
                    throw new UserException("User logged in.", ERROR_USER_LOGGED_IN);
                }
                break;
            case FORM_PERMISSION_ALWAYS:
                break;
            case FORM_PERMISSION_NEVER:
                throw new UserException("Loading form forbidden.", ERROR_FORM_FORBIDDEN);
            default:
                throw new CodeException("Unknown permission mode: '" . $mode . "'", ERROR_FORM_UNKNOWN_PERMISSION_MODE);
        }
Carsten  Rose's avatar
Carsten Rose committed
255

Carsten  Rose's avatar
Carsten Rose committed
256
        // Form Definition valid?
257
        if ($this->formSpec['multiMode'] !== 'none' && $this->formSpec['multiSql'] === '') {
Carsten  Rose's avatar
Carsten Rose committed
258
259
260
            throw new UserException("MultiMode selected, but MultiSQL missing", ERROR_MULTI_SQL_MISSING);
        }

261
        return $sipFound;
262
    }
Carsten  Rose's avatar
Carsten Rose committed
263

264
}