You are here

commerce_coupon_ui.module in Commerce Coupon 7

Coupon User Interface for Drupal Commerce

This file contains all the user interface functionality for the coupon system built on Drupal Commerce.

File

commerce_coupon_ui.module
View source
<?php

/**
 * @file
 * Coupon User Interface for Drupal Commerce
 *
 * This file contains all the user interface functionality for the coupon system
 * built on Drupal Commerce.
 */

/**
 * Implements hook_entity_info_alter().
 */
function commerce_coupon_ui_entity_info_alter(&$entity_info) {

  // Expose the admin UI for coupon fields.
  foreach ($entity_info['commerce_coupon']['bundles'] as $type => &$bundle) {
    $bundle['admin'] = array(
      'path' => 'admin/commerce/coupons/types/' . strtr($type, '_', '-'),
      'access arguments' => array(
        'administer coupon types',
      ),
    );
  }
}

/**
 * Implements hook_menu().
 */
function commerce_coupon_ui_menu() {
  $items['admin/commerce/coupons/add'] = array(
    'title' => 'Create Coupon',
    'description' => 'Create a new coupon',
    'page callback' => 'commerce_coupon_ui_add_page',
    'weight' => 10,
    'access callback' => 'commerce_coupon_ui_coupon_add_any_access',
  );
  foreach (commerce_coupon_get_types() as $type => $coupon_type) {
    if (!empty($coupon_type->status)) {
      $items['admin/commerce/coupons/add/' . strtr($type, array(
        '_' => '-',
      ))] = array(
        'title' => 'Create !name',
        'title arguments' => array(
          '!name' => $coupon_type->label,
        ),
        'page callback' => 'commerce_coupon_ui_coupon_form_wrapper',
        'page arguments' => array(
          commerce_coupon_create($type),
          'add',
        ),
        'access callback' => 'commerce_coupon_access',
        'access arguments' => array(
          'create',
          commerce_coupon_create($type),
        ),
      );
    }
  }

  // Edit & Delete coupon forms.
  $items['admin/commerce/coupons/%commerce_coupon'] = array(
    'title' => 'Edit',
    'page callback' => 'commerce_coupon_ui_coupon_form_wrapper',
    'page arguments' => array(
      3,
      'edit',
    ),
    'access callback' => 'commerce_coupon_access',
    'access arguments' => array(
      'update',
      3,
    ),
    'weight' => 0,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  );
  $items['admin/commerce/coupons/%commerce_coupon/edit'] = array(
    'title' => 'Edit',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
  );
  $items['admin/commerce/coupons/%commerce_coupon/delete'] = array(
    'title' => 'Delete',
    'page callback' => 'commerce_coupon_ui_coupon_delete_form_wrapper',
    'page arguments' => array(
      3,
    ),
    'access callback' => 'commerce_coupon_access',
    'access arguments' => array(
      'update',
      3,
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 20,
    'context' => MENU_CONTEXT_INLINE,
  );

  // Coupon types.
  $items['admin/commerce/coupons/types'] = array(
    'title' => 'Coupon types',
    'description' => 'Manage coupon types for your store.',
    'page callback' => 'commerce_coupon_ui_types_overview',
    'access arguments' => array(
      'administer coupon types',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 0,
  );
  $items['admin/commerce/coupons/types/add'] = array(
    'title' => 'Add coupon type',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_coupon_ui_type_form',
      commerce_coupon_type_create(),
    ),
    'access arguments' => array(
      'administer coupon types',
    ),
    'type' => MENU_LOCAL_ACTION,
    'file' => 'includes/commerce_coupon_ui.forms.inc',
  );
  foreach (commerce_coupon_get_types() as $type => $coupon_type) {
    if (!empty($coupon_type->status)) {

      // Convert underscores to hyphens for the menu item argument.
      $type_arg = strtr($type, '_', '-');
      $items['admin/commerce/coupons/types/' . $type_arg] = array(
        'title' => $coupon_type->label,
        'page callback' => 'drupal_get_form',
        'page arguments' => array(
          'commerce_coupon_ui_type_form',
          $coupon_type,
        ),
        'access arguments' => array(
          'administer coupon types',
        ),
        'file' => 'includes/commerce_coupon_ui.forms.inc',
      );
      $items['admin/commerce/coupons/types/' . $type_arg . '/edit'] = array(
        'title' => 'Edit',
        'access arguments' => array(
          'administer coupon types',
        ),
        'type' => MENU_DEFAULT_LOCAL_TASK,
        'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
      );
      $items['admin/commerce/coupons/types/' . $type_arg . '/delete'] = array(
        'title' => 'Delete',
        'page callback' => 'commerce_coupon_ui_coupon_type_delete_form_wrapper',
        'page arguments' => array(
          $coupon_type,
        ),
        'access arguments' => array(
          'administer coupon types',
        ),
        'type' => MENU_LOCAL_TASK,
        'context' => MENU_CONTEXT_INLINE,
        'weight' => 10,
      );
    }
  }

  // Add a general settings form.
  $items['admin/commerce/coupons/settings'] = array(
    'title' => 'Settings',
    'description' => 'Global configuration for coupons.',
    'file' => 'includes/commerce_coupon_ui.settings.inc',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_coupon_ui_settings_form',
    ),
    'access arguments' => array(
      'administer coupon settings',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 50,
  );

  // Remove coupon from order.
  $items['commerce/coupons/order/remove/%commerce_coupon/%commerce_order'] = array(
    'title' => 'Delete from order',
    'page callback' => 'commerce_coupon_ui_coupon_remove_coupon_from_order',
    'page arguments' => array(
      4,
      5,
    ),
    'access arguments' => array(
      'access checkout',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Access callback: determines if the user can create any type of coupon.
 */
function commerce_coupon_ui_coupon_add_any_access() {

  // Grant automatic access to users with administer coupons permission.
  if (user_access('administer commerce_coupon entities')) {
    return TRUE;
  }

  // Check the user's access on a coupon type basis.
  foreach (commerce_coupon_get_types() as $type => $coupon_type) {
    if (commerce_coupon_access('create', commerce_coupon_create($type))) {
      return TRUE;
    }
  }
  return FALSE;
}

/**
 * Implements hook_menu_alter().
 */
function commerce_coupon_ui_menu_alter(&$items) {

  // Transform the field UI tabs into contextual links.
  foreach (commerce_coupon_get_types() as $type => $coupon_type) {

    // Convert underscores to hyphens for the menu item argument.
    $type_arg = strtr($type, '_', '-');
    if (!empty($coupon_type->status)) {
      $items['admin/commerce/coupons/types/' . $type_arg . '/fields']['context'] = MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE;
      $items['admin/commerce/coupons/types/' . $type_arg . '/display']['context'] = MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE;
    }
    else {

      // Hide the menu if the coupon type is not enabled.
      $items['admin/commerce/coupons/types/' . $type_arg . '/fields']['access callback'] = FALSE;
      $items['admin/commerce/coupons/types/' . $type_arg . '/display']['access callback'] = FALSE;
    }
  }
}

/**
 * Implements hook_menu_local_tasks_alter().
 */
function commerce_coupon_ui_menu_local_tasks_alter(&$data, $router_item, $root_path) {

  // Add action link 'admin/commerce/coupons/add' on 'admin/commerce/coupons'.
  if ($root_path == 'admin/commerce/coupons') {
    $item = menu_get_item('admin/commerce/coupons/add');
    if ($item['access']) {
      $data['actions']['output'][] = array(
        '#theme' => 'menu_local_action',
        '#link' => $item,
      );
    }
  }
}

/**
 * Implements hook_views_api().
 */
function commerce_coupon_ui_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'commerce_coupon_ui') . '/includes/views',
  );
}
function commerce_coupon_ui_create_page() {
  drupal_goto('admin/commerce/coupons/add');
}
function commerce_coupon_ui_add_page($type = NULL) {
  $item = menu_get_item();
  $content = system_admin_menu_block($item);

  // Bypass the admin/commerce/coupons/add listing if only one product type is
  // available.
  if (count($content) == 1) {
    $item = array_shift($content);
    drupal_goto($item['href']);
  }
  return theme('commerce_coupon_ui_add_list', array(
    'content' => $content,
  ));
}

/**
 * Implements hook_theme().
 */
function commerce_coupon_ui_theme() {
  return array(
    'commerce_coupon_ui_add_list' => array(
      'variables' => array(
        'content' => NULL,
      ),
    ),
    'coupon_type_admin_overview' => array(
      'variables' => array(
        'type' => NULL,
      ),
    ),
  );
}
function theme_commerce_coupon_ui_add_list($variables) {
  $content = $variables['content'];
  $output = '';
  if ($content) {
    $output = '<dl class="commerce-coupon-type-list">';
    foreach ($content as $item) {
      $output .= '<dt>' . l($item['title'], $item['href'], $item['localized_options']) . '</dt>';
      $output .= '<dd>' . filter_xss_admin($item['description']) . '</dd>';
    }
    $output .= '</dl>';
  }
  else {
    if (user_access('administer coupon types')) {
      $output = '<p>' . t('You have not created any coupon types yet. Go to the <a href="@create-coupon-type">coupon type creation page</a> to add a new coupon type.', array(
        '@create-coupon-type' => url('admin/commerce/coupons/types/add'),
      )) . '</p>';
    }
    else {
      $output = '<p>' . t('No coupon type have been created yet for you to use.') . '</p>';
    }
  }
  return $output;
}

/**
 * Form callback wrapper: create or edit a coupon.
 *
 * @param $coupon
 *   The coupon object being edited by this form.
 */
function commerce_coupon_ui_coupon_form_wrapper($coupon, $op = NULL) {

  // Add the breadcrumb for the form's location.
  commerce_coupon_ui_set_breadcrumb();

  // Include the forms file from the Coupon module.
  module_load_include('inc', 'commerce_coupon', 'includes/commerce_coupon_ui.forms');
  return drupal_get_form('commerce_coupon_form', $coupon, $op);
}

/**
 * Form callback wrapper: confirmation form for deleting a coupon.
 *
 * @param $coupon
 *   The coupon object being deleted by this form.
 */
function commerce_coupon_ui_coupon_delete_form_wrapper($coupon) {

  // Add the breadcrumb for the form's location.
  commerce_coupon_ui_set_breadcrumb();

  // Include the forms file from the Coupon module.
  module_load_include('inc', 'commerce_coupon', 'includes/commerce_coupon_ui.forms');
  return drupal_get_form('commerce_coupon_ui_delete_form', $coupon, 'delete');
}

/**
 * Callback function to remove a coupon from the order used in the checkout
 * pane view.
 *
 * @param $coupon
 *   Commerce coupon.
 * @param $order
 *   Commerce order.
 *
 * @see commerce_coupon_handler_field_coupon_order_remove
 */
function commerce_coupon_ui_coupon_remove_coupon_from_order($coupon, $order) {
  if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], 'commerce_coupon_remove_checkout:' . $coupon->coupon_id . ':' . $order->order_id) || !commerce_checkout_access($order)) {
    return MENU_ACCESS_DENIED;
  }
  commerce_coupon_remove_coupon_from_order($order, $coupon);
  drupal_set_message(t('Coupon removed from order'));
  drupal_goto();
}

