You are here

function merci_delete_item_validate in MERCI (Manage Equipment Reservations, Checkout and Inventory) 6

Same name and namespace in other branches
  1. 6.2 merci.module \merci_delete_item_validate()
  2. 7.2 merci.module \merci_delete_item_validate()

Validates if an item node can be deleted.

Parameters

$node: The item node.

$single: TRUE if a single item node deletion is being processed, FALSE otherwise.

Return value

TRUE if the item can be deleted, FALSE otherwise.

2 calls to merci_delete_item_validate()
merci_form_alter in ./merci.module
Implementation of hook_form_alter().
merci_node_admin_delete_validate in ./merci.module
Custom validation function to protect merci nodes from mass deletion.

File

./merci.module, line 1506
MERCI - Managed Equipment Reservation Checkout and Inventory

Code

function merci_delete_item_validate($node, $single = TRUE) {

  // Only validate bucket/resource items.
  if ($node->type != 'merci_reservation' && $node->merci['type_setting'] != 'disabled' && $node->merci['sub_type'] == MERCI_SUB_TYPE_ITEM) {

    // Determine CCK table and columns the date data is stored in.
    $field = content_fields('field_merci_date');
    $db_info = content_database_info($field);
    $table = $db_info['table'];

    // Join on nid here so that any version of the reservation that contain
    // the item is caught.
    $reservations = db_query("SELECT ctn.nid, ctn.title FROM {" . $table . "} ct INNER JOIN {merci_reservation_detail} md ON ct.vid = md.vid INNER JOIN {node} ctn ON ct.nid = ctn.nid WHERE md.item_nid = %d ORDER BY ct.nid, ct.vid", $node->nid);
    $bad_reservations = array();
    while ($reservation = db_fetch_object($reservations)) {

      // Key by nid to prevent duplicate revisions from appearing.
      $bad_reservations[$reservation->nid] = l($reservation->title, "node/{$reservation->nid}/edit", array(
        'query' => drupal_get_destination(),
      ));
    }
    if (!empty($bad_reservations)) {
      drupal_set_message(t('%title can not be deleted, because it is associated with the following reservations:', array(
        '%title' => $node->title,
      )) . theme('item_list', $bad_reservations), 'error');

      // Lock out single deletion attempts here.
      if ($single) {
        module_invoke_all('exit');
        drupal_access_denied();
      }
      else {
        return FALSE;
      }
    }
  }
  return TRUE;
}