From c32d70abc4f3cca4a8b3533d45b09d2a830d6a57 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sat, 5 Dec 2015 14:41:51 +0000 Subject: [PATCH] Added custom meta titles to many pages. Closes #30. --- app/Entity.php | 15 +++++++++++++-- app/Http/Controllers/BookController.php | 14 +++++++++++++- app/Http/Controllers/ChapterController.php | 4 ++++ app/Http/Controllers/Controller.php | 9 +++++++++ app/Http/Controllers/PageController.php | 6 ++++++ app/Http/Controllers/SearchController.php | 1 + app/Http/Controllers/SettingController.php | 1 + app/Http/Controllers/UserController.php | 3 +++ app/Page.php | 2 -- resources/views/base.blade.php | 2 +- resources/views/books/sort-box.blade.php | 2 +- resources/views/pages/show.blade.php | 4 ++-- resources/views/pages/sidebar-tree-list.blade.php | 4 ++-- 13 files changed, 56 insertions(+), 11 deletions(-) diff --git a/app/Entity.php b/app/Entity.php index 977b02e77..db244e014 100644 --- a/app/Entity.php +++ b/app/Entity.php @@ -97,18 +97,29 @@ abstract class Entity extends Model */ public static function isA($type) { - return static::getName() === strtolower($type); + return static::getClassName() === strtolower($type); } /** * Gets the class name. * @return string */ - public static function getName() + public static function getClassName() { return strtolower(array_slice(explode('\\', static::class), -1, 1)[0]); } + /** + *Gets a limited-length version of the entities name. + * @param int $length + * @return string + */ + public function getShortName($length = 25) + { + if(strlen($this->name) <= $length) return $this->name; + return substr($this->name, 0, $length-3) . '...'; + } + /** * Perform a full-text search on this entity. * @param string[] $fieldsToSearch diff --git a/app/Http/Controllers/BookController.php b/app/Http/Controllers/BookController.php index 3cb8237ea..a4365d605 100644 --- a/app/Http/Controllers/BookController.php +++ b/app/Http/Controllers/BookController.php @@ -44,6 +44,7 @@ class BookController extends Controller $books = $this->bookRepo->getAllPaginated(10); $recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed(4, 0) : false; $popular = $this->bookRepo->getPopular(4, 0); + $this->setPageTitle('Books'); return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]); } @@ -55,6 +56,7 @@ class BookController extends Controller public function create() { $this->checkPermission('book-create'); + $this->setPageTitle('Create New Book'); return view('books/create'); } @@ -89,8 +91,9 @@ class BookController extends Controller public function show($slug) { $book = $this->bookRepo->getBySlug($slug); - Views::add($book); $bookChildren = $this->bookRepo->getChildren($book); + Views::add($book); + $this->setPageTitle($book->getShortName()); return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]); } @@ -104,6 +107,7 @@ class BookController extends Controller { $this->checkPermission('book-update'); $book = $this->bookRepo->getBySlug($slug); + $this->setPageTitle('Edit Book ' . $book->getShortName()); return view('books/edit', ['book' => $book, 'current' => $book]); } @@ -139,6 +143,7 @@ class BookController extends Controller { $this->checkPermission('book-delete'); $book = $this->bookRepo->getBySlug($bookSlug); + $this->setPageTitle('Delete Book ' . $book->getShortName()); return view('books/delete', ['book' => $book, 'current' => $book]); } @@ -153,9 +158,16 @@ class BookController extends Controller $book = $this->bookRepo->getBySlug($bookSlug); $bookChildren = $this->bookRepo->getChildren($book); $books = $this->bookRepo->getAll(); + $this->setPageTitle('Sort Book ' . $book->getShortName()); return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]); } + /** + * Shows the sort box for a single book. + * Used via AJAX when loading in extra books to a sort. + * @param $bookSlug + * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View + */ public function getSortItem($bookSlug) { $book = $this->bookRepo->getBySlug($bookSlug); diff --git a/app/Http/Controllers/ChapterController.php b/app/Http/Controllers/ChapterController.php index 1fe1e8b3e..fc13e8b58 100644 --- a/app/Http/Controllers/ChapterController.php +++ b/app/Http/Controllers/ChapterController.php @@ -40,6 +40,7 @@ class ChapterController extends Controller { $this->checkPermission('chapter-create'); $book = $this->bookRepo->getBySlug($bookSlug); + $this->setPageTitle('Create New Chapter'); return view('chapters/create', ['book' => $book, 'current' => $book]); } @@ -79,6 +80,7 @@ class ChapterController extends Controller $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id); $sidebarTree = $this->bookRepo->getChildren($book); Views::add($chapter); + $this->setPageTitle($chapter->getShortName()); return view('chapters/show', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter, 'sidebarTree' => $sidebarTree]); } @@ -93,6 +95,7 @@ class ChapterController extends Controller $this->checkPermission('chapter-update'); $book = $this->bookRepo->getBySlug($bookSlug); $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id); + $this->setPageTitle('Edit Chapter' . $chapter->getShortName()); return view('chapters/edit', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]); } @@ -127,6 +130,7 @@ class ChapterController extends Controller $this->checkPermission('chapter-delete'); $book = $this->bookRepo->getBySlug($bookSlug); $chapter = $this->chapterRepo->getBySlug($chapterSlug, $book->id); + $this->setPageTitle('Delete Chapter' . $chapter->getShortName()); return view('chapters/delete', ['book' => $book, 'chapter' => $chapter, 'current' => $chapter]); } diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 4cc865d29..5dc79eb02 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -42,6 +42,15 @@ abstract class Controller extends BaseController $this->signedIn = auth()->check(); } + /** + * Adds the page title into the view. + * @param $title + */ + public function setPageTitle($title) + { + view()->share('pageTitle', $title); + } + /** * Checks for a permission. * diff --git a/app/Http/Controllers/PageController.php b/app/Http/Controllers/PageController.php index d9d53178e..2002fbdf0 100644 --- a/app/Http/Controllers/PageController.php +++ b/app/Http/Controllers/PageController.php @@ -46,6 +46,7 @@ class PageController extends Controller $this->checkPermission('page-create'); $book = $this->bookRepo->getBySlug($bookSlug); $chapter = $chapterSlug ? $this->chapterRepo->getBySlug($chapterSlug, $book->id) : false; + $this->setPageTitle('Create New Page'); return view('pages/create', ['book' => $book, 'chapter' => $chapter]); } @@ -89,6 +90,7 @@ class PageController extends Controller $page = $this->pageRepo->getBySlug($pageSlug, $book->id); $sidebarTree = $this->bookRepo->getChildren($book); Views::add($page); + $this->setPageTitle($page->getShortName()); return view('pages/show', ['page' => $page, 'book' => $book, 'current' => $page, 'sidebarTree' => $sidebarTree]); } @@ -104,6 +106,7 @@ class PageController extends Controller $this->checkPermission('page-update'); $book = $this->bookRepo->getBySlug($bookSlug); $page = $this->pageRepo->getBySlug($pageSlug, $book->id); + $this->setPageTitle('Editing Page ' . $page->getShortName()); return view('pages/edit', ['page' => $page, 'book' => $book, 'current' => $page]); } @@ -148,6 +151,7 @@ class PageController extends Controller $this->checkPermission('page-delete'); $book = $this->bookRepo->getBySlug($bookSlug); $page = $this->pageRepo->getBySlug($pageSlug, $book->id); + $this->setPageTitle('Delete Page ' . $page->getShortName()); return view('pages/delete', ['book' => $book, 'page' => $page, 'current' => $page]); } @@ -179,6 +183,7 @@ class PageController extends Controller { $book = $this->bookRepo->getBySlug($bookSlug); $page = $this->pageRepo->getBySlug($pageSlug, $book->id); + $this->setPageTitle('Revisions For ' . $page->getShortName()); return view('pages/revisions', ['page' => $page, 'book' => $book, 'current' => $page]); } @@ -195,6 +200,7 @@ class PageController extends Controller $page = $this->pageRepo->getBySlug($pageSlug, $book->id); $revision = $this->pageRepo->getRevisionById($revisionId); $page->fill($revision->toArray()); + $this->setPageTitle('Page Revision For ' . $page->getShortName()); return view('pages/revision', ['page' => $page, 'book' => $book]); } diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php index c6222156d..c9ca1f09f 100644 --- a/app/Http/Controllers/SearchController.php +++ b/app/Http/Controllers/SearchController.php @@ -45,6 +45,7 @@ class SearchController extends Controller $pages = $this->pageRepo->getBySearch($searchTerm); $books = $this->bookRepo->getBySearch($searchTerm); $chapters = $this->chapterRepo->getBySearch($searchTerm); + $this->setPageTitle('Search For ' . $searchTerm); return view('search/all', ['pages' => $pages, 'books' => $books, 'chapters' => $chapters, 'searchTerm' => $searchTerm]); } diff --git a/app/Http/Controllers/SettingController.php b/app/Http/Controllers/SettingController.php index 3e96de62e..bca48807f 100644 --- a/app/Http/Controllers/SettingController.php +++ b/app/Http/Controllers/SettingController.php @@ -18,6 +18,7 @@ class SettingController extends Controller public function index() { $this->checkPermission('settings-update'); + $this->setPageTitle('Settings'); return view('settings/index'); } diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index dacb91a7b..ed9deab2d 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -35,6 +35,7 @@ class UserController extends Controller public function index() { $users = $this->user->all(); + $this->setPageTitle('Users'); return view('users/index', ['users' => $users]); } @@ -90,6 +91,7 @@ class UserController extends Controller $user = $this->user->findOrFail($id); $activeSocialDrivers = $socialAuthService->getActiveDrivers(); + $this->setPageTitle('User Profile'); return view('users/edit', ['user' => $user, 'activeSocialDrivers' => $activeSocialDrivers]); } @@ -139,6 +141,7 @@ class UserController extends Controller return $this->currentUser->id == $id; }); $user = $this->user->findOrFail($id); + $this->setPageTitle('Delete User ' . $user->name); return view('users/delete', ['user' => $user]); } diff --git a/app/Page.php b/app/Page.php index feedb1eae..bd5f3bafe 100644 --- a/app/Page.php +++ b/app/Page.php @@ -32,7 +32,6 @@ class Page extends Entity return $this->chapter()->count() > 0; } - public function revisions() { return $this->hasMany('BookStack\PageRevision')->orderBy('created_at', 'desc'); @@ -40,7 +39,6 @@ class Page extends Entity public function getUrl() { - // TODO - Extract this and share with chapters $bookSlug = $this->getAttribute('bookSlug') ? $this->getAttribute('bookSlug') : $this->book->slug; return '/books/' . $bookSlug . '/page/' . $this->slug; } diff --git a/resources/views/base.blade.php b/resources/views/base.blade.php index be67109c5..9c3ddbf91 100644 --- a/resources/views/base.blade.php +++ b/resources/views/base.blade.php @@ -1,7 +1,7 @@ - BookStack + {{ isset($pageTitle) ? $pageTitle . ' | ' : '' }}{{ Setting::get('app-name', 'BookStack') }} diff --git a/resources/views/books/sort-box.blade.php b/resources/views/books/sort-box.blade.php index 768a9f608..2e38f0b0b 100644 --- a/resources/views/books/sort-box.blade.php +++ b/resources/views/books/sort-box.blade.php @@ -2,7 +2,7 @@

{{ $book->name }}