function delete_item_cascaded($item_id) { if (db_query("LOCK TABLES item WRITE, item_attribute WRITE, item_instance WRITE")) { if (delete_item_attributes($item_id, NULL, NULL, NULL)) { if (delete_item($item_id)) { // Can't forget to unlock table. db_query("UNLOCK TABLES"); return TRUE; } else { // Can't forget to unlock table. db_query("UNLOCK TABLES"); return FALSE; } } else { // Can't forget to unlock table. db_query("UNLOCK TABLES"); return FALSE; } } else { opendb_logger(OPENDB_LOG_ERROR, __FILE__, __FUNCTION__, db_error(), array($item_id)); return FALSE; } }
function handle_item_delete($item_r, $status_type_r, $HTTP_VARS, &$errors, $delete_with_closed_borrow_records = NULL) { if ($item_r['owner_id'] != get_opendb_session_var('user_id') && !is_user_granted_permission(PERM_ITEM_ADMIN)) { $errors = array('error' => get_opendb_lang_var('cannot_delete_item_not_owned'), 'detail' => ''); opendb_logger(OPENDB_LOG_WARN, __FILE__, __FUNCTION__, 'User to delete item instance they do not own', $item_r); return FALSE; } if ($status_type_r['delete_ind'] != 'Y') { $errors = array('error' => get_opendb_lang_var('operation_not_avail_s_status_type', 's_status_type_desc', $status_type_r['description']), 'detail' => ''); opendb_logger(OPENDB_LOG_WARN, __FILE__, __FUNCTION__, 'Attempted to delete item instance with a status that prevents the item being deleted', $item_r); return FALSE; } if (is_exists_item_instance_relationship($item_r['item_id'], $item_r['instance_no'])) { $errors = array('error' => get_opendb_lang_var('item_not_deleted'), 'detail' => get_opendb_lang_var('item_related_to_other_items')); opendb_logger(OPENDB_LOG_WARN, __FILE__, __FUNCTION__, 'User attempted to to delete item with related items', $item_r); return FALSE; } if (is_item_reserved_or_borrowed($item_r['item_id'], $item_r['instance_no'])) { $errors = array('error' => get_opendb_lang_var('item_not_deleted'), 'detail' => get_opendb_lang_var('item_reserved_or_borrowed')); opendb_logger(OPENDB_LOG_WARN, __FILE__, __FUNCTION__, 'User attempted to to delete item with active reservation(s)', $item_r); return FALSE; } $inactive_borrowed_items_exist = FALSE; if (is_exists_item_instance_borrowed_item($item_r['item_id'], $item_r['instance_no'])) { if (get_opendb_config_var('item_input', 'allow_delete_with_closed_or_cancelled_borrow_records') !== TRUE) { $errors = array('error' => get_opendb_lang_var('item_not_deleted'), 'detail' => get_opendb_lang_var('item_has_inactive_borrowed_item')); opendb_logger(OPENDB_LOG_WARN, __FILE__, __FUNCTION__, 'User attempted to to delete item with inactive reservation(s)', $item_r); return FALSE; } else { $inactive_borrowed_items_exist = TRUE; } } if ($HTTP_VARS['confirmed'] == 'true' || $inactive_borrowed_items_exist !== TRUE && get_opendb_config_var('item_input', 'confirm_item_delete') === FALSE) { if ($inactive_borrowed_items_exist) { if (!delete_item_instance_inactive_borrowed_items($item_r['item_id'], $item_r['instance_no'])) { $db_error = db_error(); $errors = array('error' => get_opendb_lang_var('undefined_error'), 'detail' => $db_error); return FALSE; } } if (!is_exists_item_instance_borrowed_item($item_r['item_id'], $item_r['instance_no'])) { delete_related_item_instance_relationship($item_r['item_id'], $item_r['instance_no'], $HTTP_VARS['parent_item_id'], $HTTP_VARS['parent_instance_no']); if (!is_exists_related_item_instance_relationship($item_r['item_id'], $item_r['instance_no'])) { if (!delete_item_instance($item_r['item_id'], $item_r['instance_no'])) { $db_error = db_error(); $errors = array('error' => get_opendb_lang_var('item_not_deleted'), 'detail' => $db_error); return FALSE; } } else { $errors = array('error' => get_opendb_lang_var('item_not_deleted'), 'detail' => get_opendb_lang_var('item_related_to_other_items')); opendb_logger(OPENDB_LOG_WARN, __FILE__, __FUNCTION__, 'User attempted to to delete item with attached item instance relationship record(s)', $item_r); return FALSE; } } else { if (is_item_reserved_or_borrowed($item_r['item_id'], $item_r['instance_no'])) { $errors = array('error' => get_opendb_lang_var('item_not_deleted'), 'detail' => get_opendb_lang_var('item_reserved_or_borrowed')); } else { $errors = array('error' => get_opendb_lang_var('item_not_deleted'), 'detail' => get_opendb_lang_var('item_has_inactive_borrowed_item')); } return FALSE; } // If child and no more instance left, // proceed with item and item_attribute delete. if (!is_exists_item_instance($item_r['item_id'], NULL)) { // Get rid of all reviews. if (is_item_reviewed($item_r['item_id'])) { delete_reviews($item_r['item_id']); } delete_item_attributes($item_r['item_id'], $item_r['instance_no']); if (!delete_item($item_r['item_id'])) { $db_error = db_error(); $errors = array('error' => get_opendb_lang_var('item_not_deleted'), 'detail' => $db_error); return FALSE; } // As long as delete_item has worked, we do not care about anything else. return TRUE; } } else { if ($HTTP_VARS['confirmed'] != 'false') { if ($inactive_borrowed_items_exist) { return "__CONFIRM_INACTIVE_BORROW__"; } else { return "__CONFIRM__"; } } else { // confirmation required. return "__ABORTED__"; } } }
/** */ function _insert_or_update_item_attributes($item_id, $instance_no, $s_item_type, $s_attribute_type, $order_no, $attribute_val_r, $file_r = NULL) { $is_lookup_attribute_type = is_lookup_attribute_type($s_attribute_type); $attribute_val_r = validate_attribute_val_r($attribute_val_r, $is_lookup_attribute_type); // if not instance item attribute, then discard the $instance_no if (!is_instance_item_attribute_type($s_item_type, $s_attribute_type)) { $instance_no = NULL; } $is_file_resource_attribute_type = is_file_resource_attribute_type($s_attribute_type); if (db_query("LOCK TABLES item_attribute WRITE, item_attribute AS ia READ, s_attribute_type AS sat READ")) { $item_attribute_type_rs = fetch_arrayof_item_attribute_rs($item_id, $instance_no, $s_attribute_type, $order_no); // if same number of attributes, then we can perform an update only. if (count($item_attribute_type_rs) > 0 && count($item_attribute_type_rs) == count($attribute_val_r)) { $op = 'update'; } else { if (count($item_attribute_type_rs) == 0 || delete_item_attributes($item_id, $instance_no, $s_attribute_type, $order_no)) { $op = 'insert'; } else { // if this occurs then the delete_item_attributes function returned FALSE, and that failure would have been logged. db_query("UNLOCK TABLES"); return FALSE; } } // if there is actually something to insert at this point. if (count($attribute_val_r) > 0) { $file_attributes_r = NULL; for ($i = 0; $i < count($attribute_val_r); $i++) { $attribute_no = $i + 1; if ($is_lookup_attribute_type) { if ($op == 'insert') { insert_item_attribute($item_id, $instance_no, $s_attribute_type, $order_no, $attribute_no, $attribute_val_r[$i], NULL); } else { if ($item_attribute_type_rs[$i]['lookup_attribute_val'] != $attribute_val_r[$i]) { update_item_attribute($item_id, $instance_no, $s_attribute_type, $order_no, $attribute_no, $attribute_val_r[$i], NULL); } } } else { if ($is_file_resource_attribute_type) { if (is_array($file_r) && is_uploaded_file($file_r['tmp_name'])) { if ($item_attribute_type_rs[$i]['attribute_val'] != $attribute_val_r[$i] && is_exists_upload_file_item_attribute($attribute_val_r[$i])) { $attribute_val_r[$i] = get_unique_filename($attribute_val_r[$i]); } if (!save_uploaded_file($file_r['tmp_name'], $attribute_val_r[$i])) { $attribute_val_r[$i] = NULL; } $file_attributes_rs[] = array('file_attribute_ind' => 'Y', 'attribute_no' => $attribute_no, 'attribute_val' => $attribute_val_r[$i]); } else { $file_attributes_rs[] = array('attribute_no' => $attribute_no, 'attribute_val' => $attribute_val_r[$i]); } } if (strlen($attribute_val_r[$i]) > 0) { if ($op == 'insert') { insert_item_attribute($item_id, $instance_no, $s_attribute_type, $order_no, $attribute_no, NULL, $attribute_val_r[$i]); } else { if ($item_attribute_type_rs[$i]['attribute_val'] != $attribute_val_r[$i]) { update_item_attribute($item_id, $instance_no, $s_attribute_type, $order_no, $attribute_no, NULL, $attribute_val_r[$i]); } } } } } db_query("UNLOCK TABLES"); if (is_array($file_attributes_rs)) { while (list(, $file_attribute_r) = each($file_attributes_rs)) { file_cache_insert_file($file_attribute_r['attribute_val'], NULL, NULL, NULL, 'ITEM', $file_attribute_r['file_attribute_ind'] == 'Y'); } } } else { db_query("UNLOCK TABLES"); } return TRUE; } else { opendb_logger(OPENDB_LOG_ERROR, __FILE__, __FUNCTION__, db_error(), array($item_id, $instance_no, $s_item_type, $s_attribute_type, $order_no, $attribute_val_r, $file_r)); return FALSE; } }