/[drupal]/drupal/modules/book/book.pages.inc
ViewVC logotype

Contents of /drupal/modules/book/book.pages.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.20 - (show annotations) (download) (as text)
Sun Nov 1 12:11:10 2009 UTC (3 weeks, 6 days ago) by dries
Branch: MAIN
Changes since 1.19: +4 -4 lines
File MIME type: text/x-php
- Patch #595084 by c960657: use type hinting for .
1 <?php
2 // $Id: book.pages.inc,v 1.19 2009/10/15 14:07:27 dries Exp $
3
4 /**
5 * @file
6 * User page callbacks for the book module.
7 */
8
9 /**
10 * Menu callback; prints a listing of all books.
11 */
12 function book_render() {
13 $book_list = array();
14 foreach (book_get_books() as $book) {
15 $book_list[] = l($book['title'], $book['href'], $book['options']);
16 }
17
18 return theme('item_list', array('items' => $book_list));
19 }
20
21 /**
22 * Menu callback; Generates various representation of a book page and its children.
23 *
24 * The function delegates the generation of output to helper functions.
25 * The function name is derived by prepending 'book_export_' to the
26 * given output type. So, e.g., a type of 'html' results in a call to
27 * the function book_export_html().
28 *
29 * @param $type
30 * A string encoding the type of output requested. The following
31 * types are currently supported in book module:
32 *
33 * - html: HTML (printer friendly output)
34 *
35 * Other types may be supported in contributed modules.
36 * @param $nid
37 * An integer representing the node id (nid) of the node to export
38 * @return
39 * A string representing the node and its children in the book hierarchy
40 * in a format determined by the $type parameter.
41 */
42 function book_export($type, $nid) {
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 }
81
82 return theme('book_export_html', array('title' => $node->title[FIELD_LANGUAGE_NONE][0]['value'], 'contents' => $contents, 'depth' => $node->book['depth']));
83 }
84 else {
85 drupal_access_denied();
86 }
87 }
88
89 /**
90 * Menu callback; show the outline form for a single node.
91 */
92 function book_outline(stdClass $node) {
93 drupal_set_title($node->title[FIELD_LANGUAGE_NONE][0]['value']);
94 return drupal_get_form('book_outline_form', $node);
95 }
96
97 /**
98 * Build the form to handle all book outline operations via the outline tab.
99 *
100 * @see book_outline_form_submit()
101 * @see book_remove_button_submit()
102 *
103 * @ingroup forms
104 */
105 function book_outline_form($form, &$form_state, stdClass $node) {
106 if (!isset($node->book)) {
107 // The node is not part of any book yet - set default options.
108 $node->book = _book_link_defaults($node->nid);
109 }
110 else {
111 $node->book['original_bid'] = $node->book['bid'];
112 }
113
114 // Find the depth limit for the parent select.
115 if (!isset($node->book['parent_depth_limit'])) {
116 $node->book['parent_depth_limit'] = _book_parent_depth_limit($node->book);
117 }
118 $form['#node'] = $node;
119 $form['#id'] = 'book-outline';
120 _book_add_form_elements($form, $node);
121
122 $form['book']['#collapsible'] = FALSE;
123
124 $form['update'] = array(
125 '#type' => 'submit',
126 '#value' => $node->book['original_bid'] ? t('Update book outline') : t('Add to book outline'),
127 '#weight' => 15,
128 );
129
130 $form['remove'] = array(
131 '#type' => 'submit',
132 '#value' => t('Remove from book outline'),
133 '#access' => $node->nid != $node->book['bid'] && $node->book['bid'],
134 '#weight' => 20,
135 '#submit' => array('book_remove_button_submit'),
136 );
137
138 return $form;
139 }
140
141 /**
142 * Button submit function to redirect to removal confirm form.
143 *
144 * @see book_outline_form()
145 */
146 function book_remove_button_submit($form, &$form_state) {
147 $form_state['redirect'] = 'node/' . $form['#node']->nid . '/outline/remove';
148 }
149
150 /**
151 * Handles book outline form submissions from the outline tab.
152 *
153 * @see book_outline_form()
154 */
155 function book_outline_form_submit($form, &$form_state) {
156 $node = $form['#node'];
157 $form_state['redirect'] = "node/" . $node->nid;
158 $book_link = $form_state['values']['book'];
159 if (!$book_link['bid']) {
160 drupal_set_message(t('No changes were made'));
161
162 return;
163 }
164
165 $book_link['menu_name'] = book_menu_name($book_link['bid']);
166 $node->book = $book_link;
167 if (_book_update_outline($node)) {
168 if ($node->book['parent_mismatch']) {
169 // This will usually only happen when JS is disabled.
170 drupal_set_message(t('The post has been added to the selected book. You may now position it relative to other pages.'));
171 $form_state['redirect'] = "node/" . $node->nid . "/outline";
172 }
173 else {
174 drupal_set_message(t('The book outline has been updated.'));
175 }
176 }
177 else {
178 drupal_set_message(t('There was an error adding the post to the book.'), 'error');
179 }
180 }
181
182 /**
183 * Menu callback; builds a form to confirm removal of a node from the book.
184 *
185 * @see book_remove_form_submit()
186 *
187 * @ingroup forms
188 */
189 function book_remove_form($form, &$form_state, stdClass $node) {
190 $form['#node'] = $node;
191 $title = array('%title' => $node->title[FIELD_LANGUAGE_NONE][0]['value']);
192
193 if ($node->book['has_children']) {
194 $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);
195 }
196 else {
197 $description = t('%title may be added to hierarchy again using the Outline tab.', $title);
198 }
199
200 return confirm_form($form, t('Are you sure you want to remove %title from the book hierarchy?', $title), 'node/' . $node->nid, $description, t('Remove'));
201 }
202
203 /**
204 * Confirm form submit function to remove a node from the book.
205 *
206 * @see book_remove_form()
207 */
208 function book_remove_form_submit($form, &$form_state) {
209 $node = $form['#node'];
210 if ($node->nid != $node->book['bid']) {
211 // Only allowed when this is not a book (top-level page).
212 menu_link_delete($node->book['mlid']);
213 db_delete('book')
214 ->condition('nid', $node->nid)
215 ->execute();
216 drupal_set_message(t('The post has been removed from the book.'));
217 }
218 $form_state['redirect'] = 'node/' . $node->nid;
219 }
220
221 /**
222 * AJAX callback to replace the book parent select options.
223 *
224 * This function is called when the selected book is changed. It updates the
225 * cached form (either the node form or the book outline form) and returns
226 * rendered output to be used to replace the select containing the possible
227 * parent pages in the newly selected book.
228 *
229 * @param $build_id
230 * The form's build_id.
231 * @param $bid
232 * A bid from from among those in the form's book select.
233 * @return
234 * Prints the replacement HTML in JSON format.
235 */
236 function book_form_update() {
237 // Load the form based upon the $_POST data sent via the ajax call.
238 list($form, $form_state) = ajax_get_form();
239
240 $commands = array();
241 $bid = $_POST['book']['bid'];
242
243 // Validate the bid.
244 if (isset($form['book']['bid']['#options'][$bid])) {
245 $book_link = $form['#node']->book;
246 $book_link['bid'] = $bid;
247 // Get the new options and update the cache.
248 $form['book']['plid'] = _book_parent_select($book_link);
249 form_set_cache($form['values']['form_build_id'], $form, $form_state);
250
251 // Build and render the new select element, then return it in JSON format.
252 $form_state = array();
253 $form = form_builder($form['form_id']['#value'], $form, $form_state);
254
255 $commands[] = ajax_command_replace(NULL, drupal_render($form['book']['plid']));
256 }
257
258 // @todo: We could and should just return $form['book']['plid'] and skip the
259 // ajax_command_replace() above. But for now, this provides a test case of
260 // returning an AJAX commands array.
261 return array('#type' => 'ajax_commands', '#ajax_commands' => $commands);
262 }

  ViewVC Help
Powered by ViewVC 1.1.2