<?php
// $Id: front_page.module,v 1.28 2006/04/17 11:06:54 jasonm3m Exp $ 

/** 
 *  
 * This module allows the user to set a custom front page
 * to the Drupal installation.
 * 
 * This version works with Drupal 4.7 and will not work with Drupal 4.5 or Drupal 4.6
 *
 *
 * This module was developed by various members of the drupal community
 * and is maintained by Dublin Drupaller.
 * 
 * If you have any ideas/patches or requests, please post them on the drupal.org
 * site or email us at gus(at)modernmediamuse.com
 *
 *
 */ 

/** 
 * Implementation of hook_help(). 
 * 
 * This is the explanatory text that appears in your administer - modules page 
 * where you switch on/off modules
 */ 
function front_page_help($section) { 
  switch ($section) { 
    case 'admin/modules#description': 
      // This description is shown in the listing at admin/modules. 
      return t('Allows you to setup custom front pages for your site. After enabling this module, click on %link to setup your custom front pages.', array('%link'=>l('admin/settings/front_page', 'admin/settings/front_page'))); 
       case 'admin/settings/front_page': 
      // This description is shown in the listing at admin/modules. 
      return t('<P>Setup custom front pages for your site. Click on the option to expand the form and customise your settings.'); 
  } 
} 

/** 
 * this function sets the necessary paths etc. so drupal
 * knows where to find the front_page
 * 
 * in your Administer - Settings make sure default front page is set
 * to front_page
 * 
 * Please note that I have left the Title blank below so nothing appears above the 
 * page when it is displayed.
 */ 
function front_page_menu($may_cache) {
  if ($may_cache) {
    $items = array();
    $items[] = array(
      'path' => 'front_page',
      'title' => t(''),
      'callback' => 'front_page',
      'access' => TRUE,
      'type' => MENU_SUGGESTED_ITEM);
  } 
  return $items;
}

/** 
 * this function sets up the admin/settings/front_page settings page
 * 
 */ 

function front_page_settings() {

  $form['anon'] = array('#type' => 'fieldset','#collapsible' => TRUE, '#collapsed' => TRUE, '#title' => t('Front Page settings for Anonymous Users (visitors not logged in)'),);
  $form['anon']['front_page_text'] = array('#type' => 'textarea', '#title' => t('Body'), '#default_value' => variable_get('front_page_text', ''), '#cols' => 60, '#rows' => 20, '#description' => t('Paste your HTML or TEXT here for visitors to your site who are not logged in. <br><br>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.'));
  $form['anon']['front_page_size'] = array('#type' => 'select', '#title' => t('Select type'), '#default_value' => variable_get('front_page_size','themed'), '#options' => array('themed' => t('themed'), 'full' => t('full'), 'redirect' => t('redirect'),), '#description' => t('<P><strong>THEMED</strong> means your default layout, theme and stylesheet will be loaded with your custom front_page. <br><strong>FULL</strong> allows you to have a completely different layout, style sheet etc. Useful if you want to use a FLASH based front-end to your site or a splash-page for a pay-per-view website.<br><strong>REDIRECT</strong> will automatically redirect visitors not logged in to a specific page specified in the REDIRECT TO box.</p>'),);
  $form['anon']['front_page_anon_redirect'] = array('#type' => 'textfield', '#title' => t('Redirect to'), '#default_value' => variable_get('front_page_anon_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['anon']['front_page_php'] = array('#type' => 'checkbox', '#title' => t('Allow embedded PHP code in front page for anonymous users'), '#return_value' => 1, '#default_value' => variable_get('front_page_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['authenticated'] = array('#type' => 'fieldset', '#collapsible' => TRUE, '#collapsed' => TRUE, '#title' => t('Front Page settings for Authenticated Users (visitors already logged in)'));
  $form['authenticated']['front_page_text_yes'] = array('#type' => 'textarea', '#title' => t('Body'), '#default_value' => variable_get('front_page_text_yes', ''), '#cols' => 60, '#rows' => 20, '#description' => t('Paste your HTML or TEXT here for visitors to your site who are already logged in. <br><br>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.'));
  $form['authenticated']['front_page2_size'] = array('#type' => 'select', '#title' => t('Select type'), '#default_value' => variable_get('front_page2_size','themed'), '#options' => array('themed' => t('themed'), 'full' => t('full'), 'redirect' => t('redirect'),'same' => t('same'),), '#description' => t('<P><strong>THEMED</strong> means your default layout, theme and stylesheet will be loaded with your custom front_page. <br><strong>FULL</strong> allows you to have a completely different layout, style sheet etc.<br><strong>REDIRECT</strong> will automatically redirect visitors already logged in to a specific page specified in the REDIRECT TO box.<br><strong>SAME</strong> will display the same content as for Anonymous (not logged in) users.</p>'),);
  $form['authenticated']['front_page_redirect'] = array('#type' => 'textfield', '#title' => t('Redirect to'), '#default_value' => variable_get('front_page_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['authenticated']['front_page_php_yes'] = array('#type' => 'checkbox', '#title' => t('Allow embedded PHP code in front page for authenticated users'), '#return_value' => 1, '#default_value' => variable_get('front_page_php_yes',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.'));  

  
  $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'), '#cols'          => 20, '#rows'          => 1, '#description'   => t('Once you are happy how your pages <a href="%url">look</a> change your default front page setting from <em>%node</em> to <em>%front_page</em>.', array('%url' => base_path() . 'front_page', '%node' => 'node', '%front_page' => 'front_page')),);  
  return $form; 
}

/** 
 * print out the page
 * 
 * this function loads up the front page and displays it 
 *
 */ 

 
function front_page() {
	//this checks to see if you are overriding HOME links on the site
	if (variable_get('front_page_breadcrumb',0)) {
	  // This checks to see if the referer is an internal HOME link or not.
	  $newhome =  variable_get('front_page_breadcrumb_redirect','node');
	  $ref = $_SERVER["HTTP_REFERER"];
	  global $user, $base_url;
	  $parsed_url=parse_url($base_url);
      $domain = $parsed_url['host'];
	  if (stristr($ref, $domain)) 
	   {drupal_goto($path = $newhome, $query = NULL, $fragment = NULL);
	   }
  }
  			
  // the following is the code that loads the front_page settings
  
  global $user;
  if (!$user->uid || variable_get("front_page2_size", "drupal") == "same"){
    $output = variable_get("front_page_text", "drupal");
    
    if (variable_get('front_page_php', 0)) {
      $output = drupal_eval($output);
    }
		$fpsize = variable_get("front_page_size", "drupal");
		switch ($fpsize) {
        case "themed":
		    print theme('page', $output);
		    return;
		
        case "full":
		    print $output;
		    return;
	  
	    case "redirect":
        $output = variable_get("front_page_anon_redirect", "drupal");
        break;
    }
  }
  else {
    $output = variable_get("front_page_text_yes", "drupal");

    // If PHP content is allowed, execute it.
    if (variable_get('front_page_php_yes',0)) {
      $output = drupal_eval($output);
    }
    $fpsize = variable_get("front_page2_size", "drupal");
		switch ($fpsize) {
      case "themed":
			  print theme('page', $output);
		    return;
		
      case "full":
			  print $output;
			  return;

	  case "redirect":
      $output = variable_get("front_page_redirect", "drupal");
      break;
    }
  }
  // redirect
  drupal_goto($path = $output, $query = NULL, $fragment = NULL);
  
}
?>