/**
 * Sets the breadcrumb for administrative coupon pages.
 *
 * @param $coupon_types
 *   TRUE or FALSE indicating whether or not the breadcrumb should include the
 *     coupon types administrative page.
 */
function commerce_coupon_ui_set_breadcrumb($coupon_types = FALSE) {
  $breadcrumb = array(
    l(t('Home'), '<front>'),
    l(t('Administration'), 'admin'),
    l(t('Store'), 'admin/commerce'),
    l(t('Coupons'), 'admin/commerce/coupons'),
  );
  if ($coupon_types) {
    $breadcrumb[] = l(t('Coupon types'), 'admin/commerce/coupons/types');
  }
  drupal_set_breadcrumb($breadcrumb);
}

/**
 * Menu callback: display an overview of available types.
 */
function commerce_coupon_ui_types_overview() {
  $header = array(
    t('Name'),
    t('Operations'),
  );
  $rows = array();

  // Loop through all defined product types.
  foreach (commerce_coupon_get_types() as $type => $coupon_type) {
    if (!empty($coupon_type->status)) {

      // Build the operation links for the current product type.
      $links = menu_contextual_links('commerce-coupon-type', 'admin/commerce/coupons/types', array(
        strtr($type, array(
          '_' => '-',
        )),
      ));

      // Add the product type's row to the table's rows array.
      $rows[] = array(
        theme('coupon_type_admin_overview', array(
          'coupon_type' => $coupon_type,
        )),
        theme('links', array(
          'links' => $links,
          'attributes' => array(
            'class' => 'links inline operations',
          ),
        )),
      );
    }
  }

  // If no product types are defined...
  if (empty($rows)) {

    // Add a standard empty row with a link to add a new coupon type.
    $rows[] = array(
      array(
        'data' => t('There are no coupon types yet. <a href="@link">Add coupon type</a>.', array(
          '@link' => url('admin/commerce/coupons/types/add'),
        )),
        'colspan' => 2,
      ),
    );
  }
  return theme('table', array(
    'header' => $header,
    'rows' => $rows,
  ));
}

