Projet

Général

Profil

Paste
Télécharger (26,3 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / shortcut / shortcut.admin.inc @ c7768a53

1
<?php
2

    
3
/**
4
 * @file
5
 * Administrative page callbacks for the shortcut module.
6
 */
7

    
8
/**
9
 * Returns the maximum number of shortcut "slots" available per shortcut set.
10
 *
11
 * This is used as a limitation in the user interface only.
12
 *
13
 * @return
14
 *   The maximum number of shortcuts allowed to be added to a shortcut set.
15
 */
16
function shortcut_max_slots() {
17
  return variable_get('shortcut_max_slots', 7);
18
}
19

    
20
/**
21
 * Form callback: builds the form for switching shortcut sets.
22
 *
23
 * @param $form
24
 *   An associative array containing the structure of the form.
25
 * @param $form_state
26
 *   An associative array containing the current state of the form.
27
 * @param $account
28
 *   (optional) The user account whose shortcuts will be switched. Defaults to
29
 *   the current logged-in user.
30
 *
31
 * @return
32
 *   An array representing the form definition.
33
 *
34
 * @ingroup forms
35
 * @see shortcut_set_switch_validate()
36
 * @see shortcut_set_switch_submit()
37
 */
38
function shortcut_set_switch($form, &$form_state, $account = NULL) {
39
  global $user;
40
  if (!isset($account)) {
41
    $account = $user;
42
  }
43

    
44
  // Prepare the list of shortcut sets.
45
  $sets = shortcut_sets();
46
  $current_set = shortcut_current_displayed_set($account);
47

    
48
  $options = array();
49
  foreach ($sets as $name => $set) {
50
    $options[$name] = check_plain($set->title);
51
  }
52

    
53
  // Only administrators can add shortcut sets.
54
  $add_access = user_access('administer shortcuts');
55
  if ($add_access) {
56
    $options['new'] = t('New set');
57
  }
58

    
59
  if (count($options) > 1) {
60
    $form['account'] = array(
61
      '#type' => 'value',
62
      '#value' => $account,
63
    );
64

    
65
    $form['set'] = array(
66
      '#type' => 'radios',
67
      '#title' => $user->uid == $account->uid ? t('Choose a set of shortcuts to use') : t('Choose a set of shortcuts for this user'),
68
      '#options' => $options,
69
      '#default_value' => $current_set->set_name,
70
    );
71

    
72
    $form['new'] = array(
73
      '#type' => 'textfield',
74
      '#title' => t('Name'),
75
      '#title_display' => 'invisible',
76
      '#description' => t('The new set is created by copying items from your default shortcut set.'),
77
      '#access' => $add_access,
78
    );
79

    
80
    if ($user->uid != $account->uid) {
81
      $default_set = shortcut_default_set($account);
82
      $form['new']['#description'] = t('The new set is created by copying items from the %default set.', array('%default' => $default_set->title));
83
    }
84

    
85
    $form['#attached'] = array(
86
      'css' => array(drupal_get_path('module', 'shortcut') . '/shortcut.admin.css'),
87
      'js' => array(drupal_get_path('module', 'shortcut') . '/shortcut.admin.js'),
88
    );
89

    
90
    $form['actions'] = array('#type' => 'actions');
91
    $form['actions']['submit'] = array(
92
      '#type' => 'submit',
93
      '#value' => t('Change set'),
94
    );
95
  }
96
  else {
97
    // There is only 1 option, so output a message in the $form array.
98
    $form['info'] = array(
99
      '#markup' => '<p>' . t('You are currently using the %set-name shortcut set.', array('%set-name' => $current_set->title)) . '</p>',
100
    );
101
  }
102

    
103
  return $form;
104
}
105

    
106
/**
107
 * Validation handler for shortcut_set_switch().
108
 */
109
function shortcut_set_switch_validate($form, &$form_state) {
110
  if ($form_state['values']['set'] == 'new') {
111
    // Check to prevent creating a shortcut set with an empty title.
112
    if (trim($form_state['values']['new']) == '') {
113
      form_set_error('new', t('The new set name is required.'));
114
    }
115
    // Check to prevent a duplicate title.
116
    if (shortcut_set_title_exists($form_state['values']['new'])) {
117
      form_set_error('new', t('The shortcut set %name already exists. Choose another name.', array('%name' => $form_state['values']['new'])));
118
    }
119
  }
120
}
121

    
122
/**
123
 * Submit handler for shortcut_set_switch().
124
 */
125
function shortcut_set_switch_submit($form, &$form_state) {
126
  global $user;
127
  $account = $form_state['values']['account'];
128

    
129
  if ($form_state['values']['set'] == 'new') {
130
    // Save a new shortcut set with links copied from the user's default set.
131
    $default_set = shortcut_default_set($account);
132
    $set = (object) array(
133
      'title' => $form_state['values']['new'],
134
      'links' => menu_links_clone($default_set->links),
135
    );
136
    shortcut_set_save($set);
137
    $replacements = array(
138
      '%user' => $account->name,
139
      '%set_name' => $set->title,
140
      '@switch-url' => url(current_path()),
141
    );
142
    if ($account->uid == $user->uid) {
143
      // Only administrators can create new shortcut sets, so we know they have
144
      // access to switch back.
145
      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));
146
    }
147
    else {
148
      drupal_set_message(t('%user is now using a new shortcut set called %set_name. You can edit it from this page.', $replacements));
149
    }
150
    $form_state['redirect'] = 'admin/config/user-interface/shortcut/' . $set->set_name;
151
  }
