aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/forms/Form.php
blob: c3a490a46ac82be2ec86c910f931193373d3dfb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php

namespace Studip\Forms;

class Form extends Part
{

    //models:
    protected $afterStore = [];

    //internals
    protected $inputs = [];
    protected $parts = [];

    //appearance in html-form
    protected $url = null;
    protected $autoStore = false;
    protected $collapsable = false;

    //to identify a form element
    protected $id = null;

    /**
     * Creates a new Form object from a SORM object so that each field of the db-table becomes
     * an input-field of the form. You can modify the form by the params.
     * @param \SimpleORMap $object
     * @param array $params
     * @param string|null $url
     * @return Form
     */
    public static function fromSORM(\SimpleORMap $object, $params = [], $url = null)
    {
        $form = static::create();
        $form->addSORM($object, $params);
        if ($url) {
            $form->setURL($url);
        }
        return $form;
    }


    /**
     * A static constructor for an empty Form object.
     * @return Form
     */
    public static function create() : Form
    {
        $form = new static();
        return $form;
    }

    /**
     * Finalized constructor.
     *
     * @param mixed[] ...$parts
     */
    final public function __construct(...$parts)
    {
        parent::__construct(...$parts);
    }

    /**
     * Adds a new Fieldset to the Form object with the SORM object's fields as
     * input fields. These fields can be modified or specified by the $params array.
     * @param \SimpleORMap $object
     * @param array $params
     * @return Form $this
     */
    public function addSORM(\SimpleORMap $object, array $params = [])
    {
        $metadata = $object->getTableMetadata();

        if ($params['fields']) {
            //Setting the label
            foreach ($params['fields'] as $fieldname => $fielddata) {
                if (is_string($fielddata)) {
                    $params['fields'][$fieldname] = [
                        'label' => $fielddata
                    ];
                }
            }
            //Setting the type and name
            foreach ($params['fields'] as $fieldname => $fielddata) {
                if (is_array($fielddata)) {
                    $meta = $metadata['fields'][$fieldname];
                    if (!isset($fielddata['type'])) {
                        if ($meta) {
                            $fielddata = array_merge(Input::getFielddataFromMeta($meta, $object), $fielddata);
                        } else {
                            $fielddata['type'] = 'text';
                        }

                        $params['fields'][$fieldname] = $fielddata;
                    }
                    $params['fields'][$fieldname]['name'] = $fieldname;
                }
            }
        } else {
            foreach ($metadata['fields'] as $attribute => $meta) {
                if (!in_array($attribute, (array) $params['without'])) {
                    $fielddata = [
                        'label' => $attribute
                    ];
                    $fielddata = array_merge(Input::getFielddataFromMeta($meta, $object), $fielddata);

                    $params['fields'][$attribute] = $fielddata;
                }
            }
        }
        foreach ($params['fields'] as $fieldname => $fielddata) {
            if (is_array($fielddata) && !array_key_exists('value', $fielddata)) {
                if ($object->isField($fieldname)) {
                    $params['fields'][$fieldname]['value'] = $object[$fieldname];
                }
            }
        }
        foreach ((array) $params['types'] as $fieldname => $type) {
            $params['fields'][$fieldname]['type'] = $type;
        }
        //respect the without param:
        foreach ((array) $params['without'] as $fieldname) {
            unset($params['fields'][$fieldname]);
        }
        $fields = $params['fields'];

        //Now initializing the fieldset:
        $fieldset = new Fieldset($params['legend'] ?: _("Daten"));
        $fieldset->setContextObject($object);
        $this->addPart($fieldset);

        foreach ($fields as $fieldname => $fielddata) {
            if (is_array($fielddata)) {
                $fieldset->addInput($fieldset->getInputFromArray($fielddata));
            } elseif(is_subclass_of($fielddata, Part::class)) {
                $fieldset->addPart($fielddata);
            } elseif(is_subclass_of($fielddata, Input::class)) {
                $fieldset->addInput($fielddata);
            }
        }
        return $this;
    }

    /**
     * Sets the URL where the Form should be leading after submitting.
     * @param $url
     * @return Form $this
     */
    public function setURL($url)
    {
        $this->url = $url;
        return $this;
    }

    /**
     * Returns the URL where the Form is leading to after the submit.
     * @return string|null
     */
    public function getURL()
    {
        return $this->url;
    }

    public function setCollapsable($collapsing = true)
    {
        $this->collapsable = $collapsing;
        return $this;
    }

    public function isCollapsable()
    {
        return $this->collapsable;
    }

