C:\$Recycle.Bin\S-1-5-21-3967099092-2644009230-141905953-500\$RU7MSUA\html\class\xoopsform\form.php


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
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
206
207
208
209
210
211
212
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
248
249
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
315
316
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<?php
// $Id: form.php,v 1.16.4.2.2.8 2005/07/19 11:37:19 phppp Exp $
//  ------------------------------------------------------------------------ //
//                XOOPS - PHP Content Management System                      //
//                    Copyright (c) 2000 XOOPS.org                           //
//                       <http://www.xoops.org/>                             //
//  ------------------------------------------------------------------------ //
//  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.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //
// Author: Kazumi Ono (AKA onokazu)                                          //
// URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
// Project: The XOOPS Project                                                //
// ------------------------------------------------------------------------- //
// public abstruct
/**
 * 
 * 
 * @package     kernel
 * @subpackage  form
 * 
 * @author        Kazumi Ono    <onokazu@xoops.org>
 * @copyright    copyright (c) 2000-2003 XOOPS.org
 */


/**
 * Abstract base class for forms
 * 
 * @author    Kazumi Ono    <onokazu@xoops.org>
 * @copyright    copyright (c) 2000-2003 XOOPS.org
 * 
 * @package     kernel
 * @subpackage  form
 */
class XoopsForm {
    
/**#@+
     * @access  private
     */
    /** 
     * "action" attribute for the html form
     * @var string  
     */
    
var $_action;

    
/**
     * "method" attribute for the form.
     * @var string  
     */
    
var $_method;

    
/**
     * "name" attribute of the form
     * @var string  
     */
    
var $_name;

    
/**
     * title for the form
     * @var string  
     */
    
var $_title;

    
/**
     * array of {@link XoopsFormElement} objects
     * @var  array   
     */
    
var $_elements = array();

    
/**
     * extra information for the <form> tag
     * @var string  
     */
    
var $_extra;

    
/**
     * required elements
     * @var array   
     */
    
var $_required = array();
    
    
/**#@-*/

    /**
     * constructor
     * 
     * @param    string  $title  title of the form
     * @param    string  $name   "name" attribute for the <form> tag
     * @param    string  $action "action" attribute for the <form> tag
     * @param   string  $method "method" attribute for the <form> tag
     * @param   bool    $addtoken whether to add a security token to the form
     */
    
function XoopsForm($title$name$action$method="post"$addtoken=false){
        
$this->_title $title;
        
$this->_name $name != "" $name "xoops_form";
        
$this->_action $action;
        
$this->_method $method;
        if (
$addtoken != false) {
            
$this->addElement(new XoopsFormHiddenToken());
        }
        
$this->addElement(new XoopsFormHidden('skipValidationJS'0));
        
$this->setExtra('onsubmit="return xoopsFormValidate_'.$this->getName().'();"');
    }

    
/**
     * return the title of the form
     * 
     * @return    string
     */
    
function getTitle(){
        return 
$this->_title;
    }

    
/**
     * get the "name" attribute for the <form> tag
     * 
     * @return    string
     */
    
function getName(){
        return 
$this->_name;
    }

    
/**
     * get the "action" attribute for the <form> tag
     * 
     * @return    string
     */
    
function getAction(){
        return 
$this->_action;
    }

    
/**
     * get the "method" attribute for the <form> tag
     * 
     * @return    string
     */
    
function getMethod(){
        return 
$this->_method;
    }

    
/**
     * Add an element to the form
     *
     * @param    object  &$formElement    reference to a {@link XoopsFormElement}
     * @param    bool    $required       is this a "required" element?
     */
    
function addElement(&$formElement$required=false){
        if ( 
is_string$formElement ) ) {
            
$this->_elements[] = $formElement;
        } elseif ( 
is_subclass_of($formElement'xoopsformelement') ) {
            
$this->_elements[] = $formElement;
            if (
$required) {
                if (!
$formElement->isContainer()) {
                    
$this->_required[] = $formElement;
                } else {
                    
$required_elements $formElement->getRequired();
                    
$count count($required_elements);
                    for (
$i $i $count$i++) {
                        
$this->_required[] = $required_elements[$i];
                    }
                }
            }
        }
    }

    
