BookOutline.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Drupal\book;
  3. /**
  4. * Provides handling to render the book outline.
  5. */
  6. class BookOutline {
  7. /**
  8. * The book manager.
  9. *
  10. * @var \Drupal\book\BookManagerInterface
  11. */
  12. protected $bookManager;
  13. /**
  14. * Constructs a new BookOutline.
  15. *
  16. * @param \Drupal\book\BookManagerInterface $book_manager
  17. * The book manager.
  18. */
  19. public function __construct(BookManagerInterface $book_manager) {
  20. $this->bookManager = $book_manager;
  21. }
  22. /**
  23. * Fetches the book link for the previous page of the book.
  24. *
  25. * @param array $book_link
  26. * A fully loaded book link that is part of the book hierarchy.
  27. *
  28. * @return array
  29. * A fully loaded book link for the page before the one represented in
  30. * $book_link.
  31. */
  32. public function prevLink(array $book_link) {
  33. // If the parent is zero, we are at the start of a book.
  34. if ($book_link['pid'] == 0) {
  35. return NULL;
  36. }
  37. // Assigning the array to $flat resets the array pointer for use with each().
  38. $flat = $this->bookManager->bookTreeGetFlat($book_link);
  39. $curr = NULL;
  40. do {
  41. $prev = $curr;
  42. list($key, $curr) = each($flat);
  43. } while ($key && $key != $book_link['nid']);
  44. if ($key == $book_link['nid']) {
  45. // The previous page in the book may be a child of the previous visible link.
  46. if ($prev['depth'] == $book_link['depth']) {
  47. // The subtree will have only one link at the top level - get its data.
  48. $tree = $this->bookManager->bookSubtreeData($prev);
  49. $data = array_shift($tree);
  50. // The link of interest is the last child - iterate to find the deepest one.
  51. while ($data['below']) {
  52. $data = end($data['below']);
  53. }
  54. $this->bookManager->bookLinkTranslate($data['link']);
  55. return $data['link'];
  56. }
  57. else {
  58. $this->bookManager->bookLinkTranslate($prev);
  59. return $prev;
  60. }
  61. }
  62. }
  63. /**
  64. * Fetches the book link for the next page of the book.
  65. *
  66. * @param array $book_link
  67. * A fully loaded book link that is part of the book hierarchy.
  68. *
  69. * @return array
  70. * A fully loaded book link for the page after the one represented in
  71. * $book_link.
  72. */
  73. public function nextLink(array $book_link) {
  74. // Assigning the array to $flat resets the array pointer for use with each().
  75. $flat = $this->bookManager->bookTreeGetFlat($book_link);
  76. do {
  77. list($key,) = each($flat);
  78. } while ($key && $key != $book_link['nid']);
  79. if ($key == $book_link['nid']) {
  80. $next = current($flat);
  81. if ($next) {
  82. $this->bookManager->bookLinkTranslate($next);
  83. }
  84. return $next;
  85. }
  86. }
  87. /**
  88. * Formats the book links for the child pages of the current page.
  89. *
  90. * @param array $book_link
  91. * A fully loaded book link that is part of the book hierarchy.
  92. *
  93. * @return array
  94. * HTML for the links to the child pages of the current page.
  95. */
  96. public function childrenLinks(array $book_link) {
  97. $flat = $this->bookManager->bookTreeGetFlat($book_link);
  98. $children = [];
  99. if ($book_link['has_children']) {
  100. // Walk through the array until we find the current page.
  101. do {
  102. $link = array_shift($flat);
  103. } while ($link && ($link['nid'] != $book_link['nid']));
  104. // Continue though the array and collect the links whose parent is this page.
  105. while (($link = array_shift($flat)) && $link['pid'] == $book_link['nid']) {
  106. $data['link'] = $link;
  107. $data['below'] = '';
  108. $children[] = $data;
  109. }
  110. }
  111. if ($children) {
  112. return $this->bookManager->bookTreeOutput($children);
  113. }
  114. return '';
  115. }
  116. }