function autoassignrole_update_7105 in Auto Assign Role 7.2
Same name and namespace in other branches
- 7 autoassignrole.install \autoassignrole_update_7105()
Features integration.
File
- ./
autoassignrole.install, line 262 - Installation related functionality for the auto assign role module.
Code
function autoassignrole_update_7105() {
$roles = user_roles(TRUE);
foreach (array(
'autoassignrole_auto_roles',
'autoassignrole_user_roles',
) as $variable) {
$stored_roles = array();
foreach (variable_get($variable, array()) as $rid => $checked) {
$stored_roles[$roles[$rid]] = $checked ? $roles[$rid] : 0;
}
variable_set($variable, $stored_roles);
}
// Add a machine name to {autoassignrole_page} table, to make it exportable.
db_add_field('autoassignrole_page', 'name', array(
'type' => 'varchar',
'length' => 255,
'not null' => FALSE,
'description' => 'The unique machine name of this page.',
));
// Add a larger column 'roles' to replace 'rids'.
db_add_field('autoassignrole_page', 'roles', array(
'type' => 'text',
'not null' => FALSE,
'description' => 'The roles for this page',
'serialize' => TRUE,
));
// Convert the {autoassignrole_page} table data.
foreach (db_query("SELECT * FROM {autoassignrole_page}")
->fetchAll() as $row) {
$name = preg_replace('@[^a-z0-9]+@', '_', drupal_strtolower($row->title));
$suffixed = $name;
$count = 0;
// Handle possible duplicates in 'title'.
while (db_query("SELECT 1 FROM {autoassignrole_page} WHERE name = :name AND rid_page_id <> :id", array(
':name' => $suffixed,
':id' => $row->rid_page_id,
))
->fetchField()) {
$suffixed = $name . '_' . $count;
$count++;
}
$rids = serialize(array_values(array_filter(array_map(function ($rid) use ($roles) {
return empty($roles[$rid]) ? FALSE : $roles[$rid];
}, unserialize($row->rids)))));
db_update('autoassignrole_page')
->fields(array(
'name' => $suffixed,
'roles' => $rids,
))
->condition('rid_page_id', $row->rid_page_id)
->execute();
}
// Drop legacy fields and indexes.
db_drop_field('autoassignrole_page', 'rid_page_id');
db_drop_field('autoassignrole_page', 'rids');
// Make 'name' primary key.
db_add_primary_key('autoassignrole_page', array(
'name',
));
// Fix the 'not null' problem.
db_change_field('autoassignrole_page', 'name', 'name', array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'The unique machine name of this page.',
));
}