Passed
Pull Request — master (#1323)
by Michael
05:22
created

XoopsForm::getArrayID()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 21
nc 8
nop 4
dl 0
loc 31
rs 8.4444
c 0
b 0
f 0
1
<?php
2
/**
3
 * XOOPS Form Class
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             kernel
15
 * @subpackage          form
16
 * @since               2.0.0
17
 * @author              Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
18
 * @author              Taiwen Jiang <[email protected]>
19
 */
20
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
21
22
/**
23
 * Abstract base class for forms
24
 *
25
 * @author         Kazumi Ono <[email protected]>
26
 * @author         Taiwen Jiang <[email protected]>
27
 * @package        kernel
28
 * @subpackage     form
29
 * @access         public
30
 */
31
class XoopsForm
32
{
33
    /**
34
     * *#@+
35
     *
36
     * @access private
37
     */
38
    /**
39
     * "action" attribute for the html form
40
     *
41
     * @var string
42
     */
43
    public $_action;
44
45
    /**
46
     * "method" attribute for the form.
47
     *
48
     * @var string
49
     */
50
    public $_method;
51
52
    /**
53
     * "name" attribute of the form
54
     *
55
     * @var string
56
     */
57
    public $_name;
58
59
    /**
60
     * title for the form
61
     *
62
     * @var string
63
     */
64
    public $_title;
65
66
    /**
67
     * summary for the form (WGAC2 Requirement)
68
     *
69
     * @var string
70
     */
71
    public $_summary = '';
72
73
    /**
74
     * array of {@link XoopsFormElement} objects
75
     *
76
     * @var array
77
     */
78
    public $_elements = array();
79
80
    /**
81
     * HTML classes for the <form> tag
82
     *
83
     * @var array
84
     */
85
    public $_class = array();
86
    
87
    /**
88
     * extra information for the <form> tag
89
     *
90
     * @var array
91
     */
92
    public $_extra = array();
93
94
    /**
95
     * required elements
96
     *
97
     * @var array
98
     */
99
    public $_required = array();
100
101
    /**
102
     * additional serialised object checksum (ERM Analysis - Requirement)
103
     * @deprecated
104
     * @access private
105
     */
106
    public $_objid = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
107
108
    /**
109
     * *#@-
110
     */
111
112
    /**
113
     * constructor
114
     *
115
     * @param string $title    title of the form
116
     * @param string $name     "name" attribute for the <form> tag
117
     * @param string $action   "action" attribute for the <form> tag
118
     * @param string $method   "method" attribute for the <form> tag
119
     * @param bool   $addtoken whether to add a security token to the form
120
     * @param string $summary
121
     */
122
    public function __construct($title, $name, $action, $method = 'post', $addtoken = false, $summary = '')
123
    {
124
        $this->_title   = $title;
125
        $this->_name    = $name;
126
        $this->_action  = $action;
127
        $this->_method  = $method;
128
        $this->_summary = $summary;
129
        if (false != $addtoken) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
130
            $this->addElement(new XoopsFormHiddenToken());
131
        }
132
    }
133
    /**
134
     * PHP 4 style constructor compatibility shim
135
     * @deprecated all callers should be using parent::__construct()
136
     */
137
    public function XoopsForm()
138
    {
139
        $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
140
        trigger_error("Should call parent::__construct in {$trace[0]['file']} line {$trace[0]['line']},");
141
        self::__construct();
0 ignored issues
show
Bug Best Practice introduced by
The method XoopsForm::__construct() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
        self::/** @scrutinizer ignore-call */ 
142
              __construct();
Loading history...
Bug introduced by
The call to XoopsForm::__construct() has too few arguments starting with title. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
        self::/** @scrutinizer ignore-call */ 
142
              __construct();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
142
    }
143
    /**
144
     * *#@+
145
     * retrieves object serialisation/identification id (sha1 used)
146
     *
147
     * each object has serialisation<br>
148
     * - legal requirement of enterprise relational management (ERM)
149
     *
150
     * @deprecated
151
     * @access public
152
     * @param         $object
153
     * @param  string $hashinfo
154
     * @return string
155
     */
156
    public function getObjectID($object, $hashinfo = 'sha1')
157
    {
158
        if (!is_object($object)) {
159
            $object = $this;
160
        }
161
162
        switch ($hashinfo) {
163
            case 'md5':
164
165
                if (!isset($var['name'])) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $var seems to never exist and therefore isset should always be false.
Loading history...
166
                    $var['name'] = '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$var was never initialized. Although not strictly required by PHP, it is generally a good practice to add $var = array(); before regardless.
Loading history...
167
                }
168
                $var['name'] = md5(get_class($object));
169
170
                foreach (get_object_vars($object) as $key => $value) {
171
                    if ($key !== '_objid') {
172
                        if (!isset($var['value'])) {
173
                            $var['value'] = '';
174
                        }
175
                        $var['value'] = $this->getArrayID($value, $key, $var['value'], $hashinfo);
176
                    }
177
                }
178
179
                foreach (get_class_methods($object) as $key => $value) {
180
                    if (!isset($var['func'])) {
181
                        $var['func'] = '';
182
                    }
183
                    $var['func'] = $this->getArrayID($value, $key, $var['func'], $hashinfo);
184
                }
185
186
                if (!isset($this->_objid)) {
0 ignored issues
show
Deprecated Code introduced by
The property XoopsForm::$_objid has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

186
                if (!isset(/** @scrutinizer ignore-deprecated */ $this->_objid)) {
Loading history...
187
                    $this->_objid = '';
0 ignored issues
show
Deprecated Code introduced by
The property XoopsForm::$_objid has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

187
                    /** @scrutinizer ignore-deprecated */ $this->_objid = '';
Loading history...
188
                }
189
                $this->_objid = md5($var['name'] . ':' . $var['func'] . ':' . $var['value']);
190
191
                return $this->_objid;
192
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
193
194
            default:
195
196
                if (!isset($var['name'])) {
197
                    $var['name'] = '';
198
                }
199
                $var['name'] = sha1(get_class($object));
200
201
                foreach (get_object_vars($object) as $key => $value) {
202
                    if ($key !== '_objid') {
203
                        if (!isset($var['value'])) {
204
                            $var['value'] = '';
205
                        }
206
                        $var['value'] = $this->getArrayID($value, $key, $var['value'], $hashinfo);
207
                    }
208
                }
209
210
                foreach (get_class_methods($object) as $key => $value) {
211
                    if (!isset($var['func'])) {
212
                        $var['func'] = '';
213
                    }
214
                    $var['func'] = $this->getArrayID($value, $key, $var['func'], $hashinfo);
215
                }
216
217
                if (!isset($this->_objid)) {
0 ignored issues
show
Deprecated Code introduced by
The property XoopsForm::$_objid has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

217
                if (!isset(/** @scrutinizer ignore-deprecated */ $this->_objid)) {
Loading history...
218
                    $this->_objid = '';
0 ignored issues
show
Deprecated Code introduced by
The property XoopsForm::$_objid has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

218
                    /** @scrutinizer ignore-deprecated */ $this->_objid = '';
Loading history...
219
                }
220
                $this->_objid = sha1($var['name'] . ':' . $var['func'] . ':' . $var['value']);
221
222
                return $this->_objid;
223
224
        }
225
    }
226
227
    /**
228
     * @param        $value
229
     * @param        $key
230
     * @param        $ret
231
     * @param string $hashinfo
232
     *
233
     * @return string
234
     */
235
    public function getArrayID($value, $key, $ret, $hashinfo = 'sha1')
236
    {
237
        switch ($hashinfo) {
238
            case 'md5':
239
                if (!isset($ret)) {
240
                    $ret = '';
241
                }
242
                if (is_array($value)) {
243
                    foreach ($value as $keyb => $valueb) {
244
                        $ret = md5($ret . ':' . $this->getArrayID($valueb, $keyb, $ret, $hashinfo));
245
                    }
246
                } else {
247
                    $ret = md5($ret . ':' . $key . ':' . $value);
248
                }
249
250
                return $ret;
251
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
252
            default:
253
                if (!isset($ret)) {
254
                    $ret = '';
255
                }
256
                if (is_array($value)) {
257
                    foreach ($value as $keyb => $valueb) {
258
                        $ret = sha1($ret . ':' . $this->getArrayID($valueb, $keyb, $ret, $hashinfo));
259
                    }
260
                } else {
261
                    $ret = sha1($ret . ':' . $key . ':' . $value);
262
                }
263
264
                return $ret;
265
                break;
266
        }
267
    }
268
269
    /**
270
     * return the summary of the form
271
     *
272
     * @param  bool $encode To sanitizer the text?
273
     * @return string
274
     */
275
    public function getSummary($encode = false)
276
    {
277
        return $encode ? htmlspecialchars($this->_summary, ENT_QUOTES) : $this->_summary;
278
    }
279
280
    /**
281
     * return the title of the form
282
     *
283
     * @param  bool $encode To sanitizer the text?
284
     * @return string
285
     */
286
    public function getTitle($encode = false)
287
    {
288
        return $encode ? htmlspecialchars($this->_title, ENT_QUOTES) : $this->_title;
289
    }
290
291
    /**
292
     * get the "name" attribute for the <form> tag
293
     *
294
     * Deprecated, to be refactored
295
     *
296
     * @param  bool $encode To sanitizer the text?
297
     * @return string
298
     */
299
    public function getName($encode = true)
300
    {
301
        return $encode ? htmlspecialchars($this->_name, ENT_QUOTES) : $this->_name;
302
    }
303
304
    /**
305
     * get the "action" attribute for the <form> tag
306
     *
307
     * @param  bool $encode To sanitizer the text?
308
     * @return string
309
     */
310
    public function getAction($encode = true)
311
    {
312
        // Convert &amp; to & for backward compatibility
313
        return $encode ? htmlspecialchars(str_replace('&amp;', '&', $this->_action), ENT_QUOTES) : $this->_action;
314
    }
315
316
    /**
317
     * get the "method" attribute for the <form> tag
318
     *
319
     * @return string
320
     */
321
    public function getMethod()
322
    {
323
        return (strtolower($this->_method) === 'get') ? 'get' : 'post';
324
    }
325
326
    /**
327
     * Add an element to the form
328
     *
329
     * @param string|XoopsFormElement $formElement reference to a {@link XoopsFormElement}
330
     * @param bool             $required    is this a "required" element?
331
     *
332
     */
333
    public function addElement($formElement, $required = false)
334
    {
335
        if (is_string($formElement)) {
336
            $this->_elements[] = $formElement;
337
        } elseif (is_subclass_of($formElement, 'XoopsFormElement')) {
338
            $this->_elements[] = &$formElement;
339
            if (!$formElement->isContainer()) {
340
                if ($required) {
341
                    $formElement->_required = true;
342
                    $this->_required[]      = &$formElement;
343
                }
344
            } else {
345
                $required_elements = &$formElement->getRequired();
0 ignored issues
show
Bug introduced by
The method getRequired() does not exist on XoopsFormElement. It seems like you code against a sub-type of XoopsFormElement such as XoopsFormElementTray. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

345
                $required_elements = &$formElement->/** @scrutinizer ignore-call */ getRequired();
Loading history...
346
                $count             = count($required_elements);
347
                for ($i = 0; $i < $count; ++$i) {
348
                    $this->_required[] = &$required_elements[$i];
349
                }
350
            }
351
        }
352
    }
353
354
    /**
355
     * get an array of forms elements
356
     *
357
     * @param bool $recurse get elements recursively?
358
     *
359
     * @return XoopsFormElement[] array of {@link XoopsFormElement}s
360
     */
361
    public function &getElements($recurse = false)
362
    {
363
        if (!$recurse) {
364
            return $this->_elements;
365
        } else {
366
            $ret   = array();
367
            $count = count($this->_elements);
368
            for ($i = 0; $i < $count; ++$i) {
369
                if (is_object($this->_elements[$i])) {
370
                    if (!$this->_elements[$i]->isContainer()) {
371
                        $ret[] = &$this->_elements[$i];
372
                    } else {
373
                        $elements = &$this->_elements[$i]->getElements(true);
374
                        $count2   = count($elements);
375
                        for ($j = 0; $j < $count2; ++$j) {
376
                            $ret[] = &$elements[$j];
377
                        }
378
                        unset($elements);
379
                    }
380
                }
381
            }
382
383
            return $ret;
384
        }
385
    }
386
387
    /**
388
     * get an array of "name" attributes of form elements
389
     *
390
     * @return array array of form element names
391
     */
392
    public function getElementNames()
393
    {
394
        $ret      = array();
395
        $elements = &$this->getElements(true);
396
        $count    = count($elements);
397
        for ($i = 0; $i < $count; ++$i) {
398
            $ret[] = $elements[$i]->getName();
399
        }
400
401
        return $ret;
402
    }
403
404
    /**
405
     * get a reference to a {@link XoopsFormElement} object by its "name"
406
     *
407
     * @param  string $name "name" attribute assigned to a {@link XoopsFormElement}
408
     * @return object reference to a {@link XoopsFormElement}, false if not found
409
     */
410
    public function &getElementByName($name)
411
    {
412
        $elements =& $this->getElements(true);
413
        $count    = count($elements);
414
        for ($i = 0; $i < $count; ++$i) {
415
            if ($name == $elements[$i]->getName(false)) {
416
                return $elements[$i];
417
            }
418
        }
419
        $elt = null;
420
421
        return $elt;
422
    }
423
424
    /**
425
     * Sets the "value" attribute of a form element
426
     *
427
     * @param string $name  the "name" attribute of a form element
428
     * @param string $value the "value" attribute of a form element
429
     */
430
    public function setElementValue($name, $value)
431
    {
432
        $ele = &$this->getElementByName($name);
433
        if (is_object($ele) && method_exists($ele, 'setValue')) {
434
            $ele->setValue($value);
435
        }
436
    }
437
438
    /**
439
     * Sets the "value" attribute of form elements in a batch
440
     *
441
     * @param array $values array of name/value pairs to be assigned to form elements
442
     */
443
    public function setElementValues($values)
444
    {
445
        if (is_array($values) && !empty($values)) {
446
            // will not use getElementByName() for performance..
447
            $elements = &$this->getElements(true);
448
            $count    = count($elements);
449
            for ($i = 0; $i < $count; ++$i) {
450
                $name = $elements[$i]->getName(false);
451
                if ($name && isset($values[$name]) && method_exists($elements[$i], 'setValue')) {
452
                    $elements[$i]->setValue($values[$name]);
0 ignored issues
show
Bug introduced by
The method setValue() does not exist on XoopsFormElement. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsFormFile or XoopsFormLabel or XoopsFormElementTray or XoopsFormCaptcha or XoopsFormSelectEditor or XoopsFormDateTime or XoopsFormSelectUser. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

452
                    $elements[$i]->/** @scrutinizer ignore-call */ 
453
                                   setValue($values[$name]);
Loading history...
453
                }
454
            }
455
        }
456
    }
457
458
    /**
459
     * Gets the "value" attribute of a form element
460
     *
461
     * @param  string $name   the "name" attribute of a form element
462
     * @param  bool   $encode To sanitizer the text?
463
     * @return string the "value" attribute assigned to a form element, null if not set
464
     */
465
    public function getElementValue($name, $encode = false)
466
    {
467
        $ele = &$this->getElementByName($name);
468
        if (is_object($ele) && method_exists($ele, 'getValue')) {
469
            return $ele->getValue($encode);
470
        }
471
472
        return null;
473
    }
474
475
    /**
476
     * gets the "value" attribute of all form elements
477
     *
478
     * @param  bool $encode To sanitizer the text?
479
     * @return array array of name/value pairs assigned to form elements
480
     */
481
    public function getElementValues($encode = false)
482
    {
483
        // will not use getElementByName() for performance..
484
        $elements = &$this->getElements(true);
485
        $count    = count($elements);
486
        $values   = array();
487
        for ($i = 0; $i < $count; ++$i) {
488
            $name = $elements[$i]->getName(false);
489
            if ($name && method_exists($elements[$i], 'getValue')) {
490
                $values[$name] = &$elements[$i]->getValue($encode);
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on XoopsFormElement. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsFormFile or XoopsFormElementTray or XoopsFormCaptcha or XoopsGroupFormCheckBox or XoopsFormSelectEditor or XoopsFormDateTime or XoopsFormSelectUser. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

490
                $values[$name] = &$elements[$i]->/** @scrutinizer ignore-call */ getValue($encode);
Loading history...
491
            }
492
        }
493
494
        return $values;
495
    }
496
497
    /**
498
     * set the "class" attribute for the <form> tag
499
     *
500
     * @param string $class
501
     */
502
    public function setClass($class)
503
    {
504
        $class = trim($class);
505
        if (!empty($class)) {
506
            $this->_class[] = $class;
507
        }
508
    }
509
    
510
    /**
511
     * set the extra attributes for the <form> tag
512
     *
513
     * @param string $extra extra attributes for the <form> tag
514
     */
515
    public function setExtra($extra)
516
    {
517
        if (!empty($extra)) {
518
            $this->_extra[] = $extra;
519
        }
520
    }
521
522
    /**
523
     * set the summary tag for the <form> tag
524
     *
525
     * @param string $summary
526
     */
527
    public function setSummary($summary)
528
    {
529
        if (!empty($summary)) {
530
            $this->summary = strip_tags($summary);
0 ignored issues
show
Bug Best Practice introduced by
The property summary does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
531
        }
532
    }
533
534
    /**
535
     * get the "class" attribute for the <form> tag
536
     *
537
     * @return string "class" attribute value
538
     */
539
    public function &getClass()
540
    {
541
        if (empty($this->_class)) {
542
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type string.
Loading history...
543
        }
544
        $classes = array();
545
        foreach ($this->_class as $class) {
546
            $classes[] = htmlspecialchars($class, ENT_QUOTES);
547
        }
548
549
        return implode(' ', $classes);
550
    }
551
    
552
    /**
553
     * get the extra attributes for the <form> tag
554
     *
555
     * @return string
556
     */
557
    public function &getExtra()
558
    {
559
        $extra = empty($this->_extra) ? '' : ' ' . implode(' ', $this->_extra);
560
561
        return $extra;
562
    }
563
564
    /**
565
     * make an element "required"
566
     *
567
     * @param XoopsFormElement $formElement reference to a {@link XoopsFormElement}
568
     */
569
    public function setRequired(XoopsFormElement $formElement)
570
    {
571
        $this->_required[] = &$formElement;
572
    }
573
574
    /**
575
     * get an array of "required" form elements
576
     *
577
     * @return array array of {@link XoopsFormElement}s
578
     */
579
    public function &getRequired()
580
    {
581
        return $this->_required;
582
    }
583
584
    /**
585
     * insert a break in the form
586
     *
587
     * This method is abstract. It must be overwritten in the child classes.
588
     *
589
     * @param string $extra extra information for the break
590
     * @abstract
591
     */
592
    public function insertBreak($extra = null)
593
    {
594
    }
595
596
    /**
597
     * returns renderered form
598
     *
599
     * This method is abstract. It must be overwritten in the child classes.
600
     *
601
     * @abstract
602
     */
603
    public function render()
604
    {
605
    }
606
607
    /**
608
     * displays rendered form
609
     */
610
    public function display()
611
    {
612
        echo $this->render();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->render() targeting XoopsForm::render() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
613
    }
614
615
    /**
616
     * Renders the Javascript function needed for client-side for validation
617
     *
618
     * Form elements that have been declared "required" and not set will prevent the form from being
619
     * submitted. Additionally, each element class may provide its own "renderValidationJS" method
620
     * that is supposed to return custom validation code for the element.
621
     *
622
     * The element validation code can assume that the JS "myform" variable points to the form, and must
623
     * execute <i>return false</i> if validation fails.
624
     *
625
     * A basic element validation method may contain something like this:
626
     * <code>
627
     * function renderValidationJS() {
628
     *            $name = $this->getName();
629
     *            return "if (myform.{$name}.value != 'valid') { " .
630
     *              "myform.{$name}.focus(); window.alert( '$name is invalid' ); return false;" .
631
     *              " }";
632
     * }
633
     * </code>
634
     *
635
     * @param boolean $withtags Include the < javascript > tags in the returned string
636
     *
637
     * @return string
638
     */
639
    public function renderValidationJS($withtags = true)
640
    {
641
        $js = '';
642
        if ($withtags) {
643
            $js .= "\n<!-- Start Form Validation JavaScript //-->\n<script type='text/javascript'>\n<!--//\n";
644
        }
645
        $formname = $this->getName();
646
        $js .= "function xoopsFormValidate_{$formname}() { var myform = window.document.{$formname}; ";
647
        $elements =& $this->getElements(true);
648
        foreach ($elements as $elt) {
649
            if (method_exists($elt, 'renderValidationJS')) {
650
                $js .= $elt->renderValidationJS();
651
            }
652
        }
653
        $js .= "return true;\n}\n";
654
        if ($withtags) {
655
            $js .= "//--></script>\n<!-- End Form Validation JavaScript //-->\n";
656
        }
657
658
        return $js;
659
    }
660
661
    /**
662
     * assign to smarty form template instead of displaying directly
663
     *
664
     * @param XoopsTpl $tpl reference to a {@link Smarty} object object
665
     * @see      Smarty
666
     */
667
    public function assign(XoopsTpl $tpl)
668
    {
669
        $i        = -1;
670
        $elements = array();
671
        if (count($this->getRequired()) > 0) {
672
            $this->_elements[] = "<tr class='foot'><td colspan='2'>* = " . _REQUIRED . '</td></tr>';
673
        }
674
        foreach ($this->getElements() as $ele) {
675
            ++$i;
676
            if (is_string($ele)) {
677
                $elements[$i]['body'] = $ele;
678
                continue;
679
            }
680
            $ele_name                 = $ele->getName();
681
            $ele_description          = $ele->getDescription();
682
            $n                        = $ele_name ?: $i;
683
            $elements[$n]['name']     = $ele_name;
684
            $elements[$n]['caption']  = $ele->getCaption();
685
            $elements[$n]['body']     = $ele->render();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $elements[$n]['body'] is correct as $ele->render() targeting XoopsFormElement::render() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
686
            $elements[$n]['hidden']   = $ele->isHidden();
687
            $elements[$n]['required'] = $ele->isRequired();
688
            if ($ele_description != '') {
689
                $elements[$n]['description'] = $ele_description;
690
            }
691
        }
692
        $js = $this->renderValidationJS();
693
        $tpl->assign($this->getName(), array(
694
            'title'      => $this->getTitle(),
695
            'name'       => $this->getName(),
696
            'action'     => $this->getAction(),
697
            'method'     => $this->getMethod(),
698
            'extra'      => 'onsubmit="return xoopsFormValidate_' . $this->getName() . '();"' . $this->getExtra(),
699
            'javascript' => $js,
700
            'elements'   => $elements,
701
            'rendered'   => $this->render(),
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->render() targeting XoopsForm::render() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
702
        ));
703
    }
704
}
705