dolibarr 24.0.0-beta
chkbxlstfield.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/commonsellistfield.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 global $conf;
54
55 $moreCss = $this->getInputCss($fieldInfos, $moreCss);
56 $moreAttrib = trim((string) $moreAttrib);
57 if (empty($moreAttrib)) {
58 $moreAttrib = ' ' . $moreAttrib;
59 }
60 $htmlName = $keyPrefix . $key . $keySuffix;
61 $values = $this->isEmptyValue($fieldInfos, $value) ? array() : (is_string($value) ? explode(',', $value) : (is_array($value) ? $value : array($value)));
62
63 $optionsList = array();
64 $options = $this->getOptions($fieldInfos, $key);
65 foreach ($options as $optionKey => $optionInfos) {
66 $options[$optionKey] = $optionInfos['label'];
67 }
68
69 return self::$form->multiselectarray($htmlName, $optionsList, $values, 0, 0, $moreCss, 0, 0, $moreAttrib, '', '', (int) (!empty($conf->use_javascript_ajax) && !getDolGlobalString('MAIN_EXTRAFIELDS_DISABLE_SELECT2')));
70 }
71
84 public function printInputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
85 {
86 global $conf;
87
88 $moreCss = $this->getInputCss($fieldInfos, $moreCss);
89 $moreAttrib = trim((string) $moreAttrib);
90 if (empty($moreAttrib)) {
91 $moreAttrib = ' ' . $moreAttrib;
92 }
93 $htmlName = $keyPrefix . $key . $keySuffix;
94 $values = $this->isEmptyValue($fieldInfos, $value) ? array() : (is_string($value) ? explode(',', $value) : (is_array($value) ? $value : array($value)));
95
96 $options = $this->getOptions($fieldInfos, $key);
97
98 return self::$form->multiselectarray($htmlName, $options, $values, 0, 0, $moreCss, 0, 0, $moreAttrib, '', '', (int) (!empty($conf->use_javascript_ajax) && !getDolGlobalString('MAIN_EXTRAFIELDS_DISABLE_SELECT2')));
99 }
100
113 public function printOutputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
114 {
115 $values = $this->isEmptyValue($fieldInfos, $value) ? array() : (is_string($value) ? explode(',', $value) : (is_array($value) ? $value : array($value)));
116
117 $out = '';
118 if (!$this->isEmptyValue($fieldInfos, $values)) {
119 $options = $this->getOptions($fieldInfos, $key, false, false, $values);
120 $optionParams = $this->getOptionsParams($fieldInfos->options);
121 $isCategory = $optionParams['tableName'] == 'categorie' && !empty($optionParams['categoryType']);
122
123 $toPrint = array();
124 foreach ($values as $val) {
125 $valueToPrint = '';
126 $colorToPrint = 'bbb';
127 if (isset($options[$val])) {
128 $valueToPrint = $options[$val]['label'];
129
130 if ($isCategory) {
131 require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
132 $c = new Categorie($this->db);
133 $c->fetch($val);
134 $colorToPrint = $c->color ? $c->color : 'bbb';
135 $valueToPrint = img_object('', 'category') . ' ' . $valueToPrint;
136 }
137 } else {
138 $valueToPrint = $val;
139 }
140 $toPrint[] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #' . $colorToPrint . ';">' . $valueToPrint . '</li>';
141 }
142 if (!empty($toPrint)) {
143 $out = '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode('', $toPrint) . '</ul></div>';
144 }
145 }
146
147 return $out;
148 }
149
159 public function getInputCss($fieldInfos, $moreCss = '', $defaultCss = '')
160 {
161 return parent::getInputCss($fieldInfos, $moreCss, $defaultCss ? $defaultCss : 'minwidth400');
162 }
163
173 public function verifyFieldValue($fieldInfos, $key, $value)
174 {
175 $values = $this->isEmptyValue($fieldInfos, $value) ? array() : (is_string($value) ? explode(',', $value) : (is_array($value) ? $value : array($value)));
176
177 $result = parent::verifyFieldValue($fieldInfos, $key, $values);
178 if ($result && !$this->isEmptyValue($fieldInfos, $values)) {
179 $optionParams = $this->getOptionsParams($fieldInfos->options);
180 if (!self::$validator->isInDb($values, $optionParams['tableName'], $optionParams['keyField'])) {
181 return false;
182 }
183
184 $result = true;
185 }
186
187 return $result;
188 }
189
200 public function verifyPostFieldValue($fieldInfos, $key, $keyPrefix = '', $keySuffix = '')
201 {
202 $htmlName = $keyPrefix . $key . $keySuffix;
203 $values = GETPOST($htmlName, 'array');
204
205 return $this->verifyFieldValue($fieldInfos, $key, $values);
206 }
207
219 public function getPostFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
220 {
221 $htmlName = $keyPrefix . $key . $keySuffix;
222
223 if (GETPOSTISSET($htmlName)) {
224 $values = GETPOST($htmlName, 'array');
225 if (is_array($values)) {
226 $values = implode(',', $values);
227 }
228 } else {
229 $values = $defaultValue;
230 }
231
232 return $values;
233 }
234
246 public function getPostSearchFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
247 {
248 $htmlName = $keyPrefix . $key . $keySuffix;
249
250 if (GETPOSTISSET($htmlName)) {
251 $value = GETPOST($htmlName, 'array');
252 } else {
253 $value = $defaultValue;
254 }
255
256 return $value;
257 }
258
268 public function sqlFilterSearchField($fieldInfos, $key, $value)
269 {
270 if (!empty($value) && is_array($value)) {
271 $alias = $fieldInfos->sqlAlias ?? 't.';
272 $field = $this->db->sanitize($alias . ($fieldInfos->nameInTable ?? $key));
273
274 $sanitizedSqlIn = "'" . implode("','", array_map(array($this->db, 'escape'), $value)) . "'";
275 $sqlPartialCond = " AND " . $field . " IN (" . $sanitizedSqlIn . ")";
276 return $sqlPartialCond;
277 }
278
279 return '';
280 }
281
292 public function getOptions($fieldInfos, $key, $addEmptyValue = false, $reload = false, $selectedValues = array())
293 {
294 return parent::getOptions($fieldInfos, $key, $addEmptyValue, $reload, $selectedValues);
295 }
296}
$c
Definition line.php:334
Class to manage categories.
Class to chkbxlst field (multiselect)
verifyPostFieldValue($fieldInfos, $key, $keyPrefix='', $keySuffix='')
Verify if the field value from GET/POST is valid.
getPostFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get field value from GET/POST.
printInputField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to put an input field into a page.
verifyFieldValue($fieldInfos, $key, $value)
Verify if the field value is valid.
sqlFilterSearchField($fieldInfos, $key, $value)
Get sql filter for search field.
getPostSearchFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get search field value from GET/POST.
getInputCss($fieldInfos, $moreCss='', $defaultCss='')
Get input CSS.
printInputSearchField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to put an input search field into a page.
getOptions($fieldInfos, $key, $addEmptyValue=false, $reload=false, $selectedValues=array())
Get list of options.
printOutputField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to show a field into a page.
isEmptyValue($fieldInfos, $value, $emptyValues=null)
Check if the value is deemed as empty.
Class to common sellist field.
getOptionsParams($options)
Get all parameters in the options.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
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.