You are here

front_page.module in Front Page 7

File

front_page.module
View source
<?php

/**
 *
 * This module allows the site admin to set advanced front page settings.
 *
 * This version is for Drupal 7. Earlier versions can be found at http://drupal.org/project/front.
 *
 * This module was developed by Dublin Drupaller and various members of the drupal community.
 *
 * If you have any ideas/patches or requests, please post them at http://drupal.org/project/issues/front.
 *
 */

/**
 * Implements hook_help().
 */
function front_page_help($section) {
  switch ($section) {
    case 'admin/config/system/front':
      $roles = user_roles();
      $special_note = '';
      if (count($roles) >= 3) {
        krsort($roles);
        $special_note = '<fieldset>' . t('<legend><span class="fieldset-legend">front page by role usage</span></legend><div class="fieldset-wrapper"><p><strong>Note:</strong>When using the front pages by role option, please note that if a user has multiple roles the first role match will determine which front page they see. Here is the order that the roles will be checked (from left to right).</p>') . '<p>';
        foreach ($roles as $role_id => $role_name) {
          $special_note .= "<strong>{$role_id}</strong> {$role_name} |  ";
        }
        $special_note .= '</p>' . t('<p>The default setting for new roles is SAME AS AUTHENTICATED USERS.</p>') . '</div></fieldset>';
      }
      return t('<p>Setup custom front pages for your site. Click on the option to expand the form and customise your settings.</p><p><em>Anonymous User</em> = visitor not logged in  | <em>Authenticated User</em> = visitor who is logged in</p>') . $special_note;
  }
}

/**
 * Implements hook_menu().
 */
