[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/modules/book/ -> book.admin.inc (source)

   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    foreach (book_get_books() as $book) {
  14      $rows[] = array(l($book['title'], $book['href'], $book['options']), l(t('edit order and titles'), "admin/content/book/". $book['nid']));
  15    }
  16    $headers = array(t('Book'), t('Operations'));
  17  
  18    return theme('table', $headers, $rows);
  19  }
  20  
  21  /**
  22   * Builds and returns the book settings form.
  23   *
  24   * @see book_admin_settings_validate()
  25   *
  26   * @ingroup forms
  27   */
  28  function book_admin_settings() {
  29    $types = node_get_types('names');
  30    $form['book_allowed_types'] = array(
  31      '#type' => 'checkboxes',
  32      '#title' => t('Allowed book outline types'),
  33      '#default_value' => variable_get('book_allowed_types', array('book')),
  34      '#options' => $types,
  35      '#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'))),
  36      '#required' => TRUE,
  37    );
  38    $form['book_child_type'] = array(
  39      '#type' => 'radios',
  40      '#title' => t('Default child page type'),
  41      '#default_value' => variable_get('book_child_type', 'book'),
  42      '#options' => $types,
  43      '#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'))),
  44      '#required' => TRUE,
  45    );
  46    $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
  47    $form['#validate'][] = 'book_admin_settings_validate';
  48    return system_settings_form($form);
  49  }
  50  
  51  /**
  52   * Validate the book settings form.
  53   *
  54   * @see book_admin_settings()
  55   */
  56  function book_admin_settings_validate($form, &$form_state) {
  57    $child_type = $form_state['values']['book_child_type'];
  58    if (empty($form_state['values']['book_allowed_types'][$child_type])) {
  59      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'))));
  60    }
  61  }
  62  
  63  /**
  64   * Build the form to administrate the hierarchy of a single book.
  65   *
  66   * @see book_admin_edit_submit()
  67   *
  68   * @ingroup forms.
  69   */
  70  function book_admin_edit($form_state, $node) {
  71    drupal_set_title(check_plain($node->title));
  72    $form = array();
  73    $form['#node'] = $node;
  74    _book_admin_table($node, $form);
  75    $form['save'] = array(
  76      '#type' => 'submit',
  77      '#value' => t('Save book pages'),
  78    );
  79    return $form;
  80  }
  81  
  82  /**
  83   * Check that the book has not been changed while using the form.
  84   *
  85   * @see book_admin_edit()
  86   */
  87  function book_admin_edit_validate($form, &$form_state) {
  88    if ($form_state['values']['tree_hash'] != $form_state['values']['tree_current_hash']) {
  89      form_set_error('', t('This book has been modified by another user, the changes could not be saved.'));
  90      $form_state['rebuild'] = TRUE;
  91    }
  92  }
  93  
  94  /**
  95   * Handle submission of the book administrative page form.
  96   *
  97   * This function takes care to save parent menu items before their children.
  98   * Saving menu items in the incorrect order can break the menu tree.
  99   *
 100   * @see book_admin_edit()
 101   * @see menu_overview_form_submit()
 102   */
 103  function book_admin_edit_submit($form, &$form_state) {
 104    // Save elements in the same order as defined in post rather than the form.
 105    // This ensures parents are updated before their children, preventing orphans.
 106    $order = array_flip(array_keys($form['#post']['table']));
 107    $form['table'] = array_merge($order, $form['table']);
 108  
 109    foreach (element_children($form['table']) as $key) {
 110      if ($form['table'][$key]['#item']) {
 111        $row = $form['table'][$key];
 112        $values = $form_state['values']['table'][$key];
 113  
 114        // Update menu item if moved.
 115        if ($row['plid']['#default_value'] != $values['plid'] || $row['weight']['#default_value'] != $values['weight']) {
 116          $row['#item']['plid'] = $values['plid'];
 117          $row['#item']['weight'] = $values['weight'];
 118          menu_link_save($row['#item']);
 119        }
 120  
 121        // Update the title if changed.
 122        if ($row['title']['#default_value'] != $values['title']) {
 123          $node = node_load($values['nid'], FALSE);
 124          $node->title = $values['title'];
 125          $node->book['link_title'] = $values['title'];
 126          $node->revision = 1;
 127          $node->log = t('Title changed from %original to %current.', array('%original' => $node->title, '%current' => $values['title']));
 128          node_save($node);
 129          watchdog('content', 'book: updated %title.', array('%title' => $node->title), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid));
 130        }
 131      }
 132    }
 133  
 134    drupal_set_message(t('Updated book %title.', array('%title' => $form['#node']->title)));
 135  }
 136  
 137  /**
 138   * Build the table portion of the form for the book administration page.
 139   *
 140   * @see book_admin_edit()
 141   */
 142  function _book_admin_table($node, &$form) {
 143    $form['table'] = array(
 144      '#theme' => 'book_admin_table',
 145      '#tree' => TRUE,
 146    );
 147  
 148    $tree = book_menu_subtree_data($node->book);
 149    $tree = array_shift($tree); // Do not include the book item itself.
 150    if ($tree['below']) {
 151      $hash = sha1(serialize($tree['below']));
 152      // Store the hash value as a hidden form element so that we can detect
 153      // if another user changed the book hierarchy.
 154      $form['tree_hash'] = array(
 155        '#type' => 'hidden',
 156        '#default_value' => $hash,
 157      );
 158      $form['tree_current_hash'] = array(
 159        '#type' => 'value',
 160        '#value' => $hash,
 161      );
 162      _book_admin_table_tree($tree['below'], $form['table']);
 163    }
 164  }
 165  
 166  /**
 167   * Recursive helper to build the main table in the book administration page form.
 168   *
 169   * @see book_admin_edit()
 170   */
 171  function _book_admin_table_tree($tree, &$form) {
 172    foreach ($tree as $data) {
 173      $form['book-admin-'. $data['link']['nid']] = array(
 174        '#item' => $data['link'],
 175        'nid' => array('#type' => 'value', '#value' => $data['link']['nid']),
 176        'depth' => array('#type' => 'value', '#value' => $data['link']['depth']),
 177        'href' => array('#type' => 'value', '#value' => $data['link']['href']),
 178        'title' => array(
 179          '#type' => 'textfield',
 180          '#default_value' => $data['link']['link_title'],
 181          '#maxlength' => 255,
 182          '#size' => 40,
 183        ),
 184        'weight' => array(
 185          '#type' => 'weight',
 186          '#default_value' => $data['link']['weight'],
 187          '#delta' => 15,
 188        ),
 189        'plid' => array(
 190          '#type' => 'textfield',
 191          '#default_value' => $data['link']['plid'],
 192          '#size' => 6,
 193        ),
 194        'mlid' => array(
 195          '#type' => 'hidden',
 196          '#default_value' => $data['link']['mlid'],
 197        ),
 198      );
 199      if ($data['below']) {
 200        _book_admin_table_tree($data['below'], $form);
 201      }
 202    }
 203  
 204    return $form;
 205  }
 206  
 207  /**
 208   * Theme function for the book administration page form.
 209   *
 210   * @ingroup themeable
 211   * @see book_admin_table()
 212   */
 213  function theme_book_admin_table($form) {
 214    drupal_add_tabledrag('book-outline', 'match', 'parent', 'book-plid', 'book-plid', 'book-mlid', TRUE, MENU_MAX_DEPTH - 2);
 215    drupal_add_tabledrag('book-outline', 'order', 'sibling', 'book-weight');
 216  
 217    $header = array(t('Title'), t('Weight'), t('Parent'), array('data' => t('Operations'), 'colspan' => '3'));
 218  
 219    $rows = array();
 220    $destination = drupal_get_destination();
 221    $access = user_access('administer nodes');
 222    foreach (element_children($form) as $key) {
 223      $nid = $form[$key]['nid']['#value'];
 224      $href = $form[$key]['href']['#value'];
 225  
 226      // Add special classes to be used with tabledrag.js.
 227      $form[$key]['plid']['#attributes']['class'] = 'book-plid';
 228      $form[$key]['mlid']['#attributes']['class'] = 'book-mlid';
 229      $form[$key]['weight']['#attributes']['class'] = 'book-weight';
 230  
 231      $data = array(
 232        theme('indentation', $form[$key]['depth']['#value'] - 2) . drupal_render($form[$key]['title']),
 233        drupal_render($form[$key]['weight']),
 234        drupal_render($form[$key]['plid']) . drupal_render($form[$key]['mlid']),
 235        l(t('view'), $href),
 236        $access ? l(t('edit'), 'node/'. $nid .'/edit', array('query' => $destination)) : '&nbsp',
 237        $access ? l(t('delete'), 'node/'. $nid .'/delete', array('query' => $destination) )  : '&nbsp',
 238      );
 239      $row = array('data' => $data);
 240      if (isset($form[$key]['#attributes'])) {
 241        $row = array_merge($row, $form[$key]['#attributes']);
 242      }
 243      $row['class'] = empty($row['class']) ? 'draggable' : $row['class'] .' draggable';
 244      $rows[] = $row;
 245    }
 246  
 247    return theme('table', $header, $rows, array('id' => 'book-outline'));
 248  }
 249  


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7