[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * @file
   5   * User page callbacks for the book module.
   6   */
   7  
   8  /**
   9   * Menu callback; prints a listing of all books.
  10   */
  11  function book_render() {
  12    $book_list = array();
  13    foreach (book_get_books() as $book) {
  14      $book_list[] = l($book['title'], $book['href'], $book['options']);
  15    }
  16  
  17    return theme('item_list', $book_list);
  18  }
  19  
  20  /**
  21   * Menu callback; Generates various representation of a book page and its children.
  22   *
  23   * The function delegates the generation of output to helper functions.
  24   * The function name is derived by prepending 'book_export_' to the
  25   * given output type. So, e.g., a type of 'html' results in a call to
  26   * the function book_export_html().
  27   *
  28   * @param $type
  29   *   A string encoding the type of output requested. The following
  30   *   types are currently supported in book module:
  31   *
  32   *   - html: HTML (printer friendly output)
  33   *
  34   *   Other types may be supported in contributed modules.
  35   * @param $nid
  36   *   An integer representing the node id (nid) of the node to export
  37   * @return
  38   *   A string representing the node and its children in the book hierarchy
  39   *   in a format determined by the $type parameter.
  40   */
  41  function book_export($type, $nid) {
  42  
  43    $type = drupal_strtolower($type);
  44  
  45    $export_function = 'book_export_'. $type;
  46  
  47    if (function_exists($export_function)) {
  48      print call_user_func($export_function, $nid);
  49    }
  50    else {
  51      drupal_set_message(t('Unknown export format.'));
  52      drupal_not_found();
  53    }
  54  }
  55  
  56  /**
  57   * This function is called by book_export() to generate HTML for export.
  58   *
  59   * The given node is /embedded to its absolute depth in a top level
  60   * section/. For example, a child node with depth 2 in the hierarchy
  61   * is contained in (otherwise empty) &lt;div&gt; elements
  62   * corresponding to depth 0 and depth 1. This is intended to support
  63   * WYSIWYG output - e.g., level 3 sections always look like level 3
  64   * sections, no matter their depth relative to the node selected to be
  65   * exported as printer-friendly HTML.
  66   *
  67   * @param $nid
  68   *   An integer representing the node id (nid) of the node to export.
  69   * @return
  70   *   A string containing HTML representing the node and its children in
  71   *   the book hierarchy.
  72   */
  73  function book_export_html($nid) {
  74    if (user_access('access printer-friendly version')) {
  75      $export_data = array();
  76      $node = node_load($nid);
  77      if (isset($node->book)) {
  78        $tree = book_menu_subtree_data($node->book);
  79        $contents = book_export_traverse($tree, 'book_node_export');
  80        return theme('book_export_html', $node->title, $contents, $node->book['depth']);
  81      }
  82      else {
  83        drupal_not_found();
  84      }
  85    }
  86    else {
  87      drupal_access_denied();
  88    }
  89  }
  90  
  91  /**
  92   * Menu callback; show the outline form for a single node.
  93   */
  94  function book_outline($node) {
  95    drupal_set_title(check_plain($node->title));
  96    return drupal_get_form('book_outline_form', $node);
  97  }
  98  
  99  /**
 100   * Build the form to handle all book outline operations via the outline tab.
 101   *
 102   * @see book_outline_form_submit()
 103   * @see book_remove_button_submit()
 104   *
 105   * @ingroup forms
 106   */
 107  function book_outline_form(&$form_state, $node) {
 108  
 109    if (!isset($node->book)) {
 110      // The node is not part of any book yet - set default options.
 111      $node->book = _book_link_defaults($node->nid);
 112    }
 113    else {
 114      $node->book['original_bid'] = $node->book['bid'];
 115    }
 116    // Find the depth limit for the parent select.
 117    if (!isset($node->book['parent_depth_limit'])) {
 118      $node->book['parent_depth_limit'] = _book_parent_depth_limit($node->book);
 119    }
 120    $form['#node'] = $node;
 121    $form['#id'] = 'book-outline';
 122    _book_add_form_elements($form, $node);
 123  
 124    $form['book']['#collapsible'] = FALSE;
 125  
 126    $form['update'] = array(
 127      '#type' => 'submit',
 128      '#value' => $node->book['original_bid'] ? t('Update book outline') : t('Add to book outline'),
 129      '#weight' => 15,
 130    );
 131  
 132    $form['remove'] = array(
 133      '#type' => 'submit',
 134      '#value' => t('Remove from book outline'),
 135      '#access' => $node->nid != $node->book['bid'] && $node->book['bid'],
 136      '#weight' => 20,
 137      '#submit' => array('book_remove_button_submit'),
 138    );
 139  
 140    return $form;
 141  }
 142  
 143  /**
 144   * Button submit function to redirect to removal confirm form.
 145   *
 146   * @see book_outline_form()
 147   */
 148  function book_remove_button_submit($form, &$form_state) {
 149    $form_state['redirect'] = 'node/'. $form['#node']->nid .'/outline/remove';
 150  }
 151  
 152  /**
 153   * Handles book outline form submissions from the outline tab.
 154   *
 155   * @see book_outline_form()
 156   */
 157  function book_outline_form_submit($form, &$form_state) {
 158    $node = $form['#node'];
 159    $form_state['redirect'] = "node/". $node->nid;
 160    $book_link = $form_state['values']['book'];
 161    if (!$book_link['bid']) {
 162      drupal_set_message(t('No changes were made'));
 163      return;
 164    }
 165  
 166    $book_link['menu_name'] = book_menu_name($book_link['bid']);
 167    $node->book = $book_link;
 168    if (_book_update_outline($node)) {
 169      if ($node->book['parent_mismatch']) {
 170        // This will usually only happen when JS is disabled.
 171        drupal_set_message(t('The post has been added to the selected book. You may now position it relative to other pages.'));
 172        $form_state['redirect'] = "node/". $node->nid ."/outline";
 173      }
 174      else {
 175        drupal_set_message(t('The book outline has been updated.'));
 176      }
 177    }
 178    else {
 179      drupal_set_message(t('There was an error adding the post to the book.'), 'error');
 180    }
 181  }
 182  
 183  /**
 184   * Menu callback; builds a form to confirm removal of a node from the book.
 185   *
 186   * @see book_remove_form_submit()
 187   *
 188   * @ingroup forms
 189   */
 190  function book_remove_form(&$form_state, $node) {
 191    $form['#node'] = $node;
 192    $title = array('%title' => $node->title);
 193  
 194    if ($node->book['has_children']) {
 195      $description = t('%title has associated child pages, which will be relocated automatically to maintain their connection to the book. To recreate the hierarchy (as it was before removing this page), %title may be added again using the Outline tab, and each of its former child pages will need to be relocated manually.', $title);
 196    }
 197    else {
 198      $description = t('%title may be added to hierarchy again using the Outline tab.', $title);
 199    }
 200  
 201    return confirm_form($form, t('Are you sure you want to remove %title from the book hierarchy?', $title), 'node/'. $node->nid, $description, t('Remove'));
 202  }
 203  
 204  /**
 205   * Confirm form submit function to remove a node from the book.
 206   *
 207   * @see book_remove_form()
 208   */
 209  function book_remove_form_submit($form, &$form_state) {
 210    $node = $form['#node'];
 211    if ($node->nid != $node->book['bid']) {
 212      // Only allowed when this is not a book (top-level page).
 213      menu_link_delete($node->book['mlid']);
 214      db_query('DELETE FROM {book} WHERE nid = %d', $node->nid);
 215      drupal_set_message(t('The post has been removed from the book.'));
 216    }
 217    $form_state['redirect'] = 'node/'. $node->nid;
 218  }
 219  
 220  /**
 221   * Renders a new parent page select element when the book selection changes.
 222   *
 223   * This function is called via AJAX when the selected book is changed on a node
 224   * or book outline form. It creates a new parent page select element, adds it
 225   * to the cached form, and then returns the rendered element so it can be
 226   * displayed on the form.
 227   *
 228   * @return
 229   *   The rendered parent page select element.
 230   */
 231  function book_form_update() {
 232    $bid = $_POST['book']['bid'];
 233    if ($form = form_get_cache($_POST['form_build_id'], $form_state)) {
 234  
 235      // Validate the bid.
 236      if (isset($form['book']['bid']['#options'][$bid])) {
 237        $book_link = $form['#node']->book;
 238        $book_link['bid'] = $bid;
 239        // Get the new options and update the cache.
 240        $form['book']['plid'] = _book_parent_select($book_link);
 241        form_set_cache($_POST['form_build_id'], $form, $form_state);
 242  
 243        // Build and render the new select element, then return it in JSON format.
 244        $form_state = array();
 245        $form['#post'] = array();
 246        $form = form_builder($form['form_id']['#value'] , $form, $form_state);
 247        $output = drupal_render($form['book']['plid']);
 248        drupal_json(array('status' => TRUE, 'data' => $output));
 249      }
 250      else {
 251        drupal_json(array('status' => FALSE, 'data' => ''));
 252      }
 253    }
 254    else {
 255      drupal_json(array('status' => FALSE, 'data' => ''));
 256    }
 257    exit();
 258  }


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