dolibarr 22.0.5
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$sql = "SELECT name, label, type, param, fieldcomputed, fielddefault, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields";
40$sql .= " WHERE elementtype = '".$this->db->escape($keyforselect)."' AND type <> 'separate' AND entity IN (0, ".((int) $conf->entity).') ORDER BY pos ASC';
41//print $sql;
42$resql = $this->db->query($sql);
43if ($resql) { // This can fail when class is used on old database (during migration for example)
44 while ($obj = $this->db->fetch_object($resql)) {
45 $fieldname = $keyforaliasextra.'.'.$obj->name;
46 $fieldlabel = ucfirst($obj->label);
47 $typeFilter = "Text";
48 $typefield = preg_replace('/\‍(.*$/', '', $obj->type); // double(24,8) -> double
49 switch ($typefield) {
50 case 'int':
51 case 'integer':
52 case 'double':
53 case 'price':
54 $typeFilter = "Numeric";
55 break;
56 case 'date':
57 case 'datetime':
58 case 'timestamp':
59 $typeFilter = "Date";
60 break;
61 case 'boolean':
62 $typeFilter = "Boolean";
63 break;
64 case 'checkbox':
65 case 'select':
66 if (getDolGlobalString('EXPORT_LABEL_FOR_SELECT')) {
67 $tmpparam = jsonOrUnserialize($obj->param); // $tmpparam may be array with 'options' = array(key1=>val1, key2=>val2 ...)
68 if ($tmpparam['options'] && is_array($tmpparam['options'])) {
69 $typeFilter = "Select:".$obj->param;
70 }
71 }
72 break;
73 case 'sellist':
74 $tmp = '';
75 $tmpparam = jsonOrUnserialize($obj->param); // $tmp may be array 'options' => array 'c_currencies:code_iso:code_iso' => null
76 if (is_array($tmpparam) && array_key_exists('options', $tmpparam) && $tmpparam['options'] && is_array($tmpparam['options'])) {
77 $tmpkeys = array_keys($tmpparam['options']);
78 $tmp = (string) array_shift($tmpkeys);
79 }
80 if (preg_match('/[a-z0-9_]+:[a-z0-9_]+:[a-z0-9_]+/', $tmp)) {
81 $typeFilter = "List:".$tmp;
82 }
83 break;
84 }
85 if ($obj->type != 'separate') {
86 // If not a computed field
87 if (empty($obj->fieldcomputed)) {
88 $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
89 $this->import_TypeFields_array[$r][$fieldname] = $typeFilter;
90 $this->import_entities_array[$r][$fieldname] = $keyforelement;
91 } else {
92 // If this is a computed field
93 $this->import_fields_array[$r][$fieldname] = $fieldlabel.($obj->fieldrequired ? '*' : '');
94 $this->import_TypeFields_array[$r][$fieldname] = $typeFilter.'Compute';
95 $this->import_entities_array[$r][$fieldname] = $keyforelement;
96 }
97 }
98 }
99}
100// End add axtra fields
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
jsonOrUnserialize($stringtodecode)
Decode an encode string.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79