shortcut.admin.inc 16.2 KB
Newer Older
1 2 3 4 5 6 7
<?php

/**
 * @file
 * Administrative page callbacks for the shortcut module.
 */

8 9
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

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

  // Prepare the list of shortcut sets.
35
  $sets = entity_load_multiple('shortcut');
36
  $current_set = shortcut_current_displayed_set($account);
37

38 39
  $options = array();
  foreach ($sets as $name => $set) {
40
    $options[$name] = check_plain($set->label());
41 42 43 44 45 46 47 48
  }

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

49 50 51 52 53
  if (count($options) > 1) {
    $form['account'] = array(
      '#type' => 'value',
      '#value' => $account,
    );
54

55 56 57 58
    $form['set'] = array(
      '#type' => 'radios',
      '#title' => $user->uid == $account->uid ? t('Choose a set of shortcuts to use') : t('Choose a set of shortcuts for this user'),
      '#options' => $options,
59
      '#default_value' => $current_set->id(),
60
    );
61

62
    $form['label'] = array(
63
      '#type' => 'textfield',
64
      '#title' => t('Label'),
65
      '#title_display' => 'invisible',
66 67 68
      '#description' => t('The new set is created by copying items from your default shortcut set.'),
      '#access' => $add_access,
    );
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    $form['id'] = array(
      '#type' => 'machine_name',
      '#machine_name' => array(
        'exists' => 'shortcut_set_load',
        'source' => array('label'),
        'replace_pattern' => '[^a-z0-9-]+',
        'replace' => '-',
      ),
      // This id could be used for menu name.
      '#maxlength' => 23,
      '#states' => array(
        'required' => array(
          ':input[name="set"]' => array('value' => 'new'),
        ),
      ),
      '#required' => FALSE,
    );
86

87 88
    if ($user->uid != $account->uid) {
      $default_set = shortcut_default_set($account);
89
      $form['new']['#description'] = t('The new set is created by copying items from the %default set.', array('%default' => $default_set->label()));
90
    }
91

92
    $form['#attached'] = array(
93
      'library' => array(array('shortcut', 'drupal.shortcut.admin')),
94 95
    );

96
    $form['actions'] = array('#type' => 'actions');
97 98 99 100 101 102 103 104
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Change set'),
    );
  }
  else {
    // There is only 1 option, so output a message in the $form array.
    $form['info'] = array(
105
      '#markup' => '<p>' . t('You are currently using the %set-name shortcut set.', array('%set-name' => $current_set->label())) . '</p>',
106 107
    );
  }
108 109 110 111

  return $form;
}

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

128
/**
129
 * Submit handler for shortcut_set_switch().
130 131 132 133 134 135
 */
