You are here

function resource_conflict_nodeapi in Resource Conflict 5

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

Implementation of hook_nodeapi

Determine if a CCK nodereferenced resource is currently booked.

File

./resource_conflict.module, line 9

Code

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

        // Find the relevant fields to check against
        $fields = variable_get('resource_conflict_fields_' . $type, FALSE);
        if (empty($fields)) {
          break;
        }

        // Get all overlapping events
        $overlap = _resource_conflict_event_overlap($node->event_start, $node->event_end);
        if (empty($overlap)) {
          break;
        }
        foreach ($overlap as $nid) {

          // Don't have the node conflict with itself
          if ($nid != $node->nid) {
            $overlap_nodes[] = node_load($nid);
          }
        }
        if (empty($overlap_nodes)) {
          break;
        }
        foreach ($fields as $field) {
          foreach ($node->{$field} as $instance) {
            foreach ($overlap_nodes as $conflict) {

              // If there is empty variable $conflict->{$field} when you use more then one resource conflict contents
              if (empty($conflict->{$field})) {
                continue;
              }
              foreach ($conflict->{$field} as $conflict_instance) {
                if ($instance['nid'] == $conflict_instance['nid']) {
                  $booked_resource = node_load($instance['nid']);
                  form_set_error('Resource Conflict', t('There is a resource conflict. "%resource" is currently booked. Please choose a different time or a different resource.', array(
                    '%resource' => $booked_resource->title,
                  )));
                }
              }
            }
          }
        }
      }
      break;
  }
}