]> BookStack Code Mirror - bookstack/commitdiff
Added better entity deletion and commented up repos
authorDan Brown <redacted>
Sat, 21 Nov 2015 18:05:03 +0000 (18:05 +0000)
committerDan Brown <redacted>
Sat, 21 Nov 2015 18:05:03 +0000 (18:05 +0000)
app/Http/Controllers/BookController.php
app/Http/Controllers/ChapterController.php
app/Http/Controllers/HomeController.php
app/Http/Controllers/PageController.php
app/Repos/BookRepo.php
app/Repos/ChapterRepo.php
app/Repos/PageRepo.php
resources/views/books/index.blade.php
resources/views/home.blade.php
resources/views/partials/entity-list.blade.php

index ff172f3b2f9b504b2bc94d8e521b599d1f6453c4..e7ade577e0e3325a35ac93523b59480179c4efe4 100644 (file)
@@ -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]);
     }
 
index a8076cdf2afa5a5bcfadc5107717968a6b2d14d1..0aa1b6ee2033a4d6291e20001cd0d409c0079cb9 100644 (file)
@@ -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());
     }
 }
index 3b04a1f41dbf7b67f2809b90bdc073c7e96d57c3..8fd1c10095f4ab4ebda488c990ee12a463aa4d58 100644 (file)
@@ -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]);
     }
 
 }
index 006d84f6ee01a8c08efa1bd3cdc96a91ee86ffb9..e061cea1bbd809ed505c1710b55d93f8b83bdb6e 100644 (file)
@@ -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());
     }
 
index 55dbb86dc711f22a079befdf5615299aba7fd204..b7189aaf0fbf7712c61838a04499aa4cb9dc4e03 100644 (file)
@@ -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();
     }
 
index 4aba1dc1283c5e37b8ade0fa27802bb2295c1059..db2a72d96b49ee525bd537672a6edebfbf93e95e 100644 (file)
@@ -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;
index 2f7bbe709909e337cf90c26eb609173c0c08506a..3f8f813947c262d456616d2d937438f3067ddb74 100644 (file)
@@ -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
index 3448351329b9ef0b0843376cbf93db03b97b7ac3..32742d12635f6b870df45b9e13325edd44c74224 100644 (file)
             </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>
index 0ffc8b3bfb6067e8a481ca64897686b595b58a9c..282c15677f932c2ea4a19fb206d3b780ea215d55 100644 (file)
@@ -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>
 
index a4b2bdfb60ce9ed572591c739f3006e03215dc07..058b0b24f58ca8587a3dbd705ffebc91a6a8ff18 100644 (file)
@@ -12,6 +12,6 @@
     @endforeach
 @else
     <p class="text-muted">
-        No items available :(
+        No items available
     </p>
 @endif
\ No newline at end of file