<?php

/**
 * To force a section for debugging, set it here! NULL to disable.
 */
define('SECTION_DEBUG', NULL);

/**
 * Set default associations here. If new sections are to be created or
 * modified, this is the place to do it!!
 *
 * The sections should not contain odd characters. Only dashes and
 * alphanumeric characters should be used to keep it consistent.
 */
function _section_data($type = NULL) {
  // Setup paths.
  // "*" can be used as a numeric wildcard and "^" can be used to set base paths.
  // i.e. 'node/*/^' would affect node/23/edit, node/23/delete but not node/23.
  // 'node/^' affects all subpaths of nodes and 'node/*' will only affect node views.
  $front_path = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
  $associate = array(
    'path' => array(
      $front_path                   => 'front',
      'node/XYZ'                    => 'sectionXYZ',
    ),
    // Term id's for each section. "tid NUM" should just be the tid.
    // All of this is just a place holder. Configure the server first.
    'taxonomy' => array(
      '1' => 'taxonomy-1',
      '2' => 'taxonomy-2',
      '3' => 'taxonomy-3',
      // Forum ID.
      '4' => 'forum',
    ),
    // Node types.
    'node_types' => array(
      'story'         => 'story-section',
      'blog'          => 'blog-section',
    ),
  );
  return isset($associate[$type]) ? $associate[$type] : $associate;
}

/**
 * Part of _phptemplate_callback()
 */
function _phptemplate_variables($hook, $vars = array()) {
  // $section variable will be available to all templates.
  $section = get_section(isset($vars['node']) ? $vars['node'] : NULL);
  $vars['section'] = $section;

  // This sets a new template file based on the section.
  if (!empty($section)) {
    $vars['template_files'][] "$hook-$section";
  }

  // Auto load styles. The style sheet must be placed within "styles/" sub-directory
  // inside the theme folder named after the section and prepended with "autoload-".
  if ($hook == 'page' && !empty($section) {
    if (file_exists(path_to_theme() ."/styles/autoload-$section.css")) {
      drupal_add_css(path_to_theme() ."/styles/autoload-$section.css");
      $vars['styles'] = drupal_get_css();
    }
  }

  return $vars;
}


/**
 * Determine the current section. The default associations are at the very top of this file.
 *
 * @see _section_data()
 */
function get_section($node = NULL) {
  // Figure out where we are. Path takes precedence then taxonomy then node types.
  static $output = '';
  static $path_checked = FALSE;
  if (defined('SECTION_DEBUG') && SECTION_DEBUG !== NULL) {
    $output = SECTION_DEBUG;
  }
  if (empty($output)) {
    // Get default associations.
    $path_associate = _section_data('path');
    // Only need to check once.
    if (!$path_checked) {
      // Get the current section based on path.
      foreach ($path_associate as $path => $section) {
        // Check for numeric wild cards matches "*", i.e. node/*.
        if (strpos($path, '*') !== FALSE) {
          $path_pieces = explode('/', $path);
          $count_pieces = count($path_pieces);
          $new_path = '';
          foreach ($path_pieces as $i => $arg) {
            $arg = ($arg == '*') ? arg($i) : $arg;
            $new_path .= ($i < $count_pieces -1) ? "$arg/" : $arg;
          }
          $path = $new_path;
        }
        // Check for parent path matching "^", i.e. node^.
        if (strpos($_GET['q'], substr($path, 0, strpos($path, '^'))) === 0) {
          $path = $_GET['q'];
        }
        if ($_GET['q'] == $path) {
          $output = $section;
          break;
        }
      }
      $path_checked = TRUE;
    }
    if (empty($output) && isset($node->taxonomy)) {
      $taxonomy_associate = _section_data('taxonomy');
      foreach ($node->taxonomy as $term) {
        if (isset($taxonomy_associate[$term->tid])) {
          $output = $taxonomy_associate[$term->tid];
          break;
        }
      }
    }
    if (empty($output) && isset($node)) {
      $node_types_associate = _section_data('node_type');
      if (isset($node_types_associate[$node->type])) {
        $output = $node_types_associate[$node->type];
      }
    }
  }
  return $output;
}