View source
<?php
include_once 'basic_cart_order.features.inc';
module_load_include('inc', 'basic_cart', 'basic_cart.cart');
function basic_cart_order_view($node) {
$oid = $node->nid;
$order_products = db_select('basic_cart_order_node')
->fields('basic_cart_order_node')
->condition('oid', $oid)
->execute()
->fetchAll();
if (is_array($order_products)) {
$products = array();
foreach ($order_products as $product) {
$p = node_load($product->nid);
$p->basic_cart_quantity = $product->quantity;
$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;
}
}
$order = $node;
$name = check_plain($order->title);
$email = field_get_items('node', $order, 'field_email');
$email = isset($email[0]['value']) ? check_plain($email[0]['value']) : '';
$phone = field_get_items('node', $order, 'field_phone');
$phone = isset($phone[0]['value']) ? check_plain($phone[0]['value']) : '';
$city = field_get_items('node', $order, 'field_city');
$city = isset($city[0]['value']) ? check_plain($city[0]['value']) : '';
$zipcode = field_get_items('node', $order, 'field_zipcode');
$zipcode = isset($zipcode[0]['value']) ? check_plain($zipcode[0]['value']) : '';
$address = field_get_items('node', $order, 'field_address');
$address = isset($address[0]['value']) ? check_plain($address[0]['value']) : '';
$message = field_get_items('node', $order, 'body');
$message = isset($message[0]['value']) ? check_plain($message[0]['value']) : '';
$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 = 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;
}
$payment_arr = array(
'status' => $status,
'method' => $payment->method->title_specific,
);
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']);
$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,
));
}
function basic_cart_order_menu() {
$items = array();
$items['admin/structure/orders/%node'] = array(
'title' => 'Basic Cart Orders',
'page callback' => 'basic_cart_order_view',
'page arguments' => array(
3,
),
'access arguments' => array(
'view basic cart orders',
),
'type' => MENU_CALLBACK,
);
$items['admin/config/basic_cart/settings/checkout'] = array(
'title' => 'Checkout',
'description' => 'Basic cart checkout settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'basic_cart_admin_checkout',
),
'access arguments' => array(
'administer basic cart',
),
'type' => MENU_LOCAL_TASK,
'file' => 'basic_cart_order.admin.inc',
'weight' => 10,
);
$items['checkout'] = array(
'title' => 'Checkout',
'description' => 'Checkout.',
'page callback' => 'basic_cart_order_checkout',
'access arguments' => array(
'use basic cart',
),
'type' => MENU_CALLBACK,
);
$items['checkout/thank-you'] = array(
'title' => 'Thank you',
'description' => 'Checkout thank you page.',
'page callback' => 'basic_cart_order_checkout_thank_you',
'access arguments' => array(
'use basic cart',
),
'type' => MENU_CALLBACK,
);
return $items;
}
function basic_cart_order_theme() {
return array(
'basic_cart_order_details' => array(
'template' => 'basic_cart_order_details',
'variables' => array(
'name' => NULL,
'email' => NULL,
'phone' => NULL,
'city' => NULL,
'zipcode' => NULL,
'address' => NULL,
'message' => NULL,
'products' => NULL,
'total_price' => NULL,
'vat' => NULL,
'timestamp' => NULL,
'payment' => NULL,
),
),
);
}
function basic_cart_order_checkout() {
$shopping_cart = basic_cart_get_cart();
$price = basic_cart_get_total_price();
$total = basic_cart_price_format($price->total);
$options = array(
'cart' => $shopping_cart,
'price' => $total,
);
$vat_is_enabled = (int) variable_get('basic_cart_vat_state');
if (!empty($vat_is_enabled) && $vat_is_enabled) {
$options['vat'] = basic_cart_price_format($price->vat);
}
$cart = theme('basic_cart_cart_flat', $options);
if (empty($shopping_cart)) {
return $cart;
}
module_load_include('inc', 'node', 'node.pages');
$form = node_add('order');
unset($form['field_total_price']);
unset($form['vat']);
$form['actions']['submit']['#value'] = t('Submit order');
$form = drupal_render($form);
return $cart . $form;
}
function basic_cart_order_send_notifications($order) {
$message_html = t(variable_get('basic_cart_admin_message'));
$data = array(
'node' => $order,
);
$message_html = token_replace($message_html, $data, array(
'callback' => 'basic_cart_order_tokens_format',
));
$params['admin_message'] = $message_html;
$site_mail = variable_get('site_mail');
$admin_emails = variable_get('basic_cart_admin_emails');
if (empty($admin_emails)) {
$message = drupal_mail('basic_cart_order', 'admin_mail', $site_mail, language_default(), $params);
$mails_sent = 0;
if ($message['result']) {
$mails_sent++;
}
}
else {
$admin_emails = explode("\n", $admin_emails);
if (is_array($admin_emails) && !empty($admin_emails)) {
$ok = FALSE;
foreach ($admin_emails as $admin_email) {
$message = drupal_mail('basic_cart_order', 'admin_mail', $admin_email, language_default(), $params);
if ($message['result']) {
$ok = TRUE;
}
}
$mails_sent = 0;
if ($ok) {
$mails_sent++;
}
}
}
$send_user_mail = variable_get('basic_cart_send_user_message');
if ($send_user_mail) {
$message_html = t(variable_get('basic_cart_user_message'));
$data = array(
'node' => $order,
);
$message_html = token_replace($message_html, $data, array(
'callback' => 'basic_cart_order_tokens_format',
));
$params['user_message'] = $message_html;
$email = field_get_items('node', $order, 'field_email');
$email = $email[0]['value'];
$message = drupal_mail('basic_cart_order', 'user_mail', $email, language_default(), $params);
if ($message['result']) {
$mails_sent++;
}
}
return $mails_sent;
}
function basic_cart_order_mail($key, &$message, $params) {
switch ($key) {
case 'admin_mail':
$message['subject'] = t(check_plain(variable_get('basic_cart_admin_subject')));
$message['body'][] = filter_xss($params['admin_message']);
break;
case 'user_mail':
$message['subject'] = t(check_plain(variable_get('basic_cart_user_subject')));
$message['body'][] = filter_xss($params['user_message']);
break;
}
}
function basic_cart_order_checkout_thank_you() {
$title = t(variable_get('basic_cart_thank_you_title'));
drupal_set_title($title);
$message = t(variable_get('basic_cart_thank_you_message'));
return filter_xss($message);
}
function basic_cart_order_node_insert($node) {
if ($node->type == 'order') {
$cart = basic_cart_get_cart();
if (!is_array($cart)) {
return NULL;
}
$price = basic_cart_get_total_price();
$node->field_total_price[$node->language][0]['value'] = $price->total;
$node->field_total_price[LANGUAGE_NONE][0]['value'] = $price->total;
$vat_is_enabled = (int) variable_get('basic_cart_vat_state');
if (!empty($vat_is_enabled) && $vat_is_enabled) {
$node->vat[$node->language][0]['value'] = $price->vat;
$node->vat[LANGUAGE_NONE][0]['value'] = $price->vat;
}
field_attach_update('node', $node);
$oid = $node->nid;
foreach ($cart as $n) {
$record = array(
'oid' => $oid,
'nid' => $n->nid,
'quantity' => $n->basic_cart_quantity,
);
drupal_write_record('basic_cart_order_node', $record);
}
if (module_exists('basic_cart_payment')) {
$pid = basic_cart_payment_pid();
if (!empty($pid)) {
db_insert('basic_cart_order_payment')
->fields(array(
'oid' => $oid,
'pid' => $pid,
))
->execute();
}
}
$emails = basic_cart_order_send_notifications($node);
if ($emails >= 1) {
basic_cart_empty_cart();
}
else {
drupal_set_message(t('There was a problem in submitting your order. Please try again later.'), 'error');
}
}
}
function basic_cart_order_tokens_format(&$tokens) {
if (is_array($tokens) && count($tokens) > 0) {
foreach ($tokens as $token => $value) {
switch ($token) {
case '[node:field_total_price]':
$tokens[$token] = basic_cart_price_format($value);
break;
}
}
}
return $tokens;
}
function basic_cart_order_token_info() {
$info = array();
$info['tokens']['basic_cart_order']['products'] = array(
'name' => t('Product listing'),
'description' => t('Listing of ordered products.'),
);
return $info;
}
function basic_cart_order_tokens($type, $tokens, array $data = array(), array $options = array()) {
$replacements = array();
if ($type == 'basic_cart_order') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'products':
$new = '';
$cart = basic_cart_get_cart();
$i = 0;
$products = '';
foreach ($cart as $nid => $node) {
$unit_price = basic_cart_price_format($node->basic_cart_unit_price);
$new .= ++$i . '. ' . $node->title . "\t" . $node->basic_cart_quantity . ' x ' . $unit_price . "\n";
}
$replacements[$original] = $new;
break;
}
}
}
return $replacements;
}