diff options
author | Dries Buytaert <dries@buytaert.net> | 2010-02-25 15:58:54 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2010-02-25 15:58:54 +0000 |
commit | 7982be718e01eb167014567a1d8fca8a6d1c08ca (patch) | |
tree | 7040ca0c42040923420caa7af75738d3ba5e78f8 /modules/shortcut | |
parent | c9f1086327a39f7e69397fe28fcc2471a1e94a11 (diff) | |
download | brdo-7982be718e01eb167014567a1d8fca8a6d1c08ca.tar.gz brdo-7982be718e01eb167014567a1d8fca8a6d1c08ca.tar.bz2 |
- Patch #647084 by matt2000, jhodgdon, alpritt, David_Rothstein: fixed missing shortcut edit and delete operations on shortcuts admin.
Diffstat (limited to 'modules/shortcut')
-rw-r--r-- | modules/shortcut/shortcut.admin.css | 5 | ||||
-rw-r--r-- | modules/shortcut/shortcut.admin.inc | 325 | ||||
-rw-r--r-- | modules/shortcut/shortcut.module | 174 |
3 files changed, 406 insertions, 98 deletions
diff --git a/modules/shortcut/shortcut.admin.css b/modules/shortcut/shortcut.admin.css index f2ff5bed8..3670da01a 100644 --- a/modules/shortcut/shortcut.admin.css +++ b/modules/shortcut/shortcut.admin.css @@ -1,10 +1,5 @@ /* $Id$ */ -h4.shortcuts-set, -div.shortcuts-change-set { - display: inline; -} - .shortcut-slot-hidden { display: none; } diff --git a/modules/shortcut/shortcut.admin.inc b/modules/shortcut/shortcut.admin.inc index c2aeaab5d..f3f1500ba 100644 --- a/modules/shortcut/shortcut.admin.inc +++ b/modules/shortcut/shortcut.admin.inc @@ -19,7 +19,7 @@ function shortcut_max_slots() { } /** - * Menu callback; Build the form for switching shortcut sets. + * Form callback: builds the form for switching shortcut sets. * * @param $form * An associative array containing the structure of the form. @@ -28,6 +28,7 @@ function shortcut_max_slots() { * @param $account * (optional) The user account whose shortcuts will be switched. Defaults to * the current logged-in user. + * * @return * An array representing the form definition. * @@ -43,7 +44,7 @@ function shortcut_set_switch($form, &$form_state, $account = NULL) { // Prepare the list of shortcut sets. $sets = shortcut_sets(); $current_set = shortcut_current_displayed_set($account); - $default_set = shortcut_default_set($account); + $options = array(); foreach ($sets as $name => $set) { $options[$name] = check_plain($set->title); @@ -55,48 +56,61 @@ function shortcut_set_switch($form, &$form_state, $account = NULL) { $options['new'] = t('New set'); } - $form['account'] = array( - '#type' => 'value', - '#value' => $account, - ); + if (count($options) > 1) { + $form['account'] = array( + '#type' => 'value', + '#value' => $account, + ); - $form['set'] = array( - '#type' => 'radios', - '#title' => $user->uid == $account->uid ? t('Choose a set of shortcuts to use') : t('Choose a set of shortcuts for this user'), - '#options' => $options, - '#default_value' => $current_set->set_name, - ); + $form['set'] = array( + '#type' => 'radios', + '#title' => $user->uid == $account->uid ? t('Choose a set of shortcuts to use') : t('Choose a set of shortcuts for this user'), + '#options' => $options, + '#default_value' => $current_set->set_name, + ); - $form['new'] = array( - '#type' => 'textfield', - '#description' => t('The new set is created by copying items from the @default set.', array('@default' => $default_set->title)), - '#access' => $add_access, - ); + $form['new'] = array( + '#type' => 'textfield', + '#description' => t('The new set is created by copying items from your default shortcut set.'), + '#access' => $add_access, + ); - $form['#attached'] = array( - 'css' => array(drupal_get_path('module', 'shortcut') . '/shortcut.admin.css'), - 'js' => array(drupal_get_path('module', 'shortcut') . '/shortcut.admin.js'), - ); + if ($user->uid != $account->uid) { + $default_set = shortcut_default_set($account); + $form['new']['#description'] = t('The new set is created by copying items from the %default set.', array('%default' => $default_set->title)); + } - $form['actions'] = array('#type' => 'container', '#attributes' => array('class' => array('form-actions'))); - $form['actions']['submit'] = array( - '#type' => 'submit', - '#value' => t('Save configuration'), - ); + $form['#attached'] = array( + 'css' => array(drupal_get_path('module', 'shortcut') . '/shortcut.admin.css'), + 'js' => array(drupal_get_path('module', 'shortcut') . '/shortcut.admin.js'), + ); + + $form['actions'] = array('#type' => 'container', '#attributes' => array('class' => array('form-actions'))); + $form['actions']['submit'] = array( + '#type' => 'submit', + '#value' => t('Change set'), + ); + } + else { + // There is only 1 option, so output a message in the $form array. + $form['info'] = array( + '#markup' => '<p>' . t('You are currently using the %set-name shortcut set.', array('%set-name' => $current_set->title)) . '</p>', + ); + } return $form; } /** - * Submit handler for the form that switches shortcut sets. + * Submit handler for shortcut_set_switch(). */ function shortcut_set_switch_submit($form, &$form_state) { global $user; $account = $form_state['values']['account']; if ($form_state['values']['set'] == 'new') { - // Save a new shortcut set with links copied from the default set. - $default_set = shortcut_default_set(); + // Save a new shortcut set with links copied from the user's default set. + $default_set = shortcut_default_set($account); $set = (object) array( 'title' => $form_state['values']['new'], 'links' => menu_links_clone($default_set->links), @@ -105,9 +119,7 @@ function shortcut_set_switch_submit($form, &$form_state) { $replacements = array( '%user' => $account->name, '%set_name' => $set->title, - // This form can be displayed on more than one page, so make sure we link - // back to the correct one. - '@switch-url' => url($_GET['q']), + '@switch-url' => url(current_path()), ); if ($account->uid == $user->uid) { // Only administrators can create new shortcut sets, so we know they have @@ -134,7 +146,79 @@ function shortcut_set_switch_submit($form, &$form_state) { } /** - * Menu callback; Build the form for customizing shortcut sets. + * Menu page callback: builds the page for administering shortcut sets. + */ +function shortcut_set_admin() { + $shortcut_sets = shortcut_sets(); + $header = array(t('Name'), array('data' => t('Operations'), 'colspan' => 4)); + + $rows = array(); + foreach ($shortcut_sets as $set) { + $row = array( + check_plain($set->title), + l(t('list links'), "admin/config/user-interface/shortcut/$set->set_name"), + l(t('edit set name'), "admin/config/user-interface/shortcut/$set->set_name/edit"), + ); + if (shortcut_set_delete_access($set)) { + $row[] = l(t('delete set'), "admin/config/user-interface/shortcut/$set->set_name/delete"); + } + else { + $row[] = ''; + } + + $rows[] = $row; + } + + return theme('table', array('header' => $header, 'rows' => $rows)); +} + +/** + * Form callback: builds the form for adding a shortcut set. + * + * @param $form + * An associative array containing the structure of the form. + * @param $form_state + * An associative array containing the current state of the form. + * + * @return + * An array representing the form definition. + * + * @ingroup forms + * @see shortcut_set_add_form_submit() + */ +function shortcut_set_add_form($form, &$form_state) { + $form['new'] = array( + '#type' => 'textfield', + '#title' => t('Set name'), + '#description' => t('The new set is created by copying items from your default shortcut set.'), + ); + + $form['actions'] = array('#type' => 'container', '#attributes' => array('class' => array('form-actions'))); + $form['actions']['submit'] = array( + '#type' => 'submit', + '#value' => t('Create new set'), + ); + + return $form; +} + +/** + * Submit handler for shortcut_set_add_form(). + */ +function shortcut_set_add_form_submit($form, &$form_state) { + // Save a new shortcut set with links copied from the user's default set. + $default_set = shortcut_default_set(); + $set = (object) array( + 'title' => $form_state['values']['new'], + 'links' => menu_links_clone($default_set->links), + ); + shortcut_set_save($set); + drupal_set_message(t('The %set_name shortcut set has been created. You can edit it from this page.', array('%set_name' => $set->title))); + $form_state['redirect'] = 'admin/config/user-interface/shortcut/' . $set->set_name; +} + +/** + * Form callback: builds the form for customizing shortcut sets. * * @param $form * An associative array containing the structure of the form. @@ -142,6 +226,7 @@ function shortcut_set_switch_submit($form, &$form_state) { * An associative array containing the current state of the form. * @param $shortcut_set * An object representing the shortcut set which is being edited. + * * @return * An array representing the form definition. * @@ -149,25 +234,13 @@ function shortcut_set_switch_submit($form, &$form_state) { * @see shortcut_set_customize_submit() */ function shortcut_set_customize($form, &$form_state, $shortcut_set) { - $form['set'] = array( - '#markup' => t('Using set "@set"', array('@set' => $shortcut_set->title)), - '#prefix' => '<h4 class="shortcuts-set">', - '#suffix' => '</h4>', - '#weight' => -100, - ); - - $form['change_set'] = array( - '#type' => 'link', - '#title' => t('Change set'), - '#href' => 'admin/config/user-interface/shortcut', - '#prefix' => '<div class="shortcuts-change-set"> (', - '#suffix' => ')</div>', - '#weight' => -99, - '#access' => shortcut_set_switch_access(), + $form['shortcuts'] = array( + '#tree' => TRUE, + '#weight' => -20, + 'enabled' => array(), + 'disabled' => array(), ); - $form['shortcuts']['#tree'] = TRUE; - $form['shortcuts']['enabled'] = $form['shortcuts']['disabled'] = array(); foreach ($shortcut_set->links as $link) { $mlid = $link['mlid']; $status = $link['hidden'] ? 'disabled' : 'enabled'; @@ -206,7 +279,7 @@ function shortcut_set_customize($form, &$form_state, $shortcut_set) { } /** - * Submit handler for the shortcut set customization form. + * Submit handler for shortcut_set_customize(). */ function shortcut_set_customize_submit($form, &$form_state) { foreach ($form_state['values']['shortcuts'] as $group => $links) { @@ -221,11 +294,12 @@ function shortcut_set_customize_submit($form, &$form_state) { } /** - * Theme function for the shortcut set customization form. + * Themes the shortcut set customization form. * * @param $variables * An associative array containing: * - form: An array representing the form. + * * @return * A themed HTML string representing the content of the form. * @@ -247,6 +321,7 @@ function theme_shortcut_set_customize($variables) { )), 'class' => array('shortcut-status', 'shortcut-status-' . $status), ); + foreach (element_children($form['shortcuts'][$status]) as $key) { $shortcut = &$form['shortcuts'][$status][$key]; $row = array(); @@ -260,6 +335,7 @@ function theme_shortcut_set_customize($variables) { 'class' => array('draggable'), ); } + if ($status == 'enabled') { for ($i = 0; $i < shortcut_max_slots(); $i++) { $rows['empty-' . $i] = array( @@ -278,15 +354,16 @@ function theme_shortcut_set_customize($variables) { } } } + $header = array(t('Name'), t('Weight'), t('Status'), array('data' => t('Operations'), 'colspan' => 2)); $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'shortcuts'))); - $output .= drupal_render($form['submit']); + $output .= drupal_render($form['actions']); $output = drupal_render_children($form) . $output; return $output; } /** - * Menu callback; Build the form for adding a new shortcut link. + * Form callback: builds the form for adding a new shortcut link. * * @param $form * An associative array containing the structure of the form. @@ -294,6 +371,7 @@ function theme_shortcut_set_customize($variables) { * An associative array containing the current state of the form. * @param $shortcut_set * An object representing the shortcut set to which the link will be added. + * * @return * An array representing the form definition. * @@ -312,7 +390,7 @@ function shortcut_link_add($form, &$form_state, $shortcut_set) { } /** - * Menu callback; Build the form for editing a shortcut link. + * Form callback: builds the form for editing a shortcut link. * * @param $form * An associative array containing the structure of the form. @@ -320,6 +398,7 @@ function shortcut_link_add($form, &$form_state, $shortcut_set) { * An associative array containing the current state of the form. * @param $shortcut_link * An array representing the link that is being edited. + * * @return * An array representing the form definition. * @@ -343,6 +422,7 @@ function shortcut_link_edit($form, &$form_state, $shortcut_link) { * @param $shortcut_link * (optional) An array representing the shortcut link that will be edited. If * not provided, a new link will be created. + * * @return * An array of form elements. */ @@ -376,7 +456,8 @@ function _shortcut_link_form_elements($shortcut_link = NULL) { $form['#validate'][] = 'shortcut_link_edit_validate'; - $form['submit'] = array( + $form['actions'] = array('#type' => 'container', '#attributes' => array('class' => array('form-actions'))); + $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save'), ); @@ -394,7 +475,7 @@ function shortcut_link_edit_validate($form, &$form_state) { } /** - * Submit handler for the shortcut link editing form. + * Submit handler for shortcut_link_edit(). */ function shortcut_link_edit_submit($form, &$form_state) { $shortcut_link = array_merge($form_state['values']['original_shortcut_link'], $form_state['values']['shortcut_link']); @@ -404,7 +485,7 @@ function shortcut_link_edit_submit($form, &$form_state) { } /** - * Submit handler for the form that adds shortcut links. + * Submit handler for shortcut_link_add(). */ function shortcut_link_add_submit($form, &$form_state) { // Add the shortcut link to the set. @@ -418,7 +499,7 @@ function shortcut_link_add_submit($form, &$form_state) { } /** - * Add a link to the end of a shortcut set, keeping within a prescribed limit. + * Adds a link to the end of a shortcut set, keeping within a prescribed limit. * * @param $link * An array representing a shortcut link. @@ -455,7 +536,125 @@ function shortcut_admin_add_link($shortcut_link, &$shortcut_set, $limit = NULL) } /** - * Menu callback; Build the form for deleting a shortcut link. + * Form callback: builds the form for editing the shortcut set name. + * + * @param $form + * An associative array containing the structure of the form. + * @param $form_state + * An associative array containing the current state of the form. + * @param object $shortcut_set + * An object representing the shortcut set, as returned from + * shortcut_set_load(). + * + * @return + * An array representing the form definition. + * + * @ingroup forms + * @see shortcut_set_edit_form_submit() + */ +function shortcut_set_edit_form($form, &$form_state, $shortcut_set) { + $form['shortcut_set'] = array( + '#type' => 'value', + '#value' => $shortcut_set, + ); + $form['title'] = array( + '#type' => 'textfield', + '#title' => t('Set name'), + '#default_value' => $shortcut_set->title, + '#maxlength' => 255, + '#required' => TRUE, + '#weight' => -5, + ); + $form['actions'] = array( + '#type' => 'container', + '#attributes' => array('class' => array('form-actions')), + '#weight' => 100, + ); + $form['actions']['submit'] = array( + '#type' => 'submit', + '#value' => t('Save'), + '#weight' => 5, + ); + + return $form; +} + +/** + * Submit handler for shortcut_set_edit_form(). + */ +function shortcut_set_edit_form_submit($form, &$form_state) { + $shortcut_set = $form_state['values']['shortcut_set']; + $shortcut_set->title = $form_state['values']['title']; + shortcut_set_save($shortcut_set); + drupal_set_message(t('Updated set name to %set-name.', array('%set-name' => $shortcut_set->title))); + $form_state['redirect'] = "admin/config/user-interface/shortcut/$shortcut_set->set_name"; +} + +/** + * Form callback: builds the confirmation form for deleting a shortcut set. + * + * @param $form + * An associative array containing the structure of the form. + * @param $form_state + * An associative array containing the current state of the form. + * @param object $shortcut_set + * An object representing the shortcut set, as returned from + * shortcut_set_load(). + * + * @return + * An array representing the form definition. + * + * @ingroup forms + * @see shortcut_set_delete_form_submit() + */ +function shortcut_set_delete_form($form, &$form_state, $shortcut_set) { + $form['shortcut_set'] = array( + '#type' => 'value', + '#value' => $shortcut_set->set_name, + ); + + // Find out how many users are directly assigned to this shortcut set, and + // make a message. + $number = db_query('SELECT COUNT(*) FROM {shortcut_set_users} WHERE set_name = :name', array(':name' => $shortcut_set->set_name))->fetchField(); + $info = ''; + if ($number) { + $info .= '<p>' . format_plural($number, + '1 user has chosen or been assigned to this shortcut set.', + '@count users have chosen or been assigned to this shortcut set.') . '</p>'; + } + + // Also, if a module implements hook_shortcut_default_set(), it's possible + // that this set is being used as a default set. Add a message about that too. + if (count(module_implements('shortcut_default_set')) > 0) { + $info .= '<p>' . t('If you have chosen this shortcut set as the default for some or all users, they may also be affected by deleting it.') . '</p>'; + } + + $form['info'] = array( + '#markup' => $info, + ); + + return confirm_form( + $form, + t('Are you sure you want to delete the shortcut set %title?', array('%title' => $shortcut_set->title)), + 'admin/config/user-interface/shortcut/' . $shortcut_set->set_name, + t('This action cannot be undone.'), + t('Delete'), + t('Cancel') + ); +} + +/** + * Submit handler for shortcut_set_delete_form(). + */ +function shortcut_set_delete_form_submit($form, &$form_state) { + $shortcut_set = shortcut_set_load($form_state['values']['shortcut_set']); + shortcut_set_delete($shortcut_set); + $form_state['redirect'] = 'admin/config/user-interface/shortcut'; + drupal_set_message(t('The shortcut set %title has been deleted.', array('%title' => $shortcut_set->title))); +} + +/** + * Form callback: builds the confirmation form for deleting a shortcut link. * * @param $form * An associative array containing the structure of the form. @@ -463,6 +662,7 @@ function shortcut_admin_add_link($shortcut_link, &$shortcut_set, $limit = NULL) * An associative array containing the current state of the form. * @param $shortcut_link * An array representing the link that will be deleted. + * * @return * An array representing the form definition. * @@ -486,7 +686,7 @@ function shortcut_link_delete($form, &$form_state, $shortcut_link) { } /** - * Submit handler for the shortcut link deletion form. + * Submit handler for shortcut_link_delete_submit(). */ function shortcut_link_delete_submit($form, &$form_state) { $shortcut_link = $form_state['values']['shortcut_link']; @@ -496,7 +696,7 @@ function shortcut_link_delete_submit($form, &$form_state) { } /** - * Menu callback; Creates a new link in the provided shortcut set + * Menu page callback: creates a new link in the provided shortcut set. * * After completion, redirects the user back to where they came from. * @@ -520,5 +720,6 @@ function shortcut_link_add_inline($shortcut_set) { } drupal_goto(); } + return drupal_access_denied(); } diff --git a/modules/shortcut/shortcut.module b/modules/shortcut/shortcut.module index 7aab087ca..fccb0f61b 100644 --- a/modules/shortcut/shortcut.module +++ b/modules/shortcut/shortcut.module @@ -10,7 +10,7 @@ * The name of the default shortcut set. * * This set will be displayed to any user that does not have another set - * assigned. + * assigned, unless overridden by a hook_shortcut_default_set() implementation. */ define('SHORTCUT_DEFAULT_SET_NAME', 'shortcut-set-1'); @@ -18,19 +18,30 @@ define('SHORTCUT_DEFAULT_SET_NAME', 'shortcut-set-1'); * Implements hook_help(). */ 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/handbook/modules/shortcut/')) . '</p>'; $output .= '<h3>' . t('Uses') . '</h3>'; - $output .= '<dl><dt>' . t('Creating and modifying shortcuts and sets') . '</dt>'; - $output .= '<dd>' . t('Users with the appropriate permissions can manage shortcut sets, choose a preferred shortcut set, 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 .= '<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). 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 .= '<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; + } } } @@ -59,21 +70,53 @@ function shortcut_permission() { function shortcut_menu() { $items['admin/config/user-interface/shortcut'] = array( 'title' => 'Shortcuts', - 'description' => 'List the available shortcut sets and switch between them.', + '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_switch'), + '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', + 'title 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/%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), + 'type' => MENU_CALLBACK, + 'file' => 'shortcut.admin.inc', + ); $items['admin/config/user-interface/shortcut/%shortcut_set/add-link'] = array( 'title' => 'Add shortcut', 'page callback' => 'drupal_get_form', @@ -119,6 +162,7 @@ function shortcut_menu() { 'type' => MENU_LOCAL_TASK, 'file' => 'shortcut.admin.inc', ); + return $items; } @@ -160,9 +204,10 @@ function shortcut_block_view($delta = '') { /** * Access callback for editing a shortcut set. * - * @param $shortcut_set - * (optional) The shortcut set to be edited. If not set, the current - * displayed shortcut set will be assumed. + * @param object $shortcut_set + * (optional) The shortcut set to be edited. If not set, the current user's + * shortcut set will be used. + * * @return * TRUE if the current user has access to edit the shortcut set, FALSE * otherwise. @@ -180,20 +225,61 @@ function shortcut_set_edit_access($shortcut_set = NULL) { } /** + * Access callback for deleting a shortcut set. + * + * @param $shortcut_set + * The shortcut set to be deleted. + * + * @return + * TRUE if the current user has access to delete shortcut sets and this is + * not the site-wide default set; FALSE otherwise. + */ +function shortcut_set_delete_access($shortcut_set) { + // Only admins can delete sets. + if (!user_access('administer shortcuts')) { + return FALSE; + } + + // Never let the default shortcut set be deleted. + if ($shortcut_set->set_name == SHORTCUT_DEFAULT_SET_NAME) { + return FALSE; + } + + return TRUE; +} + +/** * Access callback for switching the shortcut set assigned to a user account. * - * @param $account + * @param object $account * (optional) The user account whose shortcuts will be switched. If not set, - * the account of the current logged-in user will be assumed. + * permissions will be checked for switching the logged-in user's own + * shortcut set. + * * @return * TRUE if the current user has access to switch the shortcut set of the * provided account, FALSE otherwise. */ function shortcut_set_switch_access($account = NULL) { global $user; - // Sufficiently-privileged users can switch their own shortcut sets, but not - // those of other users. Shortcut administrators can switch any user's set. - return user_access('administer shortcuts') || (user_access('switch shortcut sets') && (!isset($account) || $user->uid == $account->uid)); + + if (user_access('administer shortcuts')) { + // Administrators can switch anyone's shortcut set. + return TRUE; + } + + if (!user_access('switch shortcut sets')) { + // The user has no permission to switch anyone's shortcut set. + return FALSE; + } + + if (!isset($account) || $user->uid == $account->uid) { + // Users with the 'switch shortcut sets' permission can switch their own + // shortcuts sets. + return TRUE; + } + + return FALSE; } /** @@ -213,9 +299,9 @@ function shortcut_link_access($menu_link) { * * @param $set_name * The name of the shortcut set to load. - * @return - * If the shortcut set exists, an object of type stdClass containing the - * following properties: + * + * @return object + * If the shortcut set exists, an object containing the following properties: * - 'set_name': The internal name of the shortcut set. * - 'title': The title of the shortcut set. * - 'links': An array of links associated with this shortcut set. @@ -250,6 +336,7 @@ function shortcut_set_load($set_name) { * - 'link_path': The Drupal path or external path that the link points to. * - 'link_title': The title of the link. * Any other keys accepted by menu_link_save() may also be provided. + * * @return * A constant which is either SAVED_NEW or SAVED_UPDATED depending on whether * a new set was created or an existing one was updated. @@ -292,37 +379,41 @@ function shortcut_set_save(&$shortcut_set) { * * @param $shortcut_set * An object representing the shortcut set to delete. + * * @return * TRUE if the set was deleted, FALSE otherwise. */ function shortcut_set_delete($shortcut_set) { - // Make sure not to delete the default set. - $default_set = shortcut_default_set(); - if ($shortcut_set->set_name == $default_set->set_name) { + // Don't allow deletion of the system default shortcut set. + if ($shortcut_set->set_name == SHORTCUT_DEFAULT_SET_NAME) { return FALSE; } + // First, delete any user assignments for this set, so that each of these // users will go back to using whatever default set applies. db_delete('shortcut_set_users') ->condition('set_name', $shortcut_set->set_name) ->execute(); + // Next, delete the menu links for this set. menu_delete_links($shortcut_set->set_name); + // Finally, delete the set itself. $deleted = db_delete('shortcut_set') ->condition('set_name', $shortcut_set->set_name) ->execute(); + return (bool) $deleted; } /** - * Reset the link weights in a shortcut set to match their current order. + * Resets the link weights in a shortcut set to match their current order. * * This function can be used, for example, when a new shortcut link is added to * the set. If the link is added to the end of the array and this function is * called, it will force that link to display at the end of the list. * - * @param $shortcut_set + * @param object $shortcut_set * An object representing a shortcut set. The link weights of the passed-in * object will be reset as described above. */ @@ -335,7 +426,7 @@ function shortcut_set_reset_link_weights(&$shortcut_set) { } /** - * Assign a user to a particular shortcut set. + * Assigns a user to a particular shortcut set. * * @param $shortcut_set * An object representing the shortcut set. @@ -351,12 +442,13 @@ function shortcut_set_assign_user($shortcut_set, $account) { } /** - * Unassign a user from any shortcut set they may have been assigned to. + * Unassigns a user from any shortcut set they may have been assigned to. * * The user will go back to using whatever default set applies. * * @param $account * A user account that will be removed from the shortcut set assignment. + * * @return * TRUE if the user was previously assigned to a shortcut set and has been * successfully removed from it. FALSE if the user was already not assigned @@ -374,7 +466,8 @@ function shortcut_set_unassign_user($account) { * * @param $account * (optional) The user account whose shortcuts will be returned. Defaults to - * the current logged-in user. + * the currently logged-in user. + * * @return * An object representing the shortcut set that should be displayed to the * current user. If the user does not have an explicit shortcut set defined, @@ -412,9 +505,11 @@ function shortcut_current_displayed_set($account = NULL) { /** * Returns the default shortcut set for a given user account. * - * @param $account - * (optional) The user account whose shortcuts will be returned. Defaults to - * the current logged-in user. + * @param object $account + * (optional) The user account whose default shortcut set will be returned. + * If not provided, the function will return the currently logged-in user's + * default shortcut set. + * * @return * An object representing the default shortcut set. */ @@ -423,16 +518,19 @@ function shortcut_default_set($account = NULL) { if (!isset($account)) { $account = $user; } + // Allow modules to return a default shortcut set name. Since we can only // have one, we allow the last module which returns a valid result to take // precedence. If no module returns a valid set, fall back on the site-wide - // default. - $shortcut_set_names = array_reverse(array_merge(array(SHORTCUT_DEFAULT_SET_NAME), module_invoke_all('shortcut_default_set', $account))); - foreach ($shortcut_set_names as $name) { + // default, which is the lowest-numbered shortcut set. + $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; } @@ -461,6 +559,7 @@ function shortcut_set_get_unique_name() { * * @param $number * A number representing the shortcut set whose name should be retrieved. + * * @return * A string representing the expected shortcut name. */ @@ -613,3 +712,16 @@ function shortcut_toolbar_pre_render($toolbar) { return $toolbar; } +/** + * Returns the title of a shortcut set. + * + * Title callback for the editing pages for shortcut sets. + * + * @param $shortcut_set + * An object representing the shortcut set, as returned by + * shortcut_set_load(). + */ +function shortcut_set_title($shortcut_set) { + return check_plain($shortcut_set->title); +} + |