View source
<?php
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;
}
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;
}
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');
}
function regcode_og_delete_rule($rid) {
db_query('DELETE FROM {regcode_og} WHERE id = %d', $rid);
}
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;
}
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();
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);
}
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'));
}
function regcode_og_get_rules($category = '') {
$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);
}
$rules = array();
while ($row = db_fetch_array($res)) {
$rules[] = $row;
}
return $rules;
}
function regcode_og_create_rule($category, $ogs) {
foreach ($ogs as $og) {
db_query("INSERT INTO {regcode_og} (category, og) VALUES ('%s', %d)", $category, $og);
}
}
function regcode_og_regcode_used(&$edit, &$account, $regcode) {
if (!is_array($regcode)) {
return;
}
$rules = regcode_og_get_rules($regcode['category']);
if (count($rules)) {
foreach ($rules as $rule) {
og_save_subscription($rule['ogid'], $account->uid, array(
'is_active' => 1,
));
}
}
}