<?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_admin_menu.admin.inc',
  );

  return $items;
}

/**
 * Implements hook_module_implements_alter().
 */
function adminimal_admin_menu_module_implements_alter(&$implementations, $hook) {
  // Put Adminimal Admin Menu's implementation of hook_admin_menu_output_alter()
  // to the end of the list of implementors, assuring that it will be executed
  // at the very end. Other modules may manipulate the menu items in the
  // administration menu (i.e. the excellent Administration Menu Source) and we
  // need to make sure those changes are preserved and respected by the output
  // of Adminimal Admin Menu.
  if ($hook == 'admin_menu_output_alter') {
    $group = $implementations['adminimal_admin_menu'];
    unset($implementations['adminimal_admin_menu']);
    $implementations['adminimal_admin_menu'] = $group;
  }
}

/**
 * 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');
  $load_slicknav = variable_get('adminimal_admin_menu_slicknav', TRUE);
  $load_jquery = variable_get('adminimal_admin_menu_jquery', TRUE);

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

  _adminimal_admin_menu_support_environment_indicator();

  // Check if both slicknav and custom jQuery must be loaded.
  if ($load_slicknav and $load_jquery) {
    drupal_add_js($path . '/js/jquery.min.js', array(
      'type' => 'file',
      'scope' => 'header',
      'weight' => 888,
    ));
    drupal_add_js($path . '/js/slicknav/jquery-no-conflict.slicknav.js', array(
      'type' => 'file',
      'scope' => 'header',
      'weight' => 888,
    ));
    drupal_add_css($path . '/js/slicknav/slicknav.css');
  }
  elseif ($load_slicknav and !$load_jquery) {
    drupal_add_js($path . '/js/slicknav/jquery.slicknav.js', array(
      'type' => 'file',
      'scope' => 'header',
      'weight' => 888,
    ));
    drupal_add_css($path . '/js/slicknav/slicknav.css');
  }

  drupal_add_js($path . '/js/adminimal_admin_menu.js', array(
    'type' => 'file',
    'scope' => 'header',
    'weight' => 888,
  ));

  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 (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',
  );
}

/**
 * Add CSS for module environment_indicator.
 */
function _adminimal_admin_menu_support_environment_indicator() {
  if (!module_exists('environment_indicator')) {
    return;
  }

  $environment_info = environment_indicator_get_active();

  if (!environment_indicator_check_access($environment_info)) {
    return;
  }

  drupal_add_css('
      div.slicknav_menu a.slicknav_btn:after {
        content: "' . $environment_info['name'] . '";
        margin-left: 1em;
        color: ' . $environment_info['text_color'] . ';
        font-size: 12px;
        background-color: ' . $environment_info['color'] . ';
        padding: 2px 5px;
      }
      #admin-menu-wrapper {background: #222 !important;}
      ',
    array(
      'group' => CSS_DEFAULT,
      'type' => 'inline',
      'preprocess' => FALSE,
      'weight' => '100',
    ));
}

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

  // Shortcut menu.
  if (module_exists('shortcut') && 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';
  // @todo Fix this CS-issue.
  $page['#attributes']['class'][] = 'adminimal-menu';

  // Hide the menu.
  if (module_exists('shortcut') && 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]);
    }
  }

  // Create the responsive menu.
  if (variable_get('adminimal_admin_menu_slicknav', TRUE)) {
    // Join the Icon menu with the administration menu.
    $responsivemenu = array_merge($content['icon'], $content['menu']);

    // Give it ID to target it later with js and css.
    $responsivemenu['#wrapper_attributes']['id'] = 'admin-menu-menu-responsive';

    // Move the icon menu to the top.
    $responsivemenu['icon']['#weight'] = '-100';

    // Change the link title for 'admin/index' to Administration if it exists.
    if (!empty($responsivemenu['admin/index'])) {
      $responsivemenu['admin/index']['#title'] = t('Administration');
    }

    // Add the account menu, and push account and logout links toward bottom.
    $responsivemenu = array_merge($content['account'], $responsivemenu);
    $responsivemenu['account']['#weight'] = 99;
    $responsivemenu['logout']['#weight'] = 100;

    // Bind the responsive menu the the content varable so it can be rendered.
    $content['responsive-menu'] = $responsivemenu;

    // Create the responsive shortucts.
    if (module_exists('shortcut')) {
      $content['responsive']['shortcuts'] = array(
        '#prefix' => '<div id="admin-menu-shortcuts-responsive">',
        '#suffix' => '</div>',
        '#pre_render' => array('shortcut_toolbar_pre_render'),
      );
    }
  }
}

/**
 * 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 frontend and backend classes to the body.
  if (path_is_admin(current_path())) {
    $vars['classes_array'][] = 'adminimal-backend';
  }
  else {
    $vars['classes_array'][] = 'adminimal-frontend';
  }

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

  _adminimal_admin_menu_viewport();
}

/**
 * Add viewport for scaling on devices.
 */
function _adminimal_admin_menu_viewport() {
  $use_slicknav = variable_get('adminimal_admin_menu_slicknav', TRUE);
  $use_viewport = variable_get('adminimal_admin_menu_viewport', TRUE);

  // Only needed for slicknav.
  if ($use_slicknav && $use_viewport) {
    $viewport = array(
      '#tag' => 'meta',
      '#attributes' => array(
        'name' => 'viewport',
        'content' => 'width=device-width, initial-scale=1',
      ),
    );
    drupal_add_html_head($viewport, 'viewport');
  }
}

/**
 * Check if the user has access to use the admin menu.
 *
 * @return bool
 *   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;
}
