dolibarr 23.0.3
chkbxlstfield.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/commonsellistfield.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 global $conf;
53
54 $moreCss = $this->getInputCss($fieldInfos, $moreCss);
55 $moreAttrib = trim((string) $moreAttrib);
56 if (empty($moreAttrib)) $moreAttrib = ' ' . $moreAttrib;
57 $htmlName = $keyPrefix . $key . $keySuffix;
58 $values = $this->isEmptyValue($fieldInfos, $value) ? array() : (is_string($value) ? explode(',', $value) : (is_array($value) ? $value: array($value)));
59
60 $optionsList = array();
61 $options = $this->getOptions($fieldInfos, $key);
62 foreach ($options as $optionKey => $optionInfos) {
63 $options[$optionKey] = $optionInfos['label'];
64 }
65
66 return self::$form->multiselectarray($htmlName, $optionsList, $values, 0, 0, $moreCss, 0, 0, $moreAttrib, '', '', (int) (!empty($conf->use_javascript_ajax) && !getDolGlobalString('MAIN_EXTRAFIELDS_DISABLE_SELECT2')));
67 }
68
81 public function printInputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
82 {
83 global $conf;
84
85 $moreCss = $this->getInputCss($fieldInfos, $moreCss);
86 $moreAttrib = trim((string) $moreAttrib);
87 if (empty($moreAttrib)) $moreAttrib = ' ' . $moreAttrib;
88 $htmlName = $keyPrefix . $key . $keySuffix;
89 $values = $this->isEmptyValue($fieldInfos, $value) ? array() : (is_string($value) ? explode(',', $value) : (is_array($value) ? $value: array($value)));
90
91 $options = $this->getOptions($fieldInfos, $key);
92
93 return self::$form->multiselectarray($htmlName, $options, $values, 0, 0, $moreCss, 0, 0, $moreAttrib, '', '', (int) (!empty($conf->use_javascript_ajax) && !getDolGlobalString('MAIN_EXTRAFIELDS_DISABLE_SELECT2')));
94 }
95
108 public function printOutputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
109 {
110 $values = $this->isEmptyValue($fieldInfos, $value) ? array() : (is_string($value) ? explode(',', $value) : (is_array($value) ? $value: array($value)));
111
112 $out = '';
113 if (!$this->isEmptyValue($fieldInfos, $values)) {
114 $options = $this->getOptions($fieldInfos, $key, false, false, $values);
115 $optionParams = $this->getOptionsParams($fieldInfos->options);
116 $isCategory = $optionParams['tableName'] == 'categorie' && !empty($optionParams['categoryType']);
117
118 $toPrint = array();
119 foreach ($values as $val) {
120 $valueToPrint = '';
121 $colorToPrint = 'bbb';
122 if (isset($options[$val])) {
123 $valueToPrint = $options[$val]['label'];
124
125 if ($isCategory) {
126 require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
127 $c = new Categorie($this->db);
128 $c->fetch($val);
129 $colorToPrint = $c->color ? $c->color : 'bbb';
130 $valueToPrint = img_object('', 'category') . ' ' . $valueToPrint;
131 }
132 } else {
133 $valueToPrint = $val;
134 }
135 $toPrint[] = '<li class="select2-search-choice-dolibarr noborderoncategories" style="background: #' . $colorToPrint . ';">' . $valueToPrint . '</li>';
136 }
137 if (!empty($toPrint)) {
138 $out = '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr">' . implode('', $toPrint) . '</ul></div>';
139 }
140 }
141
142 return $out;
143 }
144
154 public function getInputCss($fieldInfos, $moreCss = '', $defaultCss = '')
155 {
156 return parent::getInputCss($fieldInfos, $moreCss, $defaultCss ? $defaultCss : 'minwidth400');
157 }
158
168 public function verifyFieldValue($fieldInfos, $key, $value)
169 {
170 $values = $this->isEmptyValue($fieldInfos, $value) ? array() : (is_string($value) ? explode(',', $value) : (is_array($value) ? $value: array($value)));
171
172 $result = parent::verifyFieldValue($fieldInfos, $key, $values);
173 if ($result && !$this->isEmptyValue($fieldInfos, $values)) {
174 $optionParams = $this->getOptionsParams($fieldInfos->options);
175 if (!self::$validator->isInDb($values, $optionParams['tableName'], $optionParams['keyField'])) {
176 return false;
177 }
178
179 $result = true;
180 }
181
182 return $result;
183 }
184
195 public function verifyPostFieldValue($fieldInfos, $key, $keyPrefix = '', $keySuffix = '')
196 {
197 $htmlName = $keyPrefix . $key . $keySuffix;
198 $values = GETPOST($htmlName, 'array');
199
200 return $this->verifyFieldValue($fieldInfos, $key, $values);
201 }
202
214 public function getPostFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
215 {
216 $htmlName = $keyPrefix . $key . $keySuffix;
217
218 if (GETPOSTISSET($htmlName)) {
219 $values = GETPOST($htmlName, 'array');
220 if (is_array($values)) $values = implode(',', $values);
221 } else {
222 $values = $defaultValue;
223 }
224
225 return $values;
226 }
227
239 public function getPostSearchFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
240 {
241 $htmlName = $keyPrefix . $key . $keySuffix;
242
243 if (GETPOSTISSET($htmlName)) {
244 $value = GETPOST($htmlName, 'array');
245 } else {
246 $value = $defaultValue;
247 }
248
249 return $value;
250 }
251
261 public function sqlFilterSearchField($fieldInfos, $key, $value)
262 {
263 if (!empty($value) && is_array($value)) {
264 $alias = $fieldInfos->sqlAlias ?? 't.';
265 $field = $this->db->sanitize($alias . ($fieldInfos->nameInTable ?? $key));
266
267 $tmp = "'" . implode("','", array_map(array($this->db, 'escape'), $value)) . "'";
268 return " AND " . $field . " IN (" . $this->db->sanitize($tmp, 1) . ")";
269 }
270
271 return '';
272 }
273
284 public function getOptions($fieldInfos, $key, $addEmptyValue = false, $reload = false, $selectedValues = array())
285 {
286 return parent::getOptions($fieldInfos, $key, $addEmptyValue, $reload, $selectedValues);
287 }
288}
$c
Definition line.php:331
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.
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.