1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-30 04:23:11 +03:00

Got the tree view working

This commit is contained in:
Dan Brown
2015-07-20 22:05:26 +01:00
parent 52cf7e4483
commit da2ba4d9f3
13 changed files with 223 additions and 21 deletions

View File

@ -53,4 +53,11 @@ class BookRepo
$book->delete();
}
public function getTree($book)
{
$tree = $book->toArray();
$tree['pages'] = $this->pageRepo->getTreeByBookId($book->id);
return $tree;
}
}

View File

@ -59,4 +59,62 @@ class PageRepo
return $query->get();
}
public function getBreadCrumbs($page)
{
$tree = [];
$cPage = $page;
while($cPage->parent && $cPage->parent->id !== 0) {
$cPage = $cPage->parent;
$tree[] = $cPage;
}
return count($tree) > 0 ? array_reverse($tree) : false;
}
/**
* Creates a tree of child pages, Nested by their
* set parent pages.
* @param $bookId
* @param bool $currentPageId
* @return array
*/
public function getTreeByBookId($bookId, $currentPageId = false)
{
$topLevelPages = $this->getTopLevelPages($bookId);
$pageTree = [];
foreach($topLevelPages as $key => $topPage) {
$pageTree[$key] = $this->toArrayTree($topPage, $currentPageId);
}
return $pageTree;
}
/**
* Creates a page tree array with the supplied page
* as the parent of the tree.
* @param $page
* @param bool $currentPageId
* @return mixed
*/
private function toArrayTree($page, $currentPageId = false)
{
$cPage = $page->toSimpleArray();
$cPage['current'] = ($currentPageId !== false && $cPage['id'] === $currentPageId);
$cPage['pages'] = [];
foreach($page->children as $key => $childPage) {
$cPage['pages'][$key] = $this->toArrayTree($childPage);
}
$cPage['hasChildren'] = count($cPage['pages']) > 0;
return $cPage;
}
/**
* Gets the pages at the top of the page hierarchy.
* @param $bookId
*/
private function getTopLevelPages($bookId)
{
return $this->page->where('book_id', '=', $bookId)->where('page_id', '=', 0)->get();
}
}