dolibarr 24.0.0-beta
timestampfield.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2025 Open-Dsi <support@open-dsi.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
24require_once DOL_DOCUMENT_ROOT . '/core/class/fields/commonfield.class.php';
25
26
31{
35 public $emptyValues = array('');
36
49 public function printInputSearchField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
50 {
51 global $langs;
52
53 // TODO Must also support $moreCss ?
54
55 $htmlName = $keyPrefix . $key . $keySuffix;
56 $prefill = array(
57 'start' => $value['start'] ?? '',
58 'end' => $value['end'] ?? '',
59 );
60
61 // Search filter on a date field shows two inputs to select a date range
62 $out = '<div ' . $moreAttrib . '><div class="nowrap">';
63 $out .= self::$form->selectDate($prefill['start'], $htmlName . '_start', 1, 1, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans("From"), 'tzuserrel');
64 $out .= '</div><div class="nowrap">';
65 $out .= self::$form->selectDate($prefill['end'], $htmlName . '_end', 1, 1, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans("to"), 'tzuserrel');
66 $out .= '</div></div>';
67
68 return $out;
69 }
70
83 public function printInputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
84 {
85 $required = $fieldInfos->required ? 1 : 0;
86 $htmlName = $keyPrefix . $key . $keySuffix;
87
88 // Do not show current date when field not required (see selectDate() method)
89 if (!$required && $this->isEmptyValue($fieldInfos, $value)) {
90 $value = '-1';
91 }
92
93 // TODO Must also support $moreAttrib and $moreCss ?
94 return self::$form->selectDate($value, $htmlName, 1, 1, $required, '', 1, 1, 0, 1, '', '', '', 1, '', '', 'tzuserrel');
95 }
96
109 public function printOutputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
110 {
111 return !$this->isEmptyValue($fieldInfos, $value) ? dol_print_date($value, 'dayhour', 'tzuserrel') : '';
112 }
113
123 public function getInputCss($fieldInfos, $moreCss = '', $defaultCss = '')
124 {
125 return parent::getInputCss($fieldInfos, $moreCss, $defaultCss ? $defaultCss : 'minwidth200imp');
126 }
127
137 public function verifyFieldValue($fieldInfos, $key, $value)
138 {
139 $result = parent::verifyFieldValue($fieldInfos, $key, $value);
140 if ($result && !$this->isEmptyValue($fieldInfos, $value)) {
141 if (!self::$validator->isTimestamp($value)) {
142 return false;
143 }
144
145 $result = true;
146 }
147
148 return $result;
149 }
150
161 public function verifyPostFieldValue($fieldInfos, $key, $keyPrefix = '', $keySuffix = '')
162 {
163 return parent::verifyPostFieldValue($fieldInfos, $key, $keyPrefix, $keySuffix);
164 }
165
177 public function getPostFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
178 {
179 $htmlName = $keyPrefix . $key . $keySuffix;
180
181 if (GETPOSTISSET($htmlName . 'hour') || GETPOSTISSET($htmlName . 'min') || GETPOSTISSET($htmlName . 'sec') || GETPOSTISSET($htmlName . 'month') || GETPOSTISSET($htmlName . 'day') || GETPOSTISSET($htmlName . 'year')) {
182 $value = dol_mktime(GETPOSTINT($htmlName . 'hour'), GETPOSTINT($htmlName . 'min'), GETPOSTINT($htmlName . 'sec'), GETPOSTINT($htmlName . 'month'), GETPOSTINT($htmlName . 'day'), GETPOSTINT($htmlName . 'year'), 'tzuserrel'); // for date without hour, we use gmt
183 } else {
184 $value = $defaultValue;
185 }
186
187 return $value;
188 }
189
201 public function getPostSearchFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
202 {
203 $htmlName = $keyPrefix . $key . $keySuffix;
204
205 if (GETPOSTISSET($htmlName . '_startmonth') || GETPOSTISSET($htmlName . '_startday') || GETPOSTISSET($htmlName . '_startyear')) {
206 $start = dol_mktime(0, 0, 0, GETPOSTINT($htmlName . '_startmonth'), GETPOSTINT($htmlName . '_startday'), GETPOSTINT($htmlName . '_startyear'), 'tzuserrel');
207 } else {
208 $start = is_array($defaultValue) && isset($defaultValue['start']) ? $defaultValue['start'] : '';
209 }
210
211 if (GETPOSTISSET($htmlName . '_endmonth') || GETPOSTISSET($htmlName . '_endday') || GETPOSTISSET($htmlName . '_endyear')) {
212 $end = dol_mktime(23, 59, 59, GETPOSTINT($htmlName . '_endmonth'), GETPOSTINT($htmlName . '_endday'), GETPOSTINT($htmlName . '_endyear'), 'tzuserrel');
213 } else {
214 $end = is_array($defaultValue) && isset($defaultValue['end']) ? $defaultValue['end'] : '';
215 }
216
217 return array(
218 'start' => $start,
219 'end' => $end,
220 );
221 }
222
232 public function sqlFilterSearchField($fieldInfos, $key, $value)
233 {
234 if (!$this->isEmptyValue($fieldInfos, $value)) {
235 $alias = $fieldInfos->sqlAlias ?? 't.';
236 $field = $this->db->sanitize($alias . ($fieldInfos->nameInTable ?? $key));
237 $sql = '';
238
239 if (is_array($value)) {
240 $hasStart = !is_null($value['start']) && $value['start'] !== '';
241 $hasEnd = !is_null($value['start']) && $value['start'] !== '';
242 if ($hasStart && $hasEnd) {
243 $sql = " AND (" . $this->db->sanitize($field) . " BETWEEN '" . $this->db->idate($value['start']) . "' AND '" . $this->db->idate($value['end']) . "')";
244 } elseif ($hasStart) {
245 $sql = " AND " . $this->db->sanitize($field) . " >= '" . $this->db->idate($value['start']) . "'";
246 } elseif ($hasEnd) {
247 $sql = " AND " . $this->db->sanitize($field) . " <= '" . $this->db->idate($value['end']) . "'";
248 }
249 } elseif (is_numeric($value)) {
250 $sql = " AND " . $this->db->sanitize($field) . " = '" . $this->db->idate($value) . "'";
251 }
252
253 return $sql;
254 }
255
256 return '';
257 }
258}
Class to common field.
isEmptyValue($fieldInfos, $value, $emptyValues=null)
Check if the value is deemed as empty.
Class to timestamp field.
getInputCss($fieldInfos, $moreCss='', $defaultCss='')
Get input CSS.
getPostSearchFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get search field value from GET/POST.
printOutputField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to show a field into a page.
getPostFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get field value from GET/POST.
verifyPostFieldValue($fieldInfos, $key, $keyPrefix='', $keySuffix='')
Verify if the field value from GET/POST is valid.
printInputSearchField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to put an input search field into a page.
sqlFilterSearchField($fieldInfos, $key, $value)
Get sql filter for search field.
verifyFieldValue($fieldInfos, $key, $value)
Verify if the field value is valid.
printInputField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to put an input field into a page.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).