You are here

commerce_coupon_batch.module in Commerce coupon batch 7.2

Same filename and directory in other branches
  1. 7 commerce_coupon_batch.module

Batch generation functionalities for commerce_coupon module.

File

commerce_coupon_batch.module
View source
<?php

/**
 * @file
 * Batch generation functionalities for commerce_coupon module.
 */

/**
 * Implements hook_menu().
 */
function commerce_coupon_batch_menu() {
  $items['admin/commerce/coupons/batch'] = array(
    'title' => 'Batch create coupons',
    'page callback' => 'commerce_coupon_batch_overview_page',
    'access arguments' => array(
      'commerce coupon batch creation',
    ),
  );
  foreach (commerce_coupon_get_types() as $type => $info) {
    $type_arg = strtr($type, '_', '-');
    $coupon = commerce_coupon_create($type);
    $items['admin/commerce/coupons/batch/' . $type_arg] = array(
      'tab_parent' => 'admin/commerce/coupons/batch',
      'title' => 'Batch create @name',
      'title arguments' => array(
        '@name' => $type,
      ),
      'page callback' => 'drupal_get_form',
      'page arguments' => array(
        'commerce_coupon_batch_form',
        $coupon,
      ),
      'access callback' => 'commerce_coupon_access',
      'access arguments' => array(
        'create',
        $coupon,
      ),
      'file' => 'commerce_coupon_batch.form.inc',
    );
  }
  return $items;
}

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

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

/**
 * Implements hook_views_api().
 */
function commerce_coupon_batch_views_api($module = NULL, $api = NULL) {
  return array(
    'api' => '3.0',
  );
}

/**
 * Implements hook_permission().
 */
function commerce_coupon_batch_permission() {
  $permissions['commerce coupon batch creation'] = array(
    'title' => t('Commerce Coupon Batch Creation'),
    'description' => t('Allows users to create coupons by a batch.'),
  );
  return $permissions;
}

/**
 * Display the coupon type overview page for batch creation if there's more than
 * one coupon type.
 */
function commerce_coupon_batch_overview_page() {
  $item = menu_get_item();
  $content = system_admin_menu_block($item);

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

/**
 * Generates a new unique coupon code.
 *
 * @param string $type
 *   Coupon type.
 * @param int $length
 *   Optional The length of the new code.
 *
 * @return string
 *   The new coupon code.
 */
function commerce_coupon_batch_generate_coupon_code($type, $length = NULL) {

  // We define the possible characters. No 'l','1', 'i' to prevent
  // reconisation problems.
  $characters = array(
    'A',
    'B',
    'C',
    'D',
    'E',
    'F',
    'G',
    'H',
    'J',
    'K',
    'L',
    'M',
    'N',
    'P',
    'Q',
    'R',
    'S',
    'T',
    'U',
    'V',
    'W',
    'X',
    'Y',
    'Z',
    'a',
    'b',
    'c',
    'd',
    'e',
    'f',
    'g',
    'h',
    'j',
    'k',
    'm',
    'n',
    'o',
    'p',
    'q',
    'r',
    's',
    't',
    'u',
    'v',
    'w',
    'x',
    'y',
    'z',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
  );
  $number_of_characters = count($characters);
  $code_found = FALSE;
  if ($length == NULL) {
    $length = variable_get('commerce_coupon_' . $type . '_default_code_size', 8);
  }

  // We need to check if the produced coupon code is already in the
  // database. We try this for 1000 iteration. If we then not found a
  // a code, we stop. There must be an error in this case.
  for ($i = 0; $i < 1000 && $code_found == FALSE; $i++) {
    $code = '';

    // Create the code per character.
    for ($c = 0; $c < $length; $c++) {
      $rand_index = mt_rand(0, $number_of_characters - 1);
      $code .= $characters[$rand_index];
    }

    // Check in the database if the generated code is already defined.
    if (commerce_coupon_code_exists($code) == FALSE) {
      $code_found = TRUE;
    }
  }
  return $code;
}

Functions

Namesort descending Description
commerce_coupon_batch_generate_coupon_code Generates a new unique coupon code.
commerce_coupon_batch_menu Implements hook_menu().
commerce_coupon_batch_menu_local_tasks_alter Implements hook_menu_local_tasks_alter().
commerce_coupon_batch_overview_page Display the coupon type overview page for batch creation if there's more than one coupon type.
commerce_coupon_batch_permission Implements hook_permission().
commerce_coupon_batch_views_api Implements hook_views_api().