- <?php
- * @file
- * Render an administrative bar as a dropdown menu at the top of the window.
- */
-
- * Implements hook_menu().
- */
- function admin_bar_menu() {
-
- $items['js/admin_bar/cache'] = array(
- 'page callback' => 'admin_bar_js_cache',
- 'delivery callback' => 'admin_bar_deliver',
- 'access arguments' => array('access administration bar'),
- 'type' => MENU_CALLBACK,
- );
-
- $items['admin/config/administration/admin-bar'] = array(
- 'title' => 'Administration bar',
- 'description' => 'Adjust administration bar settings.',
- 'page callback' => 'backdrop_get_form',
- 'page arguments' => array('admin_bar_theme_settings'),
- 'access arguments' => array('administer site configuration'),
- 'file' => 'admin_bar.inc',
- );
-
- $items['admin_bar/flush-cache'] = array(
- 'page callback' => 'admin_bar_flush_cache',
- 'access arguments' => array('flush caches'),
- 'type' => MENU_CALLBACK,
- 'file' => 'admin_bar.inc',
- );
- return $items;
- }
-
- * Implements hook_permission().
- */
- function admin_bar_permission() {
- return array(
- 'access administration bar' => array(
- 'title' => t('Access administration bar'),
- 'description' => t('Display the administration bar at the top of each page.'),
- ),
- );
- }
-
- * Implements hook_theme().
- */
- function admin_bar_theme() {
- $base = array(
- 'file' => 'admin_bar.theme.inc',
- );
-
- return array(
- 'admin_bar_links' => array(
- 'render element' => 'elements',
- ) + $base,
- 'admin_bar_icon' => array(
- 'variables' => array('src' => NULL, 'alt' => NULL),
- ) + $base,
- 'admin_bar_icon_page' => array(
- 'variables' => array('src' => NULL, 'alt' => NULL),
- ) + $base,
- 'admin_bar_icon_locale' => array(
- 'variables' => array('src' => NULL, 'alt' => NULL),
- ) + $base,
- );
- }
-
- * Implements hook_config_info().
- */
- function admin_bar_config_info() {
- $prefixes['admin_bar.settings'] = array(
- 'label' => t('Admin bar settings'),
- 'group' => t('Configuration'),
- );
- return $prefixes;
- }
-
- * Implements hook_menu_alter().
- */
- function admin_bar_menu_alter(&$items) {
-
- cache('admin_bar')->flush();
- }
-
- * Implements hook_menu_link_insert().
- */
- function admin_bar_menu_link_insert($link) {
-
- if ($link['menu_name'] == 'management') {
- cache('admin_bar')->flush();
- }
- }
-
- * Implements hook_menu_link_update().
- */
- function admin_bar_menu_link_update($link) {
-
- if ($link['menu_name'] == 'management') {
- cache('admin_bar')->flush();
- }
- }
-
- * Implements hook_menu_link_delete().
- */
- function admin_bar_menu_link_delete($link) {
-
- if ($link['menu_name'] == 'management') {
- cache('admin_bar')->flush();
- }
- }
-
- * Implements hook_language_insert().
- */
- function admin_bar_language_insert() {
- cache('admin_bar')->flush();
- }
-
- * Implements hook_language_update().
- */
- function admin_bar_language_update() {
- cache('admin_bar')->flush();
- }
-
- * Implements hook_language_delete().
- */
- function admin_bar_language_delete() {
- cache('admin_bar')->flush();
- }
-
- * Implements hook_field_create_instance().
- */
- function admin_bar_field_create_instance($instance) {
- cache('admin_bar')->flush();
- }
-
- * Implements hook_field_update_instance().
- */
- function admin_bar_field_update_instance($instance, $prior_instance) {
- cache('admin_bar')->flush();
- }
-
- * Implements hook_field_delete_instance().
- */
- function admin_bar_field_delete_instance($instance) {
- cache('admin_bar')->flush();
- }
-
- * Implements hook_taxonomy_vocabulary_insert().
- */
- function admin_bar_taxonomy_vocabulary_insert(TaxonomyVocabulary $vocabulary) {
- cache('admin_bar')->flush();
- }
-
- * Implements hook_taxonomy_vocabulary_update().
- */
- function admin_bar_taxonomy_vocabulary_update(TaxonomyVocabulary $vocabulary) {
- cache('admin_bar')->flush();
- }
-
- * Implements hook_taxonomy_vocabulary_delete().
- */
- function admin_bar_taxonomy_vocabulary_delete(TaxonomyVocabulary $vocabulary) {
- cache('admin_bar')->flush();
- }
-
- * Implements hook_preprocess_page().
- */
- function admin_bar_preprocess_page(&$variables) {
- if (!user_access('access administration bar') || admin_bar_suppress(FALSE)) {
- return;
- }
-
-
- if (!backdrop_is_html()) {
- return;
- }
-
-
-
- if (strpos($_GET['q'], 'admin/reports/status') === 0) {
- cache('admin_bar')->flush();
- }
-
- global $user, $language;
-
-
- $args = explode('/', $_GET['q']);
- if ($args[0] == 'admin' && !empty($args[1])) {
- $settings['activeTrail'] = url($args[0] . '/' . $args[1]);
- }
- elseif (backdrop_is_front_page()) {
- $settings['activeTrail'] = url('<front>');
- }
-
-
- $settings['destination'] = backdrop_http_build_query(backdrop_get_destination());
-
-
- $complete = FALSE;
-
-
-
- $cid = 'admin_bar:' . $user->uid . ':' . session_id() . ':' . $language->langcode;
- if (!$complete && ($hash = admin_bar_cache_get($cid))) {
- $settings['hash'] = $hash;
-
-
-
-
-
-
-
-
- $settings['basePath'] = url('admin_bar');
- }
-
- else {
- module_load_include('inc', 'admin_bar');
- $variables['page_bottom'] .= admin_bar_output($complete);
- }
-
- $replacements = module_invoke_all('admin_bar_replacements', $complete);
- if (!empty($replacements)) {
- $settings['replacements'] = $replacements;
- }
-
-
- $config = config('admin_bar.settings');
- if ($setting = $config->get('margin_top')) {
- $settings['margin_top'] = $setting;
- }
- if ($setting = $config->get('position_fixed')) {
- $settings['position_fixed'] = $setting;
- }
-
-
- $settings['current_path_is_admin'] = FALSE;
- $current_path = current_path();
- if (path_is_admin($current_path)) {
- $settings['current_path_is_admin'] = TRUE;
- }
-
- $components = $config->get('components');
- if (in_array('admin_bar.back_to_site_link', $components)) {
- $settings['back_to_site_link'] = TRUE;
- }
- backdrop_add_js(array('admin_bar' => $settings), 'setting');
- backdrop_add_library('admin_bar', 'admin_bar', TRUE);
- }
-
- * Implements hook_library_info().
- */
- function admin_bar_library_info() {
- $path = backdrop_get_path('module', 'admin_bar');
- $libraries['admin_bar'] = array(
- 'title' => 'Admin Bar',
- 'version' => BACKDROP_VERSION,
- 'js' => array(
- $path . '/js/admin_bar.js' => array('group' => JS_DEFAULT),
- ),
- 'css' => array(
- $path . '/css/admin_bar.css' => array(
- 'type' => 'file',
- 'media' => 'screen',
- ),
- $path . '/css/admin_bar-print.css' => array(
- 'media' => 'print',
- ),
- ),
- 'icons' => array(
- 'arrow-left' => array('immutable' => TRUE),
- 'arrow-right' => array('immutable' => TRUE),
- 'caret-circle-left-fill' => array('immutable' => TRUE),
- 'caret-circle-right-fill' => array('immutable' => TRUE),
- 'magnifying-glass' => array('immutable' => TRUE),
- ),
- );
- return $libraries;
- }
-
- * Suppress display of administration bar.
- *
- * This function should be called from within another module's page callback
- * (preferably using module_invoke()) when the menu should not be displayed.
- * This is useful for modules that implement popup pages or other special
- * pages where the menu would be distracting or break the layout.
- *
- * @param $set
- * Defaults to TRUE. If called before hook_footer(), the menu will not be
- * displayed. If FALSE is passed, the suppression state is returned.
- */
- function admin_bar_suppress($set = TRUE) {
- static $suppress = FALSE;
-
- if (!empty($set) && $suppress === FALSE) {
- $suppress = TRUE;
- backdrop_add_js(array('admin_bar' => array('suppress' => 1)), 'setting');
- }
- return $suppress;
- }
-
- * Implements hook_js().
- */
- function admin_bar_js() {
- return array(
- 'cache' => array(
- 'callback' => 'admin_bar_js_cache',
- 'includes' => array('common', 'theme', 'unicode'),
- 'dependencies' => array('filter', 'user'),
- ),
- );
- }
-
- * Retrieve a client-side cache hash from cache.
- *
- * The hash cache is consulted more than once per request; we therefore cache
- * the results statically to avoid multiple database requests.
- *
- * This should only be used for client-side cache hashes. Use cache_menu for
- * administration bar content.
- *
- * @param $cid
- * The cache ID of the data to retrieve.
- */
- function admin_bar_cache_get($cid) {
- $cache = &backdrop_static(__FUNCTION__, array());
-
- if (!array_key_exists($cid, $cache)) {
- $cache[$cid] = cache('admin_bar')->get($cid);
- if ($cache[$cid] && isset($cache[$cid]->data)) {
- $cache[$cid] = $cache[$cid]->data;
- }
- }
-
- return $cache[$cid];
- }
-
- * Store a client-side cache hash in persistent cache.
- *
- * This should only be used for client-side cache hashes. Use cache_menu for
- * administration bar content.
- *
- * @param $cid
- * The cache ID of the data to retrieve.
- */
- function admin_bar_cache_set($cid, $data) {
- cache('admin_bar')->set($cid, $data);
- }
-
- * Menu callback; Output administration bar for HTTP caching via AJAX request.
- *
- * @see admin_bar_deliver()
- */
- function admin_bar_js_cache() {
- global $conf;
-
-
- $conf['cache'] = 1;
- backdrop_page_is_cacheable(TRUE);
-
-
-
- $cache = backdrop_page_get_cache();
- if (is_object($cache)) {
- backdrop_add_http_header('X-Backdrop-Cache', 'HIT');
- backdrop_serve_page_from_cache($cache);
- exit;
- }
-
-
- backdrop_add_http_header('X-Backdrop-Cache', 'MISS');
-
-
-
-
-
- $max_age = 3600 * 24 * 365;
- backdrop_add_http_header('Expires', gmdate(DATE_RFC1123, REQUEST_TIME + $max_age));
- backdrop_add_http_header('Cache-Control', 'private, max-age=' . $max_age);
-
-
- module_load_include('inc', 'admin_bar');
- return admin_bar_output();
- }
-
- * Delivery callback for client-side HTTP caching.
- *
- * @see admin_bar_js_cache()
- */
- function admin_bar_deliver($page_callback_result) {
- backdrop_add_http_header('Content-Type', 'text/html; charset=utf-8');
-
-
- global $language;
- backdrop_add_http_header('Content-Language', $language->langcode);
-
-
-
-
- print $page_callback_result;
-
-
- backdrop_page_footer();
- }
-
- * Implements hook_admin_bar_replacements().
- *
- * @see admin_bar_links_page()
- */
- function admin_bar_admin_bar_replacements($complete) {
- $items = array();
-
-
-
- if (!$complete) {
-
- $components = config_get('admin_bar.settings', 'components');
- if (in_array('admin_bar.users', $components) && ($user_count = admin_bar_get_user_count())) {
-
- $items['.admin-bar-users .user-counter-value'] = $user_count;
- }
-
- if (in_array('admin_bar.page', $components)) {
- $query = array('query' => array(
- 'destination' => current_path(),
- ));
-
- if (user_access('administer layouts')) {
- $layout = layout_get_layout_by_path();
- $items['#admin-bar-page-layout'] = l(t('Layout (@layout)', array('@layout' => $layout->title)), 'admin/structure/layouts/manage/' . $layout->name, $query);
- $items['#admin-bar-page-layout'] .= theme('admin_bar_links', array('elements' => array(
- '#level' => 2,
- array(
- '#title' => t('Configure'),
- '#href' => 'admin/structure/layouts/manage/' . $layout->name . '/configure',
- '#options' => $query,
- ),
- )));
- }
- if (user_access('administer themes')) {
- global $theme;
- $theme_data = list_themes();
- $theme_object = $theme_data[$theme];
- $items['#admin-bar-page-theme'] = l(t('Theme (@theme)', array('@theme' => $theme_object->info['name'])), 'admin/appearance/settings/' . $theme_object->name, $query);
- }
- }
-
- if (in_array('admin_bar.locale', $components)) {
- if (module_exists('locale') && language_multilingual()) {
- if ($locale_links = admin_bar_locale_links()) {
-
-
- $locale_links['#level'] = 0;
- $theme_locale_links = theme('admin_bar_links', array(
- 'elements' => $locale_links,
- ));
- $items['.admin-bar-locale-links>ul.dropdown'] = strip_tags($theme_locale_links, '<li><a><span>');
- }
- }
- }
- }
-
- return $items;
- }
-
- * Build Locale links.
- *
- */
- function admin_bar_locale_links() {
- if (!module_exists('locale') || !language_multilingual()) {
- return array();
- }
-
- global $language;
- $language_default = $language;
-
- $path = backdrop_is_front_page() ? '<front>' : $_GET['q'];
- $language_links = language_negotiation_get_switch_links(LANGUAGE_TYPE_INTERFACE, $path);
-
- if (!isset($language_links->links)) {
- return array();
- }
-
- foreach ($language_links->links as $language_link) {
- if ($language_link['language']->langcode == $language_default->langcode) {
- continue;
- }
-
- if (!empty($language_link['href'])) {
- $links[$language_link['language']->langcode] = array(
- '#attributes' => array('class' => array('locale')),
- '#title' => isset($language_link['language']->native) ? $language_link['language']->native : $language_link['language']->name,
- '#href' => url($language_link['href'], array(
- 'language' => $language_link['language'],
- 'absolute' => TRUE,
- )),
- '#below' => array(),
- '#original_link' => array(),
- );
- }
- else {
- $links[$language_link['language']->langcode] = array(
- '#attributes' => array('class' => array('locale')),
- '#title' => isset($language_link['language']->native) ? $language_link['language']->native : $language_link['language']->name,
- '#type' => 'markup',
- '#prefix' => '<span>',
- '#markup' => (isset($language_link['language']->native) ? $language_link['language']->native : $language_link['language']->name) . ' (' . t('unavailable') . ')',
- '#suffix' => '</span>',
- );
- }
- }
-
- return $links;
- }
-
- * Return count of online authenticated users.
- *
- * @see user_block(), user.module
- */
- function admin_bar_get_user_count() {
- $interval = REQUEST_TIME - 900;
- $count_auth = admin_bar_session_count($interval, FALSE);
-
- return $count_auth;
- }
-
- * Counts how many users are active on the site.
- *
- * Counts how many users have sessions which have been active since the
- * specified time. Can count either anonymous sessions or authenticated
- * sessions.
- *
- * @param $timestamp
- * A Unix timestamp. Users who have been active since this time will be
- * counted. The default is 0, which counts all existing sessions.
- * @param $anonymous
- * TRUE counts only anonymous users. FALSE counts only authenticated users.
- *
- * @return
- * The number of users with sessions.
- *
- * @todo There are mostly no anonymous sessions anymore. Split this into a
- * separate module providing proper user statistics.
- */
- function admin_bar_session_count($timestamp = 0, $anonymous = TRUE) {
- $query = db_select('sessions');
- $query->addExpression('COUNT(sid)', 'count');
- $query->condition('timestamp', $timestamp, '>=');
- $query->condition('uid', 0, $anonymous ? '=' : '>');
- return $query->execute()->fetchField();
- }
-
- * Implements hook_admin_bar_output_build().
- */
- function admin_bar_admin_bar_output_build(&$content) {
- if (!isset($content['menu']['menu'])) {
- return;
- }
-
-
-
- if (isset($content['menu']['menu']['admin/config'])) {
- foreach (element_children($content['menu']['menu']['admin/config']) as $key) {
- $content['menu']['menu']['admin/config'][$key]['#weight_original'] = $content['menu']['menu']['admin/config'][$key]['#weight'];
- unset($content['menu']['menu']['admin/config'][$key]['#weight']);
- }
- }
-
-
- if (isset($content['menu']['menu']['node/add']) && isset($content['menu']['menu']['admin/content'])) {
- $content['menu']['menu']['admin/content']['node/add'] = $content['menu']['menu']['node/add'];
- unset($content['menu']['menu']['node/add']);
- }
-
- else {
- $link = db_query("SELECT * FROM {menu_links} WHERE router_path = 'node/add' AND module = 'system'")->fetchAssoc();
- $conditions = array();
- for ($i = 1; $i < MENU_MAX_DEPTH; $i++) {
- if (!empty($link["p$i"])) {
- $conditions["p$i"] = $link["p$i"];
- }
- }
- $tree = menu_build_tree($link['menu_name'], array(
- 'conditions' => $conditions,
- 'min_depth' => $link['depth'],
- ));
- $links = admin_bar_links_menu($tree);
- if (!empty($links)) {
- $key = key($links);
- $links[$key]['#weight'] = -100;
-
-
-
- if (isset($content['menu']['menu']['admin/content'])) {
- $content['menu']['menu']['admin/content'] += $links;
- }
-
- else {
- $content['menu']['menu'] += $links;
- }
- }
- }
-
- if (isset($content['menu']['menu']['file/add']) && isset($content['menu']['menu']['admin/content'])) {
- $content['menu']['menu']['admin/content']['file/add'] = $content['menu']['menu']['file/add'];
- unset($content['menu']['menu']['file/add']);
- }
- }
-
- * Implements hook_admin_bar_output_alter().
- */
- function admin_bar_admin_bar_output_alter(&$content) {
- if (isset($content['menu']['menu'])) {
- foreach ($content['menu']['menu'] as $key => $link) {
-
- if ($key == 'admin/tasks' || $key == 'admin/index') {
- unset($content['menu']['menu'][$key]);
- }
- }
- }
- }
-
- * Implements hook_translated_menu_link_alter().
- *
- * Here is where we make changes to links that need dynamic information such
- * as the current page path or the number of users.
- */
- function admin_bar_translated_menu_link_alter(&$item, $map) {
- if ($item['menu_name'] != 'admin_bar') {
- return;
- }
-
-
- if (!$item['access']) {
- $item['access'] = TRUE;
-
- if (!isset($item['localized_options'])) {
- _menu_item_localize($item, $map, TRUE);
- }
- }
-
-
- if (!$item['access']) {
- return;
- }
- }
-
- * Implements hook_flush_caches().
- *
- * Flushes client-side caches.
- */
- function admin_bar_flush_caches() {
-
- backdrop_static_reset('admin_bar_cache_get');
-
-
- return array('cache_admin_bar');
- }
-
- * Implements hook_form_alter().
- */
- function admin_bar_form_alter(&$form, &$form_state, $form_id) {
- $global_flush_ids = array(
- 'admin_bar_theme_settings',
-
- 'system_clean_url_settings',
-
- 'user_admin_permissions',
-
- 'user_admin_role_delete_confirm',
-
- 'user_profile_form',
- );
- if (in_array($form_id, $global_flush_ids)) {
- $form['#submit'][] = 'admin_bar_form_alter_flush_cache_submit';
-
-
- $form_state['admin_bar_uid'] = NULL;
- if ($form_id == 'user_profile_form') {
- $form_state['admin_bar_uid'] = $form_state['user']->uid;
- }
- }
-
-
-
-
- if ($form_id == 'user_admin_permissions') {
- $form['#attached']['js'][] = backdrop_get_path('module', 'admin_bar') . '/js/admin_bar.admin.js';
- }
- }
-
- * Form submission handler to flush Administration bar caches.
- */
- function admin_bar_form_alter_flush_cache_submit($form, &$form_state) {
- cache('admin_bar')->flush();
- }