You are here

autoassignrole.install in Auto Assign Role 7

Installation related functionality for the auto assign role module.

File

autoassignrole.install
View source
<?php

/**
 * @file
 *
 * Installation related functionality for the auto assign role module.
 */

/**
 * Implements hook_install().
 */

// function autoassignrole_install() {
// }

/**
 * Implements hook_uninstall().
 */
function autoassignrole_uninstall() {
  variable_del('autoassignrole_admin_active');
  variable_del('autoassignrole_auto_active');
  variable_del('autoassignrole_auto_roles');
  variable_del('autoassignrole_user_active');
  variable_del('autoassignrole_user_description');
  variable_del('autoassignrole_user_fieldset_title');
  variable_del('autoassignrole_user_multiple');
  variable_del('autoassignrole_user_required');
  variable_del('autoassignrole_user_roles');
  variable_del('autoassignrole_user_selection');
  variable_del('autoassignrole_user_sort');
  variable_del('autoassignrole_user_title');

  // This functionality hasn't been added yet, but it might exist from a prior
  // D6 installation.
  variable_del('autoassignrole_node_user_register');
  variable_del('autoassignrole_page_active');

  // Remove the autoassignrole_page table.
  db_drop_table('autoassignrole_page');
}

/**
 * Implements of hook_schema().
 */
function autoassignrole_schema() {
  $schema['autoassignrole_page'] = array(
    'description' => 'Stores autoassignrole page information',
    'fields' => array(
      'rid_page_id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'The unique identifier for this role page',
      ),
      'rids' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'The role IDs for this page',
      ),
      'path' => array(
        'type' => 'text',
        'not null' => TRUE,
        'description' => 'The path for this page',
      ),
      'menu' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'description' => 'The default parent menu for this menu item',
      ),
      'title' => array(
        'type' => 'text',
        'not null' => TRUE,
        'description' => 'The description for this page',
      ),
      'display' => array(
        'type' => 'int',
        'not null' => TRUE,
        'description' => 'The display type for this page',
      ),
    ),
    'primary key' => array(
      'rid_page_id',
    ),
  );
  return $schema;
}

/**
 * Implementations of hook_update_N().
 */

/**
 * Upgrade the custom table used on the D6 version.
 */
function autoassignrole_update_7100() {
  if (db_table_exists('autoassignrole')) {

    // The D6 variables with the D7 defaults.
    $defaults = array(
      'auto_active' => 0,
      'auto_admin_active' => 0,
      'auto_roles' => serialize(array()),
      'node_user_register' => '',
      'page_active' => 0,
      'user_active' => 0,
      'user_description' => t('Select a role'),
      'user_fieldset_title' => t('User Roles'),
      'user_multiple' => 0,
      'user_required' => 0,
      'user_roles' => serialize(array()),
      'user_selection' => 0,
      'user_sort' => 'SORT_ASC',
      'user_title' => t("Role"),
    );

    // Get a list of all current variables.
    $results = db_select('autoassignrole', 'a')
      ->fields('a', array(
      'arid',
      'value',
    ))
      ->execute();
    while ($setting = $results
      ->fetchObject()) {
      if (isset($defaults[$setting->arid])) {
        if (strcmp($setting->value, $defaults[$setting->arid]) === 0) {
          drupal_set_message(t('AutoAssignRole upgrade: @var is using the default value so has been skipped.', array(
            '@var' => $setting->arid,
          )));
        }
        else {

          // Expand any serialized objects, ignore any error messages.
          if (!empty($setting->value) && !is_numeric($setting->value)) {
            $unserialized = @unserialize($setting->value);
            if (!empty($unserialized) && $setting->value != $unserialized) {
              $setting->value = $unserialized;
            }
          }

          // Save the custom value.
          variable_set('autoassignrole_' . $setting->arid, $setting->value);
          drupal_set_message(t('AutoAssignRole upgrade: @var has been converted.', array(
            '@var' => $setting->arid,
          )));
        }
      }
    }
    drupal_set_message(t('AutoAssignRole upgrade: removed the "autoassignrole" table.'));
    db_drop_table('autoassignrole');
  }
  else {
    drupal_set_message(t('AutoAssignRole: no need to upgrade existing D7 sites.'));
  }
}

