Project

General

Profile

Paste
Download (26.6 KB) Statistics
| Branch: | Revision:

root / drupal7 / modules / shortcut / shortcut.module @ db2d93dd

1
<?php
2

    
3
/**
4
 * @file
5
 * Allows users to manage customizable lists of shortcut links.
6
 */
7

    
8
/**
9
 * The name of the default shortcut set.
10
 *
11
 * This set will be displayed to any user that does not have another set
12
 * assigned, unless overridden by a hook_shortcut_default_set() implementation.
13
 */
14
define('SHORTCUT_DEFAULT_SET_NAME', 'shortcut-set-1');
15

    
16
/**
17
 * Implements hook_help().
18
 */
19
function shortcut_help($path, $arg) {
20
  global $user;
21

    
22
  switch ($path) {
23
    case 'admin/help#shortcut':
24
      $output = '<h3>' . t('About') . '</h3>';
25
      $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>';
26
      $output .= '<h3>' . t('Uses') . '</h3>';
27
      $output .= '<dl><dt>' . t('Administering shortcuts') . '</dt>';
28
      $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>';
29
      $output .= '<dt>' . t('Choosing shortcut sets') . '</dt>';
30
      $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>';
31
      $output .= '<dt>' . t('Adding and removing shortcuts') . '</dt>';
32
      $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>';
33
      $output .= '<dt>' . t('Displaying shortcuts') . '</dt>';
34
      $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>';
35
      $output .= '</dl>';
36
      return $output;
37

    
38
    case 'admin/config/user-interface/shortcut':
39
    case 'admin/config/user-interface/shortcut/%':
40
      if (user_access('switch shortcut sets')) {
41
        $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>';
42
        return $output;
43
      }
44
  }
45
}
46

    
47
/**
48
 * Implements hook_permission().
49
 */
50
function shortcut_permission() {
51
  return array(
52
    'administer shortcuts' => array(
53
      'title' => t('Administer shortcuts'),
54
    ),
55
    'customize shortcut links' => array(
56
      'title' => t('Edit current shortcut set'),
57
      '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.'),
58
    ),
59
    'switch shortcut sets' => array(
60
      'title' => t('Select any shortcut set'),
61
      'description' => t('From all shortcut sets, select one to be own active set. Without this permission, an administrator selects shortcut sets for users.'),
62
    ),
63
  );
64
}
65

    
66
/**
67
 * Implements hook_menu().
68
 */
