You are here

public function MerciDefaultController::getErrors in MERCI (Manage Equipment Reservations, Checkout and Inventory) 7.3

File

merci_core/reservation.handler.inc, line 95
Abstraction of the selection logic of an entity reference field.

Class

MerciDefaultController
A null implementation of EntityReference_SelectionHandler.

Code

public function getErrors($delta = NULL, $dates = array()) {

  // Determine if reserving too many of the same item.
  if ($this->errors === NULL) {
    $this
      ->validate();
    $entity = $this->entity;
    $entity_type = $entity
      ->type();
    $context = $this->context;
    $errors = array();
    if ($this->items_is_list) {
      foreach ($entity->{$context['item_field']}
        ->getIterator() as $delta => $resource) {
        $item_id = $resource
          ->getIdentifier();
        if (empty($item_id)) {
          continue;
        }
        if (empty($item_count[$item_id])) {
          $item_count[$item_id] = 0;
        }
        $item_count[$item_id]++;
        try {
          $quantity_reservable = $resource->field_quantity
            ->value();
        } catch (EntityMetadataWrapperException $e) {
          $quantity_reservable = 1;
        }
        if ($item_count[$item_id] > $quantity_reservable) {

          // Selected to many.
          if (!array_key_exists($delta, $errors)) {
            $errors[$delta] = array();
          }
          $parents_path = implode('][', array(
            $context['item_field'],
            'und',
            $delta,
            'target_id',
          ));
          $errors[$delta][MERCI_ERROR_TOO_MANY] = t('%name: You have selected too many of the same item.  We only have %quantity available but you reserved %reserved.', array(
            '%name' => $resource
              ->label(),
            '%quantity' => $quantity_reservable,
            '%reserved' => $item_count[$item_id],
          ));
        }
      }
    }
    else {
      $resource = $entity->{$context['item_field']};
      $item_count[$resource
        ->getIdentifier()] = $this->entity_quantity;
      try {
        $quantity_reservable = $resource->field_quantity
          ->value();
      } catch (EntityMetadataWrapperException $e) {
        $quantity_reservable = 1;
      }
      if ($this->entity_quantity > $quantity_reservable) {
        if (!array_key_exists($delta, $errors)) {
          $errors[$delta] = array();
        }
        $errors[$delta][MERCI_ERROR_TOO_MANY] = t('%name: You have selected too many of the same item.  We only have %quantity available but you reserved %reserved.', array(
          '%name' => $resource
            ->label(),
          '%quantity' => $quantity_reservable,
          '%reserved' => $this->entity_quantity,
        ));

        // Selected to many.
      }
    }
    $reserved = $this
      ->getQuantityReserved();
    $reserved = $reserved ? $reserved : array();
    $reserved_so_far_by_me = array();
    foreach ($reserved as $delta => $start_dates) {
      $conflict_errors = array();

      // Load the resource being reserved.
      if ($this->items_is_list) {
        $resource = $entity->{$context['item_field']}[$delta];
      }
      else {
        $resource = $entity->{$context['item_field']};
      }

      // Determine if the quantity field exists.  If so use it.
      try {
        $quantity_reservable = $resource->field_quantity
          ->value();
      } catch (EntityMetadataWrapperException $e) {
        $quantity_reservable = 1;
      }
      $item_id = $resource
        ->getIdentifier();
      if (empty($reserved_so_far_by_me[$item_id])) {
        $reserved_so_far_by_me[$item_id] = 0;
      }
      $reserved_so_far_by_me[$item_id]++;
      foreach ($this->dates as $dates) {
        $quantity_reserved = $this
          ->getQuantityReserved($delta, $dates);

        // Determine if there are conflicts for this date and item.
        if ($quantity_reservable >= $quantity_reserved + $reserved_so_far_by_me[$item_id]) {
          continue;
        }

        // Load each conflicting entity so we can show information about it to
        // the user.
        $ids = array();
        foreach ($this
          ->getConflicts($delta, $dates) as $conflict) {
          $ids[] = $conflict->parent_id;
        }

        // Load the entities which hold the conflicting item.
        $entities = entity_load($entity_type, $ids);
        $line_items = array();
        foreach ($entities as $id => $line_item) {
          $entity_uri = entity_uri($entity_type, $line_item);
          $entity_label = entity_label($entity_type, $line_item);
          $line_items[] = l(t("@label", array(
            '@label' => $entity_label,
          )), $entity_uri['path']);
        }
        $date_start = $dates['value'];

        // Don't show the date repeat rule in the error message.
        unset($dates['rrule']);
        $render_dates = field_view_value($entity_type, $entity
          ->value(), $context['date_field'], $dates);
        $conflict_errors[$date_start] = t('%name is already reserved by: !items for selected dates !dates', array(
          '%name' => $resource
            ->label(),
          '!items' => implode(', ', $line_items),
          '!dates' => render($render_dates),
        ));
      }
      if ($conflict_errors) {
        if (!array_key_exists($delta, $errors)) {
          $errors[$delta] = array();
        }
        $errors[$delta][MERCI_ERROR_CONFLICT] = $conflict_errors;
      }
    }
    $this->errors = $errors;
  }
  return $this->errors;
}