You are here

regcode_og.module in Registration codes 6

File

regcode_og/regcode_og.module
View source
<?php

/**
 * Implementation of hook_help()
 */
function regcode_og_help($path, $arg) {
  $output = '';
  switch ($path) {
    case 'admin/user/regcodes/ogs':
      $output = '<p>' . t('Automate organic group assignment based on regcode. Create a list of rules which are applied when users register.') . '</p>';
      break;
  }
  return $output;
}

/**
 * Implementation of hook_menu()
 */
function regcode_og_menu() {
  $items = array();
  $items['admin/user/regcodes/ogs'] = array(
    'title' => t('Organic Groups'),
    'description' => t('Automate organic group assignment'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'regcode_og_admin',
    ),
    'type' => MENU_LOCAL_TASK,
    'access arguments' => array(
      'administer registration codes',
    ),
    'weight' => 101,
  );
  $items['admin/user/regcodes/ogs/delete/%'] = array(
    'page callback' => 'regcode_og_delete_rule_confirm',
    'page arguments' => array(
      5,
    ),
    'type' => MENU_CALLBACK,
    'access arguments' => array(
      'administer registration codes',
    ),
  );
  return $items;
}

/**
 * Page callback for role-rule deletion.
 */
function regcode_og_delete_rule_confirm($rid) {
  if (empty($_GET['token']) || !drupal_valid_token($_GET['token'], $rid)) {
    return MENU_ACCESS_DENIED;
  }
  regcode_og_delete_rule($rid);
  drupal_set_message(t('Rule was deleted (Rule #@rule)', array(
    '@rule' => $rid,
  )));
  drupal_goto('admin/user/regcodes/ogs');
}

/**
 * Delete a rule
 */
function regcode_og_delete_rule($rid) {
  db_query('DELETE FROM {regcode_og} WHERE id = %d', $rid);
}

/**
 * Admin page for role assignment
 */
function regcode_og_admin() {
  $form = array();
  $form['regcode_og']['new'] = array(
    '#type' => 'fieldset',
    '#title' => t('Create a new rule'),
  );
  $form['regcode_og']['new']['role'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Assign organic group(s)'),
    '#options' => og_all_groups_options(),
  );
  $form['regcode_og']['new']['category'] = array(
    '#type' => 'select',
    '#title' => t('When a user uses a regcode in category'),
    '#options' => regcode_get_categories(),
  );
  $form['regcode_og']['new']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Create rule'),
  );
  $form['regcode_og']['list'] = array(
    '#type' => 'markup',
    '#value' => regcode_og_get_list_markup(),
  );
  return $form;
}

/** 
 * Return the formatted HTML list
 */
function regcode_og_get_list_markup() {
  $headings = array(
    t('Id'),
    t('Category'),
    t('Group ID'),
    t('Group Name'),
    t('Actions'),
  );
  $rows = regcode_og_get_rules();
  $group_name_mapping = og_all_groups_options();

  // Add actions
  foreach ($rows as &$row) {
    $row['group_name'] = $group_name_mapping[$row['ogid']];
    $row = array_map('check_plain', $row);
    $row['action'] = l(t('Remove'), 'admin/user/regcodes/ogs/delete/' . $row['id'], array(
      'query' => array(
        'token' => $row['id'],
      ),
    ));
  }
  return theme('table', $headings, $rows);
}

/**
 * Create the roles
 */
function regcode_og_admin_submit($form, $form_state) {
  $roles = array_filter($form_state['values']['role']);
  $category = $form_state['values']['category'];
  regcode_og_create_rule($category, $roles);
  drupal_set_message(t('New registration code rule created'));
}

/**
 * List all of the regcode rules
 */
function regcode_og_get_rules($category = '') {

  // Query
  $query = 'SELECT id, category, regcode_og.og AS ogid
            FROM {regcode_og} AS regcode_og';
  if (empty($category)) {
    $res = db_query($query);
  }
  else {
    $res = db_query($query . " WHERE category='%s'", $category);
  }

  // Build array
  $rules = array();
  while ($row = db_fetch_array($res)) {
    $rules[] = $row;
  }
  return $rules;
}

/**
 * Create a new regcode rule
 */
function regcode_og_create_rule($category, $ogs) {
  foreach ($ogs as $og) {
    db_query("INSERT INTO {regcode_og} (category, og) VALUES ('%s', %d)", $category, $og);
  }
}

/**
 * Implements hook_regcode_used()
 *
 * Add the new role to the user
 */
function regcode_og_regcode_used(&$edit, &$account, $regcode) {

  // Do nothing if the regcode is not valid
  if (!is_array($regcode)) {
    return;
  }

  // Grab applicable roles for category used
  $rules = regcode_og_get_rules($regcode['category']);

  // Apply the rules
  if (count($rules)) {
    foreach ($rules as $rule) {
      og_save_subscription($rule['ogid'], $account->uid, array(
        'is_active' => 1,
      ));
    }
  }
}

Functions

Namesort descending Description
regcode_og_admin Admin page for role assignment
regcode_og_admin_submit Create the roles
regcode_og_create_rule Create a new regcode rule
regcode_og_delete_rule Delete a rule
regcode_og_delete_rule_confirm Page callback for role-rule deletion.
regcode_og_get_list_markup Return the formatted HTML list
regcode_og_get_rules List all of the regcode rules
regcode_og_help Implementation of hook_help()
regcode_og_menu Implementation of hook_menu()
regcode_og_regcode_used Implements hook_regcode_used()