function shortcut_set_switch_submit($form, &$form_state) {
  global $user;
  $account = $form_state['values']['account'];

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

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

173
/**
174
 * Page callback: provides the shortcut set creation form.
175
 */
176 177 178
function shortcut_set_add() {
  $entity = entity_create('shortcut', array());
  return entity_get_form($entity);
179 180 181 182
}

/**
 * Form callback: builds the form for customizing shortcut sets.
183 184 185 186 187
 *
 * @param $form
 *   An associative array containing the structure of the form.
 * @param $form_state
 *   An associative array containing the current state of the form.
188
 * @param $shortcut_set Drupal\shortcut\Plugin\Core\Entity\Shortcut
189
 *   An object representing the shortcut set which is being edited.
190
 *
191 192 193 194 195 196 197
 * @return
 *   An array representing the form definition.
 *
 * @ingroup forms
 * @see shortcut_set_customize_submit()
 */
function shortcut_set_customize($form, &$form_state, $shortcut_set) {
198
  $form['#shortcut_set_name'] = $shortcut_set->id();
199 200 201
  $form['shortcuts'] = array(
    '#tree' => TRUE,
    '#weight' => -20,
202
    'links' => array(),
203 204
  );

205
  foreach ($shortcut_set->links as $uuid => $link) {
206
    $mlid = $link['mlid'];
207 208
    $form['shortcuts']['links'][$mlid]['name']['#markup'] = l($link['link_title'], $link['link_path']);
    $form['shortcuts']['links'][$mlid]['weight'] = array(
209
      '#type' => 'weight',
210 211
      '#title' => t('Weight for @title', array('@title' => $link['link_title'])),
      '#title_display' => 'invisible',
212 213 214 215
      '#delta' => 50,
      '#default_value' => $link['weight'],
      '#attributes' => array('class' => array('shortcut-weight')),
    );
216

217
    $links['edit'] = array(
218
      'title' => t('Edit'),
219 220 221
      'href' => "admin/config/user-interface/shortcut/link/$mlid",
    );
    $links['delete'] = array(
222
      'title' => t('Delete'),
223 224 225 226 227 228
      'href' => "admin/config/user-interface/shortcut/link/$mlid/delete",
    );
    $form['shortcuts']['links'][$mlid]['operations'] = array(
      '#type' => 'operations',
      '#links' => $links,
    );
229 230
  }

231 232 233 234
  $form['actions'] = array(
    '#type' => 'actions',
    '#access' => !empty($shortcut_set->links),
  );
235
  $form['actions']['submit'] = array(
236
    '#type' => 'submit',
237
    '#value' => t('Save changes'),
238 239 240 241 242 243
  );

  return $form;
}

/**
244
 * Submit handler for shortcut_set_customize().
245 246
 */
function shortcut_set_customize_submit($form, &$form_state) {
247 248 249 250
  foreach ($form_state['values']['shortcuts']['links'] as $mlid => $data) {
    $link = menu_link_load($mlid);
    $link['weight'] = $data['weight'];
    menu_link_save($link);
251 252 253 254 255
  }
  drupal_set_message(t('The shortcut set has been updated.'));
}

/**
256
 * Returns HTML for a shortcut set customization form.
257 258 259
 *
 * @param $variables
 *   An associative array containing:
260
 *   - form: A render element representing the form.
261
 *
262
 * @see shortcut_set_customize()
263
 * @ingroup themeable
264 265 266
 */
function theme_shortcut_set_customize($variables) {
  $form = $variables['form'];
267

268 269
  // Do not add any rows to the table if there are no shortcuts to display.
  $statuses = empty($shortcuts_by_status['enabled']) && empty($shortcuts_by_status['disabled']) ? array() : array_keys($shortcuts_by_status);
270 271

  $rows = array();
272 273 274 275 276 277
  drupal_add_tabledrag('shortcuts', 'order', 'sibling', 'shortcut-weight');
  foreach (element_children($form['shortcuts']['links']) as $key) {
    $shortcut = &$form['shortcuts']['links'][$key];
    $row = array();
    $row[] = drupal_render($shortcut['name']);
    $row[] = drupal_render($shortcut['weight']);
278
    $row[] = drupal_render($shortcut['operations']);
279
    $rows[] = array(
280 281
      'data' => $row,
      'class' => array('draggable'),
282 283
    );
  }
284

285
  $header = array(t('Name'), t('Weight'), t('Operations'));
286
  $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')))));
287
  $output .= drupal_render($form['actions']);
288 289 290 291 292
  $output = drupal_render_children($form) . $output;
  return $output;
}

/**
293
 * Form callback: builds the form for adding a new shortcut link.
294 295 296 297 298
 *
 * @param $form
 *   An associative array containing the structure of the form.
 * @param $form_state
 *   An associative array containing the current state of the form.
299
 * @param $shortcut_set Drupal\shortcut\Plugin\Core\Entity\Shortcut
300
 *   An object representing the shortcut set to which the link will be added.
301
 *
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
 * @return
 *   An array representing the form definition.
 *
 * @ingroup forms
 * @see shortcut_link_edit_validate()
 * @see shortcut_link_add_submit()
 */
function shortcut_link_add($form, &$form_state, $shortcut_set) {
  drupal_set_title(t('Add new shortcut'));
  $form['shortcut_set'] = array(
    '#type' => 'value',
    '#value' => $shortcut_set,
  );
  $form += _shortcut_link_form_elements();
  return $form;
}

/**
320
 * Form callback: builds the form for editing a shortcut link.
321 322 323 324 325 326 327
 *
 * @param $form
 *   An associative array containing the structure of the form.
 * @param $form_state
 *   An associative array containing the current state of the form.
 * @param $shortcut_link
 *   An array representing the link that is being edited.
328
 *
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
 * @return
 *   An array representing the form definition.
 *
 * @ingroup forms
 * @see shortcut_link_edit_validate()
 * @see shortcut_link_edit_submit()
 */
function shortcut_link_edit($form, &$form_state, $shortcut_link) {
  drupal_set_title(t('Editing @shortcut', array('@shortcut' => $shortcut_link['link_title'])));
  $form['original_shortcut_link'] = array(
    '#type' => 'value',
    '#value' => $shortcut_link,
  );
  $form += _shortcut_link_form_elements($shortcut_link);
  return $form;
}

/**
 * Helper function for building a form for adding or editing shortcut links.
 *
 * @param $shortcut_link
 *   (optional) An array representing the shortcut link that will be edited. If
 *   not provided, a new link will be created.
352
 *
353 354 355 356 357
 * @return
 *   An array of form elements.
 */
function _shortcut_link_form_elements($shortcut_link = NULL) {
  if (!isset($shortcut_link)) {
358
    $shortcut_link = entity_create('menu_link', array(
359 360
      'link_title' => '',
      'link_path' => ''
361
    ));
362
  }
363
  else {
364
    $shortcut_link['link_path'] = ($shortcut_link['link_path'] == '<front>') ? '' : drupal_container()->get('path.alias_manager')->getPathAlias($shortcut_link['link_path']);
365
  }
366 367 368 369 370 371 372 373

  $form['shortcut_link']['#tree'] = TRUE;
  $form['shortcut_link']['link_title'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#size' => 40,
    '#maxlength' => 255,
    '#default_value' => $shortcut_link['link_title'],
374
    '#required' => TRUE,
375 376 377 378 379 380 381
  );

  $form['shortcut_link']['link_path'] = array(
    '#type' => 'textfield',
    '#title' => t('Path'),
    '#size' => 40,
    '#maxlength' => 255,
382
    '#field_prefix' => url(NULL, array('absolute' => TRUE)),
383 384 385 386 387
    '#default_value' => $shortcut_link['link_path'],
  );

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

388
  $form['actions'] = array('#type' => 'actions');
389
  $form['actions']['submit'] = array(
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
    '#type' => 'submit',
    '#value' => t('Save'),
  );

  return $form;
}

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

/**
407
 * Submit handler for shortcut_link_edit().
408 409
 */
function shortcut_link_edit_submit($form, &$form_state) {
410
  // Normalize the path in case it is an alias.
411
  $shortcut_path = drupal_container()->get('path.alias_manager')->getSystemPath($form_state['values']['shortcut_link']['link_path']);
412 413 414 415
  if (empty($shortcut_path)) {
    $shortcut_path = '<front>';
  }
  $form_state['values']['shortcut_link']['link_path'] = $shortcut_path;
416

417 418 419 420
  $shortcut_link = $form_state['values']['original_shortcut_link'];
  foreach ($form_state['values']['shortcut_link'] as $key => $value) {
   $shortcut_link[$key] = $value;
  }
421

422
  menu_link_save($shortcut_link);
423 424
  $set_name = str_replace('shortcut-', '' , $shortcut_link['menu_name']);
  $form_state['redirect'] = 'admin/config/user-interface/shortcut/manage/' . $set_name;
425 426 427 428
  drupal_set_message(t('The shortcut %link has been updated.', array('%link' => $shortcut_link['link_title'])));
}

/**
429
 * Submit handler for shortcut_link_add().
430 431 432 433 434
 */
function shortcut_link_add_submit($form, &$form_state) {
  // Add the shortcut link to the set.
  $shortcut_set = $form_state['values']['shortcut_set'];
  $shortcut_link = $form_state['values']['shortcut_link'];
435
  $shortcut_link['menu_name'] = $shortcut_set->id();
436
  shortcut_admin_add_link($shortcut_link, $shortcut_set);
437 438
  $shortcut_set->save();
  $form_state['redirect'] = 'admin/config/user-interface/shortcut/manage/' . $shortcut_set->id();
439 440 441 442
  drupal_set_message(t('Added a shortcut for %title.', array('%title' => $shortcut_link['link_title'])));
}

/**
443
 * Adds a link to the end of a shortcut set, keeping within a prescribed limit.
444 445 446
 *
 * @param $link
 *   An array representing a shortcut link.
447
 * @param $shortcut_set Drupal\shortcut\Plugin\Core\Entity\Shortcut
448 449 450 451 452
 *   An object representing the shortcut set which the link will be added to.
 *   The links in the shortcut set will be re-weighted so that the new link is
 *   at the end, and some existing links may be disabled (if the $limit
 *   parameter is provided).
 */
453
function shortcut_admin_add_link($shortcut_link, &$shortcut_set) {
454
  // Normalize the path in case it is an alias.
455
  $shortcut_link['link_path'] = drupal_container()->get('path.alias_manager')->getSystemPath($shortcut_link['link_path']);
456 457 458
  if (empty($shortcut_link['link_path'])) {
    $shortcut_link['link_path'] = '<front>';
  }
459 460
  $menu_link = entity_create('menu_link', $shortcut_link);
  $menu_link->save();
461

462
  // Add the link to the end of the list.
463
  $shortcut_set->links[$menu_link->uuid()] = $menu_link;
464 465 466 467
  shortcut_set_reset_link_weights($shortcut_set);
}

/**
468
 * Menu page callback: creates a new link in the provided shortcut set.
469 470 471
 *
 * After completion, redirects the user back to where they came from.
 *
472
 * @param $shortcut_set Drupal\shortcut\Plugin\Core\Entity\Shortcut
473 474 475 476
 *   Returned from shortcut_set_load().
 */
function shortcut_link_add_inline($shortcut_set) {
  if (isset($_REQUEST['token']) && drupal_valid_token($_REQUEST['token'], 'shortcut-add-link') && shortcut_valid_link($_GET['link'])) {
477 478
    $item = menu_get_item($_GET['link']);
    $title = ($item && $item['title']) ? $item['title'] : $_GET['name'];
479
    $link = array(
480
      'link_title' => $title,
481 482
      'link_path' => $_GET['link'],
    );
483
    shortcut_admin_add_link($link, $shortcut_set);
484
    if ($shortcut_set->save() == SAVED_UPDATED) {
485 486 487 488 489 490 491
      drupal_set_message(t('Added a shortcut for %title.', array('%title' => $link['link_title'])));
    }
    else {
      drupal_set_message(t('Unable to add a shortcut for %title.', array('%title' => $link['link_title'])));
    }
    drupal_goto();
  }
492

493
  throw new AccessDeniedHttpException();
494
}