<?php
define('SHORTCUT_DEFAULT_SET_NAME', 'shortcut-set-1');
function shortcut_help($path, $arg) {
global $user;
switch ($path) {
case 'admin/help#shortcut':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Shortcut module allows users to create sets of <em>shortcut</em> links to commonly-visited pages of the site. Shortcuts are contained within <em>sets</em>. Each user with <em>Select any shortcut set</em> permission can select a shortcut set created by anyone at the site. For more information, see the online handbook entry for <a href="@shortcut">Shortcut module</a>.', array(
'@shortcut' => 'http://drupal.org/documentation/modules/shortcut/',
)) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl><dt>' . t('Administering shortcuts') . '</dt>';
$output .= '<dd>' . t('Users with the <em>Administer shortcuts</em> permission can manage shortcut sets and edit the shortcuts within sets from the <a href="@shortcuts">Shortcuts administration page</a>.', array(
'@shortcuts' => url('admin/config/user-interface/shortcut'),
)) . '</dd>';
$output .= '<dt>' . t('Choosing shortcut sets') . '</dt>';
$output .= '<dd>' . t('Users with permission to switch shortcut sets can choose a shortcut set to use from the Shortcuts tab of their user account page.') . '</dd>';
$output .= '<dt>' . t('Adding and removing shortcuts') . '</dt>';
$output .= '<dd>' . t('The Shortcut module creates an add/remove link for each page on your site; the link lets you add or remove the current page from the currently-enabled set of shortcuts (if your theme displays it and you have permission to edit your shortcut set). The core Seven administration theme displays this link next to the page title, as a small + or - sign. If you click on the + sign, you will add that page to your preferred set of shortcuts. If the page is already part of your shortcut set, the link will be a - sign, and will allow you to remove the current page from your shortcut set.') . '</dd>';
$output .= '<dt>' . t('Displaying shortcuts') . '</dt>';
$output .= '<dd>' . t('You can display your shortcuts by enabling the Shortcuts block on the <a href="@blocks">Blocks administration page</a>. Certain administrative modules also display your shortcuts; for example, the core <a href="@toolbar-help">Toolbar module</a> displays them near the top of the page, along with an <em>Edit shortcuts</em> link.', array(
'@blocks' => url('admin/structure/block'),
'@toolbar-help' => url('admin/help/toolbar'),
)) . '</dd>';
$output .= '</dl>';
return $output;
case 'admin/config/user-interface/shortcut':
case 'admin/config/user-interface/shortcut/%':
if (user_access('switch shortcut sets')) {
$output = '<p>' . t('Define which shortcut set you are using on the <a href="@shortcut-link">Shortcuts tab</a> of your account page.', array(
'@shortcut-link' => url("user/{$user->uid}/shortcuts"),
)) . '</p>';
return $output;
}
}
}
function shortcut_permission() {
return array(
'administer shortcuts' => array(
'title' => t('Administer shortcuts'),
),
'customize shortcut links' => array(
'title' => t('Edit current shortcut set'),
'description' => t('Editing the current shortcut set will affect other users if that set has been assigned to or selected by other users. Granting "Select any shortcut set" permission along with this permission will grant permission to edit any shortcut set.'),
),
'switch shortcut sets' => array(
'title' => t('Select any shortcut set'),
'description' => t('From all shortcut sets, select one to be own active set. Without this permission, an administrator selects shortcut sets for users.'),
),
);
}
function shortcut_menu() {
$items['admin/config/user-interface/shortcut'] = array(
'title' => 'Shortcuts',
'description' => 'Add and modify shortcut sets.',
'page callback' => 'shortcut_set_admin',
'access arguments' => array(
'administer shortcuts',
),
'file' => 'shortcut.admin.inc',
);
$items['admin/config/user-interface/shortcut/add-set'] = array(
'title' => 'Add shortcut set',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'shortcut_set_add_form',
),
'access arguments' => array(
'administer shortcuts',
),
'type' => MENU_LOCAL_ACTION,
'file' => 'shortcut.admin.inc',
);
$items['admin/config/user-interface/shortcut/%shortcut_set'] = array(
'title' => 'Edit shortcuts',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'shortcut_set_customize',
4,
),
'title callback' => 'shortcut_set_title_callback',
'title arguments' => array(
4,
),
'access callback' => 'shortcut_set_edit_access',
'access arguments' => array(
4,
),
'file' => 'shortcut.admin.inc',
);
$items['admin/config/user-interface/shortcut/%shortcut_set/links'] = array(
'title' => 'List links',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/config/user-interface/shortcut/%shortcut_set/edit'] = array(
'title' => 'Edit set name',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'shortcut_set_edit_form',
4,
),
'access callback' => 'shortcut_set_edit_access',
'access arguments' => array(
4,
),
'type' => MENU_LOCAL_TASK,
'file' => 'shortcut.admin.inc',
'weight' => 10,
);
$items['admin/config/user-interface/shortcut/%shortcut_set/delete'] = array(
'title' => 'Delete shortcut set',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'shortcut_set_delete_form',
4,
),
'access callback' => 'shortcut_set_delete_access',
'access arguments' => array(
4,
),
'file' => 'shortcut.admin.inc',
);
$items['admin/config/user-interface/shortcut/%shortcut_set/add-link'] = array(
'title' => 'Add shortcut',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'shortcut_link_add',
4,
),
'access callback' => 'shortcut_set_edit_access',
'access arguments' => array(
4,
),
'type' => MENU_LOCAL_ACTION,
'file' => 'shortcut.admin.inc',
);
$items['admin/config/user-interface/shortcut/%shortcut_set/add-link-inline'] = array(
'title' => 'Add shortcut',
'page callback' => 'shortcut_link_add_inline',
'page arguments' => array(
4,
),
'access callback' => 'shortcut_set_edit_access',
'access arguments' => array(
4,
),
'type' => MENU_CALLBACK,
'file' => 'shortcut.admin.inc',
);
$items['admin/config/user-interface/shortcut/link/%menu_link'] = array(
'title' => 'Edit shortcut',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'shortcut_link_edit',
5,
),
'access callback' => 'shortcut_link_access',
'access arguments' => array(
5,
),
'file' => 'shortcut.admin.inc',
);
$items['admin/config/user-interface/shortcut/link/%menu_link/delete'] = array(
'title' => 'Delete shortcut',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'shortcut_link_delete',
5,
),
'access callback' => 'shortcut_link_access',
'access arguments' => array(
5,
),
'file' => 'shortcut.admin.inc',
);
$items['user/%user/shortcuts'] = array(
'title' => 'Shortcuts',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'shortcut_set_switch',
1,
),
'access callback' => 'shortcut_set_switch_access',
'access arguments' => array(
1,
),
'type' => MENU_LOCAL_TASK,
'file' => 'shortcut.admin.inc',
);
return $items;
}
function shortcut_admin_paths() {
$paths = array(
'user/*/shortcuts' => TRUE,
);
return $paths;
}
function shortcut_theme() {
return array(
'shortcut_set_customize' => array(
'render element' => 'form',
'file' => 'shortcut.admin.inc',
),
);
}
function shortcut_block_info() {
$blocks['shortcuts']['info'] = t('Shortcuts');
$blocks['shortcuts']['cache'] = DRUPAL_NO_CACHE;
return $blocks;
}
function shortcut_block_view($delta = '') {
if ($delta == 'shortcuts') {
$shortcut_set = shortcut_current_displayed_set();
$data['subject'] = t('@shortcut_set shortcuts', array(
'@shortcut_set' => $shortcut_set->title,
));
$data['content'] = shortcut_renderable_links($shortcut_set);
return $data;
}
}
function shortcut_set_edit_access($shortcut_set = NULL) {
if (user_access('administer shortcuts')) {
return TRUE;
}
if (user_access('customize shortcut links')) {
return !isset($shortcut_set) || $shortcut_set == shortcut_current_displayed_set();
}
return FALSE;
}
function shortcut_set_delete_access($shortcut_set) {
if (!user_access('administer shortcuts')) {
return FALSE;
}
if ($shortcut_set->set_name == SHORTCUT_DEFAULT_SET_NAME) {
return FALSE;
}
return TRUE;
}
function shortcut_set_switch_access($account = NULL) {
global $user;
if (user_access('administer shortcuts')) {
return TRUE;
}
if (!user_access('switch shortcut sets')) {
return FALSE;
}
if (!isset($account) || $user->uid == $account->uid) {
return TRUE;
}
return FALSE;
}
function shortcut_link_access($menu_link) {
if ($shortcut_set = shortcut_set_load($menu_link['menu_name'])) {
return shortcut_set_edit_access($shortcut_set);
}
return FALSE;
}
function shortcut_set_load($set_name) {
$set = db_select('shortcut_set', 'ss')
->fields('ss')
->condition('set_name', $set_name)
->execute()
->fetchObject();
if (!$set) {
return FALSE;
}
$set->links = menu_load_links($set_name);
return $set;
}
function shortcut_set_save(&$shortcut_set) {
if (isset($shortcut_set->set_name)) {
$return = drupal_write_record('shortcut_set', $shortcut_set, 'set_name');
}
else {
$shortcut_set->set_name = shortcut_set_get_unique_name();
$return = drupal_write_record('shortcut_set', $shortcut_set);
}
if (isset($shortcut_set->links)) {
foreach ($shortcut_set->links as &$link) {
$link['menu_name'] = $shortcut_set->set_name;
$link['plid'] = 0;
menu_link_save($link);
}
if (empty($return)) {
$return = SAVED_UPDATED;
}
}
return $return;
}
function shortcut_set_delete($shortcut_set) {
if ($shortcut_set->set_name == SHORTCUT_DEFAULT_SET_NAME) {
return FALSE;
}
db_delete('shortcut_set_users')
->condition('set_name', $shortcut_set->set_name)
->execute();
menu_delete_links($shortcut_set->set_name);
$deleted = db_delete('shortcut_set')
->condition('set_name', $shortcut_set->set_name)
->execute();
return (bool) $deleted;
}
function shortcut_set_reset_link_weights(&$shortcut_set) {
$weight = -50;
foreach ($shortcut_set->links as &$link) {
$link['weight'] = $weight;
$weight++;
}
}
function shortcut_set_assign_user($shortcut_set, $account) {
db_merge('shortcut_set_users')
->key(array(
'uid' => $account->uid,
))
->fields(array(
'set_name' => $shortcut_set->set_name,
))
->execute();
drupal_static_reset('shortcut_current_displayed_set');
}
function shortcut_set_unassign_user($account) {
$deleted = db_delete('shortcut_set_users')
->condition('uid', $account->uid)
->execute();
return (bool) $deleted;
}
function shortcut_current_displayed_set($account = NULL) {
$shortcut_sets =& drupal_static(__FUNCTION__, array());
global $user;
if (!isset($account)) {
$account = $user;
}
if (isset($shortcut_sets[$account->uid])) {
return $shortcut_sets[$account->uid];
}
$query = db_select('shortcut_set', 's');
$query
->addField('s', 'set_name');
$query
->join('shortcut_set_users', 'u', 's.set_name = u.set_name');
$query
->condition('u.uid', $account->uid);
$shortcut_set_name = $query
->execute()
->fetchField();
if ($shortcut_set_name) {
$shortcut_set = shortcut_set_load($shortcut_set_name);
}
else {
$shortcut_set = shortcut_default_set($account);
}
$shortcut_sets[$account->uid] = $shortcut_set;
return $shortcut_set;
}
function shortcut_default_set($account = NULL) {
global $user;
if (!isset($account)) {
$account = $user;
}
$suggestions = array_reverse(module_invoke_all('shortcut_default_set', $account));
$suggestions[] = SHORTCUT_DEFAULT_SET_NAME;
foreach ($suggestions as $name) {
if ($shortcut_set = shortcut_set_load($name)) {
break;
}
}
return $shortcut_set;
}
function shortcut_set_get_unique_name() {
$number = db_query("SELECT COUNT(*) FROM {shortcut_set}")
->fetchField() + 1;
do {
$name = shortcut_set_name($number);
$number++;
} while ($shortcut_set = shortcut_set_load($name));
return $name;
}
function shortcut_set_name($number) {
return "shortcut-set-{$number}";
}
function shortcut_sets() {
return db_select('shortcut_set', 'ss')
->fields('ss')
->execute()
->fetchAllAssoc('set_name');
}
function shortcut_set_title_exists($title) {
return (bool) db_query_range('SELECT 1 FROM {shortcut_set} WHERE title = :title', 0, 1, array(
':title' => $title,
))
->fetchField();
}
function shortcut_valid_link($path) {
$normal_path = drupal_get_normal_path($path);
if ($path != $normal_path) {
$path = $normal_path;
}
return !url_is_external($path) && menu_get_item($path) || empty($path) || $path == '<front>';
}
function shortcut_renderable_links($shortcut_set = NULL) {
if (!isset($shortcut_set)) {
$shortcut_set = shortcut_current_displayed_set();
}
return menu_tree($shortcut_set->set_name);
}
function shortcut_preprocess_page(&$variables) {
if (shortcut_set_edit_access() && ($item = menu_get_item()) && $item['access']) {
$link = $_GET['q'];
$query_parameters = drupal_get_query_parameters();
if (!empty($query_parameters)) {
$link .= '?' . drupal_http_build_query($query_parameters);
}
$query = array(
'link' => $link,
'name' => drupal_get_title(),
);
$query += drupal_get_destination();
$shortcut_set = shortcut_current_displayed_set();
foreach ($shortcut_set->links as $shortcut) {
if ($link == $shortcut['link_path']) {
$mlid = $shortcut['mlid'];
break;
}
}
$link_mode = isset($mlid) ? "remove" : "add";
if ($link_mode == "add") {
$query['token'] = drupal_get_token('shortcut-add-link');
$link_text = shortcut_set_switch_access() ? t('Add to %shortcut_set shortcuts', array(
'%shortcut_set' => $shortcut_set->title,
)) : t('Add to shortcuts');
$link_path = 'admin/config/user-interface/shortcut/' . $shortcut_set->set_name . '/add-link-inline';
}
else {
$query['mlid'] = $mlid;
$link_text = shortcut_set_switch_access() ? t('Remove from %shortcut_set shortcuts', array(
'%shortcut_set' => $shortcut_set->title,
)) : t('Remove from shortcuts');
$link_path = 'admin/config/user-interface/shortcut/link/' . $mlid . '/delete';
}
if (theme_get_setting('shortcut_module_link')) {
$variables['title_suffix']['add_or_remove_shortcut'] = array(
'#attached' => array(
'css' => array(
drupal_get_path('module', 'shortcut') . '/shortcut.css',
),
),
'#prefix' => '<div class="add-or-remove-shortcuts ' . $link_mode . '-shortcut">',
'#type' => 'link',
'#title' => '<span class="icon"></span><span class="text">' . $link_text . '</span>',
'#href' => $link_path,
'#options' => array(
'query' => $query,
'html' => TRUE,
),
'#suffix' => '</div>',
);
}
}
}
function shortcut_page_alter(&$page) {
if (isset($page['page_top']['toolbar'])) {
$page['page_top']['toolbar']['#pre_render'][] = 'shortcut_toolbar_pre_render';
}
}
function shortcut_toolbar_pre_render($toolbar) {
$links = shortcut_renderable_links();
$links['#attached'] = array(
'css' => array(
drupal_get_path('module', 'shortcut') . '/shortcut.css',
),
);
$links['#prefix'] = '<div class="toolbar-shortcuts">';
$links['#suffix'] = '</div>';
$shortcut_set = shortcut_current_displayed_set();
$configure_link = NULL;
if (shortcut_set_edit_access($shortcut_set)) {
$configure_link = array(
'#type' => 'link',
'#title' => t('Edit shortcuts'),
'#href' => 'admin/config/user-interface/shortcut/' . $shortcut_set->set_name,
'#options' => array(
'attributes' => array(
'id' => 'edit-shortcuts',
),
),
);
}
$drawer = array(
'shortcuts' => $links,
'configure' => $configure_link,
);
$toolbar['toolbar_drawer'][] = $drawer;
return $toolbar;
}
function shortcut_set_title($shortcut_set) {
return check_plain($shortcut_set->title);
}
function shortcut_set_title_callback($shortcut_set) {
return $shortcut_set->title;
}