dolibarr 24.0.0-beta
checkboxfield.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2025 Open-Dsi <support@open-dsi.fr>
3 * Copyright (C) 2026 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25require_once DOL_DOCUMENT_ROOT . '/core/class/fields/commonselectfield.class.php';
26
27
32{
36 public $emptyValues = array(array(), '');
37
38
51 public function printInputSearchField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
52 {
53 return $this->printInputField($fieldInfos, $key, $value, $keyPrefix, $keySuffix, $moreCss, $moreAttrib);
54 }
55
68 public function printInputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
69 {
70 global $conf;
71
72 $moreCss = $this->getInputCss($fieldInfos, $moreCss);
73 $moreAttrib = trim((string) $moreAttrib);
74 if (empty($moreAttrib)) {
75 $moreAttrib = ' ' . $moreAttrib;
76 }
77 $htmlName = $keyPrefix . $key . $keySuffix;
78 $values = $this->isEmptyValue($fieldInfos, $value) ? array() : (is_string($value) ? explode(',', $value) : (is_array($value) ? $value : array($value)));
79
80 $optionsList = array();
81 $options = $this->getOptions($fieldInfos, $key);
82
83 return self::$form->multiselectarray($htmlName, $options, $values, 0, 0, $moreCss, 0, 0, $moreAttrib, '', '', (int) (!empty($conf->use_javascript_ajax) && !getDolGlobalString('MAIN_EXTRAFIELDS_DISABLE_SELECT2')));
84 }
85
98 public function printOutputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
99 {
100 global $langs;
101 $values = $this->isEmptyValue($fieldInfos, $value) ? array() : (is_string($value) ? explode(',', $value) : (is_array($value) ? $value : array($value)));
102
103 $out = '';
104 if (!$this->isEmptyValue($fieldInfos, $values)) {
105 $options = $this->getOptions($fieldInfos, $key);
106 $toPrint = array();
107 foreach ($values as $val) {
108 $valueToPrint = null;
109 foreach ($options as $optionKey => $optionInfos) {
110 if (((string) $optionKey) == $val) {
111 $valueToPrint = $optionInfos['label'];
112 break;
113 }
114 }
115 if (!isset($valueToPrint)) {
116 $langs->load("errors");
117 $valueToPrint = $langs->trans('ErrorRecordNotFound') . ' ( ' . $val . ' )';
118 }
119 $toPrint[] = $valueToPrint;
120 }
121 $out = self::$form->outputMultiValues($toPrint);
122 }
123
124 return $out;
125 }
126
136 public function getInputCss($fieldInfos, $moreCss = '', $defaultCss = '')
137 {
138 return parent::getInputCss($fieldInfos, $moreCss, $defaultCss ? $defaultCss : 'minwidth400');
139 }
140
150 public function verifyFieldValue($fieldInfos, $key, $value)
151 {
152 global $langs;
153 $values = $this->isEmptyValue($fieldInfos, $value) ? array() : (is_string($value) ? explode(',', $value) : (is_array($value) ? $value : array($value)));
154
155 $result = parent::verifyFieldValue($fieldInfos, $key, $values);
156 if ($result && !$this->isEmptyValue($fieldInfos, $values)) {
157 $options = $this->getOptions($fieldInfos, $key);
158 foreach ($values as $val) {
159 $newVal = trim((string) $val);
160 if (!isset($options[$newVal])) {
161 self::$validator->error = $langs->trans('RequireValidValue');
162 return false;
163 }
164 }
165
166 $result = true;
167 }
168
169 return $result;
170 }
171
182 public function verifyPostFieldValue($fieldInfos, $key, $keyPrefix = '', $keySuffix = '')
183 {
184 $htmlName = $keyPrefix . $key . $keySuffix;
185 $values = GETPOST($htmlName, 'array');
186
187 return $this->verifyFieldValue($fieldInfos, $key, $values);
188 }
189
201 public function getPostFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
202 {
203 $htmlName = $keyPrefix . $key . $keySuffix;
204
205 if (GETPOSTISSET($htmlName)) {
206 $values = GETPOST($htmlName, 'array');
207 if (is_array($values)) {
208 $values = implode(',', $values);
209 }
210 } else {
211 $values = $defaultValue;
212 }
213
214 return $values;
215 }
216
228 public function getPostSearchFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
229 {
230 $htmlName = $keyPrefix . $key . $keySuffix;
231
232 if (GETPOSTISSET($htmlName)) {
233 $values = GETPOST($htmlName, 'array');
234 } else {
235 $values = $defaultValue;
236 }
237
238 return $values;
239 }
240
250 public function sqlFilterSearchField($fieldInfos, $key, $value)
251 {
252 if (!$this->isEmptyValue($fieldInfos, $value) && is_array($value)) {
253 $alias = $fieldInfos->sqlAlias ?? 't.';
254 $field = $this->db->sanitize($alias . ($fieldInfos->nameInTable ?? $key));
255
256 $sanitizedSqlIn = "'" . implode("','", array_map(array($this->db, 'escape'), $value)) . "'";
257 $sqlPartialCond = " AND " . $field . " IN (" . $sanitizedSqlIn . ")";
258 return $sqlPartialCond;
259 }
260
261 return '';
262 }
263
273 public function getOptions($fieldInfos, $key, $addEmptyValue = false, $reload = false)
274 {
275 return parent::getOptions($fieldInfos, $key, $addEmptyValue, $reload);
276 }
277}
Class to checkbox field (multiselect)
getOptions($fieldInfos, $key, $addEmptyValue=false, $reload=false)
Get list of options.
printInputField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to put an input field into a page.
printInputSearchField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to put an input search field into a page.
verifyPostFieldValue($fieldInfos, $key, $keyPrefix='', $keySuffix='')
Verify if the field value from GET/POST is valid.
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.
getPostFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get field value from GET/POST.
sqlFilterSearchField($fieldInfos, $key, $value)
Get sql filter for search field.
printOutputField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to show a field into a page.
getInputCss($fieldInfos, $moreCss='', $defaultCss='')
Get input CSS.
isEmptyValue($fieldInfos, $value, $emptyValues=null)
Check if the value is deemed as empty.
Class to common select field.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.