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

/**
 * @file
 * Admin page callbacks for the book module.
 */

8
9
use Drupal\node\Node;

10
/**
11
12
13
 * Page callback: Returns an administrative overview of all books.
 *
 * @see book_menu()
14
15
16
 */
function book_admin_overview() {
  $rows = array();
17
18
19
20

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

  // Add any recognized books to the table list.
21
  foreach (book_get_books() as $book) {
22
23
24
    $rows[] = array(l($book['title'], $book['href'], $book['options']), l(t('edit order and titles'), 'admin/content/book/' . $book['nid']));
  }

25
  return theme('table', array('header' => $headers, 'rows' => $rows, 'empty' => t('No books available.')));
26
27
28
}

/**
29
 * Form constructor for the book settings form.
30
 *
31
 * @see book_menu()
32
 * @see book_admin_settings_submit()
33
 * @see book_admin_settings_validate()
34
35
 * @ingroup forms
 */
36
function book_admin_settings($form, &$form_state) {
37
  $types = node_type_get_names();
38
  $config = config('book.settings');
39
40
  $form['book_allowed_types'] = array(
    '#type' => 'checkboxes',
41
    '#title' => t('Content types allowed in book outlines'),
42
    '#default_value' => $config->get('allowed_types'),
43
    '#options' => $types,
44
    '#description' => t('Users with the %outline-perm permission can add all content types.', array('%outline-perm' => t('Administer book outlines'))),
45
46
47
48
    '#required' => TRUE,
  );
  $form['book_child_type'] = array(
    '#type' => 'radios',
49
    '#title' => t('Content type for child pages'),
50
    '#default_value' => $config->get('child_type'),
51
52
53
    '#options' => $types,
    '#required' => TRUE,
  );
54
  $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
55
  $form['#validate'][] = 'book_admin_settings_validate';
56

57
  return system_config_form($form, $form_state);
58
59
60
}

/**
61
 * Form validation handler for book_admin_settings().
62
63
64
65
66
67
68
69
 */
function book_admin_settings_validate($form, &$form_state) {
  $child_type = $form_state['values']['book_child_type'];
  if (empty($form_state['values']['book_allowed_types'][$child_type])) {
    form_set_error('book_child_type', t('The content type for the %add-child link must be one of those selected as an allowed book outline type.', array('%add-child' => t('Add child page'))));
  }
}

70
71
72
73
74
75
76
77
78
79
80
81
/**
 * Form submission handler for book_admin_settings().
 *
 * @see book_admin_settings_validate()
 */
function book_admin_settings_submit($form, &$form_state) {
  config('book.settings')
    ->set('allowed_types', $form_state['values']['book_allowed_types'])
    ->set('child_type', $form_state['values']['book_child_type'])
    ->save();
 }

82
/**
83
 * Form constructor for administering a single book's hierarchy.
84
 *
85
 * @param Drupal\node\Node $node
86
87
88
89
90
91
 *   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
92
 */
93
function book_admin_edit($form, $form_state, Node $node) {
94
  drupal_set_title($node->label());
95
  $form['#node'] = $node;
96
  _book_admin_table($node, $form);
97
98
99
100
  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save book pages'),
  );
101

102
103
104
  return $form;
}

105
/**
106
 * Form validation handler for book_admin_edit().
107
 *
108
109
110
 * Checks that the book has not been changed while using the form.
 *
 * @see book_admin_edit_submit()
111
112
113
114
115
116
117
 */
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.'));
  }
}

118
/**
119
 * Form submission handler for book_admin_edit().
120
 *
121
122
123
 * This function takes care to save parent menu items before their children.
 * Saving menu items in the incorrect order can break the menu tree.
 *
124
 * @see book_admin_edit_validate()
125
 * @see menu_overview_form_submit()
126
127
 */
function book_admin_edit_submit($form, &$form_state) {
128
129
  // Save elements in the same order as defined in post rather than the form.
  // This ensures parents are updated before their children, preventing orphans.
130
  $order = array_flip(array_keys($form_state['input']['table']));
131
132
133
134
135
136
137
138
139
140
141
142
  $form['table'] = array_merge($order, $form['table']);

  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']) {
        $row['#item']['plid'] = $values['plid'];
        $row['#item']['weight'] = $values['weight'];
        menu_link_save($row['#item']);
143
      }
144

