book.module

  1. drupal
    1. 4.6 modules/book.module
    2. 4.7 modules/book.module
    3. 5 modules/book/book.module
    4. 6 modules/book/book.module
    5. 7 modules/book/book.module
    6. 8 core/modules/book/book.module

Allows users to collaboratively author a book.

Functions & methods

NameDescription
book_accessImplementation of hook_access().
book_adminMenu callback; displays the book administration page.
book_admin_editDisplay an administrative view of the hierarchy of a book.
book_admin_edit_submit
book_admin_orphanMenu callback; displays a listing of all orphaned book pages.
book_admin_overviewReturns an administrative overview of all books.
book_blockImplementation of hook_block().
book_contentReturns the content of a given node. If $teaser if true, returns the teaser rather than full content. Displays the most recently approved revision of a node (if any) unless we have to display this page in the context of the moderation queue.
book_deleteImplementation of hook_delete().
book_exportMenu callback; Generates various representation of a book page with all descendants and prints the requested representation to output.
book_export_htmlThis function is called by book_export() to generate HTML for export.
book_formImplementation of hook_form().
book_helpImplementation of hook_help().
book_insertImplementation of hook_insert().
book_linkImplementation of hook_link().
book_loadImplementation of hook_load().
book_locationGiven a node, this function returns an array of 'book node' objects representing the path in the book tree from the root to the parent of the given node.
book_location_downGiven a node, this function returns an array of 'book node' objects representing the path in the book tree from the given node down to the last sibling of it.
book_menuImplementation of hook_menu().
book_nextFetches the node object of the next page of the book.
book_nodeapiImplementation of hook_nodeapi().
book_node_infoImplementation of hook_node_info().
book_node_visitor_html_postFinishes up generation of printer-friendly HTML after visiting a node. This function is a 'post-node' visitor function for book_recurse().
book_node_visitor_html_preGenerates printer-friendly HTML for a node. This function is a 'pre-node' visitor function for book_recurse().
book_outlineImplementation of function book_outline() Handles all book outline operations.
book_outline_submitHandles book outline form submissions.
book_permImplementation of hook_perm().
book_prevFetches the node object of the previous page of the book.
book_recurseTraverses the book tree. Applies the $visit_pre() callback to each node, is called recursively for each child of the node (in weight, title order). Finally appends the output of the $visit_post() callback to the output before returning the generated…
book_renderMenu callback; prints a listing of all books.
book_submitImplementation of hook_submit().
book_tocReturns an array of titles and nid entries of book pages in table of contents order.
book_toc_recurseThis is a helper function for book_toc().
book_treeReturns an HTML nested list (wrapped in a menu-class div) representing the book nodes as a tree.
book_tree_recurseThis is a helper function for book_tree()
book_updateImplementation of hook_update().
book_viewImplementation of hook_view().
theme_book_admin_table
theme_book_export_htmlHow the book's HTML export should be themed
theme_book_navigationPrepares the links to children (TOC) and forward/backward navigation for a node presented as a book page.
_book_admin_table
_book_admin_table_tree

File

