You are here

function commerce_payment_ui_admin_page in Commerce Core 7

Builds the payment settings page using the Rules UI overview table filtered to display payment method rules.

1 string reference to 'commerce_payment_ui_admin_page'
commerce_payment_ui_menu in modules/payment/commerce_payment_ui.module
Implements hook_menu().

File

modules/payment/includes/commerce_payment_ui.admin.inc, line 21
Administrative page callbacks for the Payment UI module.

Code

function commerce_payment_ui_admin_page() {
  RulesPluginUI::$basePath = 'admin/commerce/config/payment-methods';
  $options = array(
    'show plugin' => FALSE,
  );

  // Add the table for enabled payment method rules.
  $content['enabled']['title']['#markup'] = '<h3>' . t('Enabled payment method rules') . '</h3>';
  $conditions = array(
    'event' => 'commerce_payment_methods',
    'plugin' => 'reaction rule',
    'active' => TRUE,
  );
  $content['enabled']['rules'] = RulesPluginUI::overviewTable($conditions, $options);
  $content['enabled']['rules']['#empty'] = t('There are no active payment methods.');

  // Add the table for disabled payment method rules.
  $content['disabled']['title']['#markup'] = '<h3>' . t('Disabled payment method rules') . '</h3>';
  $conditions['active'] = FALSE;
  $content['disabled']['rules'] = RulesPluginUI::overviewTable($conditions, $options);
  $content['disabled']['rules']['#empty'] = t('There are no disabled payment methods.');

  // Update the descriptions of items in the tables to show whether or not
  // they're enabled for use on the checkout form and payment terminal form.
  foreach (array(
    'enabled',
    'disabled',
  ) as $status) {
    foreach ($content[$status]['rules']['#rows'] as &$row) {

      // We don't have access to the actual machine-name of the rule each row
      // represents, so we must extract the name from the machine-name markup.
      $rule_name_markup = $row[0]['data']['description']['settings']['machine_name']['#markup'];
      $rule_name = drupal_substr($rule_name_markup, drupal_strlen(t('Machine name') . ': '));

      // If we were able to extract a valid name from the markup...
      if ($rule = rules_config_load($rule_name)) {

        // Loop over the actions to find the first enabled payment method.
        foreach ($rule
          ->actions() as $action) {

          // Parse the action name into a payment method ID.
          if (method_exists($action, 'getElementName') && strpos($action
            ->getElementName(), 'commerce_payment_enable_') === 0) {
            $method_id = drupal_substr($action
              ->getElementName(), 24);

            // Load the payment method instance and determine availability.
            $payment_method = commerce_payment_method_load($method_id);

            // Create an items array that describes where the payment method
            // will be available for use.
            $items = array();
            if (empty($payment_method['checkout'])) {
              $items[] = t('Not available on the checkout form');
            }
            else {
              $items[] = t('Available on the checkout form');
            }
            if (empty($payment_method['terminal'])) {
              $items[] = t('Not available on the order payment terminal');
            }
            else {
              $items[] = t('Available on the order payment terminal');
            }
            $row[0]['data']['availability'] = array(
              '#theme' => 'item_list',
              '#items' => $items,
            );
            break;
          }
        }
      }
    }
  }

  // Store the function name in the content array to make it easy to alter the
  // contents of this page.
  $content['#page_callback'] = 'commerce_payment_ui_admin_page';
  return $content;
}