role_export.module in Role Export 6
Same filename and directory in other branches
Role Export's primary module file.
Mainly Drupal hooks along with some additional submit handlers for the roles form. Also adds the ability for roles do have machine_names associated wit the role id.
File
role_export.moduleView source
<?php
/**
* @file
* Role Export's primary module file.
*
* Mainly Drupal hooks along with some additional submit handlers for the roles
* form. Also adds the ability for roles do have machine_names associated wit
* the role id.
*/
/**
* Implements hook_enable().
*/
function role_export_enable() {
role_export_normalize();
}
/**
* Implements hook_flush_caches().
*/
function role_export_flush_caches() {
role_export_normalize();
}
/**
* Implements hook_drush_exit().
*/
function role_export_drush_exit() {
role_export_roles(true);
}
/**
* Implements hook_form_alter().
*/
function role_export_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'user_admin_new_role':
// Add formatting to get the form element to show up correctly.
drupal_add_css(drupal_get_path('module', 'role_export') . '/role_export.css');
$form['name']['#description'] = t('The human-readable name of this role.<br />This text will be displayed as part of the list on the <em>Roles</em> page.');
$form['machine_name'] = array(
'#title' => t('Machine name'),
'#type' => 'textfield',
'#maxlength' => 255,
);
$form['#validate'][] = 'role_export_role_machine_name_validate';
$form['#submit'][] = 'role_export_submit';
break;
case 'user_admin_role':
$roles = role_export_roles();
$form['machine_name'] = array(
'#title' => t('Machine name'),
'#type' => 'textfield',
'#maxlength' => 255,
'#default_value' => $roles[$form['rid']['#value']]->machine_name,
// Role machine names can not be edited.
'#disabled' => TRUE,
'#weight' => -1,
);
$form['#submit'][] = 'role_export_submit';
break;
}
}
/**
* Ensure the machine_name of the new role is valid and doesn't already exist.
*/
function role_export_role_machine_name_validate($form, &$form_state) {
$value = $form_state['values']['machine_name'];
if ($value !== role_export_machine_name_gen($value)) {
form_set_error('machine_name', t("That's not a valid machine name. Please use only lowercase letters a–z, numbers 0–9, and underscore."));
}
foreach (role_export_roles() as $role) {
if ($role->machine_name == $value) {
form_set_error('machine_name', t('The machine_name you\'ve currently entered is already taken, but it must be unique. Please enter in a new machine_name.'));
}
}
}
/**
* Save the role information to the {role} table.
*/
function role_export_submit($form, &$form_state) {
$op = $form_state['values']['op'];
if ($op == 'Add role') {
$role = (object) $form_state['values'];
$query = db_fetch_object(db_query("SELECT rid FROM {role} WHERE name = '%s'", $role->name));
$rid = role_export_generate_id($role->machine_name);
db_query("UPDATE {role} SET rid = %d, machine_name = '%s' WHERE rid = %d", $rid, $role->machine_name, $query->rid);
role_export_normalize();
}
}
/**
* Implements hook_theme().
*/
function role_export_theme() {
return array(
'user_admin_roles' => array(
'render element' => 'form',
'file' => 'role_export.admin.inc',
'function' => 'theme_role_export_user_admin_roles',
),
);
}
/**
* Ensure that all rids are hashes of their machine_name.
*/
function role_export_normalize() {
foreach (role_export_roles(true) as $role) {
// Ensure the rid is the machine_name's hash
if ($role->rid != role_export_generate_id($role->machine_name)) {
if (empty($role->machine_name)) {
$role->machine_name = role_export_machine_name_gen($role->name);
}
$new_rid = role_export_generate_id($role->machine_name);
// Update the current role with the new data.
db_query("UPDATE {role} SET rid = %d, machine_name = '%s' WHERE rid = %d", $new_rid, $role->machine_name, $role->rid);
}
}
}
/**
* Create an array with the current roles in the system.
*/
function role_export_roles($reset = FALSE) {
static $roles;
if (!isset($roles) || $reset) {
$roles = array();
// Select all roles that are not 'anonymous' or 'authenticated'
$query = db_query("SELECT rid, name, machine_name FROM {role} WHERE rid > 2");
while ($value = db_fetch_object($query)) {
$roles[$value->rid] = $value;
}
}
return $roles;
}
/**
* Generates a machine_name from a role name.
*/
function role_export_machine_name_gen($name) {
// Convert any non-alphanumeric characters into underscores.
return preg_replace('/[^a-z0-9]+/', '_', strtolower($name));
}
/**
* Generates a numeric id from a machine name.
*/
function role_export_generate_id($mname) {
$hash = md5($mname);
// Use the last 8 hex digits from the hash.
$shorter = substr($hash, -8);
$number = hexdec($shorter);
return (int) $number;
}
/**
* Implements hook_features_api().
*/
function role_export_features_api() {
return array(
'role_export' => array(
'name' => t('Role Export'),
'feature_source' => TRUE,
'default_hook' => 'role_export_defaults',
'default_file' => FEATURES_DEFAULTS_INCLUDED,
),
);
}
/**
* Implements hook_features_export().
*/
function role_export_features_export($data, &$export, $module_name = '') {
$export['dependencies']['features'] = 'features';
$export['dependencies']['role_export'] = 'role_export';
$map = features_get_default_map('role_export', 'name');
foreach ($data as $role) {
// Role is provided by another module. Add dependency.
if (isset($map[$role]) && $map[$role] != $module_name) {
$export['dependencies'][$map[$role]] = $map[$role];
}
else {
$export['features']['role_export'][$role] = $role;
}
}
return array();
}
/**
* Implements hook_features_export_options().
*/
function role_export_features_export_options() {
$roles = role_export_roles();
$options = array();
foreach ($roles as $rid => $role) {
$options[$role->machine_name] = $role->name;
}
asort($options);
return $options;
}
/**
* Implements hook_features_export_render().
*/
function role_export_features_export_render($module, $data) {
$code = array();
$code[] = ' $roles = array();';
$code[] = '';
$roles = role_export_roles();
$roles_by_machine_name = array();
foreach ($roles as $rid => $role) {
$roles_by_machine_name[$role->machine_name] = $role;
}
foreach ($data as $machine_name) {
if (isset($roles_by_machine_name[$machine_name])) {
$role_identifier = features_var_export($machine_name);
$role_export = features_var_export($roles_by_machine_name[$machine_name], ' ');
$code[] = " \$roles[{$role_identifier}] = {$role_export};";
$code[] = "";
}
}
$code[] = ' return $roles;';
$code = implode("\n", $code);
return array(
'role_export_defaults' => $code,
);
}
/**
* Implements hook_features_revert().
*/
function role_export_features_revert($module) {
role_export_features_rebuild($module);
}
/**
* Implements hook_features_rebuild().
*/
function role_export_features_rebuild($module) {
if ($defaults = features_get_default('role_export', $module)) {
$normals = role_export_roles();
foreach ($defaults as $machine_name => $role) {
db_query("DELETE FROM {role} WHERE rid = %d OR name = '%s' OR machine_name = '%s'", $role['rid'], $role['name'], $role['machine_name']);
drupal_write_record('role', $role);
}
role_export_normalize();
}
}