extrafieldsinexport.inc.php 2.9 KB

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