152
  else {
153
    // Switch to a different shortcut set.
154
    $set = shortcut_set_load($form_state['values']['set']);
155
    $replacements = array(
156
      '%user' => $account->name,
157
      '%set_name' => $set->title,
158
    );
159
    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));
160
  }
161

    
162
  // Assign the shortcut set to the provided user account.
163
  shortcut_set_assign_user($set, $account);
164
}
165

    
166
/**
167
 * Menu page callback: builds the page for administering shortcut sets.
168
 */
169
function shortcut_set_admin() {
170
  $shortcut_sets = shortcut_sets();
171
  $header = array(t('Name'), array('data' => t('Operations'), 'colspan' => 4));
172

    
173
  $rows = array();
174
  foreach ($shortcut_sets as $set) {
175
    $row = array(
176
      check_plain($set->title),
177
      l(t('list links'), "admin/config/user-interface/shortcut/$set->set_name"),
178
      l(t('edit set name'), "admin/config/user-interface/shortcut/$set->set_name/edit"),
179
    );
180
    if (shortcut_set_delete_access($set)) {
181
      $row[] = l(t('delete set'), "admin/config/user-interface/shortcut/$set->set_name/delete");
182
    }
183
    else {
184
      $row[] = '';
185
    }
186

    
187
    $rows[] = $row;
188
  }
189

    
190
  return theme('table', array('header' => $header, 'rows' => $rows));
191
}
192

    
193
/**
194
 * Form callback: builds the form for adding a shortcut set.
195
 *
196
 * @param $form
197
 *   An associative array containing the structure of the form.
198
 * @param $form_state
199
 *   An associative array containing the current state of the form.
200
 *
201
 * @return
202
 *   An array representing the form definition.
203
 *
204
 * @ingroup forms
205
 * @see shortcut_set_add_form_validate()
206
 * @see shortcut_set_add_form_submit()
207
 */
208
function shortcut_set_add_form($form, &$form_state) {
209
  $form['new'] = array(
210
    '#type' => 'textfield',
211
    '#title' => t('Set name'),
212
    '#description' => t('The new set is created by copying items from your default shortcut set.'),
213
    '#required' => TRUE,
214
  );
215

    
216
  $form['actions'] = array('#type' => 'actions');
217
  $form['actions']['submit'] = array(
218
    '#type' => 'submit',
219
    '#value' => t('Create new set'),
220
  );
221

    
222
  return $form;
223
}
224

    
225
/**
226
 * Validation handler for shortcut_set_add_form().
227
 */