69
function shortcut_menu() {
70
  $items['admin/config/user-interface/shortcut'] = array(
71
    'title' => 'Shortcuts',
72
    'description' => 'Add and modify shortcut sets.',
73
    'page callback' => 'shortcut_set_admin',
74
    'access arguments' => array('administer shortcuts'),
75
    'file' => 'shortcut.admin.inc',
76
  );
77
  $items['admin/config/user-interface/shortcut/add-set'] = array(
78
    'title' => 'Add shortcut set',
79
    'page callback' => 'drupal_get_form',
80
    'page arguments' => array('shortcut_set_add_form'),
81
    'access arguments' => array('administer shortcuts'),
82
    'type' => MENU_LOCAL_ACTION,
83
    'file' => 'shortcut.admin.inc',
84
  );
85
  $items['admin/config/user-interface/shortcut/%shortcut_set'] = array(
86
    'title' => 'Edit shortcuts',
87
    'page callback' => 'drupal_get_form',
88
    'page arguments' => array('shortcut_set_customize', 4),
89
    'title callback' => 'shortcut_set_title_callback',
90
    'title arguments' => array(4),
91
    'access callback' => 'shortcut_set_edit_access',
92
    'access arguments' => array(4),
93
    'file' => 'shortcut.admin.inc',
94
  );
95
  $items['admin/config/user-interface/shortcut/%shortcut_set/links'] = array(
96
    'title' => 'List links',
97
    'type' => MENU_DEFAULT_LOCAL_TASK,
98
  );
99
  $items['admin/config/user-interface/shortcut/%shortcut_set/edit'] = array(
100
    'title' => 'Edit set name',
101
    'page callback' => 'drupal_get_form',
102
    'page arguments' => array('shortcut_set_edit_form', 4),
103
    'access callback' => 'shortcut_set_edit_access',
104
    'access arguments' => array(4),
105
    'type' => MENU_LOCAL_TASK,
106
    'file' => 'shortcut.admin.inc',
107
    'weight' => 10,
108
  );
109
  $items['admin/config/user-interface/shortcut/%shortcut_set/delete'] = array(
110
    'title' => 'Delete shortcut set',
111
    'page callback' => 'drupal_get_form',
112
    'page arguments' => array('shortcut_set_delete_form', 4),
113
    'access callback' => 'shortcut_set_delete_access',
114
    'access arguments' => array(4),
115
    'file' => 'shortcut.admin.inc',
116
  );
117
  $items['admin/config/user-interface/shortcut/%shortcut_set/add-link'] = array(
118
    'title' => 'Add shortcut',
119
    'page callback' => 'drupal_get_form',
120
    'page arguments' => array('shortcut_link_add', 4),
121
    'access callback' => 'shortcut_set_edit_access',
122
    'access arguments' => array(4),
123
    'type' => MENU_LOCAL_ACTION,
124
    'file' => 'shortcut.admin.inc',
125
  );
126
  $items['admin/config/user-interface/shortcut/%shortcut_set/add-link-inline'] = array(
127
    'title' => 'Add shortcut',
128
    'page callback' => 'shortcut_link_add_inline',
129
    'page arguments' => array(4),
130
    'access callback' => 'shortcut_set_edit_access',
131
    'access arguments' => array(4),
132
    'type' => MENU_CALLBACK,
133
    'file' => 'shortcut.admin.inc',
134
  );
135
  $items['admin/config/user-interface/shortcut/link/%menu_link'] = array(
136
    'title' => 'Edit shortcut',
137
    'page callback' => 'drupal_get_form',
138
    'page arguments' => array('shortcut_link_edit', 5),
139
    'access callback' => 'shortcut_link_access',
140
    'access arguments' => array(5),
141
    'file' => 'shortcut.admin.inc',
142
  );
143
  $items['admin/config/user-interface/shortcut/link/%menu_link/delete'] = array(
144
    'title' => 'Delete shortcut',
145
    'page callback' => 'drupal_get_form',
146
    'page arguments' => array('shortcut_link_delete', 5),
147
    'access callback' => 'shortcut_link_access',
148
    'access arguments' => array(5),
149
    'file' => 'shortcut.admin.inc',
150
  );
151
  $items['user/%user/shortcuts'] = array(
152
    'title' => 'Shortcuts',
153
    'page callback' => 'drupal_get_form',
154
    'page arguments' => array('shortcut_set_switch', 1),
155
    'access callback' => 'shortcut_set_switch_access',
156
    'access arguments' => array(1),
157
    'type' => MENU_LOCAL_TASK,
158
    'file' => 'shortcut.admin.inc',
159
  );
160

    
161
  return $items;
162
}
163

    
164
/**
165
 * Implements hook_admin_paths().
166
 */
167
function shortcut_admin_paths() {
168
  $paths = array(
169
    'user/*/shortcuts' => TRUE,
170
  );
171
  return $paths;
172
}
173

    
174
/**
175
 * Implements hook_theme().
176
 */
177
function shortcut_theme() {
178
  return array(
179
    'shortcut_set_customize' => array(
180
      'render element' => 'form',
181
      'file' => 'shortcut.admin.inc',
182
    ),
183
  );
184
}
185

    
186
/**
187
 * Implements hook_block_info().
188
 */
189
function shortcut_block_info() {
190
  $blocks['shortcuts']['info'] = t('Shortcuts');
191
  // Shortcut blocks can't be cached because each menu item can have a custom
192
  // access callback. menu.inc manages its own caching.
193
  $blocks['shortcuts']['cache'] = DRUPAL_NO_CACHE;
194
  return $blocks;
195
}
196

    
197
/**
198
 * Implements hook_block_view().
199
 */
200
function shortcut_block_view($delta = '') {
201
  if ($delta == 'shortcuts') {
202
    $shortcut_set = shortcut_current_displayed_set();
203
    $data['subject'] = t('@shortcut_set shortcuts', array('@shortcut_set' => $shortcut_set->title));
204
    $data['content'] = shortcut_renderable_links($shortcut_set);
205
    return $data;
206
  }
207
}
208

    
209
/**
210
 * Access callback for editing a shortcut set.
211
 *
212
 * @param object $shortcut_set
213
 *   (optional) The shortcut set to be edited. If not set, the current user's
214
 *   shortcut set will be used.
215
 *
216
 * @return
217
 *   TRUE if the current user has access to edit the shortcut set, FALSE
218
 *   otherwise.
219
 */
