pieform.php 48.8 KB
Newer Older
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
<?php
/**
 * This program is part of Pieforms
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * @package    pieform
 * @subpackage core
 * @author     Nigel McNie <nigel@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

27
28
$GLOBALS['_PIEFORM_REGISTRY'] = array();

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
/**
 * Builds, validates and processes a form.
 *
 * Given a form definition, and as long as one or two functions are implemented
 * by the caller, this function will handle everything else.
 *
 * USAGE:
 *
 * <pre>
 * $form = array(
 *     'name' => 'myform',
 *     'action' => '/myscript.php',
 *     'method' => 'post',
 *     'elements' => array(
 *         // definition of elements in the form
 *     )
 * );
 *
 * $smarty->assign('myform', form($form));
 *
 * function myform_validate($form, $values) {
 *     // perform validation agains form elements here
 *     // some types of validation are conveniently available already as
 *     // as part of the form definition hash
 * }
 *
 * function myform_submit($values) {
 *     // perform action knowing that the values are valid, e.g. DB insert.
 * }
 * </pre>
 *
 * Please see https://eduforge.org/wiki/wiki/mahara/wiki?pagename=FormAPI for
 * more information on creating and using forms.
 *
 */
function pieform($data) {
    return Pieform::process($data);
    //
    // @todo stuff to do for forms:
    // 
    //  - more form element types (inc. types like autocomplete and date picker and wyswiyg)
    //  - support processing of data before validation occurs (e.g. trim(), strtoupper())
    //  - Basic validation is possible as there's a callback function for checking,
    //    but some helper functions could be written to make people's job validating
    //    stuff much easier (form_validate_email, form_validate_date etc).
    //  - Collapsible js for fieldsets
    //  - Grippie for textareas
    //  - javascript validation
    //  - handle multipage forms?
    //  - handle a tabbed interface type of form?
    //  
}

if (!function_exists('json_encode')) {
    function json_encode($data) {
        require_once 'JSON/JSON.php';
        $json = new Services_JSON();
        return $json->encode($data);
    }
}

/**
 * Pieform throws PieformExceptions, so you can catch them specifically
 */
class PieformException extends Exception {}

/**
 * Represents an HTML form. Forms created using this class have a lot of the
 * legwork for forms abstracted away.
 *
 * The form API makes it really easy to build complex HTML forms, simply by
 * building a hash describing your form, and defining one or two callback
 * functions.
 *
 * For more information on how the form API works, please see the documentation
 * at https://eduforge.org/wiki/wiki/mahara/wiki?pagename=FormAPI
 */
class Pieform {

    /**
     * Data for the form
     *
     * @var array
     * @todo move all of the member fields here into this field
     */
    private $data = array();

    /**
     * Maintains a tab index across all created forms, to make it easy for
     * people to forget about it and have it just work for all of their forms.
     *
     * @var int
     */
    public static $formtabindex = 1;

    /**
     * The form name. This is required.
     *
     * @var string
     */
    private $name = '';

    /**
     * The method that the form will be submitted by. Either 'get' or 'post'.
     *
     * @var string
     */
    private $method = 'get';

    /**
     * The URL that the form will be submitted to.
     *
     * @var string
     */
    private $action = '';

    /**
     * Whether the form should be validated. Forms that are not validated are
     * also not submitted. This is useful if you just want to draw a form, and
     * have no validation rules apply to it.
     */
    private $validate = true;

    /**
     * Whether the form should be checked for submission. Forms can have
     * validate on and submit off in order to validate submitted data, but to
     * not bother with the submit.
     *  
     * @var bool
     */
    private $submit = true;

    /**
     * Whether to submit the form via ajax
     *
     * @todo rename this probably, because AJAX GET is supported too
     *
     * @var bool
     */
    private $ajaxpost = false;

    /**
     * A callback to call before submitting the form via AJAX
     *
     * @var string
     */
    private $preajaxsubmitcallback = '';

    /**
     * A callback to call after submitting the form via AJAX, regardless of
     * the result of the submission
     *
     * @var string
     */
    private $postajaxsubmitcallback = '';

    /**
     * Name of a javascript function to call on successful ajax submission
     *
     * @var string
     */
    private $ajaxsuccessfunction = '';

    /**
     * Name of a javascript function to call on failed ajax submission
     *
     * @var string
     */
    private $ajaxfailurefunction = '';

    /**
     * The tab index for this particular form.
     *
     * @var int
     */
    private $tabindex = 1;

206
207
208
209
210
211
212
    /**
     * Directories to look for elements, renderers and rules
     *
     * @var array
     */
    private $configdirs = array();

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
    /**
     * Whether to autofocus fields in this form, and if so, optionally which
     * field to focus.
     *
     * @var mixed
     */
    private $autofocus = false;

    /**
     * The renderer used to build the HTML for the form that each element sits
     * in. See the form/renderer package to find out the allowed types.
     *
     * @var string
     */
    private $renderer = 'table';

    /**
     * The language used for form rule error messages.
     *
     * @var string
     */
    private $language = 'en.utf8';