/**
 * Add the assign to page table.
 */
function autoassignrole_update_7101() {
  if (!db_table_exists('autoassignrole_page')) {
    $schema['autoassignrole_page'] = array(
      'description' => 'Stores autoassignrole page information',
      'fields' => array(
        'rid_page_id' => array(
          'type' => 'serial',
          'not null' => TRUE,
          'description' => 'The unique identifier for this role page',
        ),
        'rids' => array(
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'description' => 'The role IDs for this page',
        ),
        'path' => array(
          'type' => 'text',
          'not null' => TRUE,
          'description' => 'The path for this page',
        ),
        'menu' => array(
          'type' => 'varchar',
          'length' => 255,
          'not null' => TRUE,
          'description' => 'The default parent menu for this menu item',
        ),
        'title' => array(
          'type' => 'text',
          'not null' => TRUE,
          'description' => 'The description for this page',
        ),
        'display' => array(
          'type' => 'int',
          'not null' => TRUE,
          'description' => 'The display type for this page',
        ),
      ),
      'primary key' => array(
        'rid_page_id',
      ),
    );
    db_create_table('autoassignrole_page', $schema['autoassignrole_page']);
  }
}

/**
 * Update variable autoassignrole_user_selection.
 */
function autoassignrole_update_7102() {
  $user_selection = variable_get('autoassignrole_user_selection', 0);

  // If the current user_selection is set to be a checkbox (2), update it.
  if ($user_selection == 2) {
    variable_set('autoassignrole_user_selection', AUTOASSIGNROLE_ELEMENT_RADIO_CHECKBOX);
  }
}

/**
 * Migrate db schema from Drupal 6 to 7.
 */
function autoassignrole_update_7103() {

  // The current autoassignrole_page table is in a d6 format.
  if (db_field_exists('autoassignrole_page', 'rid')) {

    // Rename the current 'old' table.
    db_rename_table('autoassignrole_page', 'autoassignrole_page_v6');

    // Recreate the new table using the new schema.
    autoassignrole_update_7101();

    // Get the the contents of the old table to add to the new table.
    $query = db_select('autoassignrole_page_v6', 'aarp6')
      ->fields('aarp6');
    $result = $query
      ->execute();
    $values = array();
    $roles_keys = array_keys(user_roles(TRUE));
    while ($row = $result
      ->fetchAssoc()) {
      foreach ($roles_keys as $role_id) {
        $rids[$role_id] = $role_id == $row['rid'] ? $row['rid'] : 0;
      }
      $values[] = array(
        'rids' => serialize($rids),
        'path' => $row['path'],
        'menu' => $row['menu'],
        'title' => $row['title'],
        'display' => $row['display'],
      );
    }

    // Insert the values from the old table into the new table.
    $query = db_insert('autoassignrole_page')
      ->fields(array(
      'rids',
      'path',
      'menu',
      'title',
      'display',
    ));
    foreach ($values as $record) {
      $query
        ->values($record);
    }
    $query
      ->execute();

    // Delete the old d6 table.
    db_drop_table('autoassignrole_page_v6');
  }
}

/**
 * Update the user description variable to allow HTML.
 */
function autoassignrole_update_7104() {
  $user_desc = _autoassignrole_get_user_description();
  if (!is_array($user_desc)) {
    $defaults = array(
      'value' => $user_desc,
      'format' => filter_default_format(),
    );
    variable_set('autoassignrole_user_description', $defaults);
  }
}

/**
 * Change the rids column from varchar to text.
 */
function autoassignrole_update_7105() {
  db_change_field('autoassignrole_page', 'rids', 'rids', array(
    'type' => 'text',
    'not null' => TRUE,
  ));
  drupal_flush_all_caches();
}

Functions

Namesort descending Description
autoassignrole_schema Implements of hook_schema().
autoassignrole_uninstall Implements hook_uninstall().
autoassignrole_update_7100 Upgrade the custom table used on the D6 version.
autoassignrole_update_7101 Add the assign to page table.
autoassignrole_update_7102 Update variable autoassignrole_user_selection.
autoassignrole_update_7103 Migrate db schema from Drupal 6 to 7.
autoassignrole_update_7104 Update the user description variable to allow HTML.
autoassignrole_update_7105 Change the rids column from varchar to text.