220
function shortcut_set_edit_access($shortcut_set = NULL) {
221
  // Sufficiently-privileged users can edit their currently displayed shortcut
222
  // set, but not other sets. Shortcut administrators can edit any set.
223
  if (user_access('administer shortcuts')) {
224
    return TRUE;
225
  }
226
  if (user_access('customize shortcut links')) {
227
    return !isset($shortcut_set) || $shortcut_set == shortcut_current_displayed_set();
228
  }
229
  return FALSE;
230
}
231

    
232
/**
233
 * Access callback for deleting a shortcut set.
234
 *
235
 * @param $shortcut_set
236
 *   The shortcut set to be deleted.
237
 *
238
 * @return
239
 *   TRUE if the current user has access to delete shortcut sets and this is
240
 *   not the site-wide default set; FALSE otherwise.
241
 */
242
function shortcut_set_delete_access($shortcut_set) {
243
  // Only admins can delete sets.
244
  if (!user_access('administer shortcuts')) {
245
    return FALSE;
246
  }
247

    
248
  // Never let the default shortcut set be deleted.
249
  if ($shortcut_set->set_name == SHORTCUT_DEFAULT_SET_NAME) {
250
    return FALSE;
251
  }
252

    
253
  return TRUE;
254
}
255

    
256
/**
257
 * Access callback for switching the shortcut set assigned to a user account.
258
 *
259
 * @param object $account
260
 *   (optional) The user account whose shortcuts will be switched. If not set,
261
 *   permissions will be checked for switching the logged-in user's own
262
 *   shortcut set.
263
 *
264
 * @return
265
 *   TRUE if the current user has access to switch the shortcut set of the
266
 *   provided account, FALSE otherwise.
267
 */
268
function shortcut_set_switch_access($account = NULL) {
269
  global $user;
270

    
271
  if (user_access('administer shortcuts')) {
272
    // Administrators can switch anyone's shortcut set.
273
    return TRUE;
274
  }
275

    
276
  if (!user_access('switch shortcut sets')) {
277
    // The user has no permission to switch anyone's shortcut set.
278
    return FALSE;
279
  }
280

    
281
  if (!isset($account) || $user->uid == $account->uid) {
282
    // Users with the 'switch shortcut sets' permission can switch their own
283
    // shortcuts sets.
284
    return TRUE;
285
  }
286

    
287
  return FALSE;
288
}
289

    
290
/**
291
 * Access callback for editing a link in a shortcut set.
292
 */
293
function shortcut_link_access($menu_link) {
294
  // The link must belong to a shortcut set that the current user has access
295
  // to edit.
296
  if ($shortcut_set = shortcut_set_load($menu_link['menu_name'])) {
297
    return shortcut_set_edit_access($shortcut_set);
298
  }
299
  return FALSE;
300
}
301

    
302
/**
303
 * Loads the data for a shortcut set.
304
 *
305
 * @param $set_name
306
 *   The name of the shortcut set to load.
307
 *
308
 * @return object
309
 *   If the shortcut set exists, an object containing the following properties:
310
 *   - 'set_name': The internal name of the shortcut set.
311
 *   - 'title': The title of the shortcut set.
312
 *   - 'links': An array of links associated with this shortcut set.
313
 *   If the shortcut set does not exist, the function returns FALSE.
314
 */