    /**
     * Language strings for rules
     *
     * @var array
     */
    private $language_strings = array(
        'en.utf8' => array(
            'required'  => 'This field is required',
            'email'     => 'E-mail address is invalid',
            'maxlength' => 'This field must be at most %d characters long',
            'minlength' => 'This field must be at least %d characters long', 
            'integer'   => 'The field must be an integer',
Nigel McNie's avatar
Nigel McNie committed
248
249
            'validateoptions' => 'The option "%s" is invalid',
            'regex'     => 'This field is not in valid form'
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
        )
    );

    /**
     * Whether this form includes a file element. If so, the enctype attribute
     * for the form will be specified as "multipart/mixed" as required. This
     * is auto-detected by the Form class.
     *
     * @var bool
     */
    private $fileupload = false;

    /**
     * Whether the form has been submitted. Available through the
     * {@link is_submitted} method.
     *
     * @var bool
     */
    private $submitted = false;

    /**
     * Whether the form is cancellable or not - that is, whether sending a
     * request to cancel the form will be honoured or not. This is useful for
     * the transient login form, where it must pass on cancel requests from
     * other forms sometimes.
     *
     * @var bool
     */
    private $iscancellable = true;

    /**
     * Name of validate function
     *
     * @var string
     */ 
    private $validatefunction = '';

    /**
     * Name of submit function
     *
     * @var string
     */
    private $submitfunction = '';

    /**
     * Processes the form. Called by the {@link pieform} function. It simply
     * builds the form (processing it if it has been submitted), and returns
     * the HTML to display the form
     *
     * @param array $data The form description hash
     * @return string     The HTML representing the form
     */
    public static function process($data) {
        $form = new Pieform($data);
        return $form->build();
    }

