shortcut.admin.inc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. <?php
  2. /**
  3. * @file
  4. * Administrative page callbacks for the shortcut module.
  5. */
  6. /**
  7. * Returns the maximum number of shortcut "slots" available per shortcut set.
  8. *
  9. * This is used as a limitation in the user interface only.
  10. *
  11. * @return
  12. * The maximum number of shortcuts allowed to be added to a shortcut set.
  13. */
  14. function shortcut_max_slots() {
  15. return variable_get('shortcut_max_slots', 7);
  16. }
  17. /**
  18. * Form callback: builds the form for switching shortcut sets.
  19. *
  20. * @param $form
  21. * An associative array containing the structure of the form.
  22. * @param $form_state
  23. * An associative array containing the current state of the form.
  24. * @param $account
  25. * (optional) The user account whose shortcuts will be switched. Defaults to
  26. * the current logged-in user.
  27. *
  28. * @return
  29. * An array representing the form definition.
  30. *
  31. * @ingroup forms
  32. * @see shortcut_set_switch_validate()
  33. * @see shortcut_set_switch_submit()
  34. */
  35. function shortcut_set_switch($form, &$form_state, $account = NULL) {
  36. global $user;
  37. if (!isset($account)) {
  38. $account = $user;
  39. }
  40. // Prepare the list of shortcut sets.
  41. $sets = shortcut_sets();
  42. $current_set = shortcut_current_displayed_set($account);
  43. $options = array();
  44. foreach ($sets as $name => $set) {
  45. $options[$name] = check_plain($set->title);
  46. }
  47. // Only administrators can add shortcut sets.
  48. $add_access = user_access('administer shortcuts');
  49. if ($add_access) {
  50. $options['new'] = t('New set');
  51. }
  52. if (count($options) > 1) {
  53. $form['account'] = array(
  54. '#type' => 'value',
  55. '#value' => $account,
  56. );
  57. $form['set'] = array(
  58. '#type' => 'radios',
  59. '#title' => $user->uid == $account->uid ? t('Choose a set of shortcuts to use') : t('Choose a set of shortcuts for this user'),
  60. '#options' => $options,
  61. '#default_value' => $current_set->set_name,
  62. );
  63. $form['new'] = array(
  64. '#type' => 'textfield',
  65. '#title' => t('Name'),
  66. '#title_display' => 'invisible',
  67. '#description' => t('The new set is created by copying items from your default shortcut set.'),
  68. '#access' => $add_access,
  69. );
  70. if ($user->uid != $account->uid) {
  71. $default_set = shortcut_default_set($account);
  72. $form['new']['#description'] = t('The new set is created by copying items from the %default set.', array('%default' => $default_set->title));
  73. }
  74. $form['#attached'] = array(
  75. 'css' => array(drupal_get_path('module', 'shortcut') . '/shortcut.admin.css'),
  76. 'js' => array(drupal_get_path('module', 'shortcut') . '/shortcut.admin.js'),
  77. );
  78. $form['actions'] = array('#type' => 'actions');
  79. $form['actions']['submit'] = array(
  80. '#type' => 'submit',
  81. '#value' => t('Change set'),
  82. );
  83. }
  84. else {
  85. // There is only 1 option, so output a message in the $form array.
  86. $form['info'] = array(
  87. '#markup' => '<p>' . t('You are currently using the %set-name shortcut set.', array('%set-name' => $current_set->title)) . '</p>',
  88. );
  89. }
  90. return $form;
  91. }
  92. /**
  93. * Validation handler for shortcut_set_switch().
  94. */
  95. function shortcut_set_switch_validate($form, &$form_state) {
  96. if ($form_state['values']['set'] == 'new') {
  97. // Check to prevent creating a shortcut set with an empty title.
  98. if (trim($form_state['values']['new']) == '') {
  99. form_set_error('new', t('The new set name is required.'));
  100. }
  101. // Check to prevent a duplicate title.
  102. if (shortcut_set_title_exists($form_state['values']['new'])) {
  103. form_set_error('new', t('The shortcut set %name already exists. Choose another name.', array('%name' => $form_state['values']['new'])));
  104. }
  105. }
  106. }
  107. /**
  108. * Submit handler for shortcut_set_switch().
  109. */
  110. function shortcut_set_switch_submit($form, &$form_state) {
  111. global $user;
  112. $account = $form_state['values']['account'];
  113. if ($form_state['values']['set'] == 'new') {
  114. // Save a new shortcut set with links copied from the user's default set.
  115. $default_set = shortcut_default_set($account);
  116. $set = (object) array(
  117. 'title' => $form_state['values']['new'],
  118. 'links' => menu_links_clone($default_set->links),
  119. );
  120. shortcut_set_save($set);
  121. $replacements = array(
  122. '%user' => $account->name,
  123. '%set_name' => $set->title,
  124. '@switch-url' => url(current_path()),
  125. );
  126. if ($account->uid == $user->uid) {
  127. // Only administrators can create new shortcut sets, so we know they have
  128. // access to switch back.
  129. drupal_set_message(t('You are now using the new %set_name shortcut set. You can edit it from this page or <a href="@switch-url">switch back to a different one.</a>', $replacements));
  130. }
  131. else {
  132. drupal_set_message(t('%user is now using a new shortcut set called %set_name. You can edit it from this page.', $replacements));
  133. }
  134. $form_state['redirect'] = 'admin/config/user-interface/shortcut/' . $set->set_name;
  135. }
  136. else {
  137. // Switch to a different shortcut set.
  138. $set = shortcut_set_load($form_state['values']['set']);
  139. $replacements = array(
  140. '%user' => $account->name,
  141. '%set_name' => $set->title,
  142. );
  143. drupal_set_message($account->uid == $user->uid ? t('You are now using the %set_name shortcut set.', $replacements) : t('%user is now using the %set_name shortcut set.', $replacements));
  144. }
  145. // Assign the shortcut set to the provided user account.
  146. shortcut_set_assign_user($set, $account);
  147. }
  148. /**
  149. * Menu page callback: builds the page for administering shortcut sets.
  150. */
  151. function shortcut_set_admin() {
  152. $shortcut_sets = shortcut_sets();
  153. $header = array(t('Name'), array('data' => t('Operations'), 'colspan' => 4));
  154. $rows = array();
  155. foreach ($shortcut_sets as $set) {
  156. $row = array(
  157. check_plain($set->title),
  158. l(t('list links'), "admin/config/user-interface/shortcut/$set->set_name"),
  159. l(t('edit set name'), "admin/config/user-interface/shortcut/$set->set_name/edit"),
  160. );
  161. if (shortcut_set_delete_access($set)) {
  162. $row[] = l(t('delete set'), "admin/config/user-interface/shortcut/$set->set_name/delete");
  163. }
  164. else {
  165. $row[] = '';
  166. }
  167. $rows[] = $row;
  168. }
  169. return theme('table', array('header' => $header, 'rows' => $rows));
  170. }
  171. /**
  172. * Form callback: builds the form for adding a shortcut set.
  173. *
  174. * @param $form
  175. * An associative array containing the structure of the form.
  176. * @param $form_state
  177. * An associative array containing the current state of the form.
  178. *
  179. * @return
  180. * An array representing the form definition.
  181. *
  182. * @ingroup forms
  183. * @see shortcut_set_add_form_validate()
  184. * @see shortcut_set_add_form_submit()
  185. */
  186. function shortcut_set_add_form($form, &$form_state) {
  187. $form['new'] = array(
  188. '#type' => 'textfield',
  189. '#title' => t('Set name'),
  190. '#description' => t('The new set is created by copying items from your default shortcut set.'),
  191. '#required' => TRUE,
  192. );
  193. $form['actions'] = array('#type' => 'actions');
  194. $form['actions']['submit'] = array(
  195. '#type' => 'submit',
  196. '#value' => t('Create new set'),
  197. );
  198. return $form;
  199. }
  200. /**
  201. * Validation handler for shortcut_set_add_form().
  202. */
  203. function shortcut_set_add_form_validate($form, &$form_state) {
  204. // Check to prevent a duplicate title.
  205. if (shortcut_set_title_exists($form_state['values']['new'])) {
  206. form_set_error('new', t('The shortcut set %name already exists. Choose another name.', array('%name' => $form_state['values']['new'])));
  207. }
  208. }
  209. /**
  210. * Submit handler for shortcut_set_add_form().
  211. */
  212. function shortcut_set_add_form_submit($form, &$form_state) {
  213. // Save a new shortcut set with links copied from the user's default set.
  214. $default_set = shortcut_default_set();
  215. $set = (object) array(
  216. 'title' => $form_state['values']['new'],
  217. 'links' => menu_links_clone($default_set->links),
  218. );
  219. shortcut_set_save($set);
  220. drupal_set_message(t('The %set_name shortcut set has been created. You can edit it from this page.', array('%set_name' => $set->title)));
  221. $form_state['redirect'] = 'admin/config/user-interface/shortcut/' . $set->set_name;
  222. }
  223. /**
  224. * Form callback: builds the form for customizing shortcut sets.
  225. *
  226. * @param $form
  227. * An associative array containing the structure of the form.
  228. * @param $form_state
  229. * An associative array containing the current state of the form.
  230. * @param $shortcut_set
  231. * An object representing the shortcut set which is being edited.
  232. *
  233. * @return
  234. * An array representing the form definition.
  235. *
  236. * @ingroup forms
  237. * @see shortcut_set_customize_submit()
  238. */
  239. function shortcut_set_customize($form, &$form_state, $shortcut_set) {
  240. $form['#shortcut_set_name'] = $shortcut_set->set_name;
  241. $form['shortcuts'] = array(
  242. '#tree' => TRUE,
  243. '#weight' => -20,
  244. 'enabled' => array(),
  245. 'disabled' => array(),
  246. );
  247. foreach ($shortcut_set->links as $link) {
  248. $mlid = $link['mlid'];
  249. $status = $link['hidden'] ? 'disabled' : 'enabled';
  250. $form['shortcuts'][$status][$mlid]['name']['#markup'] = l($link['link_title'], $link['link_path']);
  251. $form['shortcuts'][$status][$mlid]['weight'] = array(
  252. '#type' => 'weight',
  253. '#title' => t('Weight'),
  254. '#delta' => 50,
  255. '#default_value' => $link['weight'],
  256. '#attributes' => array('class' => array('shortcut-weight')),
  257. );
  258. $form['shortcuts'][$status][$mlid]['status'] = array(
  259. '#type' => 'select',
  260. '#title' => t('Status'),
  261. '#options' => array('disabled' => t('Disabled'), 'enabled' => t('Enabled')),
  262. '#default_value' => $status,
  263. '#attributes' => array('class' => array('shortcut-status-select')),
  264. );
  265. $form['shortcuts'][$status][$mlid]['edit']['#markup'] = l(t('edit'), 'admin/config/user-interface/shortcut/link/' . $mlid);
  266. $form['shortcuts'][$status][$mlid]['delete']['#markup'] = l(t('delete'), 'admin/config/user-interface/shortcut/link/' . $mlid . '/delete');
  267. }
  268. $form['#attached'] = array(
  269. 'css' => array(drupal_get_path('module', 'shortcut') . '/shortcut.admin.css'),
  270. 'js' => array(drupal_get_path('module', 'shortcut') . '/shortcut.admin.js'),
  271. );
  272. $form['actions'] = array(
  273. '#type' => 'actions',
  274. '#access' => !empty($shortcut_set->links),
  275. );
  276. $form['actions']['submit'] = array(
  277. '#type' => 'submit',
  278. '#value' => t('Save changes'),
  279. );
  280. return $form;
  281. }
  282. /**
  283. * Submit handler for shortcut_set_customize().
  284. */
  285. function shortcut_set_customize_submit($form, &$form_state) {
  286. foreach ($form_state['values']['shortcuts'] as $group => $links) {
  287. foreach ($links as $mlid => $data) {
  288. $link = menu_link_load($mlid);
  289. $link['hidden'] = $data['status'] == 'enabled' ? 0 : 1;
  290. $link['weight'] = $data['weight'];
  291. menu_link_save($link);
  292. }
  293. }
  294. drupal_set_message(t('The shortcut set has been updated.'));
  295. }
  296. /**
  297. * Returns HTML for a shortcut set customization form.
  298. *
  299. * @param $variables
  300. * An associative array containing:
  301. * - form: A render element representing the form.
  302. *
  303. * @see shortcut_set_customize()
  304. * @ingroup themeable
  305. */
  306. function theme_shortcut_set_customize($variables) {
  307. $form = $variables['form'];
  308. $map = array('disabled' => t('Disabled'), 'enabled' => t('Enabled'));
  309. $shortcuts_by_status = array(
  310. 'enabled' => element_children($form['shortcuts']['enabled']),
  311. 'disabled' => element_children($form['shortcuts']['disabled']),
  312. );
  313. // Do not add any rows to the table if there are no shortcuts to display.
  314. $statuses = empty($shortcuts_by_status['enabled']) && empty($shortcuts_by_status['disabled']) ? array() : array_keys($shortcuts_by_status);
  315. $rows = array();
  316. foreach ($statuses as $status) {
  317. drupal_add_tabledrag('shortcuts', 'match', 'sibling', 'shortcut-status-select');
  318. drupal_add_tabledrag('shortcuts', 'order', 'sibling', 'shortcut-weight');
  319. $rows[] = array(
  320. 'data' => array(array(
  321. 'colspan' => 5,
  322. 'data' => '<strong>' . $map[$status] . '</strong>',
  323. )),
  324. 'class' => array('shortcut-status', 'shortcut-status-' . $status),
  325. );
  326. foreach ($shortcuts_by_status[$status] as $key) {
  327. $shortcut = &$form['shortcuts'][$status][$key];
  328. $row = array();
  329. $row[] = drupal_render($shortcut['name']);
  330. $row[] = drupal_render($shortcut['weight']);
  331. $row[] = drupal_render($shortcut['status']);
  332. $row[] = drupal_render($shortcut['edit']);
  333. $row[] = drupal_render($shortcut['delete']);
  334. $rows[] = array(
  335. 'data' => $row,
  336. 'class' => array('draggable'),
  337. );
  338. }
  339. if ($status == 'enabled') {
  340. for ($i = 0; $i < shortcut_max_slots(); $i++) {
  341. $rows['empty-' . $i] = array(
  342. 'data' => array(array(
  343. 'colspan' => 5,
  344. 'data' => '<em>' . t('Empty') . '</em>',
  345. )),
  346. 'class' => array('shortcut-slot-empty'),
  347. );
  348. }
  349. $count_shortcuts = count($shortcuts_by_status[$status]);
  350. if (!empty($count_shortcuts)) {
  351. for ($i = 0; $i < min($count_shortcuts, shortcut_max_slots()); $i++) {
  352. $rows['empty-' . $i]['class'][] = 'shortcut-slot-hidden';
  353. }
  354. }
  355. }
  356. }
  357. $header = array(t('Name'), t('Weight'), t('Status'), array('data' => t('Operations'), 'colspan' => 2));
  358. $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'shortcuts'), 'empty' => t('No shortcuts available. <a href="@link">Add a shortcut</a>.', array('@link' => url('admin/config/user-interface/shortcut/' . $form['#shortcut_set_name'] . '/add-link')))));
  359. $output .= drupal_render($form['actions']);
  360. $output = drupal_render_children($form) . $output;
  361. return $output;
  362. }
  363. /**
  364. * Form callback: builds the form for adding a new shortcut link.
  365. *
  366. * @param $form
  367. * An associative array containing the structure of the form.
  368. * @param $form_state
  369. * An associative array containing the current state of the form.
  370. * @param $shortcut_set
  371. * An object representing the shortcut set to which the link will be added.
  372. *
  373. * @return
  374. * An array representing the form definition.
  375. *
  376. * @ingroup forms
  377. * @see shortcut_link_edit_validate()
  378. * @see shortcut_link_add_submit()
  379. */
  380. function shortcut_link_add($form, &$form_state, $shortcut_set) {
  381. drupal_set_title(t('Add new shortcut'));
  382. $form['shortcut_set'] = array(
  383. '#type' => 'value',
  384. '#value' => $shortcut_set,
  385. );
  386. $form += _shortcut_link_form_elements();
  387. return $form;
  388. }
  389. /**
  390. * Form callback: builds the form for editing a shortcut link.
  391. *
  392. * @param $form
  393. * An associative array containing the structure of the form.
  394. * @param $form_state
  395. * An associative array containing the current state of the form.
  396. * @param $shortcut_link
  397. * An array representing the link that is being edited.
  398. *
  399. * @return
  400. * An array representing the form definition.
  401. *
  402. * @ingroup forms
  403. * @see shortcut_link_edit_validate()
  404. * @see shortcut_link_edit_submit()
  405. */
  406. function shortcut_link_edit($form, &$form_state, $shortcut_link) {
  407. drupal_set_title(t('Editing @shortcut', array('@shortcut' => $shortcut_link['link_title'])));
  408. $form['original_shortcut_link'] = array(
  409. '#type' => 'value',
  410. '#value' => $shortcut_link,
  411. );
  412. $form += _shortcut_link_form_elements($shortcut_link);
  413. return $form;
  414. }
  415. /**
  416. * Helper function for building a form for adding or editing shortcut links.
  417. *
  418. * @param $shortcut_link
  419. * (optional) An array representing the shortcut link that will be edited. If
  420. * not provided, a new link will be created.
  421. *
  422. * @return
  423. * An array of form elements.
  424. */
  425. function _shortcut_link_form_elements($shortcut_link = NULL) {
  426. if (!isset($shortcut_link)) {
  427. $shortcut_link = array(
  428. 'link_title' => '',
  429. 'link_path' => ''
  430. );
  431. }
  432. else {
  433. $shortcut_link['link_path'] = ($shortcut_link['link_path'] == '<front>') ? '' : drupal_get_path_alias($shortcut_link['link_path']);
  434. }
  435. $form['shortcut_link']['#tree'] = TRUE;
  436. $form['shortcut_link']['link_title'] = array(
  437. '#type' => 'textfield',
  438. '#title' => t('Name'),
  439. '#description' => t('The name of the shortcut.'),
  440. '#size' => 40,
  441. '#maxlength' => 255,
  442. '#default_value' => $shortcut_link['link_title'],
  443. '#required' => TRUE,
  444. );
  445. $form['shortcut_link']['link_path'] = array(
  446. '#type' => 'textfield',
  447. '#title' => t('Path'),
  448. '#description' => t('The path to the shortcut.'),
  449. '#size' => 40,
  450. '#maxlength' => 255,
  451. '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
  452. '#default_value' => $shortcut_link['link_path'],
  453. );
  454. $form['#validate'][] = 'shortcut_link_edit_validate';
  455. $form['actions'] = array('#type' => 'actions');
  456. $form['actions']['submit'] = array(
  457. '#type' => 'submit',
  458. '#value' => t('Save'),
  459. );
  460. return $form;
  461. }
  462. /**
  463. * Validation handler for the shortcut link add and edit forms.
  464. */
  465. function shortcut_link_edit_validate($form, &$form_state) {
  466. if (!shortcut_valid_link($form_state['values']['shortcut_link']['link_path'])) {
  467. form_set_error('shortcut_link][link_path', t('The link must correspond to a valid path on the site.'));
  468. }
  469. }
  470. /**
  471. * Submit handler for shortcut_link_edit().
  472. */
  473. function shortcut_link_edit_submit($form, &$form_state) {
  474. // Normalize the path in case it is an alias.
  475. $shortcut_path = drupal_get_normal_path($form_state['values']['shortcut_link']['link_path']);
  476. if (empty($shortcut_path)) {
  477. $shortcut_path = '<front>';
  478. }
  479. $form_state['values']['shortcut_link']['link_path'] = $shortcut_path;
  480. $shortcut_link = array_merge($form_state['values']['original_shortcut_link'], $form_state['values']['shortcut_link']);
  481. menu_link_save($shortcut_link);
  482. $form_state['redirect'] = 'admin/config/user-interface/shortcut/' . $shortcut_link['menu_name'];
  483. drupal_set_message(t('The shortcut %link has been updated.', array('%link' => $shortcut_link['link_title'])));
  484. }
  485. /**
  486. * Submit handler for shortcut_link_add().
  487. */
  488. function shortcut_link_add_submit($form, &$form_state) {
  489. // Add the shortcut link to the set.
  490. $shortcut_set = $form_state['values']['shortcut_set'];
  491. $shortcut_link = $form_state['values']['shortcut_link'];
  492. $shortcut_link['menu_name'] = $shortcut_set->set_name;
  493. shortcut_admin_add_link($shortcut_link, $shortcut_set, shortcut_max_slots());
  494. shortcut_set_save($shortcut_set);
  495. $form_state['redirect'] = 'admin/config/user-interface/shortcut/' . $shortcut_link['menu_name'];
  496. drupal_set_message(t('Added a shortcut for %title.', array('%title' => $shortcut_link['link_title'])));
  497. }
  498. /**
  499. * Adds a link to the end of a shortcut set, keeping within a prescribed limit.
  500. *
  501. * @param $link
  502. * An array representing a shortcut link.
  503. * @param $shortcut_set
  504. * An object representing the shortcut set which the link will be added to.
  505. * The links in the shortcut set will be re-weighted so that the new link is
  506. * at the end, and some existing links may be disabled (if the $limit
  507. * parameter is provided).
  508. * @param $limit
  509. * (optional) The maximum number of links that are allowed to be enabled for
  510. * this shortcut set. If provided, existing links at the end of the list that
  511. * exceed the limit will be automatically disabled. If not provided, no limit
  512. * will be enforced.
  513. */
  514. function shortcut_admin_add_link($shortcut_link, &$shortcut_set, $limit = NULL) {
  515. if (isset($limit)) {
  516. // Disable any existing links at the end of the list that would cause the
  517. // limit to be exceeded. Take into account whether or not the new link will
  518. // be enabled and count towards the total.
  519. $number_enabled = !empty($shortcut_link['hidden']) ? 0 : 1;
  520. foreach ($shortcut_set->links as &$link) {
  521. if (!$link['hidden']) {
  522. $number_enabled++;
  523. if ($number_enabled > $limit) {
  524. $link['hidden'] = 1;
  525. }
  526. }
  527. }
  528. }
  529. // Normalize the path in case it is an alias.
  530. $shortcut_link['link_path'] = drupal_get_normal_path($shortcut_link['link_path']);
  531. if (empty($shortcut_link['link_path'])) {
  532. $shortcut_link['link_path'] = '<front>';
  533. }
  534. // Add the link to the end of the list.
  535. $shortcut_set->links[] = $shortcut_link;
  536. shortcut_set_reset_link_weights($shortcut_set);
  537. }
  538. /**
  539. * Form callback: builds the form for editing the shortcut set name.
  540. *
  541. * @param $form
  542. * An associative array containing the structure of the form.
  543. * @param $form_state
  544. * An associative array containing the current state of the form.
  545. * @param object $shortcut_set
  546. * An object representing the shortcut set, as returned from
  547. * shortcut_set_load().
  548. *
  549. * @return
  550. * An array representing the form definition.
  551. *
  552. * @ingroup forms
  553. * @see shortcut_set_edit_form_validate()
  554. * @see shortcut_set_edit_form_submit()
  555. */
  556. function shortcut_set_edit_form($form, &$form_state, $shortcut_set) {
  557. $form['shortcut_set'] = array(
  558. '#type' => 'value',
  559. '#value' => $shortcut_set,
  560. );
  561. $form['title'] = array(
  562. '#type' => 'textfield',
  563. '#title' => t('Set name'),
  564. '#default_value' => $shortcut_set->title,
  565. '#maxlength' => 255,
  566. '#required' => TRUE,
  567. '#weight' => -5,
  568. );
  569. $form['actions'] = array('#type' => 'actions');
  570. $form['actions']['submit'] = array(
  571. '#type' => 'submit',
  572. '#value' => t('Save'),
  573. '#weight' => 5,
  574. );
  575. return $form;
  576. }
  577. /**
  578. * Validation handler for shortcut_set_edit_form().
  579. */
  580. function shortcut_set_edit_form_validate($form, &$form_state) {
  581. // Check to prevent a duplicate title, if the title was edited from its
  582. // original value.
  583. if ($form_state['values']['title'] != $form_state['values']['shortcut_set']->title && shortcut_set_title_exists($form_state['values']['title'])) {
  584. form_set_error('title', t('The shortcut set %name already exists. Choose another name.', array('%name' => $form_state['values']['title'])));
  585. }
  586. }
  587. /**
  588. * Submit handler for shortcut_set_edit_form().
  589. */
  590. function shortcut_set_edit_form_submit($form, &$form_state) {
  591. $shortcut_set = $form_state['values']['shortcut_set'];
  592. $shortcut_set->title = $form_state['values']['title'];
  593. shortcut_set_save($shortcut_set);
  594. drupal_set_message(t('Updated set name to %set-name.', array('%set-name' => $shortcut_set->title)));
  595. $form_state['redirect'] = "admin/config/user-interface/shortcut/$shortcut_set->set_name";
  596. }
  597. /**
  598. * Form callback: builds the confirmation form for deleting a shortcut set.
  599. *
  600. * @param $form
  601. * An associative array containing the structure of the form.
  602. * @param $form_state
  603. * An associative array containing the current state of the form.
  604. * @param object $shortcut_set
  605. * An object representing the shortcut set, as returned from
  606. * shortcut_set_load().
  607. *
  608. * @return
  609. * An array representing the form definition.
  610. *
  611. * @ingroup forms
  612. * @see shortcut_set_delete_form_submit()
  613. */
  614. function shortcut_set_delete_form($form, &$form_state, $shortcut_set) {
  615. $form['shortcut_set'] = array(
  616. '#type' => 'value',
  617. '#value' => $shortcut_set->set_name,
  618. );
  619. // Find out how many users are directly assigned to this shortcut set, and
  620. // make a message.
  621. $number = db_query('SELECT COUNT(*) FROM {shortcut_set_users} WHERE set_name = :name', array(':name' => $shortcut_set->set_name))->fetchField();
  622. $info = '';
  623. if ($number) {
  624. $info .= '<p>' . format_plural($number,
  625. '1 user has chosen or been assigned to this shortcut set.',
  626. '@count users have chosen or been assigned to this shortcut set.') . '</p>';
  627. }
  628. // Also, if a module implements hook_shortcut_default_set(), it's possible
  629. // that this set is being used as a default set. Add a message about that too.
  630. if (count(module_implements('shortcut_default_set')) > 0) {
  631. $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>';
  632. }
  633. $form['info'] = array(
  634. '#markup' => $info,
  635. );
  636. return confirm_form(
  637. $form,
  638. t('Are you sure you want to delete the shortcut set %title?', array('%title' => $shortcut_set->title)),
  639. 'admin/config/user-interface/shortcut/' . $shortcut_set->set_name,
  640. t('This action cannot be undone.'),
  641. t('Delete'),
  642. t('Cancel')
  643. );
  644. }
  645. /**
  646. * Submit handler for shortcut_set_delete_form().
  647. */
  648. function shortcut_set_delete_form_submit($form, &$form_state) {
  649. $shortcut_set = shortcut_set_load($form_state['values']['shortcut_set']);
  650. shortcut_set_delete($shortcut_set);
  651. $form_state['redirect'] = 'admin/config/user-interface/shortcut';
  652. drupal_set_message(t('The shortcut set %title has been deleted.', array('%title' => $shortcut_set->title)));
  653. }
  654. /**
  655. * Form callback: builds the confirmation form for deleting a shortcut link.
  656. *
  657. * @param $form
  658. * An associative array containing the structure of the form.
  659. * @param $form_state
  660. * An associative array containing the current state of the form.
  661. * @param $shortcut_link
  662. * An array representing the link that will be deleted.
  663. *
  664. * @return
  665. * An array representing the form definition.
  666. *
  667. * @ingroup forms
  668. * @see shortcut_link_delete_submit()
  669. */
  670. function shortcut_link_delete($form, &$form_state, $shortcut_link) {
  671. $form['shortcut_link'] = array(
  672. '#type' => 'value',
  673. '#value' => $shortcut_link,
  674. );
  675. return confirm_form(
  676. $form,
  677. t('Are you sure you want to delete the shortcut %title?', array('%title' => $shortcut_link['link_title'])),
  678. 'admin/config/user-interface/shortcut/' . $shortcut_link['menu_name'],
  679. t('This action cannot be undone.'),
  680. t('Delete'),
  681. t('Cancel')
  682. );
  683. }
  684. /**
  685. * Submit handler for shortcut_link_delete_submit().
  686. */
  687. function shortcut_link_delete_submit($form, &$form_state) {
  688. $shortcut_link = $form_state['values']['shortcut_link'];
  689. menu_link_delete($shortcut_link['mlid']);
  690. $form_state['redirect'] = 'admin/config/user-interface/shortcut/' . $shortcut_link['menu_name'];
  691. drupal_set_message(t('The shortcut %title has been deleted.', array('%title' => $shortcut_link['link_title'])));
  692. }
  693. /**
  694. * Menu page callback: creates a new link in the provided shortcut set.
  695. *
  696. * After completion, redirects the user back to where they came from.
  697. *
  698. * @param $shortcut_set
  699. * Returned from shortcut_set_load().
  700. */
  701. function shortcut_link_add_inline($shortcut_set) {
  702. if (isset($_REQUEST['token']) && drupal_valid_token($_REQUEST['token'], 'shortcut-add-link') && shortcut_valid_link($_GET['link'])) {
  703. $item = menu_get_item($_GET['link']);
  704. $title = ($item && $item['title']) ? $item['title'] : $_GET['name'];
  705. $link = array(
  706. 'link_title' => $title,
  707. 'link_path' => $_GET['link'],
  708. );
  709. shortcut_admin_add_link($link, $shortcut_set, shortcut_max_slots());
  710. if (shortcut_set_save($shortcut_set)) {
  711. drupal_set_message(t('Added a shortcut for %title.', array('%title' => $link['link_title'])));
  712. }
  713. else {
  714. drupal_set_message(t('Unable to add a shortcut for %title.', array('%title' => $link['link_title'])));
  715. }
  716. drupal_goto();
  717. }
  718. return drupal_access_denied();
  719. }