]> git.openstreetmap.org Git - chef.git/blob - cookbooks/dmca/files/default/html/HTML/QuickForm/PageDHTMLRulesTableless.php
Configure the dmca form site on ridley
[chef.git] / cookbooks / dmca / files / default / html / HTML / QuickForm / PageDHTMLRulesTableless.php
1 <?php
2 /**
3  * DHTML replacement for the standard JavaScript alert window for client-side
4  * validation
5  *
6  * LICENSE:
7  * 
8  * Copyright (c) 2005-2007, Mark Wiesemann <wiesemann@php.net>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  *    * Redistributions of source code must retain the above copyright
16  *      notice, this list of conditions and the following disclaimer.
17  *    * Redistributions in binary form must reproduce the above copyright
18  *      notice, this list of conditions and the following disclaimer in the 
19  *      documentation and/or other materials provided with the distribution.
20  *    * The names of the authors may not be used to endorse or promote products 
21  *      derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
31  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  * @category   HTML
36  * @package    HTML_QuickForm_DHTMLRulesTableless
37  * @author     Alexey Borzov <borz_off@cs.msu.su>
38  * @author     Adam Daniel <adaniel1@eesus.jnj.com>
39  * @author     Bertrand Mansion <bmansion@mamasam.com>
40  * @author     Justin Patrin <papercrane@gmail.com>
41  * @author     Mark Wiesemann <wiesemann@php.net>
42  * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
43  * @version    CVS: $Id: PageDHTMLRulesTableless.php,v 1.3 2007/10/24 20:36:11 wiesemann Exp $
44  * @link       http://pear.php.net/package/HTML_QuickForm_DHTMLRulesTableless
45  */
46
47 require_once 'HTML/QuickForm/Page.php';
48
49 /**
50  * This is a DHTML replacement for the standard JavaScript alert window for
51  * client-side validation of forms built with HTML_QuickForm
52  *
53  * @category   HTML
54  * @package    HTML_QuickForm_DHTMLRulesTableless
55  * @author     Alexey Borzov <borz_off@cs.msu.su>
56  * @author     Adam Daniel <adaniel1@eesus.jnj.com>
57  * @author     Bertrand Mansion <bmansion@mamasam.com>
58  * @author     Justin Patrin <papercrane@gmail.com>
59  * @author     Mark Wiesemann <wiesemann@php.net>
60  * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
61  * @version    Release: 0.3.3
62  * @link       http://pear.php.net/package/HTML_QuickForm_DHTMLRulesTableless
63  */
64 class HTML_QuickForm_PageDHTMLRulesTableless extends HTML_QuickForm_Page
65 {
66    /**
67     * Class constructor
68     * 
69     * @access public
70     */
71     function HTML_QuickForm_PageDHTMLRulesTableless($formName, $method = 'post',
72         $target = '', $attributes = null)
73     {
74         $this->HTML_QuickForm_Page($formName, $method, '', $target, $attributes);
75     }
76
77     // {{{ getValidationScript()
78
79     /**
80      * Returns the client side validation script
81      *
82      * The code here was copied from HTML_QuickForm and slightly modified to run rules per-element
83      *
84      * @access    public
85      * @return    string    Javascript to perform validation, empty string if no 'client' rules were added
86      */
87     function getValidationScript()
88     {
89         if (empty($this->_rules) || empty($this->_attributes['onsubmit'])) {
90             return '';
91         }
92
93         include_once('HTML/QuickForm/RuleRegistry.php');
94         $registry =& HTML_QuickForm_RuleRegistry::singleton();
95         $test = array();
96         $js_escape = array(
97             "\r"    => '\r',
98             "\n"    => '\n',
99             "\t"    => '\t',
100             "'"     => "\\'",
101             '"'     => '\"',
102             '\\'    => '\\\\'
103         );
104
105         foreach ($this->_rules as $elementName => $rules) {
106             foreach ($rules as $rule) {
107                 if ('client' == $rule['validation']) {
108                     unset($element);
109
110                     $dependent  = isset($rule['dependent']) && is_array($rule['dependent']);
111                     $rule['message'] = strtr($rule['message'], $js_escape);
112
113                     if (isset($rule['group'])) {
114                         $group    =& $this->getElement($rule['group']);
115                         // No JavaScript validation for frozen elements
116                         if ($group->isFrozen()) {
117                             continue 2;
118                         }
119                         $elements =& $group->getElements();
120                         foreach (array_keys($elements) as $key) {
121                             if ($elementName == $group->getElementName($key)) {
122                                 $element =& $elements[$key];
123                                 break;
124                             }
125                         }
126                     } elseif ($dependent) {
127                         $element   =  array();
128                         $element[] =& $this->getElement($elementName);
129                         foreach ($rule['dependent'] as $idx => $elName) {
130                             $element[] =& $this->getElement($elName);
131                         }
132                     } else {
133                         $element =& $this->getElement($elementName);
134                     }
135                     // No JavaScript validation for frozen elements
136                     if (is_object($element) && $element->isFrozen()) {
137                         continue 2;
138                     } elseif (is_array($element)) {
139                         foreach (array_keys($element) as $key) {
140                             if ($element[$key]->isFrozen()) {
141                                 continue 3;
142                             }
143                         }
144                     }
145
146                     $test[$elementName][] = $registry->getValidationScript($element, $elementName, $rule);
147                 }
148             }
149         }
150         $js = '
151 <script type="text/javascript"><!--//--><![CDATA[//><!--
152 qf_errorHandler = function(element, _qfMsg) {
153   div = element.parentNode;
154   var elementName = element.name.replace(/\[/, "_____");
155   var elementName = elementName.replace(/\]/, "_____");
156   if (_qfMsg != \'\') {
157     span = document.createElement("span");
158     span.className = "error";
159     _qfMsg = _qfMsg.substring(4);
160     span.appendChild(document.createTextNode(_qfMsg));
161     br = document.createElement("br");
162
163     var errorDiv = document.getElementById(elementName + \'_errorDiv\');
164     if (!errorDiv) {
165       errorDiv = document.createElement("div");
166       errorDiv.id = elementName + \'_errorDiv\';
167     } else {
168       if (   div.firstChild.textContent == \'\'
169           || _qfMsg == div.firstChild.textContent
170          ) {
171         return false;
172       }
173     }
174     while (errorDiv.firstChild) {
175       errorDiv.removeChild(errorDiv.firstChild);
176     }
177
178     errorDiv.insertBefore(br, errorDiv.firstChild);
179     errorDiv.insertBefore(span, errorDiv.firstChild);
180
181     errorDivInserted = false;
182     for (var i = element.parentNode.childNodes.length - 1; i >= 0; i--) {
183       j = i - 1;
184       if (j >= 0 && element.parentNode.childNodes[j].nodeName == "DIV") {
185         element.parentNode.insertBefore(errorDiv, element.parentNode.childNodes[i]);
186         errorDivInserted = true;
187         break;
188       }
189     }
190     if (!errorDivInserted) {
191       element.parentNode.insertBefore(errorDiv, element.parentNode.firstChild);
192     }
193
194     if (div.className.substr(div.className.length - 6, 6) != " error"
195         && div.className != "error") {
196       div.className += " error";
197     }
198
199     return false;
200   } else {
201     var errorDiv = document.getElementById(elementName + \'_errorDiv\');
202     if (errorDiv) {
203       errorDiv.parentNode.removeChild(errorDiv);
204     }
205     
206     // do not remove the error style from the div tag if there is still an error
207     // message
208     if (div.firstChild.innerHTML != "") {
209       return true;
210     }
211
212     if (div.className.substr(div.className.length - 6, 6) == " error") {
213       div.className = div.className.substr(0, div.className.length - 6);
214     } else if (div.className == "error") {
215       div.className = "";
216     }
217
218     return true;
219   }
220 }';
221         $validateJS = '';
222         foreach ($test as $elementName => $jsArr) {
223             // remove group element part of the element name to avoid JS errors
224             $singleElementName = $elementName;
225             $shortNameForJS = str_replace(array('[', ']'), '__', $elementName);
226             $bracketPos = strpos($elementName, '[');
227             if ($bracketPos !== false) {
228                 $singleElementName = substr($elementName, 0, $bracketPos);
229                 $groupElementName = substr($elementName, $bracketPos + 1, -1);
230             }
231             if ($bracketPos === false || !$this->elementExists($singleElementName)) {
232                 $groupElementName = $elementName;
233                 $singleElementName = $elementName;
234             }
235             $id = str_replace('-', '_', $this->_attributes['id']);
236             $js .= '
237 validate_' . $id . '_' . $shortNameForJS . ' = function(element) {
238   var value = \'\';
239   var errFlag = new Array();
240   var _qfGroups = {};
241   var _qfMsg = \'\';
242   var frm = element.parentNode;
243   while (frm && frm.nodeName != "FORM") {
244     frm = frm.parentNode;
245   }
246 ' . join("\n", $jsArr) . '
247   return qf_errorHandler(element, _qfMsg);
248 }
249 ';
250             unset($element);
251             $element =& $this->getElement($singleElementName);
252             $elementNameForJS = 'frm.elements[\'' . $elementName . '\']';
253             if ($element->getType() === 'group' && $singleElementName === $elementName) {
254                 $elementNameForJS = 'document.getElementById(\'' . $element->_elements[0]->getAttribute('id') . '\')';
255             }
256             $validateJS .= '
257   ret = validate_' . $id . '_' . $shortNameForJS . '('. $elementNameForJS . ') && ret;';
258             if ($element->getType() !== 'group') {  // not a group
259                 $valFunc = 'validate_' . $id . '_' . $shortNameForJS . '(this)';
260                 $onBlur = $element->getAttribute('onBlur');
261                 $onChange = $element->getAttribute('onChange');
262                 $element->updateAttributes(array('onBlur' => $onBlur . $valFunc,
263                                                  'onChange' => $onChange . $valFunc));
264             } else {  // group
265                 $elements =& $element->getElements();
266                 for ($i = 0; $i < count($elements); $i++) {
267                     // $groupElementName is a substring of attribute name of the element
268                     if (strpos($elements[$i]->getAttribute('name'), $groupElementName) === 0) {
269                         $valFunc = 'validate_' . $id . '_' . $shortNameForJS . '(this)';
270                         $onBlur = $elements[$i]->getAttribute('onBlur');
271                         $onChange = $elements[$i]->getAttribute('onChange');
272                         $elements[$i]->updateAttributes(array('onBlur'   => $onBlur . $valFunc,
273                                                               'onChange' => $onChange . $valFunc));
274                     }
275                 }
276             }
277         }
278         $js .= '
279 validate_' . $id . ' = function (frm) {
280   var ret = true;
281 ' . $validateJS . ';
282   return ret;
283 }
284 //--><!]]></script>';
285         return $js;
286     } // end func getValidationScript
287
288     // }}}
289
290     function display() {
291         $this->getValidationScript();
292         return parent::display();
293     }
294 }
295
296 ?>