145
146
      // Update the title if changed.
      if ($row['title']['#default_value'] != $values['title']) {
147
        $node = node_load($values['nid']);
148
        $langcode = LANGUAGE_NOT_SPECIFIED;
149
        $node->title = $values['title'];
150
151
        $node->book['link_title'] = $values['title'];
        $node->revision = 1;
152
        $node->log = t('Title changed from %original to %current.', array('%original' => $node->title, '%current' => $values['title']));
153

154
        $node->save();
155
        watchdog('content', 'book: updated %title.', array('%title' => $node->label()), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
156
      }
157
158
    }
  }
159

160
  drupal_set_message(t('Updated book %title.', array('%title' => $form['#node']->label())));
161
162
163
}

/**
164
165
 * Builds the table portion of the form for the book administration page.
 *
166
 * @param Drupal\node\Node $node
167
168
169
 *   The node of the top-level page in the book.
 * @param $form
 *   The form that is being modified.
170
171
172
 *
 * @see book_admin_edit()
 */
173
function _book_admin_table(Node $node, &$form) {
174
  $form['table'] = array(
175
176
177
178
179
    '#theme' => 'book_admin_table',
    '#tree' => TRUE,
  );

  $tree = book_menu_subtree_data($node->book);
180
181
  $tree = array_shift($tree); // Do not include the book item itself.
  if ($tree['below']) {
182
    $hash = drupal_hash_base64(serialize($tree['below']));
183
184
185
186
187
188
189
190
191
192
193
    // 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']);
194
  }
195

196
197
198
}

/**
199
200
201
202
203
204
205
206
207
 * Helps build the main table in the book administration page form.
 *
 * @param $tree
 *   A subtree of the book menu hierarchy.
 * @param $form
 *   The form that is being modified.
 *
 * @return
 *   The form that is being modified.
208
209
210
211
 *
 * @see book_admin_edit()
 */
function _book_admin_table_tree($tree, &$form) {
212
213
214
215
  // The delta must be big enough to give each node a distinct value.
  $count = count($tree);
  $delta = ($count < 30) ? 15 : intval($count / 2) + 1;

216
217
  foreach ($tree as $data) {
    $form['book-admin-' . $data['link']['nid']] = array(
218
      '#item' => $data['link'],
219
220
221
222
223
224
225
      '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,
226
        '#size' => 40,
227
228
      ),
      'weight' => array(
229
        '#type' => 'weight',
230
        '#default_value' => $data['link']['weight'],
231
        '#delta' => max($delta, abs($data['link']['weight'])),
232
233
        '#title' => t('Weight for @title', array('@title' => $data['link']['title'])),
        '#title_display' => 'invisible',
234
      ),
235
      'plid' => array(
236
        '#type' => 'hidden',
237
238
239
240
241
242
        '#default_value' => $data['link']['plid'],
      ),
      'mlid' => array(
        '#type' => 'hidden',
        '#default_value' => $data['link']['mlid'],
      ),
243
244
245
246
247
248
249
250
251
252
    );
    if ($data['below']) {
      _book_admin_table_tree($data['below'], $form);
    }
  }

  return $form;
}

/**
253
254
255
256
257
 * Returns HTML for a book administration form.
 *
 * @param $variables
 *   An associative array containing:
 *   - form: A render element representing the form.
258
 *
259
 * @see book_admin_table()
260
 * @ingroup themeable
261
 */
262
263
264
function theme_book_admin_table($variables) {
  $form = $variables['form'];

265
266
267
268
  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');

  $header = array(t('Title'), t('Weight'), t('Parent'), array('data' => t('Operations'), 'colspan' => '3'));
269
270
271
272
273
274
275

  $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'];
276
277

    // Add special classes to be used with tabledrag.js.
278
279
280
    $form[$key]['plid']['#attributes']['class'] = array('book-plid');
    $form[$key]['mlid']['#attributes']['class'] = array('book-mlid');
    $form[$key]['weight']['#attributes']['class'] = array('book-weight');
281

282
    $data = array(
283
      theme('indentation', array('size' => $form[$key]['depth']['#value'] - 2)) . drupal_render($form[$key]['title']),
284
      drupal_render($form[$key]['weight']),
285
      drupal_render($form[$key]['plid']) . drupal_render($form[$key]['mlid']),
286
      l(t('view'), $href),
287
288
      $access ? l(t('edit'), 'node/' . $nid . '/edit', array('query' => $destination)) : '&nbsp;',
      $access ? l(t('delete'), 'node/' . $nid . '/delete', array('query' => $destination) )  : '&nbsp;',
289
    );
290
291
292
293
    $row = array('data' => $data);
    if (isset($form[$key]['#attributes'])) {
      $row = array_merge($row, $form[$key]['#attributes']);
    }
294
    $row['class'][] = 'draggable';
295
    $rows[] = $row;
296
297
  }

298
  return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'book-outline')));
299
}