View source
<?php
class commerce_payment_handler_area_totals extends views_handler_area {
function init(&$view, &$options) {
parent::init($view, $options);
$this->additional_fields['amount'] = 'amount';
$this->additional_fields['currency_code'] = 'currency_code';
$this->additional_fields['status'] = 'status';
}
function option_definition() {
$options = parent::option_definition();
$options['add_payment_form'] = array(
'default' => TRUE,
);
return $options;
}
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$form['add_payment_form'] = array(
'#type' => 'checkbox',
'#title' => t('Display an add payment form in the totals area when using a single order argument.'),
'#description' => t('The argument should be setup using a Views relationship on the transaction Order ID.'),
'#default_value' => $this->options['add_payment_form'],
);
}
function get_value($values, $field = NULL) {
if (!isset($field)) {
return;
}
$aliases = array(
'status' => $this->view->query->fields['commerce_payment_transaction_status']['alias'],
'currency_code' => $this->view->query->fields['commerce_payment_transaction_currency_code']['alias'],
'amount' => $this->view->query->fields['commerce_payment_transaction_amount']['alias'],
);
$alias = $aliases[$field];
if (isset($values->{$alias})) {
return $values->{$alias};
}
}
function render($empty = FALSE) {
if (array_key_exists('order_id', $this->view->argument) && !array_key_exists('order_id_1', $this->view->argument) && !empty($this->view->args[$this->view->argument['order_id']->position])) {
$order = commerce_order_load($this->view->args[$this->view->argument['order_id']->position]);
}
else {
$order = FALSE;
}
$transaction_statuses = commerce_payment_transaction_statuses();
$totals = array();
foreach ($this->view->result as $result) {
$status = $this
->get_value($result, 'status');
$currency_code = $this
->get_value($result, 'currency_code');
$amount = $this
->get_value($result, 'amount');
if (!empty($transaction_statuses[$status]) && $transaction_statuses[$status]['total']) {
if (isset($totals[$currency_code])) {
$totals[$currency_code] += $amount;
}
else {
$totals[$currency_code] = $amount;
}
}
}
if ($this->options['add_payment_form'] && $order) {
module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.forms');
$content = drupal_get_form('commerce_payment_order_transaction_add_form', $order);
$form = drupal_render($content);
}
else {
$form = NULL;
}
$variables = array(
'rows' => commerce_payment_totals_rows($totals, $order),
'form' => $form,
'view' => $this->view,
'totals' => $totals,
'order' => $order,
);
return theme('commerce_payment_totals', $variables);
}
}