dolibarr 23.0.3
checkboxfield.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/commonselectfield.class.php';
25
26
31{
35 public $emptyValues = array(array(), '');
36
37
50 public function printInputSearchField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
51 {
52 return $this->printInputField($fieldInfos, $key, $value, $keyPrefix, $keySuffix, $moreCss, $moreAttrib);
53 }
54
67 public function printInputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
68 {
69 global $conf;
70
71 $moreCss = $this->getInputCss($fieldInfos, $moreCss);
72 $moreAttrib = trim((string) $moreAttrib);
73 if (empty($moreAttrib)) $moreAttrib = ' ' . $moreAttrib;
74 $htmlName = $keyPrefix . $key . $keySuffix;
75 $values = $this->isEmptyValue($fieldInfos, $value) ? array() : (is_string($value) ? explode(',', $value) : (is_array($value) ? $value: array($value)));
76
77 $optionsList = array();
78 $options = $this->getOptions($fieldInfos, $key);
79
80 return self::$form->multiselectarray($htmlName, $options, $values, 0, 0, $moreCss, 0, 0, $moreAttrib, '', '', (int) (!empty($conf->use_javascript_ajax) && !getDolGlobalString('MAIN_EXTRAFIELDS_DISABLE_SELECT2')));
81 }
82
95 public function printOutputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
96 {
97 global $langs;
98 $values = $this->isEmptyValue($fieldInfos, $value) ? array() : (is_string($value) ? explode(',', $value) : (is_array($value) ? $value: array($value)));
99
100 $out = '';
101 if (!$this->isEmptyValue($fieldInfos, $values)) {
102 $options = $this->getOptions($fieldInfos, $key);
103 $toPrint = array();
104 foreach ($values as $val) {
105 $valueToPrint = null;
106 foreach ($options as $optionKey => $optionInfos) {
107 if (((string) $optionKey) == $val) {
108 $valueToPrint = $optionInfos['label'];
109 break;
110 }
111 }
112 if (!isset($valueToPrint)) {
113 $langs->load("errors");
114 $valueToPrint = $langs->trans('ErrorRecordNotFound') . ' ( ' . $val . ' )';
115 }
116 $toPrint[] = $valueToPrint;
117 }
118 $out = self::$form->outputMultiValues($toPrint);
119 }
120
121 return $out;
122 }
123
133 public function getInputCss($fieldInfos, $moreCss = '', $defaultCss = '')
134 {
135 return parent::getInputCss($fieldInfos, $moreCss, $defaultCss ? $defaultCss : 'minwidth400');
136 }
137
147 public function verifyFieldValue($fieldInfos, $key, $value)
148 {
149 global $langs;
150 $values = $this->isEmptyValue($fieldInfos, $value) ? array() : (is_string($value) ? explode(',', $value) : (is_array($value) ? $value: array($value)));
151
152 $result = parent::verifyFieldValue($fieldInfos, $key, $values);
153 if ($result && !$this->isEmptyValue($fieldInfos, $values)) {
154 $options = $this->getOptions($fieldInfos, $key);
155 foreach ($values as $val) {
156 $newVal = trim((string) $val);
157 if (!isset($options[$newVal])) {
158 self::$validator->error = $langs->trans('RequireValidValue');
159 return false;
160 }
161 }
162
163 $result = true;
164 }
165
166 return $result;
167 }
168
179 public function verifyPostFieldValue($fieldInfos, $key, $keyPrefix = '', $keySuffix = '')
180 {
181 $htmlName = $keyPrefix . $key . $keySuffix;
182 $values = GETPOST($htmlName, 'array');
183
184 return $this->verifyFieldValue($fieldInfos, $key, $values);
185 }
186
198 public function getPostFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
199 {
200 $htmlName = $keyPrefix . $key . $keySuffix;
201
202 if (GETPOSTISSET($htmlName)) {
203 $values = GETPOST($htmlName, 'array');
204 if (is_array($values)) $values = implode(',', $values);
205 } else {
206 $values = $defaultValue;
207 }
208
209 return $values;
210 }
211
223 public function getPostSearchFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
224 {
225 $htmlName = $keyPrefix . $key . $keySuffix;
226
227 if (GETPOSTISSET($htmlName)) {
228 $values = GETPOST($htmlName, 'array');
229 } else {
230 $values = $defaultValue;
231 }
232
233 return $values;
234 }
235
245 public function sqlFilterSearchField($fieldInfos, $key, $value)
246 {
247 if (!$this->isEmptyValue($fieldInfos, $value) && is_array($value)) {
248 $alias = $fieldInfos->sqlAlias ?? 't.';
249 $field = $this->db->sanitize($alias . ($fieldInfos->nameInTable ?? $key));
250
251 $tmp = "'" . implode("','", array_map(array($this->db, 'escape'), $value)) . "'";
252 return " AND " . $field . " IN (" . $this->db->sanitize($tmp, 1) . ")";
253 }
254
255 return '';
256 }
257
267 public function getOptions($fieldInfos, $key, $addEmptyValue = false, $reload = false)
268 {
269 return parent::getOptions($fieldInfos, $key, $addEmptyValue, $reload);
270 }
271}
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.
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.