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 | // Check that the node exists and that the current user has access to it. |
---|
43 | $node = node_load($nid); |
---|
44 | if (!$node) { |
---|
45 | return MENU_NOT_FOUND; |
---|
46 | } |
---|
47 | if (!node_access('view', $node)) { |
---|
48 | return MENU_ACCESS_DENIED; |
---|
49 | } |
---|
50 | |
---|
51 | $type = drupal_strtolower($type); |
---|
52 | |
---|
53 | $export_function = 'book_export_'. $type; |
---|
54 | |
---|
55 | if (function_exists($export_function)) { |
---|
56 | print call_user_func($export_function, $nid); |
---|
57 | } |
---|
58 | else { |
---|
59 | drupal_set_message(t('Unknown export format.')); |
---|
60 | drupal_not_found(); |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * This function is called by book_export() to generate HTML for export. |
---|
66 | * |
---|
67 | * The given node is /embedded to its absolute depth in a top level |
---|
68 | * section/. For example, a child node with depth 2 in the hierarchy |
---|
69 | * is contained in (otherwise empty) <div> elements |
---|
70 | * corresponding to depth 0 and depth 1. This is intended to support |
---|
71 | * WYSIWYG output - e.g., level 3 sections always look like level 3 |
---|
72 | * sections, no matter their depth relative to the node selected to be |
---|
73 | * exported as printer-friendly HTML. |
---|
74 | * |
---|
75 | * @param $nid |
---|
76 | * An integer representing the node id (nid) of the node to export. |
---|
77 | * @return |
---|
78 | * A string containing HTML representing the node and its children in |
---|
79 | * the book hierarchy. |
---|
80 | */ |
---|
81 | function book_export_html($nid) { |
---|
82 | if (user_access('access printer-friendly version')) { |
---|
83 | $export_data = array(); |
---|
84 | $node = node_load($nid); |
---|
85 | if (isset($node->book)) { |
---|
86 | $tree = book_menu_subtree_data($node->book); |
---|
87 | $contents = book_export_traverse($tree, 'book_node_export'); |
---|
88 | return theme('book_export_html', $node->title, $contents, $node->book['depth']); |
---|
89 | } |
---|
90 | else { |
---|
91 | drupal_not_found(); |
---|
92 | } |
---|
93 | } |
---|
94 | else { |
---|
95 | drupal_access_denied(); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Menu callback; show the outline form for a single node. |
---|
101 | */ |
---|
102 | function book_outline($node) { |
---|
103 | drupal_set_title(check_plain($node->title)); |
---|
104 | return drupal_get_form('book_outline_form', $node); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Build the form to handle all book outline operations via the outline tab. |
---|
109 | * |
---|
110 | * @see book_outline_form_submit() |
---|
111 | * @see book_remove_button_submit() |
---|
112 | * |
---|
113 | * @ingroup forms |
---|
114 | */ |
---|
115 | function book_outline_form(&$form_state, $node) { |
---|
116 | |
---|
117 | if (!isset($node->book)) { |
---|
118 | // The node is not part of any book yet - set default options. |
---|
119 | $node->book = _book_link_defaults($node->nid); |
---|
120 | } |
---|
121 | else { |
---|
122 | $node->book['original_bid'] = $node->book['bid']; |
---|
123 | } |
---|
124 | // Find the depth limit for the parent select. |
---|
125 | if (!isset($node->book['parent_depth_limit'])) { |
---|
126 | $node->book['parent_depth_limit'] = _book_parent_depth_limit($node->book); |
---|
127 | } |
---|
128 | $form['#node'] = $node; |
---|
129 | $form['#id'] = 'book-outline'; |
---|
130 | _book_add_form_elements($form, $node); |
---|
131 | |
---|
132 | $form['book']['#collapsible'] = FALSE; |
---|
133 | |
---|
134 | $form['update'] = array( |
---|
135 | '#type' => 'submit', |
---|
136 | '#value' => $node->book['original_bid'] ? t('Update book outline') : t('Add to book outline'), |
---|
137 | '#weight' => 15, |
---|
138 | ); |
---|
139 | |
---|
140 | $form['remove'] = array( |
---|
141 | '#type' => 'submit', |
---|
142 | '#value' => t('Remove from book outline'), |
---|
143 | '#access' => $node->nid != $node->book['bid'] && $node->book['bid'], |
---|
144 | '#weight' => 20, |
---|
145 | '#submit' => array('book_remove_button_submit'), |
---|
146 | ); |
---|
147 | |
---|
148 | return $form; |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * Button submit function to redirect to removal confirm form. |
---|
153 | * |
---|
154 | * @see book_outline_form() |
---|
155 | */ |
---|
156 | function book_remove_button_submit($form, &$form_state) { |
---|
157 | $form_state['redirect'] = 'node/'. $form['#node']->nid .'/outline/remove'; |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * Handles book outline form submissions from the outline tab. |
---|
162 | * |
---|
163 | * @see book_outline_form() |
---|
164 | */ |
---|
165 | function book_outline_form_submit($form, &$form_state) { |
---|
166 | $node = $form['#node']; |
---|
167 | $form_state['redirect'] = "node/". $node->nid; |
---|
168 | $book_link = $form_state['values']['book']; |
---|
169 | if (!$book_link['bid']) { |
---|
170 | drupal_set_message(t('No changes were made')); |
---|
171 | return; |
---|
172 | } |
---|
173 | |
---|
174 | $book_link['menu_name'] = book_menu_name($book_link['bid']); |
---|
175 | $node->book = $book_link; |
---|
176 | if (_book_update_outline($node)) { |
---|
177 | if ($node->book['parent_mismatch']) { |
---|
178 | // This will usually only happen when JS is disabled. |
---|
179 | drupal_set_message(t('The post has been added to the selected book. You may now position it relative to other pages.')); |
---|
180 | $form_state['redirect'] = "node/". $node->nid ."/outline"; |
---|
181 | } |
---|
182 | else { |
---|
183 | drupal_set_message(t('The book outline has been updated.')); |
---|
184 | } |
---|
185 | } |
---|
186 | else { |
---|
187 | drupal_set_message(t('There was an error adding the post to the book.'), 'error'); |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | /** |
---|
192 | * Menu callback; builds a form to confirm removal of a node from the book. |
---|
193 | * |
---|
194 | * @see book_remove_form_submit() |
---|
195 | * |
---|
196 | * @ingroup forms |
---|
197 | */ |
---|
198 | function book_remove_form(&$form_state, $node) { |
---|
199 | $form['#node'] = $node; |
---|
200 | $title = array('%title' => $node->title); |
---|
201 | |
---|
202 | if ($node->book['has_children']) { |
---|
203 | $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); |
---|
204 | } |
---|
205 | else { |
---|
206 | $description = t('%title may be added to hierarchy again using the Outline tab.', $title); |
---|
207 | } |
---|
208 | |
---|
209 | return confirm_form($form, t('Are you sure you want to remove %title from the book hierarchy?', $title), 'node/'. $node->nid, $description, t('Remove')); |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | * Confirm form submit function to remove a node from the book. |
---|
214 | * |
---|
215 | * @see book_remove_form() |
---|
216 | */ |
---|
217 | function book_remove_form_submit($form, &$form_state) { |
---|
218 | $node = $form['#node']; |
---|
219 | if ($node->nid != $node->book['bid']) { |
---|
220 | // Only allowed when this is not a book (top-level page). |
---|
221 | menu_link_delete($node->book['mlid']); |
---|
222 | db_query('DELETE FROM {book} WHERE nid = %d', $node->nid); |
---|
223 | drupal_set_message(t('The post has been removed from the book.')); |
---|
224 | } |
---|
225 | $form_state['redirect'] = 'node/'. $node->nid; |
---|
226 | } |
---|
227 | |
---|
228 | /** |
---|
229 | * Renders a new parent page select element when the book selection changes. |
---|
230 | * |
---|
231 | * This function is called via AJAX when the selected book is changed on a node |
---|
232 | * or book outline form. It creates a new parent page select element, adds it |
---|
233 | * to the cached form, and then returns the rendered element so it can be |
---|
234 | * displayed on the form. |
---|
235 | * |
---|
236 | * @return |
---|
237 | * The rendered parent page select element. |
---|
238 | */ |
---|
239 | function book_form_update() { |
---|
240 | $bid = $_POST['book']['bid']; |
---|
241 | if ($form = form_get_cache($_POST['form_build_id'], $form_state)) { |
---|
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($_POST['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['#post'] = array(); |
---|
254 | $form = form_builder($form['form_id']['#value'] , $form, $form_state); |
---|
255 | $output = drupal_render($form['book']['plid']); |
---|
256 | drupal_json(array('status' => TRUE, 'data' => $output)); |
---|
257 | } |
---|
258 | else { |
---|
259 | drupal_json(array('status' => FALSE, 'data' => '')); |
---|
260 | } |
---|
261 | } |
---|
262 | else { |
---|
263 | drupal_json(array('status' => FALSE, 'data' => '')); |
---|
264 | } |
---|
265 | exit(); |
---|
266 | } |
---|