You are here

regcode.module in Registration codes 6

File

regcode.module
View source
<?php

/**
 * Make use of the regcode API
 */
require_once 'regcode.api.php';

/**
 * Implementation of hook_help().
 */
function regcode_help($path, $arg) {
  $output = '';

  //declare your output variable
  switch ($path) {
    case 'admin/help#regcode':
      $output = '<p>' . t('Provides user registration code functionality.') . '</p>';
      break;
    case 'admin/user/regcodes':
      $output = '<p>' . t('Change global settings for registration codes.') . '</p>';
      break;
    case 'admin/user/regcodes/add':
      $output = '<p>' . t('Create a registration code.') . '</p>';
      break;
    case 'admin/user/regcodes/list':
      $output = '<p>' . t('List and filter all registration codes.') . '</p>';
      $output .= '<p>' . t('Note: The UID indicates only the last user to use a registration code.') . '</p>';
      break;
    case 'admin/user/regcodes/create':
      $output = '<p>' . t('Create and generate registration codes.') . '</p>';
      break;
    case 'admin/user/regcodes/import':
      $output = '<p>' . t('Import registration codes from a CSV file.') . '</p>';
      break;
  }
  return $output;
}

/**
 * Implementation of hook_perm().
 */
function regcode_perm() {
  return array(
    'administer registration codes',
    'bypass registration code entry',
  );
}

/**
 * Implementation of hook_menu().
 */
function regcode_menu() {
  $items = array();
  $items['admin/user/regcodes'] = array(
    'title' => 'Registration codes',
    'description' => 'Settings for registration code functionality.',
    'file' => 'regcode.admin.php',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'regcode_admin_list',
    ),
    'access arguments' => array(
      'administer registration codes',
    ),
  );
  $items['admin/user/regcodes/list'] = array(
    'title' => 'List',
    'description' => 'List of registration codes',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 10,
  );
  $items['admin/user/regcodes/create'] = array(
    'title' => 'Create',
    'description' => 'Create a registration code',
    'type' => MENU_LOCAL_TASK,
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'regcode_admin_create',
    ),
    'access arguments' => array(
      'administer registration codes',
    ),
    'file' => 'regcode.admin.php',
    'weight' => 20,
  );
  $items['admin/user/regcodes/settings'] = array(
    'title' => 'Settings',
    'description' => 'Settings for registration code functionality.',
    'type' => MENU_LOCAL_TASK,
    'access arguments' => array(
      'administer registration codes',
    ),
    'page arguments' => array(
      'regcode_admin_settings',
    ),
    'weight' => 30,
  );
  $items['admin/user/regcodes/import'] = array(
    'title' => 'Import',
    'description' => 'Import registration codes',
    'file' => 'regcode.admin.php',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'regcode_admin_import',
    ),
    'access arguments' => array(
      'administer registration codes',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 40,
  );
  $items['admin/user/regcodes/export'] = array(
    'title' => 'Export',
    'description' => 'Export registration codes',
    'file' => 'regcode.admin.php',
    'page callback' => 'regcode_admin_export',
    'access arguments' => array(
      'administer registration codes',
    ),
    'type' => MENU_CALLBACK,
    'weight' => 50,
  );
  $items['admin/user/regcodes/delete'] = array(
    'file' => 'regcode.admin.php',
    'page callback' => 'regcode_admin_delete_confirm',
    'access argumenajax_base_page_themets' => array(
      'administer registration codes',
    ),
    'page arguments' => array(
      4,
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/user/regcodes/autocomplete'] = array(
    'page callback' => 'regcode_admin_ajax_categories',
    'file' => 'regcode.admin.php',
    'type' => MENU_CALLBACK,
    'access arguments' => array(
      'administer registration codes',
    ),
  );
  return $items;
}

/**
 * Implementation of hook_user().
 */
function regcode_user($op, &$edit, &$account, $category = NULL) {
  $code_optional = variable_get('regcode_optional', FALSE);
  switch ($op) {
    case 'register':
      $form['regcode'] = array(
        '#type' => 'fieldset',
        '#title' => variable_get('regcode_fieldset_title', t('Registration Code')),
      );
      $form['regcode']['regcode_code'] = array(
        '#type' => 'textfield',
        '#title' => variable_get('regcode_field_title', t('Registration Code')),
        '#description' => variable_get('regcode_field_description', t('Please enter your registration code.')),
        '#required' => !$code_optional,
      );
      if (user_access('bypass registration code entry')) {
        $form['regcode']['regcode_code']['#required'] = FALSE;
      }
      return $form;
      break;
    case 'validate':
      if ($category === 'account' && drupal_strlen(trim($edit['regcode_code']))) {
        $code = regcode_get_code($edit['regcode_code']);
        if (!is_array($code)) {
          form_set_error('regcode_code', regcode_message($code));
          watchdog('regcode', 'User entered invalid registration code (@code)', array(
            '@code' => $edit['regcode_code'],
          ), WATCHDOG_WARNING);
        }
      }
      break;
    case 'insert':

      // Use the code
      $regcode = regcode_get_code($edit['regcode_code'], $account->uid);
      watchdog('regcode', 'The registration code %code was used by !user', array(
        '%code' => $edit['regcode_code'],
        '!user' => l($account->name, 'user/' . $account->uid),
      ));

      // Trigger the event
      foreach (module_implements('regcode_used') as $module) {
        $hook = $module . '_regcode_used';
        $hook($edit, $account, $regcode);
      }

      // Notify rules module
      if (is_array($regcode) && module_exists('rules')) {
        rules_invoke_event('regcode_used', $account, $regcode);
      }

      // Nothing to save
      $edit['regcode_code'] = NULL;
      break;
  }
}

/**
 * Implements hook_hook_info().
 */
function regcode_hook_info() {
  $events = array();
  $events['regcode'] = array(
    'regcode' => array(
      'regcode_used' => array(
        'runs when' => t('A valid registration code was used'),
      ),
    ),
  );
  return $events;
}

Functions

Namesort descending Description
regcode_help Implementation of hook_help().
regcode_hook_info Implements hook_hook_info().
regcode_menu Implementation of hook_menu().
regcode_perm Implementation of hook_perm().
regcode_user Implementation of hook_user().