Skip to content

NOW MANAGED ON CODEBERG

Added better entity deletion and commented up repos

Dan Brown61ae96c5f84a
diff --git a/app/Http/Controllers/BookController.php b/app/Http/Controllers/BookController.php
index ff172f3b2..e7ade577e 100644
--- a/app/Http/Controllers/BookController.php
+++ b/app/Http/Controllers/BookController.php
@@ -42,7 +42,7 @@ class BookController extends Controller
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]);
}
diff --git a/app/Http/Controllers/ChapterController.php b/app/Http/Controllers/ChapterController.php
index a8076cdf2..0aa1b6ee2 100644
--- a/app/Http/Controllers/ChapterController.php
+++ b/app/Http/Controllers/ChapterController.php
@@ -146,15 +146,8 @@ class ChapterController extends Controller
$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());
}
}
diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php
index 3b04a1f41..8fd1c1009 100644
--- a/app/Http/Controllers/HomeController.php
+++ b/app/Http/Controllers/HomeController.php
@@ -34,8 +34,8 @@ class HomeController extends Controller
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]);
}
}
diff --git a/app/Http/Controllers/PageController.php b/app/Http/Controllers/PageController.php
index 006d84f6e..e061cea1b 100644
--- a/app/Http/Controllers/PageController.php
+++ b/app/Http/Controllers/PageController.php
@@ -164,8 +164,7 @@ class PageController extends Controller
$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());
}
diff --git a/app/Repos/BookRepo.php b/app/Repos/BookRepo.php
index 55dbb86dc..b7189aaf0 100644
--- a/app/Repos/BookRepo.php
+++ b/app/Repos/BookRepo.php
@@ -1,6 +1,6 @@
<?php namespace BookStack\Repos;
-use BookStack\Activity;
+use Activity;
use Illuminate\Support\Str;
use BookStack\Book;
use Views;
@@ -10,16 +10,19 @@ class BookRepo
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;
}
/**
@@ -52,6 +55,23 @@ class BookRepo
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);
@@ -105,13 +125,12 @@ class BookRepo
{
$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();
}
diff --git a/app/Repos/ChapterRepo.php b/app/Repos/ChapterRepo.php
index 4aba1dc12..db2a72d96 100644
--- a/app/Repos/ChapterRepo.php
+++ b/app/Repos/ChapterRepo.php
@@ -1,6 +1,7 @@
<?php namespace BookStack\Repos;
+use Activity;
use Illuminate\Support\Str;
use BookStack\Chapter;
@@ -18,37 +19,80 @@ class ChapterRepo
$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);
@@ -58,6 +102,14 @@ class ChapterRepo
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);
@@ -67,6 +119,12 @@ class ChapterRepo
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)));
@@ -80,6 +138,12 @@ class ChapterRepo
return $chapters;
}
+ /**
+ * Sets a chapters book id.
+ * @param $bookId
+ * @param Chapter $chapter
+ * @return Chapter
+ */
public function setBookId($bookId, Chapter $chapter)
{
$chapter->book_id = $bookId;
diff --git a/app/Repos/PageRepo.php b/app/Repos/PageRepo.php
index 2f7bbe709..3f8f81394 100644
--- a/app/Repos/PageRepo.php
+++ b/app/Repos/PageRepo.php
@@ -1,6 +1,7 @@
<?php namespace BookStack\Repos;
+use Activity;
use BookStack\Book;
use BookStack\Chapter;
use Illuminate\Http\Request;
@@ -26,21 +27,41 @@ class PageRepo
$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();
@@ -56,6 +77,12 @@ class PageRepo
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();
@@ -137,12 +164,14 @@ class PageRepo
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)));
@@ -299,7 +328,6 @@ class PageRepo
/**
* Gets a suitable slug for the resource
- *
* @param $name
* @param $bookId
* @param bool|false $currentId
@@ -314,5 +342,17 @@ class PageRepo
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
diff --git a/resources/views/books/index.blade.php b/resources/views/books/index.blade.php
index 344835132..32742d126 100644
--- a/resources/views/books/index.blade.php
+++ b/resources/views/books/index.blade.php
@@ -35,8 +35,10 @@
</div>
<div class="col-sm-4 col-sm-offset-1">
<div class="margin-top large">&nbsp;</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>
diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php
index 0ffc8b3bf..282c15677 100644
--- a/resources/views/home.blade.php
+++ b/resources/views/home.blade.php
@@ -6,7 +6,11 @@
<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>
diff --git a/resources/views/partials/entity-list.blade.php b/resources/views/partials/entity-list.blade.php
index a4b2bdfb6..058b0b24f 100644
--- a/resources/views/partials/entity-list.blade.php
+++ b/resources/views/partials/entity-list.blade.php
@@ -12,6 +12,6 @@
@endforeach
@else
<p class="text-muted">
- No items available :(
+ No items available
</p>
@endif
\ No newline at end of file