/**
 * Builds an overview of a product type for display to an administrator.
 *
 * @param $variables
 *   An array of variables used to generate the display; by default includes the
 *     type key with a value of the product type array.
 *
 * @ingroup themeable
 */
function theme_coupon_type_admin_overview($variables) {
  $coupon_type = $variables['coupon_type'];
  $output = check_plain($coupon_type->label);
  $output .= ' <small>' . t('(Machine name: @type)', array(
    '@type' => $coupon_type->type,
  )) . '</small>';
  return $output;
}

/**
 * Form callback wrapper: confirmation form for deleting a coupon type.
 *
 * @param $coupon
 *   The coupon type object.
 */
function commerce_coupon_ui_coupon_type_delete_form_wrapper(CommerceCouponType $coupon_type) {

  // Add the breadcrumb for the form's location.
  commerce_coupon_ui_set_breadcrumb(TRUE);

  // Don't allow deletion of coupon types that have coupons already.
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'commerce_coupon', '=')
    ->entityCondition('bundle', $coupon_type->type, '=')
    ->count();
  $count = $query
    ->execute();
  if ($count > 0) {
    drupal_set_title(t('Cannot delete the %label coupon type', array(
      '%name' => $coupon_type->label,
    )), PASS_THROUGH);
    return format_plural($count, 'There is 1 coupon of this type. It cannot be deleted.', 'There are @count coupons of this type. It cannot be deleted.');
  }

  // Include the forms file from the Coupon module.
  module_load_include('inc', 'commerce_coupon_ui', 'includes/commerce_coupon_ui.forms');
  return drupal_get_form('commerce_coupon_ui_coupon_type_delete_form', $coupon_type);
}

