You are here

function rabbit_hole_update_7001 in Rabbit Hole 7.2

Enable Rabbit Hole nodes, and migrate the old data to the new module.

This involves changing database fields names, permissions and existing variables.

File

./rabbit_hole.install, line 14
Install, update and uninstall functions for Rabbit Hole.

Code

function rabbit_hole_update_7001() {

  // Change the name of the database fields.
  $fields['rabbit_hole_action'] = array(
    'name' => 'rh_action',
    'spec' => array(
      'description' => 'Specifies which action that Rabbit Hole should take.',
      'type' => 'int',
      'default' => NULL,
    ),
  );
  $fields['rabbit_hole_redirect'] = array(
    'name' => 'rh_redirect',
    'spec' => array(
      'description' => 'The path to where the user should get redirected to.',
      'type' => 'varchar',
      'default' => NULL,
      'length' => 255,
    ),
  );
  $fields['rabbit_hole_redirect_response'] = array(
    'name' => 'rh_redirect_response',
    'spec' => array(
      'description' => 'Specifies the HTTP response code that should be used when perform a redirect.',
      'type' => 'int',
      'default' => NULL,
    ),
  );
  foreach ($fields as $name => $changes) {
    if (db_field_exists('node', $name)) {
      db_change_field('node', $name, $changes['name'], $changes['spec']);
    }
  }

  // Enable the Rabbit Hole nodes module.
  if (!module_exists('rh_node')) {
    module_enable(array(
      'rh_node',
    ));
  }

  // Update the permissions.
  $administer_rabbit_hole = user_roles(FALSE, 'administer rabbit hole');
  foreach ($administer_rabbit_hole as $rid => $name) {
    user_role_change_permissions($rid, array(
      'administer rabbit hole' => FALSE,
      'administer rh_node' => TRUE,
    ));
  }
  $bypass_rabbit_hole = user_roles(FALSE, 'bypass rabbit hole');
  foreach ($bypass_rabbit_hole as $rid => $name) {
    user_role_change_permissions($rid, array(
      'bypass rabbit hole' => FALSE,
      'bypass rh_node' => TRUE,
    ));
  }

  // Change the name of the variables.
  $node_types = array_keys(node_type_get_types());
  foreach ($node_types as $node_type) {
    $action = variable_get('rabbit_hole_action_' . $node_type, NULL);
    if (isset($action)) {
      variable_set('rh_node_action_' . $node_type, $action);
      variable_del('rabbit_hole_action_' . $node_type);
    }
    $redirect = variable_get('rabbit_hole_redirect_' . $node_type, NULL);
    if (isset($redirect)) {
      variable_set('rh_node_redirect_' . $node_type, $redirect);
      variable_del('rabbit_hole_redirect_' . $node_type);
    }
    $redirect_response = variable_get('rabbit_hole_redirect_response_' . $node_type, NULL);
    if (isset($redirect_response)) {
      variable_set('rh_node_redirect_response_' . $node_type, $redirect_response);
      variable_del('rabbit_hole_redirect_response_' . $node_type);
    }
  }
}