228
function shortcut_set_add_form_validate($form, &$form_state) {
229
  // Check to prevent a duplicate title.
230
  if (shortcut_set_title_exists($form_state['values']['new'])) {
231
    form_set_error('new', t('The shortcut set %name already exists. Choose another name.', array('%name' => $form_state['values']['new'])));
232
  }
233
}
234

    
235
/**
236
 * Submit handler for shortcut_set_add_form().
237
 */
238
function shortcut_set_add_form_submit($form, &$form_state) {
239
  // Save a new shortcut set with links copied from the user's default set.
240
  $default_set = shortcut_default_set();
241
  $set = (object) array(
242
    'title' => $form_state['values']['new'],
243
    'links' => menu_links_clone($default_set->links),
244
  );
245
  shortcut_set_save($set);
246
  drupal_set_message(t('The %set_name shortcut set has been created. You can edit it from this page.', array('%set_name' => $set->title)));
247
  $form_state['redirect'] = 'admin/config/user-interface/shortcut/' . $set->set_name;
248
}
249

    
250
/**
251
 * Form callback: builds the form for customizing shortcut sets.
252
 *
253
 * @param $form
254
 *   An associative array containing the structure of the form.
255
 * @param $form_state
256
 *   An associative array containing the current state of the form.
257
 * @param $shortcut_set
258
 *   An object representing the shortcut set which is being edited.
259
 *
260
 * @return
261
 *   An array representing the form definition.
262
 *
263
 * @ingroup forms
264
 * @see shortcut_set_customize_submit()
265
 */
266
function shortcut_set_customize($form, &$form_state, $shortcut_set) {
267
  $form['#shortcut_set_name'] = $shortcut_set->set_name;
268
  $form['shortcuts'] = array(
269
    '#tree' => TRUE,
270
    '#weight' => -20,
271
    'enabled' => array(),
272
    'disabled' => array(),
273
  );
274

    
275
  foreach ($shortcut_set->links as $link) {
276
    $mlid = $link['mlid'];
277
    $status = $link['hidden'] ? 'disabled' : 'enabled';
278
    $form['shortcuts'][$status][$mlid]['name']['#markup'] = l($link['link_title'], $link['link_path']);
279
    $form['shortcuts'][$status][$mlid]['weight'] = array(
280
      '#type' => 'weight',
281
      '#title' => t('Weight'),
282
      '#delta' => 50,
283
      '#default_value' => $link['weight'],
284
      '#attributes' => array('class' => array('shortcut-weight')),
285
    );
286
    $form['shortcuts'][$status][$mlid]['status'] = array(
287
      '#type' => 'select',
288
      '#title' => t('Status'),
289
      '#options' => array('disabled' => t('Disabled'), 'enabled' => t('Enabled')),
290
      '#default_value' => $status,
291
      '#attributes' => array('class' => array('shortcut-status-select')),
292
    );
293

    
294
    $form['shortcuts'][$status][$mlid]['edit']['#markup'] = l(t('edit'), 'admin/config/user-interface/shortcut/link/' . $mlid);
295
    $form['shortcuts'][$status][$mlid]['delete']['#markup'] = l(t('delete'), 'admin/config/user-interface/shortcut/link/' . $mlid . '/delete');
296
  }
297

    
298
  $form['#attached'] = array(
299
    'css' => array(drupal_get_path('module', 'shortcut') . '/shortcut.admin.css'),
300
    'js' => array(drupal_get_path('module', 'shortcut') . '/shortcut.admin.js'),
301
  );
302

    
303
  $form['actions'] = array(
304
    '#type' => 'actions',
305
    '#access' => !empty($shortcut_set->links),
306
  );
307
  $form['actions']['submit'] = array(
308
    '#type' => 'submit',
309
    '#value' => t('Save changes'),
310
  );
311

    
312
  return $form;
313
}
314

    
315
/**
316
 * Submit handler for shortcut_set_customize().
317
 */