    /**
     * Sets the attributes of the form according to the passed data, performing
     * validation on the way. If the form is submitted, this checks and processes
     * the form.
     *
     * @param array $data The form description hash
     */
    public function __construct($data) {
315
316
        $GLOBALS['_PIEFORM_REGISTRY'][] = $this;

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
        if (!isset($data['name']) || !preg_match('/^[a-z_][a-z0-9_]*$/', $data['name'])) {
            throw new PieformException('Forms must have a name, and that name must be valid (validity test: could you give a PHP function the name?)');
        }
        $this->name = $data['name'];

        // If the form has global configuration, get it now
        if (function_exists('pieform_configure')) {
            $formconfig = pieform_configure();
            $defaultelements = (isset($formconfig['elements'])) ? $formconfig['elements'] : array();
            foreach ($defaultelements as $name => $element) {
                if (!isset($data['elements'][$name])) {
                    $data['elements'][$name] = $element;
                }
            }
        }
        else {
            $formconfig = array();
        }

        // Assign defaults for the form
        $formdefaults = array(
            'method'    => 'get',
            'action'    => '',
            'ajaxpost'  => false,
            'preajaxsubmitcallback'  => '',
            'postajaxsubmitcallback' => '',
            'ajaxsuccessfunction'    => '',
            'ajaxfailurefunction'    => '',
345
346
347
348
349
350
            'configdirs' => array(),
            'autofocus'  => false,
            'language'   => 'en.utf8',
            'validate'   => true,
            'submit'     => true,
            'elements'   => array(),
351
352
353
354
355
356
357
358
359
360
361
            'submitfunction' => '',
            'validatefunction' => '',
        );
        $data = array_merge($formdefaults, $formconfig, $data);
        $this->data = $data;

        // Set the method - only get/post allowed
        $data['method'] = strtolower($data['method']);
        if ($data['method'] != 'post') {
            $data['method'] = 'get';
        }
362
363
364
365
366
367
368
369
370
        $this->method     = $data['method'];
        $this->action     = $data['action'];
        $this->validate   = $data['validate'];
        $this->submit     = $data['submit'];
        $this->configdirs = array_map(
            create_function('$a', 'return substr($a, -1) == "/" ? substr($a, 0, -1) : $a;'),
            (array) $data['configdirs']);
        $this->autofocus  = $data['autofocus'];
        $this->language   = $data['language'];
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
        
        if ($data['submitfunction']) {
            $this->submitfunction = $data['submitfunction'];
        }
        else {
            $this->submitfunction = $this->name . '_submit';
        }

        if ($data['validatefunction']) {
            $this->validatefunction = $data['validatefunction'];
        }
        else {
            $this->validatefunction = $this->name . '_validate';
        }

        if ($data['ajaxpost']) {
            $this->ajaxpost = true;
            $this->preajaxsubmitcallback  = self::validate_js_callback($data['preajaxsubmitcallback']);
            $this->postajaxsubmitcallback = self::validate_js_callback($data['postajaxsubmitcallback']);
            // @todo rename to *callback instead of *function for consistency
            $this->ajaxsuccessfunction    = self::validate_js_callback($data['ajaxsuccessfunction']);
            $this->ajaxfailurefunction    = self::validate_js_callback($data['ajaxfailurefunction']);
        }

        if (isset($data['renderer'])) {
            $this->renderer = $data['renderer'];
        }

        if (isset($data['tabindex'])) {
            $this->tabindex = intval($data['tabindex']);
        }
        else {
            $this->tabindex = self::$formtabindex++;
        }

        $this->iscancellable = (isset($data['iscancellable']) && !$data['iscancellable']) ? false : true;

        if (!is_array($data['elements']) || count($data['elements']) == 0) {
            throw new PieformException('Forms must have a list of elements');
        }
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427

        // Remove elements to ignore
        foreach ($data['elements'] as $name => $element) {
            if (isset($element['type']) && $element['type'] == 'fieldset') {
                foreach ($element['elements'] as $subname => $subelement) {
                    if (!empty($subelement['ignore'])) {
                        unset ($data['elements'][$name]['elements'][$subname]);
                    }
                }
            }
            else {
                if (!empty($element['ignore'])) {
                    unset($data['elements'][$name]);
                }
            }
        }

428
429
430
431
432
        $this->elements = $data['elements'];

        // Set some attributes for all elements
        $autofocusadded = false;
        foreach ($this->elements as $name => &$element) {
Nigel McNie's avatar
Nigel McNie committed
433
434
435
436
            // The name can be in the element itself. This is compatibility for the perl version
            if (isset($element['name'])) {
                $name = $element['name'];
            }
437
            if (count($element) == 0) {
Nigel McNie's avatar
Nigel McNie committed
438
                throw new PieformException('An element in form "' . $this->name . '" has no data (' . $name . ')');
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
            }
            if (!isset($element['type'])) {
                $element['type'] = 'markup';
                if (!isset($element['value'])) {
                    throw new PieformException('The markup element "'
                        . $name . '" has no value');
                }
            }
            if (!isset($element['title'])) {
                $element['title'] = '';
            }
            if ($element['type'] == 'file') {
                $this->fileupload = true;
                if ($this->method == 'get') {
                    $this->method = 'post';
                    self::info("Your form '$this->name' had the method 'get' and also a file element - it has been converted to 'post'");
                }
            }
            if ($element['type'] == 'fieldset') {
                foreach ($element['elements'] as $subname => &$subelement) {
Nigel McNie's avatar
Nigel McNie committed
459
460
461
462
463
464
465
                    // The name can be in the element itself. This is compatibility for the perl version
                    if (isset($subelement['name'])) {
                        $subname = $subelement['name'];
                    }
                    if (count($subelement) == 0) {
                        throw new PieformException('An element in form "' . $this->name . '" has no data (' . $subname . ')');
                    }
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
                    if (!isset($subelement['type'])) {
                        $subelement['type'] = 'markup';
                        if (!isset($subelement['value'])) {
                            throw new PieformException('The markup element "'
                                . $name . '" has no value');
                        }
                    }
                    if (!isset($subelement['title'])) {
                        $subelement['title'] = '';
                    }
                    if ($subelement['type'] == 'file') {
                        $this->fileupload = true;
                        if ($this->method == 'get') {
                            $this->method = 'post';
                            self::info("Your form '$this->name' had the method 'get' and also a file element - it has been converted to 'post'");
                        }
                    }
                    if (!$autofocusadded && $this->autofocus === true) {
                        $subelement['autofocus'] = true;
                        $autofocusadded = true;
                    }
                    else if (!empty($this->autofocus) && $this->autofocus !== true
                        && $subname == $this->autofocus) {
                        $subelement['autofocus'] = true;
                    }
                    $subelement['name'] = $subname;
                    $subelement['tabindex'] = $this->tabindex;

                    // Let each element set and override attributes if necessary
                    if ($subelement['type'] != 'markup') {
496
497
498
499
500
501
502
503
504
505
                        // This function can be defined by the application using Pieforms,
                        // and applies to all elements of this type
                        $function = 'pieform_configure_' . $subelement['type'];
                        if (function_exists($function)) {
                            $subelement = $function($subelement);
                        }

                        // This function is defined by the plugin itself, to set fields on
                        // the element that need to be set but should not be set by the
                        // application
506
                        $function = 'pieform_render_' . $subelement['type'] . '_set_attributes';
507
                        $this->include_plugin('element',  $subelement['type']);
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
                        if (function_exists($function)) {
                            $subelement = $function($subelement);
                        }
                    }
                }
            }
            else {
                if (!$autofocusadded && $this->autofocus === true) {
                    $element['autofocus'] = true;
                    $autofocusadded = true;
                }
                elseif (!empty($this->autofocus) && $this->autofocus !== true
                    && $name == $this->autofocus) {
                    $element['autofocus'] = true;
                }
                $element['name'] = $name;
                $element['tabindex'] = $this->tabindex;
            }

            // Let each element set and override attributes if necessary
            if ($element['type'] != 'markup') {
529
530
531
532
533
                $function = 'pieform_configure_' . $element['type'];
                if (function_exists($function)) {
                    $element = $function($element);
                }

534
                $function = 'pieform_render_' . $element['type'] . '_set_attributes';
535
                $this->include_plugin('element',  $element['type']);
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
                if (function_exists($function)) {
                    $element = $function($element);
                }
            }
        }

        // Check if the form was submitted, and if so, validate and process it
        $global = ($this->method == 'get') ? $_GET: $_POST;
        if ($this->validate && isset($global['pieform_' . $this->name] )) {
            if ($this->submit) {
                $this->submitted = true;
                // Check if the form has been cancelled
                if ($this->iscancellable) {
                    foreach ($global as $key => $value) {
                        if (substr($key, 0, 7) == 'cancel_') {
                            // Check for and call the cancel function handler
                            // @todo<nigel>: it might be that this function could be optional
                            $function = $this->name . '_' . $key;
                            if (!function_exists($function)) {
                                throw new PieformException('Form "' . $this->name . '" does not have a cancel function handler for "' . substr($key, 7) . '"');
                            }
                            $function();
                            $element = $this->get_element(substr($key, 7));
                            if (!isset($element['goto'])) {
                                throw new PieformException('Cancel element "' . $element['name'] . '" has no page to go to');
                            }
                            // @todo what happens in the case of ajax post?
                            redirect($element['goto']);
                            return;
                        }
                    }
                }
            }

            // Get the values that were submitted
            $values = $this->get_submitted_values();
            // Perform general validation first
            $this->validate($values);
            // Then user specific validation if a function is available for that
            if (function_exists($this->validatefunction)) {
                $function = $this->validatefunction;
                $function($this, $values);
            }

            // Submit the form if things went OK
            if ($this->submit && !$this->has_errors()) {
582
583
584
                $submitted = false;
                foreach ($this->get_elements() as $element) {
                    // @todo Rename 'ajaxmessages' to 'submitelement'
585
                    if (!empty($element['ajaxmessages']) == true && isset($global[$element['name']])) {
586
587
588
589
590
591
592
593
594
                        $function = "{$this->name}_submit_{$element['name']}";
                        if (function_exists($function)) {
                            $function($values);
                            $submitted = true;
                            break;
                        }
                    }
                }
                if (!$submitted && function_exists($this->submitfunction)) {
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
                    $function = $this->submitfunction;
                    // Call the user defined function for processing a submit
                    // This function should really redirect/exit after it has
                    // finished processing the form.
                    // @todo maybe it should do just that...
                    $function($values);
                    // This will only work if I can make the login_submit function stuff work in login_validate
                    //if ($this->ajaxpost) {
                    //    $message = 'Your ' . $this->name . '_submit function should output some json and exit';
                    //}
                    //else {
                    //    $message = 'Your ' . $this->name . '_submit function should redirect when it is finished';
                    //}
                    //throw new PieformException($message);
                }
610
                else if (!$submitted) {
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
                    throw new PieformException('No function registered to handle form submission for form "' . $this->name . '"');
                }
            }

            // Auto focus the first element with an error if required
            if ($this->autofocus !== false) {
                $this->auto_focus_first_error();
            }
            
            // If the form has been submitted by ajax, return ajax
            if ($this->ajaxpost) {
                $errors = $this->get_errors();
                $json = array();
                foreach ($errors as $element) {
                    $json[$element['name']] = $element['error'];
                }
                echo json_encode(array('error' => 'local', 'message' => '@todo allow forms to specify a local error message', 'errors' => $json));
                exit;
            }
        }
    }

