You are here

url_redirect.module in Url Redirect 7

Adds URL Redirect and UI to set paths to redirect.

File

url_redirect.module
View source
<?php

/**
 * @file
 * Adds URL Redirect and UI to set paths to redirect.
 */

/**
 * Implements hook_permission().
 */
function url_redirect_permission() {
  return array(
    'access url redirect settings page' => array(
      'title' => t('Access URL Redirect Settings'),
      'description' => t('Url Redirect page.'),
    ),
    'access url redirect edit page' => array(
      'title' => t('Access URL Redirect Edit Page'),
      'description' => t('Url Redirect Edit page.'),
    ),
    'access url redirect delete page' => array(
      'title' => t('Access URL Redirect Delete'),
      'description' => t('Url Redirect delete page.'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function url_redirect_menu() {
  $items = array();
  $items['admin/config/url_redirect'] = array(
    'title' => t('URL Redirect'),
    'description' => 'Configuration Page for Url Redirect',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'url_redirect_settings_form',
    ),
    'file' => 'url_redirect.admin.inc',
    'access arguments' => array(
      'access url redirect settings page',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/config/url_redirect/list'] = array(
    'title' => t('List URL Redirect'),
    'description' => 'Url Redirect add',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'url_redirect_list_form',
    ),
    'file' => 'url_redirect_list.inc',
    'access arguments' => array(
      'access url redirect settings page',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  $items['admin/config/url_redirect/edit'] = array(
    'title' => t('URL Redirect Edit'),
    'description' => 'Edit URL Redirect',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'url_redirect_edit_form',
    ),
    'file' => 'url_redirect_edit.inc',
    'access arguments' => array(
      'access url redirect edit page',
    ),
    'type' => MENU_SUGGESTED_ITEM,
  );
  $items['admin/config/url_redirect/delete'] = array(
    'title' => t('Url Redirect Delete'),
    'description' => 'Delete Url Redirect',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'url_redirect_delete_form',
    ),
    'file' => 'url_redirect_delete.inc',
    'access arguments' => array(
      'access url redirect delete page',
    ),
    'type' => MENU_SUGGESTED_ITEM,
  );
  return $items;
}

/**
 * Checks if current page is registered as a menu item (exists).
 *
 * @return bool
 *   Whether or not the menu item was found.
 */
function match_redirect_url_exists() {
  return (bool) menu_get_item();
}

/**
 * Implements hook_page_build().
 */
function url_redirect_page_build(&$page) {
  global $user;
  $current_alias = drupal_get_path_alias();

  // Check URL path in url_redirect table.
  if (drupal_is_front_page()) {
    $path = check_plain('<front>');
    $path_check = url_redirect_path_check($path);
  }
  else {
    $path = check_plain($_GET['q']);
    $path_check = url_redirect_path_check($path);
  }

  // Get wildcards list.
  $wildcard_list = url_redirect_get_wildcards();

  // check for path matcher.
  foreach ($wildcard_list as $delta => $list) {
    $redirect = $list['redirect_path'];
    if (drupal_match_path($current_alias, $list['path'])) {

      // Do the redirect.
      $path_check = url_redirect_path_check($list['path']);
      break;
    }
  }
  if ($path_check) {
    $check_for = $path_check['check_for'];
    $message = $path_check['message'];
    if ($message == t('Yes')) {
      drupal_set_message(t("You have been redirected to '@link_path'.", array(
        '@link_path' => $path_check['redirect_path'],
      )));
    }

    // Check for Role.
    if ($check_for == t('Role')) {
      $role_check = (array) json_decode($path_check['roles']);
      $role_check_array = array_keys($role_check);
      $user_role_check_array = array_keys($user->roles);
      $checked_result = array_intersect($role_check_array, $user_role_check_array);
      if ($checked_result) {
        drupal_goto($path_check['redirect_path']);
      }
    }
    elseif ($check_for == t('User')) {
      $user_check = (array) json_decode($path_check['users']);
      $user_check_array = array_keys($user_check);
      $uid = array(
        $user->uid,
      );
      $checked_user_result = array_intersect($user_check_array, $uid);
      if ($checked_user_result) {
        drupal_goto($path_check['redirect_path']);
      }
    }
  }
}

/**
 * Fetch list of all the users.
 */
function url_redirect_user_fetch() {
  $query = db_select('users', 'u')
    ->fields('u', array(
    'uid',
    'name',
  ))
    ->condition('uid', 0, '!=')
    ->orderBy('name');
  $result = $query
    ->execute()
    ->fetchAllKeyed();
  return $result;
}

/**
 * Checking the path.
 */
function url_redirect_path_check($path) {
  $query = db_select('url_redirect', 'u')
    ->fields('u')
    ->condition('path', $path, '=')
    ->condition('status', 1, '=');
  $result = $query
    ->execute()
    ->fetchAssoc();
  if (!$result) {
    $query = db_select('url_redirect', 'u')
      ->fields('u')
      ->condition('path', '/' . $path, '=')
      ->condition('status', 1, '=');
    $result = $query
      ->execute()
      ->fetchAssoc();
  }
  return $result;
}

/**
 * Edit the path.
 */
function url_redirect_path_edit($path) {
  $query = db_select('url_redirect', 'u')
    ->fields('u')
    ->condition('path', $path, '=');
  $result = $query
    ->execute()
    ->fetchAssoc();
  return $result;
}

/**
 * Get Wildcard path(s).
 */
function url_redirect_get_wildcards() {
  $wildcards = array();
  $query = db_select('url_redirect', 'u');
  $query
    ->fields('u', array(
    'path',
    'redirect_path',
  ));
  $db_or = db_or();
  $db_or
    ->condition('path', '%' . db_like('/*') . '%', 'LIKE');
  $db_or
    ->condition('path', '%' . db_like('/%') . '%', 'LIKE');
  $query
    ->condition($db_or);
  $results = $query
    ->execute()
    ->fetchAll();
  $i = 0;
  foreach ($results as $result) {
    $wildcards[$i]['path'] = $result->path;
    $wildcards[$i]['redirect_path'] = $result->redirect_path;
    $i++;
  }
  return $wildcards;
}

Functions

Namesort descending Description
match_redirect_url_exists Checks if current page is registered as a menu item (exists).
url_redirect_get_wildcards Get Wildcard path(s).
url_redirect_menu Implements hook_menu().
url_redirect_page_build Implements hook_page_build().
url_redirect_path_check Checking the path.
url_redirect_path_edit Edit the path.
url_redirect_permission Implements hook_permission().
url_redirect_user_fetch Fetch list of all the users.