318
function shortcut_set_customize_submit($form, &$form_state) {
319
  foreach ($form_state['values']['shortcuts'] as $group => $links) {
320
    foreach ($links as $mlid => $data) {
321
      $link = menu_link_load($mlid);
322
      $link['hidden'] = $data['status'] == 'enabled' ? 0 : 1;
323
      $link['weight'] = $data['weight'];
324
      menu_link_save($link);
325
    }
326
  }
327
  drupal_set_message(t('The shortcut set has been updated.'));
328
}
329

    
330
/**
331
 * Returns HTML for a shortcut set customization form.
332
 *
333
 * @param $variables
334
 *   An associative array containing:
335
 *   - form: A render element representing the form.
336
 *
337
 * @see shortcut_set_customize()
338
 * @ingroup themeable
339
 */
340
function theme_shortcut_set_customize($variables) {
341
  $form = $variables['form'];
342
  $map = array('disabled' => t('Disabled'), 'enabled' => t('Enabled'));
343
  $shortcuts_by_status = array(
344
    'enabled' => element_children($form['shortcuts']['enabled']),
345
    'disabled' => element_children($form['shortcuts']['disabled']),
346
  );
347
  // Do not add any rows to the table if there are no shortcuts to display.
348
  $statuses = empty($shortcuts_by_status['enabled']) && empty($shortcuts_by_status['disabled']) ? array() : array_keys($shortcuts_by_status);
349

    
350
  $rows = array();
351
  foreach ($statuses as $status) {
352
    drupal_add_tabledrag('shortcuts', 'match', 'sibling', 'shortcut-status-select');
353
    drupal_add_tabledrag('shortcuts', 'order', 'sibling', 'shortcut-weight');
354
    $rows[] = array(
355
      'data' => array(array(
356
        'colspan' => 5,
357
        'data' => '<strong>' . $map[$status] . '</strong>',
358
      )),
359
      'class' => array('shortcut-status', 'shortcut-status-' . $status),
360
    );
361

    
362
    foreach ($shortcuts_by_status[$status] as $key) {
363
      $shortcut = &$form['shortcuts'][$status][$key];
364
      $row = array();
365
      $row[] = drupal_render($shortcut['name']);
366
      $row[] = drupal_render($shortcut['weight']);
367
      $row[] = drupal_render($shortcut['status']);
368
      $row[] = drupal_render($shortcut['edit']);
369
      $row[] = drupal_render($shortcut['delete']);
370
      $rows[] = array(
371
        'data' => $row,
372
        'class' => array('draggable'),
373
      );
374
    }
375

    
376
    if ($status == 'enabled') {
377
      for ($i = 0; $i < shortcut_max_slots(); $i++) {
378
        $rows['empty-' . $i] = array(
379
          'data' => array(array(
380
            'colspan' => 5,
381
            'data' => '<em>' . t('Empty') . '</em>',
382
          )),
383
          'class' => array('shortcut-slot-empty'),
384
        );
385
      }
386
      $count_shortcuts = count($shortcuts_by_status[$status]);
387
      if (!empty($count_shortcuts)) {
388
        for ($i = 0; $i < min($count_shortcuts, shortcut_max_slots()); $i++) {
389
          $rows['empty-' . $i]['class'][] = 'shortcut-slot-hidden';
390
        }
391
      }
392
    }
393
  }
394

    
395
  $header = array(t('Name'), t('Weight'), t('Status'), array('data' => t('Operations'), 'colspan' => 2));
396
  $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')))));
397
  $output .= drupal_render($form['actions']);
398
  $output = drupal_render_children($form) . $output;
399
  return $output;
400
}
401

    
402
/**
403
 * Form callback: builds the form for adding a new shortcut link.
404
 *
405
 * @param $form
406
 *   An associative array containing the structure of the form.
407
 * @param $form_state
408
 *   An associative array containing the current state of the form.
409
 * @param $shortcut_set
410
 *   An object representing the shortcut set to which the link will be added.
411
 *
412
 * @return
413
 *   An array representing the form definition.
414
 *
415
 * @ingroup forms
416
 * @see shortcut_link_edit_validate()
417
 * @see shortcut_link_add_submit()
418
 */