    /**
     * Returns a generic property. This can be used to retrieve any property
     * set in the form data array, so developers can pass in random stuff and
     * get access to it.
     *
     * @param string The key of the property to return
     * @return mixed
     */
    public function get_property($key) {
        return $this->data[$key];
    }

    /**
     * Returns the form name
     *
     * @return string
     */
    public function get_name() {
        return $this->name;
    }

    /**
     * Returns the form submission method
     *
     * @return string
     */
    public function get_method() {
        return $this->method;
    }

    /**
     * Is the form being submitted by ajax?
     *
     * @return bool
     */
    public function get_ajaxpost() {
        return $this->ajaxpost;
    }

    /**
     * Returns the renderer used on to render the form
     *
     * @return string
     */
    public function get_renderer() {
        return $this->renderer;
    }

681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
    /**
     * Returns the HTML for the <form...> tag
     *
     * @return string
     */
    public function get_form_tag() {
        $result = '<form';
        foreach (array('name', 'method', 'action') as $attribute) {
            $result .= ' ' . $attribute . '="' . $this->{$attribute} . '"';
        }
        $result .= ' id="' . $this->name . '"';
        if ($this->fileupload) {
            $result .= ' enctype="multipart/form-data"';
        }
        $result .= '>';
        return $result;
    }

699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
    /**
     * Returns whether the form has been submitted
     *
     * @return bool
     */
    public function is_submitted() {
        return $this->submitted;
    }