315
function shortcut_set_load($set_name) {
316
  $set = db_select('shortcut_set', 'ss')
317
  ->fields('ss')
318
  ->condition('set_name', $set_name)
319
  ->execute()
320
  ->fetchObject();
321
  if (!$set) {
322
    return FALSE;
323
  }
324
  $set->links = menu_load_links($set_name);
325
  return $set;
326
}
327

    
328
/**
329
 * Saves a shortcut set.
330
 *
331
 * @param $shortcut_set
332
 *   An object containing the following properties:
333
 *   - 'title': The title of the shortcut set.
334
 *   - 'set_name': (optional) The internal name of the shortcut set. If
335
 *     omitted, a new shortcut set will be created, and the 'set_name' property
336
 *     will be added to the passed-in object.
337
 *   - 'links': (optional) An array of menu links to save for the shortcut set.
338
 *     Each link is an array containing at least the following keys (which will
339
 *     be expanded to fill in other default values after the shortcut set is
340
 *     saved):
341
 *     - 'link_path': The Drupal path or external path that the link points to.
342
 *     - 'link_title': The title of the link.
343
 *     Any other keys accepted by menu_link_save() may also be provided.
344
 *
345
 * @return
346
 *   A constant which is either SAVED_NEW or SAVED_UPDATED depending on whether
347
 *   a new set was created or an existing one was updated.
348
 *
349
 * @see menu_link_save()
350
 */
351
function shortcut_set_save(&$shortcut_set) {
352
  // First save the shortcut set itself.
353
  if (isset($shortcut_set->set_name)) {
354
    $return = drupal_write_record('shortcut_set', $shortcut_set, 'set_name');
355
  }
356
  else {
357
    $shortcut_set->set_name = shortcut_set_get_unique_name();
358
    $return = drupal_write_record('shortcut_set', $shortcut_set);
359
  }
360
  // If links were provided for the set, save them.
361
  if (isset($shortcut_set->links)) {
362
    foreach ($shortcut_set->links as &$link) {
363
      // Do not specifically associate these links with the shortcut module,
364
      // since other modules may make them editable via the menu system.
365
      // However, we do need to specify the correct menu name.
366
      $link['menu_name'] = $shortcut_set->set_name;
367
      $link['plid'] = 0;
368
      menu_link_save($link);
369
    }
370
    // Make sure that we have a return value, since if the links were updated
371
    // but the shortcut set was not, the call to drupal_write_record() above
372
    // would not return an indication that anything had changed.
373
    if (empty($return)) {
374
      $return = SAVED_UPDATED;
375
    }
376
  }
377
  return $return;
378
}
379

    
380
/**
381
 * Deletes a shortcut set.
382
 *
383
 * Note that the default set cannot be deleted.
384
 *
385
 * @param $shortcut_set
386
 *   An object representing the shortcut set to delete.
387
 *
388
 * @return
389
 *   TRUE if the set was deleted, FALSE otherwise.
390
 */
391
function shortcut_set_delete($shortcut_set) {
392
  // Don't allow deletion of the system default shortcut set.
393
  if ($shortcut_set->set_name == SHORTCUT_DEFAULT_SET_NAME) {
394
    return FALSE;
395
  }
396

    
397
  // First, delete any user assignments for this set, so that each of these
398
  // users will go back to using whatever default set applies.
399
  db_delete('shortcut_set_users')
400
    ->condition('set_name', $shortcut_set->set_name)
401
    ->execute();
402

    
403
  // Next, delete the menu links for this set.
404
  menu_delete_links($shortcut_set->set_name);
405

    
406
  // Finally, delete the set itself.
407
  $deleted = db_delete('shortcut_set')
408
    ->condition('set_name', $shortcut_set->set_name)
409
    ->execute();
410

    
411
  return (bool) $deleted;
412
}
413

    
414
/**
415
 * Resets the link weights in a shortcut set to match their current order.
416
 *
417
 * This function can be used, for example, when a new shortcut link is added to
418
 * the set. If the link is added to the end of the array and this function is
419
 * called, it will force that link to display at the end of the list.
420
 *
421
 * @param object $shortcut_set
422
 *   An object representing a shortcut set. The link weights of the passed-in
423
 *   object will be reset as described above.
424
 */
425
function shortcut_set_reset_link_weights(&$shortcut_set) {
426
  $weight = -50;
427
  foreach ($shortcut_set->links as &$link) {
428
    $link['weight'] = $weight;
429
    $weight++;
430
  }
431
}
432

    
433
/**
434
 * Assigns a user to a particular shortcut set.
435
 *
436
 * @param $shortcut_set
437
 *   An object representing the shortcut set.
438
 * @param $account
439
 *   A user account that will be assigned to use the set.
440
 */