419
function shortcut_link_add($form, &$form_state, $shortcut_set) {
420
  drupal_set_title(t('Add new shortcut'));
421
  $form['shortcut_set'] = array(
422
    '#type' => 'value',
423
    '#value' => $shortcut_set,
424
  );
425
  $form += _shortcut_link_form_elements();
426
  return $form;
427
}
428

    
429
/**
430
 * Form callback: builds the form for editing a shortcut link.
431
 *
432
 * @param $form
433
 *   An associative array containing the structure of the form.
434
 * @param $form_state
435
 *   An associative array containing the current state of the form.
436
 * @param $shortcut_link
437
 *   An array representing the link that is being edited.
438
 *
439
 * @return
440
 *   An array representing the form definition.
441
 *
442
 * @ingroup forms
443
 * @see shortcut_link_edit_validate()
444
 * @see shortcut_link_edit_submit()
445
 */
446
function shortcut_link_edit($form, &$form_state, $shortcut_link) {
447
  drupal_set_title(t('Editing @shortcut', array('@shortcut' => $shortcut_link['link_title'])));
448
  $form['original_shortcut_link'] = array(
449
    '#type' => 'value',
450
    '#value' => $shortcut_link,
451
  );
452
  $form += _shortcut_link_form_elements($shortcut_link);
453
  return $form;
454
}
455

    
456
/**
457
 * Helper function for building a form for adding or editing shortcut links.
458
 *
459
 * @param $shortcut_link
460
 *   (optional) An array representing the shortcut link that will be edited. If
461
 *   not provided, a new link will be created.
462
 *
463
 * @return
464
 *   An array of form elements.
465
 */
466
function _shortcut_link_form_elements($shortcut_link = NULL) {
467
  if (!isset($shortcut_link)) {
468
    $shortcut_link = array(
469
      'link_title' => '',
470
      'link_path' => ''
471
    );
472
  }
473
  else {
474
    $shortcut_link['link_path'] = ($shortcut_link['link_path'] == '<front>') ? '' : drupal_get_path_alias($shortcut_link['link_path']);
475
  }
476

    
477
  $form['shortcut_link']['#tree'] = TRUE;
478
  $form['shortcut_link']['link_title'] = array(
479
    '#type' => 'textfield',
480
    '#title' => t('Name'),
481
    '#description' => t('The name of the shortcut.'),
482
    '#size' => 40,
483
    '#maxlength' => 255,
484
    '#default_value' => $shortcut_link['link_title'],
485
    '#required' => TRUE,
486
  );
487

    
488
  $form['shortcut_link']['link_path'] = array(
489
    '#type' => 'textfield',
490
    '#title' => t('Path'),
491
    '#description' => t('The path to the shortcut.'),
492
    '#size' => 40,
493
    '#maxlength' => 255,
494
    '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='),
495
    '#default_value' => $shortcut_link['link_path'],
496
  );
497

    
498
  $form['#validate'][] = 'shortcut_link_edit_validate';
499

    
500
  $form['actions'] = array('#type' => 'actions');
501
  $form['actions']['submit'] = array(
502
    '#type' => 'submit',
503
    '#value' => t('Save'),
504
  );
505

    
506
  return $form;
507
}
508

    
509
/**
510
 * Validation handler for the shortcut link add and edit forms.
511
 */
