You are here

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

Same name and namespace in other branches
  1. 6.2 merci.module \merci_nodeapi()

Implementation of hook_nodeapi().

File

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

Code

function merci_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if (($cache = cache_get('merci_' . $node->type . '_data')) && !empty($cache->data)) {
    $merci_node_type = $cache->data;
  }
  else {

    // Load MERCI node type settings.
    $merci_node_type = db_fetch_array(db_query("SELECT * FROM {merci_node_type} WHERE type = '%s' AND type_setting <> 'disabled'", $node->type));

    // Do your expensive calculations here, and populate $my_data
    // with the correct stuff..
    if ($merci_node_type) {
      cache_set('merci_' . $node->type . '_data', $merci_node_type);
    }
  }

  // Process active MERCI node types and reservation nodes.
  if ($merci_node_type || $node->type == 'merci_reservation') {
    $type = $node->type == 'merci_reservation' ? 'reservation' : $merci_node_type['type_setting'];
    switch ($op) {
      case 'prepare':
      case 'load':
        if (isset($node->nid)) {
          switch ($type) {
            case 'bucket':
              $merci_node = db_fetch_array(db_query("SELECT default_availability, late_fee_per_hour, rate_per_hour, fee_free_hours, min_cancel_hours, autocheckout, autocheckin, selfcheckout, sub_type FROM {merci_bucket_node} WHERE vid = %d", $node->vid));
              break;
            case 'resource':
              $merci_node = db_fetch_array(db_query("SELECT default_availability, late_fee_per_hour, rate_per_hour, fee_free_hours, min_cancel_hours, autocheckout, autocheckin, selfcheckout, sub_type FROM {merci_resource_node} WHERE vid = %d", $node->vid));
              break;
          }
        }
        if ($merci_node_type) {
          if (isset($merci_node)) {
            $node->merci = array_merge($merci_node_type, $merci_node);
          }
          else {
            $node->merci = $merci_node_type;
          }
        }
        break;
      case 'validate':
        if ($type != 'reservation') {
          merci_validate_default_availability($node);
        }
        break;
      case 'insert':
      case 'update':
        if ($op == 'insert' || $node->revision) {
          switch ($type) {
            case 'bucket':
              db_query("INSERT INTO {merci_bucket_node} (nid, vid, default_availability, late_fee_per_hour, rate_per_hour, fee_free_hours, min_cancel_hours, autocheckout, autocheckin, selfcheckout, sub_type) VALUES (%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d)", $node->nid, $node->vid, $node->merci_default_availability, $node->merci_late_fee_per_hour, $node->merci_rate_per_hour, $node->merci_fee_free_hours, $node->merci_min_cancel_hours, $node->merci_autocheckout, $node->merci_autocheckin, $node->merci_selfcheckout, $node->merci_sub_type);
              break;
            case 'resource':
              db_query("INSERT INTO {merci_resource_node} (nid, vid, default_availability, late_fee_per_hour, rate_per_hour, fee_free_hours, min_cancel_hours, autocheckout, autocheckin, selfcheckout, sub_type) VALUES (%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d)", $node->nid, $node->vid, $node->merci_default_availability, $node->merci_late_fee_per_hour, $node->merci_rate_per_hour, $node->merci_fee_free_hours, $node->merci_min_cancel_hours, $node->merci_autocheckout, $node->merci_autocheckin, $node->merci_selfcheckout, $node->merci_sub_type);
              break;
          }
        }
        else {
          switch ($type) {
            case 'bucket':
              db_query("UPDATE {merci_bucket_node} SET default_availability = %d, late_fee_per_hour = %d, rate_per_hour = %d, fee_free_hours = %d, autocheckout = %d, autocheckin = %d, selfcheckout = %d WHERE vid = %d", $node->merci_default_availability, $node->merci_late_fee_per_hour, $node->merci_rate_per_hour, $node->merci_fee_free_hours, $node->merci_min_cancel_hours, $node->merci_autocheckout, $node->merci_autocheckin, $node->merci_selfcheckout, $node->vid);
              break;
            case 'resource':
              db_query("UPDATE {merci_resource_node} SET default_availability = %d, late_fee_per_hour = %d, rate_per_hour = %d, fee_free_hours = %d, autocheckout = %d, autocheckin = %d, selfcheckout = %d WHERE vid = %d", $node->merci_default_availability, $node->merci_late_fee_per_hour, $node->merci_rate_per_hour, $node->merci_fee_free_hours, $node->merci_min_cancel_hours, $node->merci_autocheckout, $node->merci_autocheckin, $node->merci_selfcheckout, $node->vid);
              break;
          }
        }
        break;
      case 'delete':

        // In the case were a reservation placeholder node is being
        // deleted, remove it from the detail table here.
        if ($type != 'reservation') {
          db_query("DELETE FROM {merci_reservation_detail} WHERE placeholder_nid = %d", $node->nid);
        }
        switch ($type) {
          case 'bucket':
            db_query("DELETE FROM {merci_bucket_node} WHERE nid = %d", $node->nid);
            break;
          case 'resource':
            db_query("DELETE FROM {merci_resource_node} WHERE nid = %d", $node->nid);
            break;
        }
        break;
      case 'delete revision':
        switch ($type) {
          case 'bucket':
            db_query("DELETE FROM {merci_bucket_node} WHERE vid = %d", $node->vid);
            break;
          case 'resource':
            db_query("DELETE FROM {merci_resource_node} WHERE vid = %d", $node->vid);
            break;
          case 'reservation':
            db_query("DELETE FROM {merci_reservation} WHERE vid = %d", $node->vid);
            db_query("DELETE FROM {merci_reservation_detail} WHERE vid = %d", $node->vid);
            break;
        }
        break;
    }
  }
}