book.admin.inc 8.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php
// $Id$

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

/**
 * Returns an administrative overview of all books.
 */
function book_admin_overview() {
  $rows = array();
14 15 16 17

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

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

  // If no books were found, let the user know.
  if (empty($rows)) {
    $rows[] = array(array('data' => t('No books available.'), 'colspan' => 2));
25 26
  }

27
  return theme('table', array('header' => $headers, 'rows' => $rows));
28 29 30 31 32 33 34 35 36 37
}

/**
 * Builds and returns the book settings form.
 *
 * @see book_admin_settings_validate()
 *
 * @ingroup forms
 */
function book_admin_settings() {
38
  $types = node_type_get_names();
39 40 41
  $form['book_allowed_types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Allowed book outline types'),
42
    '#default_value' => array('book'),
43 44 45 46 47 48 49
    '#options' => $types,
    '#description' => t('Select content types which users with the %add-perm permission will be allowed to add to the book hierarchy. Users with the %outline-perm permission can add all content types.', array('%add-perm' => t('add content to books'),  '%outline-perm' => t('administer book outlines'))),
    '#required' => TRUE,
  );
  $form['book_child_type'] = array(
    '#type' => 'radios',
    '#title' => t('Default child page type'),
50
    '#default_value' => 'book',
51 52 53 54
    '#options' => $types,
    '#description' => 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'))),
    '#required' => TRUE,
  );
55
  $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
56
  $form['#validate'][] = 'book_admin_settings_validate';
57

58
  return system_settings_form($form, TRUE);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
}

/**
 * Validate the book settings form.
 *
 * @see book_admin_settings()
 */
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'))));
  }
}

/**
 * Build the form to administrate the hierarchy of a single book.
 *
 * @see book_admin_edit_submit()
 *
 * @ingroup forms.
 */
80
function book_admin_edit($form, $form_state, $node) {
81
  drupal_set_title($node->title[FIELD_LANGUAGE_NONE][0]['value']);
82
  $form['#node'] = $node;
83
  _book_admin_table($node, $form);
84 85 86 87
  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save book pages'),
  );
88

89 90 91
  return $form;
}

92 93 94 95 96 97 98 99 100 101 102 103
/**
 * Check that the book has not been changed while using the form.
 *
 * @see book_admin_edit()
 */
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.'));
    $form_state['rebuild'] = TRUE;
  }
}

104 105 106
/**
 * Handle submission of the book administrative page form.
 *
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()
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 133
      // Update the title if changed.
      if ($row['title']['#default_value'] != $values['title']) {
        $node = node_load($values['nid'], FALSE);
134 135
        $langcode = FIELD_LANGUAGE_NONE;
        $node->title = array($langcode => array(array('value' => $values['title'])));
136 137
        $node->book['link_title'] = $values['title'];
        $node->revision = 1;
138 139
        $node->log = t('Title changed from %original to %current.', array('%original' => $node->title[$langcode][0]['value'], '%current' => $values['title']));

140
        node_save($node);
141
        watchdog('content', 'book: updated %title.', array('%title' => $node->title[$langcode][0]['value']), 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[$langcode][0]['value'])));
147 148 149 150 151 152 153
}

/**
 * Build the table portion of the form for the book administration page.
 *
 * @see book_admin_edit()
 */
154
function _book_admin_table($node, &$form) {
155
  $form['table'] = array(
156 157 158 159 160
    '#theme' => 'book_admin_table',
    '#tree' => TRUE,
  );

  $tree = book_menu_subtree_data($node->book);
161 162
  $tree = array_shift($tree); // Do not include the book item itself.
  if ($tree['below']) {
163 164 165 166 167 168 169 170 171 172 173 174
    $hash = sha1(serialize($tree['below']));
    // 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']);
175
  }
176

177 178 179 180 181 182 183 184
}

/**
 * Recursive helper to build the main table in the book administration page form.
 *
 * @see book_admin_edit()
 */
function _book_admin_table_tree($tree, &$form) {
185 186
  foreach ($tree as $data) {
    $form['book-admin-' . $data['link']['nid']] = array(
187
      '#item' => $data['link'],
188 189 190 191 192 193 194
      '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,
195
        '#size' => 40,
196 197 198 199 200 201
      ),
      'weight' => array(
        '#type' => 'weight',
        '#default_value' => $data['link']['weight'],
        '#delta' => 15,
      ),
202 203 204 205 206 207 208 209 210
      'plid' => array(
        '#type' => 'textfield',
        '#default_value' => $data['link']['plid'],
        '#size' => 6,
      ),
      'mlid' => array(
        '#type' => 'hidden',
        '#default_value' => $data['link']['mlid'],
      ),
211 212 213 214 215 216 217 218 219 220 221 222 223
    );
    if ($data['below']) {
      _book_admin_table_tree($data['below'], $form);
    }
  }

  return $form;
}

/**
 * Theme function for the book administration page form.
 *
 * @ingroup themeable
224
 * @see book_admin_table()
225
 */
226 227 228
function theme_book_admin_table($variables) {
  $form = $variables['form'];

229 230 231 232
  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'));
233 234 235 236 237 238 239

  $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'];
240 241

    // Add special classes to be used with tabledrag.js.
242 243 244
    $form[$key]['plid']['#attributes']['class'] = array('book-plid');
    $form[$key]['mlid']['#attributes']['class'] = array('book-mlid');
    $form[$key]['weight']['#attributes']['class'] = array('book-weight');
245

246
    $data = array(
247
      theme('indentation', array('size' => $form[$key]['depth']['#value'] - 2)) . drupal_render($form[$key]['title']),
248
      drupal_render($form[$key]['weight']),
249
      drupal_render($form[$key]['plid']) . drupal_render($form[$key]['mlid']),
250
      l(t('view'), $href),
251 252
      $access ? l(t('edit'), 'node/' . $nid . '/edit', array('query' => $destination)) : '&nbsp',
      $access ? l(t('delete'), 'node/' . $nid . '/delete', array('query' => $destination) )  : '&nbsp',
253
    );
254 255 256 257
    $row = array('data' => $data);
    if (isset($form[$key]['#attributes'])) {
      $row = array_merge($row, $form[$key]['#attributes']);
    }
258
    $row['class'][] = 'draggable';
259
    $rows[] = $row;
260 261
  }

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