div.php 3.18 KB
Newer Older
1 2
<?php
/**
3 4
 * Pieforms: Advanced web forms made easy
 * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
5
 *
6 7 8 9
 * 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 3 of the License, or
 * (at your option) any later version.
10
 *
11 12 13 14
 * 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.
15
 *
16 17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 *
19 20
 * @package    pieform
 * @subpackage renderer
21
 * @author     Nigel McNie <nigel@catalyst.net.nz>
22 23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
24 25 26 27
 *
 */

/**
28
 * Renders form elements inside <div>s.
29
 *
30 31 32
 * @param Pieform $form    The form the element is being rendered for
 * @param array   $element The element to be rendered
 * @return string          The element rendered inside an appropriate container
33
 */
34
function pieform_renderer_div(Pieform $form, $element) {/*{{{*/
Naomi Guyer's avatar
Naomi Guyer committed
35

36
    $formname = $form->get_name();
37 38
    // Set the class of the enclosing <div> to match that of the element
    $result = '<div';
39
    if (isset($element['name'])) {
40
        $result .= ' id="' . $formname . '_' .  Pieform::hsc($element['name']) . '_container"';
41
    }
42
    if (!empty($element['class'])) {
Naomi Guyer's avatar
Naomi Guyer committed
43 44

        // add form-group classes to all real form fields
Pat Kira's avatar
Pat Kira committed
45
        if (strpos($element['class'],'html') === false) {
Naomi Guyer's avatar
Naomi Guyer committed
46 47 48
            $element['class'] = $element['class'] . ' form-group';
        }

Pat Kira's avatar
Pat Kira committed
49 50 51 52 53
        // add bootstrap has-error class to any error fields
        if (strpos($element['class'],'error') !== false) {
             $element['class'] = $element['class'] . ' has-error';
        }

54
        $result .= ' class="' . Pieform::hsc($element['class']) . '"';
Naomi Guyer's avatar
Naomi Guyer committed
55

56
    }
57
    $result .= '>';
58

59 60
    if (isset($element['labelhtml'])) {
        $result .= $element['labelhtml'];
61
    }
62

63
    if (isset($element['prehtml'])) {
64
        $result .= '<span class="prehtml">' . $element['prehtml'] . '</span>';
65 66
    }

67 68
    //$result .= $builtelement;
    $result .= $element['html'];
69

70
    if (isset($element['posthtml'])) {
71
        $result .= '<span class="posthtml">' . $element['posthtml'] . '</span>';
72 73
    }

74 75
    if (isset($element['helphtml'])) {
        $result .= ' ' . $element['helphtml'];
76 77 78
    }

    // Description - optional description of the element, or other note that should be visible
Aaron Wells's avatar
Aaron Wells committed
79
    // on the form itself (without the user having to hover over contextual help
80 81
    if ((!$form->has_errors() || $form->get_property('showdescriptiononerror')) && !empty($element['descriptionhtml'])) {
        $result .= '<div class="description"> ' . $element['descriptionhtml'] . "</div>";
82 83
    }

84
    if (!empty($element['error'])) {
85
        $result .= '<div class="errmsg">' . $element['errorhtml'] . '</div>';
86 87
    }

88
    $result .= "</div>\n";
89
    return $result;
90
}/*}}}*/