book.admin.inc 9.26 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 32
 * @see book_menu()
 * @see book_admin_settings_validate()
33 34 35
 * @ingroup forms
 */
function book_admin_settings() {
36
  $types = node_type_get_names();
37 38
  $form['book_allowed_types'] = array(
    '#type' => 'checkboxes',
39
    '#title' => t('Content types allowed in book outlines'),
40
    '#default_value' => variable_get('book_allowed_types', array('book')),
41
    '#options' => $types,
42
    '#description' => t('Users with the %outline-perm permission can add all content types.', array('%outline-perm' => t('Administer book outlines'))),
43 44 45 46
    '#required' => TRUE,
  );
  $form['book_child_type'] = array(
    '#type' => 'radios',
47
    '#title' => t('Content type for child pages'),
48
    '#default_value' => variable_get('book_child_type', 'book'),
49 50 51
    '#options' => $types,
    '#required' => TRUE,
  );
52
  $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
53
  $form['#validate'][] = 'book_admin_settings_validate';
54

55
  return system_settings_form($form);
56 57 58
}

/**
59
 * Form validation handler for book_admin_settings().
60 61 62 63 64 65 66 67 68
 */
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'))));
  }
}

/**
69
 * Form constructor for administering a single book's hierarchy.
70
 *
71
 * @param Drupal\node\Node $node
72 73 74 75 76 77
 *   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
78
 */
79
function book_admin_edit($form, $form_state, Node $node) {
80
  drupal_set_title($node->title);
81
  $form['#node'] = $node;
82
  _book_admin_table($node, $form);
83 84 85 86
  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save book pages'),
  );
87

88 89 90
  return $form;
}

91
/**
92
 * Form validation handler for book_admin_edit().
93
 *
94 95 96
 * Checks that the book has not been changed while using the form.
 *
 * @see book_admin_edit_submit()
97 98 99 100 101 102 103
 */
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.'));
  }
}

104
/**
105
 * Form submission handler for book_admin_edit().
106
 *
107 108 109
 * This function takes care to save parent menu items before their children.
 * Saving menu items in the incorrect order can break the menu tree.
 *
110
 * @see book_admin_edit_validate()
111
 * @see menu_overview_form_submit()
112 113
 */
function book_admin_edit_submit($form, &$form_state) {
114 115
  // Save elements in the same order as defined in post rather than the form.
  // This ensures parents are updated before their children, preventing orphans.
116
  $order = array_flip(array_keys($form_state['input']['table']));
117 118 119 120 121 122 123 124 125 126 127 128
  $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']);
129
      }
130

131 132
      // Update the title if changed.
      if ($row['title']['#default_value'] != $values['title']) {
133
        $node = node_load($values['nid']);
134
        $langcode = LANGUAGE_NOT_SPECIFIED;
135
        $node->title = $values['title'];
136 137
        $node->book['link_title'] = $values['title'];
        $node->revision = 1;
138
        $node->log = t('Title changed from %original to %current.', array('%original' => $node->title, '%current' => $values['title']));
139

140
        $node->save();
141
        watchdog('content', 'book: updated %title.', array('%title' => $node->title), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
142
      }
143 144
    }
  }
145

146
  drupal_set_message(t('Updated book %title.', array('%title' => $form['#node']->title)));
147 148 149
}

/**
150 151
 * Builds the table portion of the form for the book administration page.
 *
152
 * @param Drupal\node\Node $node
153 154 155
 *   The node of the top-level page in the book.
 * @param $form
 *   The form that is being modified.
156 157 158
 *
 * @see book_admin_edit()
 */
159
function _book_admin_table(Node $node, &$form) {
160
  $form['table'] = array(
161 162 163 164 165
    '#theme' => 'book_admin_table',
    '#tree' => TRUE,
  );

  $tree = book_menu_subtree_data($node->book);
166 167
  $tree = array_shift($tree); // Do not include the book item itself.
  if ($tree['below']) {
168
    $hash = drupal_hash_base64(serialize($tree['below']));
169 170 171 172 173 174 175 176 177 178 179
    // 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']);
180
  }
181

182 183 184
}

/**
185 186 187 188 189 190 191 192 193
 * 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.
194 195 196 197
 *
 * @see book_admin_edit()
 */
function _book_admin_table_tree($tree, &$form) {
198 199 200 201
  // The delta must be big enough to give each node a distinct value.
  $count = count($tree);
  $delta = ($count < 30) ? 15 : intval($count / 2) + 1;

202 203
  foreach ($tree as $data) {
    $form['book-admin-' . $data['link']['nid']] = array(
204
      '#item' => $data['link'],
205 206 207 208 209 210 211
      '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,
212
        '#size' => 40,
213 214
      ),
      'weight' => array(
215
        '#type' => 'weight',
216
        '#default_value' => $data['link']['weight'],
217
        '#delta' => max($delta, abs($data['link']['weight'])),
218 219
        '#title' => t('Weight for @title', array('@title' => $data['link']['title'])),
        '#title_display' => 'invisible',
220
      ),
221
      'plid' => array(
222
        '#type' => 'hidden',
223 224 225 226 227 228
        '#default_value' => $data['link']['plid'],
      ),
      'mlid' => array(
        '#type' => 'hidden',
        '#default_value' => $data['link']['mlid'],
      ),
229 230 231 232 233 234 235 236 237 238
    );
    if ($data['below']) {
      _book_admin_table_tree($data['below'], $form);
    }
  }

  return $form;
}

/**
239 240 241 242 243
 * Returns HTML for a book administration form.
 *
 * @param $variables
 *   An associative array containing:
 *   - form: A render element representing the form.
244
 *
245
 * @see book_admin_table()
246
 * @ingroup themeable
247
 */
248 249 250
function theme_book_admin_table($variables) {
  $form = $variables['form'];

251 252 253 254
  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'));
255 256 257 258 259 260 261

  $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'];
262 263

    // Add special classes to be used with tabledrag.js.
264 265 266
    $form[$key]['plid']['#attributes']['class'] = array('book-plid');
    $form[$key]['mlid']['#attributes']['class'] = array('book-mlid');
    $form[$key]['weight']['#attributes']['class'] = array('book-weight');
267

268
    $data = array(
269
      theme('indentation', array('size' => $form[$key]['depth']['#value'] - 2)) . drupal_render($form[$key]['title']),
270
      drupal_render($form[$key]['weight']),
271
      drupal_render($form[$key]['plid']) . drupal_render($form[$key]['mlid']),
272
      l(t('view'), $href),
273 274
      $access ? l(t('edit'), 'node/' . $nid . '/edit', array('query' => $destination)) : '&nbsp;',
      $access ? l(t('delete'), 'node/' . $nid . '/delete', array('query' => $destination) )  : '&nbsp;',
275
    );
276 277 278 279
    $row = array('data' => $data);
    if (isset($form[$key]['#attributes'])) {
      $row = array_merge($row, $form[$key]['#attributes']);
    }
280
    $row['class'][] = 'draggable';
281
    $rows[] = $row;
282 283
  }

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