<?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!!
 */
function _section_data() {
  // Setup paths. "*" can be used as a numeric wildcard.
  $front_path = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
  $path_associate = array(
    $front_path => 'front',
    'forum'     => 'forum',
    'forum/*'   => 'forum',
    'blog/*'    => 'user',
  );
  // 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_associate = array(
    '1' => 'section_1',
    '2' => 'section_2',
    '3' => 'section_3',
    // Whatever the forum ID.
    '4' => 'forum',
  );
  // Node types.
  $node_types_associate = array(
    'news'  => 'news',
    'blog'  => 'user',
    'forum' => 'forum',
  );
  return array($path_associate, $taxonomy_associate, $node_types_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;

  if ($hook == 'page') {
    if (!empty($vars['section']) && 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.
    list($path_associate, $taxonomy_associate, $node_types_associate) = _section_data();
    // Only need to check once.
    if (!$path_checked) {
      // Get the current section based on path.
      foreach ($path_associate as $path => $section) {
        if (strpos('*', $path)) {
          $path_pieces = explode('/', $path);
          $count_pieces = count($path_pieces);
          $new_path = '';
          foreach ($path_pieces as $i => $arg) {
            $arg = ($arg == '*') ? arg($i) : $arg;
            $new_path .= $count_pices !== $i ? "$arg/" : $arg;
          }
          $path = $new_path;
        }
        if ($_GET['q'] == $path) {
          $output = $section;
          break;
        }
      }
      $path_checked = TRUE;
    }
    if (empty($output) && isset($node->taxonomy)) {
      foreach ($node->taxonomy as $term) {
        if (isset($taxonomy_associate[$term->tid])) {
          $output = $taxonomy_associate[$term->tid];
          break;
        }
      }
    }
    if (empty($output) && isset($node) && isset($node_types_associate[$node->type])) {
      $output = $node_types_associate[$node->type];
    }
  }
  return $output;
}