<?php
/**
 * @file
 * Primary hook implementations for Metatag Context.
 */

/**
 * Implements hook_menu().
 */
function metatag_context_menu() {
  $items['admin/config/search/metatags/context'] = array(
    'title' => 'By path',
    'page callback' => 'metatag_context_context_overview',
    'access arguments' => array('administer meta tags'),
    'file' => 'metatag_context.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/config/search/metatags/context/add'] = array(
    'title' => 'Add a meta tag by path',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('metatag_context_config_add_form'),
    'access arguments' => array('administer meta tags'),
    'file' => 'metatag_context.admin.inc',
    'type' => MENU_LOCAL_ACTION,
  );
  $items['admin/config/search/metatags/context/%context'] = array(
    'title' => 'Configure metatags by path',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('metatag_context_config_edit_form', 5),
    'access arguments' => array('administer meta tags'),
    'file' => 'metatag_context.admin.inc',
  );
  $items['admin/config/search/metatags/context/%context/delete'] = array(
    'title' => 'Delete metatags by path',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('metatag_context_delete_form', 5),
    'access arguments' => array('administer meta tags'),
    'file' => 'metatag_context.admin.inc',
  );

  return $items;
}

/**
 * Implements hook_context_plugins().
 */
function metatag_context_context_plugins() {
  return array(
    'metatag_context_reaction' => array(
      'handler' => array(
        'path' => drupal_get_path('module', 'metatag_context'),
        'file' => 'metatag_context.context.inc',
        'class' => 'metatag_context_reaction',
        'parent' => 'context_reaction',
      ),
    ),
  );
}

/**
 * Implements hook_context_registry().
 */
function metatag_context_context_registry() {
  return array(
    'reactions' => array(
      'metatag_context_reaction' => array(
        'title' => t('Meta Data'),
        'description' => t('Control page meta tags using the Metatag module.'),
        'plugin' => 'metatag_context_reaction',
      ),
    ),
  );
}

/**
 * Implements hook_context_page_reaction().
 */
function metatag_context_context_page_reaction() {
  if ($plugin = context_get_plugin('reaction', 'metatag_context_reaction')) {
    $plugin->execute();
  }
}

/**
 * Implements hook_page_build().
 */
function metatag_context_page_build(&$page) {
  // Load the meta tags that have been generated for this page.
  $metatags = drupal_static('metatag_context', array());

  if (!empty($metatags)) {
    // The page region can be changed.
    $region = variable_get('metatag_page_region', 'content');
    $page[$region]['metatags']['global'] = $metatags;
  }
}

/**
 * Implements hook_preprocess_html().
 */
function metatag_context_preprocess_html(&$variables) {
  $metadata = drupal_static('metatag_context');

  if (isset($metadata['metadata_title'])) {
    $variables['head_title'] = token_replace($metadata['metadata_title']);
  }
}

/**
 * Implements hook_ctools_plugin_api().
 */
function metatag_context_ctools_plugin_api() {
  list($module, $api) = func_get_args();
  if ($module == "context" && $api == "context") {
    return array("version" => "3");
  }
}