441
function shortcut_set_assign_user($shortcut_set, $account) {
442
  db_merge('shortcut_set_users')
443
    ->key(array('uid' => $account->uid))
444
    ->fields(array('set_name' => $shortcut_set->set_name))
445
    ->execute();
446
  drupal_static_reset('shortcut_current_displayed_set');
447
}
448

    
449
/**
450
 * Unassigns a user from any shortcut set they may have been assigned to.
451
 *
452
 * The user will go back to using whatever default set applies.
453
 *
454
 * @param $account
455
 *   A user account that will be removed from the shortcut set assignment.
456
 *
457
 * @return
458
 *   TRUE if the user was previously assigned to a shortcut set and has been
459
 *   successfully removed from it. FALSE if the user was already not assigned
460
 *   to any set.
461
 */
462
function shortcut_set_unassign_user($account) {
463
  $deleted = db_delete('shortcut_set_users')
464
    ->condition('uid', $account->uid)
465
    ->execute();
466
  return (bool) $deleted;
467
}
468

    
469
/**
470
 * Returns the current displayed shortcut set for the provided user account.
471
 *
472
 * @param $account
473
 *   (optional) The user account whose shortcuts will be returned. Defaults to
474
 *   the currently logged-in user.
475
 *
476
 * @return
477
 *   An object representing the shortcut set that should be displayed to the
478
 *   current user. If the user does not have an explicit shortcut set defined,
479
 *   the default set is returned.
480
 */
481
function shortcut_current_displayed_set($account = NULL) {
482
  $shortcut_sets = &drupal_static(__FUNCTION__, array());
483
  global $user;
484
  if (!isset($account)) {
485
    $account = $user;
486
  }
487
  // Try to return a shortcut set from the static cache.
488
  if (isset($shortcut_sets[$account->uid])) {
489
    return $shortcut_sets[$account->uid];
490
  }
491
  // If none was found, try to find a shortcut set that is explicitly assigned
492
  // to this user.
493
  $query = db_select('shortcut_set', 's');
494
  $query->addField('s', 'set_name');
495
  $query->join('shortcut_set_users', 'u', 's.set_name = u.set_name');
496
  $query->condition('u.uid', $account->uid);
497
  $shortcut_set_name = $query->execute()->fetchField();
498
  if ($shortcut_set_name) {
499
    $shortcut_set = shortcut_set_load($shortcut_set_name);
500
  }
501
  // Otherwise, use the default set.
502
  else {
503
    $shortcut_set = shortcut_default_set($account);
504
  }
505

    
506
  $shortcut_sets[$account->uid] = $shortcut_set;
507
  return $shortcut_set;
508
}
509

    
510
/**
511
 * Returns the default shortcut set for a given user account.
512
 *
513
 * @param object $account
514
 *   (optional) The user account whose default shortcut set will be returned.
515
 *   If not provided, the function will return the currently logged-in user's
516
 *   default shortcut set.
517
 *
518
 * @return
519
 *   An object representing the default shortcut set.
520
 */
521
function shortcut_default_set($account = NULL) {
522
  global $user;
523
  if (!isset($account)) {
524
    $account = $user;
525
  }
526

    
527
  // Allow modules to return a default shortcut set name. Since we can only
528
  // have one, we allow the last module which returns a valid result to take
529
  // precedence. If no module returns a valid set, fall back on the site-wide
530
  // default, which is the lowest-numbered shortcut set.
531
  $suggestions = array_reverse(module_invoke_all('shortcut_default_set', $account));
532
  $suggestions[] = SHORTCUT_DEFAULT_SET_NAME;
533
  foreach ($suggestions as $name) {
534
    if ($shortcut_set = shortcut_set_load($name)) {
535
      break;
536
    }
537
  }
538

    
539
  return $shortcut_set;
540
}
541

    
542
/**
543
 * Returns a unique, machine-readable shortcut set name.
544
 */
545
function shortcut_set_get_unique_name() {
546
  // Shortcut sets are numbered sequentially, so we keep trying until we find
547
  // one that is available. For better performance, we start with a number
548
  // equal to one more than the current number of shortcut sets, so that if
549
  // no shortcut sets have been deleted from the database, this will
550
  // automatically give us the correct one.
551
  $number = db_query("SELECT COUNT(*) FROM {shortcut_set}")->fetchField() + 1;
552
  do {
553
    $name = shortcut_set_name($number);
554
    $number++;
555
  } while ($shortcut_set = shortcut_set_load($name));
556
  return $name;
557
}
558

    
559
/**
560
 * Returns the name of a shortcut set, based on a provided number.
561
 *
562
 * All shortcut sets have names like "shortcut-set-N" so that they can be
563
 * matched with a properly-namespaced entry in the {menu_links} table.
564
 *
565
 * @param $number
566
 *   A number representing the shortcut set whose name should be retrieved.
567
 *
568
 * @return
569
 *   A string representing the expected shortcut name.
570
 */
