00001 <?php
00002
00003
00012 function book_render() {
00013 $book_list = array();
00014 foreach (book_get_books() as $book) {
00015 $book_list[] = l($book['title'], $book['href'], $book['options']);
00016 }
00017
00018 return theme('item_list', $book_list);
00019 }
00020
00042 function book_export($type, $nid) {
00043 $type = drupal_strtolower($type);
00044
00045 $export_function = 'book_export_' . $type;
00046
00047 if (function_exists($export_function)) {
00048 print call_user_func($export_function, $nid);
00049 }
00050 else {
00051 drupal_set_message(t('Unknown export format.'));
00052 drupal_not_found();
00053 }
00054 }
00055
00073 function book_export_html($nid) {
00074 if (user_access('access printer-friendly version')) {
00075 $export_data = array();
00076 $node = node_load($nid);
00077 if (isset($node->book)) {
00078 $tree = book_menu_subtree_data($node->book);
00079 $contents = book_export_traverse($tree, 'book_node_export');
00080 }
00081
00082 return theme('book_export_html', $node->title, $contents, $node->book['depth']);
00083 }
00084 else {
00085 drupal_access_denied();
00086 }
00087 }
00088
00092 function book_outline($node) {
00093 drupal_set_title(check_plain($node->title));
00094 return drupal_get_form('book_outline_form', $node);
00095 }
00096
00105 function book_outline_form(&$form_state, $node) {
00106 if (!isset($node->book)) {
00107
00108 $node->book = _book_link_defaults($node->nid);
00109 }
00110 else {
00111 $node->book['original_bid'] = $node->book['bid'];
00112 }
00113
00114
00115 if (!isset($node->book['parent_depth_limit'])) {
00116 $node->book['parent_depth_limit'] = _book_parent_depth_limit($node->book);
00117 }
00118 $form['#node'] = $node;
00119 $form['#id'] = 'book-outline';
00120 _book_add_form_elements($form, $node);
00121
00122 $form['book']['#collapsible'] = FALSE;
00123
00124 $form['update'] = array(
00125 '#type' => 'submit',
00126 '#value' => $node->book['original_bid'] ? t('Update book outline') : t('Add to book outline'),
00127 '#weight' => 15,
00128 );
00129
00130 $form['remove'] = array(
00131 '#type' => 'submit',
00132 '#value' => t('Remove from book outline'),
00133 '#access' => $node->nid != $node->book['bid'] && $node->book['bid'],
00134 '#weight' => 20,
00135 '#submit' => array('book_remove_button_submit'),
00136 );
00137
00138 return $form;
00139 }
00140
00146 function book_remove_button_submit($form, &$form_state) {
00147 $form_state['redirect'] = 'node/' . $form['#node']->nid . '/outline/remove';
00148 }
00149
00155 function book_outline_form_submit($form, &$form_state) {
00156 $node = $form['#node'];
00157 $form_state['redirect'] = "node/" . $node->nid;
00158 $book_link = $form_state['values']['book'];
00159 if (!$book_link['bid']) {
00160 drupal_set_message(t('No changes were made'));
00161
00162 return;
00163 }
00164
00165 $book_link['menu_name'] = book_menu_name($book_link['bid']);
00166 $node->book = $book_link;
00167 if (_book_update_outline($node)) {
00168 if ($node->book['parent_mismatch']) {
00169
00170 drupal_set_message(t('The post has been added to the selected book. You may now position it relative to other pages.'));
00171 $form_state['redirect'] = "node/" . $node->nid . "/outline";
00172 }
00173 else {
00174 drupal_set_message(t('The book outline has been updated.'));
00175 }
00176 }
00177 else {
00178 drupal_set_message(t('There was an error adding the post to the book.'), 'error');
00179 }
00180 }
00181
00189 function book_remove_form(&$form_state, $node) {
00190 $form['#node'] = $node;
00191 $title = array('%title' => $node->title);
00192
00193 if ($node->book['has_children']) {
00194 $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);
00195 }
00196 else {
00197 $description = t('%title may be added to hierarchy again using the Outline tab.', $title);
00198 }
00199
00200 return confirm_form($form, t('Are you sure you want to remove %title from the book hierarchy?', $title), 'node/' . $node->nid, $description, t('Remove'));
00201 }
00202
00208 function book_remove_form_submit($form, &$form_state) {
00209 $node = $form['#node'];
00210 if ($node->nid != $node->book['bid']) {
00211
00212 menu_link_delete($node->book['mlid']);
00213 db_query('DELETE FROM {book} WHERE nid = %d', $node->nid);
00214 drupal_set_message(t('The post has been removed from the book.'));
00215 }
00216 $form_state['redirect'] = 'node/' . $node->nid;
00217 }
00218
00234 function book_form_update() {
00235 $cid = 'form_' . $_POST['form_build_id'];
00236 $bid = $_POST['book']['bid'];
00237 $cache = cache_get($cid, 'cache_form');
00238 if ($cache) {
00239 $form = $cache->data;
00240
00241
00242 if (isset($form['book']['bid']['#options'][$bid])) {
00243 $book_link = $form['#node']->book;
00244 $book_link['bid'] = $bid;
00245
00246 $form['book']['plid'] = _book_parent_select($book_link);
00247 cache_set($cid, $form, 'cache_form', $cache->expire);
00248
00249 $form_state = array();
00250 $form['#post'] = array();
00251 $form = form_builder($form['form_id']['#value'] , $form, $form_state);
00252 $output = drupal_render($form['book']['plid']);
00253 drupal_json(array('status' => TRUE, 'data' => $output));
00254 }
00255 else {
00256 drupal_json(array('status' => FALSE, 'data' => ''));
00257 }
00258 }
00259 else {
00260 drupal_json(array('status' => FALSE, 'data' => ''));
00261 }
00262 exit();
00263 }