28 require_once DOL_DOCUMENT_ROOT.
'/core/modules/import/modules_import.php';
51 public $errors = array();
69 public $version =
'dolibarr';
81 public $cacheconvert = array();
83 public $cachefieldtable = array();
101 $this->separator = (
GETPOST(
'separator') ?
GETPOST(
'separator') : (empty($conf->global->IMPORT_CSV_SEPARATOR_TO_USE) ?
',' : $conf->global->IMPORT_CSV_SEPARATOR_TO_USE));
102 $this->enclosure =
'"';
106 $this->label =
'Csv';
107 $this->desc = $langs->trans(
"CSVFormatDesc", $this->separator, $this->enclosure, $this->escape);
108 $this->extension =
'csv';
109 $this->picto =
'mime/other';
110 $this->version =
'1.34';
113 $this->label_lib =
'Dolibarr';
114 $this->version_lib = DOL_VERSION;
116 $this->datatoimport = $datatoimport;
117 if (preg_match(
'/^societe_/', $datatoimport)) {
118 $this->thirdpartyobject =
new Societe($this->
db);
147 $s = join($this->separator, array_map(
'cleansep', $headerlinefields));
162 $s = join($this->separator, array_map(
'cleansep', $contentlinevalues));
193 dol_syslog(get_class($this).
"::open_file file=".$file);
195 ini_set(
'auto_detect_line_endings', 1);
198 if (!$this->handle) {
199 $langs->load(
"errors");
200 $this->error = $langs->trans(
"ErrorFailToOpenFile", $file);
248 $arrayres = fgetcsv($this->handle, 100000, $this->separator, $this->enclosure, $this->escape);
251 if ($arrayres ===
false) {
257 $newarrayres = array();
258 if ($arrayres && is_array($arrayres)) {
259 foreach ($arrayres as $key => $val) {
260 if (!empty($conf->global->IMPORT_CSV_FORCE_CHARSET)) {
261 if (strtolower($conf->global->IMPORT_CSV_FORCE_CHARSET) ==
'utf8') {
262 $newarrayres[$key][
'val'] = $val;
263 $newarrayres[$key][
'type'] = (
dol_strlen($val) ? 1 : -1);
265 $newarrayres[$key][
'val'] = utf8_encode($val);
266 $newarrayres[$key][
'type'] = (
dol_strlen($val) ? 1 : -1);
271 $newarrayres[$key][
'val'] = $val;
272 $newarrayres[$key][
'type'] = (
dol_strlen($val) ? 1 : -1);
274 $newarrayres[$key][
'val'] = utf8_encode($val);
275 $newarrayres[$key][
'type'] = (
dol_strlen($val) ? 1 : -1);
280 $this->col = count($newarrayres);
295 fclose($this->handle);
312 public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
315 global $langs, $conf, $user;
316 global $thirdparty_static;
317 global $tablewithentity_cache;
321 $this->errors = array();
322 $this->warnings = array();
329 $array_match_database_to_file = array_flip($array_match_file_to_database);
330 $sort_array_match_file_to_database = $array_match_file_to_database;
331 ksort($sort_array_match_file_to_database);
335 if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[0][
'val']))) {
337 $this->warnings[$warning][
'lib'] = $langs->trans(
'EmptyLine');
338 $this->warnings[$warning][
'type'] =
'EMPTY';
341 $last_insert_id_array = array();
345 foreach ($objimport->array_import_tables[0] as $alias => $tablename) {
348 $listfields = array();
349 $listvalues = array();
351 $errorforthistable = 0;
354 if (!isset($tablewithentity_cache[$tablename])) {
355 dol_syslog(
"Check if table ".$tablename.
" has an entity field");
356 $resql = $this->
db->DDLDescTable($tablename,
'entity');
358 $obj = $this->
db->fetch_object(
$resql);
360 $tablewithentity_cache[$tablename] = 1;
362 $tablewithentity_cache[$tablename] = 0;
372 $arrayfield = array();
373 foreach ($sort_array_match_file_to_database as $key => $val) {
374 $arrayfield[$val] = ($key - 1);
381 foreach ($sort_array_match_file_to_database as $key => $val) {
382 $fieldalias = preg_replace(
'/\..*$/i',
'', $val);
383 $fieldname = preg_replace(
'/^.*\./i',
'', $val);
385 if ($alias != $fieldalias) {
389 if ($key <= $maxfields) {
392 if ($arrayrecord[($key - 1)][
'type'] > 0) {
393 $newval = $arrayrecord[($key - 1)][
'val'];
402 if (preg_match(
'/\*/', $objimport->array_import_fields[0][$val]) && ((string) $newval ==
'')) {
403 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMissingMandatoryValue', $key);
404 $this->errors[$error][
'type'] =
'NOTNULL';
405 $errorforthistable++;
410 if (!empty($objimport->array_import_convertvalue[0][$val])) {
412 if ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeid'
413 || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromref'
414 || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeorlabel'
418 if (!is_numeric($newval) && $newval !=
'' && !preg_match(
'/^id:/i', $newval)) {
422 $newval = preg_replace(
'/^(id|ref):/i',
'', $newval);
425 if ($isidorref ==
'ref') {
426 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
427 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
428 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
429 if ($this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'][$newval] !=
'') {
430 $newval = $this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'][$newval];
433 if (empty($resultload)) {
434 dol_print_error(
'',
'Error trying to call file='.$file.
', class='.$class.
', method='.$method);
437 $classinstance =
new $class($this->
db);
438 if ($class ==
'CGenericDic') {
439 $classinstance->element = $objimport->array_import_convertvalue[0][$val][
'element'];
440 $classinstance->table_element = $objimport->array_import_convertvalue[0][$val][
'table_element'];
444 $param_array = array(
'', $newval);
445 if ($class ==
'AccountingAccount') {
458 $param_array = array(
'', $newval, 0, $arrayrecord[0][
'val']);
461 $result = call_user_func_array(array($classinstance, $method), $param_array);
464 if (!($classinstance->id !=
'') && $result == -2) {
465 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMultipleRecordFoundFromRef', $newval);
466 $this->errors[$error][
'type'] =
'FOREIGNKEY';
467 $errorforthistable++;
472 if (!($classinstance->id !=
'') && $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeorlabel') {
473 $param_array = array(
'',
'', $newval);
474 call_user_func_array(array($classinstance, $method), $param_array);
476 $this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'][$newval] = $classinstance->id;
479 if ($classinstance->id !=
'') {
480 $newval = $classinstance->id;
481 } elseif (! $error) {
482 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
483 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
484 } elseif (!empty($objimport->array_import_convertvalue[0][$val][
'element'])) {
485 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldRefNotIn',
num2Alpha($key - 1), $newval, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'element']));
487 $this->errors[$error][
'lib'] =
'ErrorBadDefinitionOfImportProfile';
489 $this->errors[$error][
'type'] =
'FOREIGNKEY';
490 $errorforthistable++;
495 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeandlabel') {
497 if (!is_numeric($newval) && $newval !=
'' && !preg_match(
'/^id:/i', $newval)) {
500 $newval = preg_replace(
'/^(id|ref):/i',
'', $newval);
502 if ($isidorref ==
'ref') {
503 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
504 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
505 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
506 $codefromfield = $objimport->array_import_convertvalue[0][$val][
'codefromfield'];
507 $code = $arrayrecord[$arrayfield[$codefromfield]][
'val'];
508 if ($this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'.$code][$newval] !=
'') {
509 $newval = $this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'.$code][$newval];
512 if (empty($resultload)) {
513 dol_print_error(
'',
'Error trying to call file='.$file.
', class='.$class.
', method='.$method.
', code='.$code);
516 $classinstance =
new $class($this->
db);
518 $param_array = array(
'', $newval, $code);
519 call_user_func_array(array($classinstance, $method), $param_array);
520 $this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'.$code][$newval] = $classinstance->id;
521 if ($classinstance->id > 0) {
522 $newval = $classinstance->id;
524 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
525 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
527 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
529 $this->errors[$error][
'type'] =
'FOREIGNKEY';
530 $errorforthistable++;
535 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'zeroifnull') {
536 if (empty($newval)) {
539 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits' || $objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchscalefromcodeunits') {
540 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
541 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
542 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
543 $units = $objimport->array_import_convertvalue[0][$val][
'units'];
544 if ($this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'.$units][$newval] !=
'') {
545 $newval = $this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'.$units][$newval];
548 if (empty($resultload)) {
549 dol_print_error(
'',
'Error trying to call file='.$file.
', class='.$class.
', method='.$method.
', units='.$units);
552 $classinstance =
new $class($this->
db);
554 call_user_func_array(array($classinstance, $method), array(
'',
'', $newval, $units));
555 $scaleorid = (($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'fetchidfromcodeunits') ? $classinstance->id : $classinstance->scale);
556 $this->cacheconvert[$file.
'_'.$class.
'_'.$method.
'_'.$units][$newval] = $scaleorid;
558 if ($classinstance->id > 0) {
559 $newval = $scaleorid ? $scaleorid : 0;
561 if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
562 $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval,
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
564 $this->errors[$error][
'lib'] =
'ErrorFieldValueNotIn';
566 $this->errors[$error][
'type'] =
'FOREIGNKEY';
567 $errorforthistable++;
571 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomercodeifauto') {
572 if (strtolower($newval) ==
'auto') {
573 $this->thirdpartyobject->get_codeclient(0, 0);
574 $newval = $this->thirdpartyobject->code_client;
577 if (empty($newval)) {
578 $arrayrecord[($key - 1)][
'type'] = -1;
580 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsuppliercodeifauto') {
581 if (strtolower($newval) ==
'auto') {
582 $this->thirdpartyobject->get_codefournisseur(0, 1);
583 $newval = $this->thirdpartyobject->code_fournisseur;
586 if (empty($newval)) {
587 $arrayrecord[($key - 1)][
'type'] = -1;
589 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getcustomeraccountancycodeifauto') {
590 if (strtolower($newval) ==
'auto') {
591 $this->thirdpartyobject->get_codecompta(
'customer');
592 $newval = $this->thirdpartyobject->code_compta;
595 if (empty($newval)) {
596 $arrayrecord[($key - 1)][
'type'] = -1;
598 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getsupplieraccountancycodeifauto') {
599 if (strtolower($newval) ==
'auto') {
600 $this->thirdpartyobject->get_codecompta(
'supplier');
601 $newval = $this->thirdpartyobject->code_compta_fournisseur;
602 if (empty($newval)) {
603 $arrayrecord[($key - 1)][
'type'] = -1;
607 if (empty($newval)) {
608 $arrayrecord[($key - 1)][
'type'] = -1;
610 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'getrefifauto') {
611 if (strtolower($newval) ==
'auto') {
614 $classModForNumber = $objimport->array_import_convertvalue[0][$val][
'class'];
615 $pathModForNumber = $objimport->array_import_convertvalue[0][$val][
'path'];
617 if (!empty($classModForNumber) && !empty($pathModForNumber) && is_readable(DOL_DOCUMENT_ROOT.$pathModForNumber)) {
618 require_once DOL_DOCUMENT_ROOT.$pathModForNumber;
619 $modForNumber =
new $classModForNumber;
623 if (!empty($objimport->array_import_convertvalue[0][$val][
'classobject'])) {
624 $pathForObject = $objimport->array_import_convertvalue[0][$val][
'pathobject'];
625 require_once DOL_DOCUMENT_ROOT.$pathForObject;
626 $tmpclassobject = $objimport->array_import_convertvalue[0][$val][
'classobject'];
627 $tmpobject =
new $tmpclassobject($this->
db);
628 foreach ($arrayfield as $tmpkey => $tmpval) {
629 if (in_array($tmpkey, array(
't.date',
'c.date_commande'))) {
630 $tmpobject->date =
dol_stringtotime($arrayrecord[$arrayfield[$tmpkey]][
'val'], 1);
635 $defaultref = $modForNumber->getNextValue(
null, $tmpobject);
637 if (is_numeric($defaultref) && $defaultref <= 0) {
640 $newval = $defaultref;
642 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'compute') {
643 $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
644 $class = $objimport->array_import_convertvalue[0][$val][
'class'];
645 $method = $objimport->array_import_convertvalue[0][$val][
'method'];
647 if (empty($resultload)) {
648 dol_print_error(
'',
'Error trying to call file='.$file.
', class='.$class.
', method='.$method);
651 $classinstance =
new $class($this->
db);
652 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $listfields, ($key - 1)));
654 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'numeric') {
656 } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] ==
'accountingaccount') {
657 if (empty($conf->global->ACCOUNTING_MANAGE_ZERO)) {
658 $newval = rtrim(trim($newval),
"0");
660 $newval = trim($newval);
668 if (!empty($objimport->array_import_regex[0][$val]) && ($newval !=
'')) {
671 if (preg_match(
'/^(.+)@([^:]+)(:.+)?$/', $objimport->array_import_regex[0][$val], $reg)) {
674 $filter = !empty($reg[3]) ?substr($reg[3], 1) :
'';
676 $cachekey = $field.
'@'.$table;
677 if (!empty($filter)) {
678 $cachekey .=
':'.$filter;
682 if (!is_array($this->cachefieldtable[$cachekey])) {
683 $sql =
"SELECT ".$field.
" as aliasfield FROM ".$table;
684 if (!empty($filter)) {
685 $sql .=
' WHERE '.$filter;
693 $obj = $this->
db->fetch_object(
$resql);
695 $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
705 if (!is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey])) {
706 $tableforerror = $table;
707 if (!empty($filter)) {
708 $tableforerror .=
':'.$filter;
710 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorFieldValueNotIn',
num2Alpha($key - 1), $newval, $field, $tableforerror);
711 $this->errors[$error][
'type'] =
'FOREIGNKEY';
712 $errorforthistable++;
715 } elseif (!preg_match(
'/'.$objimport->array_import_regex[0][$val].
'/i', $newval)) {
718 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorWrongValueForField',
num2Alpha($key - 1), $newval, $objimport->array_import_regex[0][$val]);
719 $this->errors[$error][
'type'] =
'REGEX';
720 $errorforthistable++;
728 $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorHtmlInjectionForField',
num2Alpha($key - 1),
dol_trunc($newval, 100));
729 $this->errors[$error][
'type'] =
'HTMLINJECTION';
730 $errorforthistable++;
739 if (
isModEnabled(
"socialnetworks") && strpos($fieldname,
"socialnetworks") !==
false) {
740 if (!in_array(
"socialnetworks", $listfields)) {
741 $listfields[] =
"socialnetworks";
742 $socialkey = array_search(
"socialnetworks", $listfields);
743 $listvalues[$socialkey] =
'';
746 if (!empty($newval) && $arrayrecord[($key - 1)][
'type'] > 0) {
747 $socialkey = array_search(
"socialnetworks", $listfields);
749 $socialnetwork = explode(
"_", $fieldname)[1];
750 if (empty($listvalues[$socialkey]) || $listvalues[$socialkey] ==
"null") {
751 $json =
new stdClass();
752 $json->$socialnetwork = $newval;
753 $listvalues[$socialkey] = json_encode($json);
755 $jsondata = $listvalues[$socialkey];
756 $json = json_decode($jsondata);
757 $json->$socialnetwork = $newval;
758 $listvalues[$socialkey] = json_encode($json);
762 $listfields[] = $fieldname;
764 if (empty($newval) && $arrayrecord[($key - 1)][
'type'] < 0) {
765 $listvalues[] = ($newval ==
'0' ? $newval :
"null");
766 } elseif (empty($newval) && $arrayrecord[($key - 1)][
'type'] == 0) {
767 $listvalues[] =
"''";
769 $listvalues[] =
"'".$this->db->escape($newval).
"'";
779 if (!empty($listfields) && is_array($objimport->array_import_fieldshidden[0])) {
781 foreach ($objimport->array_import_fieldshidden[0] as $key => $val) {
782 if (!preg_match(
'/^'.preg_quote($alias,
'/').
'\./', $key)) {
785 if ($val ==
'user->id') {
786 $listfields[] = preg_replace(
'/^'.preg_quote($alias,
'/').
'\./',
'', $key);
787 $listvalues[] = ((int) $user->id);
788 } elseif (preg_match(
'/^lastrowid-/', $val)) {
789 $tmp = explode(
'-', $val);
790 $lastinsertid = (isset($last_insert_id_array[$tmp[1]])) ? $last_insert_id_array[$tmp[1]] : 0;
791 $keyfield = preg_replace(
'/^'.preg_quote($alias,
'/').
'\./',
'', $key);
792 $listfields[] = $keyfield;
793 $listvalues[] = $lastinsertid;
795 } elseif (preg_match(
'/^const-/', $val)) {
796 $tmp = explode(
'-', $val, 2);
797 $listfields[] = preg_replace(
'/^'.preg_quote($alias,
'/').
'\./',
'', $key);
798 $listvalues[] =
"'".$this->db->escape($tmp[1]).
"'";
799 } elseif (preg_match(
'/^rule-/', $val)) {
801 if (!empty($objimport->array_import_convertvalue[0][$fieldname])) {
802 if ($objimport->array_import_convertvalue[0][$fieldname][
'rule'] ==
'compute') {
803 $file = (empty($objimport->array_import_convertvalue[0][$fieldname][
'classfile']) ? $objimport->array_import_convertvalue[0][$fieldname][
'file'] : $objimport->array_import_convertvalue[0][$fieldname][
'classfile']);
804 $class = $objimport->array_import_convertvalue[0][$fieldname][
'class'];
805 $method = $objimport->array_import_convertvalue[0][$fieldname][
'method'];
807 if (empty($resultload)) {
808 dol_print_error(
'',
'Error trying to call file=' . $file .
', class=' . $class .
', method=' . $method);
811 $classinstance =
new $class($this->
db);
812 $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $listfields, ($key - 1)));
813 $fieldArr = explode(
'.', $fieldname);
814 if (count($fieldArr) > 0) {
815 $fieldname = $fieldArr[1];
817 $listfields[] = $fieldname;
818 $listvalues[] = $res;
822 $this->errors[$error][
'lib'] =
'Bad value of profile setup '.$val.
' for array_import_fieldshidden';
823 $this->errors[$error][
'type'] =
'Import profile setup';
832 if (!$errorforthistable) {
834 if (!empty($listfields)) {
838 $is_table_category_link =
false;
840 if (strpos($tablename,
'_categorie_') !==
false) {
841 $is_table_category_link =
true;
845 if (!empty($updatekeys)) {
848 if (empty($lastinsertid)) {
849 $sqlSelect =
"SELECT ".$fname.
" FROM ".$tablename;
850 $data = array_combine($listfields, $listvalues);
853 foreach ($updatekeys as $key) {
854 $col = $objimport->array_import_updatekeys[0][$key];
855 $key = preg_replace(
'/^.*\./i',
'', $key);
856 if (
isModEnabled(
"socialnetworks") && strpos($key,
"socialnetworks") !==
false) {
857 $tmp = explode(
"_", $key);
859 $socialnetwork = $tmp[1];
860 $jsondata = $data[$key];
861 $json = json_decode($jsondata);
862 $stringtosearch = json_encode($socialnetwork).
':'.json_encode($json->$socialnetwork);
865 $where[] = $key.
" LIKE '%".$this->
db->escape($this->
db->escapeforlike($stringtosearch)).
"%'";
866 $filters[] = $col.
" LIKE '%".$this->
db->escape($this->
db->escapeforlike($stringtosearch)).
"%'";
869 $where[] = $key.
' = '.$data[$key];
870 $filters[] = $col.
' = '.$data[$key];
873 $sqlSelect .=
" WHERE ".implode(
' AND ', $where);
875 $resql = $this->
db->query($sqlSelect);
877 $num_rows = $this->
db->num_rows(
$resql);
878 if ($num_rows == 1) {
879 $res = $this->
db->fetch_object(
$resql);
880 $lastinsertid = $res->rowid;
881 if ($is_table_category_link) $lastinsertid =
'linktable';
882 $last_insert_id_array[$tablename] = $lastinsertid;
883 } elseif ($num_rows > 1) {
884 $this->errors[$error][
'lib'] = $langs->trans(
'MultipleRecordFoundWithTheseFilters', implode(
', ', $filters));
885 $this->errors[$error][
'type'] =
'SQL';
892 $this->errors[$error][
'lib'] = $this->
db->lasterror();
893 $this->errors[$error][
'type'] =
'SQL';
902 $sqlSelect =
"SELECT rowid FROM ".$tablename;
904 if (empty($keyfield)) {
907 $sqlSelect .=
" WHERE ".$keyfield.
" = ".((int) $lastinsertid);
909 $resql = $this->
db->query($sqlSelect);
911 $res = $this->
db->fetch_object(
$resql);
912 if ($this->
db->num_rows(
$resql) == 1) {
921 $this->errors[$error][
'lib'] = $this->
db->lasterror();
922 $this->errors[$error][
'type'] =
'SQL';
927 if (!empty($lastinsertid)) {
929 if (in_array(
"socialnetworks", $listfields)) {
930 $socialkey = array_search(
"socialnetworks", $listfields);
931 $tmpsql = $listvalues[$socialkey];
932 $listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
936 $sqlstart =
"UPDATE ".$tablename;
938 $data = array_combine($listfields, $listvalues);
940 foreach ($data as $key => $val) {
941 $set[] = $key.
" = ".$val;
943 $sqlstart .=
" SET ".implode(
', ', $set);
945 if (empty($keyfield)) {
948 $sqlend =
" WHERE ".$keyfield.
" = ".((int) $lastinsertid);
950 if ($is_table_category_link) {
951 $sqlend =
" WHERE " . implode(
' AND ', $where);
954 $sql = $sqlstart.$sqlend;
963 $this->errors[$error][
'lib'] = $this->
db->lasterror();
964 $this->errors[$error][
'type'] =
'SQL';
971 if (!$error && !$updatedone) {
973 if (in_array(
"socialnetworks", $listfields)) {
974 $socialkey = array_search(
"socialnetworks", $listfields);
975 $tmpsql = $listvalues[$socialkey];
976 $listvalues[$socialkey] =
"'".$this->db->escape($tmpsql).
"'";
980 $sqlstart =
"INSERT INTO ".$tablename.
"(".implode(
", ", $listfields).
", import_key";
981 $sqlend =
") VALUES(".implode(
', ', $listvalues).
", '".$this->
db->escape($importid).
"'";
982 if (!empty($tablewithentity_cache[$tablename])) {
983 $sqlstart .=
", entity";
984 $sqlend .=
", ".$conf->entity;
986 if (!empty($objimport->array_import_tables_creator[0][$alias])) {
987 $sqlstart .=
", ".$objimport->array_import_tables_creator[0][$alias];
988 $sqlend .=
", ".$user->id;
990 $sql = $sqlstart.$sqlend.
")";
997 if (!$is_table_category_link) {
998 $last_insert_id_array[$tablename] = $this->
db->last_insert_id($tablename);
1003 $this->errors[$error][
'lib'] = $this->
db->lasterror();
1004 $this->errors[$error][
'type'] =
'SQL';
1041 return str_replace(array(
',',
';'),
'/', $value);
Class to import CSV files.
write_header_example($outputlangs)
Output header of an example file for this format.
import_get_nb_of_lines($file)
Return nb of records.
import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
Insert a record into database.
__construct($db, $datatoimport)
Constructor.
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.
write_footer_example($outputlangs)
Output footer of an example file for this format.
import_close_file()
Close file handle.
import_read_header()
Input header line from file.
write_title_example($outputlangs, $headerlinefields)
Output title line of an example file for this format.
import_open_file($file)
Open input file.
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...
dol_count_nb_of_line($file)
Count number of lines in a file.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
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.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
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.
utf8_check($str)
Check if a string is in UTF8.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
cleansep($value)
Clean a string from separator.
testSqlAndScriptInject($val, $type)
Security: WAF layer for SQL Injection and XSS Injection (scripts) protection (Filters on GET,...
$conf db
API class for accounts.