You are here

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

Same name and namespace in other branches
  1. 6.2 merci.module \merci_delete_item_validate()
  2. 6 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 includes/api.inc
Custom validation function to protect merci nodes from mass deletion.

File

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

Code

function merci_delete_item_validate($node) {

  // Only validate bucket/resource items.
  if ($node->type != 'merci_reservation' && isset($node->merci_type_setting) && $node->merci_type_setting != 'disabled' && isset($node->merci_sub_type) && $node->merci_sub_type == MERCI_SUB_TYPE_ITEM) {

    // Determine CCK table and columns the date data is stored in.
    $field = field_info_field('field_merci_date');
    $table = key($field['storage']['details']['sql']['FIELD_LOAD_CURRENT']);

    // Join on nid here so that any version of the reservation that contain
    // the item is caught.
    // Pull any reservations that use the item in question
    // TODO Please convert this statement to the D7 database API syntax.
    $reservations = db_query("SELECT n.nid, n.title FROM {node} n INNER JOIN {" . $table . "} ct ON ct.revision_id = n.vid INNER JOIN {merci_reservation_detail} md ON ct.revision_id = md.vid WHERE md.merci_item_nid = :merci_item_nid", array(
      ':merci_item_nid' => $node->nid,
    ));
    $bad_reservations = array();
    foreach ($reservations as $reservation) {

      // 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', array(
        'items' => $bad_reservations,
      )), 'error');

      // Lock out single deletion attempts here.
      return FALSE;
    }
  }
  return TRUE;
}