BookStack/app/Repos/PageRepo.php

99 lines
2.2 KiB
PHP
Raw Normal View History

2015-07-12 16:31:15 -04:00
<?php namespace Oxbow\Repos;
use Illuminate\Support\Str;
use Oxbow\Page;
class PageRepo
{
protected $page;
/**
* PageRepo constructor.
* @param $page
*/
public function __construct(Page $page)
{
$this->page = $page;
}
public function getById($id)
{
return $this->page->findOrFail($id);
}
public function getAll()
{
return $this->page->all();
}
2015-07-15 17:55:49 -04:00
public function getBySlug($slug, $bookId)
2015-07-12 16:31:15 -04:00
{
2015-07-15 17:55:49 -04:00
return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
2015-07-12 16:31:15 -04:00
}
public function newFromInput($input)
{
$page = $this->page->fill($input);
return $page;
}
public function countBySlug($slug, $bookId)
{
return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count();
}
public function destroyById($id)
{
$page = $this->getById($id);
$page->delete();
}
2015-07-16 14:53:24 -04:00
public function getBySearch($term)
{
$terms = explode(' ', trim($term));
$query = $this->page;
foreach($terms as $term) {
$query = $query->where('text', 'like', '%'.$term.'%');
}
return $query->get();
}
2015-07-20 17:05:26 -04:00
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;
}
/**
* Gets the pages at the top of the page hierarchy.
* @param $bookId
*/
private function getTopLevelPages($bookId)
{
2015-07-27 15:17:08 -04:00
return $this->page->where('book_id', '=', $bookId)->where('chapter_id', '=', 0)->orderBy('priority')->get();
2015-07-21 15:13:29 -04:00
}
/**
* Applies a sort map to all applicable pages.
* @param $sortMap
* @param $bookId
*/
public function applySortMap($sortMap, $bookId)
{
foreach($sortMap as $index => $map) {
$page = $this->getById($map->id);
if($page->book_id === $bookId) {
$page->page_id = $map->parent;
$page->priority = $index;
$page->save();
}
}
2015-07-20 17:05:26 -04:00
}
2015-07-12 16:31:15 -04:00
}