You are here

function _resource_conflict_display_conflict_errors in Resource Conflict 5.2

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

Compare our demand to the demand of overlapping nodes and display errors for the intersections

Parameters

$node the currently validating node:

$conflicting_nodes array of nodes that overlap with this node:

1 call to _resource_conflict_display_conflict_errors()
resource_conflict_nodeapi in ./resource_conflict.module
Implementation of hook_nodeapi

File

./resource_conflict.module, line 67

Code

function _resource_conflict_display_conflict_errors($node, $conflicting_nodes) {
  $our_demands = _resource_conflict_get_node_resource_demand($node);
  foreach ($conflicting_nodes as $conflicting_node) {
    $other_demands = _resource_conflict_get_node_resource_demand($conflicting_node);

    // select the resources that both this node and the other node demands
    $conflicting_resources = array();
    foreach ($our_demands as $our_demand => $dummy) {
      if ($other_demands[$our_demand]) {
        $conflicting_resources[$our_demand] = node_load($our_demand);
      }
    }

    // display the error for each conflict
    foreach ($conflicting_resources as $conflicting_resource) {
      $date_field = variable_get('rc_date_field_' . $conflicting_node->type, FALSE);
      if (strpos($date_field, 'field_', 0) === 0) {
        $start = format_date(date_convert($conflicting_node->{$date_field}[0]['value'], DATE_ISO, DATE_UNIX));
        $end = format_date(date_convert($conflicting_node->{$date_field}[0]['value2'], DATE_ISO, DATE_UNIX));
      }
      else {
        $start = format_date($conflicting_node->event_start);
        $end = format_date($conflicting_node->event_end);
      }
      $error = t('There is a resource conflict: <a href="@resource-url">%resource</a> is currently booked for <a href="@booker-url">%booker</a> from %start to %end. Please choose a different time or a different resource.', array(
        '@booker-url' => url('node/' . $conflicting_node->nid),
        '%booker' => $conflicting_node->title,
        '@resource-url' => url('node/' . $conflicting_resource->nid),
        '%resource' => $conflicting_resource->title,
        '%start' => $start,
        '%end' => $end,
      ));

      // This is a bit of a hack, but there's no way with FAPI to have
      // multiple form errors on the same field.  So, we just pass in
      // a bogus (but unique) ID for each error message, to ensure
      // that all conflicts are reported simultaneously.  We use the
      // nid of the conflicting resource (thing) appended with the nid
      // of the conflicting node (reservation event).
      $conflict_id = $conflicting_resource->nid . '_' . $conflicting_node->nid;
      form_set_error($conflict_id, $error);
    }
  }
}