1
|
|
|
<?php |
|
|
|
|
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, ' '); |
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
|
|
|
|
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.