annotate core/modules/book/src/BookOutline.php @ 0:4c8ae668cc8c

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