dolibarr 24.0.0-beta
extrafieldsinexport.inc.php
1<?php
2/* Copyright (C) 2024 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'
27@phan-var-force DolibarrModules $this
28@phan-var-force int $r
29';
30
31// $keyforselect = name of main table
32// keyforelement = name of picto
33// $keyforaliasextra = a key to avoid conflict with extrafields of other objects
34
35if (empty($keyforselect) || empty($keyforelement) || empty($keyforaliasextra)) {
36 //print $keyforselet.' - '.$keyforelement.' - '.$keyforaliasextra;
37 dol_print_error(null, 'include of file extrafieldsinexport.inc.php was done but var $keyforselect or $keyforelement or $keyforaliasextra was not set');
38 exit;
39}
40
41// Add extra fields
42$sql = "SELECT name, label, type, param, fieldcomputed, fielddefault FROM ".MAIN_DB_PREFIX."extrafields";
43$sql .= " WHERE elementtype = '".$this->db->escape($keyforselect)."' AND type <> 'separate' AND entity IN (0, ".((int) $conf->entity).') ORDER BY pos ASC';
44//print $sql;
45$resql = $this->db->query($sql);
46if ($resql) { // This can fail when class is used on old database (during migration for example)
47 while ($obj = $this->db->fetch_object($resql)) {
48 $fieldname = $keyforaliasextra.'.'.$obj->name;
49 $fieldlabel = ucfirst($obj->label);
50 $typeFilter = "Text";
51 $typefield = preg_replace('/\‍(.*$/', '', $obj->type); // double(24,8) -> double
52 switch ($typefield) {
53 case 'int':
54 case 'integer':
55 case 'double':
56 case 'price':
57 $typeFilter = "Numeric";
58 break;
59 case 'date':
60 case 'datetime':
61 case 'timestamp':
62 $typeFilter = "Date";
63 break;
64 case 'boolean':
65 $typeFilter = "Boolean";
66 break;
67 case 'checkbox':
68 case 'select':
69 if (getDolGlobalString('EXPORT_LABEL_FOR_SELECT')) {
70 $tmpparam = jsonOrUnserialize($obj->param); // $tmpparam may be array with 'options' = array(key1=>val1, key2=>val2 ...)
71 if ($tmpparam['options'] && is_array($tmpparam['options'])) {
72 $typeFilter = "Select:".$obj->param;
73 }
74 }
75 break;
76 case 'sellist':
77 $tmp = '';
78 $tmpparam = jsonOrUnserialize($obj->param); // $tmp may be array 'options' => array 'c_currencies:code_iso:code_iso' => null
79 if (is_array($tmpparam) && array_key_exists('options', $tmpparam) && $tmpparam['options'] && is_array($tmpparam['options'])) {
80 $tmpkeys = array_keys($tmpparam['options']);
81 $tmp = array_shift($tmpkeys);
82 }
83 if (preg_match('/[a-z0-9_]+:[a-z0-9_]+:[a-z0-9_]+/', (string) $tmp)) {
84 $typeFilter = "List:".$tmp;
85 }
86 break;
87 }
88 if ($obj->type != 'separate') {
89 // If not a computed field
90 if (empty($obj->fieldcomputed)) {
91 $this->export_fields_array[$r][$fieldname] = $fieldlabel;
92 $this->export_TypeFields_array[$r][$fieldname] = $typeFilter;
93 $this->export_entities_array[$r][$fieldname] = $keyforelement;
94 } else {
95 // If this is a computed field
96 $this->export_fields_array[$r][$fieldname] = $fieldlabel;
97 $this->export_TypeFields_array[$r][$fieldname] = $typeFilter.'Compute';
98 $this->export_special_array[$r][$fieldname] = $obj->fieldcomputed;
99 $this->export_entities_array[$r][$fieldname] = $keyforelement;
100 }
101 }
102 }
103}
104// 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:
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.