372  public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
 
  375    global $langs, $conf, $user;
 
  376    global $thirdparty_static; 
 
  377    global $tablewithentity_cache; 
 
  381    $this->errors = array();
 
  382    $this->warnings = array();
 
  389    $array_match_database_to_file = array_flip($array_match_file_to_database);
 
  390    $sort_array_match_file_to_database = $array_match_file_to_database;
 
  391    ksort($sort_array_match_file_to_database);
 
  395    if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[1][
'val']))) {
 
  397      $this->warnings[$warning][
'lib'] = $langs->trans(
'EmptyLine');
 
  398      $this->warnings[$warning][
'type'] = 
'EMPTY';
 
  401      $last_insert_id_array = array(); 
 
  405      foreach ($objimport->array_import_tables[0] as $alias => $tablename) {
 
  408        $listfields = array();
 
  409        $listvalues = array();
 
  411        $errorforthistable = 0;
 
  414        if (!isset($tablewithentity_cache[$tablename])) { 
 
  415          dol_syslog(
"Check if table " . $tablename . 
" has an entity field");
 
  416          $resql = $this->db->DDLDescTable($tablename, 
'entity');
 
  418            $obj = $this->db->fetch_object($resql);
 
  420              $tablewithentity_cache[$tablename] = 1; 
 
  422              $tablewithentity_cache[$tablename] = 0; 
 
  432        $arrayfield = array();
 
  433        foreach ($sort_array_match_file_to_database as $key => $val) {
 
  434          $arrayfield[$val] = ($key);
 
  441        foreach ($sort_array_match_file_to_database as $key => $val) {
 
  442          $fieldalias = preg_replace(
'/\..*$/i', 
'', $val);
 
  443          $fieldname = preg_replace(
'/^.*\./i', 
'', $val);
 
  445          if ($alias != $fieldalias) {
 
  449          if ($key <= $maxfields) {
 
  452            if ($arrayrecord[($key)][
'type'] > 0) {
 
  453              $newval = $arrayrecord[($key)][
'val']; 
 
  462            if (preg_match(
'/\*/', $objimport->array_import_fields[0][$val]) && ((string) $newval == 
'')) {
 
  463              $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMissingMandatoryValue', 
num2Alpha($key - 1));
 
  464              $this->errors[$error][
'type'] = 
'NOTNULL';
 
  465              $errorforthistable++;
 
  470              if (!empty($objimport->array_import_convertvalue[0][$val])) {
 
  472                if ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'fetchidfromcodeid' 
  473                  || $objimport->array_import_convertvalue[0][$val][
'rule'] == 
'fetchidfromref' 
  474                  || $objimport->array_import_convertvalue[0][$val][
'rule'] == 
'fetchidfromcodeorlabel' 
  478                  if (!is_numeric($newval) && $newval != 
'' && !preg_match(
'/^id:/i', $newval)) {
 
  481                  $newval = preg_replace(
'/^(id|ref):/i', 
'', $newval); 
 
  484                  if ($isidorref == 
'ref') {    
 
  485                    $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
 
  486                    $class = $objimport->array_import_convertvalue[0][$val][
'class'];
 
  487                    $method = $objimport->array_import_convertvalue[0][$val][
'method'];
 
  488                    if ($this->cacheconvert[$file . 
'_' . $class . 
'_' . $method . 
'_'][$newval] != 
'') {
 
  489                      $newval = $this->cacheconvert[$file . 
'_' . $class . 
'_' . $method . 
'_'][$newval];
 
  492                      if (empty($resultload)) {
 
  493                        dol_print_error(
null, 
'Error trying to call file=' . $file . 
', class=' . $class . 
', method=' . $method);
 
  496                      $classinstance = 
new $class($this->db);
 
  497                      if ($class == 
'CGenericDic') {
 
  498                        $classinstance->element = $objimport->array_import_convertvalue[0][$val][
'element'];
 
  499                        $classinstance->table_element = $objimport->array_import_convertvalue[0][$val][
'table_element'];
 
  503                      $param_array = array(
'', $newval);
 
  504                      if ($class == 
'AccountingAccount') {
 
  517                        $param_array = array(
'', $newval, 0, $arrayrecord[0][
'val']); 
 
  520                      $result = call_user_func_array(array($classinstance, $method), $param_array);
 
  523                      if (!($classinstance->id != 
'') && $result == -2) {
 
  524                        $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMultipleRecordFoundFromRef', $newval);
 
  525                        $this->errors[$error][
'type'] = 
'FOREIGNKEY';
 
  526                        $errorforthistable++;
 
  531                      if (!($classinstance->id != 
'') && $objimport->array_import_convertvalue[0][$val][
'rule'] == 
'fetchidfromcodeorlabel') {
 
  532                        $param_array = array(
'', 
'', $newval);
 
  533                        call_user_func_array(array($classinstance, $method), $param_array);
 
  535                      $this->cacheconvert[$file . 
'_' . $class . 
'_' . $method . 
'_'][$newval] = $classinstance->id;
 
  538                      if ($classinstance->id != 
'') { 
 
  539                        $newval = $classinstance->id;
 
  540                      } elseif (! $error) {
 
  541                        if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
 
  543                          $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn', $key, $newval, 
'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
 
  544                        } elseif (!empty($objimport->array_import_convertvalue[0][$val][
'element'])) {
 
  546                          $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldRefNotIn', $key, $newval, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'element']));
 
  548                          $this->errors[$error][
'lib'] = 
'ErrorBadDefinitionOfImportProfile';
 
  550                        $this->errors[$error][
'type'] = 
'FOREIGNKEY';
 
  551                        $errorforthistable++;
 
  556                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'fetchidfromcodeandlabel') {
 
  558                  if (!is_numeric($newval) && $newval != 
'' && !preg_match(
'/^id:/i', $newval)) {
 
  561                  $newval = preg_replace(
'/^(id|ref):/i', 
'', $newval);
 
  563                  if ($isidorref == 
'ref') {
 
  564                    $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
 
  565                    $class = $objimport->array_import_convertvalue[0][$val][
'class'];
 
  566                    $method = $objimport->array_import_convertvalue[0][$val][
'method'];
 
  567                    $codefromfield = $objimport->array_import_convertvalue[0][$val][
'codefromfield'];
 
  568                    $code = $arrayrecord[$arrayfield[$codefromfield]][
'val'];
 
  569                    if ($this->cacheconvert[$file . 
'_' . $class . 
'_' . $method . 
'_' . $code][$newval] != 
'') {
 
  570                      $newval = $this->cacheconvert[$file . 
'_' . $class . 
'_' . $method . 
'_' . $code][$newval];
 
  573                      if (empty($resultload)) {
 
  574                        dol_print_error(
null, 
'Error trying to call file=' . $file . 
', class=' . $class . 
', method=' . $method . 
', code=' . $code);
 
  577                      $classinstance = 
new $class($this->db);
 
  579                      $param_array = array(
'', $newval, $code);
 
  580                      call_user_func_array(array($classinstance, $method), $param_array);
 
  581                      $this->cacheconvert[$file . 
'_' . $class . 
'_' . $method . 
'_' . $code][$newval] = $classinstance->id;
 
  582                      if ($classinstance->id > 0) {    
 
  583                        $newval = $classinstance->id;
 
  585                        if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
 
  587                          $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn', $key, $newval, 
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
 
  589                          $this->errors[$error][
'lib'] = 
'ErrorFieldValueNotIn';
 
  591                        $this->errors[$error][
'type'] = 
'FOREIGNKEY';
 
  592                        $errorforthistable++;
 
  597                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'zeroifnull') {
 
  598                  if (empty($newval)) {
 
  601                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'fetchidfromcodeunits' || $objimport->array_import_convertvalue[0][$val][
'rule'] == 
'fetchscalefromcodeunits') {
 
  602                  $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
 
  603                  $class = $objimport->array_import_convertvalue[0][$val][
'class'];
 
  604                  $method = $objimport->array_import_convertvalue[0][$val][
'method'];
 
  605                  $units = $objimport->array_import_convertvalue[0][$val][
'units'];
 
  606                  if ($this->cacheconvert[$file . 
'_' . $class . 
'_' . $method . 
'_' . $units][$newval] != 
'') {
 
  607                    $newval = $this->cacheconvert[$file . 
'_' . $class . 
'_' . $method . 
'_' . $units][$newval];
 
  610                    if (empty($resultload)) {
 
  611                      dol_print_error(
null, 
'Error trying to call file=' . $file . 
', class=' . $class . 
', method=' . $method . 
', units=' . $units);
 
  614                    $classinstance = 
new $class($this->db);
 
  616                    call_user_func_array(array($classinstance, $method), array(
'', 
'', $newval, $units));
 
  617                    $scaleorid = (($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'fetchidfromcodeunits') ? $classinstance->id : $classinstance->scale);
 
  618                    $this->cacheconvert[$file . 
'_' . $class . 
'_' . $method . 
'_' . $units][$newval] = $scaleorid;
 
  620                    if ($classinstance->id > 0) { 
 
  621                      $newval = $scaleorid ? $scaleorid : 0;
 
  623                      if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
 
  625                        $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn', $key, $newval, 
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
 
  627                        $this->errors[$error][
'lib'] = 
'ErrorFieldValueNotIn';
 
  629                      $this->errors[$error][
'type'] = 
'FOREIGNKEY';
 
  630                      $errorforthistable++;
 
  634                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'getcustomercodeifauto') {
 
  635                  if (strtolower($newval) == 
'auto') {
 
  636                    $this->thirdpartyobject->get_codeclient(0, 0);
 
  637                    $newval = $this->thirdpartyobject->code_client;
 
  640                  if (empty($newval)) {
 
  641                    $arrayrecord[($key)][
'type'] = -1; 
 
  643                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'getsuppliercodeifauto') {
 
  644                  if (strtolower($newval) == 
'auto') {
 
  645                    $this->thirdpartyobject->get_codefournisseur(0, 1);
 
  646                    $newval = $this->thirdpartyobject->code_fournisseur;
 
  649                  if (empty($newval)) {
 
  650                    $arrayrecord[($key)][
'type'] = -1; 
 
  652                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'getcustomeraccountancycodeifauto') {
 
  653                  if (strtolower($newval) == 
'auto') {
 
  654                    $this->thirdpartyobject->get_codecompta(
'customer');
 
  655                    $newval = $this->thirdpartyobject->code_compta;
 
  658                  if (empty($newval)) {
 
  659                    $arrayrecord[($key)][
'type'] = -1; 
 
  661                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'getsupplieraccountancycodeifauto') {
 
  662                  if (strtolower($newval) == 
'auto') {
 
  663                    $this->thirdpartyobject->get_codecompta(
'supplier');
 
  664                    $newval = $this->thirdpartyobject->code_compta_fournisseur;
 
  665                    if (empty($newval)) {
 
  666                      $arrayrecord[($key)][
'type'] = -1; 
 
  670                  if (empty($newval)) {
 
  671                    $arrayrecord[($key)][
'type'] = -1; 
 
  673                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'getrefifauto') {
 
  674                  if (strtolower($newval) == 
'auto') {
 
  677                    $classModForNumber = $objimport->array_import_convertvalue[0][$val][
'class'];
 
  678                    $pathModForNumber = $objimport->array_import_convertvalue[0][$val][
'path'];
 
  680                    if (!empty($classModForNumber) && !empty($pathModForNumber) && is_readable(DOL_DOCUMENT_ROOT.$pathModForNumber)) {
 
  681                      require_once DOL_DOCUMENT_ROOT.$pathModForNumber;
 
  682                      $modForNumber = 
new $classModForNumber();
 
  686                      if (!empty($objimport->array_import_convertvalue[0][$val][
'classobject'])) {
 
  687                        $pathForObject = $objimport->array_import_convertvalue[0][$val][
'pathobject'];
 
  688                        require_once DOL_DOCUMENT_ROOT.$pathForObject;
 
  689                        $tmpclassobject = $objimport->array_import_convertvalue[0][$val][
'classobject'];
 
  690                        $tmpobject = 
new $tmpclassobject($this->db);
 
  691                        foreach ($arrayfield as $tmpkey => $tmpval) { 
 
  692                          if (in_array($tmpkey, array(
't.date', 
'c.date_commande'))) {
 
  693                            $tmpobject->date = 
dol_stringtotime($arrayrecord[$arrayfield[$tmpkey]][
'val'], 1);
 
  698                      $defaultref = $modForNumber->getNextValue(
null, $tmpobject);
 
  700                    if (is_numeric($defaultref) && $defaultref <= 0) {  
 
  703                    $newval = $defaultref;
 
  705                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'compute') {
 
  706                  $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
 
  707                  $class = $objimport->array_import_convertvalue[0][$val][
'class'];
 
  708                  $method = $objimport->array_import_convertvalue[0][$val][
'method'];
 
  710                  if (empty($resultload)) {
 
  711                    dol_print_error(
null, 
'Error trying to call file=' . $file . 
', class=' . $class . 
', method=' . $method);
 
  714                  $classinstance = 
new $class($this->db);
 
  715                  $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, $key));
 
  716                  if (empty($classinstance->error) && empty($classinstance->errors)) {
 
  719                    $this->errors[$error][
'type'] = 
'CLASSERROR';
 
  720                    $this->errors[$error][
'lib'] = implode(
 
  722                      array_merge([$classinstance->error], $classinstance->errors)
 
  724                    $errorforthistable++;
 
  727                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'numeric') {
 
  729                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'accountingaccount') {
 
  731                    $newval = rtrim(trim($newval), 
"0");
 
  733                    $newval = trim($newval);
 
  741              if (!empty($objimport->array_import_regex[0][$val]) && ($newval != 
'')) {
 
  744                if (preg_match(
'/^(.+)@([^:]+)(:.+)?$/', $objimport->array_import_regex[0][$val], $reg)) {
 
  747                  $filter = !empty($reg[3]) ? substr($reg[3], 1) : 
'';
 
  749                  $cachekey = $field . 
'@' . $table;
 
  750                  if (!empty($filter)) {
 
  751                    $cachekey .= 
':' . $filter;
 
  755                  if (!is_array($this->cachefieldtable[$cachekey])) { 
 
  756                    $sql = 
"SELECT " . $field . 
" as aliasfield FROM " . $table;
 
  757                    if (!empty($filter)) {
 
  758                      $sql .= 
' WHERE ' . $filter;
 
  761                    $resql = $this->db->query($sql);
 
  763                      $num = $this->db->num_rows($resql);
 
  766                        $obj = $this->db->fetch_object($resql);
 
  768                          $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
 
  778                  if (!is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey])) {
 
  779                    $tableforerror = $table;
 
  780                    if (!empty($filter)) {
 
  781                      $tableforerror .= 
':' . $filter;
 
  784                    $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorFieldValueNotIn', $key, $newval, $field, $tableforerror);
 
  785                    $this->errors[$error][
'type'] = 
'FOREIGNKEY';
 
  786                    $errorforthistable++;
 
  789                } elseif (!preg_match(
'/' . $objimport->array_import_regex[0][$val] . 
'/i', $newval)) {
 
  793                  $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorWrongValueForField', $key, $newval, $objimport->array_import_regex[0][$val]);
 
  794                  $this->errors[$error][
'type'] = 
'REGEX';
 
  795                  $errorforthistable++;
 
  804                $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorHtmlInjectionForField', $key, 
dol_trunc($newval, 100));
 
  805                $this->errors[$error][
'type'] = 
'HTMLINJECTION';
 
  806                $errorforthistable++;
 
  815            if (isModEnabled(
"socialnetworks") && strpos($fieldname, 
"socialnetworks") !== 
false) {
 
  816              if (!in_array(
"socialnetworks", $listfields)) {
 
  817                $listfields[] = 
"socialnetworks";
 
  818                $socialkey = array_search(
"socialnetworks", $listfields); 
 
  819                $listvalues[$socialkey] = 
'';
 
  821              if (!empty($newval) && $arrayrecord[($key)][
'type'] > 0) {
 
  822                $socialkey = array_search(
"socialnetworks", $listfields); 
 
  823                $socialnetwork = explode(
"_", $fieldname)[1];
 
  824                if (empty($listvalues[$socialkey]) || $listvalues[$socialkey] == 
"null") {
 
  826                  $json->$socialnetwork = $newval;
 
  827                  $listvalues[$socialkey] = json_encode($json);
 
  829                  $jsondata = $listvalues[$socialkey];
 
  830                  $json = json_decode($jsondata);
 
  831                  $json->$socialnetwork = $newval;
 
  832                  $listvalues[$socialkey] = json_encode($json);
 
  836              $listfields[] = $fieldname;
 
  839              if (empty($newval) && $arrayrecord[($key)][
'type'] < 0) {
 
  840                $listvalues[] = ($newval == 
'0' ? (int) $newval : 
"null");
 
  841              } elseif (empty($newval) && $arrayrecord[($key)][
'type'] == 0) {
 
  842                $listvalues[] = 
"''";
 
  844                $listvalues[] = 
"'".$this->db->escape($newval).
"'";
 
  854        if (!empty($listfields) && is_array($objimport->array_import_fieldshidden[0])) {
 
  856          foreach ($objimport->array_import_fieldshidden[0] as $key => $val) {
 
  857            if (!preg_match(
'/^' . preg_quote($alias, 
'/') . 
'\./', $key)) {
 
  860            $keyfield = preg_replace(
'/^' . preg_quote($alias, 
'/') . 
'\./', 
'', $key);
 
  862            if (in_array($keyfield, $listfields)) { 
 
  864            } elseif ($val == 
'user->id') {
 
  865              $listfields[] = $keyfield;
 
  866              $listvalues[] = ((int) $user->id);
 
  867            } elseif (preg_match(
'/^lastrowid-/', $val)) {
 
  868              $tmp = explode(
'-', $val);
 
  869              $lastinsertid = (isset($last_insert_id_array[$tmp[1]])) ? $last_insert_id_array[$tmp[1]] : 0;
 
  870              $listfields[] = $keyfield;
 
  871              $listvalues[] = (int) $lastinsertid;
 
  873            } elseif (preg_match(
'/^const-/', $val)) {
 
  874              $tmp = explode(
'-', $val, 2);
 
  875              $listfields[] = $keyfield;
 
  876              $listvalues[] = 
"'".$this->db->escape($tmp[1]).
"'";
 
  877            } elseif (preg_match(
'/^rule-/', $val)) {
 
  879              if (!empty($objimport->array_import_convertvalue[0][$fieldname])) {
 
  880                if ($objimport->array_import_convertvalue[0][$fieldname][
'rule'] == 
'compute') {
 
  881                  $file = (empty($objimport->array_import_convertvalue[0][$fieldname][
'classfile']) ? $objimport->array_import_convertvalue[0][$fieldname][
'file'] : $objimport->array_import_convertvalue[0][$fieldname][
'classfile']);
 
  882                  $class = $objimport->array_import_convertvalue[0][$fieldname][
'class'];
 
  883                  $method = $objimport->array_import_convertvalue[0][$fieldname][
'method'];
 
  884                  $type = $objimport->array_import_convertvalue[0][$fieldname][
'type'];
 
  886                  if (empty($resultload)) {
 
  887                    dol_print_error(
null, 
'Error trying to call file=' . $file . 
', class=' . $class . 
', method=' . $method);
 
  890                  $classinstance = 
new $class($this->db);
 
  891                  $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, $key));
 
  892                  $fieldArr = explode(
'.', $fieldname);
 
  893                  if (count($fieldArr) > 0) {
 
  894                    $fieldname = $fieldArr[1];
 
  898                  $listfields[] = $fieldname;
 
  899                  if ($type == 
'int') {
 
  900                    $listvalues[] = (int) $res;
 
  901                  } elseif ($type == 
'double') {
 
  902                    $listvalues[] = (float) $res;
 
  904                    $listvalues[] = 
"'".$this->db->escape($res).
"'";
 
  907                  $this->errors[$error][
'type'] = 
'CLASSERROR';
 
  908                  $this->errors[$error][
'lib'] = implode(
 
  910                    array_merge([$classinstance->error], $classinstance->errors)
 
  912                  $errorforthistable++;
 
  917              $this->errors[$error][
'lib'] = 
'Bad value of profile setup ' . $val . 
' for array_import_fieldshidden';
 
  918              $this->errors[$error][
'type'] = 
'Import profile setup';
 
  927        if (!$errorforthistable) {
 
  929          if (!empty($listfields)) {
 
  933            $is_table_category_link = 
false;
 
  935            if (strpos($tablename, 
'_categorie_') !== 
false) {
 
  936              $is_table_category_link = 
true;
 
  940            if (!empty($updatekeys)) {
 
  943              if (empty($lastinsertid)) { 
 
  944                $sqlSelect = 
"SELECT ".$fname.
" FROM " . $tablename;
 
  946                $data = array_combine($listfields, $listvalues);
 
  950                '@phan-var string[] $where';
 
  952                foreach ($updatekeys as $key) {
 
  953                  $col = $objimport->array_import_updatekeys[0][$key];
 
  954                  $key = preg_replace(
'/^.*\./i', 
'', $key);
 
  955                  if (isModEnabled(
"socialnetworks") && strpos($key, 
"socialnetworks") !== 
false) {
 
  956                    $tmp = explode(
"_", $key);
 
  958                    $socialnetwork = $tmp[1];
 
  959                    $jsondata = $data[$key];
 
  960                    $json = json_decode($jsondata);
 
  961                    $stringtosearch = json_encode($socialnetwork).
':'.json_encode($json->$socialnetwork);
 
  964                    $where[] = $key.
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
 
  965                    $filters[] = $col.
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
 
  968                    $where[] = $key.
' = '.$data[$key];
 
  969                    $filters[] = $col.
' = '.$data[$key];
 
  972                if (!empty($tablewithentity_cache[$tablename])) {
 
  976                $sqlSelect .= 
" WHERE " . implode(
' AND ', $where);
 
  978                $resql = $this->db->query($sqlSelect);
 
  980                  $num_rows = $this->db->num_rows($resql);
 
  981                  if ($num_rows == 1) {
 
  982                    $res = $this->db->fetch_object($resql);
 
  983                    $lastinsertid = $res->rowid;
 
  984                    if ($is_table_category_link) {
 
  985                      $lastinsertid = 
'linktable';
 
  987                    $last_insert_id_array[$tablename] = $lastinsertid;
 
  988                  } elseif ($num_rows > 1) {
 
  989                    $this->errors[$error][
'lib'] = $langs->trans(
'MultipleRecordFoundWithTheseFilters', implode(
', ', $filters));
 
  990                    $this->errors[$error][
'type'] = 
'SQL';
 
  997                  $this->errors[$error][
'lib'] = $this->db->lasterror();
 
  998                  $this->errors[$error][
'type'] = 
'SQL';
 
 1007                $sqlSelect = 
"SELECT rowid FROM " . $tablename;
 
 1010                if (empty($keyfield)) {
 
 1011                  $keyfield = 
'rowid';
 
 1013                $sqlSelect .= 
" WHERE ".$keyfield.
" = ".((int) $lastinsertid);
 
 1015                if (!empty($tablewithentity_cache[$tablename])) {
 
 1019                $resql = $this->db->query($sqlSelect);
 
 1021                  $res = $this->db->fetch_object($resql);
 
 1022                  if ($this->db->num_rows($resql) == 1) {
 
 1031                  $this->errors[$error][
'lib'] = $this->db->lasterror();
 
 1032                  $this->errors[$error][
'type'] = 
'SQL';
 
 1037              if (!empty($lastinsertid)) {
 
 1039                if (in_array(
"socialnetworks", $listfields)) {
 
 1040                  $socialkey = array_search(
"socialnetworks", $listfields);
 
 1041                  $tmpsql =  $listvalues[$socialkey];
 
 1042                  $listvalues[$socialkey] = 
"'".$this->db->escape($tmpsql).
"'";
 
 1046                $sqlstart = 
"UPDATE " . $tablename;
 
 1048                $data = array_combine($listfields, $listvalues);
 
 1050                foreach ($data as $key => $val) {
 
 1051                  $set[] = $key.
" = ".$val; 
 
 1053                $sqlstart .= 
" SET " . implode(
', ', $set) . 
", import_key = '" . $this->db->escape($importid) . 
"'";
 
 1055                if (empty($keyfield)) {
 
 1056                  $keyfield = 
'rowid';
 
 1058                $sqlend = 
" WHERE " . $keyfield . 
" = ".((int) $lastinsertid);
 
 1060                if ($is_table_category_link) {
 
 1061                  '@phan-var-force string[] $where';
 
 1062                  $sqlend = 
" WHERE " . implode(
' AND ', $where);
 
 1065                if (!empty($tablewithentity_cache[$tablename])) {
 
 1069                $sql = $sqlstart . $sqlend;
 
 1072                $resql = $this->db->query($sql);
 
 1078                  $this->errors[$error][
'lib'] = $this->db->lasterror();
 
 1079                  $this->errors[$error][
'type'] = 
'SQL';
 
 1086            if (!$error && !$updatedone) {
 
 1088              if (in_array(
"socialnetworks", $listfields)) {
 
 1089                $socialkey = array_search(
"socialnetworks", $listfields);
 
 1090                $tmpsql =  $listvalues[$socialkey];
 
 1091                $listvalues[$socialkey] = 
"'".$this->db->escape($tmpsql).
"'";
 
 1095              $sqlstart = 
"INSERT INTO " . $tablename . 
"(" . implode(
", ", $listfields) . 
", import_key";
 
 1096              $sqlend = 
") VALUES(" . implode(
', ', $listvalues) . 
", '" . $this->db->escape($importid) . 
"'";
 
 1097              if (!empty($tablewithentity_cache[$tablename])) {
 
 1098                $sqlstart .= 
", entity";
 
 1099                $sqlend .= 
", " . $conf->entity;
 
 1101              if (!empty($objimport->array_import_tables_creator[0][$alias])) {
 
 1102                $sqlstart .= 
", " . $objimport->array_import_tables_creator[0][$alias];
 
 1103                $sqlend .= 
", " . $user->id;
 
 1105              $sql = $sqlstart . $sqlend . 
")";
 
 1110                $resql = $this->db->query($sql);
 
 1112                  if (!$is_table_category_link) {
 
 1113                    $last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename); 
 
 1118                  $this->errors[$error][
'lib'] = $this->db->lasterror();
 
 1119                  $this->errors[$error][
'type'] = 
'SQL';