512
function shortcut_link_edit_validate($form, &$form_state) {
513
  if (!shortcut_valid_link($form_state['values']['shortcut_link']['link_path'])) {
514
    form_set_error('shortcut_link][link_path', t('The link must correspond to a valid path on the site.'));
515
  }
516
}
517

    
518
/**
519
 * Submit handler for shortcut_link_edit().
520
 */
521
function shortcut_link_edit_submit($form, &$form_state) {
522
  // Normalize the path in case it is an alias.
523
  $shortcut_path = drupal_get_normal_path($form_state['values']['shortcut_link']['link_path']);
524
  if (empty($shortcut_path)) {
525
    $shortcut_path = '<front>';
526
  }
527
  $form_state['values']['shortcut_link']['link_path'] = $shortcut_path;
528

    
529
  $shortcut_link = array_merge($form_state['values']['original_shortcut_link'], $form_state['values']['shortcut_link']);
530

    
531
  menu_link_save($shortcut_link);
532
  $form_state['redirect'] = 'admin/config/user-interface/shortcut/' . $shortcut_link['menu_name'];
533
  drupal_set_message(t('The shortcut %link has been updated.', array('%link' => $shortcut_link['link_title'])));
534
}
535

    
536
/**
537
 * Submit handler for shortcut_link_add().
538
 */
539
function shortcut_link_add_submit($form, &$form_state) {
540
  // Add the shortcut link to the set.
541
  $shortcut_set = $form_state['values']['shortcut_set'];
542
  $shortcut_link = $form_state['values']['shortcut_link'];
543
  $shortcut_link['menu_name'] = $shortcut_set->set_name;
544
  shortcut_admin_add_link($shortcut_link, $shortcut_set, shortcut_max_slots());
545
  shortcut_set_save($shortcut_set);
546
  $form_state['redirect'] = 'admin/config/user-interface/shortcut/' . $shortcut_link['menu_name'];
547
  drupal_set_message(t('Added a shortcut for %title.', array('%title' => $shortcut_link['link_title'])));
548
}
549

    
550
/**
551
 * Adds a link to the end of a shortcut set, keeping within a prescribed limit.
552
 *
553
 * @param $link
554
 *   An array representing a shortcut link.
555
 * @param $shortcut_set
556
 *   An object representing the shortcut set which the link will be added to.
557
 *   The links in the shortcut set will be re-weighted so that the new link is
558
 *   at the end, and some existing links may be disabled (if the $limit
559
 *   parameter is provided).
560
 * @param $limit
561
 *   (optional) The maximum number of links that are allowed to be enabled for
562
 *   this shortcut set. If provided, existing links at the end of the list that
563
 *   exceed the limit will be automatically disabled. If not provided, no limit
564
 *   will be enforced.
565
 */
566
function shortcut_admin_add_link($shortcut_link, &$shortcut_set, $limit = NULL) {
567
  if (isset($limit)) {
568
    // Disable any existing links at the end of the list that would cause the
569
    // limit to be exceeded. Take into account whether or not the new link will
570
    // be enabled and count towards the total.
571
    $number_enabled = !empty($shortcut_link['hidden']) ? 0 : 1;
572
    foreach ($shortcut_set->links as &$link) {
573
      if (!$link['hidden']) {
574
        $number_enabled++;
575
        if ($number_enabled > $limit) {
576
          $link['hidden'] = 1;
577
        }
578
      }
579
    }
580
  }
581

    
582
  // Normalize the path in case it is an alias.
583
  $shortcut_link['link_path'] = drupal_get_normal_path($shortcut_link['link_path']);
584
  if (empty($shortcut_link['link_path'])) {
585
    $shortcut_link['link_path'] = '<front>';
586
  }
587

    
588
  // Add the link to the end of the list.
589
  $shortcut_set->links[] = $shortcut_link;
590
  shortcut_set_reset_link_weights($shortcut_set);
591
}
592

    
593
/**
594
 * Form callback: builds the form for editing the shortcut set name.
595
 *
596
 * @param $form
597
 *   An associative array containing the structure of the form.
598
 * @param $form_state
599
 *   An associative array containing the current state of the form.
600
 * @param object $shortcut_set
601
 *   An object representing the shortcut set, as returned from
602
 *   shortcut_set_load().
603
 *
604
 * @return
605
 *   An array representing the form definition.
606
 *
607
 * @ingroup forms
608
 * @see shortcut_set_edit_form_validate()
609
 * @see shortcut_set_edit_form_submit()
610
 */
