You are here

function basic_cart_order_view in Basic cart 7.3

Same name and namespace in other branches
  1. 7.2 basic_cart_order/basic_cart_order.module \basic_cart_order_view()

Implements hook_node_view().

1 string reference to 'basic_cart_order_view'
basic_cart_order_menu in basic_cart_order/basic_cart_order.module
Implements hook_menu().

File

basic_cart_order/basic_cart_order.module, line 13

Code

function basic_cart_order_view($node) {
  $oid = $node->nid;

  // Getting the order products.
  $order_products = db_select('basic_cart_order_node')
    ->fields('basic_cart_order_node')
    ->condition('oid', $oid)
    ->execute()
    ->fetchAll();

  // Building the products array.
  if (is_array($order_products)) {
    $products = array();
    foreach ($order_products as $product) {
      $p = node_load($product->nid);
      $p->basic_cart_quantity = $product->quantity;

      // Price in a nicer form.
      $price = field_get_items('node', $p, 'price');
      $price = isset($price[0]['value']) ? check_plain($price[0]['value']) : '';
      $p->price = basic_cart_price_format($price);
      $products[] = $p;
    }
  }

  // Building the order variables.

  //$order = node_load($oid);
  $order = $node;

  // Name.
  $name = check_plain($order->title);

  // Email.
  $email = field_get_items('node', $order, 'field_email');
  $email = isset($email[0]['value']) ? check_plain($email[0]['value']) : '';

  // Phone.
  $phone = field_get_items('node', $order, 'field_phone');
  $phone = isset($phone[0]['value']) ? check_plain($phone[0]['value']) : '';

  // City.
  $city = field_get_items('node', $order, 'field_city');
  $city = isset($city[0]['value']) ? check_plain($city[0]['value']) : '';

  // Zip Code.
  $zipcode = field_get_items('node', $order, 'field_zipcode');
  $zipcode = isset($zipcode[0]['value']) ? check_plain($zipcode[0]['value']) : '';

  // Adsress.
  $address = field_get_items('node', $order, 'field_address');
  $address = isset($address[0]['value']) ? check_plain($address[0]['value']) : '';

  // Message.
  $message = field_get_items('node', $order, 'body');
  $message = isset($message[0]['value']) ? check_plain($message[0]['value']) : '';

  // Total price.
  $total_price = field_get_items('node', $order, 'field_total_price');
  $total_price = isset($total_price[0]['value']) ? check_plain($total_price[0]['value']) : '';
  $total_price = basic_cart_price_format($total_price);

  // VAT.
  $vat = NULL;
  $vat_is_enabled = (int) variable_get('basic_cart_vat_state');
  if (!empty($vat_is_enabled) && $vat_is_enabled) {
    $vat = field_get_items('node', $order, 'vat');
    $vat = isset($vat[0]['value']) ? check_plain($vat[0]['value']) : '';
    $vat = basic_cart_price_format($vat);
  }
  $pid = db_query("SELECT pid FROM {basic_cart_order_payment} WHERE oid = :oid", array(
    ':oid' => $oid,
  ))
    ->fetchAssoc();
  if (isset($pid['pid'])) {
    $pid = $pid['pid'];
  }
  else {
    drupal_set_message(t('Error: empty payment ID.'), 'error');
  }
  $payment = entity_load_single('payment', $pid);
  if (payment_access('view', $payment)) {
    $view = ' (' . l(t('View payment'), 'payment/' . $payment->pid) . ')';
  }
  if (payment_status_is_or_has_ancestor($payment
    ->getStatus()->status, PAYMENT_STATUS_PENDING)) {
    $status = 'PENDING' . $view;
  }
  elseif (payment_status_is_or_has_ancestor($payment
    ->getStatus()->status, PAYMENT_STATUS_SUCCESS)) {
    $status = 'SUCCESS' . $view;
  }
  elseif (payment_status_is_or_has_ancestor($payment
    ->getStatus()->status, PAYMENT_STATUS_FAILED)) {
    $status = 'FAILED' . $view;
  }

  // Building the payment information array.
  $payment_arr = array(
    'status' => $status,
    'method' => $payment->method->title_specific,
  );

  // Hide field data.
  unset($node->content['field_email']);
  unset($node->content['field_phone']);
  unset($node->content['field_city']);
  unset($node->content['field_zipcode']);
  unset($node->content['field_address']);
  unset($node->content['field_total_price']);

  // Page title.
  $title = t('Order ID: @oid', array(
    '@oid' => $oid,
  ));
  drupal_set_title($title);
  return theme('basic_cart_order_details', array(
    'name' => $name,
    'email' => $email,
    'phone' => $phone,
    'city' => $city,
    'zipcode' => $zipcode,
    'address' => $address,
    'message' => $message,
    'products' => $products,
    'total_price' => $total_price,
    'vat' => $vat,
    'timestamp' => $node->created,
    'payment' => $payment_arr,
  ));
}