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

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

8
use Drupal\Core\Entity\EntityInterface;
9

10
/**
11
12
 * Page callback: Returns an administrative overview of all books.
 *
13
14
15
 * @return string
 *   A HTML-formatted string with the administrative page content.
 *
16
 * @see book_menu()
17
18
19
 */
function book_admin_overview() {
  $rows = array();
20
21
22
23

  $headers = array(t('Book'), t('Operations'));

  // Add any recognized books to the table list.
24
  foreach (book_get_books() as $book) {
25
26
27
28
29
    $row = array(
      l($book['title'], $book['href'], $book['options']),
    );
    $links = array();
    $links['edit'] = array(
30
      'title' => t('Edit order and titles'),
31
32
33
34
35
36
37
38
39
      'href' => 'admin/content/book/' . $book['nid'],
    );
    $row[] = array(
      'data' => array(
        '#type' => 'operations',
        '#links' => $links,
      ),
    );
    $rows[] = $row;
40
41
  }

42
  return theme('table', array('header' => $headers, 'rows' => $rows, 'empty' => t('No books available.')));
43
44
45
}

/**
46
 * Form constructor for administering a single book's hierarchy.
47
 *
48
 * @param \Drupal\Core\Entity\EntityInterface $node
49
50
51
52
53
54
 *   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
55
 */
56
function book_admin_edit($form, $form_state, EntityInterface $node) {
57
  drupal_set_title($node->label());
58
  $form['#node'] = $node;
59
  _book_admin_table($node, $form);
60
61
62
63
  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save book pages'),
  );
64

65
66
67
  return $form;
}

68
/**
69
 * Form validation handler for book_admin_edit().
70
 *
71
72
73
 * Checks that the book has not been changed while using the form.
 *
 * @see book_admin_edit_submit()
74
75
76
77
78
79
80
 */
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.'));
  }
}

81
/**
82
 * Form submission handler for book_admin_edit().
83
 *
84
85
86
 * This function takes care to save parent menu items before their children.
 * Saving menu items in the incorrect order can break the menu tree.
 *
87
 * @see book_admin_edit_validate()
88
 * @see menu_overview_form_submit()
89
90
 */
function book_admin_edit_submit($form, &$form_state) {
91
92
  // Save elements in the same order as defined in post rather than the form.
  // This ensures parents are updated before their children, preventing orphans.
93
  $order = array_flip(array_keys($form_state['input']['table']));
94
95
  $form['table'] = array_merge($order, $form['table']);

96
97
  // Track updates.
  $updated = FALSE;
98
99
100
101
102
103
104
  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']) {
105
106
107
108
109
        $menu_link = entity_load('menu_link', $values['mlid']);
        $menu_link->weight = $values['weight'];
        $menu_link->plid = $values['plid'];
        $menu_link->save();
        $updated = TRUE;
110
      }
111

112
113
      // Update the title if changed.
      if ($row['title']['#default_value'] != $values['title']) {
114
        $node = node_load($values['nid']);
115
        $langcode = LANGUAGE_NOT_SPECIFIED;
116
        $node->title = $values['title'];
117
        $node->book['link_title'] = $values['title'];
118
        $node->setNewRevision();
119
        $node->log = t('Title changed from %original to %current.', array('%original' => $node->title, '%current' => $values['title']));
120

121
        $node->save();
122
        watchdog('content', 'book: updated %title.', array('%title' => $node->label()), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
123
      }
124
125
    }
  }
126
127
128
129
130
131
  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);
  }
132

133
  drupal_set_message(t('Updated book %title.', array('%title' => $form['#node']->label())));
134
135
136
}

/**
137
138
 * Builds the table portion of the form for the book administration page.
 *
139
 * @param \Drupal\Core\Entity\EntityInterface $node
140
141
 *   The node of the top-level page in the book.
 * @param $form
142
 *   The form that is being modified, passed by reference.
143
144
145
 *
 * @see book_admin_edit()
 */
