dolibarr 23.0.3
datefield.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', 0, 0, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans("From"));
64 $out .= '</div><div class="nowrap">';
65 $out .= self::$form->selectDate($prefill['end'], $htmlName . '_end', 0, 0, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans("to"));
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 // Do not whow "add now link" (eg for birthday date)
94 $addnowlink = 1;
95 if (in_array($fieldInfos->key, array('birth'))) {
96 $addnowlink = 0;
97 }
98
99 // TODO Must also support $moreAttrib and $moreCss ?
100 return self::$form->selectDate($value, $htmlName, 0, 0, $required, '', 1, $addnowlink, 0, 1);
101 }
102
115 public function printOutputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
116 {
117 // We suppose dates without time are always gmt (storage of course + output)
118 return !$this->isEmptyValue($fieldInfos, $value) ? dol_print_date($value, 'day') : '';
119 }
120
130 public function getInputCss($fieldInfos, $moreCss = '', $defaultCss = '')
131 {
132 return parent::getInputCss($fieldInfos, $moreCss, $defaultCss ? $defaultCss : 'minwidth100imp');
133 }
134
144 public function verifyFieldValue($fieldInfos, $key, $value)
145 {
146 $result = parent::verifyFieldValue($fieldInfos, $key, $value);
147 if ($result && !$this->isEmptyValue($fieldInfos, $value)) {
148 // FIXME this check is not valid
149 /*if (!self::$validator->isTimestamp($value)) {
150 return false;
151 }*/
152
153 $result = true;
154 }
155
156 return $result;
157 }
158
169 public function verifyPostFieldValue($fieldInfos, $key, $keyPrefix = '', $keySuffix = '')
170 {
171 return parent::verifyPostFieldValue($fieldInfos, $key, $keyPrefix, $keySuffix);
172 }
173
185 public function getPostFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
186 {
187 $htmlName = $keyPrefix . $key . $keySuffix;
188
189 if (GETPOSTISSET($htmlName . 'month') || GETPOSTISSET($htmlName . 'day') || GETPOSTISSET($htmlName . 'year')) {
190 $value = dol_mktime(12, 0, 0, GETPOSTINT($htmlName . 'month'), GETPOSTINT($htmlName . 'day'), GETPOSTINT($htmlName . 'year')); // for date without hour, we use gmt
191 } else {
192 $value = $defaultValue;
193 }
194
195 return $value;
196 }
197
209 public function getPostSearchFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
210 {
211 $htmlName = $keyPrefix . $key . $keySuffix;
212
213 if (GETPOSTISSET($htmlName . '_startmonth') || GETPOSTISSET($htmlName . '_startday') || GETPOSTISSET($htmlName . '_startyear')) {
214 $start = dol_mktime(0, 0, 0, GETPOSTINT($htmlName . '_startmonth'), GETPOSTINT($htmlName . '_startday'), GETPOSTINT($htmlName . '_startyear'));
215 } else {
216 $start = is_array($defaultValue) && isset($defaultValue['start']) ? $defaultValue['start'] : '';
217 }
218
219 if (GETPOSTISSET($htmlName . '_endmonth') || GETPOSTISSET($htmlName . '_endday') || GETPOSTISSET($htmlName . '_endyear')) {
220 $end = dol_mktime(23, 59, 59, GETPOSTINT($htmlName . '_endmonth'), GETPOSTINT($htmlName . '_endday'), GETPOSTINT($htmlName . '_endyear'));
221 } else {
222 $end = is_array($defaultValue) && isset($defaultValue['end']) ? $defaultValue['end'] : '';
223 }
224
225 return array(
226 'start' => $start,
227 'end' => $end,
228 );
229 }
230
240 public function sqlFilterSearchField($fieldInfos, $key, $value)
241 {
242 if (!$this->isEmptyValue($fieldInfos, $value)) {
243 $alias = $fieldInfos->sqlAlias ?? 't.';
244 $field = $this->db->sanitize($alias . ($fieldInfos->nameInTable ?? $key));
245 $sql = '';
246
247 if (is_array($value)) {
248 $hasStart = !is_null($value['start']) && $value['start'] !== '';
249 $hasEnd = !is_null($value['start']) && $value['start'] !== '';
250 if ($hasStart && $hasEnd) {
251 $sql = " AND (" . $field . " BETWEEN '" . $this->db->idate($value['start']) . "' AND '" . $this->db->idate($value['end']) . "')";
252 } elseif ($hasStart) {
253 $sql = " AND " . $field . " >= '" . $this->db->idate($value['start']) . "'";
254 } elseif ($hasEnd) {
255 $sql = " AND " . $field . " <= '" . $this->db->idate($value['end']) . "'";
256 }
257 } elseif (is_numeric($value)) {
258 include_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
259 $value = dol_get_first_hour($value);
260 $sql = " AND " . $field . " = '" . $this->db->idate($value) . "'";
261 }
262
263 return $sql;
264 }
265
266 return '';
267 }
268}
Class to common field.
isEmptyValue($fieldInfos, $value, $emptyValues=null)
Check if the value is deemed as empty.
Class to date field.
sqlFilterSearchField($fieldInfos, $key, $value)
Get sql filter for search field.
printInputSearchField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to put an input search field into a page.
getPostFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get field value from GET/POST.
printOutputField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to show a field into a page.
printInputField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to put an input field into a page.
verifyPostFieldValue($fieldInfos, $key, $keyPrefix='', $keySuffix='')
Verify if the field value from GET/POST is valid.
getInputCss($fieldInfos, $moreCss='', $defaultCss='')
Get input CSS.
getPostSearchFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get search field value from GET/POST.
verifyFieldValue($fieldInfos, $key, $value)
Verify if the field value is valid.
dol_get_first_hour($date, $gm='tzserver')
Return GMT time for first hour of a given GMT date (it removes hours, min and second part)
Definition date.lib.php:663
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).