dolibarr 21.0.0-alpha
extrafieldsinexport.inc.php
1<?php
2/* Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
3 */
4'
5@phan-var-force DolibarrModules $this
6@phan-var-force int $r
7';
8
9// $keyforselect = name of main table
10// keyforelement = name of picto
11// $keyforaliasextra = a key to avoid conflict with extrafields of other objects
12
13if (empty($keyforselect) || empty($keyforelement) || empty($keyforaliasextra)) {
14 //print $keyforselet.' - '.$keyforelement.' - '.$keyforaliasextra;
15 dol_print_error(null, 'include of file extrafieldsinexport.inc.php was done but var $keyforselect or $keyforelement or $keyforaliasextra was not set');
16 exit;
17}
18
19// Add extra fields
20$sql = "SELECT name, label, type, param, fieldcomputed, fielddefault FROM ".MAIN_DB_PREFIX."extrafields";
21$sql .= " WHERE elementtype = '".$this->db->escape($keyforselect)."' AND type <> 'separate' AND entity IN (0, ".((int) $conf->entity).') ORDER BY pos ASC';
22//print $sql;
23$resql = $this->db->query($sql);
24if ($resql) { // This can fail when class is used on old database (during migration for example)
25 while ($obj = $this->db->fetch_object($resql)) {
26 $fieldname = $keyforaliasextra.'.'.$obj->name;
27 $fieldlabel = ucfirst($obj->label);
28 $typeFilter = "Text";
29 $typefield = preg_replace('/\‍(.*$/', '', $obj->type); // double(24,8) -> double
30 switch ($typefield) {
31 case 'int':
32 case 'integer':
33 case 'double':
34 case 'price':
35 $typeFilter = "Numeric";
36 break;
37 case 'date':
38 case 'datetime':
39 case 'timestamp':
40 $typeFilter = "Date";
41 break;
42 case 'boolean':
43 $typeFilter = "Boolean";
44 break;
45 case 'checkbox':
46 case 'select':
47 if (getDolGlobalString('EXPORT_LABEL_FOR_SELECT')) {
48 $tmpparam = jsonOrUnserialize($obj->param); // $tmpparam may be array with 'options' = array(key1=>val1, key2=>val2 ...)
49 if ($tmpparam['options'] && is_array($tmpparam['options'])) {
50 $typeFilter = "Select:".$obj->param;
51 }
52 }
53 break;
54 case 'sellist':
55 $tmp = '';
56 $tmpparam = jsonOrUnserialize($obj->param); // $tmp may be array 'options' => array 'c_currencies:code_iso:code_iso' => null
57 if (is_array($tmpparam) && array_key_exists('options', $tmpparam) && $tmpparam['options'] && is_array($tmpparam['options'])) {
58 $tmpkeys = array_keys($tmpparam['options']);
59 $tmp = array_shift($tmpkeys);
60 }
61 if (preg_match('/[a-z0-9_]+:[a-z0-9_]+:[a-z0-9_]+/', (string) $tmp)) {
62 $typeFilter = "List:".$tmp;
63 }
64 break;
65 }
66 if ($obj->type != 'separate') {
67 // If not a computed field
68 if (empty($obj->fieldcomputed)) {
69 $this->export_fields_array[$r][$fieldname] = $fieldlabel;
70 $this->export_TypeFields_array[$r][$fieldname] = $typeFilter;
71 $this->export_entities_array[$r][$fieldname] = $keyforelement;
72 } else {
73 // If this is a computed field
74 $this->export_fields_array[$r][$fieldname] = $fieldlabel;
75 $this->export_TypeFields_array[$r][$fieldname] = $typeFilter.'Compute';
76 $this->export_special_array[$r][$fieldname] = $obj->fieldcomputed;
77 $this->export_entities_array[$r][$fieldname] = $keyforelement;
78 }
79 }
80 }
81}
82// 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.