<?php

/**
 * Implementation of hook_help().
 */
function context_layouts_help($path, $arg) {
  switch ($path) {
    case 'admin/help#context_layouts':
      $output = file_get_contents(drupal_get_path('module', 'context_layouts') .'/README.txt');
      return module_exists('markdown') ? filter_xss_admin(module_invoke('markdown', 'filter', 'process', 0, -1, $output)) : '<pre>'. check_plain($output) .'</pre>';
  }
}

/**
 * Implementation of hook_context_plugins().
 * This is a ctools plugins hook.
 */
function context_layouts_context_plugins() {
  return array(
    'context_layouts_reaction_block' => array(
      'handler' => array(
        'path' => drupal_get_path('module', 'context_layouts') .'/plugins',
        'file' => 'context_layouts_reaction_block.inc',
        'class' => 'context_layouts_reaction_block',
        'parent' => 'context_reaction_block',
      ),
    ),
  );
}

/**
 * Implementation of hook_context_registry_alter().
 */
function context_layouts_context_registry_alter(&$registry) {
  if (isset($registry['reactions']['block'])) {
    $registry['reactions']['block']['plugin'] = 'context_layouts_reaction_block';
  }
}

/**
 * Implementation of hook_theme().
 * Declares each theme's layouts as a page template suggestion.
 */
function context_layouts_theme() {
  $info = array();
  foreach (list_themes() as $theme) {
    if (!empty($theme->status) && $layouts = context_layouts_get_layouts($theme->name)) {
      foreach ($layouts as $layout) {
        if (!empty($layout['template'])) {
          $info["page__context_layouts_{$theme->name}_{$layout['layout']}"] = array(
            'template' => $layout['template'],
            'path' => $layout['path'],
          );
        }
      }
    }
  }
  return $info;
}

/**
 * Implementation of hook_context_page_reaction().
 */
function context_layouts_context_page_reaction() {
  $plugin = context_get_plugin('reaction', 'block');
  if ($plugin && method_exists($plugin, 'add_layout_stylesheet')) {
    $plugin->add_layout_stylesheet();
  }
}

/**
 * Preprocessor for theme('page').
 */
function context_layouts_preprocess_page(&$vars) {
  $plugin = context_get_plugin('reaction', 'block');
  if ($plugin && method_exists($plugin, 'add_layout_template')) {
    $plugin->add_layout_template($vars);
  }
}

/**
 * Retrieve layouts for the specified theme.
 */
function context_layouts_get_layouts($theme = NULL, $reset = FALSE) {
  static $layouts = array();
  $layouts = $reset ? array() : $layouts;

  global $theme_key;
  $theme = isset($theme) ? $theme : $theme_key;

  if (!isset($layouts[$theme])) {
    $info = system_get_info('theme', $theme);
    $themes = array();

    // Find all our ancestor themes that use layouts.
    if (isset($info['base theme'])) {
      while (!empty($info['base theme'])) {
        $base_theme = $info['base theme'];
        $info = system_get_info('theme', $base_theme);
        $themes[$base_theme] = $info;
      }
    }

    // Assemble in inheritance order and add the theme on.
    $themes = array_reverse($themes);
    $themes[$theme] = system_get_info('theme', $theme);

    // Merge layout info into a single array.
    foreach ($themes as $key => $info) {
      $path = drupal_get_path('theme', $key);
      if (!empty($info['layouts'])) {
        foreach ($info['layouts'] as $layout => $layout_info) {
          $layout_info['layout'] = str_replace('-', '_', $layout);
          $layout_info['theme'] = $key;
          $layout_info['path'] = $path;
          $layouts[$theme][$layout] = $layout_info;
        }
      }
    }
  }
  return isset($layouts[$theme]) ? $layouts[$theme] : FALSE;
}

/**
 * Get the active layout for the current page.
 */
function context_layouts_get_active_layout($info = TRUE) {
  $plugin = context_get_plugin('reaction', 'block');
  if ($plugin && method_exists($plugin, 'get_active_layout')) {
    return $plugin->get_active_layout($info);
  }
}
