You are here

function commerce_coupon_handler_area_cart_form::views_form in Commerce Coupon 7.2

Same name and namespace in other branches
  1. 7 includes/views/handlers/commerce_coupon_handler_area_cart_form.inc \commerce_coupon_handler_area_cart_form::views_form()

File

includes/views/handlers/commerce_coupon_handler_area_cart_form.inc, line 79
Display a commerce coupon form field on the cart form.

Class

commerce_coupon_handler_area_cart_form
@file Display a commerce coupon form field on the cart form.

Code

function views_form(&$form, &$form_state) {

  // Ensure this include file is loaded when the form is rebuilt from the cache.
  $form_state['build_info']['files']['coupon_cart_form'] = drupal_get_path('module', 'commerce_coupon') . '/includes/views/handlers/commerce_coupon_handler_area_cart_form.inc';
  $form[$this->options['id']] = array(
    '#prefix' => '<div id="commerce-coupon-cart-form-wrapper">',
    '#suffix' => '</div>',
    '#weight' => $this->options['weight'],
  );
  $form[$this->options['id']]['coupon_code'] = array(
    '#type' => 'textfield',
    '#title' => t('Coupon code'),
    '#description' => t('Enter your coupon code here.'),
  );
  $form[$this->options['id']]['coupon_add'] = array(
    '#type' => 'submit',
    '#value' => t('Add coupon'),
    '#name' => 'coupon_add',
    // Limit validation to the coupon code.
    '#limit_validation_errors' => array(
      array(
        'coupon_code',
      ),
    ),
    '#validate' => array(
      'commerce_coupon_handler_area_cart_form_validate',
    ),
    '#submit' => array(
      'commerce_coupon_handler_area_cart_form_submit',
    ),
  );

  // Attach ajax if views ajax enabled.
  if ($this->view->use_ajax) {

    // @todo implement AJAX.
  }

  // First look for an order_id argument.
  foreach ($this->view->argument as $name => $argument) {
    if ($argument instanceof commerce_order_handler_argument_order_order_id) {

      // If it is single value...
      if (count($argument->value) == 1) {
        $order_id = reset($argument->value);
        break;
      }
    }
  }
  $order = !empty($order_id) ? commerce_order_load($order_id) : commerce_cart_order_load($GLOBALS['user']->uid);

  // Extract the View and display keys from the cart contents pane setting.
  $coupon_summary_view = $this->options['coupon_cart_form_view'];
  if ($coupon_summary_view != 'none') {
    list($view_id, $display_id) = explode('|', $coupon_summary_view);
    if (!empty($view_id) && !empty($display_id) && views_get_view($view_id)) {
      $form[$this->options['id']]['redeemed_coupons'] = array(
        '#type' => 'markup',
        '#markup' => commerce_embed_view($view_id, $display_id, array(
          $order->order_id,
        )),
      );
    }
  }
}