611
function shortcut_set_edit_form($form, &$form_state, $shortcut_set) {
612
  $form['shortcut_set'] = array(
613
    '#type' => 'value',
614
    '#value' => $shortcut_set,
615
  );
616
  $form['title'] = array(
617
    '#type' => 'textfield',
618
    '#title' => t('Set name'),
619
    '#default_value' => $shortcut_set->title,
620
    '#maxlength' => 255,
621
    '#required' => TRUE,
622
    '#weight' => -5,
623
  );
624
  $form['actions'] = array('#type' => 'actions');
625
  $form['actions']['submit'] = array(
626
    '#type' => 'submit',
627
    '#value' => t('Save'),
628
    '#weight' => 5,
629
  );
630

    
631
  return $form;
632
}
633

    
634
/**
635
 * Validation handler for shortcut_set_edit_form().
636
 */
637
function shortcut_set_edit_form_validate($form, &$form_state) {
638
  // Check to prevent a duplicate title, if the title was edited from its
639
  // original value.
640
  if ($form_state['values']['title'] != $form_state['values']['shortcut_set']->title && shortcut_set_title_exists($form_state['values']['title'])) {
641
    form_set_error('title', t('The shortcut set %name already exists. Choose another name.', array('%name' => $form_state['values']['title'])));
642
  }
643
}
644

    
645
/**
646
 * Submit handler for shortcut_set_edit_form().
647
 */
648
function shortcut_set_edit_form_submit($form, &$form_state) {
649
  $shortcut_set = $form_state['values']['shortcut_set'];
650
  $shortcut_set->title = $form_state['values']['title'];
651
  shortcut_set_save($shortcut_set);
652
  drupal_set_message(t('Updated set name to %set-name.', array('%set-name' => $shortcut_set->title)));
653
  $form_state['redirect'] = "admin/config/user-interface/shortcut/$shortcut_set->set_name";
654
}
655

    
656
/**
657
 * Form callback: builds the confirmation form for deleting a shortcut set.
658
 *
659
 * @param $form
660
 *   An associative array containing the structure of the form.
661
 * @param $form_state
662
 *   An associative array containing the current state of the form.
663
 * @param object $shortcut_set
664
 *   An object representing the shortcut set, as returned from
665
 *   shortcut_set_load().
666
 *
667
 * @return
668
 *   An array representing the form definition.
669
 *
670
 * @ingroup forms
671
 * @see shortcut_set_delete_form_submit()
672
 */
673
function shortcut_set_delete_form($form, &$form_state, $shortcut_set) {
674
  $form['shortcut_set'] = array(
675
    '#type' => 'value',
676
    '#value' => $shortcut_set->set_name,
677
  );
678

    
679
  // Find out how many users are directly assigned to this shortcut set, and
680
  // make a message.
681
  $number = db_query('SELECT COUNT(*) FROM {shortcut_set_users} WHERE set_name = :name', array(':name' => $shortcut_set->set_name))->fetchField();
682
  $info = '';
683
  if ($number) {
684
    $info .= '<p>' . format_plural($number,
685
      '1 user has chosen or been assigned to this shortcut set.',
686
      '@count users have chosen or been assigned to this shortcut set.') . '</p>';
687
  }
688

    
689
  // Also, if a module implements hook_shortcut_default_set(), it's possible
690
  // that this set is being used as a default set. Add a message about that too.
691
  if (count(module_implements('shortcut_default_set')) > 0) {
692
    $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>';
693
  }
694

    
695
  $form['info'] = array(
696
    '#markup' => $info,
697
  );
698

    
699
  return confirm_form(
700
    $form,
701
    t('Are you sure you want to delete the shortcut set %title?', array('%title' => $shortcut_set->title)),
702
    'admin/config/user-interface/shortcut/' . $shortcut_set->set_name,
703
    t('This action cannot be undone.'),
704
    t('Delete'),
705
    t('Cancel')
706
  );
707
}
708

    
709
/**
710
 * Submit handler for shortcut_set_delete_form().
711
 */
