/**
 * update a record from current list
 * this function is registered in xajax
 * @param string $list_title title of list
 * @param string $key_string comma separated name value pairs
 * @param array $form_values values of new record (array of name value pairs)
 * @return xajaxResponse every xajax registered function needs to return this object
 */
function action_update_list_record($list_title, $key_string, $form_values)
{
    global $logging;
    global $user;
    global $list_table_configuration;
    global $firstthingsfirst_field_descriptions;
    global $user_start_time_array;
    $html_str = "";
    $name_keys = array_keys($form_values);
    $db_field_name_attachments = ListTable::_get_db_field_name(DB_ATTACHMENTS_NAME);
    $new_form_values = array();
    $attachments_array = array();
    $logging->info("USER_ACTION " . __METHOD__ . " (user="******", list_title={$list_title}, key_string={$key_string})");
    # store start time
    $user_start_time_array[__METHOD__] = microtime(TRUE);
    # create necessary objects
    $result = new Result();
    $response = new xajaxResponse();
    $html_database_table = new HtmlDatabaseTable($list_table_configuration);
    foreach ($name_keys as $name_key) {
        $value_array = explode(GENERAL_SEPARATOR, $name_key);
        $db_field_name = $value_array[0];
        $field_type = $value_array[1];
        $field_number = $value_array[2];
        $check_functions = explode(" ", $firstthingsfirst_field_descriptions[$field_type][FIELD_DESCRIPTION_FIELD_INPUT_CHECKS]);
        $result->reset();
        $logging->debug("field (name=" . $db_field_name . ", type=" . $field_type . ", number=" . $field_number . ")");
        # check field values
        check_field($check_functions, $db_field_name, $form_values[$name_key], $user->get_date_format(), $result);
        if (strlen($result->get_error_message_str()) > 0) {
            set_error_message($db_field_name, "right", $result->get_error_message_str(), "", "", $response);
            return $response;
        }
        # set new value to the old value
        $new_form_value = $result->get_result_str();
        # create an array of notes for the notes field
        if ($field_type == FIELD_TYPE_DEFINITION_NOTES_FIELD) {
            # create an array with note details
            $new_note_array = array($field_number, $form_values[$name_key]);
            # create a new array of notes if none exist, otherwise add the new note to existing node array
            if (array_key_exists($db_field_name, $new_form_values)) {
                $notes_array = $new_form_values[$db_field_name];
                array_push($notes_array, $new_note_array);
                $new_form_values[$db_field_name] = $notes_array;
            } else {
                $new_form_values[$db_field_name] = array($new_note_array);
            }
        } else {
            if ($field_type == FIELD_TYPE_DEFINITION_ATTACHMENTS) {
                # add the new attachment to the attachments array
                $new_attachment_array = array($field_number, $form_values[$name_key]);
                array_push($attachments_array, $new_attachment_array);
                $new_form_values[$db_field_name_attachments] = $attachments_array;
                $logging->debug("found an attachment (count = " . count($attachments_array) . ")");
            } else {
                $new_form_values[$db_field_name] = $new_form_value;
            }
        }
    }
    # create list table object
    $list_table = new ListTable($list_title);
    if ($list_table->get_is_valid() == FALSE) {
        $logging->warn("create list object returns false");
        $error_message_str = $list_table->get_error_message_str();
        $error_log_str = $list_table->get_error_log_str();
        $error_str = $list_table->get_error_str();
        set_error_message("record_contents_buttons", "above", $error_message_str, $error_log_str, $error_str, $response);
        return $response;
    }
    # display error when insertion returns false
    if (!$list_table->update($key_string, $new_form_values)) {
        $logging->warn("update list record returns false");
        $error_message_str = $list_table->get_error_message_str();
        $error_log_str = $list_table->get_error_log_str();
        $error_str = $list_table->get_error_str();
        set_error_message("record_contents_buttons", "above", $error_message_str, $error_log_str, $error_str, $response);
        return $response;
    }
    # set content
    $result->reset();
    $html_database_table->get_content($list_table, $list_title, "", DATABASETABLE_UNKWOWN_PAGE, $result);
    $response->custom_response->assign_with_effect(LIST_CSS_NAME_PREFIX . "content_pane", $result->get_result_str());
    # set action pane
    $html_str = $html_database_table->get_action_bar($list_title, "");
    $response->custom_response->assign_with_effect("action_pane", $html_str);
    # set footer
    $response->assign("footer_text", "innerHTML", get_footer($list_table->get_creator_modifier_array()));
    # check post conditions
    if (check_postconditions($result, $response) == FALSE) {
        return $response;
    }
    # log total time for this function
    $logging->info(get_function_time_str(__METHOD__));
    return $response;
}