function InsertRecord($arr, $recInd)
{
    global $goodlines, $conn, $error_message, $keys_present, $keys, $strOriginalTableName, $strTableName, $eventObj, $locale_info, $auditObj;
    $ret = 1;
    $rawvalues = array();
    foreach ($arr as $key => $val) {
        $rawvalues[$key] = $val;
        $type = GetFieldType($key);
        if (!NeedQuotes($type)) {
            $value = (string) $val;
            $value = str_replace(",", ".", $value);
            if (strlen($value) > 0) {
                $value = str_replace($locale_info["LOCALE_SCURRENCY"], "", $value);
                $arr[$key] = 0 + $value;
            } else {
                $arr[$key] = NULL;
            }
        }
    }
    $retval = true;
    if ($eventObj->exists('BeforeInsert')) {
        $retval = $eventObj->BeforeInsert($rawvalues, $arr);
    }
    if ($retval) {
        $fields = array_keys($arr);
        foreach ($fields as $key => $val) {
            $fields_list[$key] = AddFieldWrappers(GetFullFieldName($val));
        }
        $values_list = "";
        foreach ($arr as $key => $val) {
            if (!is_null($arr[$key])) {
                $values_list .= add_db_quotes($key, $val) . ", ";
            } else {
                $values_list .= "NULL, ";
            }
        }
        if (strlen($values_list) > 0) {
            $values_list = substr($values_list, 0, strlen($values_list) - 2);
        }
        $sql = "insert into " . AddTableWrappers($strOriginalTableName) . " (" . implode(",", $fields_list) . ") values (" . $values_list . ")";
        if (db_exec_import($sql, $conn)) {
            $goodlines++;
            if ($auditObj) {
                $aKeys = GetKeysArray($arr, true);
                $auditObj->LogAdd($strTableName, $arr, $aKeys);
            }
        } else {
            $temp_error_message = "<b>Error:</b> in the line: " . implode(",", $arr) . '&nbsp;&nbsp;<a linkType="debugOpener" recId="' . $recInd . '" href="" onclick="importMore(' . $recInd . ');">More info</a><br>';
            $temp_error_message .= '<div id="importDebugInfoTable' . $recInd . '" cellpadding="3" cellspacing="1" align="center" style="display: none;"><p class="error">SQL query: ' . $sql . '; </p><p class="error">DB error: ' . db_error($conn) . ';</p></div>';
            $temp_error_message .= "<br><br>";
            // we'll try to update the record
            if ($keys_present) {
                $sql = "update " . AddTableWrappers($strOriginalTableName) . " set ";
                $sqlset = "";
                $where = " where ";
                foreach ($fields as $k => $val) {
                    if (!in_array(AddFieldWrappers($fields[$k]), $keys)) {
                        if (!is_null($arr[$val])) {
                            $sqlset .= $fields_list[$k] . "=" . add_db_quotes($val, $arr[$val]) . ", ";
                        } else {
                            $sqlset .= $fields_list[$k] . "=NULL, ";
                        }
                    } else {
                        $where .= $fields_list[$k] . "=" . add_db_quotes($val, $arr[$val]) . " and ";
                    }
                }
                if (strlen($sqlset) > 0) {
                    $sql .= substr($sqlset, 0, strlen($sqlset) - 2);
                }
                $where = substr($where, 0, strlen($where) - 5);
                $sql .= " " . $where;
                $rstmp = db_query("select * from " . AddTableWrappers($strOriginalTableName) . " " . $where, $conn);
                $data = db_fetch_array($rstmp);
                if ($data) {
                    if ($auditObj) {
                        foreach ($data as $key => $val) {
                            $auditOldValues[$key] = $val;
                        }
                    }
                    if (db_exec_import($sql, $conn)) {
                        // update successfull
                        $goodlines++;
                        if ($auditObj) {
                            $aKeys = GetKeysArray($arr);
                            $auditObj->LogEdit($strTableName, $arr, $auditOldValues, $aKeys);
                        }
                    } else {
                        echo 'not updated';
                        // update not successfull
                        $error_message .= $temp_error_message;
                        $ret = 0;
                    }
                } else {
                    $error_message .= $temp_error_message;
                    $ret = 0;
                }
            } else {
                $error_message .= $temp_error_message;
            }
        }
        return $ret;
    }
}
Example #2
0
 /**
  * Insert an imported record to the database
  * @param Array fieldsValuesData			An array having the import fields names as 'keys' and the corresponding fields' values as 'values'
  * @param Array keys						The key fields' names
  * @param Boolean isIdentityOffNeeded		The flag indicating if there is any autoincremented import field
  * @param &Number addedRecords
  * @param &Number updatedRecords
  * @param &Array errorMmessages	 
  * @param &Array unprocessedData
  */
 public function importRecord($fieldsValuesData, $keys, $isIdentityOffNeeded, &$addedRecords, &$updatedRecords, &$errorMessages, &$unprocessedData)
 {
     $rawvalues = $fieldsValuesData;
     $fieldsValuesData = $this->prepareFiledsValuesData($fieldsValuesData);
     if ($this->eventsObject->exists('BeforeInsert')) {
         if ($this->eventsObject->BeforeInsert($rawvalues, $fieldsValuesData, $this) === false) {
             return;
         }
     }
     $fieldNames = array_keys($fieldsValuesData);
     // try to insert the record
     $sql = $this->getInsertSQL($fieldNames, $fieldsValuesData);
     if (db_exec_import($sql, $this->connection, $this->connection->addTableWrappers($this->strOriginalTableName), $isIdentityOffNeeded)) {
         $addedRecords = $addedRecords + 1;
         if ($this->audit) {
             $this->audit->LogAdd($this->tName, $fieldsValuesData, GetKeysArray($fieldsValuesData, $this, true));
         }
         return;
     }
     $tempErrorMessage = $this->connection->lastError();
     $keyFieldsNames = array_intersect($fieldNames, $keys);
     if (!$keyFieldsNames) {
         if (!count($unprocessedData)) {
             $unprocessedData[] = $this->getImportFieldsLogCSVLine($fieldNames);
         }
         $unprocessedData[] = $this->parseValuesDataInLogCSVLine($fieldsValuesData);
         $errorMessages[] = $tempErrorMessage;
         return;
     }
     // try to update the record
     $updateWhere = $this->getUpdateSQLWhere($keyFieldsNames, $fieldsValuesData);
     $getAllUpdatedSQL = "select * from " . $this->connection->addTableWrappers($this->strOriginalTableName) . " where " . $updateWhere;
     $data = $this->connection->query($getAllUpdatedSQL)->fetchAssoc();
     if ($data) {
         $notKeyFieldsNames = array_diff($fieldNames, $keys);
         $sql = $this->getUpdateSQL($notKeyFieldsNames, $fieldsValuesData, $updateWhere);
         if (db_exec_import($sql, $this->connection, $this->connection->addTableWrappers($this->strOriginalTableName), $isIdentityOffNeeded)) {
             // update successfull
             $updatedRecords = $updatedRecords + 1;
             if ($this->audit) {
                 $auditOldValues = array();
                 foreach ($data as $key => $val) {
                     $auditOldValues[$key] = $val;
                 }
                 $this->audit->LogEdit($this->tName, $fieldsValuesData, $auditOldValues, GetKeysArray($fieldsValuesData, $this));
             }
         } else {
             // update not successfull
             if (!count($unprocessedData)) {
                 $unprocessedData[] = $this->getImportFieldsLogCSVLine($fieldNames);
             }
             $unprocessedData[] = $this->parseValuesDataInLogCSVLine($fieldsValuesData);
             $errorMessages[] = $this->connection->lastError();
         }
     } else {
         if (!count($unprocessedData)) {
             $unprocessedData[] = $this->getImportFieldsLogCSVLine($fieldNames);
         }
         // nothing to update
         $unprocessedData[] = $this->parseValuesDataInLogCSVLine($fieldsValuesData);
         $errorMessages[] = $tempErrorMessage;
     }
 }