712
function shortcut_set_delete_form_submit($form, &$form_state) {
713
  $shortcut_set = shortcut_set_load($form_state['values']['shortcut_set']);
714
  shortcut_set_delete($shortcut_set);
715
  $form_state['redirect'] = 'admin/config/user-interface/shortcut';
716
  drupal_set_message(t('The shortcut set %title has been deleted.', array('%title' => $shortcut_set->title)));
717
}
718

    
719
/**
720
 * Form callback: builds the confirmation form for deleting a shortcut link.
721
 *
722
 * @param $form
723
 *   An associative array containing the structure of the form.
724
 * @param $form_state
725
 *   An associative array containing the current state of the form.
726
 * @param $shortcut_link
727
 *   An array representing the link that will be deleted.
728
 *
729
 * @return
730
 *   An array representing the form definition.
731
 *
732
 * @ingroup forms
733
 * @see shortcut_link_delete_submit()
734
 */
735
function shortcut_link_delete($form, &$form_state, $shortcut_link) {
736
  $form['shortcut_link'] = array(
737
    '#type' => 'value',
738
    '#value' => $shortcut_link,
739
  );
740

    
741
  return confirm_form(
742
    $form,
743
    t('Are you sure you want to delete the shortcut %title?', array('%title' => $shortcut_link['link_title'])),
744
    'admin/config/user-interface/shortcut/' . $shortcut_link['menu_name'],
745
    t('This action cannot be undone.'),
746
    t('Delete'),
747
    t('Cancel')
748
  );
749
}
750

    
751
/**
752
 * Submit handler for shortcut_link_delete_submit().
753
 */
754
function shortcut_link_delete_submit($form, &$form_state) {
755
  $shortcut_link = $form_state['values']['shortcut_link'];
756
  menu_link_delete($shortcut_link['mlid']);
757
  $form_state['redirect'] = 'admin/config/user-interface/shortcut/' . $shortcut_link['menu_name'];
758
  drupal_set_message(t('The shortcut %title has been deleted.', array('%title' => $shortcut_link['link_title'])));
759
}
760

    
761
/**
762
 * Menu page callback: creates a new link in the provided shortcut set.
763
 *
764
 * After completion, redirects the user back to where they came from.
765
 *
766
 * @param $shortcut_set
767
 *   Returned from shortcut_set_load().
768
 */
769
function shortcut_link_add_inline($shortcut_set) {
770
  if (isset($_REQUEST['token']) && drupal_valid_token($_REQUEST['token'], 'shortcut-add-link') && shortcut_valid_link($_GET['link'])) {
771
    $item = menu_get_item($_GET['link']);
772
    $title = ($item && $item['title']) ? $item['title'] : $_GET['name'];
773
    $link = array(
774
      'link_title' => $title,
775
      'link_path' => $_GET['link'],
776
    );
777
    shortcut_admin_add_link($link, $shortcut_set, shortcut_max_slots());
778
    if (shortcut_set_save($shortcut_set)) {
779
      drupal_set_message(t('Added a shortcut for %title.', array('%title' => $link['link_title'])));
780
    }
781
    else {
782
      drupal_set_message(t('Unable to add a shortcut for %title.', array('%title' => $link['link_title'])));
783
    }
784
    drupal_goto();
785
  }
786

    
787
  return MENU_ACCESS_DENIED;
788
}