table.php 4.48 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
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
<?php
/**
 * This program is part of Mahara
 *
 *  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    mahara
 * @subpackage form-renderer
 * @author     Nigel McNie <nigel@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

defined('INTERNAL') || die();

/**
 * Renders form elements inside a <table>.
 *
 * @param string $builtelement The element, already built
 * @param array  $rawelement   The element in raw form, for looking up
 *                             information about it.
 * @return string              The element rendered inside an appropriate
 *                             container.
 */
function form_renderer_table($builtelement, $rawelement) {
    if ($rawelement['type'] == 'fieldset') {
        // Add table tags to the build element, to preserve HTML compliance
        if (0 === strpos($builtelement, "\n<fieldset>\n<legend>")) {
            $closelegendpos = strpos($builtelement, '</legend>') + 9;
            $builtelement = substr($builtelement, 0, $closelegendpos) . '<table>' . substr($builtelement, $closelegendpos);
        }
        else {
            $builtelement = substr($builtelement, 0, 11) . '<table>' . substr($builtelement, 11);
        }
        $builtelement = substr($builtelement, 0, -12) . '</table></fieldset>';

        $result = "\t<tr>\n\t\t<td colspan=\"2\">";
        $result .= $builtelement;
        $result .= "</td>\n\t</tr>";
        return $result;
    }
    // Set the class of the enclosing <tr> to match that of the element
    $result = "\t<tr";
    if ($rawelement['class']) {
        $result .= ' class="' . $rawelement['class'] . '"';
    }
    $result .= ">\n\t\t";

    $result .= '<th>';
63
64
65
66
67
68
69
70
    if (isset($rawelement['title'])) {
        if (!empty($rawelement['nolabel'])) {
            // Don't bother with a lable for the element
            $result .= hsc($rawelement['title']);
        }
        else {
            $result .= '<label for="' . $rawelement['id'] . '">' . hsc($rawelement['title']) . '</label>';
        }
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
    }
    $result .= "</th>\n\t\t<td>";
    $result .= $builtelement;

    // Contextual help
    if (!empty($rawelement['help'])) {
        $result .= ' <span class="help"><a href="#" title="' . hsc($rawelement['help']) . '">?</a></span>';
    }

    $result .= "</td>\n\t</tr>\n";

    // Description - optional description of the element, or other note that should be visible
    // on the form itself (without the user having to hover over contextual help 
    if (!empty($rawelement['description'])) {
        $result .= "\t<tr>\n\t\t<td colspan=\"2\" class=\"description\">";
        $result .= hsc($rawelement['description']);
        $result .= "</td>\n\t</tr>\n";
    }

    if (!empty($rawelement['error'])) {
91
        $result .= "\t<tr>\n\t\t<td colspan=\"2\" class=\"errmsg\">";
92
93
        $result .= hsc($rawelement['error']);
    }
94
    else {
95
        $result .= "\t<tr style=\"display:none\" id=\"" .  $rawelement['id'] . "_msg\"><td>";
96
97
    }
    $result .= "</td>\n\t</tr>\n";
98
99
100
101
102

    return $result;
}

function form_renderer_table_header() {
103
    return "<table cellspacing=\"0\" border=\"0\"><tbody>\n";
104
105
106
}

function form_renderer_table_footer() {
107
    return "</tbody></table>\n";
108
109
}

110
function form_renderer_table_messages_js($id, $submitid) {
111
112
113
114
115
116
    $result = 'function ' . $id . '_set_error(id,m) {';
    $result .= "swapDOM($(id),TR({'id':id},TD({'colspan':2,'class':'errmsg'},m)));";
    $result .= "}\n";
    $result .= 'function ' . $id . '_rem_error(id) {';
    $result .= "swapDOM($(id),TR({'id':id,'style':'display:none;'},TD(null)));";
    $result .= "}\n";
117
    $result .= 'function ' . $id . '_message(m,type) {';
118
119
    $result .= "swapDOM($('" . $submitid . "_msg'),TR({'id':'" . $submitid 
        . "_msg'},TD({'colspan':2,'class':type},m)));";
120
    $result .= "}\n";
121
122
123
    return $result;
}

124
?>