source: trunk/Packages/FormManager/Types/Date.php

Last change on this file was 8, checked in by chronos, 19 months ago
  • Modified: Updated Common package.
  • Modified: Form types made as separate FormManager package.
  • Fixed: PHP 8.1 support.
File size: 3.4 KB
Line 
1<?php
2
3include_once(dirname(__FILE__).'/Base.php');
4
5class TypeDate extends TypeBase
6{
7 function __construct(FormManager $FormManager)
8 {
9 parent::__construct($FormManager);
10 $this->DatabaseCompareOperators = array('Rovno' => '=', 'Nerovno' => '!=', 'Menší' => '<', 'Větší' => '>');
11 }
12
13 function OnView(array $Item): ?string
14 {
15 if ($Item['Value'] == null) return '';
16 if ((strtolower($Item['Value']) == 'now') or (strtolower($Item['Value']) == '')) $Item['Value'] = time();
17 $Parts = getdate($Item['Value']);
18
19 $Output = $Parts['mday'].'.'.$Parts['mon'].'.'.$Parts['year'];
20 return $Output;
21 }
22
23 function OnEdit(array $Item): string
24 {
25 global $MonthNames;
26
27 if (($Item['Value'] == null) or (($Item['Value'] !== null) and ((strtolower($Item['Value']) == 'now') or (strtolower($Item['Value']) == ''))))
28 {
29 $Item['Value'] = time();
30 $IsNull = true;
31 } else $IsNull = false;
32 $Parts = getdate($Item['Value']);
33
34 $Output = '';
35 $Style = '';
36 if (array_key_exists('Null', $Item) and $Item['Null'])
37 {
38 if (!$IsNull)
39 {
40 $Checked = ' checked="1"';
41 $Style = 'style="display:inline;"';
42 } else
43 {
44 $Checked = '';
45 $Style = 'style="display:none;"';
46 }
47 $Output .= '<input type="checkbox" name="'.$Item['Name'].'-null"'.$Checked.' onclick="toggle(\''.
48 $Item['Name'].'-day\');toggle(\''.$Item['Name'].'-month\');toggle(\''.$Item['Name'].'-year\');"/>';
49 }
50
51 // Day
52 $Output .= '<select name="'.$Item['Name'].'-day" id="'.$Item['Name'].'-day" '.$Style.'>';
53 for ($I = 1; $I <= 31; $I++)
54 {
55 if ($Parts['mday'] == $I) $Selected = ' selected="1"'; else $Selected = '';
56 $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
57 }
58 $Output .= '</select>';
59 // Month
60 $Output .= '<select name="'.$Item['Name'].'-month" id="'.$Item['Name'].'-month" '.$Style.'>';
61 for ($I = 1; $I <= 12; $I++)
62 {
63 if ($Parts['mon'] == $I) $Selected = ' selected="1"'; else $Selected = '';
64 $Output .= '<option value="'.$I.'"'.$Selected.'>'.$MonthNames[$I].'</option>';
65 }
66 $Output .= '</select>';
67 // Year
68 $Output .= '<select name="'.$Item['Name'].'-year" id="'.$Item['Name'].'-year" '.$Style.'>';
69 for ($I = 1900; $I < 2100; $I++)
70 {
71 if ($Parts['year'] == $I) $Selected = ' selected="1"'; else $Selected = '';
72 $Output .= '<option value="'.$I.'"'.$Selected.'>'.$I.'</option>';
73 }
74 $Output .= '</select>';
75 return $Output;
76 }
77
78 function OnLoad(array $Item): ?string
79 {
80 if (!array_key_exists($Item['Name'].'-null', $_POST) and array_key_exists('Null', $Item) and ($Item['Null'] == true)) return null;
81 else return mktime(0, 0, 0, $_POST[$Item['Name'].'-month'], $_POST[$Item['Name'].'-day'], $_POST[$Item['Name'].'-year']);
82 }
83
84 function OnCanLoad(array $Item): bool
85 {
86 return array_key_exists($Item['Name'].'-null', $_POST) or (array_key_exists($Item['Name'].'-day', $_POST) and
87 array_key_exists($Item['Name'].'-month', $_POST) and array_key_exists($Item['Name'].'-year', $_POST));
88 }
89
90 function OnLoadDb(array $Item): ?string
91 {
92 return MysqlDateToTime($Item['Value']);
93 }
94
95 function OnSaveDb(array $Item): ?string
96 {
97 if ($Item['Value'] == null) return null;
98 else return date('Y-m-d', $Item['Value']);
99 }
100
101 function DatabaseEscape(string $Value): string
102 {
103 return '"'.addslashes($Value).'"';
104 }
105}
Note: See TracBrowser for help on using the repository browser.