/**
 * Checks to see if a given coupon type already exists.
 *
 * @param $type
 *   The string to match against existing types.
 *
 * @return
 *   TRUE or FALSE indicating whether or not the coupon type exists.
 */
function commerce_coupon_ui_validate_coupon_type_unique($type) {

  // Look for a match of the type.
  if ($match_id = db_query('SELECT type FROM {commerce_coupon_type} WHERE type = :type', array(
    ':type' => $type,
  ))
    ->fetchField()) {
    return FALSE;
  }
  return TRUE;
}

Functions

Namesort descending Description
commerce_coupon_ui_add_page
commerce_coupon_ui_coupon_add_any_access Access callback: determines if the user can create any type of coupon.
commerce_coupon_ui_coupon_delete_form_wrapper Form callback wrapper: confirmation form for deleting a coupon.
commerce_coupon_ui_coupon_form_wrapper Form callback wrapper: create or edit a coupon.
commerce_coupon_ui_coupon_remove_coupon_from_order Callback function to remove a coupon from the order used in the checkout pane view.
commerce_coupon_ui_coupon_type_delete_form_wrapper Form callback wrapper: confirmation form for deleting a coupon type.
commerce_coupon_ui_create_page
commerce_coupon_ui_entity_info_alter Implements hook_entity_info_alter().
commerce_coupon_ui_menu Implements hook_menu().
commerce_coupon_ui_menu_alter Implements hook_menu_alter().
commerce_coupon_ui_menu_local_tasks_alter Implements hook_menu_local_tasks_alter().
commerce_coupon_ui_set_breadcrumb Sets the breadcrumb for administrative coupon pages.
commerce_coupon_ui_theme Implements hook_theme().
commerce_coupon_ui_types_overview Menu callback: display an overview of available types.
commerce_coupon_ui_validate_coupon_type_unique Checks to see if a given coupon type already exists.
commerce_coupon_ui_views_api Implements hook_views_api().
theme_commerce_coupon_ui_add_list
theme_coupon_type_admin_overview Builds an overview of a product type for display to an administrator.