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;
110 parent::__construct();
115 $this->label =
'Excel 2007';
116 $this->desc = $langs->trans(
"Excel2007FormatDesc");
117 $this->extension =
'xlsx';
118 $this->picto =
'mime/xls';
119 $this->version =
'1.0';
122 require_once DOL_DOCUMENT_ROOT.
'/includes/phpoffice/phpspreadsheet/src/autoloader.php';
123 require_once DOL_DOCUMENT_ROOT.
'/includes/Psr/autoloader.php';
124 require_once PHPEXCELNEW_PATH.
'Spreadsheet.php';
125 $this->workbook =
new Spreadsheet();
128 if (!class_exists(
'ZipArchive')) {
129 $langs->load(
"errors");
130 $this->error = $langs->trans(
'ErrorPHPNeedModule',
'zip');
133 $this->label_lib =
'PhpSpreadSheet';
134 $this->version_lib =
'1.8.0';
136 $this->datatoimport = $datatoimport;
137 if (preg_match(
'/^societe_/', $datatoimport)) {
138 $this->thirdpartyobject =
new Societe($this->db);
153 global $user, $conf, $langs, $file;
155 $this->workbook->getProperties()->setCreator($user->getFullName($outputlangs) .
' - Dolibarr ' . DOL_VERSION);
156 $this->workbook->getProperties()->setTitle($outputlangs->trans(
"Import") .
' - ' . $file);
157 $this->workbook->getProperties()->setSubject($outputlangs->trans(
"Import") .
' - ' . $file);
158 $this->workbook->getProperties()->setDescription($outputlangs->trans(
"Import") .
' - ' . $file);
160 $this->workbook->setActiveSheetIndex(0);
161 $this->workbook->getActiveSheet()->setTitle($outputlangs->trans(
"Sheet"));
162 $this->workbook->getActiveSheet()->getDefaultRowDimension()->setRowHeight(16);
179 $this->workbook->getActiveSheet()->getStyle(
'1')->getFont()->setBold(
true);
180 $this->workbook->getActiveSheet()->getStyle(
'1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT);
183 foreach ($headerlinefields as $field) {
184 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($col, 1, $outputlangs->transnoentities($field));
206 foreach ($contentlinevalues as $cell) {
207 $this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($col, $row, $cell);
225 $tempfile = tempnam(sys_get_temp_dir(),
'dol');
226 $objWriter =
new PhpOffice\PhpSpreadsheet\Writer\Xlsx($this->workbook);
227 $objWriter->save($tempfile);
228 $this->workbook->disconnectWorksheets();
229 unset($this->workbook);
231 $content = file_get_contents($tempfile);
251 dol_syslog(get_class($this) .
"::open_file file=" . $file);
253 $reader =
new Xlsx();
254 $this->workbook = $reader->load($file);
272 $reader =
new Xlsx();
273 $this->workbook = $reader->load($file);
275 $rowcount = $this->workbook->getActiveSheet()->getHighestDataRow();
277 $this->workbook->disconnectWorksheets();
278 unset($this->workbook);
294 $this->headers = array();
296 $info = $xlsx->listWorksheetinfo($this->file);
297 $countcolumns = $info[0][
'totalColumns'];
298 for ($col = 1; $col <= $countcolumns; $col++) {
299 $this->headers[$col] = $this->workbook->getActiveSheet()->getCellByColumnAndRow($col, 1)->getValue();
316 $rowcount = $this->workbook->getActiveSheet()->getHighestDataRow();
317 if ($this->record > $rowcount) {
322 $info = $xlsx->listWorksheetinfo($this->file);
323 $countcolumns = $info[0][
'totalColumns'];
324 for ($col = 1; $col <= $countcolumns; $col++) {
325 $val = $this->workbook->getActiveSheet()->getCellByColumnAndRow($col, $this->record)->getValue();
326 $array[$col][
'val'] = $val;
327 $array[$col][
'type'] = (
dol_strlen($val) ? 1 : -1);
342 $this->workbook->disconnectWorksheets();
343 unset($this->workbook);
361 public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
364 global $langs, $conf, $user;
365 global $thirdparty_static;
366 global $tablewithentity_cache;
370 $this->errors = array();
371 $this->warnings = array();
378 $array_match_database_to_file = array_flip($array_match_file_to_database);
379 $sort_array_match_file_to_database = $array_match_file_to_database;
380 ksort($sort_array_match_file_to_database);
384 if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[1][
'val']))) {
386 $this->warnings[$warning][
'lib'] = $langs->trans(
'EmptyLine');
387 $this->warnings[$warning][
'type'] =
'EMPTY';
390 $last_insert_id_array = array();
394 foreach ($objimport->array_import_tables[0] as $alias => $tablename) {
397 $listfields = array();
398 $listvalues = array();
400 $errorforthistable = 0;
403 if (!isset($tablewithentity_cache[$tablename])) {
404 dol_syslog(
"Check if table " . $tablename .
" has an entity field");
405 $resql = $this->db->DDLDescTable($tablename,
'entity');
407 $obj = $this->db->fetch_object($resql);
409 $tablewithentity_cache[$tablename] = 1;
411 $tablewithentity_cache[$tablename] = 0;
421 $arrayfield = array();
422 foreach ($sort_array_match_file_to_database as $key => $val) {
423 $arrayfield[$val] = ($key);
430 foreach ($sort_array_match_file_to_database as $key => $val) {
431 $fieldalias = preg_replace(
'/\..*$/i',
'', $val);
432 $fieldname = preg_replace(
'/^.*\./i',
'', $val);
434 if ($alias != $fieldalias) {
438 if ($key <= $maxfields) {
441 if ($arrayrecord[($key)][
'type'] > 0) {
442 $newval = $arrayrecord[($key)][
'val'];
451 if (preg_match(
'/\*/', $objimport->array_import_fields[0][$val]) && ((string) $newval ==
'')) {
452 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMissingMandatoryValue',
num2Alpha($key - 1));
453 $this->errors[$error][
'type'] =
'NOTNULL';
454 $errorforthistable++;
459 if (!empty($objimport->array_import_convertvalue[0][$val])) {
461 if ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeid'
462 || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromref'
463 || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeorlabel'
467 if (!is_numeric($newval) && $newval !=
'' && !preg_match(
'/^id:/i', $newval)) {
470 $newval = preg_replace(
'/^(id|ref):/i',
'', $newval);
473 if ($isidorref ==
'ref') {
474 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
475 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
476 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
477 if ($this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_'][$newval] !=
'') {
478 $newval = $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_'][$newval];
481 if (empty($resultload)) {
482 dol_print_error(
'',
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method);
485 $classinstance =
new $class($this->db);
486 if ($class ==
'CGenericDic') {
487 $classinstance->element = $objimport->array_import_convertvalue[0][$val][
'element'];
488 $classinstance->table_element = $objimport->array_import_convertvalue[0][$val][
'table_element'];
492 $param_array = array(
'', $newval);
493 if ($class ==
'AccountingAccount') {
506 $param_array = array(
'', $newval, 0, $arrayrecord[0][
'val']);
509 $result = call_user_func_array(array($classinstance, $method), $param_array);
512 if (!($classinstance->id !=
'') && $result == -2) {
513 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMultipleRecordFoundFromRef', $newval);
514 $this->errors[$error][
'type'] =
'FOREIGNKEY';
515 $errorforthistable++;
520 if (!($classinstance->id !=
'') && $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeorlabel') {
521 $param_array = array(
'',
'', $newval);
522 call_user_func_array(array($classinstance, $method), $param_array);
524 $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_'][$newval] = $classinstance->id;
527 if ($classinstance->id !=
'') {
528 $newval = $classinstance->id;
529 } elseif (! $error) {
530 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
531 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn', $key, $newval,
'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
532 } elseif (!empty($objimport->array_import_convertvalue[0][$val][
'element'])) {
533 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldRefNotIn', $key, $newval, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'element']));
535 $this->errors[$error][
'lib'] =
'ErrorBadDefinitionOfImportProfile';
537 $this->errors[$error][
'type'] =
'FOREIGNKEY';
538 $errorforthistable++;
543 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeandlabel') {
545 if (!is_numeric($newval) && $newval !=
'' && !preg_match(
'/^id:/i', $newval)) {
548 $newval = preg_replace(
'/^(id|ref):/i',
'', $newval);
550 if ($isidorref ==
'ref') {
551 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
552 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
553 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
554 $codefromfield = $objimport->array_import_convertvalue[0][$val][
'codefromfield'];
555 $code = $arrayrecord[$arrayfield[$codefromfield]][
'val'];
556 if ($this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $code][$newval] !=
'') {
557 $newval = $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $code][$newval];
560 if (empty($resultload)) {
561 dol_print_error(
'',
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method .
', code=' . $code);
564 $classinstance =
new $class($this->db);
566 $param_array = array(
'', $newval, $code);
567 call_user_func_array(array($classinstance, $method), $param_array);
568 $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $code][$newval] = $classinstance->id;
569 if ($classinstance->id > 0) {
570 $newval = $classinstance->id;
572 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
573 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn', $key, $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
575 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
577 $this->errors[$error][
'type'] =
'FOREIGNKEY';
578 $errorforthistable++;
583 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'zeroifnull') {
584 if (empty($newval)) {
587 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits' || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchscalefromcodeunits') {
588 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
589 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
590 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
591 $units = $objimport->array_import_convertvalue[0][$val][
'units'];
592 if ($this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $units][$newval] !=
'') {
593 $newval = $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $units][$newval];
596 if (empty($resultload)) {
597 dol_print_error(
'',
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method .
', units=' . $units);
600 $classinstance =
new $class($this->db);
602 call_user_func_array(array($classinstance, $method), array(
'',
'', $newval, $units));
603 $scaleorid = (($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits') ? $classinstance->id : $classinstance->scale);
604 $this->cacheconvert[$file .
'_' . $class .
'_' . $method .
'_' . $units][$newval] = $scaleorid;
606 if ($classinstance->id > 0) {
607 $newval = $scaleorid ? $scaleorid : 0;
609 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
610 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn', $key, $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
612 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
614 $this->errors[$error][
'type'] =
'FOREIGNKEY';
615 $errorforthistable++;
619 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomercodeifauto') {
620 if (strtolower($newval) ==
'auto') {
621 $this->thirdpartyobject->get_codeclient(0, 0);
622 $newval = $this->thirdpartyobject->code_client;
625 if (empty($newval)) {
626 $arrayrecord[($key)][
'type'] = -1;
628 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsuppliercodeifauto') {
629 if (strtolower($newval) ==
'auto') {
630 $this->thirdpartyobject->get_codefournisseur(0, 1);
631 $newval = $this->thirdpartyobject->code_fournisseur;
634 if (empty($newval)) {
635 $arrayrecord[($key)][
'type'] = -1;
637 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomeraccountancycodeifauto') {
638 if (strtolower($newval) ==
'auto') {
639 $this->thirdpartyobject->get_codecompta(
'customer');
640 $newval = $this->thirdpartyobject->code_compta;
643 if (empty($newval)) {
644 $arrayrecord[($key)][
'type'] = -1;
646 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsupplieraccountancycodeifauto') {
647 if (strtolower($newval) ==
'auto') {
648 $this->thirdpartyobject->get_codecompta(
'supplier');
649 $newval = $this->thirdpartyobject->code_compta_fournisseur;
650 if (empty($newval)) {
651 $arrayrecord[($key)][
'type'] = -1;
655 if (empty($newval)) {
656 $arrayrecord[($key)][
'type'] = -1;
658 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getrefifauto') {
659 if (strtolower($newval) ==
'auto') {
662 $classModForNumber = $objimport->array_import_convertvalue[0][$val][
'class'];
663 $pathModForNumber = $objimport->array_import_convertvalue[0][$val][
'path'];
665 if (!empty($classModForNumber) && !empty($pathModForNumber) && is_readable(DOL_DOCUMENT_ROOT.$pathModForNumber)) {
666 require_once DOL_DOCUMENT_ROOT.$pathModForNumber;
667 $modForNumber =
new $classModForNumber;
671 if (!empty($objimport->array_import_convertvalue[0][$val][
'classobject'])) {
672 $pathForObject = $objimport->array_import_convertvalue[0][$val][
'pathobject'];
673 require_once DOL_DOCUMENT_ROOT.$pathForObject;
674 $tmpclassobject = $objimport->array_import_convertvalue[0][$val][
'classobject'];
675 $tmpobject =
new $tmpclassobject($this->db);
676 foreach ($arrayfield as $tmpkey => $tmpval) {
677 if (in_array($tmpkey, array(
't.date',
'c.date_commande'))) {
678 $tmpobject->date =
dol_stringtotime($arrayrecord[$arrayfield[$tmpkey]][
'val'], 1);
683 $defaultref = $modForNumber->getNextValue(
null, $tmpobject);
685 if (is_numeric($defaultref) && $defaultref <= 0) {
688 $newval = $defaultref;
690 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'compute') {
691 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
692 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
693 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
695 if (empty($resultload)) {
696 dol_print_error(
'',
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method);
699 $classinstance =
new $class($this->db);
700 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, $key));
702 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'numeric') {
704 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'accountingaccount') {
705 if (empty($conf->global->ACCOUNTING_MANAGE_ZERO)) {
706 $newval = rtrim(trim($newval),
"0");
708 $newval = trim($newval);
716 if (!empty($objimport->array_import_regex[0][$val]) && ($newval !=
'')) {
719 if (preg_match(
'/^(.+)@([^:]+)(:.+)?$/', $objimport->array_import_regex[0][$val], $reg)) {
722 $filter = !empty($reg[3]) ? substr($reg[3], 1) :
'';
724 $cachekey = $field .
'@' . $table;
725 if (!empty($filter)) {
726 $cachekey .=
':' . $filter;
730 if (!is_array($this->cachefieldtable[$cachekey])) {
731 $sql =
"SELECT " . $field .
" as aliasfield FROM " . $table;
732 if (!empty($filter)) {
733 $sql .=
' WHERE ' . $filter;
736 $resql = $this->db->query(
$sql);
738 $num = $this->db->num_rows($resql);
741 $obj = $this->db->fetch_object($resql);
743 $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
753 if (!is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey])) {
754 $tableforerror = $table;
755 if (!empty($filter)) {
756 $tableforerror .=
':' . $filter;
758 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorFieldValueNotIn', $key, $newval, $field, $tableforerror);
759 $this->errors[$error][
'type'] =
'FOREIGNKEY';
760 $errorforthistable++;
763 } elseif (!preg_match(
'/' . $objimport->array_import_regex[0][$val] .
'/i', $newval)) {
766 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorWrongValueForField', $key, $newval, $objimport->array_import_regex[0][$val]);
767 $this->errors[$error][
'type'] =
'REGEX';
768 $errorforthistable++;
776 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorHtmlInjectionForField', $key,
dol_trunc($newval, 100));
777 $this->errors[$error][
'type'] =
'HTMLINJECTION';
778 $errorforthistable++;
787 if (
isModEnabled(
"socialnetworks") && strpos($fieldname,
"socialnetworks") !==
false) {
788 if (!in_array(
"socialnetworks", $listfields)) {
789 $listfields[] =
"socialnetworks";
790 $socialkey = array_search(
"socialnetworks", $listfields);
791 $listvalues[$socialkey] =
'';
793 if (!empty($newval) && $arrayrecord[($key)][
'type'] > 0) {
794 $socialkey = array_search(
"socialnetworks", $listfields);
795 $socialnetwork = explode(
"_", $fieldname)[1];
796 if (empty($listvalues[$socialkey]) || $listvalues[$socialkey] ==
"null") {
797 $json =
new stdClass();
798 $json->$socialnetwork = $newval;
799 $listvalues[$socialkey] = json_encode($json);
801 $jsondata = $listvalues[$socialkey];
802 $json = json_decode($jsondata);
803 $json->$socialnetwork = $newval;
804 $listvalues[$socialkey] = json_encode($json);
808 $listfields[] = $fieldname;
811 if (empty($newval) && $arrayrecord[($key)][
'type'] < 0) {
812 $listvalues[] = ($newval ==
'0' ? $newval :
"null");
813 } elseif (empty($newval) && $arrayrecord[($key)][
'type'] == 0) {
814 $listvalues[] =
"''";
816 $listvalues[] =
"'".$this->db->escape($newval).
"'";
826 if (!empty($listfields) && is_array($objimport->array_import_fieldshidden[0])) {
828 foreach ($objimport->array_import_fieldshidden[0] as $key => $val) {
829 if (!preg_match(
'/^'.preg_quote($alias,
'/').
'\./', $key)) {
832 if ($val ==
'user->id') {
833 $listfields[] = preg_replace(
'/^' . preg_quote($alias,
'/') .
'\./',
'', $key);
834 $listvalues[] = ((int) $user->id);
835 } elseif (preg_match(
'/^lastrowid-/', $val)) {
836 $tmp = explode(
'-', $val);
837 $lastinsertid = (isset($last_insert_id_array[$tmp[1]])) ? $last_insert_id_array[$tmp[1]] : 0;
838 $keyfield = preg_replace(
'/^' . preg_quote($alias,
'/') .
'\./',
'', $key);
839 $listfields[] = $keyfield;
840 $listvalues[] = $lastinsertid;
842 } elseif (preg_match(
'/^const-/', $val)) {
843 $tmp = explode(
'-', $val, 2);
844 $listfields[] = preg_replace(
'/^' . preg_quote($alias,
'/') .
'\./',
'', $key);
845 $listvalues[] =
"'".$this->db->escape($tmp[1]).
"'";
846 } elseif (preg_match(
'/^rule-/', $val)) {
848 if (!empty($objimport->array_import_convertvalue[0][$fieldname])) {
849 if ($objimport->array_import_convertvalue[0][$fieldname][
'rule'] ==
'compute') {
850 $file = (empty($objimport->array_import_convertvalue[0][$fieldname][
'classfile']) ? $objimport->array_import_convertvalue[0][$fieldname][
'file'] : $objimport->array_import_convertvalue[0][$fieldname][
'classfile']);
851 $class = $objimport->array_import_convertvalue[0][$fieldname][
'class'];
852 $method = $objimport->array_import_convertvalue[0][$fieldname][
'method'];
854 if (empty($resultload)) {
855 dol_print_error(
'',
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method);
858 $classinstance =
new $class($this->db);
859 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, $key));
860 $fieldArr = explode(
'.', $fieldname);
861 if (count($fieldArr) > 0) {
862 $fieldname = $fieldArr[1];
864 $listfields[] = $fieldname;
865 $listvalues[] = $res;
869 $this->errors[$error][
'lib'] =
'Bad value of profile setup ' . $val .
' for array_import_fieldshidden';
870 $this->errors[$error][
'type'] =
'Import profile setup';
879 if (!$errorforthistable) {
881 if (!empty($listfields)) {
885 $is_table_category_link =
false;
887 if (strpos($tablename,
'_categorie_') !==
false) {
888 $is_table_category_link =
true;
892 if (!empty($updatekeys)) {
895 if (empty($lastinsertid)) {
896 $sqlSelect =
"SELECT ".$fname.
" FROM " . $tablename;
898 $data = array_combine($listfields, $listvalues);
902 foreach ($updatekeys as $key) {
903 $col = $objimport->array_import_updatekeys[0][$key];
904 $key = preg_replace(
'/^.*\./i',
'', $key);
905 if (
isModEnabled(
"socialnetworks") && strpos($key,
"socialnetworks") !==
false) {
906 $tmp = explode(
"_", $key);
908 $socialnetwork = $tmp[1];
909 $jsondata = $data[$key];
910 $json = json_decode($jsondata);
911 $stringtosearch = json_encode($socialnetwork).
':'.json_encode($json->$socialnetwork);
914 $where[] = $key.
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
915 $filters[] = $col.
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
918 $where[] = $key.
' = '.$data[$key];
919 $filters[] = $col.
' = '.$data[$key];
922 if (!empty($tablewithentity_cache[$tablename])) {
926 $sqlSelect .=
" WHERE " . implode(
' AND ', $where);
928 $resql = $this->db->query($sqlSelect);
930 $num_rows = $this->db->num_rows($resql);
931 if ($num_rows == 1) {
932 $res = $this->db->fetch_object($resql);
933 $lastinsertid = $res->rowid;
934 if ($is_table_category_link) $lastinsertid =
'linktable';
935 $last_insert_id_array[$tablename] = $lastinsertid;
936 } elseif ($num_rows > 1) {
937 $this->errors[$error][
'lib'] = $langs->trans(
'MultipleRecordFoundWithTheseFilters', implode(
', ', $filters));
938 $this->errors[$error][
'type'] =
'SQL';
945 $this->errors[$error][
'lib'] = $this->db->lasterror();
946 $this->errors[$error][
'type'] =
'SQL';
955 $sqlSelect =
"SELECT rowid FROM " . $tablename;
958 if (empty($keyfield)) {
961 $sqlSelect .=
" WHERE ".$keyfield.
" = ".((int) $lastinsertid);
963 if (!empty($tablewithentity_cache[$tablename])) {
967 $resql = $this->db->query($sqlSelect);
969 $res = $this->db->fetch_object($resql);
970 if ($this->db->num_rows($resql) == 1) {
979 $this->errors[$error][
'lib'] = $this->db->lasterror();
980 $this->errors[$error][
'type'] =
'SQL';
985 if (!empty($lastinsertid)) {
987 if (in_array(
"socialnetworks", $listfields)) {
988 $socialkey = array_search(
"socialnetworks", $listfields);
989 $tmpsql = $listvalues[$socialkey];
990 $listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
994 $sqlstart =
"UPDATE " . $tablename;
996 $data = array_combine($listfields, $listvalues);
998 foreach ($data as $key => $val) {
999 $set[] = $key.
" = ".$val;
1001 $sqlstart .=
" SET " . implode(
', ', $set);
1003 if (empty($keyfield)) {
1004 $keyfield =
'rowid';
1006 $sqlend =
" WHERE " . $keyfield .
" = ".((int) $lastinsertid);
1008 if ($is_table_category_link) {
1009 $sqlend =
" WHERE " . implode(
' AND ', $where);
1012 if (!empty($tablewithentity_cache[$tablename])) {
1016 $sql = $sqlstart . $sqlend;
1019 $resql = $this->db->query(
$sql);
1025 $this->errors[$error][
'lib'] = $this->db->lasterror();
1026 $this->errors[$error][
'type'] =
'SQL';
1033 if (!$error && !$updatedone) {
1035 if (in_array(
"socialnetworks", $listfields)) {
1036 $socialkey = array_search(
"socialnetworks", $listfields);
1037 $tmpsql = $listvalues[$socialkey];
1038 $listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
1042 $sqlstart =
"INSERT INTO " . $tablename .
"(" . implode(
", ", $listfields) .
", import_key";
1043 $sqlend =
") VALUES(" . implode(
', ', $listvalues) .
", '" . $this->db->escape($importid) .
"'";
1044 if (!empty($tablewithentity_cache[$tablename])) {
1045 $sqlstart .=
", entity";
1046 $sqlend .=
", " . $conf->entity;
1048 if (!empty($objimport->array_import_tables_creator[0][$alias])) {
1049 $sqlstart .=
", " . $objimport->array_import_tables_creator[0][$alias];
1050 $sqlend .=
", " . $user->id;
1052 $sql = $sqlstart . $sqlend .
")";
1057 $resql = $this->db->query(
$sql);
1059 if (!$is_table_category_link) {
1060 $last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename);
1065 $this->errors[$error][
'lib'] = $this->db->lasterror();
1066 $this->errors[$error][
'type'] =
'SQL';