146
function _book_admin_table(EntityInterface $node, &$form) {
147
  $form['table'] = array(
148
149
150
151
152
    '#theme' => 'book_admin_table',
    '#tree' => TRUE,
  );

  $tree = book_menu_subtree_data($node->book);
153
154
  $tree = array_shift($tree); // Do not include the book item itself.
  if ($tree['below']) {
155
    $hash = drupal_hash_base64(serialize($tree['below']));
156
157
158
159
160
161
162
163
164
165
166
    // 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']);
167
  }
168

169
170
171
}

/**
172
173
174
175
176
 * Helps build the main table in the book administration page form.
 *
 * @param $tree
 *   A subtree of the book menu hierarchy.
 * @param $form
177
 *   The form that is being modified, passed by reference.
178
179
 *
 * @return
180
 *   The modified form array.
181
182
183
184
 *
 * @see book_admin_edit()
 */
function _book_admin_table_tree($tree, &$form) {
185
186
187
188
  // The delta must be big enough to give each node a distinct value.
  $count = count($tree);
  $delta = ($count < 30) ? 15 : intval($count / 2) + 1;

189
190
  foreach ($tree as $data) {
    $form['book-admin-' . $data['link']['nid']] = array(
191
      '#item' => $data['link'],
192
193
194
195
196
197
198
      '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,
199
        '#size' => 40,
200
201
      ),
      'weight' => array(
202
        '#type' => 'weight',
203
        '#default_value' => $data['link']['weight'],
204
        '#delta' => max($delta, abs($data['link']['weight'])),
205
206
        '#title' => t('Weight for @title', array('@title' => $data['link']['title'])),
        '#title_display' => 'invisible',
207
      ),
208
      'plid' => array(
209
        '#type' => 'hidden',
210
211
212
213
214
215
        '#default_value' => $data['link']['plid'],
      ),
      'mlid' => array(
        '#type' => 'hidden',
        '#default_value' => $data['link']['mlid'],
      ),
216
217
218
219
220
221
222
223
224
225
    );
    if ($data['below']) {
      _book_admin_table_tree($data['below'], $form);
    }
  }

  return $form;
}

/**
226
227
 * Returns HTML for a book administration form.
 *
228
 * @param array $variables
229
230
 *   An associative array containing:
 *   - form: A render element representing the form.
231
 *
232
 * @see book_admin_table()
233
 * @ingroup themeable
234
 */
235
236
237
function theme_book_admin_table($variables) {
  $form = $variables['form'];

238
239
240
  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');

241
  $header = array(t('Title'), t('Weight'), t('Parent'), t('Operations'));
242
243
244
245
246
247
248

  $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'];
249
250

    // Add special classes to be used with tabledrag.js.
251
252
253
    $form[$key]['plid']['#attributes']['class'] = array('book-plid');
    $form[$key]['mlid']['#attributes']['class'] = array('book-mlid');
    $form[$key]['weight']['#attributes']['class'] = array('book-weight');
254

255
    $data = array(
256
      theme('indentation', array('size' => $form[$key]['depth']['#value'] - 2)) . drupal_render($form[$key]['title']),
257
      drupal_render($form[$key]['weight']),
258
      drupal_render($form[$key]['plid']) . drupal_render($form[$key]['mlid']),
259
260
261
    );
    $links = array();
    $links['view'] = array(
262
      'title' => t('View'),
263
264
265
266
      'href' => $href,
    );
    if ($access) {
      $links['edit'] = array(
267
        'title' => t('Edit'),
268
269
270
271
        'href' => "node/$nid/edit",
        'query' => $destination,
      );
      $links['delete'] = array(
272
        'title' => t('Delete'),
273
274
275
276
277
278
279
280
281
        'href' => "node/$nid/delete",
        'query' => $destination,
      );
    }
    $data[] = array(
      'data' => array(
        '#type' => 'operations',
        '#links' => $links,
      ),
282
    );
283
284
285
286
    $row = array('data' => $data);
    if (isset($form[$key]['#attributes'])) {
      $row = array_merge($row, $form[$key]['#attributes']);
    }
287
    $row['class'][] = 'draggable';
288
    $rows[] = $row;
289
290
  }

291
  return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'book-outline'), 'empty' => t('No book content available.')));
292
}