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

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

/**
 * Returns an administrative overview of all books.
 */
function book_admin_overview() {
  $rows = array();
  foreach (book_get_books() as $book) {
15
    $rows[] = array(l($book['title'], $book['href'], $book['options']), l(t('edit order and titles'), "admin/content/book/" . $book['nid']));
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  }
  $headers = array(t('Book'), t('Operations'));

  return theme('table', $headers, $rows);
}

/**
 * Builds and returns the book settings form.
 *
 * @see book_admin_settings_validate()
 *
 * @ingroup forms
 */
function book_admin_settings() {
  $types = node_get_types('names');
  $form['book_allowed_types'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Allowed book outline types'),
    '#default_value' => variable_get('book_allowed_types', array('book')),
    '#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'),
    '#default_value' => variable_get('book_child_type', 'book'),
    '#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,
  );
47
  $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
48
  $form['#validate'][] = 'book_admin_settings_validate';
49

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  return system_settings_form($form);
}

/**
 * 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.
 */
function book_admin_edit($form_state, $node) {
73
  drupal_set_title(check_plain($node->title));
74
  $form = array();
75 76 77 78 79 80
  $form['#node'] = $node;
  $form['table'] = _book_admin_table($node);
  $form['save'] = array(
    '#type' => 'submit',
    '#value' => t('Save book pages'),
  );
81

82 83 84 85 86 87
  return $form;
}

/**
 * Handle submission of the book administrative page form.
 *
88 89 90
 * This function takes care to save parent menu items before their children.
 * Saving menu items in the incorrect order can break the menu tree.
 *
91
 * @see book_admin_edit()
92
 * @see menu_overview_form_submit()
93 94
 */
function book_admin_edit_submit($form, &$form_state) {
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  // Save elements in the same order as defined in post rather than the form.
  // This ensures parents are updated before their children, preventing orphans.
  $order = array_flip(array_keys($form['#post']['table']));
  $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']);
110
      }
111

112 113 114 115 116 117 118 119
      // Update the title if changed.
      if ($row['title']['#default_value'] != $values['title']) {
        $node = node_load($values['nid'], FALSE);
        $node->title = $values['title'];
        $node->book['link_title'] = $values['title'];
        $node->revision = 1;
        $node->log = t('Title changed from %original to %current.', array('%original' => $node->title, '%current' => $values['title']));
        node_save($node);
120
        watchdog('content', 'book: updated %title.', array('%title' => $node->title), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
121
      }
122 123
    }
  }
124 125

  drupal_set_message(t('Updated book %title.', array('%title' => $form['#node']->title)));
126 127 128 129 130 131 132 133 134 135 136 137 138 139
}

/**
 * Build the table portion of the form for the book administration page.
 *
 * @see book_admin_edit()
 */
function _book_admin_table($node) {
  $form = array(
    '#theme' => 'book_admin_table',
    '#tree' => TRUE,
  );

  $tree = book_menu_subtree_data($node->book);
140 141 142 143
  $tree = array_shift($tree); // Do not include the book item itself.
  if ($tree['below']) {
    _book_admin_table_tree($tree['below'], $form);
  }
144

145 146 147 148 149 150 151 152 153
  return $form;
}

/**
 * Recursive helper to build the main table in the book administration page form.
 *
 * @see book_admin_edit()
 */
function _book_admin_table_tree($tree, &$form) {
154 155
  foreach ($tree as $key => $data) {
    $form[$key] = array(
156
      '#item' => $data['link'],
157 158 159 160 161 162 163
      '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,
164
        '#size' => 40,
165 166 167 168 169 170
      ),
      'weight' => array(
        '#type' => 'weight',
        '#default_value' => $data['link']['weight'],
        '#delta' => 15,
      ),
171 172 173 174 175 176 177 178 179
      'plid' => array(
        '#type' => 'textfield',
        '#default_value' => $data['link']['plid'],
        '#size' => 6,
      ),
      'mlid' => array(
        '#type' => 'hidden',
        '#default_value' => $data['link']['mlid'],
      ),
180 181 182 183 184 185 186 187 188 189 190 191 192
    );
    if ($data['below']) {
      _book_admin_table_tree($data['below'], $form);
    }
  }

  return $form;
}

/**
 * Theme function for the book administration page form.
 *
 * @ingroup themeable
193
 * @see book_admin_table()
194 195
 */
function theme_book_admin_table($form) {
196 197 198 199
  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'));
200 201 202 203 204 205 206

  $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'];
207 208 209 210 211 212

    // Add special classes to be used with tabledrag.js.
    $form[$key]['plid']['#attributes']['class'] = 'book-plid';
    $form[$key]['mlid']['#attributes']['class'] = 'book-mlid';
    $form[$key]['weight']['#attributes']['class'] = 'book-weight';

213
    $data = array(
214
      theme('indentation', $form[$key]['depth']['#value'] - 2) . drupal_render($form[$key]['title']),
215
      drupal_render($form[$key]['weight']),
216
      drupal_render($form[$key]['plid']) . drupal_render($form[$key]['mlid']),
217
      l(t('view'), $href),
218 219
      $access ? l(t('edit'), 'node/' . $nid . '/edit', array('query' => $destination)) : '&nbsp',
      $access ? l(t('delete'), 'node/' . $nid . '/delete', array('query' => $destination) )  : '&nbsp',
220
    );
221 222 223 224
    $row = array('data' => $data);
    if (isset($form[$key]['#attributes'])) {
      $row = array_merge($row, $form[$key]['#attributes']);
    }
225
    $row['class'] = empty($row['class']) ? 'draggable' : $row['class'] . ' draggable';
226
    $rows[] = $row;
227 228
  }

229
  return theme('table', $header, $rows, array('id' => 'book-outline'));
230 231 232 233 234 235 236
}

/**
 * Recursive helper to sort each layer of a book tree by weight.
 */
function _book_admin_sort_tree(&$tree) {
  uasort($tree, '_book_admin_compare');
237
  foreach ($tree as $key => $subtree) {
238 239 240 241 242 243 244 245 246 247 248 249 250 251
    if (!empty($tree[$key]['below'])) {
      _book_admin_sort_tree($tree[$key]['below']);
    }
  }
}

/**
 * Used by uasort() in _book_admin_sort_tree() to compare items in a book tree.
 */
function _book_admin_compare($a, $b) {
  $weight = $a['link']['weight'] - $b['link']['weight'];
  if ($weight) {
    return $weight;
  }
252

253
  return strncmp($a['link']['title'], $b['link']['title']);
254
}