28 use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
29 use PhpOffice\PhpSpreadsheet\Spreadsheet;
30 use PhpOffice\PhpSpreadsheet\Style\Alignment;
32 require_once DOL_DOCUMENT_ROOT .
'/core/modules/import/modules_import.php';
55 public $errors = array();
73 public $version =
'dolibarr';
85 public $cacheconvert = array();
87 public $cachefieldtable = array();
108 global $conf, $langs;
113 $this->label =
'Excel 2007';
114 $this->desc = $langs->trans(
"Excel2007FormatDesc");
115 $this->extension =
'xlsx';
116 $this->picto =
'mime/xls';
117 $this->version =
'1.0';
120 require_once DOL_DOCUMENT_ROOT.
'/includes/phpoffice/phpspreadsheet/src/autoloader.php';
121 require_once DOL_DOCUMENT_ROOT.
'/includes/Psr/autoloader.php';
122 require_once PHPEXCELNEW_PATH.
'Spreadsheet.php';
123 $this->workbook =
new Spreadsheet();
126 if (!class_exists(
'ZipArchive')) {
127 $langs->load(
"errors");
128 $this->error = $langs->trans(
'ErrorPHPNeedModule',
'zip');
131 $this->label_lib =
'PhpSpreadSheet';
132 $this->version_lib =
'1.8.0';
134 $this->datatoimport = $datatoimport;
135 if (preg_match(
'/^societe_/', $datatoimport)) {
136 $this->thirdpartyobject =
new Societe($this->
db);
151 global $user, $conf, $langs, $file;
153 $this->workbook->getProperties()->setCreator($user->getFullName($outputlangs) .
' - Dolibarr ' . DOL_VERSION);
154 $this->workbook->getProperties()->setTitle($outputlangs->trans(
"Import") .
' - ' . $file);
155 $this->workbook->getProperties()->setSubject($outputlangs->trans(
"Import") .
' - ' . $file);
156 $this->workbook->getProperties()->setDescription($outputlangs->trans(
"Import") .
' - ' . $file);
158 $this->workbook->setActiveSheetIndex(0);
159 $this->workbook->getActiveSheet()->setTitle($outputlangs->trans(
"Sheet"));
160 $this->workbook->getActiveSheet()->getDefaultRowDimension()->setRowHeight(16);
177 $this->workbook->getActiveSheet()->getStyle(
'1')->getFont()->setBold(
true);
178 $this->workbook->getActiveSheet()->getStyle(
'1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT);
181 foreach ($headerlinefields as $field) {
182 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($col, 1, $outputlangs->transnoentities($field));
204 foreach ($contentlinevalues as $cell) {
205 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($col, $row, $cell);
223 $tempfile = tempnam(sys_get_temp_dir(),
'dol');
224 $objWriter =
new PhpOffice\PhpSpreadsheet\Writer\Xlsx($this->workbook);
225 $objWriter->save($tempfile);
226 $this->workbook->disconnectWorksheets();
227 unset($this->workbook);
229 $content = file_get_contents($tempfile);
249 dol_syslog(get_class($this) .
"::open_file file=" . $file);
251 $reader =
new Xlsx();
252 $this->workbook = $reader->load($file);
270 $reader =
new Xlsx();
271 $this->workbook = $reader->load($file);
273 $rowcount = $this->workbook->getActiveSheet()->getHighestDataRow();
275 $this->workbook->disconnectWorksheets();
276 unset($this->workbook);
292 $this->headers = array();
294 $info = $xlsx->listWorksheetinfo($this->file);
295 $countcolumns = $info[0][
'totalColumns'];
296 for ($col = 1; $col <= $countcolumns; $col++) {
297 $this->headers[$col] = $this->workbook->getActiveSheet()->getCellByColumnAndRow($col, 1)->getValue();
314 $rowcount = $this->workbook->getActiveSheet()->getHighestDataRow();
315 if ($this->record > $rowcount) {
320 $info = $xlsx->listWorksheetinfo($this->file);
321 $countcolumns = $info[0][
'totalColumns'];
322 for ($col = 1; $col <= $countcolumns; $col++) {
323 $val = $this->workbook->getActiveSheet()->getCellByColumnAndRow($col, $this->record)->getValue();
324 $array[$col][
'val'] = $val;
325 $array[$col][
'type'] = (
dol_strlen($val) ? 1 : -1);
340 $this->workbook->disconnectWorksheets();
341 unset($this->workbook);
358 public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
361 global $langs, $conf, $user;
362 global $thirdparty_static;
363 global $tablewithentity_cache;
367 $this->errors = array();
368 $this->warnings = array();
375 $array_match_database_to_file = array_flip($array_match_file_to_database);
376 $sort_array_match_file_to_database = $array_match_file_to_database;
377 ksort($sort_array_match_file_to_database);
381 if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[1][
'val']))) {
383 $this->warnings[$warning][
'lib'] = $langs->trans(
'EmptyLine');
384 $this->warnings[$warning][
'type'] =
'EMPTY';
387 $last_insert_id_array = array();
391 foreach ($objimport->array_import_tables[0] as $alias => $tablename) {
394 $listfields = array();
395 $listvalues = array();
397 $errorforthistable = 0;
400 if (!isset($tablewithentity_cache[$tablename])) {
401 dol_syslog(
"Check if table " . $tablename .
" has an entity field");
402 $resql = $this->
db->DDLDescTable($tablename,
'entity');
404 $obj = $this->
db->fetch_object(
$resql);
406 $tablewithentity_cache[$tablename] = 1;
408 $tablewithentity_cache[$tablename] = 0;
418 $arrayfield = array();
419 foreach ($sort_array_match_file_to_database as $key => $val) {
420 $arrayfield[$val] = ($key);
427 foreach ($sort_array_match_file_to_database as $key => $val) {
428 $fieldalias = preg_replace(
'/\..*$/i',
'', $val);
429 $fieldname = preg_replace(
'/^.*\./i',
'', $val);
431 if ($alias != $fieldalias) {
435 if ($key <= $maxfields) {
438 if ($arrayrecord[($key)][
'type'] > 0) {
439 $newval = $arrayrecord[($key)][
'val'];
448 if (preg_match(
'/\*/', $objimport->array_import_fields[0][$val]) && ((string) $newval ==
'')) {
449 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMissingMandatoryValue',
num2Alpha($key - 1));
450 $this->errors[$error][
'type'] =
'NOTNULL';
451 $errorforthistable++;
456 if (!empty($objimport->array_import_convertvalue[0][$val])) {
458 if ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeid'
459 || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromref'
460 || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeorlabel'
464 if (!is_numeric($newval) && $newval !=
'' && !preg_match(
'/^id:/i', $newval)) {
467 $newval = preg_replace(
'/^(id|ref):/i',
'', $newval);
470 if ($isidorref ==
'ref') {
471 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
472 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
473 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
474 if ($this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_'][$newval] !=
'') {
475 $newval = $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_'][$newval];
478 if (empty($resultload)) {
479 dol_print_error(
'',
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method);
482 $classinstance =
new $class($this->
db);
483 if ($class ==
'CGenericDic') {
484 $classinstance->element = $objimport->array_import_convertvalue[0][$val][
'element'];
485 $classinstance->table_element = $objimport->array_import_convertvalue[0][$val][
'table_element'];
489 $param_array = array(
'', $newval);
490 if ($class ==
'AccountingAccount') {
503 $param_array = array(
'', $newval, 0, $arrayrecord[0][
'val']);
506 $result = call_user_func_array(array($classinstance, $method), $param_array);
509 if (!($classinstance->id !=
'') && $result == -2) {
510 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMultipleRecordFoundFromRef', $newval);
511 $this->errors[$error][
'type'] =
'FOREIGNKEY';
512 $errorforthistable++;
517 if (!($classinstance->id !=
'') && $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeorlabel') {
518 $param_array = array(
'',
'', $newval);
519 call_user_func_array(array($classinstance, $method), $param_array);
521 $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_'][$newval] = $classinstance->id;
524 if ($classinstance->id !=
'') {
525 $newval = $classinstance->id;
526 } elseif (! $error) {
527 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
528 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn', $key, $newval,
'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
529 } elseif (!empty($objimport->array_import_convertvalue[0][$val][
'element'])) {
530 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldRefNotIn', $key, $newval, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'element']));
532 $this->errors[$error][
'lib'] =
'ErrorBadDefinitionOfImportProfile';
534 $this->errors[$error][
'type'] =
'FOREIGNKEY';
535 $errorforthistable++;
540 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeandlabel') {
542 if (!is_numeric($newval) && $newval !=
'' && !preg_match(
'/^id:/i', $newval)) {
545 $newval = preg_replace(
'/^(id|ref):/i',
'', $newval);
547 if ($isidorref ==
'ref') {
548 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
549 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
550 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
551 $codefromfield = $objimport->array_import_convertvalue[0][$val][
'codefromfield'];
552 $code = $arrayrecord[$arrayfield[$codefromfield]][
'val'];
553 if ($this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $code][$newval] !=
'') {
554 $newval = $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $code][$newval];
557 if (empty($resultload)) {
558 dol_print_error(
'',
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method .
', code=' . $code);
561 $classinstance =
new $class($this->
db);
563 $param_array = array(
'', $newval, $code);
564 call_user_func_array(array($classinstance, $method), $param_array);
565 $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $code][$newval] = $classinstance->id;
566 if ($classinstance->id > 0) {
567 $newval = $classinstance->id;
569 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
570 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn', $key, $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
572 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
574 $this->errors[$error][
'type'] =
'FOREIGNKEY';
575 $errorforthistable++;
580 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'zeroifnull') {
581 if (empty($newval)) {
584 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits' || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchscalefromcodeunits') {
585 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
586 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
587 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
588 $units = $objimport->array_import_convertvalue[0][$val][
'units'];
589 if ($this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $units][$newval] !=
'') {
590 $newval = $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $units][$newval];
593 if (empty($resultload)) {
594 dol_print_error(
'',
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method .
', units=' . $units);
597 $classinstance =
new $class($this->
db);
599 call_user_func_array(array($classinstance, $method), array(
'',
'', $newval, $units));
600 $scaleorid = (($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits') ? $classinstance->id : $classinstance->scale);
601 $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $units][$newval] = $scaleorid;
603 if ($classinstance->id > 0) {
604 $newval = $scaleorid ? $scaleorid : 0;
606 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
607 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn', $key, $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
609 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
611 $this->errors[$error][
'type'] =
'FOREIGNKEY';
612 $errorforthistable++;
616 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomercodeifauto') {
617 if (strtolower($newval) ==
'auto') {
618 $this->thirdpartyobject->get_codeclient(0, 0);
619 $newval = $this->thirdpartyobject->code_client;
622 if (empty($newval)) {
623 $arrayrecord[($key)][
'type'] = -1;
625 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsuppliercodeifauto') {
626 if (strtolower($newval) ==
'auto') {
627 $this->thirdpartyobject->get_codefournisseur(0, 1);
628 $newval = $this->thirdpartyobject->code_fournisseur;
631 if (empty($newval)) {
632 $arrayrecord[($key)][
'type'] = -1;
634 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomeraccountancycodeifauto') {
635 if (strtolower($newval) ==
'auto') {
636 $this->thirdpartyobject->get_codecompta(
'customer');
637 $newval = $this->thirdpartyobject->code_compta;
640 if (empty($newval)) {
641 $arrayrecord[($key)][
'type'] = -1;
643 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsupplieraccountancycodeifauto') {
644 if (strtolower($newval) ==
'auto') {
645 $this->thirdpartyobject->get_codecompta(
'supplier');
646 $newval = $this->thirdpartyobject->code_compta_fournisseur;
647 if (empty($newval)) {
648 $arrayrecord[($key)][
'type'] = -1;
652 if (empty($newval)) {
653 $arrayrecord[($key)][
'type'] = -1;
655 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getrefifauto') {
656 if (strtolower($newval) ==
'auto') {
659 $classModForNumber = $objimport->array_import_convertvalue[0][$val][
'class'];
660 $pathModForNumber = $objimport->array_import_convertvalue[0][$val][
'path'];
662 if (!empty($classModForNumber) && !empty($pathModForNumber) && is_readable(DOL_DOCUMENT_ROOT.$pathModForNumber)) {
663 require_once DOL_DOCUMENT_ROOT.$pathModForNumber;
664 $modForNumber =
new $classModForNumber;
668 if (!empty($objimport->array_import_convertvalue[0][$val][
'classobject'])) {
669 $pathForObject = $objimport->array_import_convertvalue[0][$val][
'pathobject'];
670 require_once DOL_DOCUMENT_ROOT.$pathForObject;
671 $tmpclassobject = $objimport->array_import_convertvalue[0][$val][
'classobject'];
672 $tmpobject =
new $tmpclassobject($this->
db);
673 foreach ($arrayfield as $tmpkey => $tmpval) {
674 if (in_array($tmpkey, array(
't.date',
'c.date_commande'))) {
675 $tmpobject->date =
dol_stringtotime($arrayrecord[$arrayfield[$tmpkey]][
'val'], 1);
680 $defaultref = $modForNumber->getNextValue(
null, $tmpobject);
682 if (is_numeric($defaultref) && $defaultref <= 0) {
685 $newval = $defaultref;
687 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'compute') {
688 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
689 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
690 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
692 if (empty($resultload)) {
693 dol_print_error(
'',
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method);
696 $classinstance =
new $class($this->
db);
697 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $listfields, $key));
699 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'numeric') {
701 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'accountingaccount') {
702 if (empty($conf->global->ACCOUNTING_MANAGE_ZERO)) {
703 $newval = rtrim(trim($newval),
"0");
705 $newval = trim($newval);
713 if (!empty($objimport->array_import_regex[0][$val]) && ($newval !=
'')) {
716 if (preg_match(
'/^(.+)@([^:]+)(:.+)?$/', $objimport->array_import_regex[0][$val], $reg)) {
719 $filter = !empty($reg[3]) ? substr($reg[3], 1) :
'';
721 $cachekey = $field .
'@' . $table;
722 if (!empty($filter)) {
723 $cachekey .=
':' . $filter;
727 if (!is_array($this->cachefieldtable[$cachekey])) {
728 $sql =
"SELECT " . $field .
" as aliasfield FROM " . $table;
729 if (!empty($filter)) {
730 $sql .=
' WHERE ' . $filter;
738 $obj = $this->
db->fetch_object(
$resql);
740 $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
750 if (!is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey])) {
751 $tableforerror = $table;
752 if (!empty($filter)) {
753 $tableforerror .=
':' . $filter;
755 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorFieldValueNotIn', $key, $newval, $field, $tableforerror);
756 $this->errors[$error][
'type'] =
'FOREIGNKEY';
757 $errorforthistable++;
760 } elseif (!preg_match(
'/' . $objimport->array_import_regex[0][$val] .
'/i', $newval)) {
763 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorWrongValueForField', $key, $newval, $objimport->array_import_regex[0][$val]);
764 $this->errors[$error][
'type'] =
'REGEX';
765 $errorforthistable++;
773 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorHtmlInjectionForField', $key,
dol_trunc($newval, 100));
774 $this->errors[$error][
'type'] =
'HTMLINJECTION';
775 $errorforthistable++;
784 if (
isModEnabled(
"socialnetworks") && strpos($fieldname,
"socialnetworks") !==
false) {
785 if (!in_array(
"socialnetworks", $listfields)) {
786 $listfields[] =
"socialnetworks";
787 $socialkey = array_search(
"socialnetworks", $listfields);
788 $listvalues[$socialkey] =
'';
790 if (!empty($newval) && $arrayrecord[($key)][
'type'] > 0) {
791 $socialkey = array_search(
"socialnetworks", $listfields);
792 $socialnetwork = explode(
"_", $fieldname)[1];
793 if (empty($listvalues[$socialkey]) || $listvalues[$socialkey] ==
"null") {
794 $json =
new stdClass();
795 $json->$socialnetwork = $newval;
796 $listvalues[$socialkey] = json_encode($json);
798 $jsondata = $listvalues[$socialkey];
799 $json = json_decode($jsondata);
800 $json->$socialnetwork = $newval;
801 $listvalues[$socialkey] = json_encode($json);
805 $listfields[] = $fieldname;
808 if (empty($newval) && $arrayrecord[($key)][
'type'] < 0) {
809 $listvalues[] = ($newval ==
'0' ? $newval :
"null");
810 } elseif (empty($newval) && $arrayrecord[($key)][
'type'] == 0) {
811 $listvalues[] =
"''";
813 $listvalues[] =
"'".$this->db->escape($newval).
"'";
823 if (!empty($listfields) && is_array($objimport->array_import_fieldshidden[0])) {
825 foreach ($objimport->array_import_fieldshidden[0] as $key => $val) {
826 if (!preg_match(
'/^'.preg_quote($alias,
'/').
'\./', $key)) {
829 if ($val ==
'user->id') {
830 $listfields[] = preg_replace(
'/^' . preg_quote($alias,
'/') .
'\./',
'', $key);
831 $listvalues[] = ((int) $user->id);
832 } elseif (preg_match(
'/^lastrowid-/', $val)) {
833 $tmp = explode(
'-', $val);
834 $lastinsertid = (isset($last_insert_id_array[$tmp[1]])) ? $last_insert_id_array[$tmp[1]] : 0;
835 $keyfield = preg_replace(
'/^' . preg_quote($alias,
'/') .
'\./',
'', $key);
836 $listfields[] = $keyfield;
837 $listvalues[] = $lastinsertid;
839 } elseif (preg_match(
'/^const-/', $val)) {
840 $tmp = explode(
'-', $val, 2);
841 $listfields[] = preg_replace(
'/^' . preg_quote($alias,
'/') .
'\./',
'', $key);
842 $listvalues[] =
"'".$this->db->escape($tmp[1]).
"'";
843 } elseif (preg_match(
'/^rule-/', $val)) {
845 if (!empty($objimport->array_import_convertvalue[0][$fieldname])) {
846 if ($objimport->array_import_convertvalue[0][$fieldname][
'rule'] ==
'compute') {
847 $file = (empty($objimport->array_import_convertvalue[0][$fieldname][
'classfile']) ? $objimport->array_import_convertvalue[0][$fieldname][
'file'] : $objimport->array_import_convertvalue[0][$fieldname][
'classfile']);
848 $class = $objimport->array_import_convertvalue[0][$fieldname][
'class'];
849 $method = $objimport->array_import_convertvalue[0][$fieldname][
'method'];
851 if (empty($resultload)) {
852 dol_print_error(
'',
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method);
855 $classinstance =
new $class($this->
db);
856 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $listfields, $key));
857 $fieldArr = explode(
'.', $fieldname);
858 if (count($fieldArr) > 0) {
859 $fieldname = $fieldArr[1];
861 $listfields[] = $fieldname;
862 $listvalues[] = $res;
866 $this->errors[$error][
'lib'] =
'Bad value of profile setup ' . $val .
' for array_import_fieldshidden';
867 $this->errors[$error][
'type'] =
'Import profile setup';
876 if (!$errorforthistable) {
878 if (!empty($listfields)) {
882 $is_table_category_link =
false;
884 if (strpos($tablename,
'_categorie_') !==
false) {
885 $is_table_category_link =
true;
889 if (!empty($updatekeys)) {
892 if (empty($lastinsertid)) {
893 $sqlSelect =
"SELECT ".$fname.
" FROM " . $tablename;
895 $data = array_combine($listfields, $listvalues);
899 foreach ($updatekeys as $key) {
900 $col = $objimport->array_import_updatekeys[0][$key];
901 $key = preg_replace(
'/^.*\./i',
'', $key);
902 if (
isModEnabled(
"socialnetworks") && strpos($key,
"socialnetworks") !==
false) {
903 $tmp = explode(
"_", $key);
905 $socialnetwork = $tmp[1];
906 $jsondata = $data[$key];
907 $json = json_decode($jsondata);
908 $stringtosearch = json_encode($socialnetwork).
':'.json_encode($json->$socialnetwork);
911 $where[] = $key.
" LIKE '%".$this->
db->escape($this->
db->escapeforlike($stringtosearch)).
"%'";
912 $filters[] = $col.
" LIKE '%".$this->
db->escape($this->
db->escapeforlike($stringtosearch)).
"%'";
915 $where[] = $key.
' = '.$data[$key];
916 $filters[] = $col.
' = '.$data[$key];
919 $sqlSelect .=
" WHERE " . implode(
' AND ', $where);
921 $resql = $this->
db->query($sqlSelect);
923 $num_rows = $this->
db->num_rows(
$resql);
924 if ($num_rows == 1) {
925 $res = $this->
db->fetch_object(
$resql);
926 $lastinsertid = $res->rowid;
927 if ($is_table_category_link) $lastinsertid =
'linktable';
928 $last_insert_id_array[$tablename] = $lastinsertid;
929 } elseif ($num_rows > 1) {
930 $this->errors[$error][
'lib'] = $langs->trans(
'MultipleRecordFoundWithTheseFilters', implode(
', ', $filters));
931 $this->errors[$error][
'type'] =
'SQL';
938 $this->errors[$error][
'lib'] = $this->
db->lasterror();
939 $this->errors[$error][
'type'] =
'SQL';
948 $sqlSelect =
"SELECT rowid FROM " . $tablename;
951 if (empty($keyfield)) {
954 $sqlSelect .=
" WHERE ".$keyfield.
" = ".((int) $lastinsertid);
956 $resql = $this->
db->query($sqlSelect);
958 $res = $this->
db->fetch_object(
$resql);
959 if ($this->
db->num_rows(
$resql) == 1) {
968 $this->errors[$error][
'lib'] = $this->
db->lasterror();
969 $this->errors[$error][
'type'] =
'SQL';
974 if (!empty($lastinsertid)) {
976 if (in_array(
"socialnetworks", $listfields)) {
977 $socialkey = array_search(
"socialnetworks", $listfields);
978 $tmpsql = $listvalues[$socialkey];
979 $listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
983 $sqlstart =
"UPDATE " . $tablename;
985 $data = array_combine($listfields, $listvalues);
987 foreach ($data as $key => $val) {
988 $set[] = $key.
" = ".$val;
990 $sqlstart .=
" SET " . implode(
', ', $set);
992 if (empty($keyfield)) {
995 $sqlend =
" WHERE " . $keyfield .
" = ".((int) $lastinsertid);
997 if ($is_table_category_link) {
998 $sqlend =
" WHERE " . implode(
' AND ', $where);
1001 $sql = $sqlstart . $sqlend;
1010 $this->errors[$error][
'lib'] = $this->
db->lasterror();
1011 $this->errors[$error][
'type'] =
'SQL';
1018 if (!$error && !$updatedone) {
1020 if (in_array(
"socialnetworks", $listfields)) {
1021 $socialkey = array_search(
"socialnetworks", $listfields);
1022 $tmpsql = $listvalues[$socialkey];
1023 $listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
1027 $sqlstart =
"INSERT INTO " . $tablename .
"(" . implode(
", ", $listfields) .
", import_key";
1028 $sqlend =
") VALUES(" . implode(
', ', $listvalues) .
", '" . $this->
db->escape($importid) .
"'";
1029 if (!empty($tablewithentity_cache[$tablename])) {
1030 $sqlstart .=
", entity";
1031 $sqlend .=
", " . $conf->entity;
1033 if (!empty($objimport->array_import_tables_creator[0][$alias])) {
1034 $sqlstart .=
", " . $objimport->array_import_tables_creator[0][$alias];
1035 $sqlend .=
", " . $user->id;
1037 $sql = $sqlstart . $sqlend .
")";
1044 if (!$is_table_category_link) {
1045 $last_insert_id_array[$tablename] = $this->
db->last_insert_id($tablename);
1050 $this->errors[$error][
'lib'] = $this->
db->lasterror();
1051 $this->errors[$error][
'type'] =
'SQL';
Class to import Excel files.
__construct($db, $datatoimport)
Constructor.
write_record_example($outputlangs, $contentlinevalues)
Output record of an example file for this format.
import_open_file($file)
Open input file.
import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
Insert a record into database.
write_footer_example($outputlangs)
Output footer of an example file for this format.
import_read_header()
Input header line from file.
write_header_example($outputlangs)
Output header of an example file for this format.
import_read_record()
Return array of next record in input file.
write_title_example($outputlangs, $headerlinefields)
Output title line of an example file for this format.
import_get_nb_of_lines($file)
Return nb of records.
import_close_file()
Close file handle.
Parent class for import file readers.
Class to manage third parties objects (customers, suppliers, prospects...)
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
num2Alpha($n)
Return a numeric value into an Excel like column number.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
testSqlAndScriptInject($val, $type)
Security: WAF layer for SQL Injection and XSS Injection (scripts) protection (Filters on GET,...
$conf db
API class for accounts.