hs_book.module

Tracking 5.x-3.x branch
  1. drupal
    1. 5 contributions/hierarchical_select/modules/hs_book.module

Implementation of the Hierarchical Select API for the Book module.

Functions & methods

NameDescription
hs_book_admin_settingsForm definition; admin settings.
hs_book_form_alterImplementation of hook_form_alter().
hs_book_hierarchical_select_childrenImplementation of hook_hierarchical_select_children().
hs_book_hierarchical_select_implementation_infoImplementation of hook_hierarchical_select_implementation_info().
hs_book_hierarchical_select_item_get_labelImplementation of hook_hierarchical_select_item_get_label().
hs_book_hierarchical_select_lineageImplementation of hook_hierarchical_select_lineage().
hs_book_hierarchical_select_paramsImplementation of hook_hierarchical_select_params().
hs_book_hierarchical_select_root_levelImplementation of hook_hierarchical_select_root_level().
hs_book_hierarchical_select_valid_itemImplementation of hook_hierarchical_select_valid_item().
hs_book_menuImplementation of hook_menu().

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Implementation of the Hierarchical Select API for the Book module.
  5. */
  6. //----------------------------------------------------------------------------
  7. // Drupal core hooks.
  8. /**
  9. * Implementation of hook_menu().
  10. */
  11. function hs_book_menu($may_cache) {
  12. $items = array();
  13. if ($may_cache) {
  14. $items[] = array(
  15. 'path' => 'admin/settings/hierarchical_select/book',
  16. 'title' => t('Book'),
  17. 'description' => t('Hierarchical Select configuration for Book'),
  18. 'callback' => 'drupal_get_form',
  19. 'callback arguments' => array('hs_book_admin_settings'),
  20. 'type' => MENU_LOCAL_TASK,
  21. );
  22. }
  23. return $items;
  24. }
  25. /**
  26. * Implementation of hook_form_alter().
  27. */
  28. function hs_book_form_alter($form_id, &$form) {
  29. if ($form_id == 'book_node_form' && isset($form['parent'])) {
  30. unset($form['parent']['#options']);
  31. $form['parent']['#type'] = 'hierarchical_select';
  32. $form['parent']['#config'] = array(
  33. 'module' => 'hs_book',
  34. 'params' => array(
  35. 'nid' => (isset($form['nid'])) ? $form['nid']['#value'] : NULL,
  36. ),
  37. 'save_lineage' => 0,
  38. 'enforce_deepest' => 0,
  39. 'resizable' => variable_get('hs_book_resizable', 1),
  40. );
  41. $form['parent']['#default_value'] = (!is_numeric($form['parent']['#default_value'])) ? 0 : $form['parent']['#default_value'];
  42. }
  43. }
  44. //----------------------------------------------------------------------------
  45. // Menu callbacks.
  46. /**
  47. * Form definition; admin settings.
  48. */
  49. function hs_book_admin_settings() {
  50. $form['hs_book_resizable'] = array(
  51. '#type' => 'radios',
  52. '#title' => t('Resizable'),
  53. '#description' => t(
  54. "When enabled, a handle appears below the Hierarchical Select to allow
  55. the user to dynamically resize it. Double clicking will toggle between
  56. the smallest and a sane 'big size'."
  57. ),
  58. '#options' => array(
  59. 0 => t('Disabled'),
  60. 1 => t('Enabled'),
  61. ),
  62. '#default_value' => variable_get('hs_book_resizable', 1),
  63. );
  64. return system_settings_form($form);
  65. }
  66. //----------------------------------------------------------------------------
  67. // Hierarchical Select hooks.
  68. /**
  69. * Implementation of hook_hierarchical_select_params().
  70. */
  71. function hs_book_hierarchical_select_params() {
  72. $params = array(
  73. 'nid', // The nid of the given book page.item. This book page and its children will be excluded from the hierarchy.
  74. );
  75. return $params;
  76. }
  77. /**
  78. * Implementation of hook_hierarchical_select_root_level().
  79. */
  80. function hs_book_hierarchical_select_root_level($params) {
  81. $root_level = array();
  82. // If the user has permission to create new books, add the top-level book page to the menu;
  83. if (user_access('create new books')) {
  84. $root_level[0] = '<'. t('top-level') .'>';
  85. }
  86. $root_level += hs_book_hierarchical_select_children(-1, $params);
  87. return $root_level;
  88. }
  89. /**
  90. * Implementation of hook_hierarchical_select_children().
  91. */
  92. function hs_book_hierarchical_select_children($parent, $params) {
  93. // TRICKY: because the book form assumes that the value "0" corresponds to
  94. // the <top-level page> option (for creating a new menu item at the root
  95. // level) *AND* 0 is also a pseudo-value in the actual hierarchy (it
  96. // represents the root), we have to pretend in the code that -1 is the root
  97. // in the hierarchy instead of 0.
  98. if ($parent == 0) {
  99. return array();
  100. }
  101. if ($parent == -1) {
  102. $parent = 0;
  103. }
  104. $result = db_query(db_rewrite_sql('SELECT n.nid, n.title FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND b.parent = %d ORDER BY b.weight, n.title'), $parent);
  105. $children = array();
  106. while ($node = db_fetch_object($result)) {
  107. // Don't include the given book page in the hierarchy!
  108. if (isset($params['nid']) && $params['nid'] == $node->nid) {
  109. continue;
  110. }
  111. $children[$node->nid] = $node->title;
  112. }
  113. return $children;
  114. }
  115. /**
  116. * Implementation of hook_hierarchical_select_lineage().
  117. */
  118. function hs_book_hierarchical_select_lineage($item, $params) {
  119. $lineage = array($item);
  120. while (TRUE) {
  121. $parent = db_result(db_query("SELECT parent FROM {book} WHERE nid = %d", $item));
  122. // 0 is the root menu item, so if $parent == 0, the lineage is complete!
  123. if ($parent == 0)
  124. break;
  125. array_unshift($lineage, $parent);
  126. $item = $parent;
  127. }
  128. return $lineage;
  129. }
  130. /**
  131. * Implementation of hook_hierarchical_select_valid_item().
  132. */
  133. function hs_book_hierarchical_select_valid_item($item, $params) {
  134. if (!is_numeric($item) || $item < 0) {
  135. return FALSE;
  136. }
  137. // Item 0 corresponds to the special <top-level page> option.
  138. if ($item == 0) {
  139. return TRUE;
  140. }
  141. $count = db_result(db_query(db_rewrite_sql("SELECT COUNT(n.nid) FROM {book} n WHERE n.nid = %d"), $item));
  142. return ($count == 1);
  143. }
  144. /**
  145. * Implementation of hook_hierarchical_select_item_get_label().
  146. */
  147. function hs_book_hierarchical_select_item_get_label($item, $params) {
  148. static $labels = array();
  149. if (!isset($labels[$item])) {
  150. $labels[$item] = t(db_result(db_query("SELECT n.title FROM {book} b INNER JOIN {node} n ON b.vid = n.vid WHERE b.nid = %d", $item)));
  151. }
  152. return $labels[$item];
  153. }
  154. /**
  155. * Implementation of hook_hierarchical_select_implementation_info().
  156. */
  157. function hs_book_hierarchical_select_implementation_info() {
  158. $info = node_get_types('type', 'book');
  159. return array(
  160. 'hierarchy type' => t('Book'),
  161. 'entity type' => t('Node (!book_page)', array('!book_page' => $info->name)),
  162. );
  163. }