<?php

/**
 * @file
 * Themes Administration menu like Adminimal theme.
 */

/**
 * Implements hook_menu().
 */
function adminimal_admin_menu_menu() {
  $items = array();
  $items['admin/config/administration/adminimal_menu'] = array(
    'title' => 'Adminimal menu',
    'description' => 'Adjust adminimal menu settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('adminimal_admin_menu_settings'),
    'access arguments' => array('administer site configuration'),
    'file' => 'adminimal_menu_settings.inc',
  );

  return $items;

}

/**
 * Implements hook_page_build().
 */
function adminimal_admin_menu_page_build(&$page) {
  if (!_adminimal_admin_menu_access()) {
    return;
  }
  $path = drupal_get_path('module', 'adminimal_admin_menu');

  // Attach the CSS and JavaScript assets.
  drupal_add_css($path . '/adminimal_admin_menu.css');
  drupal_add_js($path . '/adminimal_admin_menu.js', 'file');

  if (!isset($page['page_bottom']['admin_menu'])) {
    return;
  }
  $attached = &$page['page_bottom']['admin_menu']['#attached'];
  $options = array('every_page' => TRUE);

  // @todo Stop-gap fix until cached rendering is resolved.
  // @see http://drupal.org/node/1567622
  if (module_exists('shortcut')) {
    $attached['css'][drupal_get_path('module', 'shortcut') . '/shortcut.css'] = $options;
  }

  $settings = array();
  // Add current path to support menu item highlighting.
  // @todo Compile real active trail here?
  $args = explode('/', $_GET['q']);
  if ($args[0] == 'admin' && !empty($args[1])) {
    $settings['activeTrail'] = url($args[0] . '/' . $args[1]);
  }
  elseif (drupal_is_front_page()) {
    $settings['activeTrail'] = url('<front>');
  }

  $attached['js'][] = array(
    'data' => array('admin_menu' => array('toolbar' => $settings)),
    'type' => 'setting',
  );
}

/**
 * Implements hook_admin_menu_output_build().
 */
function adminimal_admin_menu_admin_menu_output_build(&$content) {

  if (variable_get('adminimal_admin_menu_render', 'collapsed') != 'hidden') {
    // Add shortcuts bar.
    $content['shortcut'] = array(
      '#access' => module_exists('shortcut'),
      '#weight' => 200,
      '#prefix' => '<div class="shortcut-toolbar">',
      '#suffix' => '</div>',
    );
    $content['shortcut']['shortcuts'] = array(
      // Shortcut module's CSS relies on Toolbar module's markup.
      // @see http://drupal.org/node/1217038
      '#prefix' => '<div id="toolbar">',
      '#suffix' => '</div>',
      // @todo Links may contain .active-trail classes.
      '#pre_render' => array('shortcut_toolbar_pre_render'),
    );
  }

}

/**
 * Implements hook_admin_menu_output_alter().
 */
function adminimal_admin_menu_admin_menu_output_alter(&$content) {
  // Add a class to top-level items for styling.
  foreach (element_children($content['menu']) as $link) {
    $content['menu'][$link]['#attributes']['class'][] = 'admin-menu-toolbar-category';
  }

  // Alter icon.
  unset($content['icon']['icon']['#theme']);
  $content['icon']['icon']['#title'] = '<span class="admin-menu-home-icon">&nbsp;</span>';
  $content['icon']['icon']['#attributes']['class'][] = 'admin-menu-toolbar-home-menu';
  $page['#attributes']['class'][] = 'adminimal-menu';

  // Hide the menu.
  if (variable_get('adminimal_admin_menu_render', 'collapsed') == 'exclusive') {
    unset($content['icon']['icon']);
    unset($content['search']);
    foreach ($content['menu'] as $key => $link) {
      // Move local tasks on 'admin' into icon menu.
      unset($content['menu'][$key]);
    }
  }
}

/**
 * Implements hook_preprocess_html().
 */
function adminimal_admin_menu_preprocess_html(&$vars) {
  if (!_adminimal_admin_menu_access()) {
    return;
  }
  // Add the "adminimal" class to the body for better css selection.
  $vars['classes_array'][] = 'adminimal-menu';

  // Add the shortcut render mode class.
  $vars['classes_array'][] = 'menu-render-'.variable_get('adminimal_admin_menu_render', 'collapsed');

}

/**
 * Check if the user has access to use the admin menu.
 * @return  boolean Result of access checks.
 */
function _adminimal_admin_menu_access() {
  if (!user_access('access administration menu') || admin_menu_suppress(FALSE)) {
    return FALSE;
  }

  // Performance: Skip this entirely for AJAX requests.
  if (strpos($_GET['q'], 'js/') === 0) {
    return FALSE;
  }
  return TRUE;
}
