dolibarr 24.0.0-beta
linkfield.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/commonfield.class.php';
25
26
31{
35 public $emptyValues = array('', '-1', '0', 0);
36
37
50 public function printInputSearchField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
51 {
52 $moreCss = $this->getInputCss($fieldInfos, $moreCss);
53 $htmlName = $keyPrefix . $key . $keySuffix;
54
55 $optionParams = $this->getOptionsParams($fieldInfos->options);
56
57 if (version_compare(DOL_VERSION, '19.0.0') < 0) {
58 // Example: 'ObjectName:classPath:1:(status:=:1)'
59 $objectDesc = $optionParams['all'];
60 if (strpos($objectDesc, '$ID$') !== false && !empty($fieldInfos->object->id)) {
61 $objectDesc = str_replace('$ID$', (string) $fieldInfos->object->id, $objectDesc);
62 }
63
64 $out = self::$form->selectForForms($objectDesc, $htmlName, (int) $value, 0, '', '', $moreCss, $moreAttrib);
65 } else {
66 // Example: 'ObjectName:classPath' To not propagate any filter (selectForForms do ajax call and propagating SQL filter is blocked by some WAF).
67 // Also we should use the one into the definition in the ->fields of $elem if found.
68 $objectDesc = $optionParams['objectClass'] . ':' . $optionParams['pathToClass'];
69
70 // Example: 'actioncomm:options_fff' To be used in priority to know object linked with all its definition (including filters)
71 // The selectForForms is called with parameter $objectfield defined, so the app can retrieve the filter inside the ajax component instead of being provided as parameters. The
72 // filter was used to pass SQL requests leading to serious SQL injection problem. This should not be possible. Also the call of the ajax was broken by some WAF.
73 $objectField = isset($fieldInfos->object) ? $fieldInfos->object->element . (!empty($fieldInfos->object->module) ? '@' . $fieldInfos->object->module : '') . ':' . ($fieldInfos->fieldType == FieldInfos::FIELD_TYPE_EXTRA_FIELD ? 'options_' : '') . $fieldInfos->nameInClass : '';
74
75 $out = self::$form->selectForForms($objectDesc, $htmlName, (int) $value, 0, '', '', $moreCss, $moreAttrib, 0, 0, '', $objectField);
76 }
77
78 return $out;
79 }
80
93 public function printInputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
94 {
95 global $langs;
96
97 $moreCss = $this->getInputCss($fieldInfos, $moreCss);
98 $moreAttrib = trim((string) $moreAttrib);
99 if (empty($moreAttrib)) $moreAttrib = ' ' . $moreAttrib;
100 $placeHolder = $fieldInfos->inputPlaceholder;
101 $autoFocus = $fieldInfos->inputAutofocus ? ' autofocus' : '';
102 $htmlName = $keyPrefix . $key . $keySuffix;
103 $showEmpty = $fieldInfos->required && !$this->isEmptyValue($fieldInfos, $fieldInfos->defaultValue) ? 0 : 1;
104
105 $optionParams = $this->getOptionsParams($fieldInfos->options);
106
107 // If we have to add a create button
108 if ($optionParams['addCreateButton']) {
109 if (!empty($fieldInfos->picto)) {
110 $moreCss .= ' widthcentpercentminusxx';
111 } else {
112 $moreCss .= ' widthcentpercentminusx';
113 }
114 } elseif (!empty($fieldInfos->picto)) {
115 $moreCss .= ' widthcentpercentminusx';
116 }
117
118 if (version_compare(DOL_VERSION, '19.0.0') < 0) {
119 // Example: 'ObjectName:classPath:1:(status:=:1)'
120 $objectDesc = $optionParams['all'];
121 if (strpos($objectDesc, '$ID$') !== false && !empty($fieldInfos->object->id)) {
122 $objectDesc = str_replace('$ID$', (string) $fieldInfos->object->id, $objectDesc);
123 }
124
125 $out = self::$form->selectForForms($objectDesc, $htmlName, (int) $value, $showEmpty, '', $placeHolder, $moreCss, $moreAttrib . $autoFocus, 0, $fieldInfos->inputDisabled ? 1 : 0);
126 } else {
127 // Example: 'ObjectName:classPath' To not propagate any filter (selectForForms do ajax call and propagating SQL filter is blocked by some WAF).
128 // Also we should use the one into the definition in the ->fields of $elem if found.
129 $objectDesc = $optionParams['objectClass'] . ':' . $optionParams['pathToClass'];
130
131 // Example: 'actioncomm:options_fff' To be used in priority to know object linked with all its definition (including filters)
132 // The selectForForms is called with parameter $objectfield defined, so the app can retrieve the filter inside the ajax component instead of being provided as parameters. The
133 // filter was used to pass SQL requests leading to serious SQL injection problem. This should not be possible. Also the call of the ajax was broken by some WAF.
134 $objectField = isset($fieldInfos->object) ? $fieldInfos->object->element . (!empty($fieldInfos->object->module) ? '@' . $fieldInfos->object->module : '') . ':' . ($fieldInfos->fieldType == FieldInfos::FIELD_TYPE_EXTRA_FIELD ? 'options_' : '') . $fieldInfos->nameInClass : '';
135
136 $out = self::$form->selectForForms($objectDesc, $htmlName, (int) $value, $showEmpty, '', $placeHolder, $moreCss, $moreAttrib . $autoFocus, 0, $fieldInfos->inputDisabled ? 1 : 0, '', $objectField);
137 }
138
139 if ($optionParams['addCreateButton'] && // If we have to add a create button
140 (!GETPOSTISSET('backtopage') || strpos(GETPOST('backtopage'), $_SERVER['PHP_SELF']) === 0) && // To avoid to open several times the 'Plus' button (we accept only one level)
141 !$fieldInfos->inputDisabled && // To avoid to show the button if the field is protected by a "disabled".
142 empty($fieldInfos->otherParams['nonewbutton']) // manually disable new button
143 ) {
144 $class = $optionParams['objectClass'];
145 $classfile = $optionParams['pathToClass'];
146 $classpath = dirname(dirname($classfile));
147 if (file_exists(dol_buildpath($classpath . '/card.php'))) {
148 $url_path = dol_buildpath($classpath . '/card.php', 1);
149 } else {
150 $url_path = dol_buildpath($classpath . '/' . strtolower($class) . '_card.php', 1);
151 }
152 $paramforthenewlink = '';
153 $paramforthenewlink .= (GETPOSTISSET('action') ? '&action=' . GETPOST('action', 'aZ09') : '');
154 $paramforthenewlink .= (GETPOSTISSET('id') ? '&id=' . GETPOSTINT('id') : '');
155 $paramforthenewlink .= (GETPOSTISSET('origin') ? '&origin=' . GETPOST('origin', 'aZ09') : '');
156 $paramforthenewlink .= (GETPOSTISSET('originid') ? '&originid=' . GETPOSTINT('originid') : '');
157 $paramforthenewlink .= '&fk_' . strtolower($class) . '=--IDFORBACKTOPAGE--';
158 // TODO Add JavaScript code to add input fields already filled into $paramforthenewlink so we won't loose them when going back to main page
159 $out .= '<a class="butActionNew" title="' . $langs->trans("New") . '" href="' . $url_path . '?action=create&backtopage=' . urlencode($_SERVER['PHP_SELF'] . $paramforthenewlink) . '"><span class="fa fa-plus-circle valignmiddle"></span></a>';
160 }
161
162 return $out;
163 }
164
177 public function printOutputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
178 {
179 if (!$this->isEmptyValue($fieldInfos, $value)) {
180 $optionParams = $this->getOptionsParams($fieldInfos->options);
181
182 $classpath = $optionParams['pathToClass'];
183 if (!empty($classpath)) {
184 $classname = $optionParams['objectClass'];
185
186 $object = $this->getObject($classname, $classpath);
187 if (isset($object)) {
188 '@phan-var-force CommonObject $object';
189 if ($object->element === 'product') { // Special case for product because default valut of fetch are wrong
190 '@phan-var-force Product $object';
191 $result = $object->fetch((int) $value, '', '', '', 0, 1, 1);
192 } else {
193 $result = $object->fetch($value);
194 }
195 if ($result > 0) {
196 $getNomUrlParam1 = $optionParams['getNomUrlParam1'];
197 $getNomUrlParam2 = $optionParams['getNomUrlParam2'];
198
199 if ($object->element === 'product') {
200 '@phan-var-force Product $object';
201 $get_name_url_param_arr = array($getNomUrlParam1, $getNomUrlParam2, 0, -1, 0, '', 0, ' - ');
202 if (isset($fieldInfos->getNameUrlParams)) {
203 $get_name_url_params = explode(':', $fieldInfos->getNameUrlParams);
204 if (!empty($get_name_url_params)) {
205 $param_num_max = count($get_name_url_param_arr) - 1;
206 foreach ($get_name_url_params as $param_num => $param_value) {
207 if ($param_num > $param_num_max) {
208 break;
209 }
210 $get_name_url_param_arr[$param_num] = $param_value;
211 }
212 }
213 }
214
218 return self::$form->getNomUrl($object, (int) $get_name_url_param_arr[0], $get_name_url_param_arr[1], (int) $get_name_url_param_arr[2], (int) $get_name_url_param_arr[3], (int) $get_name_url_param_arr[4], $get_name_url_param_arr[5], (int) $get_name_url_param_arr[6], $get_name_url_param_arr[7]);
219 } elseif (get_class($object) == 'Categorie') {
220 // For category object, rendering must use the same method than the one deinfed into showCategories()
221 $color = $object->color;
222 $sfortag = '<span class="noborderoncategories"' . ($color ? ' style="background: #' . $color . ';"' : ' style="background: #bbb"') . '>';
223 $sfortag .= self::$form->getNomUrl($object, (int) $getNomUrlParam1, $getNomUrlParam2);
224 $sfortag .= '</span>';
225 return $sfortag;
226 } else {
227 return self::$form->getNomUrl($object, (int) $getNomUrlParam1, $getNomUrlParam2);
228 }
229 }
230 } else {
231 dol_syslog('Error bad setup of field : ' . $key, LOG_WARNING);
232 return 'Error bad setup of field';
233 }
234 } else {
235 dol_syslog('Error bad setup of field : ' . $key, LOG_WARNING);
236 return 'Error bad setup of field';
237 }
238 }
239
240 return '';
241 }
242
252 public function getInputCss($fieldInfos, $moreCss = '', $defaultCss = '')
253 {
254 return parent::getInputCss($fieldInfos, $moreCss, $defaultCss ? $defaultCss : 'minwidth200imp');
255 }
256
266 public function verifyFieldValue($fieldInfos, $key, $value)
267 {
268 $result = parent::verifyFieldValue($fieldInfos, $key, $value);
269 if ($result && !$this->isEmptyValue($fieldInfos, $value)) {
270 $optionParams = $this->getOptionsParams($fieldInfos->options);
271 $classname = $optionParams['objectClass'];
272 $classpath = $optionParams['pathToClass'];
273 $object = $this->getObject($classname, $classpath);
274 if (isset($object) && method_exists($object, 'isExistingObject') && !self::$validator->isFetchable((int) $value, $classname, $classpath) // All class don't have isExistingObject function ...
275 && (version_compare(DOL_VERSION, '19.0.0') < 0 || !self::$validator->isFetchableElement((int) $value, $classname)) // from V19 of Dolibarr, In some cases link use element instead of class, example project_task
276 ) {
277 return false;
278 }
279
280 $result = true;
281 }
282
283 return $result;
284 }
285
296 public function verifyPostFieldValue($fieldInfos, $key, $keyPrefix = '', $keySuffix = '')
297 {
298 return parent::verifyPostFieldValue($fieldInfos, $key, $keyPrefix, $keySuffix);
299 }
300
312 public function getPostFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
313 {
314 $htmlName = $keyPrefix . $key . $keySuffix;
315
316 if (GETPOSTISSET($htmlName)) {
317 $value = GETPOSTINT($htmlName);
318 } else {
319 $value = $defaultValue;
320 }
321
322 return $value;
323 }
324
336 public function getPostSearchFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
337 {
338 $htmlName = $keyPrefix . $key . $keySuffix;
339
340 if (GETPOSTISSET($htmlName)) {
341 $value = GETPOST($htmlName, 'alphanohtml');
342 } else {
343 $value = $defaultValue;
344 }
345
346 return $value;
347 }
348
358 public function sqlFilterSearchField($fieldInfos, $key, $value)
359 {
360 if (!$this->isEmptyValue($fieldInfos, $value)) {
361 $alias = $fieldInfos->sqlAlias ?? 't.';
362
363 return natural_search($alias . ($fieldInfos->nameInTable ?? $key), $value, 2);
364 }
365
366 return '';
367 }
368
375 public function getOptionsParams($options)
376 {
377 $options = is_array($options) ? $options : array();
378 $paramList = array_keys($options);
379 // Example: $paramList[0] = 'ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]'
380 // Example: $paramList[0] = 'ObjectClass:PathToClass:#getnomurlparam1=-1#getnomurlparam2=customer'
381 // Example: $paramList[0] = 'ObjectName:classPath' but can also be 'ObjectName:classPath:1:(status:=:1)'
382
383 $all = (string) $paramList[0];
384 $InfoFieldList = explode(":", $all);
385
386 $objectClass = (string) ($InfoFieldList[0] ?? '');
387 $pathToClass = (string) ($InfoFieldList[1] ?? '');
388 $addCreateButton = !empty($InfoFieldList[2]) && is_numeric($InfoFieldList[2]);
389 $getNomUrlParam1 = 3;
390 $getNomUrlParam2 = '';
391 if (preg_match('/#getnomurlparam1=([^#:]*)/', $all, $matches)) {
392 $getNomUrlParam1 = $matches[1];
393 }
394 if (preg_match('/#getnomurlparam2=([^#:]*)/', $all, $matches)) {
395 $getNomUrlParam2 = $matches[1];
396 }
397 $filter = (string) ($InfoFieldList[3] ?? '');
398 $sortField = (string) ($InfoFieldList[4] ?? '');
399
400 return array(
401 'all' => $all,
402 'objectClass' => $objectClass,
403 'pathToClass' => $pathToClass,
404 'addCreateButton' => $addCreateButton,
405 'getNomUrlParam1' => $getNomUrlParam1,
406 'getNomUrlParam2' => $getNomUrlParam2,
407 'filter' => $filter,
408 'sortField' => $sortField,
409 );
410 }
411
419 public function getObject($objectClass, $pathToClass)
420 {
421 dol_include_once($pathToClass);
422 if ($objectClass && !class_exists($objectClass)) {
423 // from V19 of Dolibarr, In some cases link use element instead of class, example project_task
424 // TODO use newObjectByElement() introduce in V20 by PR #30036 for better errors management
425 $element_prop = getElementProperties($objectClass);
426 if ($element_prop) {
427 $objectClass = $element_prop['classname'];
428 }
429 }
430
431 if ($objectClass && class_exists($objectClass)) {
432 return new $objectClass($this->db);
433 }
434
435 return null;
436 }
437}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to common field.
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.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
natural_search($fields, $value, $mode=0, $nofirstand=0, $sqltoadd='')
Generate natural SQL search string for a criteria (this criteria can be tested on one or several fiel...
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php