    /**
     * Builds and returns the HTML for the form, respecting the chosen renderer.
     *
     * Note that the "action" attribute for the form tag are NOT HTML escaped
     * for you. This allows you to build your own URLs, should you require. On
     * the other hand, this means you must be careful about escaping the URL,
     * especially if it has data from an external source in it.
     *
716
     * @param boolean Whether to include the <form...></form> tags in the output
717
718
     * @return string The form as HTML
     */
719
720
721
722
    public function build($outputformtags=true) {
        $result = '';
        if ($outputformtags) {
            $result = $this->get_form_tag() . "\n";
723
724
        }

725
        $this->include_plugin('renderer',  $this->renderer);
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
        
        // Form header
        $function = 'pieform_renderer_' . $this->renderer . '_header';
        if (function_exists($function)) {
            $result .= $function();
        }

        // Render each element
        foreach ($this->elements as $name => $elem) {
            if ($elem['type'] != 'hidden') {
                $result .= pieform_render_element($elem, $this);
            }
        }

        // Form footer
        $function = 'pieform_renderer_' . $this->renderer . '_footer';
        if (function_exists($function)) {
            $result .= $function();
        }

        // Hidden elements
747
        $this->include_plugin('element', 'hidden');
748
749
750
751
752
753
754
755
756
757
758
        foreach ($this->get_elements() as $element) {
            if ($element['type'] == 'hidden') {
                $result .= pieform_render_hidden($element, $this);
            }
        }
        $element = array(
            'type'  => 'hidden',
            'name'  => 'pieform_' . $this->name,
            'value' => ''
        );
        $result .= pieform_render_hidden($element, $this);
759
760
761
        if ($outputformtags) {
            $result .= "</form>\n";
        }
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785

        if ($this->ajaxpost) {
            $result .= '<script language="javascript" type="text/javascript">';
            $result .= $this->submit_js();
            $result .=  "</script>\n";
        }

        return $result;
    }

    /**
     * Given an element, gets the value for it from this form
     *
     * @param  array $element The element to get the value for
     * @return mixed          The element's value. <kbd>null</kbd> if no value
     *                        is available for the element.
     */
    public function get_value($element) {
        $function = 'pieform_get_value_' . $element['type'];
        // @todo for consistency, reverse parameter order - always a Form object first
        if (function_exists($function)) {
            return $function($element, $this);
        }
        $global = ($this->method == 'get') ? $_GET : $_POST;
786
787
788
789
        // If the element is a submit element and has its value in the request, return it
        // Otherwise, we don't return the value if the form has been submitted, as they
        // aren't normally returned using a standard form.
        if (isset($element['value']) && !empty($element['ajaxmessages']) && isset($global[$element['name']])) {
790
791
            return $element['value'];
        }
792
        else if (isset($element['value']) && (!$this->is_submitted() || (empty($element['ajaxmessages'])))) {
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
            return $element['value'];
        }
        else if (isset($global[$element['name']]) && $element['type'] != 'submit') {
            return $global[$element['name']];
        }
        else if (isset($element['defaultvalue'])) {
            return $element['defaultvalue'];
        }
        return null;
    }

    /**
     * Retrieves a list of elements in the form.
     *
     * This flattens fieldsets, and ignores the actual fieldset elements
     *
     * @return array The elements of the form
     */ 
    public function get_elements() {
        $elements = array();
        foreach ($this->elements as $name => $element) {
            if ($element['type'] == 'fieldset') {
                foreach ($element['elements'] as $subelement) {
                    $elements[] = $subelement;
                }
            }
            else {
                $elements[] = $element;
            }
        }
        return $elements;
    }
    
    /**
     * Returns the element with the given name. Throws a PieformException if the
     * element cannot be found.
     *
     * Fieldset elements are ignored. This might change if a valid case for
     * needing them is found.
     *
     * @param  string $name     The name of the element to find
     * @return array            The element
     * @throws PieformException If the element could not be found
     */
    public function get_element($name) {
        foreach ($this->get_elements() as $element) {
            if ($element['name'] == $name) {
                return $element;
            }
        }
        throw new PieformException('Element "' . $name . '" cannot be found');
    }

    /**
     * Retrieves submitted values from POST for the elements of this form.
     *
     * This takes into account that some elements may not even have been set,
     * for example if they were check boxes that were not checked upon
     * submission.
     *
     * A value is returned for every element (except fieldsets of course). If
     * an element was not set, the value set is <kbd>null</kbd>.
     *
     * @return array The submitted values
     */
    private function get_submitted_values() {
        $result = array();
        $global = ($this->method == 'get') ? $_GET : $_POST;
        foreach ($this->get_elements() as $element) {
Nigel McNie's avatar
Nigel McNie committed
862
863
864
            if ($element['type'] != 'markup') {
                $result[$element['name']] = $this->get_value($element);
            }
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
        }
        return $result;
    }

    /**
     * Performs simple validation based off the definition array.
     *
     * Rules can be added to <kbd>pieform/rules/</kbd> directory, and then
     * re-used in the 'rules' index of each element in the form definition
     * hash.
     *
     * More complicated validation is possible by defining an optional
     * callback with the name {$form->name}_validate. See the documentation for
     * more information.
     *
     * @param array $values The submitted values from the form
     */
    private function validate($values) {
        foreach ($this->get_elements() as $element) {
            if (isset($element['rules']) && is_array($element['rules'])) {
                foreach ($element['rules'] as $rule => $data) {
                    if (!$this->get_error($element['name'])) {
                        // Get the rule
                        $function = 'pieform_rule_' . $rule;
                        if (!function_exists($function)) {
890
                            $this->include_plugin('rule', $rule);
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
                            if (!function_exists($function)) {
                                throw new PieformException('No such form rule "' . $rule . '"');
                            }
                        }
                        if ($error = $function($this, $values[$element['name']], $element, $data)) {
                            $this->set_error($element['name'], $error);
                        }
                    }
                }
            }
        }
    }

