349  public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
 
  352    global $langs, $conf, $user;
 
  353    global $thirdparty_static; 
 
  354    global $tablewithentity_cache; 
 
  358    $this->errors = array();
 
  359    $this->warnings = array();
 
  366    $array_match_database_to_file = array_flip($array_match_file_to_database);
 
  367    $sort_array_match_file_to_database = $array_match_file_to_database;
 
  368    ksort($sort_array_match_file_to_database);
 
  372    if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[1][
'val']))) {
 
  374      $this->warnings[$warning][
'lib'] = $langs->trans(
'EmptyLine');
 
  375      $this->warnings[$warning][
'type'] = 
'EMPTY';
 
  378      $last_insert_id_array = array(); 
 
  382      foreach ($objimport->array_import_tables[0] as $alias => $tablename) {
 
  385        $listfields = array();
 
  386        $listvalues = array();
 
  388        $errorforthistable = 0;
 
  391        if (!isset($tablewithentity_cache[$tablename])) { 
 
  392          dol_syslog(
"Check if table " . $tablename . 
" has an entity field");
 
  393          $resql = $this->db->DDLDescTable($tablename, 
'entity');
 
  395            $obj = $this->db->fetch_object($resql);
 
  397              $tablewithentity_cache[$tablename] = 1; 
 
  399              $tablewithentity_cache[$tablename] = 0; 
 
  409        $arrayfield = array();
 
  410        foreach ($sort_array_match_file_to_database as $key => $val) {
 
  411          $arrayfield[$val] = ($key);
 
  418        foreach ($sort_array_match_file_to_database as $key => $val) {
 
  419          $fieldalias = preg_replace(
'/\..*$/i', 
'', $val);
 
  420          $fieldname = preg_replace(
'/^.*\./i', 
'', $val);
 
  422          if ($alias != $fieldalias) {
 
  426          if ($key <= $maxfields) {
 
  429            if ($arrayrecord[($key)][
'type'] > 0) {
 
  430              $newval = $arrayrecord[($key)][
'val']; 
 
  439            if (preg_match(
'/\*/', $objimport->array_import_fields[0][$val]) && ((string) $newval == 
'')) {
 
  440              $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMissingMandatoryValue', 
num2Alpha($key - 1));
 
  441              $this->errors[$error][
'type'] = 
'NOTNULL';
 
  442              $errorforthistable++;
 
  447              if (!empty($objimport->array_import_convertvalue[0][$val])) {
 
  449                if ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'fetchidfromcodeid' 
  450                  || $objimport->array_import_convertvalue[0][$val][
'rule'] == 
'fetchidfromref' 
  451                  || $objimport->array_import_convertvalue[0][$val][
'rule'] == 
'fetchidfromcodeorlabel' 
  455                  if (!is_numeric($newval) && $newval != 
'' && !preg_match(
'/^id:/i', $newval)) {
 
  458                  $newval = preg_replace(
'/^(id|ref):/i', 
'', $newval); 
 
  461                  if ($isidorref == 
'ref') {    
 
  462                    $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
 
  463                    $class = $objimport->array_import_convertvalue[0][$val][
'class'];
 
  464                    $method = $objimport->array_import_convertvalue[0][$val][
'method'];
 
  465                    if ($this->cacheconvert[$file . 
'_' . $class . 
'_' . $method . 
'_'][$newval] != 
'') {
 
  466                      $newval = $this->cacheconvert[$file . 
'_' . $class . 
'_' . $method . 
'_'][$newval];
 
  469                      if (empty($resultload)) {
 
  470                        dol_print_error(
'', 
'Error trying to call file=' . $file . 
', class=' . $class . 
', method=' . $method);
 
  473                      $classinstance = 
new $class($this->db);
 
  474                      if ($class == 
'CGenericDic') {
 
  475                        $classinstance->element = $objimport->array_import_convertvalue[0][$val][
'element'];
 
  476                        $classinstance->table_element = $objimport->array_import_convertvalue[0][$val][
'table_element'];
 
  480                      $param_array = array(
'', $newval);
 
  481                      if ($class == 
'AccountingAccount') {
 
  494                        $param_array = array(
'', $newval, 0, $arrayrecord[0][
'val']); 
 
  497                      $result = call_user_func_array(array($classinstance, $method), $param_array);
 
  500                      if (!($classinstance->id != 
'') && $result == -2) {
 
  501                        $this->errors[$error][
'lib'] = $langs->trans(
'ErrorMultipleRecordFoundFromRef', $newval);
 
  502                        $this->errors[$error][
'type'] = 
'FOREIGNKEY';
 
  503                        $errorforthistable++;
 
  508                      if (!($classinstance->id != 
'') && $objimport->array_import_convertvalue[0][$val][
'rule'] == 
'fetchidfromcodeorlabel') {
 
  509                        $param_array = array(
'', 
'', $newval);
 
  510                        call_user_func_array(array($classinstance, $method), $param_array);
 
  512                      $this->cacheconvert[$file . 
'_' . $class . 
'_' . $method . 
'_'][$newval] = $classinstance->id;
 
  515                      if ($classinstance->id != 
'') { 
 
  516                        $newval = $classinstance->id;
 
  517                      } elseif (! $error) {
 
  518                        if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
 
  519                          $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn', $key, $newval, 
'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
 
  520                        } elseif (!empty($objimport->array_import_convertvalue[0][$val][
'element'])) {
 
  521                          $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldRefNotIn', $key, $newval, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'element']));
 
  523                          $this->errors[$error][
'lib'] = 
'ErrorBadDefinitionOfImportProfile';
 
  525                        $this->errors[$error][
'type'] = 
'FOREIGNKEY';
 
  526                        $errorforthistable++;
 
  531                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'fetchidfromcodeandlabel') {
 
  533                  if (!is_numeric($newval) && $newval != 
'' && !preg_match(
'/^id:/i', $newval)) {
 
  536                  $newval = preg_replace(
'/^(id|ref):/i', 
'', $newval);
 
  538                  if ($isidorref == 
'ref') {
 
  539                    $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
 
  540                    $class = $objimport->array_import_convertvalue[0][$val][
'class'];
 
  541                    $method = $objimport->array_import_convertvalue[0][$val][
'method'];
 
  542                    $codefromfield = $objimport->array_import_convertvalue[0][$val][
'codefromfield'];
 
  543                    $code = $arrayrecord[$arrayfield[$codefromfield]][
'val'];
 
  544                    if ($this->cacheconvert[$file . 
'_' . $class . 
'_' . $method . 
'_' . $code][$newval] != 
'') {
 
  545                      $newval = $this->cacheconvert[$file . 
'_' . $class . 
'_' . $method . 
'_' . $code][$newval];
 
  548                      if (empty($resultload)) {
 
  549                        dol_print_error(
'', 
'Error trying to call file=' . $file . 
', class=' . $class . 
', method=' . $method . 
', code=' . $code);
 
  552                      $classinstance = 
new $class($this->db);
 
  554                      $param_array = array(
'', $newval, $code);
 
  555                      call_user_func_array(array($classinstance, $method), $param_array);
 
  556                      $this->cacheconvert[$file . 
'_' . $class . 
'_' . $method . 
'_' . $code][$newval] = $classinstance->id;
 
  557                      if ($classinstance->id > 0) {    
 
  558                        $newval = $classinstance->id;
 
  560                        if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
 
  561                          $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn', $key, $newval, 
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
 
  563                          $this->errors[$error][
'lib'] = 
'ErrorFieldValueNotIn';
 
  565                        $this->errors[$error][
'type'] = 
'FOREIGNKEY';
 
  566                        $errorforthistable++;
 
  571                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'zeroifnull') {
 
  572                  if (empty($newval)) {
 
  575                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'fetchidfromcodeunits' || $objimport->array_import_convertvalue[0][$val][
'rule'] == 
'fetchscalefromcodeunits') {
 
  576                  $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
 
  577                  $class = $objimport->array_import_convertvalue[0][$val][
'class'];
 
  578                  $method = $objimport->array_import_convertvalue[0][$val][
'method'];
 
  579                  $units = $objimport->array_import_convertvalue[0][$val][
'units'];
 
  580                  if ($this->cacheconvert[$file . 
'_' . $class . 
'_' . $method . 
'_' . $units][$newval] != 
'') {
 
  581                    $newval = $this->cacheconvert[$file . 
'_' . $class . 
'_' . $method . 
'_' . $units][$newval];
 
  584                    if (empty($resultload)) {
 
  585                      dol_print_error(
'', 
'Error trying to call file=' . $file . 
', class=' . $class . 
', method=' . $method . 
', units=' . $units);
 
  588                    $classinstance = 
new $class($this->db);
 
  590                    call_user_func_array(array($classinstance, $method), array(
'', 
'', $newval, $units));
 
  591                    $scaleorid = (($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'fetchidfromcodeunits') ? $classinstance->id : $classinstance->scale);
 
  592                    $this->cacheconvert[$file . 
'_' . $class . 
'_' . $method . 
'_' . $units][$newval] = $scaleorid;
 
  594                    if ($classinstance->id > 0) { 
 
  595                      $newval = $scaleorid ? $scaleorid : 0;
 
  597                      if (!empty($objimport->array_import_convertvalue[0][$val][
'dict'])) {
 
  598                        $this->errors[$error][
'lib'] = $langs->trans(
'ErrorFieldValueNotIn', $key, $newval, 
'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val][
'dict']));
 
  600                        $this->errors[$error][
'lib'] = 
'ErrorFieldValueNotIn';
 
  602                      $this->errors[$error][
'type'] = 
'FOREIGNKEY';
 
  603                      $errorforthistable++;
 
  607                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'getcustomercodeifauto') {
 
  608                  if (strtolower($newval) == 
'auto') {
 
  609                    $this->thirdpartyobject->get_codeclient(0, 0);
 
  610                    $newval = $this->thirdpartyobject->code_client;
 
  613                  if (empty($newval)) {
 
  614                    $arrayrecord[($key)][
'type'] = -1; 
 
  616                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'getsuppliercodeifauto') {
 
  617                  if (strtolower($newval) == 
'auto') {
 
  618                    $this->thirdpartyobject->get_codefournisseur(0, 1);
 
  619                    $newval = $this->thirdpartyobject->code_fournisseur;
 
  622                  if (empty($newval)) {
 
  623                    $arrayrecord[($key)][
'type'] = -1; 
 
  625                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'getcustomeraccountancycodeifauto') {
 
  626                  if (strtolower($newval) == 
'auto') {
 
  627                    $this->thirdpartyobject->get_codecompta(
'customer');
 
  628                    $newval = $this->thirdpartyobject->code_compta;
 
  631                  if (empty($newval)) {
 
  632                    $arrayrecord[($key)][
'type'] = -1; 
 
  634                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'getsupplieraccountancycodeifauto') {
 
  635                  if (strtolower($newval) == 
'auto') {
 
  636                    $this->thirdpartyobject->get_codecompta(
'supplier');
 
  637                    $newval = $this->thirdpartyobject->code_compta_fournisseur;
 
  638                    if (empty($newval)) {
 
  639                      $arrayrecord[($key)][
'type'] = -1; 
 
  643                  if (empty($newval)) {
 
  644                    $arrayrecord[($key)][
'type'] = -1; 
 
  646                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'getrefifauto') {
 
  647                  if (strtolower($newval) == 
'auto') {
 
  650                    $classModForNumber = $objimport->array_import_convertvalue[0][$val][
'class'];
 
  651                    $pathModForNumber = $objimport->array_import_convertvalue[0][$val][
'path'];
 
  653                    if (!empty($classModForNumber) && !empty($pathModForNumber) && is_readable(DOL_DOCUMENT_ROOT.$pathModForNumber)) {
 
  654                      require_once DOL_DOCUMENT_ROOT.$pathModForNumber;
 
  655                      $modForNumber = 
new $classModForNumber();
 
  659                      if (!empty($objimport->array_import_convertvalue[0][$val][
'classobject'])) {
 
  660                        $pathForObject = $objimport->array_import_convertvalue[0][$val][
'pathobject'];
 
  661                        require_once DOL_DOCUMENT_ROOT.$pathForObject;
 
  662                        $tmpclassobject = $objimport->array_import_convertvalue[0][$val][
'classobject'];
 
  663                        $tmpobject = 
new $tmpclassobject($this->db);
 
  664                        foreach ($arrayfield as $tmpkey => $tmpval) { 
 
  665                          if (in_array($tmpkey, array(
't.date', 
'c.date_commande'))) {
 
  666                            $tmpobject->date = 
dol_stringtotime($arrayrecord[$arrayfield[$tmpkey]][
'val'], 1);
 
  671                      $defaultref = $modForNumber->getNextValue(
null, $tmpobject);
 
  673                    if (is_numeric($defaultref) && $defaultref <= 0) {  
 
  676                    $newval = $defaultref;
 
  678                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'compute') {
 
  679                  $file = (empty($objimport->array_import_convertvalue[0][$val][
'classfile']) ? $objimport->array_import_convertvalue[0][$val][
'file'] : $objimport->array_import_convertvalue[0][$val][
'classfile']);
 
  680                  $class = $objimport->array_import_convertvalue[0][$val][
'class'];
 
  681                  $method = $objimport->array_import_convertvalue[0][$val][
'method'];
 
  683                  if (empty($resultload)) {
 
  684                    dol_print_error(
'', 
'Error trying to call file=' . $file . 
', class=' . $class . 
', method=' . $method);
 
  687                  $classinstance = 
new $class($this->db);
 
  688                  $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, $key));
 
  689                  if (empty($classinstance->error) && empty($classinstance->errors)) {
 
  692                    $this->errors[$error][
'type'] = 
'CLASSERROR';
 
  693                    $this->errors[$error][
'lib'] = implode(
 
  695                      array_merge([$classinstance->error], $classinstance->errors)
 
  697                    $errorforthistable++;
 
  700                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'numeric') {
 
  702                } elseif ($objimport->array_import_convertvalue[0][$val][
'rule'] == 
'accountingaccount') {
 
  704                    $newval = rtrim(trim($newval), 
"0");
 
  706                    $newval = trim($newval);
 
  714              if (!empty($objimport->array_import_regex[0][$val]) && ($newval != 
'')) {
 
  717                if (preg_match(
'/^(.+)@([^:]+)(:.+)?$/', $objimport->array_import_regex[0][$val], $reg)) {
 
  720                  $filter = !empty($reg[3]) ? substr($reg[3], 1) : 
'';
 
  722                  $cachekey = $field . 
'@' . $table;
 
  723                  if (!empty($filter)) {
 
  724                    $cachekey .= 
':' . $filter;
 
  728                  if (!is_array($this->cachefieldtable[$cachekey])) { 
 
  729                    $sql = 
"SELECT " . $field . 
" as aliasfield FROM " . $table;
 
  730                    if (!empty($filter)) {
 
  731                      $sql .= 
' WHERE ' . $filter;
 
  734                    $resql = $this->db->query($sql);
 
  736                      $num = $this->db->num_rows($resql);
 
  739                        $obj = $this->db->fetch_object($resql);
 
  741                          $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
 
  751                  if (!is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey])) {
 
  752                    $tableforerror = $table;
 
  753                    if (!empty($filter)) {
 
  754                      $tableforerror .= 
':' . $filter;
 
  756                    $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorFieldValueNotIn', $key, $newval, $field, $tableforerror);
 
  757                    $this->errors[$error][
'type'] = 
'FOREIGNKEY';
 
  758                    $errorforthistable++;
 
  761                } elseif (!preg_match(
'/' . $objimport->array_import_regex[0][$val] . 
'/i', $newval)) {
 
  764                  $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorWrongValueForField', $key, $newval, $objimport->array_import_regex[0][$val]);
 
  765                  $this->errors[$error][
'type'] = 
'REGEX';
 
  766                  $errorforthistable++;
 
  774                $this->errors[$error][
'lib'] = $langs->transnoentitiesnoconv(
'ErrorHtmlInjectionForField', $key, 
dol_trunc($newval, 100));
 
  775                $this->errors[$error][
'type'] = 
'HTMLINJECTION';
 
  776                $errorforthistable++;
 
  785            if (isModEnabled(
"socialnetworks") && strpos($fieldname, 
"socialnetworks") !== 
false) {
 
  786              if (!in_array(
"socialnetworks", $listfields)) {
 
  787                $listfields[] = 
"socialnetworks";
 
  788                $socialkey = array_search(
"socialnetworks", $listfields); 
 
  789                $listvalues[$socialkey] = 
'';
 
  791              if (!empty($newval) && $arrayrecord[($key)][
'type'] > 0) {
 
  792                $socialkey = array_search(
"socialnetworks", $listfields); 
 
  793                $socialnetwork = explode(
"_", $fieldname)[1];
 
  794                if (empty($listvalues[$socialkey]) || $listvalues[$socialkey] == 
"null") {
 
  796                  $json->$socialnetwork = $newval;
 
  797                  $listvalues[$socialkey] = json_encode($json);
 
  799                  $jsondata = $listvalues[$socialkey];
 
  800                  $json = json_decode($jsondata);
 
  801                  $json->$socialnetwork = $newval;
 
  802                  $listvalues[$socialkey] = json_encode($json);
 
  806              $listfields[] = $fieldname;
 
  809              if (empty($newval) && $arrayrecord[($key)][
'type'] < 0) {
 
  810                $listvalues[] = ($newval == 
'0' ? (int) $newval : 
"null");
 
  811              } elseif (empty($newval) && $arrayrecord[($key)][
'type'] == 0) {
 
  812                $listvalues[] = 
"''";
 
  814                $listvalues[] = 
"'".$this->db->escape($newval).
"'";
 
  824        if (!empty($listfields) && is_array($objimport->array_import_fieldshidden[0])) {
 
  826          foreach ($objimport->array_import_fieldshidden[0] as $key => $val) {
 
  827            if (!preg_match(
'/^'.preg_quote($alias, 
'/').
'\./', $key)) {
 
  830            if ($val == 
'user->id') {
 
  831              $listfields[] = preg_replace(
'/^' . preg_quote($alias, 
'/') . 
'\./', 
'', $key);
 
  832              $listvalues[] = ((int) $user->id);
 
  833            } elseif (preg_match(
'/^lastrowid-/', $val)) {
 
  834              $tmp = explode(
'-', $val);
 
  835              $lastinsertid = (isset($last_insert_id_array[$tmp[1]])) ? $last_insert_id_array[$tmp[1]] : 0;
 
  836              $keyfield = preg_replace(
'/^' . preg_quote($alias, 
'/') . 
'\./', 
'', $key);
 
  837              $listfields[] = $keyfield;
 
  838              $listvalues[] = (int) $lastinsertid;
 
  840            } elseif (preg_match(
'/^const-/', $val)) {
 
  841              $tmp = explode(
'-', $val, 2);
 
  842              $listfields[] = preg_replace(
'/^' . preg_quote($alias, 
'/') . 
'\./', 
'', $key);
 
  843              $listvalues[] = 
"'".$this->db->escape($tmp[1]).
"'";
 
  844            } elseif (preg_match(
'/^rule-/', $val)) {
 
  846              if (!empty($objimport->array_import_convertvalue[0][$fieldname])) {
 
  847                if ($objimport->array_import_convertvalue[0][$fieldname][
'rule'] == 
'compute') {
 
  848                  $file = (empty($objimport->array_import_convertvalue[0][$fieldname][
'classfile']) ? $objimport->array_import_convertvalue[0][$fieldname][
'file'] : $objimport->array_import_convertvalue[0][$fieldname][
'classfile']);
 
  849                  $class = $objimport->array_import_convertvalue[0][$fieldname][
'class'];
 
  850                  $method = $objimport->array_import_convertvalue[0][$fieldname][
'method'];
 
  851                  $type = $objimport->array_import_convertvalue[0][$fieldname][
'type'];
 
  853                  if (empty($resultload)) {
 
  854                    dol_print_error(
'', 
'Error trying to call file=' . $file . 
', class=' . $class . 
', method=' . $method);
 
  857                  $classinstance = 
new $class($this->db);
 
  858                  $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord, $arrayfield, $key));
 
  859                  $fieldArr = explode(
'.', $fieldname);
 
  860                  if (count($fieldArr) > 0) {
 
  861                    $fieldname = $fieldArr[1];
 
  865                  $listfields[] = $fieldname;
 
  866                  if ($type == 
'int') {
 
  867                    $listvalues[] = (int) $res;
 
  868                  } elseif ($type == 
'double') {
 
  869                    $listvalues[] = (float) $res;
 
  871                    $listvalues[] = 
"'".$this->db->escape($res).
"'";
 
  874                  $this->errors[$error][
'type'] = 
'CLASSERROR';
 
  875                  $this->errors[$error][
'lib'] = implode(
 
  877                    array_merge([$classinstance->error], $classinstance->errors)
 
  879                  $errorforthistable++;
 
  884              $this->errors[$error][
'lib'] = 
'Bad value of profile setup ' . $val . 
' for array_import_fieldshidden';
 
  885              $this->errors[$error][
'type'] = 
'Import profile setup';
 
  894        if (!$errorforthistable) {
 
  896          if (!empty($listfields)) {
 
  900            $is_table_category_link = 
false;
 
  902            if (strpos($tablename, 
'_categorie_') !== 
false) {
 
  903              $is_table_category_link = 
true;
 
  907            if (!empty($updatekeys)) {
 
  910              if (empty($lastinsertid)) { 
 
  911                $sqlSelect = 
"SELECT ".$fname.
" FROM " . $tablename;
 
  913                $data = array_combine($listfields, $listvalues);
 
  917                foreach ($updatekeys as $key) {
 
  918                  $col = $objimport->array_import_updatekeys[0][$key];
 
  919                  $key = preg_replace(
'/^.*\./i', 
'', $key);
 
  920                  if (isModEnabled(
"socialnetworks") && strpos($key, 
"socialnetworks") !== 
false) {
 
  921                    $tmp = explode(
"_", $key);
 
  923                    $socialnetwork = $tmp[1];
 
  924                    $jsondata = $data[$key];
 
  925                    $json = json_decode($jsondata);
 
  926                    $stringtosearch = json_encode($socialnetwork).
':'.json_encode($json->$socialnetwork);
 
  929                    $where[] = $key.
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
 
  930                    $filters[] = $col.
" LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch)).
"%'";
 
  933                    $where[] = $key.
' = '.$data[$key];
 
  934                    $filters[] = $col.
' = '.$data[$key];
 
  937                if (!empty($tablewithentity_cache[$tablename])) {
 
  941                $sqlSelect .= 
" WHERE " . implode(
' AND ', $where);
 
  943                $resql = $this->db->query($sqlSelect);
 
  945                  $num_rows = $this->db->num_rows($resql);
 
  946                  if ($num_rows == 1) {
 
  947                    $res = $this->db->fetch_object($resql);
 
  948                    $lastinsertid = $res->rowid;
 
  949                    if ($is_table_category_link) {
 
  950                      $lastinsertid = 
'linktable';
 
  952                    $last_insert_id_array[$tablename] = $lastinsertid;
 
  953                  } elseif ($num_rows > 1) {
 
  954                    $this->errors[$error][
'lib'] = $langs->trans(
'MultipleRecordFoundWithTheseFilters', implode(
', ', $filters));
 
  955                    $this->errors[$error][
'type'] = 
'SQL';
 
  962                  $this->errors[$error][
'lib'] = $this->db->lasterror();
 
  963                  $this->errors[$error][
'type'] = 
'SQL';
 
  972                $sqlSelect = 
"SELECT rowid FROM " . $tablename;
 
  975                if (empty($keyfield)) {
 
  978                $sqlSelect .= 
" WHERE ".$keyfield.
" = ".((int) $lastinsertid);
 
  980                if (!empty($tablewithentity_cache[$tablename])) {
 
  984                $resql = $this->db->query($sqlSelect);
 
  986                  $res = $this->db->fetch_object($resql);
 
  987                  if ($this->db->num_rows($resql) == 1) {
 
  996                  $this->errors[$error][
'lib'] = $this->db->lasterror();
 
  997                  $this->errors[$error][
'type'] = 
'SQL';
 
 1002              if (!empty($lastinsertid)) {
 
 1004                if (in_array(
"socialnetworks", $listfields)) {
 
 1005                  $socialkey = array_search(
"socialnetworks", $listfields);
 
 1006                  $tmpsql =  $listvalues[$socialkey];
 
 1007                  $listvalues[$socialkey] = 
"'".$this->db->escape($tmpsql).
"'";
 
 1011                $sqlstart = 
"UPDATE " . $tablename;
 
 1013                $data = array_combine($listfields, $listvalues);
 
 1015                foreach ($data as $key => $val) {
 
 1016                  $set[] = $key.
" = ".$val; 
 
 1018                $sqlstart .= 
" SET " . implode(
', ', $set) . 
", import_key = '" . $this->db->escape($importid) . 
"'";
 
 1020                if (empty($keyfield)) {
 
 1021                  $keyfield = 
'rowid';
 
 1023                $sqlend = 
" WHERE " . $keyfield . 
" = ".((int) $lastinsertid);
 
 1025                if ($is_table_category_link) {
 
 1026                  $sqlend = 
" WHERE " . implode(
' AND ', $where);
 
 1029                if (!empty($tablewithentity_cache[$tablename])) {
 
 1033                $sql = $sqlstart . $sqlend;
 
 1036                $resql = $this->db->query($sql);
 
 1042                  $this->errors[$error][
'lib'] = $this->db->lasterror();
 
 1043                  $this->errors[$error][
'type'] = 
'SQL';
 
 1050            if (!$error && !$updatedone) {
 
 1052              if (in_array(
"socialnetworks", $listfields)) {
 
 1053                $socialkey = array_search(
"socialnetworks", $listfields);
 
 1054                $tmpsql =  $listvalues[$socialkey];
 
 1055                $listvalues[$socialkey] = 
"'".$this->db->escape($tmpsql).
"'";
 
 1059              $sqlstart = 
"INSERT INTO " . $tablename . 
"(" . implode(
", ", $listfields) . 
", import_key";
 
 1060              $sqlend = 
") VALUES(" . implode(
', ', $listvalues) . 
", '" . $this->db->escape($importid) . 
"'";
 
 1061              if (!empty($tablewithentity_cache[$tablename])) {
 
 1062                $sqlstart .= 
", entity";
 
 1063                $sqlend .= 
", " . $conf->entity;
 
 1065              if (!empty($objimport->array_import_tables_creator[0][$alias])) {
 
 1066                $sqlstart .= 
", " . $objimport->array_import_tables_creator[0][$alias];
 
 1067                $sqlend .= 
", " . $user->id;
 
 1069              $sql = $sqlstart . $sqlend . 
")";
 
 1074                $resql = $this->db->query($sql);
 
 1076                  if (!$is_table_category_link) {
 
 1077                    $last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename); 
 
 1082                  $this->errors[$error][
'lib'] = $this->db->lasterror();
 
 1083                  $this->errors[$error][
'type'] = 
'SQL';