root/trunk/examples/patExampleGen/customFunctions.php

Revision 358, 4.8 kB (checked in by argh, 2 years ago)

Tweaked the examples framework to correctly display array-based form element values as well as display value type information.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * Custom functions for the current examples collection - the functions
4  * defined here are available for all examples.
5  *
6  * $Id$
7  *
8  * @access        public
9  * @package        patForms
10  * @subpackage    Examples
11  * @version        0.1
12  * @author        Sebastian Mordziol <s.mordziol@metrix.de>
13  * @link        http://www.php-tools.net
14  */
15
16
17    /**
18     * Takes a patForms object, asks it if there are any validation
19     * errors and displays them if need be.
20     *
21     * NOTE: this is just a helper method for our examples collection,
22     * so that you may concentrate on the relevant parts of the examples.
23     * It does in no way represent the way it should be done :)
24     *
25     * @access    public
26     * @param    object    &$form    The patForms object to use
27     */
28     function displayErrors( &$form )
29     {
30         // get the errors from the form object - if there are none,
31         // this returns false so it is easy to check if there are any.
32         $errors = $form->getValidationErrors();
33
34         // if there are any errors, display them.
35         if ($errors) {
36             echo '<div class="piErrors">';
37             echo '    <div class="piErrorsTitle">Validation failed</div>';
38             echo '    <div class="piErrorsContent">';
39
40             // the errors collection is an associative array with the
41             // field names as keys, so we go through that.
42             foreach ($errors as $elementName => $elementErrors) {
43                 $element =& $form->getElementByName( $elementName );
44
45                 if (is_array($element)) {
46                     $element = $element[0];
47                 }
48
49                 if (!is_object($element)) {
50                     patErrorManager::raiseError("patExampleGen::0001", "The getElementByName() method did not return an element.");
51                 }
52
53                 // each element can have more than one error - this
54                 // is rare, but can happen so this is an indexed array
55                 // with one error in each row.
56                 foreach ($elementErrors as $row => $error) {
57                     echo '        <div class="piError">';
58                     echo '             <b>'.$element->getAttribute( 'label' ).':</b> '.$error['message'].' ('.$error['element'].' element error #'.$error['code'].')<br/>';
59                     echo '        </div>';
60                 }
61             }
62
63             echo '    </div>';
64             echo '</div>';
65         }
66         // no errors, tell the world everything is fine
67         else
68         {
69
70             echo '<div class="piHint">Validation successful.</div>';
71         }
72     }
73
74
75    /**
76     * Displays a standard form from the examples collection when the
77     * form is rendered via the array renderer. Does not work for any
78     * other examples.
79     *
80     * NOTE: this is just a helper method for our examples collection,
81     * so that you may concentrate on the relevant parts of the examples.
82     * It does in no way represent the way it should be done :)
83     *
84     * @access    public
85     * @param    object    &$form        The current form object
86     * @param    array    $elements    The rendered elements from the
87     * @return
88     * @see
89     */
90     function displayForm( &$form, $elements )
91     {
92         // output the opening form tag
93         echo $form->serializeStart();
94
95         // display all elements
96         foreach( $elements as $element )
97         {
98             echo '<div style="margin-bottom:8px;">';
99             echo $element['label']."<br>";
100             echo "<div>".$element["element"]."</div>";
101
102             if( isset( $element["description"] ) )
103             {
104                 echo "<i>".$element["description"]."</i><br>";
105             }
106
107             echo '</div>';
108         }
109
110         // submit button, closing form tag
111         echo '<input type="submit" name="save" value="Save form"/><br><br>';
112         echo $form->serializeEnd();
113
114
115         // form submitted? display all form values
116         if( $form->isSubmitted() ) {
117             $els =& $form->getElements();
118             $cnt = count( $els );
119
120             echo '<div class="piValues">';
121             echo '    <div class="piValuesTitle">Submitted form values</div>';
122             echo '    <div class="piValuesContent">';
123             echo '        <table cellpadding="2" cellspacing="0" border="0">';
124
125             for( $i = 0; $i < $cnt; $i++ ) {
126                 echo '<tr valign="top">';
127                 echo '    <td>'.$els[$i]->getAttribute( 'label' ).'</td><td>&nbsp;:&nbsp;</td><td>'.var2string( $els[$i]->getValue() ).'</td>';
128                 echo '</tr>';
129             }
130
131             echo '        </table>';
132             echo '    </div>';
133             echo '</div>';
134         }
135     }
136
137     function var2string( $subject )
138     {
139         if( is_object( $subject ) ) {
140             $class = get_class( $subject );
141             return '<i style="color:#666;">object</i> <span style="color:#006600;">"'.$class.'"</span>';
142         }
143
144         if( is_int( $subject ) ) {
145             return '<i style="color:#666;">int</i> <span style="color:#003366;">'.$subject.'</span>';
146         }
147
148         if( is_null( $subject ) ) {
149             return '<i style="color:#666;">null</i>';
150         }
151
152         if( is_array( $subject ) ) {
153             $string = '';
154             foreach( $subject as $key => $val ) {
155                 $string .= str_repeat( '&nbsp;', 6 ).$key.' => '.var2string( $val ).'<br/>';
156             }
157             return '<i style="color:#666;">array</i><span style="color:#006600;">(<br/>'.$string.');</span>';
158         }
159
160         if( is_bool( $subject ) ) {
161             $display = 'true';
162             if( $bool === false ) {
163                 $display = 'false';
164             }
165
166             return '<i style="color:#666">bool</i> <span style="color:#003366;">'.$display.'</span>';
167         }
168
169         return '<i style="color:#666;">'.gettype( $subject ).'</i> <span style="color:#006600;">"'.$subject.'"</span>';
170     }
171
172 ?>
Note: See TracBrowser for help on using the browser.