You are here

function merci_commerce_field_formatter_view in MERCI (Manage Equipment Reservations, Checkout and Inventory) 7.3

Implements hook_field_formatter_view().

File

merci_commerce/merci_commerce.module, line 240

Code

function merci_commerce_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $result = array();
  $element['sums'] = array();
  $element['products'] = array();

  // Collect the list of line item IDs.
  $line_item_ids = array();
  switch ($display['type']) {
    case 'merci_commerce_reference_view':
      $display_tmp = $display;
      $display_tmp['type'] = 'commerce_product_reference_sku_link';
      $result = commerce_product_reference_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display_tmp);
      foreach ($items as $delta => $item) {
        $product_ids[$delta] = $item['product_id'];
      }

      // Load any current checked out line items for the product.
      $query = new EntityFieldQuery();
      $query
        ->entityCondition('entity_type', 'merci_line_item')
        ->fieldCondition(MERCI_CHECKOUT_STATUS, 'value', 'checked out', '=')
        ->fieldCondition(MERCI_RESOURCE_REFERENCE, 'target_id', $product_ids, 'IN');
      $query_result = $query
        ->execute();
      $statuses = array();
      if (isset($query_result['merci_line_item'])) {
        $line_items_nids = array_keys($query_result['merci_line_item']);
        $line_items = entity_load('merci_line_item', $line_items_nids);
        foreach ($line_items as $line_item) {
          $product_id = $line_item->{MERCI_RESOURCE_REFERENCE}['und'][0]['target_id'];
          $status = $line_item->{MERCI_CHECKOUT_STATUS}['und'][0]['value'];
          $statuses[array_search($product_id, $product_ids)] = $line_item->entity_id;
        }
      }
      $products = entity_load('commerce_product', $product_ids);
      $sum = array();
      foreach ($product_ids as $delta => $product_id) {
        $markup = drupal_render($result[$delta]);
        if (array_key_exists($delta, $statuses)) {
          $checkout = entity_load_single('merci_reservation', $statuses[$delta]);
          $checkout_wrapper = entity_metadata_wrapper('merci_reservation', $checkout);
          $patron = $checkout_wrapper->merci_customer_billing
            ->value();
          $homeroom = $checkout_wrapper->merci_customer_homeroom
            ->value();
          $organization = $checkout_wrapper->merci_customer_organization
            ->value();
          unset($location);
          if ($patron) {
            $location = $checkout_wrapper->merci_customer_billing->field_full_name
              ->value();
          }
          if ($organization) {
            $location = $checkout_wrapper->merci_customer_organization->field_full_name
              ->value();
          }
          if ($homeroom) {
            $room = $checkout_wrapper->merci_customer_homeroom->field_full_name
              ->value();
            $location = isset($location) ? $location . '-' . $room : $room;
          }
          $result[$delta] = array(
            '#type' => 'link',
            '#title' => $location,
            '#href' => 'merci_reservation/' . $statuses[$delta] . '/edit',
          );
          $result[$delta] = $markup . ' (' . drupal_render($result[$delta]) . ')';
          if (!array_key_exists('Checked out', $sum)) {
            $sum['Checked out'] = 1;
          }
          else {
            $sum['Checked out']++;
          }
        }
        else {
          $product = $products[$product_ids[$delta]];
          $product_wrapper = entity_metadata_wrapper('commerce_product', $product);
          $status = $product_wrapper->merci_resource_status
            ->value();
          if (empty($status)) {
            $status = 'available';
          }
          if (!array_key_exists($status, $sum)) {
            $sum[$status] = 1;
          }
          else {
            $sum[$status]++;
          }
          if ($status != 'available') {
            $result[$delta] = $markup . ' (' . ucfirst($status) . ')';
          }
          else {
            $result[$delta] = $markup;
          }
        }
      }
      foreach ($sum as $key => $value) {
        $element['sums'][$key] = ucfirst($key) . ': ' . $value;
      }
      $element['sums'] = array(
        '#markup' => implode(', ', $element['sums']),
      );
      $element['products'] = array(
        '#markup' => implode('<br> ', $result),
      );
      break;
  }
  return $element;
}