modules/book.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Allows users to collaboratively author a book.
  5. */
  6. /**
  7. * Implementation of hook_node_info().
  8. */
  9. function book_node_info() {
  10. return array('book' => array('name' => t('book page'), 'base' => 'book'));
  11. }
  12. /**
  13. * Implementation of hook_perm().
  14. */
  15. function book_perm() {
  16. return array('outline posts in books', 'create book pages', 'create new books', 'edit book pages', 'edit own book pages', 'see printer-friendly version');
  17. }
  18. /**
  19. * Implementation of hook_access().
  20. */
  21. function book_access($op, $node) {
  22. global $user;
  23. if ($op == 'create') {
  24. // Only registered users can create book pages. Given the nature
  25. // of the book module this is considered to be a good/safe idea.
  26. return user_access('create book pages');
  27. }
  28. if ($op == 'update') {
  29. // Only registered users can update book pages. Given the nature
  30. // of the book module this is considered to be a good/safe idea.
  31. // One can only update a book page if there are no suggested updates
  32. // of that page waiting for approval. That is, only updates that
  33. // don't overwrite the current or pending information are allowed.
  34. if ((user_access('edit book pages') && !$node->moderate) || ($node->uid == $user->uid && user_access('edit own book pages'))) {
  35. return TRUE;
  36. }
  37. else {
  38. // do nothing. node-access() will determine further access
  39. }
  40. }
  41. }
  42. /**
  43. * Implementation of hook_link().
  44. */
  45. function book_link($type, $node = 0, $main = 0) {
  46. $links = array();
  47. if ($type == 'node' && isset($node->parent)) {
  48. if (!$main) {
  49. if (book_access('create', $node) && $node->status == 1) {
  50. $links[] = l(t('add child page'), "node/add/book/parent/$node->nid");
  51. }
  52. if (user_access('see printer-friendly version')) {
  53. $links[] = l(t('printer-friendly version'),
  54. 'book/export/html/'. $node->nid,
  55. array('title' => t('Show a printer-friendly version of this book page and its sub-pages.')));
  56. }
  57. }
  58. }
  59. return $links;
  60. }
  61. /**
  62. * Implementation of hook_menu().
  63. */
  64. function book_menu($may_cache) {
  65. $items = array();
  66. if ($may_cache) {
  67. $items[] = array(
  68. 'path' => 'node/add/book',
  69. 'title' => t('book page'),
  70. 'access' => user_access('create book pages'));
  71. $items[] = array(
  72. 'path' => 'admin/node/book',
  73. 'title' => t('books'),
  74. 'callback' => 'book_admin',
  75. 'access' => user_access('administer nodes'),
  76. 'type' => MENU_LOCAL_TASK,
  77. 'weight' => -1);
  78. $items[] = array(
  79. 'path' => 'admin/node/book/list',
  80. 'title' => t('list'),
  81. 'type' => MENU_DEFAULT_LOCAL_TASK);
  82. $items[] = array(
  83. 'path' => 'admin/node/book/orphan',
  84. 'title' => t('orphan pages'),
  85. 'callback' => 'book_admin_orphan',
  86. 'type' => MENU_LOCAL_TASK,
  87. 'weight' => 8);
  88. $items[] = array(
  89. 'path' => 'book',
  90. 'title' => t('books'),
  91. 'callback' => 'book_render',
  92. 'access' => user_access('access content'),
  93. 'type' => MENU_SUGGESTED_ITEM);
  94. $items[] = array(
  95. 'path' => 'book/export',
  96. 'callback' => 'book_export',
  97. 'access' => user_access('access content'),
  98. 'type' => MENU_CALLBACK);
  99. }
  100. else {
  101. // To avoid SQL overhead, check whether we are on a node page and whether the
  102. // user is allowed to outline posts in books.
  103. if (arg(0) == 'node' && is_numeric(arg(1)) && user_access('outline posts in books')) {
  104. // Only add the outline-tab for non-book pages:
  105. $result = db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.nid = %d AND n.type != 'book'"), arg(1));
  106. if (db_num_rows($result) > 0) {
  107. $items[] = array(
  108. 'path' => 'node/'. arg(1) .'/outline',
  109. 'title' => t('outline'),
  110. 'callback' => 'book_outline',
  111. 'callback arguments' => array(arg(1)),
  112. 'access' => user_access('outline posts in books'),
  113. 'type' => MENU_LOCAL_TASK,
  114. 'weight' => 2);
  115. }
  116. }
  117. }
  118. return $items;
  119. }
  120. /**
  121. * Implementation of hook_block().
  122. *
  123. * Displays the book table of contents in a block when the current page is a
  124. * single-node view of a book node.
  125. */
  126. function book_block($op = 'list', $delta = 0) {
  127. $block = array();
  128. if ($op == 'list') {
  129. $block[0]['info'] = t('Book navigation');
  130. return $block;
  131. }
  132. else if ($op == 'view') {
  133. // Only display this block when the user is browsing a book:
  134. if (arg(0) == 'node' && is_numeric(arg(1))) {
  135. $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.nid = %d'), arg(1));
  136. if (db_num_rows($result) > 0) {
  137. $node = db_fetch_object($result);
  138. $path = book_location($node);
  139. $path[] = $node;
  140. $expand = array();
  141. foreach ($path as $key => $node) {
  142. $expand[] = $node->nid;
  143. }
  144. $block['subject'] = check_plain($path[0]->title);
  145. $block['content'] = book_tree($expand[0], 5, $expand);
  146. }
  147. }
  148. return $block;
  149. }
  150. }
  151. /**
  152. * Implementation of hook_load().
  153. */
  154. function book_load($node) {
  155. $book = db_fetch_object(db_query('SELECT * FROM {book} WHERE vid = %d', $node->vid));
  156. return $book;
  157. }
  158. /**
  159. * Implementation of hook_insert().
  160. */
  161. function book_insert($node) {
  162. db_query("INSERT INTO {book} (nid, vid, parent, weight) VALUES (%d, %d, %d, %d)", $node->nid, $node->vid, $node->parent, $node->weight);
  163. }
  164. /**
  165. * Implementation of hook_update().
  166. */
  167. function book_update($node) {
  168. if ($node->revision) {
  169. db_query("INSERT INTO {book} (nid, vid, parent, weight) VALUES (%d, %d, %d, %d)", $node->nid, $node->vid, $node->parent, $node->weight);
  170. }
  171. else {
  172. db_query("UPDATE {book} SET parent = %d, weight = %d WHERE vid = %d", $node->parent, $node->weight, $node->vid);
  173. }
  174. }
  175. /**
  176. * Implementation of hook_delete().
  177. */
  178. function book_delete(&$node) {
  179. db_query('DELETE FROM {book} WHERE nid = %d', $node->nid);
  180. }
  181. /**
  182. * Implementation of hook_submit().
  183. */
  184. function book_submit(&$node) {
  185. global $user;
  186. // Set default values for non-administrators.
  187. if (!user_access('administer nodes')) {
  188. $node->weight = 0;
  189. $node->revision = 1;
  190. $node->uid = $user->uid;
  191. }
  192. }
  193. /**
  194. * Implementation of hook_form().
  195. */
  196. function book_form(&$node) {
  197. if ($node->nid && !$node->parent && !user_access('create new books')) {
  198. $form['parent'] = array('#type' => 'value', '#value' => $node->parent);
  199. }
  200. else {
  201. $form['parent'] = array('#type' => 'select',
  202. '#title' => t('Parent'),
  203. '#default_value' => ($node->parent ? $node->parent : arg(4)),
  204. '#options' => book_toc($node->nid),
  205. '#weight' => -4,
  206. '#description' => user_access('create new books') ? t('The parent section in which to place this page. Note that each page whose parent is &lt;top-level&gt; is an independent, top-level book.') : t('The parent that this page belongs in.'),
  207. );
  208. }
  209. $form['title'] = array('#type' => 'textfield',
  210. '#title' => t('Title'),
  211. '#required' => TRUE,
  212. '#default_value' => $node->title,
  213. '#weight' => -5,
  214. );
  215. $form['body_filter']['body'] = array('#type' => 'textarea',
  216. '#title' => t('Body'),
  217. '#default_value' => $node->body,
  218. '#rows' => 20,
  219. '#required' => TRUE,
  220. );
  221. $form['body_filter']['format'] = filter_form($node->format);
  222. $form['log'] = array(
  223. '#type' => 'textarea',
  224. '#title' => t('Log message'),
  225. '#weight' => 5,
  226. '#description' => t('An explanation of the additions or updates being made to help other authors understand your motivations.'),
  227. );
  228. if (user_access('administer nodes')) {
  229. $form['weight'] = array('#type' => 'weight',
  230. '#title' => t('Weight'),
  231. '#default_value' => $node->weight,
  232. '#delta' => 15,
  233. '#weight' => 5,
  234. '#description' => t('Pages at a given level are ordered first by weight and then by title.'),
  235. );
  236. }
  237. else {
  238. // If a regular user updates a book page, we create a new revision
  239. // authored by that user:
  240. $form['revision'] = array('#type' => 'hidden', '#value' => 1);
  241. }
  242. return $form;
  243. }
  244. /**
  245. * Implementation of function book_outline()
  246. * Handles all book outline operations.
  247. */
  248. function book_outline($nid) {
  249. $node = node_load($nid);
  250. $page = book_load($node);
  251. $form['parent'] = array('#type' => 'select',
  252. '#title' => t('Parent'),
  253. '#default_value' => $page->parent,
  254. '#options' => book_toc($node->nid),
  255. '#description' => t('The parent page in the book.'),
  256. );
  257. $form['weight'] = array('#type' => 'weight',
  258. '#title' => t('Weight'),
  259. '#default_value' => $page->weight,
  260. '#delta' => 15,
  261. '#description' => t('Pages at a given level are ordered first by weight and then by title.'),
  262. );
  263. $form['log'] = array('#type' => 'textarea',
  264. '#title' => t('Log message'),
  265. '#description' => t('An explanation to help other authors understand your motivations to put this post into the book.'),
  266. );
  267. $form['nid'] = array('#type' => 'value', '#value' => $nid);
  268. if ($page->nid) {
  269. $form['update'] = array('#type' => 'submit',
  270. '#value' => t('Update book outline'),
  271. );
  272. $form['remove'] = array('#type' => 'submit',
  273. '#value' => t('Remove from book outline'),
  274. );
  275. }
  276. else {
  277. $form['add'] = array('#type' => 'submit', '#value' => t('Add to book outline'));
  278. }
  279. drupal_set_title(check_plain($node->title));
  280. return drupal_get_form('book_outline', $form);
  281. }
  282. /**
  283. * Handles book outline form submissions.
  284. */
  285. function book_outline_submit($form_id, $form_values) {
  286. $op = $_POST['op'];
  287. $node = node_load($form_values['nid']);
  288. switch ($op) {
  289. case t('Add to book outline'):
  290. db_query('INSERT INTO {book} (nid, vid, parent, weight) VALUES (%d, %d, %d, %d)', $node->nid, $node->vid, $form_values['parent'], $form_values['weight']);
  291. db_query("UPDATE {node_revisions} SET log = '%s' WHERE vid = %d", $form_values['log'], $node->vid);
  292. drupal_set_message(t('The post has been added to the book.'));
  293. break;
  294. case t('Update book outline'):
  295. db_query('UPDATE {book} SET parent = %d, weight = %d WHERE vid = %d', $form_values['parent'], $form_values['weight'], $node->vid);
  296. db_query("UPDATE {node_revisions} SET log = '%s' WHERE vid = %d", $form_values['log'], $node->vid);
  297. drupal_set_message(t('The book outline has been updated.'));
  298. break;
  299. case t('Remove from book outline'):
  300. db_query('DELETE FROM {book} WHERE nid = %d', $node->nid);
  301. drupal_set_message(t('The post has been removed from the book.'));
  302. break;
  303. }
  304. return "node/$node->nid";
  305. }
  306. /**
  307. * Given a node, this function returns an array of 'book node' objects
  308. * representing the path in the book tree from the root to the
  309. * parent of the given node.
  310. *
  311. * @param $node
  312. * a book node object for which to compute the path
  313. *
  314. * @return
  315. * an array of book node objects representing the path nodes root to
  316. * parent of the given node. Returns an empty array if the node does
  317. * not exist or is not part of a book hierarchy.
  318. *
  319. */
  320. function book_location($node, $nodes = array()) {
  321. $parent = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.nid = %d'), $node->parent));
  322. if (isset($parent->title)) {
  323. $nodes = book_location($parent, $nodes);
  324. $nodes[] = $parent;
  325. }
  326. return $nodes;
  327. }
  328. /**
  329. * Given a node, this function returns an array of 'book node' objects
  330. * representing the path in the book tree from the given node down to
  331. * the last sibling of it.
  332. *
  333. * @param $node
  334. * a book node object where the path starts
  335. *
  336. * @return
  337. * an array of book node objects representing the path nodes from the
  338. * given node. Returns an empty array if the node does not exist or
  339. * is not part of a book hierarchy or there are no siblings.
  340. */
  341. function book_location_down($node, $nodes = array()) {
  342. $last_direct_child = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND b.parent = %d ORDER BY b.weight DESC, n.title DESC'), $node->nid));
  343. if ($last_direct_child) {
  344. $nodes[] = $last_direct_child;
  345. $nodes = book_location_down($last_direct_child, $nodes);
  346. }
  347. return $nodes;
  348. }
  349. /**
  350. * Fetches the node object of the previous page of the book.
  351. */
  352. function book_prev($node) {
  353. // If the parent is zero, we are at the start of a book so there is no previous.
  354. if ($node->parent == 0) {
  355. return NULL;
  356. }
  357. // Previous on the same level:
  358. $direct_above = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE b.parent = %d AND n.status = 1 AND n.moderate = 0 AND (b.weight < %d OR (b.weight = %d AND n.title < '%s')) ORDER BY b.weight DESC, n.title DESC"), $node->parent, $node->weight, $node->weight, $node->title));
  359. if ($direct_above) {
  360. // Get last leaf of $above.
  361. $path = book_location_down($direct_above);
  362. return $path ? (count($path) > 0 ? array_pop($path) : NULL) : $direct_above;
  363. }
  364. else {
  365. // Direct parent:
  366. $prev = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.nid = %d AND n.status = 1 AND n.moderate = 0'), $node->parent));
  367. return $prev;
  368. }
  369. }
  370. /**
  371. * Fetches the node object of the next page of the book.
  372. */
  373. function book_next($node) {
  374. // get first direct child
  375. $child = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE b.parent = %d AND n.status = 1 AND n.moderate = 0 ORDER BY b.weight ASC, n.title ASC'), $node->nid));
  376. if ($child) {
  377. return $child;
  378. }
  379. // No direct child: get next for this level or any parent in this book.
  380. $path = book_location($node); // Path to top-level node including this one.
  381. $path[] = $node;
  382. while (($leaf = array_pop($path)) && count($path)) {
  383. $next = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE b.parent = %d AND n.status = 1 AND n.moderate = 0 AND (b.weight > %d OR (b.weight = %d AND n.title > '%s')) ORDER BY b.weight ASC, n.title ASC"), $leaf->parent, $leaf->weight, $leaf->weight, $leaf->title));
  384. if ($next) {
  385. return $next;
  386. }
  387. }
  388. }
  389. /**
  390. * Returns the content of a given node. If $teaser if true, returns
  391. * the teaser rather than full content. Displays the most recently
  392. * approved revision of a node (if any) unless we have to display this
  393. * page in the context of the moderation queue.
  394. */
  395. function book_content($node, $teaser = FALSE) {
  396. // Return the page body.
  397. return node_prepare($node, $teaser);
  398. }
  399. /**
  400. * Implementation of hook_view().
  401. *
  402. * If not displayed on the main page, we render the node as a page in the
  403. * book with extra links to the previous and next pages.
  404. */
  405. function book_view(&$node, $teaser = FALSE, $page = FALSE) {
  406. $node = node_prepare($node, $teaser);
  407. }
  408. /**
  409. * Implementation of hook_nodeapi().
  410. *
  411. * Appends book navigation to all nodes in the book.
  412. */
  413. function book_nodeapi(&$node, $op, $teaser, $page) {
  414. switch ($op) {
  415. case 'view':
  416. if (!$teaser) {
  417. $book = db_fetch_array(db_query('SELECT * FROM {book} WHERE vid = %d', $node->vid));
  418. if ($book) {
  419. if ($node->moderate && user_access('administer nodes')) {
  420. drupal_set_message(t("The post has been submitted for moderation and won't be accessible until it has been approved."));
  421. }
  422. foreach ($book as $key => $value) {
  423. $node->$key = $value;
  424. }
  425. $path = book_location($node);
  426. // Construct the breadcrumb:
  427. $node->breadcrumb = array(); // Overwrite the trail with a book trail.
  428. foreach ($path as $level) {
  429. $node->breadcrumb[] = array('path' => 'node/'. $level->nid, 'title' => $level->title);
  430. }
  431. $node->breadcrumb[] = array('path' => 'node/'. $node->nid);
  432. $node->body .= theme('book_navigation', $node);
  433. if ($page) {
  434. menu_set_location($node->breadcrumb);
  435. }
  436. }
  437. }
  438. break;
  439. case 'delete revision':
  440. db_query('DELETE FROM {book} WHERE vid = %d', $node->vid);
  441. break;
  442. case 'delete':
  443. db_query('DELETE FROM {book} WHERE nid = %d', $node->nid);
  444. break;
  445. }
  446. }
  447. /**
  448. * Prepares the links to children (TOC) and forward/backward
  449. * navigation for a node presented as a book page.
  450. *
  451. * @ingroup themeable
  452. */
  453. function theme_book_navigation($node) {
  454. $output = '';
  455. $links = '';
  456. if ($node->nid) {
  457. $tree = book_tree($node->nid);
  458. if ($prev = book_prev($node)) {
  459. drupal_add_link(array('rel' => 'prev', 'href' => url('node/'. $prev->nid)));
  460. $links .= l(t('‹ ') . $prev->title, 'node/'. $prev->nid, array('class' => 'page-previous', 'title' => t('Go to previous page')));
  461. }
  462. if ($node->parent) {
  463. drupal_add_link(array('rel' => 'up', 'href' => url('node/'. $node->parent)));
  464. $links .= l(t('up'), 'node/'. $node->parent, array('class' => 'page-up', 'title' => t('Go to parent page')));
  465. }
  466. if ($next = book_next($node)) {
  467. drupal_add_link(array('rel' => 'next', 'href' => url('node/'. $next->nid)));
  468. $links .= l($next->title . t(' ›'), 'node/'. $next->nid, array('class' => 'page-next', 'title' => t('Go to next page')));
  469. }
  470. if (isset($tree) || isset($links)) {
  471. $output = '<div class="book-navigation">';
  472. if (isset($tree)) {
  473. $output .= $tree;
  474. }
  475. if (isset($links)) {
  476. $output .= '<div class="page-links">'. $links .'<br class="clear" /></div>';
  477. }
  478. $output .= '</div>';
  479. }
  480. }
  481. return $output;
  482. }
  483. /**
  484. * This is a helper function for book_toc().
  485. */
  486. function book_toc_recurse($nid, $indent, $toc, $children, $exclude) {
  487. if ($children[$nid]) {
  488. foreach ($children[$nid] as $foo => $node) {
  489. if (!$exclude || $exclude != $node->nid) {
  490. $toc[$node->nid] = $indent .' '. $node->title;
  491. $toc = book_toc_recurse($node->nid, $indent .'--', $toc, $children, $exclude);
  492. }
  493. }
  494. }
  495. return $toc;
  496. }
  497. /**
  498. * Returns an array of titles and nid entries of book pages in table of contents order.
  499. */
  500. function book_toc($exclude = 0) {
  501. $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 ORDER BY b.weight, n.title'));
  502. while ($node = db_fetch_object($result)) {
  503. if (!$children[$node->parent]) {
  504. $children[$node->parent] = array();
  505. }
  506. $children[$node->parent][] = $node;
  507. }
  508. $toc = array();
  509. // If the user has permission to create new books, add the top-level book page to the menu;
  510. if (user_access('create new books')) {
  511. $toc[0] = '<'. t('top-level') .'>';
  512. }
  513. $toc = book_toc_recurse(0, '', $toc, $children, $exclude);
  514. return $toc;
  515. }
  516. /**
  517. * This is a helper function for book_tree()
  518. */
  519. function book_tree_recurse($nid, $depth, $children, $unfold = array()) {
  520. $output = '';
  521. if ($depth > 0) {
  522. if (isset($children[$nid])) {
  523. foreach ($children[$nid] as $foo => $node) {
  524. if (in_array($node->nid, $unfold)) {
  525. if ($tree = book_tree_recurse($node->nid, $depth - 1, $children, $unfold)) {
  526. $output .= '<li class="expanded">';
  527. $output .= l($node->title, 'node/'. $node->nid);
  528. $output .= '<ul class="menu">'. $tree .'</ul>';
  529. $output .= '</li>';
  530. }
  531. else {
  532. $output .= '<li class="leaf">'. l($node->title, 'node/'. $node->nid) .'</li>';
  533. }
  534. }
  535. else {
  536. if ($tree = book_tree_recurse($node->nid, 1, $children)) {
  537. $output .= '<li class="collapsed">'. l($node->title, 'node/'. $node->nid) .'</li>';
  538. }
  539. else {
  540. $output .= '<li class="leaf">'. l($node->title, 'node/'. $node->nid) .'</li>';
  541. }
  542. }
  543. }
  544. }
  545. }
  546. return $output;
  547. }
  548. /**
  549. * Returns an HTML nested list (wrapped in a menu-class div) representing the book nodes
  550. * as a tree.
  551. */
  552. function book_tree($parent = 0, $depth = 3, $unfold = array()) {
  553. $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND n.moderate = 0 ORDER BY b.weight, n.title'));
  554. while ($node = db_fetch_object($result)) {
  555. $list = isset($children[$node->parent]) ? $children[$node->parent] : array();
  556. $list[] = $node;
  557. $children[$node->parent] = $list;
  558. }
  559. if ($tree = book_tree_recurse($parent, $depth, $children, $unfold)) {
  560. return '<ul class="menu">'. $tree .'</ul>';
  561. }
  562. }
  563. /**
  564. * Menu callback; prints a listing of all books.
  565. */
  566. function book_render() {
  567. $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE b.parent = 0 AND n.status = 1 AND n.moderate = 0 ORDER BY b.weight, n.title'));
  568. $books = array();
  569. while ($node = db_fetch_object($result)) {
  570. $books[] = l($node->title, 'node/'. $node->nid);
  571. }
  572. return theme('item_list', $books);
  573. }
  574. /**
  575. * Menu callback; Generates various representation of a book page with
  576. * all descendants and prints the requested representation to output.
  577. *
  578. * The function delegates the generation of output to helper functions.
  579. * The function name is derived by prepending 'book_export_' to the
  580. * given output type. So, e.g., a type of 'html' results in a call to
  581. * the function book_export_html().
  582. *
  583. * @param type
  584. * - a string encoding the type of output requested.
  585. * The following types are currently supported in book module
  586. * html: HTML (printer friendly output)
  587. * Other types are supported in contributed modules.
  588. * @param nid
  589. * - an integer representing the node id (nid) of the node to export
  590. *
  591. */
  592. function book_export($type = 'html', $nid = 0) {
  593. $type = drupal_strtolower($type);
  594. $node_result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.nid = %d'), $nid);
  595. if (db_num_rows($node_result) > 0) {
  596. $node = db_fetch_object($node_result);
  597. }
  598. $depth = count(book_location($node)) + 1;
  599. $export_function = 'book_export_' . $type;
  600. if (function_exists($export_function)) {
  601. print call_user_func($export_function, $nid, $depth);
  602. }
  603. else {
  604. drupal_set_message(t('Unknown export format.'));
  605. drupal_not_found();
  606. }
  607. }
  608. /**
  609. * This function is called by book_export() to generate HTML for export.
  610. *
  611. * The given node is /embedded to its absolute depth in a top level
  612. * section/. For example, a child node with depth 2 in the hierarchy
  613. * is contained in (otherwise empty) &lt;div&gt; elements
  614. * corresponding to depth 0 and depth 1. This is intended to support
  615. * WYSIWYG output - e.g., level 3 sections always look like level 3
  616. * sections, no matter their depth relative to the node selected to be
  617. * exported as printer-friendly HTML.
  618. *
  619. * @param nid
  620. * - an integer representing the node id (nid) of the node to export
  621. * @param depth
  622. * - an integer giving the depth in the book hierarchy of the node
  623. * which is to be exported
  624. *
  625. * @return
  626. * - string containing HTML representing the node and its children in
  627. * the book hierarchy
  628. */
  629. function book_export_html($nid, $depth) {
  630. if (user_access('see printer-friendly version')) {
  631. $node = node_load($nid);
  632. for ($i = 1; $i < $depth; $i++) {
  633. $content .= "<div class=\"section-$i\">\n";
  634. }
  635. $content .= book_recurse($nid, $depth, 'book_node_visitor_html_pre', 'book_node_visitor_html_post');
  636. for ($i = 1; $i < $depth; $i++) {
  637. $content .= "</div>\n";
  638. }
  639. return theme('book_export_html', check_plain($node->title), $content);
  640. }
  641. else {
  642. drupal_access_denied();
  643. }
  644. }
  645. /**
  646. * How the book's HTML export should be themed
  647. *
  648. * @ingroup themeable
  649. */
  650. function theme_book_export_html($title, $content) {
  651. global $base_url;
  652. $html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
  653. $html .= '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">';
  654. $html .= "<head>\n<title>". $title ."</title>\n";
  655. $html .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
  656. $html .= '<base href="'. $base_url .'/" />' . "\n";
  657. $html .= "<style type=\"text/css\">\n@import url(misc/print.css);\n</style>\n";
  658. $html .= "</head>\n<body>\n". $content . "\n</body>\n</html>\n";
  659. return $html;
  660. }
  661. /**
  662. * Traverses the book tree. Applies the $visit_pre() callback to each
  663. * node, is called recursively for each child of the node (in weight,
  664. * title order). Finally appends the output of the $visit_post()
  665. * callback to the output before returning the generated output.
  666. *
  667. * @param nid
  668. * - the node id (nid) of the root node of the book hierarchy.
  669. * @param depth
  670. * - the depth of the given node in the book hierarchy.
  671. * @param visit_pre
  672. * - a function callback to be called upon visiting a node in the tree
  673. * @param visit_post
  674. * - a function callback to be called after visiting a node in the tree,
  675. * but before recursively visiting children.
  676. * @return
  677. * - the output generated in visiting each node
  678. */
  679. function book_recurse($nid = 0, $depth = 1, $visit_pre, $visit_post) {
  680. $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND n.nid = %d AND n.moderate = 0 ORDER BY b.weight, n.title'), $nid);
  681. while ($page = db_fetch_object($result)) {
  682. // Load the node:
  683. $node = node_load($page->nid);
  684. if ($node) {
  685. if (function_exists($visit_pre)) {
  686. $output .= call_user_func($visit_pre, $node, $depth, $nid);
  687. }
  688. else {
  689. $output .= book_node_visitor_html_pre($node, $depth, $nid);
  690. }
  691. $children = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND b.parent = %d AND n.moderate = 0 ORDER BY b.weight, n.title'), $node->nid);
  692. while ($childpage = db_fetch_object($children)) {
  693. $childnode = node_load($childpage->nid);
  694. if ($childnode->nid != $node->nid) {
  695. $output .= book_recurse($childnode->nid, $depth + 1, $visit_pre, $visit_post);
  696. }
  697. }
  698. if (function_exists($visit_post)) {
  699. $output .= call_user_func($visit_post, $node, $depth);
  700. }
  701. else {
  702. # default
  703. $output .= book_node_visitor_html_post($node, $depth);
  704. }
  705. }
  706. }
  707. return $output;
  708. }
  709. /**
  710. * Generates printer-friendly HTML for a node. This function
  711. * is a 'pre-node' visitor function for book_recurse().
  712. *
  713. * @param $node
  714. * - the node to generate output for.
  715. * @param $depth
  716. * - the depth of the given node in the hierarchy. This
  717. * is used only for generating output.
  718. * @param $nid
  719. * - the node id (nid) of the given node. This
  720. * is used only for generating output.
  721. * @return
  722. * - the HTML generated for the given node.
  723. */
  724. function book_node_visitor_html_pre($node, $depth, $nid) {
  725. // Output the content:
  726. if (node_hook($node, 'content')) {
  727. $node = node_invoke($node, 'content');
  728. }
  729. // Allow modules to change $node->body before viewing.
  730. node_invoke_nodeapi($node, 'print', $node->body, false);
  731. $output .= "<div id=\"node-". $node->nid ."\" class=\"section-$depth\">\n";
  732. $output .= "<h1 class=\"book-heading\">". check_plain($node->title) ."</h1>\n";
  733. if ($node->body) {
  734. $output .= $node->body;
  735. }
  736. return $output;
  737. }
  738. /**
  739. * Finishes up generation of printer-friendly HTML after visiting a
  740. * node. This function is a 'post-node' visitor function for
  741. * book_recurse().
  742. */
  743. function book_node_visitor_html_post($node, $depth) {
  744. return "</div>\n";
  745. }
  746. function _book_admin_table($nodes = array()) {
  747. $form = array(
  748. '#theme' => 'book_admin_table',
  749. '#tree' => TRUE,
  750. );
  751. foreach ($nodes as $node) {
  752. $form = array_merge($form, _book_admin_table_tree($node, 0));
  753. }
  754. return $form;
  755. }
  756. function _book_admin_table_tree($node, $depth) {
  757. $form = array();
  758. $form[] = array(
  759. 'nid' => array('#type' => 'value', '#value' => $node->nid),
  760. 'depth' => array('#type' => 'value', '#value' => $depth),
  761. 'title' => array(
  762. '#type' => 'textfield',
  763. '#default_value' => $node->title,
  764. '#maxlength' => 255,
  765. ),
  766. 'weight' => array(
  767. '#type' => 'weight',
  768. '#default_value' => $node->weight,
  769. '#delta' => 15,
  770. ),
  771. );
  772. $children = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE b.parent = %d ORDER BY b.weight, n.title'), $node->nid);
  773. while ($child = db_fetch_object($children)) {
  774. $form = array_merge($form, _book_admin_table_tree(node_load($child->nid), $depth + 1));
  775. }
  776. return $form;
  777. }
  778. function theme_book_admin_table($form) {
  779. $header = array(t('Title'), t('Weight'), array('data' => t('Operations'), 'colspan' => '3'));
  780. $rows = array();
  781. foreach (element_children($form) as $key) {
  782. $nid = $form[$key]['nid']['#value'];
  783. $pid = $form[0]['nid']['#value'];
  784. if ($pid == $nid) {
  785. // Don't return to the parent book page if it is deleted.
  786. $pid = '';
  787. }
  788. $rows[] = array(
  789. '<div style="padding-left: '. (25 * $form[$key]['depth']['#value']) .'px;">'. form_render($form[$key]['title']) .'</div>',
  790. form_render($form[$key]['weight']),
  791. l(t('view'), 'node/'. $nid),
  792. l(t('edit'), 'node/'. $nid .'/edit'),
  793. l(t('delete'), 'node/'. $nid .'/delete', NULL, 'destination=admin/node/book/'. (arg(3) == 'orphan' ? 'orphan' : $pid)),
  794. );
  795. }
  796. return theme('table', $header, $rows);
  797. }
  798. /**
  799. * Display an administrative view of the hierarchy of a book.
  800. */
  801. function book_admin_edit($nid) {
  802. $node = node_load($nid);
  803. if ($node->nid) {
  804. drupal_set_title(check_plain($node->title));
  805. $form = array();
  806. $form['table'] = _book_admin_table(array($node));
  807. $form['save'] = array(
  808. '#type' => 'submit',
  809. '#value' => t('Save book pages'),
  810. );
  811. return drupal_get_form('book_admin_edit', $form);
  812. }
  813. else {
  814. drupal_not_found();
  815. }
  816. }
  817. /**
  818. * Menu callback; displays a listing of all orphaned book pages.
  819. */
  820. function book_admin_orphan() {
  821. $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, n.status, b.parent FROM {node} n INNER JOIN {book} b ON n.vid = b.vid'));
  822. $pages = array();
  823. while ($page = db_fetch_object($result)) {
  824. $pages[$page->nid] = $page;
  825. }
  826. $orphans = array();
  827. if (count($pages)) {
  828. foreach ($pages as $page) {
  829. if ($page->parent && empty($pages[$page->parent])) {
  830. $orphans[] = node_load($page->nid);
  831. }
  832. }
  833. }
  834. if (count($orphans)) {
  835. $form = array();
  836. $form['table'] = _book_admin_table($orphans);
  837. $form['save'] = array(
  838. '#type' => 'submit',
  839. '#value' => t('Save book pages'),
  840. );
  841. return drupal_get_form('book_admin_edit', $form);
  842. }
  843. else {
  844. return '<p>'. t('There are no orphan pages.') .'</p>';
  845. }
  846. }
  847. function book_admin_edit_submit($form_id, $form_values) {
  848. foreach ($form_values['table'] as $row) {
  849. $node = node_load($row['nid']);
  850. if ($row['title'] != $node->title || $row['weight'] != $node->weight) {
  851. $node->title = $row['title'];
  852. $node->weight = $row['weight'];
  853. node_save($node);
  854. watchdog('content', t('%type: updated %title.', array('%type' => theme('placeholder', t('book')), '%title' => theme('placeholder', $node->title))), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid));
  855. }
  856. }
  857. if (is_numeric(arg(3))) {
  858. // Updating pages in a single book.
  859. $book = node_load(arg(3));
  860. drupal_set_message(t('Updated book %title.', array('%title' => theme('placeholder', $book->title))));
  861. }
  862. else {
  863. // Updating the orphan pages.
  864. drupal_set_message(t('Updated orphan book pages.'));
  865. }
  866. }
  867. /**
  868. * Menu callback; displays the book administration page.
  869. */
  870. function book_admin($nid = 0) {
  871. if ($nid) {
  872. return book_admin_edit($nid);
  873. }
  874. else {
  875. return book_admin_overview();
  876. }
  877. }
  878. /**
  879. * Returns an administrative overview of all books.
  880. */
  881. function book_admin_overview() {
  882. $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE b.parent = 0 ORDER BY b.weight, n.title'));
  883. while ($book = db_fetch_object($result)) {
  884. $rows[] = array(l($book->title, "node/$book->nid"), l(t('outline'), "admin/node/book/$book->nid"));
  885. }
  886. $headers = array(t('Book'), t('Operations'));
  887. return theme('table', $headers, $rows);
  888. }
  889. /**
  890. * Implementation of hook_help().
  891. */
  892. function book_help($section) {
  893. switch ($section) {
  894. case 'admin/help#book':
  895. $output = '<p>'. t('The <em>book</em> content type is suited for creating structured, multi-page hypertexts such as site resource guides, manuals, and Frequently Asked Questions (FAQs). It permits a document to have chapters, sections, subsections, etc. Authors with suitable permissions can add pages to a collaborative book, placing them into the existing document by adding them to a table of contents menu. ') .'</p>';
  896. $output .= '<p>'. t('Books have additional <em>previous</em>, <em>up</em>, and <em>next</em> navigation elements at the bottom of each page for moving through the text. Additional navigation may be provided by enabling the <em>book navigation block</em> on the <a href="%admin-block">block administration page</a>.', array('%admin-block' => url('admin/block'))) .'</p>';
  897. $output .= '<p>'. t('Users can select the <em>printer-friendly version</em> link visible at the bottom of a book page to generate a printer-friendly display of the page and all of its subsections. ') .'</p>';
  898. $output .= '<p>'. t('Administrators can view a book outline, from which is it possible to change the titles of sections, and their <i>weight</i> (thus reordering sections). From this outline, it is also possible to edit and/or delete book pages. Many content types besides pages (for example, blog entries, stories, and polls) can be added to a collaborative book by choosing the <em>outline</em> tab when viewing the post.') .'</p>';
  899. $output .= t('<p>You can</p>
  900. <ul>
  901. <li>create new book pages: <a href="%node-add-book">create content &gt;&gt; book page</a>.</li>
  902. <li>administer individual books (choose a book from list): <a href="%admin-node-book">administer &gt;&gt; content &gt;&gt; books</a>.</li>
  903. <li>set workflow and other global book settings on the book configuration page: <a href="%admin-settings-content-types-book-page" title="book page content type">administer &gt;&gt; settings &gt;&gt; content types &gt;&gt; configure book page</a>.</li>
  904. <li>enable the book navigation block: <a href="%admin-block">administer &gt;&gt; blocks</a>.</li>
  905. <li>control who can create, edit, and outline posts in books by setting access permissions: <a href="%admin-access">administer &gt;&gt; access control</a>.</li>
  906. </ul>
  907. ', array('%node-add-book' => url('node/add/book'), '%admin-node-book' => url('admin/node/book'), '%admin-settings-content-types-book-page' => url('admin/settings/content-types/book'), '%admin-block' => url('admin/block'), '%admin-access' => url('admin/access')));
  908. $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%book">Book page</a>.', array('%book' => 'http://drupal.org/handbook/modules/book/')) .'</p>';
  909. return $output;
  910. case 'admin/modules#description':
  911. return t('Allows users to collaboratively author a book.');
  912. case 'admin/node/book':
  913. return t('<p>The book module offers a means to organize content, authored by many users, in an online manual, outline or FAQ.</p>');
  914. case 'admin/node/book/orphan':
  915. return t('<p>Pages in a book are like a tree. As pages are edited, reorganized and removed, child pages might be left with no link to the rest of the book. Such pages are referred to as "orphan pages". On this page, administrators can review their books for orphans and reattach those pages as desired.</p>');
  916. case 'node/add#book':
  917. return t("A book is a collaborative writing effort: users can collaborate writing the pages of the book, positioning the pages in the right order, and reviewing or modifying pages previously written. So when you have some information to share or when you read a page of the book and you didn't like it, or if you think a certain page could have been written better, you can do something about it.");
  918. }
  919. if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'outline') {
  920. return t('The outline feature allows you to include posts in the <a href="%book">book hierarchy</a>.', array('%book' => url('book')));
  921. }
  922. }
Login or register to post comments