<?php

/**
 * @file
 * Body Class module hooks.
 */

/**
 * Implements hook_help().
 */
function bodyclasses_help($path, $arg) {
  switch ($path) {
    case 'admin/help#bodyclasses':
      $output = '<p>' . t('@todo Add documentation') . '</p>';
      return $output;
    case 'admin/config/content/bodyclasses':
      $output = '<p>' . t('@todo Add documentation') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_permission().
 */
function bodyclasses_permission() {
  $permissions['administer bodyclasses'] = array(
    'title' => t('Administer Body Classes'),
  );

  return $permissions;
}

/**
 * Implements hook_menu().
 */
function bodyclasses_menu() {
  $items = array();

  $items['admin/config/content/bodyclasses'] = array(
    'title' => 'Body Classes',
    'description' => 'Add contextual CSS classes to the body of the page.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('bodyclasses_admin'),
    'access arguments' => array('administer bodyclasses'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'bodyclasses.admin.inc',
  );

  return $items;
}

/**
 * Implements hook_preprocess_html().
 */
function bodyclasses_preprocess_html(&$variables) {
  // Retrieve all the body classes.
  $classes = bodyclasses_get_bodyclasses($variables);
  foreach ($classes as $class) {
    $variables['classes_array'][] = $class;
  }
}

/**
 * Retrieve all body classes defined by all modules.
 */
function bodyclasses_get_bodyclasses($variables, array $hooks = array()) {
  // Retrieve all information for body classes.
  $bodyclasses_info = module_invoke_all('bodyclasses_info');
  $bodyclasses = array();

  // Load the hooks to inject into the body.
  if (empty($hooks)) {
    $hooks = variable_get('bodyclasses', array());
  }

  // Find which body classes to add.
  foreach ($hooks as $name) {
    // If the body class information is available for the given hook.
    if (isset($bodyclasses_info[$name])) {
      // Execute the hook to retrieve all classes.
      $classes = call_user_func($bodyclasses_info[$name]['function'], $variables);
      foreach ($classes as $class) {
        // Construct the class to inject.
        $bodyclasses[] = drupal_html_class($name . '-' . $class);
      }
    }
  }

  return $bodyclasses;
}

/**
 * Implements hook_bodyclasses_info().
 *
 * @see hook_bodyclasses_info()
 */
function bodyclasses_bodyclasses_info() {
  $bodyclasses['user-role'] = array(
    'name' => t('User Role'),
    'function' => 'bodyclasses_bodyclasses_user_role',
  );

  if (module_exists('context')) {
    $bodyclasses['context'] = array(
      'name' => t('Context'),
      'function' => 'bodyclasses_bodyclasses_context',
    );
  }

  if (module_exists('views')) {
    $bodyclasses['views'] = array(
      'name' => t('Views'),
      'function' => 'bodyclasses_bodyclasses_views',
    );
  }

  return $bodyclasses;
}

/**
 * Integration for the body classes for the user roles.
 */
function bodyclasses_bodyclasses_user_role($variables) {
  if (isset($variables['user']) && isset($variables['user']->roles)) {
    return $variables['user']->roles;
  }
  else {
    return array('anonymous-user');
  }
}

/*
 * Integration for the body classes for context
 */
function bodyclasses_bodyclasses_context($variables) {

  $classes = array();

  if (module_exists('context') && is_callable('context_active_contexts')) {
    foreach (context_active_contexts() as $context) {
      if (!empty($context->name)) {
        $classes[] = preg_replace('/[^A-Za-z0-9_]/', '-', trim($context->name, '_- '));
      }
    }
  }

  return $classes;
}

/*
 * Integration for the body classes for views
 */
function bodyclasses_bodyclasses_views($variables) {
  $classes = array();

  if (module_exists('views') && is_callable('views_get_page_view')) {
    if ($view = views_get_page_view()) {
      if (!empty($view->vid)) {
        $classes[] = 'vid-' . $view->vid;
      }
      if (!empty($view->name)) {
        $classes[] = preg_replace('/[^A-Za-z0-9_]/', '-', trim($view->name, '_- '));
      }
      if (!empty($view->current_display)) {
        $classes[] = 'display-' . $view->current_display;
      }
      if (!empty($view->current_page)) {
        $classes[] = 'page-index-' . $view->current_page;
      }
    }
  }

  return $classes;
}
