shortcut.module 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. <?php
  2. /**
  3. * @file
  4. * Allows users to manage customizable lists of shortcut links.
  5. */
  6. /**
  7. * The name of the default shortcut set.
  8. *
  9. * This set will be displayed to any user that does not have another set
  10. * assigned, unless overridden by a hook_shortcut_default_set() implementation.
  11. */
  12. define('SHORTCUT_DEFAULT_SET_NAME', 'shortcut-set-1');
  13. /**
  14. * Implements hook_help().
  15. */
  16. function shortcut_help($path, $arg) {
  17. global $user;
  18. switch ($path) {
  19. case 'admin/help#shortcut':
  20. $output = '<h3>' . t('About') . '</h3>';
  21. $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>';
  22. $output .= '<h3>' . t('Uses') . '</h3>';
  23. $output .= '<dl><dt>' . t('Administering shortcuts') . '</dt>';
  24. $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>';
  25. $output .= '<dt>' . t('Choosing shortcut sets') . '</dt>';
  26. $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>';
  27. $output .= '<dt>' . t('Adding and removing shortcuts') . '</dt>';
  28. $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>';
  29. $output .= '<dt>' . t('Displaying shortcuts') . '</dt>';
  30. $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>';
  31. $output .= '</dl>';
  32. return $output;
  33. case 'admin/config/user-interface/shortcut':
  34. case 'admin/config/user-interface/shortcut/%':
  35. if (user_access('switch shortcut sets')) {
  36. $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>';
  37. return $output;
  38. }
  39. }
  40. }
  41. /**
  42. * Implements hook_permission().
  43. */
  44. function shortcut_permission() {
  45. return array(
  46. 'administer shortcuts' => array(
  47. 'title' => t('Administer shortcuts'),
  48. ),
  49. 'customize shortcut links' => array(
  50. 'title' => t('Edit current shortcut set'),
  51. '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.'),
  52. ),
  53. 'switch shortcut sets' => array(
  54. 'title' => t('Select any shortcut set'),
  55. 'description' => t('From all shortcut sets, select one to be own active set. Without this permission, an administrator selects shortcut sets for users.'),
  56. ),
  57. );
  58. }
  59. /**
  60. * Implements hook_menu().
  61. */
  62. function shortcut_menu() {
  63. $items['admin/config/user-interface/shortcut'] = array(
  64. 'title' => 'Shortcuts',
  65. 'description' => 'Add and modify shortcut sets.',
  66. 'page callback' => 'shortcut_set_admin',
  67. 'access arguments' => array('administer shortcuts'),
  68. 'file' => 'shortcut.admin.inc',
  69. );
  70. $items['admin/config/user-interface/shortcut/add-set'] = array(
  71. 'title' => 'Add shortcut set',
  72. 'page callback' => 'drupal_get_form',
  73. 'page arguments' => array('shortcut_set_add_form'),
  74. 'access arguments' => array('administer shortcuts'),
  75. 'type' => MENU_LOCAL_ACTION,
  76. 'file' => 'shortcut.admin.inc',
  77. );
  78. $items['admin/config/user-interface/shortcut/%shortcut_set'] = array(
  79. 'title' => 'Edit shortcuts',
  80. 'page callback' => 'drupal_get_form',
  81. 'page arguments' => array('shortcut_set_customize', 4),
  82. 'title callback' => 'shortcut_set_title_callback',
  83. 'title arguments' => array(4),
  84. 'access callback' => 'shortcut_set_edit_access',
  85. 'access arguments' => array(4),
  86. 'file' => 'shortcut.admin.inc',
  87. );
  88. $items['admin/config/user-interface/shortcut/%shortcut_set/links'] = array(
  89. 'title' => 'List links',
  90. 'type' => MENU_DEFAULT_LOCAL_TASK,
  91. );
  92. $items['admin/config/user-interface/shortcut/%shortcut_set/edit'] = array(
  93. 'title' => 'Edit set name',
  94. 'page callback' => 'drupal_get_form',
  95. 'page arguments' => array('shortcut_set_edit_form', 4),
  96. 'access callback' => 'shortcut_set_edit_access',
  97. 'access arguments' => array(4),
  98. 'type' => MENU_LOCAL_TASK,
  99. 'file' => 'shortcut.admin.inc',
  100. 'weight' => 10,
  101. );
  102. $items['admin/config/user-interface/shortcut/%shortcut_set/delete'] = array(
  103. 'title' => 'Delete shortcut set',
  104. 'page callback' => 'drupal_get_form',
  105. 'page arguments' => array('shortcut_set_delete_form', 4),
  106. 'access callback' => 'shortcut_set_delete_access',
  107. 'access arguments' => array(4),
  108. 'file' => 'shortcut.admin.inc',
  109. );
  110. $items['admin/config/user-interface/shortcut/%shortcut_set/add-link'] = array(
  111. 'title' => 'Add shortcut',
  112. 'page callback' => 'drupal_get_form',
  113. 'page arguments' => array('shortcut_link_add', 4),
  114. 'access callback' => 'shortcut_set_edit_access',
  115. 'access arguments' => array(4),
  116. 'type' => MENU_LOCAL_ACTION,
  117. 'file' => 'shortcut.admin.inc',
  118. );
  119. $items['admin/config/user-interface/shortcut/%shortcut_set/add-link-inline'] = array(
  120. 'title' => 'Add shortcut',
  121. 'page callback' => 'shortcut_link_add_inline',
  122. 'page arguments' => array(4),
  123. 'access callback' => 'shortcut_set_edit_access',
  124. 'access arguments' => array(4),
  125. 'type' => MENU_CALLBACK,
  126. 'file' => 'shortcut.admin.inc',
  127. );
  128. $items['admin/config/user-interface/shortcut/link/%menu_link'] = array(
  129. 'title' => 'Edit shortcut',
  130. 'page callback' => 'drupal_get_form',
  131. 'page arguments' => array('shortcut_link_edit', 5),
  132. 'access callback' => 'shortcut_link_access',
  133. 'access arguments' => array(5),
  134. 'file' => 'shortcut.admin.inc',
  135. );
  136. $items['admin/config/user-interface/shortcut/link/%menu_link/delete'] = array(
  137. 'title' => 'Delete shortcut',
  138. 'page callback' => 'drupal_get_form',
  139. 'page arguments' => array('shortcut_link_delete', 5),
  140. 'access callback' => 'shortcut_link_access',
  141. 'access arguments' => array(5),
  142. 'file' => 'shortcut.admin.inc',
  143. );
  144. $items['user/%user/shortcuts'] = array(
  145. 'title' => 'Shortcuts',
  146. 'page callback' => 'drupal_get_form',
  147. 'page arguments' => array('shortcut_set_switch', 1),
  148. 'access callback' => 'shortcut_set_switch_access',
  149. 'access arguments' => array(1),
  150. 'type' => MENU_LOCAL_TASK,
  151. 'file' => 'shortcut.admin.inc',
  152. );
  153. return $items;
  154. }
  155. /**
  156. * Implements hook_admin_paths().
  157. */
  158. function shortcut_admin_paths() {
  159. $paths = array(
  160. 'user/*/shortcuts' => TRUE,
  161. );
  162. return $paths;
  163. }
  164. /**
  165. * Implements hook_theme().
  166. */
  167. function shortcut_theme() {
  168. return array(
  169. 'shortcut_set_customize' => array(
  170. 'render element' => 'form',
  171. 'file' => 'shortcut.admin.inc',
  172. ),
  173. );
  174. }
  175. /**
  176. * Implements hook_block_info().
  177. */
  178. function shortcut_block_info() {
  179. $blocks['shortcuts']['info'] = t('Shortcuts');
  180. // Shortcut blocks can't be cached because each menu item can have a custom
  181. // access callback. menu.inc manages its own caching.
  182. $blocks['shortcuts']['cache'] = DRUPAL_NO_CACHE;
  183. return $blocks;
  184. }
  185. /**
  186. * Implements hook_block_view().
  187. */
  188. function shortcut_block_view($delta = '') {
  189. if ($delta == 'shortcuts') {
  190. $shortcut_set = shortcut_current_displayed_set();
  191. $data['subject'] = t('@shortcut_set shortcuts', array('@shortcut_set' => $shortcut_set->title));
  192. $data['content'] = shortcut_renderable_links($shortcut_set);
  193. return $data;
  194. }
  195. }
  196. /**
  197. * Access callback for editing a shortcut set.
  198. *
  199. * @param object $shortcut_set
  200. * (optional) The shortcut set to be edited. If not set, the current user's
  201. * shortcut set will be used.
  202. *
  203. * @return
  204. * TRUE if the current user has access to edit the shortcut set, FALSE
  205. * otherwise.
  206. */
  207. function shortcut_set_edit_access($shortcut_set = NULL) {
  208. // Sufficiently-privileged users can edit their currently displayed shortcut
  209. // set, but not other sets. Shortcut administrators can edit any set.
  210. if (user_access('administer shortcuts')) {
  211. return TRUE;
  212. }
  213. if (user_access('customize shortcut links')) {
  214. return !isset($shortcut_set) || $shortcut_set == shortcut_current_displayed_set();
  215. }
  216. return FALSE;
  217. }
  218. /**
  219. * Access callback for deleting a shortcut set.
  220. *
  221. * @param $shortcut_set
  222. * The shortcut set to be deleted.
  223. *
  224. * @return
  225. * TRUE if the current user has access to delete shortcut sets and this is
  226. * not the site-wide default set; FALSE otherwise.
  227. */
  228. function shortcut_set_delete_access($shortcut_set) {
  229. // Only admins can delete sets.
  230. if (!user_access('administer shortcuts')) {
  231. return FALSE;
  232. }
  233. // Never let the default shortcut set be deleted.
  234. if ($shortcut_set->set_name == SHORTCUT_DEFAULT_SET_NAME) {
  235. return FALSE;
  236. }
  237. return TRUE;
  238. }
  239. /**
  240. * Access callback for switching the shortcut set assigned to a user account.
  241. *
  242. * @param object $account
  243. * (optional) The user account whose shortcuts will be switched. If not set,
  244. * permissions will be checked for switching the logged-in user's own
  245. * shortcut set.
  246. *
  247. * @return
  248. * TRUE if the current user has access to switch the shortcut set of the
  249. * provided account, FALSE otherwise.
  250. */
  251. function shortcut_set_switch_access($account = NULL) {
  252. global $user;
  253. if (user_access('administer shortcuts')) {
  254. // Administrators can switch anyone's shortcut set.
  255. return TRUE;
  256. }
  257. if (!user_access('switch shortcut sets')) {
  258. // The user has no permission to switch anyone's shortcut set.
  259. return FALSE;
  260. }
  261. if (!isset($account) || $user->uid == $account->uid) {
  262. // Users with the 'switch shortcut sets' permission can switch their own
  263. // shortcuts sets.
  264. return TRUE;
  265. }
  266. return FALSE;
  267. }
  268. /**
  269. * Access callback for editing a link in a shortcut set.
  270. */
  271. function shortcut_link_access($menu_link) {
  272. // The link must belong to a shortcut set that the current user has access
  273. // to edit.
  274. if ($shortcut_set = shortcut_set_load($menu_link['menu_name'])) {
  275. return shortcut_set_edit_access($shortcut_set);
  276. }
  277. return FALSE;
  278. }
  279. /**
  280. * Loads the data for a shortcut set.
  281. *
  282. * @param $set_name
  283. * The name of the shortcut set to load.
  284. *
  285. * @return object
  286. * If the shortcut set exists, an object containing the following properties:
  287. * - 'set_name': The internal name of the shortcut set.
  288. * - 'title': The title of the shortcut set.
  289. * - 'links': An array of links associated with this shortcut set.
  290. * If the shortcut set does not exist, the function returns FALSE.
  291. */
  292. function shortcut_set_load($set_name) {
  293. $set = db_select('shortcut_set', 'ss')
  294. ->fields('ss')
  295. ->condition('set_name', $set_name)
  296. ->execute()
  297. ->fetchObject();
  298. if (!$set) {
  299. return FALSE;
  300. }
  301. $set->links = menu_load_links($set_name);
  302. return $set;
  303. }
  304. /**
  305. * Saves a shortcut set.
  306. *
  307. * @param $shortcut_set
  308. * An object containing the following properties:
  309. * - 'title': The title of the shortcut set.
  310. * - 'set_name': (optional) The internal name of the shortcut set. If
  311. * omitted, a new shortcut set will be created, and the 'set_name' property
  312. * will be added to the passed-in object.
  313. * - 'links': (optional) An array of menu links to save for the shortcut set.
  314. * Each link is an array containing at least the following keys (which will
  315. * be expanded to fill in other default values after the shortcut set is
  316. * saved):
  317. * - 'link_path': The Drupal path or external path that the link points to.
  318. * - 'link_title': The title of the link.
  319. * Any other keys accepted by menu_link_save() may also be provided.
  320. *
  321. * @return
  322. * A constant which is either SAVED_NEW or SAVED_UPDATED depending on whether
  323. * a new set was created or an existing one was updated.
  324. *
  325. * @see menu_link_save()
  326. */
  327. function shortcut_set_save(&$shortcut_set) {
  328. // First save the shortcut set itself.
  329. if (isset($shortcut_set->set_name)) {
  330. $return = drupal_write_record('shortcut_set', $shortcut_set, 'set_name');
  331. }
  332. else {
  333. $shortcut_set->set_name = shortcut_set_get_unique_name();
  334. $return = drupal_write_record('shortcut_set', $shortcut_set);
  335. }
  336. // If links were provided for the set, save them.
  337. if (isset($shortcut_set->links)) {
  338. foreach ($shortcut_set->links as &$link) {
  339. // Do not specifically associate these links with the shortcut module,
  340. // since other modules may make them editable via the menu system.
  341. // However, we do need to specify the correct menu name.
  342. $link['menu_name'] = $shortcut_set->set_name;
  343. $link['plid'] = 0;
  344. menu_link_save($link);
  345. }
  346. // Make sure that we have a return value, since if the links were updated
  347. // but the shortcut set was not, the call to drupal_write_record() above
  348. // would not return an indication that anything had changed.
  349. if (empty($return)) {
  350. $return = SAVED_UPDATED;
  351. }
  352. }
  353. return $return;
  354. }
  355. /**
  356. * Deletes a shortcut set.
  357. *
  358. * Note that the default set cannot be deleted.
  359. *
  360. * @param $shortcut_set
  361. * An object representing the shortcut set to delete.
  362. *
  363. * @return
  364. * TRUE if the set was deleted, FALSE otherwise.
  365. */
  366. function shortcut_set_delete($shortcut_set) {
  367. // Don't allow deletion of the system default shortcut set.
  368. if ($shortcut_set->set_name == SHORTCUT_DEFAULT_SET_NAME) {
  369. return FALSE;
  370. }
  371. // First, delete any user assignments for this set, so that each of these
  372. // users will go back to using whatever default set applies.
  373. db_delete('shortcut_set_users')
  374. ->condition('set_name', $shortcut_set->set_name)
  375. ->execute();
  376. // Next, delete the menu links for this set.
  377. menu_delete_links($shortcut_set->set_name);
  378. // Finally, delete the set itself.
  379. $deleted = db_delete('shortcut_set')
  380. ->condition('set_name', $shortcut_set->set_name)
  381. ->execute();
  382. return (bool) $deleted;
  383. }
  384. /**
  385. * Resets the link weights in a shortcut set to match their current order.
  386. *
  387. * This function can be used, for example, when a new shortcut link is added to
  388. * the set. If the link is added to the end of the array and this function is
  389. * called, it will force that link to display at the end of the list.
  390. *
  391. * @param object $shortcut_set
  392. * An object representing a shortcut set. The link weights of the passed-in
  393. * object will be reset as described above.
  394. */
  395. function shortcut_set_reset_link_weights(&$shortcut_set) {
  396. $weight = -50;
  397. foreach ($shortcut_set->links as &$link) {
  398. $link['weight'] = $weight;
  399. $weight++;
  400. }
  401. }
  402. /**
  403. * Assigns a user to a particular shortcut set.
  404. *
  405. * @param $shortcut_set
  406. * An object representing the shortcut set.
  407. * @param $account
  408. * A user account that will be assigned to use the set.
  409. */
  410. function shortcut_set_assign_user($shortcut_set, $account) {
  411. db_merge('shortcut_set_users')
  412. ->key(array('uid' => $account->uid))
  413. ->fields(array('set_name' => $shortcut_set->set_name))
  414. ->execute();
  415. drupal_static_reset('shortcut_current_displayed_set');
  416. }
  417. /**
  418. * Unassigns a user from any shortcut set they may have been assigned to.
  419. *
  420. * The user will go back to using whatever default set applies.
  421. *
  422. * @param $account
  423. * A user account that will be removed from the shortcut set assignment.
  424. *
  425. * @return
  426. * TRUE if the user was previously assigned to a shortcut set and has been
  427. * successfully removed from it. FALSE if the user was already not assigned
  428. * to any set.
  429. */
  430. function shortcut_set_unassign_user($account) {
  431. $deleted = db_delete('shortcut_set_users')
  432. ->condition('uid', $account->uid)
  433. ->execute();
  434. return (bool) $deleted;
  435. }
  436. /**
  437. * Returns the current displayed shortcut set for the provided user account.
  438. *
  439. * @param $account
  440. * (optional) The user account whose shortcuts will be returned. Defaults to
  441. * the currently logged-in user.
  442. *
  443. * @return
  444. * An object representing the shortcut set that should be displayed to the
  445. * current user. If the user does not have an explicit shortcut set defined,
  446. * the default set is returned.
  447. */
  448. function shortcut_current_displayed_set($account = NULL) {
  449. $shortcut_sets = &drupal_static(__FUNCTION__, array());
  450. global $user;
  451. if (!isset($account)) {
  452. $account = $user;
  453. }
  454. // Try to return a shortcut set from the static cache.
  455. if (isset($shortcut_sets[$account->uid])) {
  456. return $shortcut_sets[$account->uid];
  457. }
  458. // If none was found, try to find a shortcut set that is explicitly assigned
  459. // to this user.
  460. $query = db_select('shortcut_set', 's');
  461. $query->addField('s', 'set_name');
  462. $query->join('shortcut_set_users', 'u', 's.set_name = u.set_name');
  463. $query->condition('u.uid', $account->uid);
  464. $shortcut_set_name = $query->execute()->fetchField();
  465. if ($shortcut_set_name) {
  466. $shortcut_set = shortcut_set_load($shortcut_set_name);
  467. }
  468. // Otherwise, use the default set.
  469. else {
  470. $shortcut_set = shortcut_default_set($account);
  471. }
  472. $shortcut_sets[$account->uid] = $shortcut_set;
  473. return $shortcut_set;
  474. }
  475. /**
  476. * Returns the default shortcut set for a given user account.
  477. *
  478. * @param object $account
  479. * (optional) The user account whose default shortcut set will be returned.
  480. * If not provided, the function will return the currently logged-in user's
  481. * default shortcut set.
  482. *
  483. * @return
  484. * An object representing the default shortcut set.
  485. */
  486. function shortcut_default_set($account = NULL) {
  487. global $user;
  488. if (!isset($account)) {
  489. $account = $user;
  490. }
  491. // Allow modules to return a default shortcut set name. Since we can only
  492. // have one, we allow the last module which returns a valid result to take
  493. // precedence. If no module returns a valid set, fall back on the site-wide
  494. // default, which is the lowest-numbered shortcut set.
  495. $suggestions = array_reverse(module_invoke_all('shortcut_default_set', $account));
  496. $suggestions[] = SHORTCUT_DEFAULT_SET_NAME;
  497. foreach ($suggestions as $name) {
  498. if ($shortcut_set = shortcut_set_load($name)) {
  499. break;
  500. }
  501. }
  502. return $shortcut_set;
  503. }
  504. /**
  505. * Returns a unique, machine-readable shortcut set name.
  506. */
  507. function shortcut_set_get_unique_name() {
  508. // Shortcut sets are numbered sequentially, so we keep trying until we find
  509. // one that is available. For better performance, we start with a number
  510. // equal to one more than the current number of shortcut sets, so that if
  511. // no shortcut sets have been deleted from the database, this will
  512. // automatically give us the correct one.
  513. $number = db_query("SELECT COUNT(*) FROM {shortcut_set}")->fetchField() + 1;
  514. do {
  515. $name = shortcut_set_name($number);
  516. $number++;
  517. } while ($shortcut_set = shortcut_set_load($name));
  518. return $name;
  519. }
  520. /**
  521. * Returns the name of a shortcut set, based on a provided number.
  522. *
  523. * All shortcut sets have names like "shortcut-set-N" so that they can be
  524. * matched with a properly-namespaced entry in the {menu_links} table.
  525. *
  526. * @param $number
  527. * A number representing the shortcut set whose name should be retrieved.
  528. *
  529. * @return
  530. * A string representing the expected shortcut name.
  531. */
  532. function shortcut_set_name($number) {
  533. return "shortcut-set-$number";
  534. }
  535. /**
  536. * Returns an array of all shortcut sets, keyed by the set name.
  537. *
  538. * @return
  539. * An array of shortcut sets. Note that only the basic shortcut set
  540. * properties (name and title) are returned by this function, not the list
  541. * of menu links that belong to the set.
  542. */
  543. function shortcut_sets() {
  544. return db_select('shortcut_set', 'ss')
  545. ->fields('ss')
  546. ->execute()
  547. ->fetchAllAssoc('set_name');
  548. }
  549. /**
  550. * Check to see if a shortcut set with the given title already exists.
  551. *
  552. * @param $title
  553. * Human-readable name of the shortcut set to check.
  554. *
  555. * @return
  556. * TRUE if a shortcut set with that title exists; FALSE otherwise.
  557. */
  558. function shortcut_set_title_exists($title) {
  559. return (bool) db_query_range('SELECT 1 FROM {shortcut_set} WHERE title = :title', 0, 1, array(':title' => $title))->fetchField();
  560. }
  561. /**
  562. * Determines if a path corresponds to a valid shortcut link.
  563. *
  564. * @param $path
  565. * The path to the link.
  566. * @return
  567. * TRUE if the shortcut link is valid, FALSE otherwise. Valid links are ones
  568. * that correspond to actual paths on the site.
  569. *
  570. * @see menu_edit_item_validate()
  571. */
  572. function shortcut_valid_link($path) {
  573. // Do not use URL aliases.
  574. $normal_path = drupal_get_normal_path($path);
  575. if ($path != $normal_path) {
  576. $path = $normal_path;
  577. }
  578. // An empty path is valid too and will be converted to <front>.
  579. return (!url_is_external($path) && menu_get_item($path)) || empty($path) || $path == '<front>';
  580. }
  581. /**
  582. * Returns an array of shortcut links, suitable for rendering.
  583. *
  584. * @param $shortcut_set
  585. * (optional) An object representing the set whose links will be displayed.
  586. * If not provided, the user's current set will be displayed.
  587. * @return
  588. * An array of shortcut links, in the format returned by the menu system.
  589. *
  590. * @see menu_tree()
  591. */
  592. function shortcut_renderable_links($shortcut_set = NULL) {
  593. if (!isset($shortcut_set)) {
  594. $shortcut_set = shortcut_current_displayed_set();
  595. }
  596. return menu_tree($shortcut_set->set_name);
  597. }
  598. /**
  599. * Implements hook_preprocess_page().
  600. */
  601. function shortcut_preprocess_page(&$variables) {
  602. // Only display the shortcut link if the user has the ability to edit
  603. // shortcuts and if the page's actual content is being shown (for example,
  604. // we do not want to display it on "access denied" or "page not found"
  605. // pages).
  606. if (shortcut_set_edit_access() && ($item = menu_get_item()) && $item['access']) {
  607. $link = $_GET['q'];
  608. $query_parameters = drupal_get_query_parameters();
  609. if (!empty($query_parameters)) {
  610. $link .= '?' . drupal_http_build_query($query_parameters);
  611. }
  612. $query = array(
  613. 'link' => $link,
  614. 'name' => drupal_get_title(),
  615. );
  616. $query += drupal_get_destination();
  617. $shortcut_set = shortcut_current_displayed_set();
  618. // Check if $link is already a shortcut and set $link_mode accordingly.
  619. foreach ($shortcut_set->links as $shortcut) {
  620. if ($link == $shortcut['link_path']) {
  621. $mlid = $shortcut['mlid'];
  622. break;
  623. }
  624. }
  625. $link_mode = isset($mlid) ? "remove" : "add";
  626. if ($link_mode == "add") {
  627. $query['token'] = drupal_get_token('shortcut-add-link');
  628. $link_text = shortcut_set_switch_access() ? t('Add to %shortcut_set shortcuts', array('%shortcut_set' => $shortcut_set->title)) : t('Add to shortcuts');
  629. $link_path = 'admin/config/user-interface/shortcut/' . $shortcut_set->set_name . '/add-link-inline';
  630. }
  631. else {
  632. $query['mlid'] = $mlid;
  633. $link_text = shortcut_set_switch_access() ? t('Remove from %shortcut_set shortcuts', array('%shortcut_set' => $shortcut_set->title)) : t('Remove from shortcuts');
  634. $link_path = 'admin/config/user-interface/shortcut/link/' . $mlid . '/delete';
  635. }
  636. if (theme_get_setting('shortcut_module_link')) {
  637. $variables['title_suffix']['add_or_remove_shortcut'] = array(
  638. '#attached' => array('css' => array(drupal_get_path('module', 'shortcut') . '/shortcut.css')),
  639. '#prefix' => '<div class="add-or-remove-shortcuts ' . $link_mode . '-shortcut">',
  640. '#type' => 'link',
  641. '#title' => '<span class="icon"></span><span class="text">' . $link_text . '</span>',
  642. '#href' => $link_path,
  643. '#options' => array('query' => $query, 'html' => TRUE),
  644. '#suffix' => '</div>',
  645. );
  646. }
  647. }
  648. }
  649. /**
  650. * Implements hook_page_alter().
  651. */
  652. function shortcut_page_alter(&$page) {
  653. if (isset($page['page_top']['toolbar'])) {
  654. // If the toolbar is available, add a pre-render function to display the
  655. // current shortcuts in the toolbar drawer.
  656. $page['page_top']['toolbar']['#pre_render'][] = 'shortcut_toolbar_pre_render';
  657. }
  658. }
  659. /**
  660. * Pre-render function for adding shortcuts to the toolbar drawer.
  661. */
  662. function shortcut_toolbar_pre_render($toolbar) {
  663. $links = shortcut_renderable_links();
  664. $links['#attached'] = array('css' => array(drupal_get_path('module', 'shortcut') . '/shortcut.css'));
  665. $links['#prefix'] = '<div class="toolbar-shortcuts">';
  666. $links['#suffix'] = '</div>';
  667. $shortcut_set = shortcut_current_displayed_set();
  668. $configure_link = NULL;
  669. if (shortcut_set_edit_access($shortcut_set)) {
  670. $configure_link = array(
  671. '#type' => 'link',
  672. '#title' => t('Edit shortcuts'),
  673. '#href' => 'admin/config/user-interface/shortcut/' . $shortcut_set->set_name,
  674. '#options' => array('attributes' => array('id' => 'edit-shortcuts')),
  675. );
  676. }
  677. $drawer = array(
  678. 'shortcuts' => $links,
  679. 'configure' => $configure_link,
  680. );
  681. $toolbar['toolbar_drawer'][] = $drawer;
  682. return $toolbar;
  683. }
  684. /**
  685. * Returns the sanitized title of a shortcut set.
  686. *
  687. * Deprecated. This function was previously used as a menu item title callback
  688. * but has been replaced by shortcut_set_title_callback() (which does not
  689. * sanitize the title, since the menu system does that automatically). In
  690. * Drupal 7, use that function for title callbacks, and call check_plain()
  691. * directly if you need a sanitized title. In Drupal 8, this function will be
  692. * restored as a title callback and therefore will no longer sanitize its
  693. * output.
  694. *
  695. * @param $shortcut_set
  696. * An object representing the shortcut set, as returned by
  697. * shortcut_set_load().
  698. */
  699. function shortcut_set_title($shortcut_set) {
  700. return check_plain($shortcut_set->title);
  701. }
  702. /**
  703. * Returns the title of a shortcut set.
  704. *
  705. * Title callback for the editing pages for shortcut sets.
  706. *
  707. * @param $shortcut_set
  708. * An object representing the shortcut set, as returned by
  709. * shortcut_set_load().
  710. */
  711. function shortcut_set_title_callback($shortcut_set) {
  712. return $shortcut_set->title;
  713. }