/**
  * add a new record to database
  * @param $name_values_array array array containing name-values of the record
  * @param $user_name string name of current user
  * @return int number indicates the id of the new record or 0 when no record was added
  */
 function insert($name_values_array, $user_name)
 {
     $values = array();
     $db_field_names = array_keys($name_values_array);
     $this->_log->trace("inserting into DatabaseTable (user_name=" . $user_name . ")");
     # check if database connection is working
     if ($this->_check_database_connection() == FALSE) {
         return 0;
     }
     # check if database table exists
     if (!$this->_database->table_exists($this->table_name)) {
         if ($this->create() == FALSE) {
             return 0;
         }
     }
     foreach ($db_field_names as $db_field_name) {
         $value = $name_values_array[$db_field_name];
         $field_type = $this->fields[$db_field_name][1];
         # check if db_field_name is known
         if ($field_type == "") {
             $this->_handle_error("unknown field type (db_field_name=" . $db_field_name . ")", "ERROR_DATABASE_PROBLEM");
             return 0;
         }
         $this->_log->debug("building insert query (db_field_name=" . $db_field_name . ", value=" . $value . ")");
         # encode text field
         if (stristr($field_type, "TEXT")) {
             $value = htmlentities($value, ENT_QUOTES);
         }
         if (stristr($field_type, "DATE")) {
             if (!$this->_check_datetime($value)) {
                 $this->_handle_error("given date string is incorrect (date_str=" . $value . ")", "ERROR_DATE_WRONG_FORMAT");
                 return 0;
             } else {
                 array_push($values, "'" . $value . "'");
             }
         } else {
             if ($field_type == FIELD_TYPE_DEFINITION_AUTO_CREATED || $field_type == FIELD_TYPE_DEFINITION_AUTO_MODIFIED) {
                 array_push($values, "'0'");
             } else {
                 array_push($values, "'" . $value . "'");
             }
         }
     }
     $query = "INSERT INTO " . $this->table_name . " VALUES (0, " . implode($values, ", ");
     # add archiver name and datetime
     if ($this->metadata_str[DATABASETABLE_METADATA_ENABLE_ARCHIVE] != DATABASETABLE_METADATA_FALSE) {
         $query .= ", \"\"";
         $query .= ", \"" . DB_NULL_DATETIME . "\"";
     }
     # add creator name and datetime
     if ($this->metadata_str[DATABASETABLE_METADATA_ENABLE_CREATE] != DATABASETABLE_METADATA_FALSE) {
         $query .= ", \"" . $user_name . "\"";
         $query .= ", \"" . strftime(DB_DATETIME_FORMAT) . "\"";
     }
     # add modifier name and datetime
     if ($this->metadata_str[DATABASETABLE_METADATA_ENABLE_MODIFY] != DATABASETABLE_METADATA_FALSE) {
         $query .= ", \"" . $user_name . "\"";
         $query .= ", \"" . strftime(DB_DATETIME_FORMAT) . "\"";
     }
     $query .= ")";
     $result = $this->_database->insertion_query($query);
     if ($result == 0) {
         $this->_handle_error("could not insert record to DatabaseTable", "ERROR_DATABASE_PROBLEM");
         return 0;
     }
     $this->_log->trace("inserted record into DatabaseTable (result=" . $result . ")");
     return $result;
 }