571
function shortcut_set_name($number) {
572
  return "shortcut-set-$number";
573
}
574

    
575
/**
576
 * Returns an array of all shortcut sets, keyed by the set name.
577
 *
578
 * @return
579
 *   An array of shortcut sets. Note that only the basic shortcut set
580
 *   properties (name and title) are returned by this function, not the list
581
 *   of menu links that belong to the set.
582
 */
583
function shortcut_sets() {
584
  return db_select('shortcut_set', 'ss')
585
  ->fields('ss')
586
  ->execute()
587
  ->fetchAllAssoc('set_name');
588
}
589

    
590
/**
591
 * Check to see if a shortcut set with the given title already exists.
592
 *
593
 * @param $title
594
 *   Human-readable name of the shortcut set to check.
595
 *
596
 * @return
597
 *   TRUE if a shortcut set with that title exists; FALSE otherwise.
598
 */
599
function shortcut_set_title_exists($title) {
600
  return (bool) db_query_range('SELECT 1 FROM {shortcut_set} WHERE title = :title', 0, 1, array(':title' => $title))->fetchField();
601
}
602

    
603
/**
604
 * Determines if a path corresponds to a valid shortcut link.
605
 *
606
 * @param $path
607
 *   The path to the link.
608
 * @return
609
 *   TRUE if the shortcut link is valid, FALSE otherwise. Valid links are ones
610
 *   that correspond to actual paths on the site.
611
 *
612
 * @see menu_edit_item_validate()
613
 */
614
function shortcut_valid_link($path) {
615
  // Do not use URL aliases.
616
  $normal_path = drupal_get_normal_path($path);
617
  if ($path != $normal_path) {
618
    $path = $normal_path;
619
  }
620
  // An empty path is valid too and will be converted to <front>.
621
  return (!url_is_external($path) && menu_get_item($path)) || empty($path) || $path == '<front>';
622
}
623

    
624
/**
625
 * Returns an array of shortcut links, suitable for rendering.
626
 *
627
 * @param $shortcut_set
628
 *   (optional) An object representing the set whose links will be displayed.
629
 *   If not provided, the user's current set will be displayed.
630
 * @return
631
 *   An array of shortcut links, in the format returned by the menu system.
632
 *
633
 * @see menu_tree()
634
 */
635
function shortcut_renderable_links($shortcut_set = NULL) {
636
  if (!isset($shortcut_set)) {
637
    $shortcut_set = shortcut_current_displayed_set();
638
  }
639
  return menu_tree($shortcut_set->set_name);
640
}
641

    
642
/**
643
 * Implements hook_preprocess_page().
644
 */