function front_page_menu() {
  $items['admin/config/system/front'] = array(
    'title' => 'Advanced front page settings',
    'description' => 'Specify a unique layout or splash page based on role type - override your HOME and breadcrumb links - display a custom mission style notice for users who haven\'t visited in a while - disable site and display a \'temporarily offline\' message.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'front_page_admin',
    ),
    'access arguments' => array(
      'administer menu',
    ),
  );
  $items['front_page'] = array(
    'title' => '',
    'page callback' => 'front_page',
    'access arguments' => array(
      'access frontpage',
    ),
    'type' => MENU_SUGGESTED_ITEM,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function front_page_permission() {
  return array(
    'access frontpage' => array(
      'title' => t('access frontpage'),
      'description' => t('Allow access to the custom front page.'),
    ),
  );
}

/**
 * This function sets up the admin/build/front_page settings page.
 *
 */
function front_page_admin($form, &$form_state) {

  // Load any existing settings and build the by redirect by role form
  $form['byrole'] = array(
    '#type' => 'fieldset',
    '#title' => t('Home pages'),
    '#description' => t('Designate a home page for each role. "Splash" pages will be used only when users are connecting from external sites. Spash pages must be in the form "node/x" where x is the id of a node, e.g., "node/21" (without the quotes). Only the body of the node will be displayed. Home pages are displayed when users follow internal home links, or for external links where there is no splash page designated, and may point to any page on the site.'),
    '#theme' => 'front_page_roles',
    '#collapsible' => TRUE,
    '#tree' => TRUE,
  );

  // build the form for roles
  $roles = user_roles();
  foreach (element_children($roles) as $role) {
    $rolename = $roles[$role];
    $form[$role] = array(
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#title' => t('Front Page for !rolename.', array(
        '!rolename' => $rolename,
      )),
    );
    $form[$role]['front_' . $role . '_text'] = array(
      '#type' => 'textarea',
      '#title' => t('Body'),
      '#default_value' => variable_get('front_' . $role . '_text', ''),
      '#cols' => 60,
      '#rows' => 20,
      '#description' => t('Paste your HTML or TEXT here.') . '<br /><br />' . t('You can paste in the full HTML code for a complete page and include a different style sheet in the HEAD of the document if you want a completely different layout and style to the rest of your site.'),
    );

    // Set the type options common for all roles.
    $options = array(
      'themed' => t('themed'),
      'full' => t('full'),
      'redirect' => t('redirect'),
    );

    // Set the description common for all roles.
    $description = '<dl><dt>' . t('themed') . '</dt>
      <dd>' . t('means your default layout, theme and stylesheet will be loaded with your custom front_page.') . '</dd></dl>';
    $description .= '<dl><dt>' . t('full') . '</dt>
      <dd>' . t('allows you to have a completely different layout, style sheet etc.') . '</dd></dl>';
    $description .= '<dl><dt>' . t('redirect') . '</dt>
      <dd>' . t('will automatically redirect visitors already logged in to a specific page specified in the REDIRECT TO box.') . '</dd></dl>';

    // Set the options that varies from role to role.
    switch ($role) {
      case 1:

        // Anonymous user
        $default_value = variable_get('front_1_type', 'themed');
        break;
      case 2:

        // Authenticated user
        $default_value = variable_get('front_2_type', 'same_as_anon');
        $options['same_as_anon'] = t('same as anonymous users');
        $description .= '<dl><dt>' . t('same as anonymous users') . '</dt>
          <dd>' . t('will display the same content as for Anonymous users.') . '</dd></dl>';
        break;
      default:

        // Other roles
        $default_value = variable_get('front_' . $role . '_type', 'same_as_anon');
        $options['same_as_anon'] = t('same as anonymous users');
        $options['sameasauth'] = t('same as authenticated users');
        $description .= '<dl><dt>' . t('same as anonymous users') . '</dt>
          <dd>' . t('will display the same content as for Anonymous users.') . '</dd></dl>';
        $description .= '<dl><dt>' . t('same as authenticated users') . '</dt>
          <dd>' . t('will display the same content as for Authenticated (logged in) users.') . '</dd></dl>';
        break;
    }
    $form[$role]['front_' . $role . '_type'] = array(
      '#type' => 'select',
      '#title' => t('Select type'),
      '#default_value' => $default_value,
      '#options' => $options,
      '#description' => '<p>' . $description . '</p>',
    );
    $form[$role]['front_' . $role . '_redirect'] = array(
      '#type' => 'textfield',
      '#title' => t('Redirect to'),
      '#default_value' => variable_get('front_' . $role . '_redirect', 'node'),
      '#cols' => 20,
      '#rows' => 1,
      '#description' => t('If you have selected <strong>REDIRECT</strong> you need to specify where the user should be pointed to.  If you are not using clean URLs, specify the part after "?q=".  If unsure, specify "node".'),
    );
    $form[$role]['front_' . $role . '_php'] = array(
      '#type' => 'checkbox',
      '#title' => t('Allow embedded PHP code in this front page'),
      '#return_value' => 1,
      '#default_value' => variable_get('front_' . $role . '_php', 0),
      '#description' => t('If this option is enabled, the body text can have embedded &lt;?php...?&gt; tags with PHP code inside. Click on the special Handbook pages for tips on <a href="http://drupal.org/node/23220">Using PHP snippets in your front_page</a>.'),
    );
  }
  $form['override'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Override your HOME breadcrumb links'),
  );
  $form['override']['front_page_breadcrumb'] = array(
    '#type' => 'checkbox',
    '#title' => t('Redirect your site HOME links'),
    '#return_value' => 1,
    '#default_value' => variable_get('front_page_breadcrumb', 0),
    '#description' => t('Check this box if you want to redirect users who click on any HOME links (including the HOME breadcrumb link). Useful if you have a splash page you don\'t want users brought back to when they click on HOME links from within your site.'),
  );
  $form['override']['front_page_breadcrumb_redirect'] = array(
    '#type' => 'textfield',
    '#title' => t('Redirect your site HOME links to'),
    '#default_value' => variable_get('front_page_breadcrumb_redirect', ''),
    '#cols' => 20,
    '#rows' => 1,
    '#description' => t('Specify where the user should be redirected to. An example would be <em>node/12</em>. Leave blank when you\'re not using HOME redirect.'),
    '#field_prefix' => url(NULL, array(
      'absolute' => TRUE,
    )) . (variable_get('clean_url', 0) ? '' : '?q='),
  );
  $form['site'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Activate your front_page settings'),
  );
  $form['site']['site_frontpage'] = array(
    '#type' => 'textfield',
    '#title' => t('Default front page'),
    '#default_value' => variable_get('site_frontpage', 'node'),
    '#size' => 40,
    '#description' => t('Change this setting to <em>front_page</em> to activate your front page settings.'),
    '#field_prefix' => url(NULL, array(
      'absolute' => TRUE,
    )) . (variable_get('clean_url', 0) ? '' : '?q='),
  );
  return system_settings_form($form);
}

/* * *********************************************
 * Function to handle the display of the front page.
 */
