Drupal PHP Cross Reference Content Management Systems

Source: /modules/book/book.admin.inc - 282 lines - 9411 bytes - Summary - Text - Print

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Admin page callbacks for the book module.
   6   */
   7  
   8  /**
   9   * Returns an administrative overview of all books.
  10   */
  11  function book_admin_overview() {
  12    $rows = array();
  13  
  14    $headers = array(t('Book'), t('Operations'));
  15  
  16    // Add any recognized books to the table list.
  17    foreach (book_get_books() as $book) {
  18      $rows[] = array(l($book['title'], $book['href'], $book['options']), l(t('edit order and titles'), 'admin/content/book/' . $book['nid']));
  19    }
  20  
  21    return theme('table', array('header' => $headers, 'rows' => $rows, 'empty' => t('No books available.')));
  22  }
  23  
  24  /**
  25   * Form constructor for the book settings form.
  26   *
  27   * @see book_admin_settings_validate()
  28   *
  29   * @ingroup forms
  30   */
  31  function book_admin_settings() {
  32    $types = node_type_get_names();
  33    $form['book_allowed_types'] = array(
  34      '#type' => 'checkboxes',
  35      '#title' => t('Content types allowed in book outlines'),
  36      '#default_value' => variable_get('book_allowed_types', array('book')),
  37      '#options' => $types,
  38      '#description' => t('Users with the %outline-perm permission can add all content types.', array('%outline-perm' => t('Administer book outlines'))),
  39      '#required' => TRUE,
  40    );
  41    $form['book_child_type'] = array(
  42      '#type' => 'radios',
  43      '#title' => t('Content type for child pages'),
  44      '#default_value' => variable_get('book_child_type', 'book'),
  45      '#options' => $types,
  46      '#required' => TRUE,
  47    );
  48    $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
  49    $form['#validate'][] = 'book_admin_settings_validate';
  50  
  51    return system_settings_form($form);
  52  }
  53  
  54  /**
  55   * Form validation handler for book_admin_settings().
  56   */
  57  function book_admin_settings_validate($form, &$form_state) {
  58    $child_type = $form_state['values']['book_child_type'];
  59    if (empty($form_state['values']['book_allowed_types'][$child_type])) {
  60      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'))));
  61    }
  62  }
  63  
  64  /**
  65   * Form constructor for administering a single book's hierarchy.
  66   *
  67   * @see book_admin_edit_submit()
  68   *
  69   * @param $node
  70   *   The node of the top-level page in the book.
  71   *
  72   * @see book_admin_edit_validate()
  73   * @see book_admin_edit_submit()
  74   * @ingroup forms
  75   */
  76  function book_admin_edit($form, $form_state, $node) {
  77    drupal_set_title($node->title);
  78    $form['#node'] = $node;
  79    _book_admin_table($node, $form);
  80    $form['save'] = array(
  81      '#type' => 'submit',
  82      '#value' => t('Save book pages'),
  83    );
  84  
  85    return $form;
  86  }
  87  
  88  /**
  89   * Form validation handler for book_admin_edit().
  90   *
  91   * Checks that the book has not been changed while using the form.
  92   *
  93   * @see book_admin_edit_submit()
  94   */
  95  function book_admin_edit_validate($form, &$form_state) {
  96    if ($form_state['values']['tree_hash'] != $form_state['values']['tree_current_hash']) {
  97      form_set_error('', t('This book has been modified by another user, the changes could not be saved.'));
  98    }
  99  }
 100  
 101  /**
 102   * Form submission handler for book_admin_edit().
 103   *
 104   * This function takes care to save parent menu items before their children.
 105   * Saving menu items in the incorrect order can break the menu tree.
 106   *
 107   * @see book_admin_edit_validate()
 108   * @see menu_overview_form_submit()
 109   */
 110  function book_admin_edit_submit($form, &$form_state) {
 111    // Save elements in the same order as defined in post rather than the form.
 112    // This ensures parents are updated before their children, preventing orphans.
 113    $order = array_flip(array_keys($form_state['input']['table']));
 114    $form['table'] = array_merge($order, $form['table']);
 115  
 116    foreach (element_children($form['table']) as $key) {
 117      if ($form['table'][$key]['#item']) {
 118        $row = $form['table'][$key];
 119        $values = $form_state['values']['table'][$key];
 120  
 121        // Update menu item if moved.
 122        if ($row['plid']['#default_value'] != $values['plid'] || $row['weight']['#default_value'] != $values['weight']) {
 123          $row['#item']['plid'] = $values['plid'];
 124          $row['#item']['weight'] = $values['weight'];
 125          menu_link_save($row['#item']);
 126        }
 127  
 128        // Update the title if changed.
 129        if ($row['title']['#default_value'] != $values['title']) {
 130          $node = node_load($values['nid']);
 131          $langcode = LANGUAGE_NONE;
 132          $node->title = $values['title'];
 133          $node->book['link_title'] = $values['title'];
 134          $node->revision = 1;
 135          $node->log = t('Title changed from %original to %current.', array('%original' => $node->title, '%current' => $values['title']));
 136  
 137          node_save($node);
 138          watchdog('content', 'book: updated %title.', array('%title' => $node->title), WATCHDOG_NOTICE, l(t('view'), 'node/' . $node->nid));
 139        }
 140      }
 141    }
 142  
 143    drupal_set_message(t('Updated book %title.', array('%title' => $form['#node']->title)));
 144  }
 145  
 146  /**
 147   * Builds the table portion of the form for the book administration page.
 148   *
 149   * @param $node
 150   *   The node of the top-level page in the book.
 151   * @param $form
 152   *   The form that is being modified.
 153   *
 154   * @see book_admin_edit()
 155   */
 156  function _book_admin_table($node, &$form) {
 157    $form['table'] = array(
 158      '#theme' => 'book_admin_table',
 159      '#tree' => TRUE,
 160    );
 161  
 162    $tree = book_menu_subtree_data($node->book);
 163    $tree = array_shift($tree); // Do not include the book item itself.
 164    if ($tree['below']) {
 165      $hash = drupal_hash_base64(serialize($tree['below']));
 166      // Store the hash value as a hidden form element so that we can detect
 167      // if another user changed the book hierarchy.
 168      $form['tree_hash'] = array(
 169        '#type' => 'hidden',
 170        '#default_value' => $hash,
 171      );
 172      $form['tree_current_hash'] = array(
 173        '#type' => 'value',
 174        '#value' => $hash,
 175      );
 176      _book_admin_table_tree($tree['below'], $form['table']);
 177    }
 178  
 179  }
 180  
 181  /**
 182   * Helps build the main table in the book administration page form.
 183   *
 184   * @param $tree
 185   *   A subtree of the book menu hierarchy.
 186   * @param $form
 187   *   The form that is being modified.
 188   *
 189   * @return
 190   *   The form that is being modified.
 191   *
 192   * @see book_admin_edit()
 193   */
 194  function _book_admin_table_tree($tree, &$form) {
 195    // The delta must be big enough to give each node a distinct value.
 196    $count = count($tree);
 197    $delta = ($count < 30) ? 15 : intval($count / 2) + 1;
 198  
 199    foreach ($tree as $data) {
 200      $form['book-admin-' . $data['link']['nid']] = array(
 201        '#item' => $data['link'],
 202        'nid' => array('#type' => 'value', '#value' => $data['link']['nid']),
 203        'depth' => array('#type' => 'value', '#value' => $data['link']['depth']),
 204        'href' => array('#type' => 'value', '#value' => $data['link']['href']),
 205        'title' => array(
 206          '#type' => 'textfield',
 207          '#default_value' => $data['link']['link_title'],
 208          '#maxlength' => 255,
 209          '#size' => 40,
 210        ),
 211        'weight' => array(
 212          '#type' => 'weight',
 213          '#default_value' => $data['link']['weight'],
 214          '#delta' => max($delta, abs($data['link']['weight'])),
 215          '#title' => t('Weight for @title', array('@title' => $data['link']['title'])),
 216          '#title_display' => 'invisible',
 217        ),
 218        'plid' => array(
 219          '#type' => 'hidden',
 220          '#default_value' => $data['link']['plid'],
 221        ),
 222        'mlid' => array(
 223          '#type' => 'hidden',
 224          '#default_value' => $data['link']['mlid'],
 225        ),
 226      );
 227      if ($data['below']) {
 228        _book_admin_table_tree($data['below'], $form);
 229      }
 230    }
 231  
 232    return $form;
 233  }
 234  
 235  /**
 236   * Returns HTML for a book administration form.
 237   *
 238   * @param $variables
 239   *   An associative array containing:
 240   *   - form: A render element representing the form.
 241   *
 242   * @see book_admin_table()
 243   * @ingroup themeable
 244   */
 245  function theme_book_admin_table($variables) {
 246    $form = $variables['form'];
 247  
 248    drupal_add_tabledrag('book-outline', 'match', 'parent', 'book-plid', 'book-plid', 'book-mlid', TRUE, MENU_MAX_DEPTH - 2);
 249    drupal_add_tabledrag('book-outline', 'order', 'sibling', 'book-weight');
 250  
 251    $header = array(t('Title'), t('Weight'), t('Parent'), array('data' => t('Operations'), 'colspan' => '3'));
 252  
 253    $rows = array();
 254    $destination = drupal_get_destination();
 255    $access = user_access('administer nodes');
 256    foreach (element_children($form) as $key) {
 257      $nid = $form[$key]['nid']['#value'];
 258      $href = $form[$key]['href']['#value'];
 259  
 260      // Add special classes to be used with tabledrag.js.
 261      $form[$key]['plid']['#attributes']['class'] = array('book-plid');
 262      $form[$key]['mlid']['#attributes']['class'] = array('book-mlid');
 263      $form[$key]['weight']['#attributes']['class'] = array('book-weight');
 264  
 265      $data = array(
 266        theme('indentation', array('size' => $form[$key]['depth']['#value'] - 2)) . drupal_render($form[$key]['title']),
 267        drupal_render($form[$key]['weight']),
 268        drupal_render($form[$key]['plid']) . drupal_render($form[$key]['mlid']),
 269        l(t('view'), $href),
 270        $access ? l(t('edit'), 'node/' . $nid . '/edit', array('query' => $destination)) : '&nbsp;',
 271        $access ? l(t('delete'), 'node/' . $nid . '/delete', array('query' => $destination) )  : '&nbsp;',
 272      );
 273      $row = array('data' => $data);
 274      if (isset($form[$key]['#attributes'])) {
 275        $row = array_merge($row, $form[$key]['#attributes']);
 276      }
 277      $row['class'][] = 'draggable';
 278      $rows[] = $row;
 279    }
 280  
 281    return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'book-outline'), 'empty' => t('No book content available.')));
 282  }

title

Description

title

Description

title

Description

title

title

Body