645
function shortcut_preprocess_page(&$variables) {
646
  // Only display the shortcut link if the user has the ability to edit
647
  // shortcuts and if the page's actual content is being shown (for example,
648
  // we do not want to display it on "access denied" or "page not found"
649
  // pages).
650
  if (shortcut_set_edit_access() && ($item = menu_get_item()) && $item['access']) {
651
    $link = $_GET['q'];
652
    $query_parameters = drupal_get_query_parameters();
653
    if (!empty($query_parameters)) {
654
     $link .= '?' . drupal_http_build_query($query_parameters);
655
    }
656
    $query = array(
657
     'link' => $link,
658
     'name' => drupal_get_title(),
659
    );
660
    $query += drupal_get_destination();
661

    
662
    $shortcut_set = shortcut_current_displayed_set();
663

    
664
    // Check if $link is already a shortcut and set $link_mode accordingly.
665
    foreach ($shortcut_set->links as $shortcut) {
666
      if ($link == $shortcut['link_path']) {
667
        $mlid = $shortcut['mlid'];
668
        break;
669
      }
670
    }
671
    $link_mode = isset($mlid) ? "remove" : "add";
672

    
673
    if ($link_mode == "add") {
674
      $query['token'] = drupal_get_token('shortcut-add-link');
675
      $link_text = shortcut_set_switch_access() ? t('Add to %shortcut_set shortcuts', array('%shortcut_set' => $shortcut_set->title)) : t('Add to shortcuts');
676
      $link_path = 'admin/config/user-interface/shortcut/' . $shortcut_set->set_name . '/add-link-inline';
677
    }
678
    else {
679
      $query['mlid'] = $mlid;
680
      $link_text = shortcut_set_switch_access() ? t('Remove from %shortcut_set shortcuts', array('%shortcut_set' => $shortcut_set->title)) : t('Remove from shortcuts');
681
      $link_path = 'admin/config/user-interface/shortcut/link/' . $mlid . '/delete';
682
    }
683

    
684
    if (theme_get_setting('shortcut_module_link')) {
685
      $variables['title_suffix']['add_or_remove_shortcut'] = array(
686
        '#attached' => array('css' => array(drupal_get_path('module', 'shortcut') . '/shortcut.css')),
687
        '#prefix' => '<div class="add-or-remove-shortcuts ' . $link_mode . '-shortcut">',
688
        '#type' => 'link',
689
        '#title' => '<span class="icon"></span><span class="text">' . $link_text . '</span>',
690
        '#href' => $link_path,
691
        '#options' => array('query' => $query, 'html' => TRUE),
692
        '#suffix' => '</div>',
693
      );
694
    }
695
  }
696
}
697

    
698
/**
699
 * Implements hook_page_alter().
700
 */
701
function shortcut_page_alter(&$page) {
702
  if (isset($page['page_top']['toolbar'])) {
703
    // If the toolbar is available, add a pre-render function to display the
704
    // current shortcuts in the toolbar drawer.
705
    $page['page_top']['toolbar']['#pre_render'][] = 'shortcut_toolbar_pre_render';
706
  }
707
}
708

    
709
/**
710
 * Pre-render function for adding shortcuts to the toolbar drawer.
711
 */
712
function shortcut_toolbar_pre_render($toolbar) {
713
  $links = shortcut_renderable_links();
714
  $links['#attached'] = array('css' => array(drupal_get_path('module', 'shortcut') . '/shortcut.css'));
715
  $links['#prefix'] = '<div class="toolbar-shortcuts">';
716
  $links['#suffix'] = '</div>';
717
  $shortcut_set = shortcut_current_displayed_set();
718
  $configure_link = NULL;
719
  if (shortcut_set_edit_access($shortcut_set)) {
720
    $configure_link = array(
721
      '#type' => 'link',
722
      '#title' => t('Edit shortcuts'),
723
      '#href' => 'admin/config/user-interface/shortcut/' . $shortcut_set->set_name,
724
      '#options' => array('attributes' => array('id' => 'edit-shortcuts')),
725
    );
726
  }
727

    
728
  $drawer = array(
729
    'shortcuts' => $links,
730
    'configure' => $configure_link,
731
  );
732

    
733
  $toolbar['toolbar_drawer'][] = $drawer;
734
  return $toolbar;
735
}
736

    
737
/**
738
 * Returns the sanitized title of a shortcut set.
739
 *
740
 * Deprecated. This function was previously used as a menu item title callback
741
 * but has been replaced by shortcut_set_title_callback() (which does not
742
 * sanitize the title, since the menu system does that automatically). In
743
 * Drupal 7, use that function for title callbacks, and call check_plain()
744
 * directly if you need a sanitized title. In Drupal 8, this function will be
745
 * restored as a title callback and therefore will no longer sanitize its
746
 * output.
747
 *
748
 * @param $shortcut_set
749
 *   An object representing the shortcut set, as returned by
750
 *   shortcut_set_load().
751
 */
752
function shortcut_set_title($shortcut_set) {
753
  return check_plain($shortcut_set->title);
754
}
755

    
756
/**
757
 * Returns the title of a shortcut set.
758
 *
759
 * Title callback for the editing pages for shortcut sets.
760
 *
761
 * @param $shortcut_set
762
 *   An object representing the shortcut set, as returned by
763
 *   shortcut_set_load().
764
 */
765
function shortcut_set_title_callback($shortcut_set) {
766
  return $shortcut_set->title;
767
}