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