Completed
Pull Request — master (#170)
by Richard
23:48
created

XoopsFormDateTime::__construct()   D

Complexity

Conditions 11
Paths 150

Size

Total Lines 46
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 37
c 1
b 0
f 0
nc 150
nop 5
dl 0
loc 46
rs 4.9629

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 28 and the first side effect is on line 19.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * XOOPS form element of datetime
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 (http://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
 */
19
defined('XOOPS_ROOT_PATH') || exit('Restricted access');
20
21
/**
22
 * Date and time selection field
23
 *
24
 * @author         Kazumi Ono <[email protected]>
25
 * @package        kernel
26
 * @subpackage     form
27
 */
28
class XoopsFormDateTime extends XoopsFormElementTray
29
{
30
    const SHOW_BOTH = 1;
31
    const SHOW_DATE = 0;
32
    const SHOW_TIME = 2;
33
34
    /**
35
     * XoopsFormDateTime::XoopsFormDateTime()
36
     *
37
     * @param mixed   $caption  form field caption
38
     * @param mixed   $name     form variable name
39
     * @param integer $size     size of date select
40
     * @param integer $value    unix timestamp, defaults to now
41
     * @param mixed   $showtime control display of date and time elements
42
     *                           SHOW_BOTH, true  - show both date and time selectors
43
     *                           SHOW_DATE, false - only show date selector
44
     *                           SHOW_TIME        - only show time selector
45
     */
46
    public function __construct($caption, $name, $size = 15, $value = 0, $showtime = true)
47
    {
48
        parent::__construct($caption, '&nbsp;');
49
        switch ((int) $showtime) {
50
            case static::SHOW_DATE:
51
                $displayDate = true;
52
                $displayTime = false;
53
                break;
54
            case static::SHOW_TIME:
55
                $displayDate = false;
56
                $displayTime = true;
57
                break;
58
            default:
59
                $displayDate = true;
60
                $displayTime = true;
61
                break;
62
        }
63
        $value    = (int)$value;
64
        $value    = ($value > 0) ? $value : time();
65
        $datetime = getdate($value);
66
        if ($displayDate) {
67
            $this->addElement(new XoopsFormTextDateSelect('', $name . '[date]', $size, $value));
68
        } else {
69
            $value = !is_numeric($value) ? time() : (int)$value;
70
            $value = ($value == 0) ? time() : $value;
71
            $displayValue = date(_SHORTDATESTRING, $value);
72
            $this->addElement(new XoopsFormHidden($name . '[date]', $displayValue));
73
        }
74
75
        if ($displayTime) {
76
            $timearray = array();
77
            for ($i = 0; $i < 24; ++$i) {
78
                for ($j = 0; $j < 60; $j += 10) {
79
                    $key = ($i * 3600) + ($j * 60);
80
                    $timearray[$key] = ($j != 0) ? $i . ':' . $j : $i . ':0' . $j;
81
                }
82
            }
83
            ksort($timearray);
84
85
            $timeselect = new XoopsFormSelect('', $name . '[time]', $datetime['hours'] * 3600 + 600 * ceil($datetime['minutes'] / 10));
86
            $timeselect->addOptionArray($timearray);
87
            $this->addElement($timeselect);
88
        } else {
89
            $this->addElement(new XoopsFormHidden($name . '[time]', 0));
90
        }
91
    }
92
}
93