regcode_og.module in Registration codes 7
Same filename and directory in other branches
Install, uninstall and scheme functions for the regcode_og module.
File
regcode_og/regcode_og.moduleView source
<?php
/**
* @file
* Install, uninstall and scheme functions for the regcode_og module.
*/
/**
* Implements hook_help().
*/
function regcode_og_help($path, $arg) {
$output = '';
switch ($path) {
case 'admin/user/regcode/og':
$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;
}
/**
* Implements hook_menu().
*/
function regcode_og_menu() {
$items = array();
$items['admin/config/people/regcode/og'] = array(
'title' => 'Organic Groups',
'description' => 'Automate organic group assignment',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'regcode_og_rules_page',
),
'type' => MENU_LOCAL_TASK,
'access arguments' => array(
'administer registration codes',
),
'weight' => 101,
);
$items['admin/config/people/regcode/og/rule/%'] = array(
'title' => 'Edit rule',
'description' => 'Modify settings for this og assignement rule',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'regcode_og_settings',
6,
),
'access arguments' => array(
'administer registration codes',
),
'type' => MENU_CALLBACK,
);
$items['admin/config/people/regcode/og/add-rule'] = array(
'title' => 'Add rule',
'description' => 'Create a new og assignement rule',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'regcode_og_create',
),
'access arguments' => array(
'administer registration codes',
),
'type' => MENU_LOCAL_ACTION,
);
$items['admin/config/people/regcode/og/rule/%/delete'] = array(
'title' => 'Delete rule',
'description' => 'Delete settings for this og assignement rule',
'page callback' => 'regcode_og_settings_delete_confirm',
'page arguments' => array(
6,
),
'access arguments' => array(
'administer registration codes',
),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Page callback for role-rule deletion; validates the security token as well.
*/
function regcode_og_settings_delete_confirm($machine_name) {
if (empty($_GET['token']) || !drupal_valid_token($_GET['token'], $machine_name)) {
return MENU_ACCESS_DENIED;
}
regcode_og_settings_delete($machine_name);
drupal_set_message(t('Rule deleted'));
drupal_goto('admin/config/people/regcode/og');
}
/**
* Callback: Delete a rule.
*/
function regcode_og_settings_delete($machine_name) {
$settings = variable_get('regcode_og_settings', array());
unset($settings[$machine_name]);
variable_set('regcode_og_settings', $settings);
}
/**
* Form: Settings form for an og assignment rule.
*/
function regcode_og_settings($form, &$form_state, $machine_name) {
$settings = variable_get('regcode_og_settings', array());
if (!isset($settings[$machine_name])) {
drupal_not_found();
}
$data = $settings[$machine_name];
// Retrieve labels for all groups.
$options = regcode_og_label(og_get_all_group());
// Store rule in form state.
$form_state['regcode_og_rule'] = $data;
$form['regcode_og_rule']['name'] = array(
'#type' => 'textfield',
'#title' => t('Rule name'),
'#required' => TRUE,
'#default_value' => $data['name'],
);
$form['regcode_og_rule']['ogs'] = array(
'#type' => 'checkboxes',
'#title' => t('Groups'),
'#description' => t('Assign user to this og group(s)'),
'#options' => $options,
'#default_value' => (array) $data['ogs'],
);
$form['regcode_og_rule']['tid'] = array(
'#type' => 'select',
'#title' => t('Tag'),
'#description' => t('When a user uses a regcode tagged with this term'),
'#options' => regcode_get_vocab_terms(),
'#default_value' => $data['tid'],
);
$form['regcode_og_save'] = array(
'#type' => 'submit',
'#value' => t('Save rule'),
);
$form['regcode_og_delete'] = array(
'#type' => 'submit',
'#value' => t('Delete rule'),
);
return $form;
}
/**
* Form submit: Save settings form.
*/
function regcode_og_settings_submit($form, $form_state) {
$machine_name = $form_state['regcode_og_rule']['machine_name'];
$settings = variable_get('regcode_og_settings', array());
$settings[$machine_name] = array(
'name' => $form_state['values']['name'],
'machine_name' => $machine_name,
'ogs' => array_filter($form_state['values']['ogs']),
'tid' => $form_state['values']['tid'],
);
variable_set('regcode_og_settings', $settings);
drupal_set_message(t('Your settings have been saved.'));
drupal_goto('admin/config/people/regcode/og');
}
/**
* Form: Create form for og assignment rules.
*/
function regcode_og_create($form, &$form_state) {
// Retrieve labels for all groups.
$options = regcode_og_label(og_get_all_group());
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Rule name'),
'#maxlength' => 255,
'#description' => t('Name this rule for administration purposes.'),
'#required' => TRUE,
);
$form['machine_name'] = array(
'#type' => 'machine_name',
'#title' => t('Machine name'),
'#maxlength' => 21,
'#description' => t('A unique name to construct the URL for this rule. It must only contain lowercase letters, numbers and hyphens.'),
'#machine_name' => array(
'exists' => 'regcode_og_name_exists',
),
);
$form['ogs'] = array(
'#type' => 'checkboxes',
'#title' => t('Groups'),
'#options' => $options,
'#description' => t('Assign user to this og group(s)'),
'#required' => TRUE,
);
$form['tid'] = array(
'#type' => 'select',
'#title' => t('Tag'),
'#description' => t('When a user uses a regcode tagged with this term'),
'#options' => regcode_get_vocab_terms(),
);
$form['save'] = array(
'#type' => 'submit',
'#value' => t('Create rule'),
);
return $form;
}
/**
* Returns true if a rule with the given machine_name exists.
*/
function regcode_og_name_exists($value) {
$settings = variable_get('regcode_og_settings', array());
return isset($settings[$value]);
}
/**
* Form submit: Create a new rule set.
*/
function regcode_og_create_submit($form, $form_state) {
$machine_name = $form_state['values']['machine_name'];
$settings = variable_get('regcode_og_settings', array());
$settings[$machine_name] = array(
'name' => $form_state['values']['name'],
'machine_name' => $form_state['values']['machine_name'],
'ogs' => array_filter($form_state['values']['ogs']),
'tid' => $form_state['values']['tid'],
);
variable_set('regcode_og_settings', $settings);
drupal_set_message(t('New og rule created'));
drupal_goto('admin/config/people/regcode/og');
}
/**
* List all of the rules as a HTML table.
*/
function regcode_og_rules_page() {
$rules = variable_get('regcode_og_settings', array());
$rows = array();
foreach ($rules as $row) {
$actions = array();
$actions[] = l(t('Edit'), sprintf('admin/config/people/regcode/og/rule/%s', $row['machine_name']));
$actions[] = l(t('Delete'), sprintf('admin/config/people/regcode/og/rule/%s/delete', $row['machine_name']), array(
'query' => array(
'token' => drupal_get_token($row['machine_name']),
),
));
$ognames = regcode_og_label($row['ogs']);
$term = taxonomy_term_load($row['tid']);
$rows[] = array(
check_plain($row['name']),
check_plain(implode(", ", $ognames)),
$term ? check_plain($term->name) : '-- ' . t('None') . ' --',
implode(' | ', $actions),
);
}
$header = array(
t('Name'),
t('Groups'),
t('Tag'),
t('Operations'),
);
$table = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('No rules defined. <a href="@link">Add rule</a>.', array(
'@link' => url('admin/config/people/regcode/og/add-rule'),
)),
);
return $table;
}
/**
* Helper function that returns the labels of multiple groups or a single group.
*
* @param array $gids
* This can be an array of group ids or just a single group id.
*
* @return array $labels
* The label for each group.
*/
function regcode_og_label($gids) {
$labels = array();
// We may want to get the label of a single group by just passing a gid.
$gids = !is_array($gids) ? array(
$gids,
) : $gids;
// Load the entities.
$entities = entity_load_multiple_by_name('node', $gids);
foreach ($entities as $id => $entity) {
// Set the label for the current entity.
$labels[$id] = entity_label('node', $entity);
// Allow altering the entity label. This is useful if we have sub-groups and
// those sub-groups have the same name. This will allow appending something
// to the label to differentiate between them.
drupal_alter('regcode_og_label', $labels, $entity);
}
return $labels;
}
/**
* Implements hook_regcode_used().
*
* Add the new role to the user.
*/
function regcode_og_regcode_used($regcode, &$account) {
// Do nothing if the regcode is not valid.
if (!is_object($regcode)) {
return;
}
$settings = variable_get('regcode_og_settings', array());
// Apply the rules.
$tags = array_keys($regcode->tags);
foreach ($settings as $rule) {
if (in_array($rule['tid'], $tags)) {
foreach ($rule['ogs'] as $ogid) {
og_group('node', $ogid, array(
'entity' => $account,
));
}
}
}
}
Functions
Name | Description |
---|---|
regcode_og_create | Form: Create form for og assignment rules. |
regcode_og_create_submit | Form submit: Create a new rule set. |
regcode_og_help | Implements hook_help(). |
regcode_og_label | Helper function that returns the labels of multiple groups or a single group. |
regcode_og_menu | Implements hook_menu(). |
regcode_og_name_exists | Returns true if a rule with the given machine_name exists. |
regcode_og_regcode_used | Implements hook_regcode_used(). |
regcode_og_rules_page | List all of the rules as a HTML table. |
regcode_og_settings | Form: Settings form for an og assignment rule. |
regcode_og_settings_delete | Callback: Delete a rule. |
regcode_og_settings_delete_confirm | Page callback for role-rule deletion; validates the security token as well. |
regcode_og_settings_submit | Form submit: Save settings form. |