    /**
     * Stores the Form object if this is a POST-request. This also erases the URL so that the auto-save URL
     * will be set automatically to the current $_SERVER['REQUEST_URI'].
     * @return $this
     * @throws \AccessDeniedException
     */
    public function autoStore()
    {
        $this->autoStore = true;
        if (\Request::isPost() && \Request::isAjax() && !\Request::isDialog()) {
            $this->store();
            \PageLayout::postSuccess(_('Daten wurden gespeichert.'));
            die();
        }
        return $this;
    }

    public function isAutoStoring()
    {
        return $this->autoStore;
    }

    /**
     * Adds a callback function that is executed right after the store-method. That callback receives this
     * Form object as the only parameter.
     * @param callable $c
     * @return Form $this
     */
    public function addAfterStoreCallback(Callable $c)
    {
        $this->afterStore[] = $c;
        return $this;
    }

    /**
     * Sets the ID if this form. This ID is only relevant for plugins to identify this Form object.
     * @param string|null $id
     * @return Form $this
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * Returns the ID if this form. This ID is only relevant for plugins to identify this Form object.
     * @return string|null
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Returns the number of storing processes
     * @return: a number of storing processes. 0 if nothing was stored.
     */
    public function store()
    {
        if (!\CSRFProtection::verifyRequest()) {
            throw new \AccessDeniedException();
        }
        \NotificationCenter::postNotification('FormWillStore', $this);

        $stored = 0;

        //store by each input
        foreach ($this->getAllInputs() as $input) {
            $value = $this->getStorableValueFromRequest($input);
            if ($value !== null) {
                $callback = $this->getStoringCallback($input);
                $stored += $callback($value, $input);
            }
        }

        foreach ($this->parts as $part) {
            $context = $part->getContextObject();
            if ($context && method_exists($context, 'store')) {
                $stored += $context->store();
            }
        }

        foreach ($this->afterStore as $callback) {
            if (is_callable($callback)) {
                $stored += call_user_func($callback, $this);
            } else {
                //throw warning if callback is not available:
                if ($callback === null) {
                    $callback = 'NULL';
                }
                trigger_error(sprintf('Could not execute callback %s in Form object.', $callback), E_USER_WARNING);
            }
        }
        return $stored;
    }

    /**
     * Adds a Part object to this form like a fieldset
     * @param Part $part
     * @return Form|void
     */
    public function addPart(Part $part)
    {
        $part->setParent($this);
        $this->parts[] = $part;
    }

    /**
     * Returns all the Part objects like Fieldsets as an array.
     * @return array
     */
    public function getParts() : array
    {
        return $this->parts;
    }

    /**
     * Returns the last part of the form. If there is none yet, it will create a fieldset and return that.
     * @return Part
     */
    public function getLastPart() : Part
    {
        if (count($this->parts) === 0) {
            $this->parts[] = new Fieldset();
        }
        return $this->parts[count($this->parts) - 1];
    }

    /**
     * Renders the whole form as a string.
     * @return string
     * @throws \Flexi_TemplateNotFoundException
     */
    public function render()
    {
        \NotificationCenter::postNotification('FormWillRender', $this);
        $template = $GLOBALS['template_factory']->open('forms/form');
        $template->form = $this;
        return $template->render();
    }

    /**
     * Returns the function to be used to store the value into the input. If the given Input has no storing
     * function it will generate a Closuer to set the value to the SimpleORMap context object.
     * @param $input
     * @return \Closure|void
     */
    protected function getStoringCallback(Input $input)
    {
        if ($input->store) {
            return $input->store;
        }
        $context = $input->getParent()->getContextObject();
        if ($context && is_subclass_of($context, \SimpleORMap::class)) {
            return function ($value) use ($context, $input) {
                $context[$input->getName()] = $value;
            };
        }
    }

    /**
     * Returns the value for the Input object from the $_REQUEST. This value will also be mapped by
     * the Input's dataMapper function and after that by a special mapper-callback the Input
     * probably has.
     * @param Input $input
     * @return mixed
     */
    protected function getStorableValueFromRequest(Input $input)
    {
        $requestparam = $input->getName();
        $bracket_pos = strpos($requestparam, "[");
        if ($bracket_pos !== false) {
            $requestparam = substr($requestparam, 0, $bracket_pos);
            $value = \Request::getArray($requestparam);
            foreach ($value as $i => $v) {
                $value[$i] = $input->dataMapper($v);
            }
        } else {
            $value = $input->getRequestValue();
            $value = $input->dataMapper($value);
        }
        if ($input->mapper && is_callable($input->mapper)) {
            $mapper = $input->mapper;
            $value = $mapper($value, $input->getContextObject());
        }
        return $value;
    }
}