dolibarr 25.0.0-alpha
extrafieldsinimport.inc.php
1<?php
2/* Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
3 * Copyright (C) 2025 Frédéric France <frederic.france@free.fr>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
26'@phan-var-force DolibarrModules $this';
27
28// $keyforselect = name of main table
29// keyforelement = name of picto
30// $keyforaliasextra = a key to avoid conflict with extrafields of other objects
31
32if (empty($keyforselect) || empty($keyforelement) || empty($keyforaliasextra)) {
33 //print $keyforselet.' - '.$keyforelement.' - '.$keyforaliasextra;
34 dol_print_error(null, 'include of file extrafieldsinimport.inc.php was done but var $keyforselect or $keyforelement or $keyforaliasextra was not set');
35 exit;
36}
37
38// Add extra fields
39// The list of extrafields for a given elementtype/entity is identical for every module that imports it,
40// so it is cached for the duration of the request instead of being re-queried on each inclusion of this file
41// (a page like user/perms.php that instantiates every module can otherwise trigger the same query dozens of times).
42$cachekey = $keyforselect.'_'.$conf->entity;
43if (!isset($conf->cache['extrafieldsinimport'][$cachekey])) {
44 $sql = "SELECT name, label, type, param, fieldcomputed, fielddefault, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields";
45 $sql .= " WHERE elementtype = '".$this->db->escape($keyforselect)."' AND type <> 'separate' AND entity IN (0, ".((int) $conf->entity).') ORDER BY pos ASC';
46 //print $sql;
47 $tmparrayextrafieldsinimport = array();
48 $resql = $this->db->query($sql);
49 if ($resql) { // This can fail when class is used on old database (during migration for example)
50 while ($obj = $this->db->fetch_object($resql)) {
51 $tmparrayextrafieldsinimport[] = $obj;
52 }
53 }
54 $conf->cache['extrafieldsinimport'][$cachekey] = $tmparrayextrafieldsinimport;
55}
56if (!empty($conf->cache['extrafieldsinimport'][$cachekey])) {
57 foreach ($conf->cache['extrafieldsinimport'][$cachekey] as $obj) {
58 '@phan-var-force stdClass $obj';
59 $fieldname = $keyforaliasextra.'.'.$obj->name;
60 $fieldlabel = ucfirst($obj->label);
61 $typeFilter = "Text";
62 $typefield = preg_replace('/\‍(.*$/', '', $obj->type); // double(24,8) -> double
63 switch ($typefield) {
64 case 'int':
65 case 'integer':
66 case 'double':
67 case 'price':
68 $typeFilter = "Numeric";
69 break;
70 case 'date':
71 case 'datetime':
72 case 'timestamp':
73 $typeFilter = "Date";
74 break;
75 case 'boolean':
76 $typeFilter = "Boolean";
77 break;
78 case 'checkbox':
79 case 'select':
80 if (getDolGlobalString('EXPORT_LABEL_FOR_SELECT')) {
81 $tmpparam = jsonOrUnserialize($obj->param); // $tmpparam may be array with 'options' = array(key1=>val1, key2=>val2 ...)
82 if ($tmpparam['options'] && is_array($tmpparam['options'])) {
83 $typeFilter = "Select:".$obj->param;
84 }
85 }
86 break;
87 case 'sellist':
88 $tmp = '';
89 $tmpparam = jsonOrUnserialize($obj->param); // $tmp may be array 'options' => array 'c_currencies:code_iso:code_iso' => null
90 if (is_array($tmpparam) && array_key_exists('options', $tmpparam) && $tmpparam['options'] && is_array($tmpparam['options'])) {
91 $tmpkeys = array_keys($tmpparam['options']);
92 $tmp = (string) array_shift($tmpkeys);
93 }
94 if (preg_match('/[a-z0-9_]+:[a-z0-9_]+:[a-z0-9_]+/', $tmp)) {
95 $typeFilter = "List:".$tmp;
96 }
97 break;
98 }
99 if ($obj->type != 'separate') {
100 // If not a computed field
101 if (empty($obj->fieldcomputed)) {
102 $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
103 $this->import_TypeFields_array[$r][$fieldname] = $typeFilter;
104 $this->import_entities_array[$r][$fieldname] = $keyforelement;
105 } else {
106 // If this is a computed field
107 $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
108 $this->import_TypeFields_array[$r][$fieldname] = $typeFilter.'Compute';
109 $this->import_entities_array[$r][$fieldname] = $keyforelement;
110 }
111 }
112 }
113}
114// End add axtra fields
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
jsonOrUnserialize($stringtodecode, $assoc=true)
Decode an encoded string.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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