You are here

function resource_conflict_nodeapi in Resource Conflict 6.2

Same name and namespace in other branches
  1. 5.2 resource_conflict.module \resource_conflict_nodeapi()
  2. 5 resource_conflict.module \resource_conflict_nodeapi()
  3. 7.2 resource_conflict.module \resource_conflict_nodeapi()

Implementation of hook_nodeapi

Determine if a required resource is currently booked.

File

./resource_conflict.module, line 9

Code

function resource_conflict_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
  switch ($op) {
    case 'validate':
      $type = $node->type;
      if (!variable_get('rc_type_' . $type, FALSE)) {

        // Conflict handling is not enabled for this node.
        break;
      }

      // Find the date field to use for time overlapping_node_ids detection.
      $date_field = variable_get('rc_date_field_' . $type, FALSE);
      $overlapping_node_ids = array();
      if (strpos($date_field, 'field_', 0) === 0) {

        // Get the start and end Date of the current node
        $start = $node->{$date_field}[0]['value'];
        $end = $node->{$date_field}[0]['value2'];

        // Get all conflicting Date nodes
        if (!empty($start) && !empty($end)) {
          $overlapping_node_ids = _resource_conflict_overlaps_from_date($start, $end);
        }
        else {

          // If we got here, someone broke the requirements, so turn off
          // resource conflict for this type and notify an admin
          _resource_conflict_disable($type);
        }
      }
      if (!empty($node->event_start)) {

        // Get all overlapping events
        $tmp = _resource_conflict_overlaps_from_event($node->event_start, $node->event_end);
        $overlapping_node_ids = array_unique(array_merge($overlapping_node_ids, $tmp));
      }

      // Load conflicting nodes
      $conflicting_nodes = array();
      foreach ($overlapping_node_ids as $nid) {

        // Don't have the node conflict with itself
        if ($nid != $node->nid) {
          $conflicting_nodes[$nid] = node_load($nid);
        }
      }

      // Display conflict errors
      _resource_conflict_display_conflict_errors($node, $conflicting_nodes);
      break;
  }
}