dolibarr 23.0.3
sellistfield.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
59 $optionsList = array();
60 $options = $this->getOptions($fieldInfos, $key);
61 foreach ($options as $optionKey => $optionInfos) {
62 $options[$optionKey] = $optionInfos['label'];
63 }
64
65 return self::$form->multiselectarray($htmlName, $optionsList, $value, 0, 0, $moreCss, 0, 0, $moreAttrib, '', '', (int) (!empty($conf->use_javascript_ajax) && !getDolGlobalString('MAIN_EXTRAFIELDS_DISABLE_SELECT2')));
66 }
67
80 public function printInputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
81 {
82 global $conf;
83
84 $moreCss = $this->getInputCss($fieldInfos, $moreCss);
85 $moreAttrib = trim((string) $moreAttrib);
86 if (empty($moreAttrib)) $moreAttrib = ' ' . $moreAttrib;
87 $placeHolder = $fieldInfos->inputPlaceholder;
88 if (!empty($placeHolder)) $placeHolder = ' placeholder="' . dolPrintHTMLForAttribute($placeHolder) . '"';
89 $autoFocus = $fieldInfos->inputAutofocus ? ' autofocus' : '';
90 $htmlName = $keyPrefix . $key . $keySuffix;
91 $selectedValue = is_null($value) || $this->isEmptyValue($fieldInfos, $value) ? '' : (string) $value;
92
93 if (!empty($conf->use_javascript_ajax) && getDolGlobalString('MAIN_EXTRAFIELDS_ENABLE_NEW_SELECT2')) {
94 $objectId = isset($fieldInfos->otherParams['objectId']) ? (int) $fieldInfos->otherParams['objectId'] : (isset($fieldInfos->object) && is_object($fieldInfos->object) ? $fieldInfos->object->id : 0);
95 $objectType = isset($fieldInfos->otherParams['objectType']) ? (int) $fieldInfos->otherParams['objectType'] : (isset($fieldInfos->object) && is_object($fieldInfos->object) ? $fieldInfos->object->element : '');
96 $printMode = empty($fieldInfos->mode) ? 'view' : $fieldInfos->mode;
97 $options = $this->getOptions($fieldInfos, $key, false, false, $selectedValue);
98
99 // TODO add dependency support (set dependencyvalue with dependency field value get by JS)
100 $ajaxData = array(
101 'objecttype' => $objectType,
102 'objectid' => $objectId,
103 'objectkey' => $key,
104 'mode' => $printMode,
105 'value' => $selectedValue,
106 'dependencyvalue' => '',
107 );
108
109 $out = self::$form->inputSelectAjax($htmlName, $options, $selectedValue, self::$ajaxUrl, $ajaxData, $moreCss, $moreAttrib . $placeHolder . $autoFocus);
110 } else {
111 $options = $this->getOptions($fieldInfos, $key, true);
112
113 $out = self::$form->selectarray($htmlName, $options, $selectedValue, 0, 0, 0, $moreAttrib . $placeHolder . $autoFocus, 0, 0, 0, '', $moreCss);
114 }
115
116 return $out;
117 }
118
131 public function printOutputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
132 {
133 $value = (string) $value;
134
135 if (!$this->isEmptyValue($fieldInfos, $value)) {
136 $options = $this->getOptions($fieldInfos, $key, false, false, $value);
137 if (isset($options[$value])) {
138 $out = $options[$value]['label'];
139
140 $optionParams = $this->getOptionsParams($fieldInfos->options);
141 if ($optionParams['tableName'] == 'categorie' && !empty($optionParams['categoryType'])) {
142 require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php';
143 $c = new Categorie($this->db);
144 $c->fetch((int) $value);
145 $color = ' style="background: #' . ($c->color ? $c->color : 'bbb') . ';"';
146 $label = img_object('', 'category') . ' ' . $value;
147 $out = '<div class="select2-container-multi-dolibarr" style="width: 90%;"><ul class="select2-choices-dolibarr"><li class="select2-search-choice-dolibarr noborderoncategories"' . $color . '>' . $label . '</li></ul></div>';
148 }
149
150 $value = $out;
151 }
152 }
153
154 return $value;
155 }
156
166 public function getInputCss($fieldInfos, $moreCss = '', $defaultCss = '')
167 {
168 return parent::getInputCss($fieldInfos, $moreCss, $defaultCss ? $defaultCss : 'minwidth400');
169 }
170
180 public function verifyFieldValue($fieldInfos, $key, $value)
181 {
182 $result = parent::verifyFieldValue($fieldInfos, $key, $value);
183 if ($result && !$this->isEmptyValue($fieldInfos, $value)) {
184 $optionParams = $this->getOptionsParams($fieldInfos->options);
185 if (!self::$validator->isInDb($value, $optionParams['tableName'], $optionParams['keyField'])) {
186 return false;
187 }
188
189 $result = true;
190 }
191
192 return $result;
193 }
194
205 public function verifyPostFieldValue($fieldInfos, $key, $keyPrefix = '', $keySuffix = '')
206 {
207 $htmlName = $keyPrefix . $key . $keySuffix;
208 $value = GETPOST($htmlName, 'restricthtml');
209 $value = trim($value);
210
211 return $this->verifyFieldValue($fieldInfos, $key, $value);
212 }
213
225 public function getPostFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
226 {
227 $htmlName = $keyPrefix . $key . $keySuffix;
228
229 if (GETPOSTISSET($htmlName)) {
230 $value = GETPOST($htmlName, 'alphanohtml');
231 } else {
232 $value = $defaultValue;
233 }
234
235 return $value;
236 }
237
249 public function getPostSearchFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
250 {
251 $htmlName = $keyPrefix . $key . $keySuffix;
252
253 if (GETPOSTISSET($htmlName)) {
254 $value = GETPOST($htmlName, 'array');
255 } else {
256 $value = $defaultValue;
257 }
258
259 return $value;
260 }
261
271 public function sqlFilterSearchField($fieldInfos, $key, $value)
272 {
273 if (!empty($value) && is_array($value)) {
274 $alias = $fieldInfos->sqlAlias ?? 't.';
275 $field = $this->db->sanitize($alias . ($fieldInfos->nameInTable ?? $key));
276
277 $tmp = "'" . implode("','", array_map(array($this->db, 'escape'), $value)) . "'";
278 return " AND " . $field . " IN (" . $this->db->sanitize($tmp, 1) . ")";
279 }
280
281 return '';
282 }
283
294 public function getOptions($fieldInfos, $key, $addEmptyValue = false, $reload = false, $selectedValues = array())
295 {
296 return parent::getOptions($fieldInfos, $key, $addEmptyValue, $reload, $selectedValues);
297 }
298}
$c
Definition line.php:331
Class to manage categories.
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.
Class to sellist field.
getInputCss($fieldInfos, $moreCss='', $defaultCss='')
Get input CSS.
getPostFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get field value from GET/POST.
verifyFieldValue($fieldInfos, $key, $value)
Verify if the field value is valid.
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.
printInputField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to put an input field into a page.
verifyPostFieldValue($fieldInfos, $key, $keyPrefix='', $keySuffix='')
Verify if the field value from GET/POST is valid.
getOptions($fieldInfos, $key, $addEmptyValue=false, $reload=false, $selectedValues=array())
Get list of options.
printInputSearchField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to put an input search field into a page.
getPostSearchFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get search field value from GET/POST.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
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.