dolibarr 23.0.3
booleanfield.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('', '-1', -1);
36
37
50 public function printInputSearchField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
51 {
52 $htmlName = $keyPrefix . $key . $keySuffix;
53
54 return self::$form->selectyesno($htmlName, $value, 1, false, 1, 1, 'width75 yesno');
55 }
56
69 public function printInputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
70 {
71 $moreCss = $this->getInputCss($fieldInfos, $moreCss);
72 $moreAttrib = trim((string) $moreAttrib);
73 if (empty($moreAttrib)) $moreAttrib = ' ' . $moreAttrib;
74 $checked = empty($value) ? '' : ' checked="checked"';
75 $htmlName = $keyPrefix . $key . $keySuffix;
76
77 $out = self::$form->inputType('checkbox', $htmlName, '1', $htmlName, $moreCss, $moreAttrib . $checked);
78 $out .= self::$form->inputType('hidden', $htmlName . '_boolean', '1'); // A hidden field ending with "_boolean" that is always set to 1.
79
80 return $out;
81 }
82
95 public function printOutputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
96 {
97 if (getDolGlobalInt('MAIN_OPTIMIZEFORTEXTBROWSER') < 2) {
98 $moreCss = $this->getInputCss($fieldInfos, $moreCss);
99 $moreAttrib = trim((string) $moreAttrib);
100 if (empty($moreAttrib)) $moreAttrib = ' ' . $moreAttrib;
101 $checked = empty($value) ? '' : ' checked="checked"';
102 $value = self::$form->inputType('checkbox', '', '1', '', $moreCss, $checked . $moreAttrib . ' readonly disabled');
103 } else {
104 $value = yn($value ? 1 : 0);
105 }
106
107 return $value;
108 }
109
119 public function getInputCss($fieldInfos, $moreCss = '', $defaultCss = '')
120 {
121 if (empty($moreCss)) $moreCss = $defaultCss;
122 $moreCss = trim((string) $moreCss);
123
124 return empty($moreCss) ? '' : ' ' . $moreCss;
125 }
126
136 public function verifyFieldValue($fieldInfos, $key, $value)
137 {
138 $result = parent::verifyFieldValue($fieldInfos, $key, $value);
139 if ($result && !$this->isEmptyValue($fieldInfos, $value)) {
140 if (!self::$validator->isBool($value)) {
141 return false;
142 }
143
144 $result = true;
145 }
146
147 return $result;
148 }
149
160 public function verifyPostFieldValue($fieldInfos, $key, $keyPrefix = '', $keySuffix = '')
161 {
162 return parent::verifyPostFieldValue($fieldInfos, $key, $keyPrefix, $keySuffix);
163 }
164
176 public function getPostFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
177 {
178 $htmlName = $keyPrefix . $key . $keySuffix;
179
180 // We test on a hidden field named "..._boolean" that is always set to 1 if param is in form so
181 // when nothing is provided we can make a difference between noparam in the form and param was set to nothing.
182 if (!GETPOSTISSET($htmlName . "_boolean")) {
183 $value = $defaultValue;
184 } elseif (GETPOSTISSET($htmlName)) {
185 $value = GETPOSTINT($htmlName) == 1 ? 1 : 0;
186 } else {
187 $value = $defaultValue;
188 }
189
190 return $value;
191 }
192
204 public function getPostSearchFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
205 {
206 $htmlName = $keyPrefix . $key . $keySuffix;
207
208 if (GETPOSTISSET($htmlName)) {
209 $value = GETPOSTINT($htmlName);
210 } else {
211 $value = $defaultValue;
212 }
213
214 return $value;
215 }
216
226 public function sqlFilterSearchField($fieldInfos, $key, $value)
227 {
228 if (!$this->isEmptyValue($fieldInfos, $value)) {
229 $alias = $fieldInfos->sqlAlias ?? 't.';
230 $field = $this->db->sanitize($alias . ($fieldInfos->nameInTable ?? $key));
231
232 $sql = " AND (" . $field . " = '" . $this->db->escape($value) . "'";
233 if ($value == '0') {
234 $sql .= " OR " . $field . " IS NULL";
235 }
236 $sql .= ")";
237
238 return $sql;
239 }
240
241 return '';
242 }
243}
Class to boolean field.
verifyPostFieldValue($fieldInfos, $key, $keyPrefix='', $keySuffix='')
Verify if the field value from GET/POST is valid.
getInputCss($fieldInfos, $moreCss='', $defaultCss='')
Get input CSS.
getPostFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get field value from GET/POST.
sqlFilterSearchField($fieldInfos, $key, $value)
Get sql filter for search field.
getPostSearchFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get search field value from GET/POST.
printInputField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to put an input field into a page.
printOutputField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to show a field into a page.
verifyFieldValue($fieldInfos, $key, $value)
Verify if the field value is valid.
printInputSearchField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to put an input search field into a page.
Class to common field.
isEmptyValue($fieldInfos, $value, $emptyValues=null)
Check if the value is deemed as empty.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
yn($yesno, $format=1, $color=0)
Return yes or no in current language.