    /**
     * Returns a js function to submit an ajax form 
     * Expects formname_message() to be defined by the renderer,
     * and formname_validate() to be defined.
     */
    private function submit_js() {
        // @todo nigel should disable all buttons on this form while the submit is happening
        $result = <<<EOF
912

913
914
915
916
917
918
919
920
921
922
923
924
925
926
var {$this->name}_btn = null;
// For each submit button, add a waffer thin flag
addLoadEvent(function () {

EOF;
        foreach ($this->get_elements() as $element) {
            if (!empty($element['ajaxmessages'])) {
                $result .= "    connect($('{$this->name}_{$element['name']}'), 'onclick', function () { {$this->name}_btn = '{$element['name']}'; });\n";
            }
        }
        $result .= <<<EOF

});

927
connect($('{$this->name}'), 'onsubmit', function (e) {
928
929
930
931
932
933
934
935
936
937
938
939
940
941
    // eventually we should check input types for wysiwyg before doing this
    // Also should only save wysiwyg elements in the form, not all of them...
    if (typeof(tinyMCE) != 'undefined') { tinyMCE.triggerSave(); } 

EOF;
        if (!empty($this->preajaxsubmitcallback)) {
            $result .= "    $this->preajaxsubmitcallback();\n";
        }
        $result .= <<<EOF
    var data = {};

EOF;
        // Get values for each element from the form via the DOM
        foreach ($this->get_elements() as $element) {
942
943
944
945
946
947
948
949
950
951
952
            // Submit elements will be handled later, as there could be more than one
            if (empty($element['ajaxmessages'])) {
                if ($element['type'] != 'markup') {
                    $function = 'pieform_get_value_js_' . $element['type'];
                    if (function_exists($function)) {
                        // @todo reverse parameter order for consistency, PieForm first
                        $result .= $function($element, $this);
                    }
                    else {
                        $result .= "    data['" . $element['name'] . "'] = document.forms['$this->name'].elements['{$element['name']}'].value;\n";
                    }
953
954
955
956
                }
            }
        }

957
        // Add only the submit button that was clicked
958
        $result .= "    data[{$this->name}_btn] = document.forms['$this->name'].elements['{$this->name}_' + {$this->name}_btn].value;\n";
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015

        // Add the hidden element for detecting form submission
        $result .= "    data['pieform_{$this->name}'] = '';\n";

        $action = ($this->action) ? $this->action : basename($_SERVER['PHP_SELF']);
        $method = ($this->get_method() == 'get') ? 'GET' : 'POST';
        $result .= <<<EOF
    var req = getXMLHttpRequest();
    req.open('{$method}', '{$action}');
    req.setRequestHeader('Content-type','application/x-www-form-urlencoded'); 
    var d = sendXMLHttpRequest(req,queryString(data));
    d.addCallbacks(
    function (result) {
        {$this->name}_remove_all_errors();
        var data = evalJSONRequest(result);
        if (data.error) {
            {$this->name}_message(data.message, 'error');
            for (error in data.errors) {
                {$this->name}_set_error(data.errors[error], error);
            }

EOF;
        
        if (!empty($this->ajaxfailurefunction)) {
            $result .= "            {$this->ajaxfailurefunction}(data);\n";
        }
        $result .= <<<EOF
        }
        else {
            {$this->name}_message(data.message, 'ok');

EOF;

        if (!empty($this->ajaxsuccessfunction)) {
            $result .= "            {$this->ajaxsuccessfunction}(data);\n";
        }

        $result .= "            {$this->name}_remove_all_errors();\n";
        $result .= "        }\n";
        if (!empty($this->postajaxsubmitcallback)) {
            $result .= "    $this->postajaxsubmitcallback();\n";
        }

        $strunknownerror =   $this->i18n('ajaxunknownerror');
        $strprocessingform = $this->i18n('processingform');
        $result .= <<<EOF
    },
    function() {
        {$this->name}_message('{$strunknownerror}', 'error');

EOF;
        if (!empty($this->postajaxsubmitcallback)) {
            $result .= "        $this->postajaxsubmitcallback();\n";
        }
        $result .= <<<EOF
    });
    {$this->name}_message('{$strprocessingform}', 'info');
1016
1017
    e.stop();
});
1018
1019
1020
1021
1022

EOF;

        $js_messages_function = 'pieform_renderer_' . $this->renderer . '_messages_js';
        if (!function_exists($js_messages_function)) {
1023
            $this->include_plugin('renderer', $this->renderer);
1024
1025
1026
1027
1028
            if (!function_exists($js_messages_function)) {
                throw new PieformException('No renderer message function "' . $js_messages_function . '"');
            }
        }

1029
        return $result . $js_messages_function($this->name);
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
    }

    /**
     * Returns whether a field has an error marked on it.
     *
     * This method should be used in the custom validation functions, to see if
     * there is an error on an element before checking for any more validation.
     *
     * Example:
     *
     * <code>
     * if (!$form->get_error('name') && /* condition {@*}) {
     *     $form->set_error('name', 'error message');
     * }
     * </code>
     *
     * @param  string $name  The name of the element to check
     * @return bool          Whether the element has an error
     * @throws PieformException If the element could not be found
     */
    public function get_error($name) {
        $element = $this->get_element($name);
        return isset($element['error']);
    }

    /**
     * Marks a field has having an error.
     *
     * This method should be used to set an error on an element in a custom
     * validation function, if one has occured.
     *
     * Note that for the Mahara project, your error messages must be passed
     * through {@link get_string} to internationalise them.
     *
     * @param string $name    The name of the element to set an error on
     * @param string $message The error message
     * @throws PieformException  If the element could not be found
     */
    public function set_error($name, $message) {
        foreach ($this->elements as &$element) {
            if ($element['type'] == 'fieldset') {
                foreach ($element['elements'] as &$subelement) {
                    if ($subelement['name'] == $name) {
                        $subelement['error'] = $message;
                        return;
                    }
                }
            }
            else {
                if ($element['name'] == $name) {
                    $element['error'] = $message;
                    return;
                }
            }
        }
        throw new PieformException('Element "' . $name . '" could not be found');
    }

    /**
     * Makes an ID for an element.
     *
     * Element IDs are used for <label>s, so use this method to ensure that
     * an element gets an ID.
     *
     * The element's existing 'id' and 'name' attributes are checked first. If
     * they are not specified, a random ID is synthesised
     *
     * @param array $element The element to make an ID for
     * @return string        The ID for the element
     */
    public static function make_id($element) {
        if (isset($element['id'])) {
            return self::hsc($element['id']);
        }
        if (isset($element['name'])) {
            return self::hsc($element['name']);
        }
        return substr(md5(mt_rand()), 0, 4);
    }

    /**
     * Makes a class for an element.
     *
     * Elements can have several classes set on them depending on their state.
     * The classes are useful for (among other things), styling elements
     * differently if they are in these states.
     *
     * Currently, the states an element can be in are 'required' and 'error'.
     *
     * @param array $element The element to make a class for
     * @return string        The class for an element
     */
    public static function make_class($element) {
        $classes = array();
        if (isset($element['class'])) {
            $classes[] = $element['class'];
        }
        if (!empty($element['rules']['required'])) {
            $classes[] = 'required';
        }
        if (!empty($element['error'])) {
            $classes[] = 'error';
        }
        // Please make sure that 'autofocus' is the last class added in this
        // method. Otherwise, improve the logic for removing 'autofocus' from
        // the elemnt class string in pieform_render_element
        if (!empty($element['autofocus'])) {
            $classes[] = 'autofocus';
        }
        return implode(' ', $classes);
    }

    /**
     * Given an element, returns a string representing the basic attribute
     * list for the element.
     *
     * This EXCLUDES the "value" attribute, as various form elements set
     * their value in different ways.
     *
     * This allows each element to have most of the standard HTML attributes
     * that you can normally set on a form element.
     *
     * The attributes generated by this method will include (if set for the
     * element itself), are <kbd>accesskey, class, dir, id, lang, maxlength,
     * name, size, style</kbd> and <kbd>tabindex</kbd>.
     *
     * The <kbd>class</kbd> and <kbd>id</kbd> attributes are typically built
     * beforehand with {@link make_class} and {@link make_id} respectively.
     * The <kbd>maxlength</kbd> attribute is only set if the element has a
     * "maxlength" rule on it.
     *
     * @param array $element The element to make attributes for
     * @param array $exclude Any attributes to explicitly exclude from adding
     * @return string        The attributes for the element
     */
1165
    public function element_attributes($element, $exclude=array()) {
1166
1167
1168
1169
1170
        static $attributes = array('accesskey', 'class', 'dir', 'id', 'lang', 'name', 'onclick', 'size', 'style', 'tabindex');
        $elementattributes = array_diff($attributes, $exclude);
        $result = '';
        foreach ($elementattributes as $attribute) {
            if (isset($element[$attribute]) && $element[$attribute] !== '') {
1171
1172
1173
                if ($attribute == 'id') {
                    $element[$attribute] = $this->name . '_' . $element[$attribute];
                }
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
                $result .= ' ' . $attribute . '="' . self::hsc($element[$attribute]) . '"';
            }
        }

        if (!in_array('maxlength', $exclude) && isset($element['rules']['maxlength'])) {
            $result .= ' maxlength="' . intval($element['rules']['maxlength']) . '"';
        }

        foreach (array_diff(array('disabled', 'readonly'), $exclude) as $attribute) {
            if (!empty($element[$attribute])) {
                $result .= " $attribute=\"$attribute\"";
            }
        }
        
        return $result;
    }

    /**
     * Checks if there are errors on any of the form elements.
     *
     * @return bool Whether there are errors with the form
     */
    public function has_errors() {
        foreach ($this->get_elements() as $element) {
            if (isset($element['error'])) {
                return true;
            }
        }
        return false;
    }

1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
    /**
     * Includes a plugin file, checking any configured plugin directories.
     *
     * @param string $type The type of plugin to include: 'element', 'renderer' or 'rule'
     * @param string $name The name of the plugin to include
     * @throws PieformException If the given type or plugin could not be found
     */
    public function include_plugin($type, $name) {
        if (!in_array($type, array('element', 'renderer', 'rule'))) {
            throw new PieformException("The type \"$type\" is not allowed for an include plugin");
        }

        // Check the configured include paths if they are specified
        foreach ($this->configdirs as $directory) {
            $file = "$directory/{$type}s/$name.php";
            if (is_readable($file)) {
                include_once($file);
                return;
            }
        }

        // Check the default include path
        $file = dirname(__FILE__) . "/pieform/{$type}s/{$name}.php";
        if (is_readable($file)) {
            include_once($file);
            return;
        }

        throw new PieformException("Could not find $type \"$name\"");
    }

1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
    /**
     * Return an internationalised string based on the passed input key
     *
     * Returns english by default.
     *
     * @param string $key The language key to look up
     * @return string     The internationalised string
     */
    public function i18n($key) {
        $function = 'pieform_' . $key . '_i18n';
        if (function_exists($function)) {
            return $function($this->language);
        }
        if (isset($this->language_strings[$this->language][$key])) {
            return $this->language_strings[$this->language][$key];
        }
        return '[[' . $key . ']]';
    }

    /**
     * HTML-escapes the given value
     *
     * @param string $text The text to escape
     * @return string      The text, HTML escaped
     */
    public static function hsc($text) {
        return htmlspecialchars($text, ENT_COMPAT, 'UTF-8');
    }

    /**
     * Hook for giving information back to the developer
Nigel McNie's avatar
Nigel McNie committed
1267
1268
     *
     * @param string $message The message to give to the developer
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
     */
    public static function info($message) {
        $function = 'pieform_info';
        if (function_exists($function)) {
            $function($message);
        }
        else {
            trigger_error($message, E_USER_NOTICE);
        }
    }

    private static function validate_js_callback($name) {
        if ($name == '') {
            return '';
        }
        if (!preg_match('/^[a-zA-Z][a-zA-Z0-9_]*$/', $name)) {
            throw new PieformException("'$name' is not a valid javascript callback name");
        }
1287
        return $name;
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
    }

    /**
     * Returns elements with errors on them
     *
     * @return array An array of elements with errors on them, the empty array
     *               in the result of no errors.
     */
    private function get_errors() {
        $result = array();
        foreach ($this->get_elements() as $element) {
            if (isset($element['error'])) {
                $result[] = $element;
            }
        }
        return $result;
    }

    /**
     * Sets the 'autofocus' property on the first element encountered that has
     * an error on it
     */
    private function auto_focus_first_error() {
        foreach ($this->elements as &$element) {
            if ($element['type'] == 'fieldset') {
                foreach ($element['elements'] as &$subelement) {
                    if (isset($subelement['error'])) {
                        $subelement['autofocus'] = true;
                        return;
                    }
                    unset($subelement['autofocus']);
                }
            }
            else {
                if (isset($element['error'])) {
                    $element['autofocus'] = true;
                    return;
                }
                unset($element['autofocus']);
            }
        }
    }
}


/**
 * Renders an element, and returns the result.
 *
 * This function looks in <kbd>pieform/renderers</kbd> for available overall form
 * renderers, and in <kbd>pieform/elements</kbd> for renderers for each form
 * element.
 *
 * If any of the renderers are not available, this function will throw a
 * PieformException.
 *
 * {@internal This is separate so that child element types can nest other
 * elements inside them (like the fieldset element does for example).}}
 *
Nigel McNie's avatar
Nigel McNie committed
1346
 * @param array    $element The element to render
1347
 * @param Pieform  $form    The form to render the element for
Nigel McNie's avatar
Nigel McNie committed
1348
 * @return string           The rendered element
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
 */
function pieform_render_element($element, Pieform $form) {
    // If the element is pure markup, don't pass it to the renderer
    if ($element['type'] == 'markup') {
        return $element['value'] . "\n";
    }

    // Make sure that the function to render the element type is available
    $function = 'pieform_render_' . $element['type'];

    // Work out the renderer function required and make sure it exists
    if ($renderer = $form->get_renderer()) {
        $rendererfunction = 'pieform_renderer_' . $renderer;
        if (!function_exists($rendererfunction)) {
1363
            $form->include_plugin('pieform/renderers/' . $renderer . '.php');
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
            if (!function_exists($rendererfunction)) {
                throw new PieformException('No such form renderer: "' . $renderer . '"');
            }
        }
    }
    else {
        throw new PieformException('No form renderer specified for form "' . $form->get_name() . '"');
    }

    $element['id']    = Pieform::make_id($element);
    $element['class'] = Pieform::make_class($element);
    // @todo reverse order of parameters for consistency, a Form object first
    $builtelement = $function($element, $form);

    // Remove the 'autofocus' class, because we only want it on the form input
    // itself, not the wrapping HTML
    $element['class'] = preg_replace('/\s?autofocus/', '', $element['class']);

    return $rendererfunction($form, $builtelement, $element);
}

1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
function pieform_get_headdata() {
    $htmlelements = array();
    foreach ($GLOBALS['_PIEFORM_REGISTRY'] as $form) {
        foreach ($form->get_elements() as $element) {
            $function = 'pieform_get_headdata_' . $element['type'];
            if (function_exists($function)) {
                $elems = $function($element);
                $htmlelements = array_merge($htmlelements, $elems);
            }
        }
    }

    return array_unique($htmlelements);
}

1400
?>