You are here

function paymentreference_form_process_paymentreference in Payment 7

Implements form process callback for paymentreference elements.

1 string reference to 'paymentreference_form_process_paymentreference'
paymentreference_element_info in modules/paymentreference/paymentreference.module
Implements hook_element_info().

File

modules/paymentreference/paymentreference.module, line 528
Hook implementations and general functions.

Code

function paymentreference_form_process_paymentreference(array $element, array &$form_state, array $form) {
  $pid = $element['#default_value'];
  if (!$pid) {
    $pid = (int) call_user_func_array($element['#payment_load_callback'], $element['#payment_load_arguments']);
  }

  // AJAX.
  $ajax_wrapper_id = drupal_html_id('paymentreference-' . $element['#name']);
  $element['#prefix'] = '<div id="' . $ajax_wrapper_id . '">';
  $element['#suffix'] = '</div>';
  $element['#attached']['js'] = array(
    drupal_get_path('module', 'paymentreference') . '/js/paymentreference.js',
    array(
      'type' => 'setting',
      'data' => array(
        'PaymentreferencePaymentAvailable' => array(
          $ajax_wrapper_id => !empty($pid),
        ),
      ),
    ),
  );

  // Payment information.
  $header = array(
    t('Amount'),
    t('Status'),
    t('Last updated'),
  );
  if (!$pid) {
    $amount = 0;
    foreach ($element['#payment_line_items'] as $line_item) {
      $amount += $line_item
        ->totalAmount(TRUE);
    }
    $row = array(
      payment_amount_human_readable($amount, $element['#payment_currency_code']),
      array(
        'data' => t('<a href="@url" target="_blank">Add a new payment</a> (opens in a new window)', array(
          '@url' => url($element['#payment_add_page_path']),
        )),
        'colspan' => 2,
      ),
    );
  }
  else {
    $payment = entity_load_single('payment', $pid);
    $row = array(
      payment_amount_human_readable($payment
        ->totalAmount(TRUE), $payment->currency_code),
      payment_status_info($payment
        ->getStatus()->status, TRUE)->title,
      format_date($payment
        ->getStatus()->created),
    );
    if (payment_access('view', $payment)) {
      $header[] = t('Operations');
      $row[] = t('<a href="@url" target="_blank">View payment details</a> (opens in a new window)', array(
        '@url' => url('payment/' . $payment->pid),
      ));
    }
  }
  $element['payment'] = array(
    '#type' => 'item',
    '#title' => isset($element['#title']) ? $element['#title'] : '',
    '#description' => isset($element['#description']) ? $element['#description'] : '',
    '#required' => $element['#required'],
    '#markup' => theme('table', array(
      'header' => $header,
      'rows' => array(
        $row,
      ),
    )),
  );

  // Refresh button.
  $element['refresh'] = array(
    '#type' => 'submit',
    '#value' => t('Re-check available payments'),
    '#submit' => isset($element['#submit']) ? $element['#submit'] : array(),
    '#limit_validation_errors' => array(),
    '#ajax' => array(
      'callback' => 'paymentreference_form_process_paymentreference_ajax_callback',
      'effect' => 'fade',
      'event' => 'mousedown',
      'wrapper' => $ajax_wrapper_id,
      'progress' => array(),
    ),
    '#attributes' => array(
      'class' => array(
        'paymentreference-refresh-button',
        'js-hide',
      ),
    ),
    '#name' => $element['#name'] . '_refresh',
  );
  $form_state[$element['refresh']['#name']] = $element['#parents'];
  return $element;
}