/**
  * update ListTableDescription object in database
  * @param string $title title of ListTableDescription
  * @param $name_values array array containing new name-values of record
  * @return bool indicates if ListTableDescription has been updated
  */
 function update($title, $name_values_array = array())
 {
     $this->_log->trace("updating ListTableDescription in database (title=" . $title . ")");
     # create encoded_key_string
     $encoded_key_string = parent::_encode_key_string(LISTTABLEDESCRIPTION_TITLE_FIELD_NAME . "='" . $title . "'");
     # convert value
     if (array_key_exists(LISTTABLEDESCRIPTION_DEFINITION_FIELD_NAME, $name_values_array) == TRUE) {
         $name_values_array[LISTTABLEDESCRIPTION_DEFINITION_FIELD_NAME] = $this->_json->encode($name_values_array[LISTTABLEDESCRIPTION_DEFINITION_FIELD_NAME]);
     }
     if (parent::update($encoded_key_string, $name_values_array) == FALSE) {
         return FALSE;
     }
     # update list title in user_list_permissions
     if (array_key_exists(LISTTABLEDESCRIPTION_TITLE_FIELD_NAME, $name_values_array) == TRUE) {
         # create key string for user_list_permissions
         $permission_key_string = USERLISTTABLEPERMISSIONS_LISTTABLE_TITLE_FIELD_NAME . "='" . $title . "'";
         # create array with new title
         $new_title_array = array();
         $new_title_array[USERLISTTABLEPERMISSIONS_LISTTABLE_TITLE_FIELD_NAME] = $name_values_array[LISTTABLEDESCRIPTION_TITLE_FIELD_NAME];
         if ($this->_user_list_permissions->update($permission_key_string, $new_title_array) == FALSE) {
             # copy error strings from user_list_permissions
             $this->error_message_str = $this->_user_list_permissions->get_error_message_str();
             $this->error_log_str = $this->_user_list_permissions->get_error_log_str();
             $this->error_str = $this->_user_list_permissions->get_error_str();
             return FALSE;
         }
     }
     $this->_log->trace("updated ListTableDescription (title=" . $title . ")");
     return TRUE;
 }
 /**
  * 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;
 }
 /**
  * update a user in database
  * @param string encoded_key_string encoded_key_string of user
  * @param $name_values array array containing new name-values of record
  * @param $update_session bool indicates if active user session parameters should be updated (only when user updates own settings)
  * @return bool indicates if user has been updated
  */
 function update($encoded_key_string, $name_values_array, $update_session = FALSE)
 {
     $this->_log->trace("update user (encoded_key_string=" . $encoded_key_string . ")");
     if (array_key_exists(USER_PW_FIELD_NAME, $name_values_array) == TRUE) {
         $password_str = $name_values_array[USER_PW_FIELD_NAME];
         if (strlen($password_str) > 0) {
             $this->_log->debug("found a password");
             $name_values_array[USER_PW_FIELD_NAME] = md5($password_str);
         } else {
             $this->_log->debug("found an empty password");
             unset($name_values_array[USER_PW_FIELD_NAME]);
         }
     }
     # if user is admin then user must also be able to create lists and users
     if (array_key_exists(USER_IS_ADMIN_FIELD_NAME, $name_values_array) == TRUE) {
         if ($name_values_array[USER_IS_ADMIN_FIELD_NAME] == 1) {
             $name_values_array[USER_CAN_CREATE_LIST_FIELD_NAME] = 1;
             $name_values_array[USER_CAN_CREATE_USER_FIELD_NAME] = 1;
         }
     }
     # if user is able to create users then user must also be able to create lists
     if (array_key_exists(USER_CAN_CREATE_USER_FIELD_NAME, $name_values_array) == TRUE) {
         if ($name_values_array[USER_CAN_CREATE_USER_FIELD_NAME] == 1) {
             $name_values_array[USER_CAN_CREATE_LIST_FIELD_NAME] = 1;
         }
     }
     # update user name in user_list_permissions
     if (array_key_exists(USER_NAME_FIELD_NAME, $name_values_array) == TRUE) {
         # select something from user_list_permissions to check if database table exists
         if ($this->_user_list_permissions->select(USERLISTTABLEPERMISSIONS_USER_NAME_FIELD_NAME, 1) == TRUE) {
             # database table user_list_permissions exists
             # first get the current user name
             $user_array = $this->select_record($encoded_key_string);
             if (count($user_array) == 0) {
                 return FALSE;
             }
             $current_user_name = $user_array[USER_NAME_FIELD_NAME];
             # create key string for user_list_permissions
             $permission_key_string = USERLISTTABLEPERMISSIONS_USER_NAME_FIELD_NAME . "='" . $current_user_name . "'";
             # create array with new title
             $new_title_array = array();
             $new_title_array[USERLISTTABLEPERMISSIONS_USER_NAME_FIELD_NAME] = $name_values_array[USER_NAME_FIELD_NAME];
             if ($this->_user_list_permissions->update($permission_key_string, $new_title_array) == FALSE) {
                 # copy error strings from user_list_permissions
                 $this->error_message_str = $this->_user_list_permissions->get_error_message_str();
                 $this->error_log_str = $this->_user_list_permissions->get_error_log_str();
                 $this->error_str = $this->_user_list_permissions->get_error_str();
                 return FALSE;
             }
         }
     }
     if (parent::update($encoded_key_string, $name_values_array) == FALSE) {
         return FALSE;
     }
     # set session parameters (only for fields that can be changed in UserSettings page)
     if ($update_session == TRUE) {
         $this->set_name($name_values_array[USER_NAME_FIELD_NAME]);
         $this->set_lang($name_values_array[USER_LANG_FIELD_NAME]);
         $this->set_date_format($name_values_array[USER_DATE_FORMAT_FIELD_NAME]);
         $this->set_decimal_mark($name_values_array[USER_DECIMAL_MARK_FIELD_NAME]);
         $this->set_lines_per_page($name_values_array[USER_LINES_PER_PAGE_FIELD_NAME]);
         $this->set_theme($name_values_array[USER_THEME_FIELD_NAME]);
     }
     $this->_log->info("user updated (encoded_key_string=" . $encoded_key_string . ")");
     return TRUE;
 }
 /**
  * update a list permission
  * @param string encoded_key_string encoded_key_string of list permission
  * @param $name_values array array containing new name-values of record
  * @return bool indicates if list permission has been updated
  */
 function update($encoded_key_string, $name_values_array)
 {
     $this->_log->trace("update user list permission (encoded_key_string=" . $encoded_key_string . ")");
     # if user is list admin then user must also be able to create list
     if (array_key_exists(USERLISTTABLEPERMISSIONS_IS_AMDIN_FIELD_NAME, $name_values_array) == TRUE) {
         if ($name_values_array[USERLISTTABLEPERMISSIONS_IS_AMDIN_FIELD_NAME] == 1) {
             $name_values_array[USERLISTTABLEPERMISSIONS_CAN_CREATE_LIST_FIELD_NAME] = 1;
         }
     }
     # if user can create list than user must also be able to edit list
     if (array_key_exists(USERLISTTABLEPERMISSIONS_CAN_CREATE_LIST_FIELD_NAME, $name_values_array) == TRUE) {
         if ($name_values_array[USERLISTTABLEPERMISSIONS_CAN_CREATE_LIST_FIELD_NAME] == 1) {
             $name_values_array[USERLISTTABLEPERMISSIONS_CAN_EDIT_LIST_FIELD_NAME] = 1;
         }
     }
     # if user can edit list than user must also be able to view list
     if (array_key_exists(USERLISTTABLEPERMISSIONS_CAN_EDIT_LIST_FIELD_NAME, $name_values_array) == TRUE) {
         if ($name_values_array[USERLISTTABLEPERMISSIONS_CAN_EDIT_LIST_FIELD_NAME] == 1) {
             $name_values_array[USERLISTTABLEPERMISSIONS_CAN_VIEW_LIST_FIELD_NAME] = 1;
         }
     }
     if (parent::update($encoded_key_string, $name_values_array) == FALSE) {
         return FALSE;
     }
     $this->_log->trace("list permission updated (encoded_key_string=" . $encoded_key_string . ")");
     return TRUE;
 }