Passed
Branch master (7e303a)
by Michael
02:15
created

ThemeTabForm::setSummary()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace XoopsModules\Publisher;
4
5
/*
6
 You may not change or alter any portion of this comment or credits
7
 of supporting developers from this source code or any supporting source code
8
 which is considered copyrighted (c) material of the original comment or credit authors.
9
10
 This program is distributed in the hope that it will be useful,
11
 but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
/**
15
 *  Publisher class
16
 *
17
 * @copyright       The XUUPS Project http://sourceforge.net/projects/xuups/
18
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
19
 * @package         Publisher
20
 * @since           1.0
21
 * @author          trabis <[email protected]>
22
 * @author          John Neill <[email protected]>
23
 */
24
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
25
26
require_once dirname(__DIR__) . '/include/common.php';
27
28
/**
29
 * XoopsThemeTabForm
30
 *
31
 * @package
32
 * @author    John
33
 * @copyright Copyright (c) 2009
34
 */
35
class ThemeTabForm extends \XoopsForm
36
{
37
    public $formTabs = [];
38
39
    /**
40
     * "action" attribute for the html form
41
     *
42
     * @var string
43
     */
44
    public $action;
45
46
    /**
47
     * "method" attribute for the form.
48
     *
49
     * @var string
50
     */
51
    public $method;
52
53
    /**
54
     * "name" attribute of the form
55
     *
56
     * @var string
57
     */
58
    public $name;
59
60
    /**
61
     * title for the form
62
     *
63
     * @var string
64
     */
65
    public $title;
66
67
    /**
68
     * summary for the form (WGAC2 Requirement)
69
     *
70
     * @var string
71
     */
72
    public $summary = '';
73
74
    /**
75
     * array of {@link XoopsFormElement} objects
76
     *
77
     * @var array
78
     */
79
    public $elements = [];
80
81
    /**
82
     * extra information for the <form> tag
83
     *
84
     * @var array
85
     */
86
    public $extra = [];
87
88
    /**
89
     * required elements
90
     *
91
     * @var array
92
     */
93
    public $required = [];
94
95
    /**
96
     * @param string $title
97
     * @param string $name
98
     * @param string $action
99
     * @param string $method
100
     * @param bool   $addtoken
101
     * @param string $summary
102
     */
103
    public function __construct($title, $name, $action, $method = 'post', $addtoken = false, $summary = '')
104
    {
105
        //        global $xoTheme;
106
        //        $GLOBALS['xoTheme']->addScript(PUBLISHER_URL . '/assets/js/ui.core.js');
107
        //        $GLOBALS['xoTheme']->addScript(PUBLISHER_URL . '/assets/js/ui.tabs.js');
108
        //        $GLOBALS['xoTheme']->addStylesheet(PUBLISHER_URL . '/assets/css/jquery-ui-1.7.1.custom.css');
109
110
        $GLOBALS['xoTheme']->addScript('browse.php?Frameworks/jquery/plugins/jquery.ui.js');
111
        $GLOBALS['xoTheme']->addStylesheet(XOOPS_URL . '/modules/system/css/ui/' . xoops_getModuleOption('jquery_theme', 'system') . '/ui.all.css');
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getModuleOption() 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

111
        $GLOBALS['xoTheme']->addStylesheet(XOOPS_URL . '/modules/system/css/ui/' . /** @scrutinizer ignore-deprecated */ xoops_getModuleOption('jquery_theme', 'system') . '/ui.all.css');
Loading history...
112
113
        $this->title   = $title;
114
        $this->name    = $name;
115
        $this->action  = $action;
116
        $this->method  = $method;
117
        $this->summary = $summary;
118
        if (false !== $addtoken) {
119
            $this->addElement(new \XoopsFormHiddenToken());
120
        }
121
    }
122
123
    //function render() {}
124
125
    /**
126
     * @param \XoopsTpl $tpl
127
     */
128
    public function assign(\XoopsTpl $tpl)
129
    {
130
        $i        = -1;
131
        $tab      = -1;
132
        $elements = [];
133
        if (count($this->getRequired()) > 0) {
134
            $this->elements[] = "<tr class='foot'><td colspan='2'>* = " . _REQUIRED . '</td></tr>';
135
        }
136
        foreach ($this->getElements() as $ele) {
137
            ++$i;
138
            if (is_string($ele) && 'addTab' === $ele) {
139
                ++$tab;
140
                continue;
141
            }
142
            if (is_string($ele) && 'endTabs' === $ele) {
143
                $tab = -1;
144
                continue;
145
            }
146
            if (is_string($ele)) {
147
                $elements[$i]['body'] = $ele;
148
                $elements[$i]['tab']  = $tab;
149
                continue;
150
            }
151
            $eleName                  = $ele->getName();
152
            $eleDescription           = $ele->getDescription();
153
            $n                        = $eleName ?: $i;
154
            $elements[$n]['name']     = $eleName;
155
            $elements[$n]['caption']  = $ele->getCaption();
156
            $elements[$n]['body']     = $ele->render();
157
            $elements[$n]['hidden']   = $ele->isHidden() ? true : false;
158
            $elements[$n]['required'] = $ele->isRequired();
159
            if ('' != $eleDescription) {
160
                $elements[$n]['description'] = $eleDescription;
161
            }
162
            $elements[$n]['tab'] = $tab;
163
        }
164
        $js = $this->renderValidationJS();
165
        $tpl->assign($this->getName(), [
166
            'title'      => $this->getTitle(),
167
            'id'         => 'tab_' . preg_replace('/[^a-z0-9]+/i', '', $this->getTitle()),
168
            'name'       => $this->getName(),
169
            'action'     => $this->getAction(),
170
            'method'     => $this->getMethod(),
171
            'extra'      => 'onsubmit="return xoopsFormValidate_' . $this->getName() . '();"' . $this->getExtra(),
172
            'javascript' => $js,
173
            'tabs'       => $this->formTabs,
174
            'elements'   => $elements
175
        ]);
176
    }
177
178
    /**
179
     * XoopsThemeTabForm::startTab()
180
     *
181
     * @param mixed $tabText
182
     */
183
    public function startTab($tabText)
184
    {
185
        $temp = $this->startFormTabs($tabText);
186
        $this->addElement($temp);
187
    }
188
189
    /**
190
     * XoopsThemeTabForm::endTab()
191
     */
192
    public function endTabs()
193
    {
194
        $temp = $this->endFormTabs();
195
        $this->addElement($temp);
196
    }
197
198
    /**
199
     * Creates a tab with title text and starts that tabs page
200
     *
201
     * @param $tabText - This is what is displayed on the tab
0 ignored issues
show
Documentation Bug introduced by
The doc comment - at position 0 could not be parsed: Unknown type name '-' at position 0 in -.
Loading history...
202
     *
203
     * @return string
204
     */
205
    public function startFormTabs($tabText)
206
    {
207
        $this->formTabs[] = $tabText;
208
        $ret              = 'addTab';
209
210
        return $ret;
211
    }
212
213
    /**
214
     * Ends a tab page
215
     *
216
     * @return string
217
     */
218
    public function endFormTabs()
219
    {
220
        $ret = 'endTabs';
221
222
        return $ret;
223
    }
224
225
    /**
226
     * @param bool $encode
227
     *
228
     * @return string
229
     */
230
    public function getSummary($encode = false)
231
    {
232
        return $encode ? htmlspecialchars($this->summary, ENT_QUOTES) : $this->summary;
233
    }
234
235
    /**
236
     * return the title of the form
237
     *
238
     * @param bool $encode To sanitizer the text?
239
     *
240
     * @return string
241
     */
242
    public function getTitle($encode = false)
243
    {
244
        return $encode ? htmlspecialchars($this->title, ENT_QUOTES) : $this->title;
245
    }
246
247
    /**
248
     * get the "name" attribute for the <form> tag
249
     * Deprecated, to be refactored
250
     *
251
     * @param bool $encode To sanitizer the text?
252
     *
253
     * @return string
254
     */
255
    public function getName($encode = true)
256
    {
257
        return $encode ? htmlspecialchars($this->name, ENT_QUOTES) : $this->name;
258
    }
259
260
    /**
261
     * get the "action" attribute for the <form> tag
262
     *
263
     * @param bool $encode To sanitizer the text?
264
     *
265
     * @return string
266
     */
267
    public function getAction($encode = true)
268
    {
269
        // Convert &amp; to & for backward compatibility
270
        return $encode ? htmlspecialchars(str_replace('&amp;', '&', $this->action), ENT_QUOTES) : $this->action;
271
    }
272
273
    /**
274
     * get the "method" attribute for the <form> tag
275
     *
276
     * @return string
277
     */
278
    public function getMethod()
279
    {
280
        return ('get' === mb_strtolower($this->method)) ? 'get' : 'post';
281
    }
282
283
    /**
284
     * Add an element to the form
285
     *
286
     * @param string|\XoopsFormElement $formElement reference to a {@link XoopsFormElement}
287
     * @param bool                     $required    is this a "required" element?
288
     */
289
    public function addElement($formElement, $required = false)
290
    {
291
        if (is_string($formElement)) {
292
            $this->elements[] =& $formElement;
293
        } elseif (is_subclass_of($formElement, 'xoopsformelement')) {
294
            $this->elements[] =& $formElement;
295
            if ($required) {
296
                if (method_exists($formElement, 'setRequired')) {
297
                    $formElement->setRequired(true);
298
                } else {
299
                    $formElement->required = true;
0 ignored issues
show
Bug introduced by
The property required does not exist on XoopsFormElement. Did you mean _required?
Loading history...
300
                }
301
                $this->required[] =& $formElement;
302
            }
303
        }
304
    }
305
306
    /**
307
     * get an array of forms elements
308
     *
309
     * @param bool $recurse get elements recursively?
310
     *
311
     * @return array array of {@link XoopsFormElement}s
312
     */
313
    public function &getElements($recurse = false)
314
    {
315
        if (!$recurse) {
316
            return $this->elements;
317
        }
318
        $ret   = [];
319
        $count = count($this->elements);
0 ignored issues
show
Unused Code introduced by
The assignment to $count is dead and can be removed.
Loading history...
320
        foreach ($this->elements as $i => $iValue) {
321
            if (is_object($this->elements[$i])) {
322
                $ret[] = &$this->elements[$i];
323
            }
324
        }
325
326
        return $ret;
327
    }
328
329
    /**
330
     * get an array of "name" attributes of form elements
331
     *
332
     * @return array array of form element names
333
     */
334
    public function getElementNames()
335
    {
336
        $ret      = [];
337
        $elements = &$this->getElements(true);
338
        $count    = count($elements);
0 ignored issues
show
Unused Code introduced by
The assignment to $count is dead and can be removed.
Loading history...
339
        foreach ($elements as $iValue) {
340
            $ret[] = $iValue->getName();
341
        }
342
343
        return $ret;
344
    }
345
346
    /**
347
     * get a reference to a {@link XoopsFormElement} object by its "name"
348
     *
349
     * @param string $name "name" attribute assigned to a {@link XoopsFormElement}
350
     *
351
     * @return bool|\XoopsFormElement reference to a {@link XoopsFormElement}, false if not found
352
     */
353
    public function &getElementByName($name)
354
    {
355
        $elements =& $this->getElements(true);
356
        $count    = count($elements);
0 ignored issues
show
Unused Code introduced by
The assignment to $count is dead and can be removed.
Loading history...
357
        foreach ($elements as $i => $iValue) {
358
            if ($name == $iValue->getName(false)) {
359
                return $elements[$i];
360
            }
361
        }
362
        $elt = null;
363
364
        return $elt;
365
    }
366
367
    /**
368
     * Sets the "value" attribute of a form element
369
     *
370
     * @param string $name  the "name" attribute of a form element
371
     * @param string $value the "value" attribute of a form element
372
     */
373
    public function setElementValue($name, $value)
374
    {
375
        $ele =& $this->getElementByName($name);
376
        if (is_object($ele) && method_exists($ele, 'setValue')) {
377
            $ele->setValue($value);
378
        }
379
    }
380
381
    /**
382
     * Sets the "value" attribute of form elements in a batch
383
     *
384
     * @param array $values array of name/value pairs to be assigned to form elements
385
     */
386
    public function setElementValues($values)
387
    {
388
        if (is_array($values) && !empty($values)) {
389
            // will not use getElementByName() for performance..
390
            $elements =& $this->getElements(true);
391
            $count    = count($elements);
0 ignored issues
show
Unused Code introduced by
The assignment to $count is dead and can be removed.
Loading history...
392
            foreach ($elements as $i => $iValue) {
393
                $name = $iValue->getName(false);
394
                if ($name && isset($values[$name]) && method_exists($elements[$i], 'setValue')) {
395
                    $iValue->setValue($values[$name]);
396
                }
397
            }
398
        }
399
    }
400
401
    /**
402
     * Gets the "value" attribute of a form element
403
     *
404
     * @param string $name   the "name" attribute of a form element
405
     * @param bool   $encode To sanitizer the text?
406
     *
407
     * @return string the "value" attribute assigned to a form element, null if not set
408
     */
409
    public function getElementValue($name, $encode = false)
410
    {
411
        $ele =& $this->getElementByName($name);
412
        if (is_object($ele) && method_exists($ele, 'getValue')) {
413
            return $ele->getValue($encode);
414
        }
415
416
        return null;
417
    }
418
419
    /**
420
     * gets the "value" attribute of all form elements
421
     *
422
     * @param bool $encode To sanitizer the text?
423
     *
424
     * @return array array of name/value pairs assigned to form elements
425
     */
426
    public function getElementValues($encode = false)
427
    {
428
        // will not use getElementByName() for performance..
429
        $elements =& $this->getElements(true);
430
        $count    = count($elements);
0 ignored issues
show
Unused Code introduced by
The assignment to $count is dead and can be removed.
Loading history...
431
        $values   = [];
432
        foreach ($elements as $i => $iValue) {
433
            $name = $iValue->getName(false);
434
            if ($name && method_exists($elements[$i], 'getValue')) {
435
                $values[$name] = $iValue->getValue($encode);
436
            }
437
        }
438
439
        return $values;
440
    }
441
442
    /**
443
     * set the extra attributes for the <form> tag
444
     *
445
     * @param string $extra extra attributes for the <form> tag
446
     * @return string|void
447
     */
448
    public function setExtra($extra)
449
    {
450
        if (!empty($extra)) {
451
            $this->extra[] = $extra;
452
        }
453
    }
454
455
    /**
456
     * set the summary tag for the <form> tag
457
     *
458
     * @param string $summary
459
     */
460
    public function setSummary($summary)
461
    {
462
        if (!empty($summary)) {
463
            $this->summary = strip_tags($summary);
464
        }
465
    }
466
467
    /**
468
     * get the extra attributes for the <form> tag
469
     *
470
     * @return string
471
     */
472
    public function &getExtra()
473
    {
474
        $extra = empty($this->extra) ? '' : ' ' . implode(' ', $this->extra);
475
476
        return $extra;
477
    }
478
479
    /**
480
     * make an element "required"
481
     *
482
     * @param \XoopsFormElement $formElement reference to a {@link XoopsFormElement}
483
     */
484
    public function setRequired(\XoopsFormElement $formElement)
485
    {
486
        $this->required[] =& $formElement;
487
    }
488
489
    /**
490
     * get an array of "required" form elements
491
     *
492
     * @return array array of {@link XoopsFormElement}s
493
     */
494
    public function &getRequired()
495
    {
496
        return $this->required;
497
    }
498
499
    /**
500
     * insert a break in the form
501
     * This method is abstract. It must be overwritten in the child classes.
502
     *
503
     * @param string $extra extra information for the break
504
     *
505
     * @abstract
506
     */
507
    public function insertBreak($extra = null)
508
    {
509
    }
510
511
    /**
512
     * returns renderered form
513
     * This method is abstract. It must be overwritten in the child classes.
514
     *
515
     * @abstract
516
     */
517
    public function render()
518
    {
519
        return '';
520
    }
521
522
    /**
523
     * displays rendered form
524
     */
525
    public function display()
526
    {
527
        echo $this->render();
528
    }
529
530
    /**
531
     * Renders the Javascript function needed for client-side for validation
532
     * Form elements that have been declared "required" and not set will prevent the form from being
533
     * submitted. Additionally, each element class may provide its own "renderValidationJS" method
534
     * that is supposed to return custom validation code for the element.
535
     * The element validation code can assume that the JS "myform" variable points to the form, and must
536
     * execute <i>return false</i> if validation fails.
537
     * A basic element validation method may contain something like this:
538
     * <code>
539
     * function renderValidationJS() {
540
     *            $name = $this->getName();
541
     *            return "if (myform.{$name}.value != 'valid') { " .
542
     *              "myform.{$name}.focus(); window.alert( '$name is invalid' ); return false;" .
543
     *              " }";
544
     * }
545
     * </code>
546
     *
547
     * @param bool $withtags Include the < javascript > tags in the returned string
548
     *
549
     * @return string
550
     */
551
    public function renderValidationJS($withtags = true)
552
    {
553
        $js = '';
554
        if ($withtags) {
555
            $js .= "\n<!-- Start Form Validation JavaScript //-->\n<script type='text/javascript'>\n<!--//\n";
556
        }
557
        $formname = $this->getName();
558
        $js       .= "function xoopsFormValidate_{$formname}() { var myform = window.document.{$formname}; ";
559
        $elements =& $this->getElements(true);
560
        foreach ($elements as $elt) {
561
            if (method_exists($elt, 'renderValidationJS')) {
562
                $js .= $elt->renderValidationJS();
563
            }
564
        }
565
        $js .= "return true;\n}\n";
566
        if ($withtags) {
567
            $js .= "//--></script>\n";
568
        }
569
570
        return $js;
571
    }
572
}
573