/**
     * get an array of forms elements
     * 
     * @param    bool    get elements recursively?
     * @return    array   array of {@link XoopsFormElement}s
     */
    
function &getElements($recurse false){
        if (!
$recurse) {
            return 
$this->_elements;
        } else {
            
$ret = array();
            
$count count($this->_elements);
            for (
$i 0$i $count$i++) {
                if (!
$this->_elements[$i]->isContainer()) {
                    
$ret[] =& $this->_elements[$i];
                } else {
                    
$elements =& $this->_elements[$i]->getElements(true);
                    
$count2 count($elements);
                    for (
$j 0$j $count2$j++) {
                        
$ret[] =& $elements[$j];
                    }
                    unset(
$elements);
                }
            }
            return 
$ret;
        }
    }

    
/**
     * get an array of "name" attributes of form elements
     * 
     * @return    array   array of form element names
     */
    
function getElementNames()
    {
        
$ret = array();
        
$elements =& $this->getElements(true);
        
$count count($elements);
        for (
$i 0$i $count$i++) {
            
$ret[] = $elements[$i]->getName();
        }
        return 
$ret;
    }

    
/**
     * get a reference to a {@link XoopsFormElement} object by its "name"
     * 
     * @param  string  $name    "name" attribute assigned to a {@link XoopsFormElement}
     * @return object  reference to a {@link XoopsFormElement}, false if not found
     */
    
function &getElementByName($name){
        
$elements =& $this->getElements(true);
        
$count count($elements);
        for (
$i 0$i $count$i++) {
            if (
$name == $elements[$i]->getName()) {
                return 
$elements[$i];
            }
        }
        return 
false;
    }

    
/**
     * Sets the "value" attribute of a form element
     * 
     * @param    string $name    the "name" attribute of a form element
     * @param    string $value    the "value" attribute of a form element
     */
    
function setElementValue($name$value){
        
$ele =& $this->getElementByName($name);
        if (
is_object($ele) && method_exists($ele'setValue')) {
            
$ele->setValue($value);
        }
    }

    
/**
     * Sets the "value" attribute of form elements in a batch
     * 
     * @param    array $values    array of name/value pairs to be assigned to form elements
     */
    
function setElementValues($values){
        if (
is_array($values) && !empty($values)) {
            
// will not use getElementByName() for performance..
            
$elements =& $this->getElements(true);
            
$count count($elements);
            for (
$i 0$i $count$i++) {
                
$name $elements[$i]->getName();
                if (
$name && isset($values[$name]) && method_exists($elements[$i], 'setValue')) {
                    
$elements[$i]->setValue($values[$name]);
                }
            }
        }
    }

    
/**
     * Gets the "value" attribute of a form element
     * 
     * @param    string     $name    the "name" attribute of a form element
     * @return    string     the "value" attribute assigned to a form element, null if not set
     */
    
function &getElementValue($name){
        
$ele =& $this->getElementByName($name);
        if (
is_object($ele) && method_exists($ele'getValue')) {
            return 
$ele->getValue($value);
        }
        return;
    }

    
/**
     * gets the "value" attribute of all form elements
     * 
     * @return    array     array of name/value pairs assigned to form elements
     */
    
function &getElementValues(){
        
// will not use getElementByName() for performance..
        
$elements =& $this->getElements(true);
        
$count count($elements);
        
$values = array();
        for (
$i 0$i $count$i++) {
            
$name $elements[$i]->getName();
            if (
$name && method_exists($elements[$i], 'getValue')) {
                
$values[$name] =& $elements[$i]->getValue();
            }
        }
        return 
$values;
    }

    
/**
     * Add extra attributes to the form.
     *
     * This string will be inserted verbatim and unvalidated in the
     * element's tag. Know what you are doing!
     *
     * @param    string  $extra
     * @param   string  $replace If true, passed string will replace current content otherwise it will be appended to it
     * @return    string New content of the extra string
     */
    
function setExtra($extra$replace false){
        if ( 
$replace) {
            
$this->_extra trim($extra);
        } else {
            
$this->_extra .= " " trim($extra);
        }
        return 
$this->_extra;
    }

    
