book.admin.inc 8.24 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Administration page callbacks for the Book module.
6 7
 */

8
use Drupal\Component\Utility\Crypt;
9
use Drupal\Core\Entity\EntityInterface;
10
use Drupal\Core\Language\Language;
11

12
/**
13
 * Form constructor for administering a single book's hierarchy.
14
 *
15
 * @param \Drupal\Core\Entity\EntityInterface $node
16 17 18 19 20 21
 *   The node of the top-level page in the book.
 *
 * @see book_menu()
 * @see book_admin_edit_validate()
 * @see book_admin_edit_submit()
 * @ingroup forms
22
 */
23
function book_admin_edit($form, $form_state, EntityInterface $node) {
24
  drupal_set_title($node->label());
25
  $form['#node'] = $node;
26
  _book_admin_table($node, $form);
27 28 29 30
  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save book pages'),
  );
31

32 33 34
  return $form;
}

35
/**
36
 * Form validation handler for book_admin_edit().
37
 *
38 39 40
 * Checks that the book has not been changed while using the form.
 *
 * @see book_admin_edit_submit()
41 42 43 44 45 46 47
 */
function book_admin_edit_validate($form, &$form_state) {
  if ($form_state['values']['tree_hash'] != $form_state['values']['tree_current_hash']) {
    form_set_error('', t('This book has been modified by another user, the changes could not be saved.'));
  }
}

48
/**
49
 * Form submission handler for book_admin_edit().
50
 *
51 52 53
 * This function takes care to save parent menu items before their children.
 * Saving menu items in the incorrect order can break the menu tree.
 *
54
 * @see book_admin_edit_validate()
55
 * @see menu_overview_form_submit()
56 57
 */
