public function index()
{
$books = $this->bookRepo->getAllPaginated(10);
- $recents = $this->bookRepo->getRecentlyViewed(10, 0);
+ $recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(10, 0) : false;
return view('books/index', ['books' => $books, 'recents' => $recents]);
}
$this->checkPermission('chapter-delete');
$book = $this->bookRepo->getBySlug($bookSlug);
$chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id);
- if (count($chapter->pages) > 0) {
- foreach ($chapter->pages as $page) {
- $page->chapter_id = 0;
- $page->save();
- }
- }
- Activity::removeEntity($chapter);
Activity::addMessage('chapter_delete', $book->id, $chapter->name);
- $chapter->delete();
+ $this->chapterRepo->destroy($chapter);
return redirect($book->getUrl());
}
}
public function index()
{
$activity = Activity::latest();
- $recentlyViewed = Views::getUserRecentlyViewed(10, 0);
- return view('home', ['activity' => $activity, 'recents' => $recentlyViewed]);
+ $recents = $this->signedIn ? Views::getUserRecentlyViewed(10, 0) : $this->bookRepo->getLatest(10);
+ return view('home', ['activity' => $activity, 'recents' => $recents]);
}
}
$book = $this->bookRepo->getBySlug($bookSlug);
$page = $this->pageRepo->getBySlug($pageSlug, $book->id);
Activity::addMessage('page_delete', $book->id, $page->name);
- Activity::removeEntity($page);
- $page->delete();
+ $this->pageRepo->destroy($page);
return redirect($book->getUrl());
}
<?php namespace BookStack\Repos;
-use BookStack\Activity;
+use Activity;
use Illuminate\Support\Str;
use BookStack\Book;
use Views;
protected $book;
protected $pageRepo;
+ protected $chapterRepo;
/**
* BookRepo constructor.
- * @param Book $book
- * @param PageRepo $pageRepo
+ * @param Book $book
+ * @param PageRepo $pageRepo
+ * @param ChapterRepo $chapterRepo
*/
- public function __construct(Book $book, PageRepo $pageRepo)
+ public function __construct(Book $book, PageRepo $pageRepo, ChapterRepo $chapterRepo)
{
$this->book = $book;
$this->pageRepo = $pageRepo;
+ $this->chapterRepo = $chapterRepo;
}
/**
return $this->book->orderBy('name', 'asc')->paginate($count);
}
+
+ /**
+ * Get the latest books.
+ * @param int $count
+ * @return mixed
+ */
+ public function getLatest($count = 10)
+ {
+ return $this->book->orderBy('created_at', 'desc')->take($count)->get();
+ }
+
+ /**
+ * Gets the most recently viewed for a user.
+ * @param int $count
+ * @param int $page
+ * @return mixed
+ */
public function getRecentlyViewed($count = 10, $page = 0)
{
return Views::getUserRecentlyViewed($count, $page, $this->book);
{
$book = $this->getBySlug($bookSlug);
foreach ($book->pages as $page) {
- \Activity::removeEntity($page);
- $page->delete();
+ $this->pageRepo->destroy($page);
}
foreach ($book->chapters as $chapter) {
- \Activity::removeEntity($chapter);
- $chapter->delete();
+ $this->chapterRepo->destroy($chapter);
}
+ $book->views()->delete();
$book->delete();
}
<?php namespace BookStack\Repos;
+use Activity;
use Illuminate\Support\Str;
use BookStack\Chapter;
$this->chapter = $chapter;
}
+ /**
+ * Check if an id exists.
+ * @param $id
+ * @return bool
+ */
public function idExists($id)
{
return $this->chapter->where('id', '=', $id)->count() > 0;
}
+ /**
+ * Get a chapter by a specific id.
+ * @param $id
+ * @return mixed
+ */
public function getById($id)
{
return $this->chapter->findOrFail($id);
}
+ /**
+ * Get all chapters.
+ * @return \Illuminate\Database\Eloquent\Collection|static[]
+ */
public function getAll()
{
return $this->chapter->all();
}
+ /**
+ * Get a chapter that has the given slug within the given book.
+ * @param $slug
+ * @param $bookId
+ * @return mixed
+ */
public function getBySlug($slug, $bookId)
{
return $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
}
+ /**
+ * Create a new chapter from request input.
+ * @param $input
+ * @return $this
+ */
public function newFromInput($input)
{
return $this->chapter->fill($input);
}
- public function destroyById($id)
+ /**
+ * Destroy a chapter and its relations by providing its slug.
+ * @param Chapter $chapter
+ */
+ public function destroy(Chapter $chapter)
{
- $page = $this->getById($id);
- $page->delete();
+ if (count($chapter->pages) > 0) {
+ foreach ($chapter->pages as $page) {
+ $page->chapter_id = 0;
+ $page->save();
+ }
+ }
+ Activity::removeEntity($chapter);
+ $chapter->views()->delete();
+ $chapter->delete();
}
+ /**
+ * Check if a chapter's slug exists.
+ * @param $slug
+ * @param $bookId
+ * @param bool|false $currentId
+ * @return bool
+ */
public function doesSlugExist($slug, $bookId, $currentId = false)
{
$query = $this->chapter->where('slug', '=', $slug)->where('book_id', '=', $bookId);
return $query->count() > 0;
}
+ /**
+ * Finds a suitable slug for the provided name.
+ * Checks database to prevent duplicate slugs.
+ * @param $name
+ * @param $bookId
+ * @param bool|false $currentId
+ * @return string
+ */
public function findSuitableSlug($name, $bookId, $currentId = false)
{
$slug = Str::slug($name);
return $slug;
}
+ /**
+ * Get chapters by the given search term.
+ * @param $term
+ * @param array $whereTerms
+ * @return mixed
+ */
public function getBySearch($term, $whereTerms = [])
{
$terms = explode(' ', preg_quote(trim($term)));
return $chapters;
}
+ /**
+ * Sets a chapters book id.
+ * @param $bookId
+ * @param Chapter $chapter
+ * @return Chapter
+ */
public function setBookId($bookId, Chapter $chapter)
{
$chapter->book_id = $bookId;
<?php namespace BookStack\Repos;
+use Activity;
use BookStack\Book;
use BookStack\Chapter;
use Illuminate\Http\Request;
$this->pageRevision = $pageRevision;
}
+ /**
+ * Check if a page id exists.
+ * @param $id
+ * @return bool
+ */
public function idExists($id)
{
return $this->page->where('page_id', '=', $id)->count() > 0;
}
+ /**
+ * Get a page via a specific ID.
+ * @param $id
+ * @return mixed
+ */
public function getById($id)
{
return $this->page->findOrFail($id);
}
+ /**
+ * Get all pages.
+ * @return \Illuminate\Database\Eloquent\Collection|static[]
+ */
public function getAll()
{
return $this->page->all();
}
+ /**
+ * Get a page identified by the given slug.
+ * @param $slug
+ * @param $bookId
+ * @return mixed
+ */
public function getBySlug($slug, $bookId)
{
return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->first();
return $page;
}
+ /**
+ * Count the pages with a particular slug within a book.
+ * @param $slug
+ * @param $bookId
+ * @return mixed
+ */
public function countBySlug($slug, $bookId)
{
return $this->page->where('slug', '=', $slug)->where('book_id', '=', $bookId)->count();
return $html;
}
- public function destroyById($id)
- {
- $page = $this->getById($id);
- $page->delete();
- }
+ /**
+ * Gets pages by a search term.
+ * Highlights page content for showing in results.
+ * @param string $term
+ * @param array $whereTerms
+ * @return mixed
+ */
public function getBySearch($term, $whereTerms = [])
{
$terms = explode(' ', preg_quote(trim($term)));
/**
* Gets a suitable slug for the resource
- *
* @param $name
* @param $bookId
* @param bool|false $currentId
return $slug;
}
+ /**
+ * Destroy a given page along with its dependencies.
+ * @param $page
+ */
+ public function destroy($page)
+ {
+ Activity::removeEntity($page);
+ $page->views()->delete();
+ $page->revisions()->delete();
+ $page->delete();
+ }
+
}
\ No newline at end of file
</div>
<div class="col-sm-4 col-sm-offset-1">
<div class="margin-top large"> </div>
- <h3>Recently Viewed</h3>
- @include('partials/entity-list', ['entities' => $recents])
+ @if($recents)
+ <h3>Recently Viewed</h3>
+ @include('partials/entity-list', ['entities' => $recents])
+ @endif
</div>
</div>
</div>
<div class="row">
<div class="col-md-7">
- <h2>My Recently Viewed</h2>
+ @if($signedIn)
+ <h2>My Recently Viewed</h2>
+ @else
+ <h2>Recent Books</h2>
+ @endif
@include('partials/entity-list', ['entities' => $recents])
</div>
@endforeach
@else
<p class="text-muted">
- No items available :(
+ No items available
</p>
@endif
\ No newline at end of file