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->thirpartyobject =
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']);
505 call_user_func_array(array($classinstance, $method), $param_array);
507 if (!($classinstance->id !=
'') && $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeorlabel') {
508 $param_array = array(
'',
'', $newval);
509 call_user_func_array(array($classinstance, $method), $param_array);
511 $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_'][$newval] = $classinstance->id;
513 if ($classinstance->id !=
'') {
514 $newval = $classinstance->id;
516 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
517 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn', $key, $newval,
'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
518 } elseif (!empty($objimport->array_import_convertvalue[0][$val][
'element'])) {
519 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldRefNotIn', $key, $newval, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'element']));
521 $this->errors[$error][
'lib'] =
'ErrorBadDefinitionOfImportProfile';
523 $this->errors[$error][
'type'] =
'FOREIGNKEY';
524 $errorforthistable++;
529 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeandlabel') {
531 if (!is_numeric($newval) && $newval !=
'' && !preg_match(
'/^id:/i', $newval)) {
534 $newval = preg_replace(
'/^(id|ref):/i',
'', $newval);
536 if ($isidorref ==
'ref') {
537 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
538 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
539 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
540 $codefromfield = $objimport->array_import_convertvalue[0][$val][
'codefromfield'];
541 $code = $arrayrecord[$arrayfield[$codefromfield]][
'val'];
542 if ($this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $code][$newval] !=
'') {
543 $newval = $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $code][$newval];
546 if (empty($resultload)) {
547 dol_print_error(
'',
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method .
', code=' . $code);
550 $classinstance =
new $class($this->
db);
552 $param_array = array(
'', $newval, $code);
553 call_user_func_array(array($classinstance, $method), $param_array);
554 $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $code][$newval] = $classinstance->id;
555 if ($classinstance->id > 0) {
556 $newval = $classinstance->id;
558 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
559 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn', $key, $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
561 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
563 $this->errors[$error][
'type'] =
'FOREIGNKEY';
564 $errorforthistable++;
569 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'zeroifnull') {
570 if (empty($newval)) {
573 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits' || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchscalefromcodeunits') {
574 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
575 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
576 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
577 $units = $objimport->array_import_convertvalue[0][$val][
'units'];
578 if ($this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $units][$newval] !=
'') {
579 $newval = $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $units][$newval];
582 if (empty($resultload)) {
583 dol_print_error(
'',
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method .
', units=' . $units);
586 $classinstance =
new $class($this->
db);
588 call_user_func_array(array($classinstance, $method), array(
'',
'', $newval, $units));
589 $scaleorid = (($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits') ? $classinstance->id : $classinstance->scale);
590 $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $units][$newval] = $scaleorid;
592 if ($classinstance->id > 0) {
593 $newval = $scaleorid ? $scaleorid : 0;
595 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
596 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn', $key, $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
598 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
600 $this->errors[$error][
'type'] =
'FOREIGNKEY';
601 $errorforthistable++;
605 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomercodeifauto') {
606 if (strtolower($newval) ==
'auto') {
607 $this->thirpartyobject->get_codeclient(0, 0);
608 $newval = $this->thirpartyobject->code_client;
611 if (empty($newval)) {
612 $arrayrecord[($key)][
'type'] = -1;
614 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsuppliercodeifauto') {
615 if (strtolower($newval) ==
'auto') {
616 $newval = $this->thirpartyobject->get_codefournisseur(0, 1);
617 $newval = $this->thirpartyobject->code_fournisseur;
620 if (empty($newval)) {
621 $arrayrecord[($key)][
'type'] = -1;
623 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomeraccountancycodeifauto') {
624 if (strtolower($newval) ==
'auto') {
625 $this->thirpartyobject->get_codecompta(
'customer');
626 $newval = $this->thirpartyobject->code_compta;
629 if (empty($newval)) {
630 $arrayrecord[($key)][
'type'] = -1;
632 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsupplieraccountancycodeifauto') {
633 if (strtolower($newval) ==
'auto') {
634 $this->thirpartyobject->get_codecompta(
'supplier');
635 $newval = $this->thirpartyobject->code_compta_fournisseur;
636 if (empty($newval)) {
637 $arrayrecord[($key)][
'type'] = -1;
641 if (empty($newval)) {
642 $arrayrecord[($key)][
'type'] = -1;
644 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getrefifauto') {
645 if (strtolower($newval) ==
'auto') {
648 $classModForNumber = $objimport->array_import_convertvalue[0][$val][
'class'];
649 $pathModForNumber = $objimport->array_import_convertvalue[0][$val][
'path'];
651 if (!empty($classModForNumber) && !empty($pathModForNumber) && is_readable(DOL_DOCUMENT_ROOT.$pathModForNumber)) {
652 require_once DOL_DOCUMENT_ROOT.$pathModForNumber;
653 $modForNumber =
new $classModForNumber;
657 if (!empty($objimport->array_import_convertvalue[0][$val][
'classobject'])) {
658 $pathForObject = $objimport->array_import_convertvalue[0][$val][
'pathobject'];
659 require_once DOL_DOCUMENT_ROOT.$pathForObject;
660 $tmpclassobject = $objimport->array_import_convertvalue[0][$val][
'classobject'];
661 $tmpobject =
new $tmpclassobject($this->
db);
662 foreach ($arrayfield as $tmpkey => $tmpval) {
663 if (in_array($tmpkey, array(
't.date',
'c.date_commande'))) {
664 $tmpobject->date =
dol_stringtotime($arrayrecord[$arrayfield[$tmpkey]][
'val'], 1);
669 $defaultref = $modForNumber->getNextValue(null, $tmpobject);
671 if (is_numeric($defaultref) && $defaultref <= 0) {
674 $newval = $defaultref;
676 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'compute') {
677 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
678 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
679 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
681 if (empty($resultload)) {
682 dol_print_error(
'',
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method);
685 $classinstance =
new $class($this->
db);
686 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $listfields, $key));
688 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'numeric') {
690 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'accountingaccount') {
691 if (empty($conf->global->ACCOUNTING_MANAGE_ZERO)) {
692 $newval = rtrim(trim($newval),
"0");
694 $newval = trim($newval);
702 if (!empty($objimport->array_import_regex[0][$val]) && ($newval !=
'')) {
705 if (preg_match(
'/^(.+)@([^:]+)(:.+)?$/', $objimport->array_import_regex[0][$val], $reg)) {
708 $filter = !empty($reg[3]) ? substr($reg[3], 1) :
'';
710 $cachekey = $field .
'@' . $table;
711 if (!empty($filter)) {
712 $cachekey .=
':' . $filter;
716 if (!is_array($this->cachefieldtable[$cachekey])) {
717 $sql =
"SELECT " . $field .
" as aliasfield FROM " . $table;
718 if (!empty($filter)) {
719 $sql .=
' WHERE ' . $filter;
727 $obj = $this->
db->fetch_object(
$resql);
729 $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
739 if (!is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey])) {
740 $tableforerror = $table;
741 if (!empty($filter)) {
742 $tableforerror .=
':' . $filter;
744 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorFieldValueNotIn', $key, $newval, $field, $tableforerror);
745 $this->errors[$error][
'type'] =
'FOREIGNKEY';
746 $errorforthistable++;
749 } elseif (!preg_match(
'/' . $objimport->array_import_regex[0][$val] .
'/i', $newval)) {
752 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorWrongValueForField', $key, $newval, $objimport->array_import_regex[0][$val]);
753 $this->errors[$error][
'type'] =
'REGEX';
754 $errorforthistable++;
762 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorHtmlInjectionForField', $key,
dol_trunc($newval, 100));
763 $this->errors[$error][
'type'] =
'HTMLINJECTION';
764 $errorforthistable++;
773 if ($conf->socialnetworks->enabled && strpos($fieldname,
"socialnetworks") !==
false) {
774 if (!in_array(
"socialnetworks", $listfields)) {
775 $listfields[] =
"socialnetworks";
777 if (!empty($newval) && $arrayrecord[($key)][
'type'] > 0) {
778 $socialkey = array_search(
"socialnetworks", $listfields);
779 $socialnetwork = explode(
"_", $fieldname)[1];
780 if (empty($listvalues[$socialkey]) || $listvalues[$socialkey] ==
"null") {
781 $json =
new stdClass();
782 $json->$socialnetwork = $newval;
783 $listvalues[$socialkey] = json_encode($json);
785 $jsondata = $listvalues[$socialkey];
786 $json = json_decode($jsondata);
787 $json->$socialnetwork = $newval;
788 $listvalues[$socialkey] = json_encode($json);
792 $listfields[] = $fieldname;
795 if (empty($newval) && $arrayrecord[($key)][
'type'] < 0) {
796 $listvalues[] = ($newval ==
'0' ? $newval :
"null");
797 } elseif (empty($newval) && $arrayrecord[($key)][
'type'] == 0) {
798 $listvalues[] =
"''";
800 $listvalues[] =
"'" . $this->
db->escape($newval) .
"'";
810 if (!empty($listfields) && is_array($objimport->array_import_fieldshidden[0])) {
812 foreach ($objimport->array_import_fieldshidden[0] as $key => $val) {
813 if (!preg_match(
'/^' . preg_quote($alias,
'/') .
'\./', $key)) {
816 if ($val ==
'user->id') {
817 $listfields[] = preg_replace(
'/^' . preg_quote($alias,
'/') .
'\./',
'', $key);
818 $listvalues[] = ((int) $user->id);
819 } elseif (preg_match(
'/^lastrowid-/', $val)) {
820 $tmp = explode(
'-', $val);
821 $lastinsertid = (isset($last_insert_id_array[$tmp[1]])) ? $last_insert_id_array[$tmp[1]] : 0;
822 $keyfield = preg_replace(
'/^' . preg_quote($alias,
'/') .
'\./',
'', $key);
823 $listfields[] = $keyfield;
824 $listvalues[] = $lastinsertid;
826 } elseif (preg_match(
'/^const-/', $val)) {
827 $tmp = explode(
'-', $val, 2);
828 $listfields[] = preg_replace(
'/^' . preg_quote($alias,
'/') .
'\./',
'', $key);
829 $listvalues[] =
"'".$this->db->escape($tmp[1]).
"'";
830 } elseif (preg_match(
'/^rule-/', $val)) {
832 if (!empty($objimport->array_import_convertvalue[0][$fieldname])) {
833 if ($objimport->array_import_convertvalue[0][$fieldname][
'rule'] ==
'compute') {
834 $file = (empty($objimport->array_import_convertvalue[0][$fieldname][
'classfile']) ? $objimport->array_import_convertvalue[0][$fieldname][
'file'] : $objimport->array_import_convertvalue[0][$fieldname][
'classfile']);
835 $class = $objimport->array_import_convertvalue[0][$fieldname][
'class'];
836 $method = $objimport->array_import_convertvalue[0][$fieldname][
'method'];
838 if (empty($resultload)) {
839 dol_print_error(
'',
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method);
842 $classinstance =
new $class($this->
db);
843 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $listfields, $key));
844 $fieldArr = explode(
'.', $fieldname);
845 if (count($fieldArr) > 0) {
846 $fieldname = $fieldArr[1];
848 $listfields[] = $fieldname;
849 $listvalues[] = $res;
853 $this->errors[$error][
'lib'] =
'Bad value of profile setup ' . $val .
' for array_import_fieldshidden';
854 $this->errors[$error][
'type'] =
'Import profile setup';
863 if (!$errorforthistable) {
865 if (!empty($listfields)) {
869 $is_table_category_link =
false;
871 if (strpos($tablename,
'_categorie_') !==
false) {
872 $is_table_category_link =
true;
876 if (!empty($updatekeys)) {
879 if (empty($lastinsertid)) {
880 $sqlSelect =
"SELECT ".$fname.
" FROM " . $tablename;
882 $data = array_combine($listfields, $listvalues);
885 foreach ($updatekeys as $key) {
886 $col = $objimport->array_import_updatekeys[0][$key];
887 $key = preg_replace(
'/^.*\./i',
'', $key);
888 if ($conf->socialnetworks->enabled && strpos($key,
"socialnetworks") !==
false) {
889 $tmp = explode(
"_", $key);
891 $socialnetwork = $tmp[1];
892 $jsondata = $data[$key];
893 $json = json_decode($jsondata);
894 $stringtosearch = json_encode($socialnetwork).
':'.json_encode($json->$socialnetwork);
897 $where[] = $key.
" LIKE '%".$this->
db->escapeforlike($this->
db->escape($stringtosearch)).
"%'";
898 $filters[] = $col.
" LIKE '%".$this->
db->escapeforlike($this->
db->escape($stringtosearch)).
"%'";
901 $where[] = $key.
' = '.$data[$key];
902 $filters[] = $col.
' = '.$data[$key];
905 $sqlSelect .=
" WHERE " . implode(
' AND ', $where);
907 $resql = $this->
db->query($sqlSelect);
909 $num_rows = $this->
db->num_rows(
$resql);
910 if ($num_rows == 1) {
911 $res = $this->
db->fetch_object(
$resql);
912 $lastinsertid = $res->rowid;
913 if ($is_table_category_link) $lastinsertid =
'linktable';
914 $last_insert_id_array[$tablename] = $lastinsertid;
915 } elseif ($num_rows > 1) {
916 $this->errors[$error][
'lib'] = $langs->trans(
'MultipleRecordFoundWithTheseFilters', implode(
', ', $filters));
917 $this->errors[$error][
'type'] =
'SQL';
924 $this->errors[$error][
'lib'] = $this->
db->lasterror();
925 $this->errors[$error][
'type'] =
'SQL';
934 $sqlSelect =
"SELECT rowid FROM " . $tablename;
936 if (empty($keyfield)) {
939 $sqlSelect .=
" WHERE ".$keyfield.
" = ".((int) $lastinsertid);
941 $resql = $this->
db->query($sqlSelect);
943 $res = $this->
db->fetch_object(
$resql);
944 if ($this->
db->num_rows(
$resql) == 1) {
953 $this->errors[$error][
'lib'] = $this->
db->lasterror();
954 $this->errors[$error][
'type'] =
'SQL';
959 if (!empty($lastinsertid)) {
961 if (in_array(
"socialnetworks", $listfields)) {
962 $socialkey = array_search(
"socialnetworks", $listfields);
963 $tmpsql = $listvalues[$socialkey];
964 $listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
968 $sqlstart =
"UPDATE " . $tablename;
970 $data = array_combine($listfields, $listvalues);
972 foreach ($data as $key => $val) {
973 $set[] = $key.
" = ".$val;
975 $sqlstart .=
" SET " . implode(
', ', $set);
977 if (empty($keyfield)) {
980 $sqlend =
" WHERE " . $keyfield .
" = ".((int) $lastinsertid);
982 if ($is_table_category_link) {
983 $sqlend =
" WHERE " . implode(
' AND ', $where);
986 $sql = $sqlstart . $sqlend;
995 $this->errors[$error][
'lib'] = $this->
db->lasterror();
996 $this->errors[$error][
'type'] =
'SQL';
1003 if (!$error && !$updatedone) {
1005 if (in_array(
"socialnetworks", $listfields)) {
1006 $socialkey = array_search(
"socialnetworks", $listfields);
1007 $tmpsql = $listvalues[$socialkey];
1008 $listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
1012 $sqlstart =
"INSERT INTO " . $tablename .
"(" . implode(
", ", $listfields) .
", import_key";
1013 $sqlend =
") VALUES(" . implode(
', ', $listvalues) .
", '" . $this->
db->escape($importid) .
"'";
1014 if (!empty($tablewithentity_cache[$tablename])) {
1015 $sqlstart .=
", entity";
1016 $sqlend .=
", " . $conf->entity;
1018 if (!empty($objimport->array_import_tables_creator[0][$alias])) {
1019 $sqlstart .=
", " . $objimport->array_import_tables_creator[0][$alias];
1020 $sqlend .=
", " . $user->id;
1022 $sql = $sqlstart . $sqlend .
")";
1029 $last_insert_id_array[$tablename] = $this->
db->last_insert_id($tablename);
1033 $this->errors[$error][
'lib'] = $this->
db->lasterror();
1034 $this->errors[$error][
'type'] =
'SQL';
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname= '')
Make an include_once using default root and alternate root if it fails.
import_open_file($file)
Open input file.
$conf db
API class for accounts.
import_read_header()
Input header line from file.
Class to import Excel files.
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...
num2Alpha($n)
Return a numeric value into an Excel like column number.
write_footer_example($outputlangs)
Output footer of an example file for this format.
import_get_nb_of_lines($file)
Return nb of records.
import_read_record()
Return array of next record in input file.
write_record_example($outputlangs, $contentlinevalues)
Output record of an example file for this format.
Class to manage third parties objects (customers, suppliers, prospects...)
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...
__construct($db, $datatoimport)
Constructor.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
Insert a record into database.
if(isModEnabled('facture')&&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur')&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)&&$user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice')&&$user->rights->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->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
write_header_example($outputlangs)
Output header of an example file for this format.
write_title_example($outputlangs, $headerlinefields)
Output title line of an example file for this format.
Parent class for import file readers.
testSqlAndScriptInject($val, $type)
Security: WAF layer for SQL Injection and XSS Injection (scripts) protection (Filters on GET...
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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. ...
import_close_file()
Close file handle.