/**
     * get the extra attributes for the <form> tag
     *
     * @return    string
     */
    
function &getExtra(){
        if (isset(
$this->_extra)) {
            return 
$this->_extra;
        }
    }

    
/**
     * make an element "required"
     *
     * @param    object  &$formElement    reference to a {@link XoopsFormElement}
     */
    
function setRequired(&$formElement){
        
$this->_required[] =& $formElement;
    }

    
/**
     * get an array of "required" form elements
     * 
     * @return    array   array of {@link XoopsFormElement}s 
     */
    
function &getRequired(){
        return 
$this->_required;
    }

    
/**
     * insert a break in the form
     * 
     * This method is abstract. It must be overwritten in the child classes.
     *
     * @param    string  $extra  extra information for the break
     * @abstract
     */
    
function insertBreak($extra null){
    }

    
/**
     * returns renderered form
     *
     * This method is abstract. It must be overwritten in the child classes.
     *
     * @abstract
     */
    
function render(){
    }

    
/**
     * displays rendered form
     */
    
function display(){
        echo 
$this->render();
    }

    
/**
     * Renders the Javascript function needed for client-side for validation
     *
     * @param        boolean  $withtags    Include the < javascript > tags in the returned string
     */
    
function renderValidationJS$withtags true ) {
        
$js "";
        if ( 
$withtags ) {
            
$js .= "\n<!-- Start Form Vaidation JavaScript //-->\n<script type='text/javascript'>\n<!--//\n";
        }
        
$myts =& MyTextSanitizer::getInstance();
        
$formname $this->getName();
        
$required =& $this->getRequired();
        
$reqcount count($required);
        
$js .= "function xoopsFormValidate_{$formname}() {
        myform = window.document.
$formname;\n
        if(myform.skipValidationJS.value == 1) return true;\n
        "
;
        for (
$i 0$i $reqcount$i++) {
            
$eltname    $required[$i]->getName();
            
$eltcaption trim$required[$i]->getCaption() );
            
$eltmsg = empty($eltcaption) ? sprintf_FORM_ENTER$eltname ) : sprintf_FORM_ENTER$eltcaption );
            
$eltmsg str_replace('"''\"'$eltmsg);
            
$js .= "if ( myform.{$eltname}.value == \"\" ) "
                
"{ window.alert(\"{$eltmsg}\"); myform.{$eltname}.focus(); return false; }\n";
        }
        
$js .= "return true;\n}\n";
        if ( 
$withtags ) {
            
$js .= "//--></script>\n<!-- End Form Vaidation JavaScript //-->\n";
        }
        return 
$js;
    }
    
/**
     * assign to smarty form template instead of displaying directly
     *
     * @param    object  &$tpl    reference to a {@link Smarty} object
     * @see     Smarty
     */
    
function assign(&$tpl){
        
$i 0;
        
$elements = array();
        
// Hack Hervé Thouzard
        
$required =& $this->getRequired();
        
$reqnames=array();
        foreach(
array_keys($required) as $i) {
            
$reqnames[] = $required[$i]->getName();
        }
        
// End Hack Hervé Thouzard
        
foreach ( $this->getElements() as $ele ) {
            
$n = ($ele->getName() != "") ? $ele->getName() : $i;
            
$elements[$n]['name']      = $ele->getName();
            
$elements[$n]['caption']  = $ele->getCaption();
            
$elements[$n]['body']      = $ele->render();
            
$elements[$n]['hidden']      = $ele->isHidden();
            
$elements[$n]['type']     = strtolower(get_class($ele));
            
$elements[$n]['required'] = in_array($ele->getName(), $reqnames);
            if (
$ele->getDescription() != '') {
                
$elements[$n]['description']  = $ele->getDescription();
            }
            
$i++;
        }
        
$js $this->renderValidationJS();
        
$tpl->assign($this->getName(), array('title' => $this->getTitle(), 'name' => $this->getName(), 'action' => $this->getAction(),  'method' => $this->getMethod(), 'extra' => $this->getExtra(), 'javascript' => $js'elements' => $elements));
    }
}
?>