dolibarr 23.0.3
selectfield.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('');
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 $value = $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, $value, 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 $moreCss = $this->getInputCss($fieldInfos, $moreCss);
84 $moreAttrib = trim((string) $moreAttrib);
85 if (empty($moreAttrib)) $moreAttrib = ' ' . $moreAttrib;
86 $placeHolder = $fieldInfos->inputPlaceholder;
87 if (!empty($placeHolder)) $placeHolder = ' placeholder="' . dolPrintHTMLForAttribute($placeHolder) . '"';
88 $autoFocus = $fieldInfos->inputAutofocus ? ' autofocus' : '';
89 $htmlName = $keyPrefix . $key . $keySuffix;
90
91 $selectedValue = is_null($value) || $this->isEmptyValue($fieldInfos, $value) ? '' : (string) $value;
92
93 $options = $this->getOptions($fieldInfos, $key, true);
94
95 return self::$form->selectarray($htmlName, $options, $selectedValue, 0, 0, 0, $moreAttrib . $placeHolder . $autoFocus, 0, 0, 0, '', $moreCss);
96 }
97
110 public function printOutputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
111 {
112 $value = (string) $value;
113
114 if (!$this->isEmptyValue($fieldInfos, $value)) {
115 $options = $this->getOptions($fieldInfos, $key);
116 foreach ($options as $optionKey => $optionInfos) {
117 if (((string) $optionKey) == $value) {
118 $value = $optionInfos['label'];
119 break;
120 }
121 }
122 }
123
124 return $value;
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 $value = (string) $value;
154
155 $result = parent::verifyFieldValue($fieldInfos, $key, $value);
156 if ($result && !$this->isEmptyValue($fieldInfos, $value)) {
157 $options = $this->getOptions($fieldInfos, $key);
158 if (!isset($options[$value])) {
159 self::$validator->error = $langs->trans('RequireValidValue');
160 return false;
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 $value = GETPOST($htmlName, 'restricthtml');
183 $value = trim($value);
184
185 return $this->verifyFieldValue($fieldInfos, $key, $value);
186 }
187
199 public function getPostFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
200 {
201 $htmlName = $keyPrefix . $key . $keySuffix;
202
203 if (GETPOSTISSET($htmlName)) {
204 $value = GETPOST($htmlName, 'alphanohtml');
205 } else {
206 $value = $defaultValue;
207 }
208
209 return $this->isEmptyValue($fieldInfos, $value) ? '' : $value;
210 }
211
223 public function getPostSearchFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
224 {
225 $htmlName = $keyPrefix . $key . $keySuffix;
226
227 if (GETPOSTISSET($htmlName)) {
228 $value = GETPOST($htmlName, 'array');
229 } else {
230 $value = $defaultValue;
231 }
232
233 return $value;
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}
isEmptyValue($fieldInfos, $value, $emptyValues=null)
Check if the value is deemed as empty.
Class to common select field.
Class to select field.
getPostFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get field value from GET/POST.
getInputCss($fieldInfos, $moreCss='', $defaultCss='')
Get input CSS.
sqlFilterSearchField($fieldInfos, $key, $value)
Get sql filter for search field.
getPostSearchFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get search field value from GET/POST.
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)
Get list of options.
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.
verifyPostFieldValue($fieldInfos, $key, $keyPrefix='', $keySuffix='')
Verify if the field value from GET/POST is valid.
printOutputField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to show a field into a page.
dolPrintHTMLForAttribute($s, $escapeonlyhtmltags=0, $allowothertags=array())
Return a string ready to be output into an HTML attribute (alt, title, data-html, ....
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.