dolibarr 25.0.0-alpha
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// The list of extrafields for a given elementtype/entity is identical for every module that exports it,
43// so it is cached for the duration of the request instead of being re-queried on each inclusion of this file
44// (a page like user/perms.php that instantiates every module can otherwise trigger the same query dozens of times).
45$cachekey = $keyforselect.'_'.$conf->entity;
46if (!isset($conf->cache['extrafieldsinexport'][$cachekey])) {
47 $sql = "SELECT name, label, type, param, fieldcomputed, fielddefault FROM ".MAIN_DB_PREFIX."extrafields";
48 $sql .= " WHERE elementtype = '".$this->db->escape($keyforselect)."' AND type <> 'separate' AND entity IN (0, ".((int) $conf->entity).') ORDER BY pos ASC';
49 //print $sql;
50 $tmparrayextrafieldsinexport = array();
51 $resql = $this->db->query($sql);
52 if ($resql) { // This can fail when class is used on old database (during migration for example)
53 while ($obj = $this->db->fetch_object($resql)) {
54 $tmparrayextrafieldsinexport[] = $obj;
55 }
56 }
57 $conf->cache['extrafieldsinexport'][$cachekey] = $tmparrayextrafieldsinexport;
58}
59if (!empty($conf->cache['extrafieldsinexport'][$cachekey])) {
60 foreach ($conf->cache['extrafieldsinexport'][$cachekey] as $obj) {
61 '@phan-var-force stdClass $obj';
62 $fieldname = $keyforaliasextra.'.'.$obj->name;
63 $fieldlabel = ucfirst($obj->label);
64 $typeFilter = "Text";
65 $typefield = preg_replace('/\‍(.*$/', '', $obj->type); // double(24,8) -> double
66 switch ($typefield) {
67 case 'int':
68 case 'integer':
69 case 'double':
70 case 'price':
71 $typeFilter = "Numeric";
72 break;
73 case 'date':
74 case 'datetime':
75 case 'timestamp':
76 $typeFilter = "Date";
77 break;
78 case 'boolean':
79 $typeFilter = "Boolean";
80 break;
81 case 'checkbox':
82 case 'select':
83 if (getDolGlobalString('EXPORT_LABEL_FOR_SELECT')) {
84 $tmpparam = jsonOrUnserialize($obj->param); // $tmpparam may be array with 'options' = array(key1=>val1, key2=>val2 ...)
85 if ($tmpparam['options'] && is_array($tmpparam['options'])) {
86 $typeFilter = "Select:".$obj->param;
87 }
88 }
89 break;
90 case 'sellist':
91 $tmp = '';
92 $tmpparam = jsonOrUnserialize($obj->param); // $tmp may be array 'options' => array 'c_currencies:code_iso:code_iso' => null
93 if (is_array($tmpparam) && array_key_exists('options', $tmpparam) && $tmpparam['options'] && is_array($tmpparam['options'])) {
94 $tmpkeys = array_keys($tmpparam['options']);
95 $tmp = array_shift($tmpkeys);
96 }
97 if (preg_match('/[a-z0-9_]+:[a-z0-9_]+:[a-z0-9_]+/', (string) $tmp)) {
98 $typeFilter = "List:".$tmp;
99 }
100 break;
101 }
102 if ($obj->type != 'separate') {
103 // If not a computed field
104 if (empty($obj->fieldcomputed)) {
105 $this->export_fields_array[$r][$fieldname] = $fieldlabel;
106 $this->export_TypeFields_array[$r][$fieldname] = $typeFilter;
107 $this->export_entities_array[$r][$fieldname] = $keyforelement;
108 } else {
109 // If this is a computed field
110 $this->export_fields_array[$r][$fieldname] = $fieldlabel;
111 $this->export_TypeFields_array[$r][$fieldname] = $typeFilter.'Compute';
112 $this->export_special_array[$r][$fieldname] = $obj->fieldcomputed;
113 $this->export_entities_array[$r][$fieldname] = $keyforelement;
114 }
115 }
116 }
117}
118// 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...