dolibarr 23.0.3
radiofield.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 $htmlName = $keyPrefix . $key . $keySuffix;
87
88 $selectedValue = $this->isEmptyValue($fieldInfos, $value) ? '' : (string) $value;
89
90 $options = $this->getOptions($fieldInfos, $key, true);
91 $values = array();
92 foreach ($options as $optionKey => $optionInfos) {
93 $values[$optionKey] = $optionInfos['label'];
94 }
95
96 return self::$form->inputRadio($htmlName, $values, $selectedValue, $moreCss, $moreAttrib);
97 }
98
111 public function printOutputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
112 {
113 global $langs;
114 $value = (string) $value;
115
116 if ($fieldInfos->required && $this->isEmptyValue($fieldInfos, $value)) {
117 $langs->load('errors');
118 $value = '<span class="opacitymedium">' . $langs->trans('ErrorNoValueForRadioType') . '</span>';
119 } else {
120 $options = $this->getOptions($fieldInfos, $key);
121 foreach ($options as $optionKey => $optionInfos) {
122 if (((string) $optionKey) == $value) {
123 $value = $optionInfos['label'];
124 break;
125 }
126 }
127 }
128
129 return $value;
130 }
131
141 public function getInputCss($fieldInfos, $moreCss = '', $defaultCss = '')
142 {
143 return parent::getInputCss($fieldInfos, $moreCss, $defaultCss ? $defaultCss : 'width25');
144 }
145
155 public function verifyFieldValue($fieldInfos, $key, $value)
156 {
157 global $langs;
158 $value = (string) $value;
159
160 $result = parent::verifyFieldValue($fieldInfos, $key, $value);
161 if ($result && !$this->isEmptyValue($fieldInfos, $value)) {
162 $options = $this->getOptions($fieldInfos, $key);
163 if (!isset($options[$value])) {
164 self::$validator->error = $langs->trans('RequireValidValue');
165 return false;
166 }
167
168 $result = true;
169 }
170
171 return $result;
172 }
173
184 public function verifyPostFieldValue($fieldInfos, $key, $keyPrefix = '', $keySuffix = '')
185 {
186 $htmlName = $keyPrefix . $key . $keySuffix;
187 $value = GETPOST($htmlName, 'restricthtml');
188 $value = trim($value);
189
190 return $this->verifyFieldValue($fieldInfos, $key, $value);
191 }
192
204 public function getPostFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
205 {
206 $htmlName = $keyPrefix . $key . $keySuffix;
207
208 if (GETPOSTISSET($htmlName)) {
209 $value = GETPOST($htmlName, 'alphanohtml');
210 } else {
211 $value = $defaultValue;
212 }
213
214 return $value;
215 }
216
228 public function getPostSearchFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
229 {
230 $htmlName = $keyPrefix . $key . $keySuffix;
231
232 if (GETPOSTISSET($htmlName)) {
233 $value = GETPOST($htmlName, 'array');
234 } else {
235 $value = $defaultValue;
236 }
237
238 return $value;
239 }
240
250 public function sqlFilterSearchField($fieldInfos, $key, $value)
251 {
252 if (!$this->isEmptyValue($fieldInfos, $value) && is_array($value)) {
253 $alias = $fieldInfos->sqlAlias ?? 't.';
254 $field = $this->db->sanitize($alias . ($fieldInfos->nameInTable ?? $key));
255
256 $tmp = "'" . implode("','", array_map(array($this->db, 'escape'), $value)) . "'";
257 return " AND " . $field . " IN (" . $this->db->sanitize($tmp, 1) . ")";
258 }
259
260 return '';
261 }
262
272 public function getOptions($fieldInfos, $key, $addEmptyValue = false, $reload = false)
273 {
274 return parent::getOptions($fieldInfos, $key, $addEmptyValue, $reload);
275 }
276}
isEmptyValue($fieldInfos, $value, $emptyValues=null)
Check if the value is deemed as empty.
Class to common select field.
Class to radio field.
getInputCss($fieldInfos, $moreCss='', $defaultCss='')
Get input CSS.
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.
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.
printInputSearchField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to put an input search field into a page.
verifyFieldValue($fieldInfos, $key, $value)
Verify if the field value is valid.
getPostSearchFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get search field value from GET/POST.
getOptions($fieldInfos, $key, $addEmptyValue=false, $reload=false)
Get list of options.
sqlFilterSearchField($fieldInfos, $key, $value)
Get sql filter for search 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.