function book_admin_edit_submit($form, &$form_state) {
58 59
  // Save elements in the same order as defined in post rather than the form.
  // This ensures parents are updated before their children, preventing orphans.
60
  $order = array_flip(array_keys($form_state['input']['table']));
61 62
  $form['table'] = array_merge($order, $form['table']);

63 64
  // Track updates.
  $updated = FALSE;
65 66 67 68 69 70 71
  foreach (element_children($form['table']) as $key) {
    if ($form['table'][$key]['#item']) {
      $row = $form['table'][$key];
      $values = $form_state['values']['table'][$key];

      // Update menu item if moved.
      if ($row['plid']['#default_value'] != $values['plid'] || $row['weight']['#default_value'] != $values['weight']) {
72 73 74 75 76
        $menu_link = entity_load('menu_link', $values['mlid']);
        $menu_link->weight = $values['weight'];
        $menu_link->plid = $values['plid'];
        $menu_link->save();
        $updated = TRUE;
77
      }
78

79 80
      // Update the title if changed.
      if ($row['title']['#default_value'] != $values['title']) {
81
        $node = node_load($values['nid']);
82
        $langcode = Language::LANGCODE_NOT_SPECIFIED;
83
        $node->title = $values['title'];
84
        $node->book['link_title'] = $values['title'];
85
        $node->setNewRevision();
86
        $node->log = t('Title changed from %original to %current.', array('%original' => $node->title, '%current' => $values['title']));
87

88
        $node->save();
89
        watchdog('content', 'book: updated %title.', array('%title' => $node->label()), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
90
      }
91 92
    }
  }
93 94 95 96 97 98
  if ($updated) {
    // Flush static and cache.
    drupal_static_reset('book_menu_subtree_data');
    $cid = 'links:' . $form['#node']->book['menu_name'] . ':subtree-cid:' . $form['#node']->book['mlid'];
    cache('menu')->delete($cid);
  }
99

100
  drupal_set_message(t('Updated book %title.', array('%title' => $form['#node']->label())));
101 102 103
}

/**
104 105
 * Builds the table portion of the form for the book administration page.
 *
106
 * @param \Drupal\Core\Entity\EntityInterface $node
107 108
 *   The node of the top-level page in the book.
 * @param $form
109
 *   The form that is being modified, passed by reference.
110 111 112
 *
 * @see book_admin_edit()
 */
113
function _book_admin_table(EntityInterface $node, &$form) {
114
  $form['table'] = array(
115 116 117 118 119
    '#theme' => 'book_admin_table',
    '#tree' => TRUE,
  );

  $tree = book_menu_subtree_data($node->book);
120 121
  $tree = array_shift($tree); // Do not include the book item itself.
  if ($tree['below']) {
122
    $hash = Crypt::hashBase64(serialize($tree['below']));
123 124 125 126 127 128 129 130 131 132 133
    // Store the hash value as a hidden form element so that we can detect
    // if another user changed the book hierarchy.
    $form['tree_hash'] = array(
      '#type' => 'hidden',
      '#default_value' => $hash,
    );
    $form['tree_current_hash'] = array(
      '#type' => 'value',
      '#value' => $hash,
    );
    _book_admin_table_tree($tree['below'], $form['table']);
134
  }
135

136 137 138
}

/**
139 140 141 142 143
 * Helps build the main table in the book administration page form.
 *
 * @param $tree
 *   A subtree of the book menu hierarchy.
 * @param $form
144
 *   The form that is being modified, passed by reference.
145 146
 *
 * @return
147
 *   The modified form array.
148 149 150 151
 *
 * @see book_admin_edit()
 */
function _book_admin_table_tree($tree, &$form) {
152 153 154 155
  // The delta must be big enough to give each node a distinct value.
  $count = count($tree);
  $delta = ($count < 30) ? 15 : intval($count / 2) + 1;

156 157
  foreach ($tree as $data) {
    $form['book-admin-' . $data['link']['nid']] = array(
158
      '#item' => $data['link'],
159 160 161 162 163 164 165
      'nid' => array('#type' => 'value', '#value' => $data['link']['nid']),
      'depth' => array('#type' => 'value', '#value' => $data['link']['depth']),
      'href' => array('#type' => 'value', '#value' => $data['link']['href']),
      'title' => array(
        '#type' => 'textfield',
        '#default_value' => $data['link']['link_title'],
        '#maxlength' => 255,
166
        '#size' => 40,
167 168
      ),
      'weight' => array(
169
        '#type' => 'weight',
170
        '#default_value' => $data['link']['weight'],
171
        '#delta' => max($delta, abs($data['link']['weight'])),
172 173
        '#title' => t('Weight for @title', array('@title' => $data['link']['title'])),
        '#title_display' => 'invisible',
174
      ),
175
      'plid' => array(
176
        '#type' => 'hidden',
177 178 179 180 181 182
        '#default_value' => $data['link']['plid'],
      ),
      'mlid' => array(
        '#type' => 'hidden',
        '#default_value' => $data['link']['mlid'],
      ),
183 184 185 186 187 188 189 190 191 192
    );
    if ($data['below']) {
      _book_admin_table_tree($data['below'], $form);
    }
  }

  return $form;
}

/**
193 194
 * Returns HTML for a book administration form.
 *
195
 * @param array $variables
196 197
 *   An associative array containing:
 *   - form: A render element representing the form.
198
 *
199
 * @see book_admin_table()
200
 * @ingroup themeable
201
 */
202 203 204
function theme_book_admin_table($variables) {
  $form = $variables['form'];

205 206 207
  drupal_add_tabledrag('book-outline', 'match', 'parent', 'book-plid', 'book-plid', 'book-mlid', TRUE, MENU_MAX_DEPTH - 2);
  drupal_add_tabledrag('book-outline', 'order', 'sibling', 'book-weight');

208
  $header = array(t('Title'), t('Weight'), t('Parent'), t('Operations'));
209 210 211 212 213 214 215

  $rows = array();
  $destination = drupal_get_destination();
  $access = user_access('administer nodes');
  foreach (element_children($form) as $key) {
    $nid = $form[$key]['nid']['#value'];
    $href = $form[$key]['href']['#value'];
216 217

    // Add special classes to be used with tabledrag.js.
218 219 220
    $form[$key]['plid']['#attributes']['class'] = array('book-plid');
    $form[$key]['mlid']['#attributes']['class'] = array('book-mlid');
    $form[$key]['weight']['#attributes']['class'] = array('book-weight');
221

222
    $indentation = array('#theme' => 'indentation', '#size' => $form[$key]['depth']['#value'] - 2);
223
    $data = array(
224
      drupal_render($indentation) . drupal_render($form[$key]['title']),
225
      drupal_render($form[$key]['weight']),
226
      drupal_render($form[$key]['plid']) . drupal_render($form[$key]['mlid']),
227 228 229
    );
    $links = array();
    $links['view'] = array(
230
      'title' => t('View'),
231 232 233 234
      'href' => $href,
    );
    if ($access) {
      $links['edit'] = array(
235
        'title' => t('Edit'),
236 237 238 239
        'href' => "node/$nid/edit",
        'query' => $destination,
      );
      $links['delete'] = array(
240
        'title' => t('Delete'),
241 242 243 244 245 246 247 248 249
        'href' => "node/$nid/delete",
        'query' => $destination,
      );
    }
    $data[] = array(
      'data' => array(
        '#type' => 'operations',
        '#links' => $links,
      ),
250
    );
251 252 253 254
    $row = array('data' => $data);
    if (isset($form[$key]['#attributes'])) {
      $row = array_merge($row, $form[$key]['#attributes']);
    }
255
    $row['class'][] = 'draggable';
256
    $rows[] = $row;
257
  }
258 259
  $table = array('#theme' => 'table', '#header' => $header, '#rows' => $rows, '#attributes' => array('id' => 'book-outline'), '#empty' => t('No book content available.'));
  return drupal_render($table);
260
}