function front_page() {

  // Set a default value for landing zone if no front_page variables are filled.
  // If the module is turned on but empty in certain areas then a redirection loop will occur.
  // Defaulting to 'node' prevents this.
  $output = 'node';

  //this checks to see if you are overriding HOME links on the site
  if (variable_get('front_page_breadcrumb', 0) && isset($_SERVER['HTTP_REFERER'])) {

    // This checks to see if the referer is an internal HOME link or not.
    $ref = $_SERVER['HTTP_REFERER'];
    global $user, $base_url;
    $parsed_url = parse_url($base_url);
    $domain = $parsed_url['host'];
    if (stristr($ref, $domain)) {
      $url = front_page_parse_url(variable_get('front_page_breadcrumb_redirect', 'node'));
      drupal_goto($url['path'], $url['options']);
    }
  }

  // the following is the code that loads the front_page settings
  global $user;

  // if the user is not logged in load up the page for anonymous users
  if (!$user->uid) {
    $output = variable_get('front_1_text', 'drupal');

    // If PHP code execution is allowed then use eval.
    if (variable_get('front_1_php', 0)) {
      if (module_exists('php')) {
        $output = php_eval($output);
      }
    }
    $fptype = variable_get('front_1_type', 'drupal');
    switch ($fptype) {
      case 'themed':

        // Removed theme('page', $output) wrapper from $output as
        // discussed at http://drupal.org/node/224333#theme_page.
        return $output;
      case 'full':
        print $output;
        exit;
      case 'redirect':
        $url = front_page_parse_url(variable_get('front_1_redirect', 'drupal'));
        drupal_goto($url['path'], $url['options']);
        break;
    }
  }
  $roles = "";

  //make sure the roles variable is clear
  $roles = user_roles();

  //load up the user roles for the site
  krsort($roles);

  //reverse the order of role types so it checks the highest or more recent role type first.
  $siteroles = count($roles);

  /* this is where the front page by role stuff happens  */
  if ($user->uid) {

    //if the user is logged in and the site has more than 2 roles setup
    foreach ($roles as $role_id => $role_name) {

      // run through each role type starting at the most recent working backwards
      // if the current visitor has a role type that matches and if the front page setting for that role type is not empty.
      if (array_key_exists($role_id, $user->roles)) {
        $output = variable_get('front_' . $role_id . '_text', '');

        // if PHP code execution is allowed then use eval
        if (variable_get('front_' . $role_id . '_php', 0)) {
          if (module_exists('php')) {
            $output = php_eval($output);
          }
        }
        $fptype = variable_get('front_' . $role_id . '_type', 'drupal');

        //check whether it is a themed or full front page.
        switch ($fptype) {
          case 'themed':

            // Removed theme('page', $output) wrapper from $output as
            // discussed at http://drupal.org/node/224333#theme_page.
            return $output;
          case 'full':
            print $output;
            exit;
          case 'same_as_anon':
            $output = variable_get('front_1_text', 'drupal');

            // If PHP code execution is allowed then use eval.
            if (variable_get('front_1_php', 0)) {
              if (module_exists('php')) {
                $output = php_eval($output);
              }
            }
            $fptype = variable_get('front_1_type', 'drupal');
            switch ($fptype) {
              case 'themed':

                // Removed theme('page', $output) wrapper from $output as
                // discussed at http://drupal.org/node/224333#theme_page.
                return $output;
              case 'full':
                print $output;
                exit;
              case 'redirect':
                $url = front_page_parse_url(variable_get('front_1_redirect', 'drupal'));
                drupal_goto($url['path'], $url['options']);
                break;
            }
          case 'sameasauth':
            $output = variable_get('front_2_text', 'drupal');

            // If PHP code execution is allowed then use eval.
            if (variable_get('front_2_php', 0)) {
              if (module_exists('php')) {
                $output = php_eval($output);
              }
            }
            $fptype = variable_get('front_2_type', 'drupal');
            switch ($fptype) {
              case 'themed':

                // Removed theme('page', $output) wrapper from $output as
                // discussed at http://drupal.org/node/224333#theme_page.
                return $output;
              case 'full':
                print $output;
                exit;
              case 'redirect':
                $url = front_page_parse_url(variable_get('front_2_redirect', 'drupal'));
                drupal_goto($url['path'], $url['options']);
                break;
            }
          case 'redirect':
            $url = front_page_parse_url(variable_get('front_' . $role_id . '_redirect', 'drupal'));
            drupal_goto($url['path'], $url['options']);
            break;
        }
      }
    }
  }
}
function front_page_parse_url($path) {
  $url['path'] = $path;
  $url['options'] = array();
  if (preg_match('@^(?P<path>[^?#]+)(\\?(?P<query>[^#]*))?(#(?P<fragment>.*))?$@', $path, $match)) {
    $url['path'] = $match['path'];
    if (!empty($match['query'])) {
      foreach (explode('&', $match['query']) as $query_part) {
        list($key, $value) = explode('=', $query_part);
        $url['options']['query'][$key] = $value;
      }
    }
    if (!empty($match['fragment'])) {
      $url['options']['fragment'] = $match['fragment'];
    }
  }
  return $url;
}

Functions

Namesort descending Description
front_page
front_page_admin This function sets up the admin/build/front_page settings page.
front_page_help Implements hook_help().
front_page_menu Implements hook_menu().
front_page_parse_url
front_page_permission Implements hook_permission().