/** * update an existing ListTableItem in database * @param $encoded_key_string string unique identifier of ListTableItem * @param $name_values_array array array containing name-values of the ListTableItem * @return bool indicates if ListTableItem has been updated */ function update($encoded_key_string, $name_values_array) { global $list_table_description; $db_field_names = array_keys($name_values_array); $all_notes_array = array(); $attachments_to_be_inserted_array = array(); $attachments_to_be_deleted_array = array(); $this->_log->trace("updating record from ListTable (encoded_key_string={$encoded_key_string})"); foreach ($db_field_names as $db_field_name) { $field_type = $this->fields[$db_field_name][1]; $value = $name_values_array[$db_field_name]; $notes_array = array(); if ($field_type == FIELD_TYPE_DEFINITION_NOTES_FIELD) { $note_count = 0; foreach ($value as $note) { # do nothing with an empty node if ($note[1] == "") { $this->_log->debug("found an empty note (field={$db_field_name})"); } else { array_push($notes_array, array($db_field_name, $note[0], $note[1])); if ($note[0] == 0) { # count new notes $note_count++; } else { # count existing notes that will not be deleted if ($note[1] != LISTTABLENOTE_EMPTY_NOTE) { $note_count++; } } } } $name_values_array[$db_field_name] = $note_count; array_push($all_notes_array, $notes_array); } if ($field_type == FIELD_TYPE_DEFINITION_ATTACHMENTS) { $attachment_count = 0; foreach ($value as $attachment) { $attachment_id = $attachment[0]; $attachment_str = $attachment[1]; $attachment_array = explode('|', $attachment_str); $tmp_file_name = $attachment_array[0]; $this->_log->debug("found an attachment (attachment_id={$attachment_id}, tmp_file_name={$tmp_file_name})"); # do not count an attachment that is about to be deleted if ($tmp_file_name == LISTTABLEATTACHMENT_DELETE_ATTACHMENT) { $this->_log->debug("found an attachment to be deleted (attachment_id={$attachment_id})"); array_push($attachments_to_be_deleted_array, $attachment); } else { if ($tmp_file_name == LISTTABLEATTACHMENT_EMPTY_ATTACHMENT) { $this->_log->debug("found an empty attachment (attachment_id={$attachment_id})"); } else { $attachment_count++; if ($tmp_file_name == LISTTABLEATTACHMENT_EXISTING_ATTACHMENT) { $this->_log->debug("found an existing attachment (attachment_id={$attachment_id})"); } else { $this->_log->debug("found a new attachment (attachment_id={$attachment_id})"); array_push($attachments_to_be_inserted_array, $attachment); } } } } $name_values_array[$db_field_name] = $attachment_count; $this->_log->debug("found {$attachment_count} attachments"); } } if (parent::update($encoded_key_string, $name_values_array) == FALSE) { return FALSE; } # get the id of this record $key_string = $this->_decode_key_string($encoded_key_string); $query = "SELECT " . DB_ID_FIELD_NAME . " FROM " . $this->table_name . " WHERE " . $key_string; $result = $this->_database->query($query); if ($result == FALSE) { $this->_handle_error("could not get id of ListTable (key_string=" . $key_string . ")", "ERROR_DATABASE_PROBLEM"); return FALSE; } $record_id = $this->_database->fetch($result); # insert new notes and update existing notes foreach ($all_notes_array as $notes_array) { foreach ($notes_array as $note_array) { # a new note if ($note_array[1] == 0) { $this->_log->debug("found a new note"); # insert the new note if ($this->_list_table_note->insert($record_id[0], $note_array[0], $note_array[2]) == 0) { # copy error strings from _list_table_note $this->error_message_str = $this->_list_table_note->get_error_message_str(); $this->error_log_str = $this->_list_table_note->get_error_log_str(); $this->error_str = $this->_list_table_note->get_error_str(); return FALSE; } } else { $this->_log->debug("update existing note"); # delete the note when it is an existing note that contains the message to delete it if ($note_array[2] == LISTTABLENOTE_EMPTY_NOTE) { if ($this->_list_table_note->delete($note_array[1]) == FALSE) { # copy error strings from _list_table_note $this->error_message_str = $this->_list_table_note->get_error_message_str(); $this->error_log_str = $this->_list_table_note->get_error_log_str(); $this->error_str = $this->_list_table_note->get_error_str(); return FALSE; } } else { if ($this->_list_table_note->update($note_array[1], $note_array[2]) == FALSE) { # copy error strings from _list_table_note $this->error_message_str = $this->_list_table_note->get_error_message_str(); $this->error_log_str = $this->_list_table_note->get_error_log_str(); $this->error_str = $this->_list_table_note->get_error_str(); return FALSE; } } } } } # delete all attachments to be deleted foreach ($attachments_to_be_deleted_array as $attachment) { $this->_log->debug("found an attachment to be deleted"); if ($this->_list_table_attachment->delete($attachment[0]) == FALSE) { # copy error strings from _list_table_attachment $this->error_message_str = $this->_list_table_attachment->get_error_message_str(); $this->error_log_str = $this->_list_table_attachment->get_error_log_str(); $this->error_str = $this->_list_table_attachment->get_error_str(); return FALSE; } } # insert all new attachments foreach ($attachments_to_be_inserted_array as $attachment) { $this->_log->debug("found a new attachment"); if ($this->_list_table_attachment->insert($record_id[0], $attachment[0], $attachment[1]) == FALSE) { # copy error strings from _list_table_attachment $this->error_message_str = $this->_list_table_attachment->get_error_message_str(); $this->error_log_str = $this->_list_table_attachment->get_error_log_str(); $this->error_str = $this->_list_table_attachment->get_error_str(); return FALSE; } } # update list table description if ($this->_update_list_table_description_statistics() == FALSE) { return FALSE; } # also update creator modifier array $record = $this->_get_list_table_description_record($this->list_title); if (count($record) == 0) { return FALSE; } $this->_log->trace("updated record of ListTable"); return TRUE; }