<?php namespace BookStack\Http\Controllers;
use Activity;
+use BookStack\Book;
use BookStack\Repos\EntityRepo;
use BookStack\Repos\UserRepo;
+use BookStack\Services\ExportService;
use Illuminate\Http\Request;
-use BookStack\Http\Requests;
-use BookStack\Repos\BookRepo;
-use BookStack\Repos\ChapterRepo;
-use BookStack\Repos\PageRepo;
use Illuminate\Http\Response;
use Views;
{
protected $entityRepo;
- protected $bookRepo;
- protected $pageRepo;
- protected $chapterRepo;
protected $userRepo;
+ protected $exportService;
/**
* BookController constructor.
- * @param BookRepo $bookRepo
- * @param PageRepo $pageRepo
- * @param ChapterRepo $chapterRepo
+ * @param EntityRepo $entityRepo
* @param UserRepo $userRepo
+ * @param ExportService $exportService
*/
- public function __construct(EntityRepo $entityRepo, BookRepo $bookRepo, PageRepo $pageRepo, ChapterRepo $chapterRepo, UserRepo $userRepo)
+ public function __construct(EntityRepo $entityRepo, UserRepo $userRepo, ExportService $exportService)
{
$this->entityRepo = $entityRepo;
- // TODO - Remove below
- $this->bookRepo = $bookRepo;
- $this->pageRepo = $pageRepo;
- $this->chapterRepo = $chapterRepo;
$this->userRepo = $userRepo;
+ $this->exportService = $exportService;
parent::__construct();
}
*/
public function index()
{
- $books = $this->entityRepo->getAllPaginated('book', 10);
+ $books = $this->entityRepo->getAllPaginated('book', 20);
$recents = $this->signedIn ? $this->entityRepo->getRecentlyViewed('book', 4, 0) : false;
$popular = $this->entityRepo->getPopular('book', 4, 0);
- $this->setPageTitle('Books');
- return view('books/index', ['books' => $books, 'recents' => $recents, 'popular' => $popular]);
+ $new = $this->entityRepo->getRecentlyCreated('book', 4, 0);
+ $booksViewType = setting()->getUser($this->currentUser, 'books_view_type', 'list');
+ $this->setPageTitle(trans('entities.books'));
+ return view('books/index', [
+ 'books' => $books,
+ 'recents' => $recents,
+ 'popular' => $popular,
+ 'new' => $new,
+ 'booksViewType' => $booksViewType
+ ]);
}
/**
'name' => 'required|string|max:255',
'description' => 'string|max:1000'
]);
- $book = $this->bookRepo->createFromInput($request->all());
+ $book = $this->entityRepo->createFromInput('book', $request->all());
Activity::add($book, 'book_create', $book->id);
return redirect($book->getUrl());
}
{
$book = $this->entityRepo->getBySlug('book', $slug);
$this->checkOwnablePermission('book-view', $book);
- $bookChildren = $this->bookRepo->getChildren($book);
+ $bookChildren = $this->entityRepo->getBookChildren($book);
Views::add($book);
$this->setPageTitle($book->getShortName());
- return view('books/show', ['book' => $book, 'current' => $book, 'bookChildren' => $bookChildren]);
+ return view('books/show', [
+ 'book' => $book,
+ 'current' => $book,
+ 'bookChildren' => $bookChildren,
+ 'activity' => Activity::entityActivity($book, 20, 0)
+ ]);
}
/**
'name' => 'required|string|max:255',
'description' => 'string|max:1000'
]);
- $book = $this->bookRepo->updateFromInput($book, $request->all());
- Activity::add($book, 'book_update', $book->id);
- return redirect($book->getUrl());
+ $book = $this->entityRepo->updateFromInput('book', $book, $request->all());
+ Activity::add($book, 'book_update', $book->id);
+ return redirect($book->getUrl());
}
/**
{
$book = $this->entityRepo->getBySlug('book', $bookSlug);
$this->checkOwnablePermission('book-update', $book);
- $bookChildren = $this->bookRepo->getChildren($book, true);
+ $bookChildren = $this->entityRepo->getBookChildren($book, true);
$books = $this->entityRepo->getAll('book', false);
$this->setPageTitle(trans('entities.books_sort_named', ['bookName'=>$book->getShortName()]));
return view('books/sort', ['book' => $book, 'current' => $book, 'books' => $books, 'bookChildren' => $bookChildren]);
public function getSortItem($bookSlug)
{
$book = $this->entityRepo->getBySlug('book', $bookSlug);
- $bookChildren = $this->bookRepo->getChildren($book);
+ $bookChildren = $this->entityRepo->getBookChildren($book);
return view('books/sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
}
$this->checkOwnablePermission('book-update', $book);
// Return if no map sent
- if (!$request->has('sort-tree')) {
+ if (!$request->filled('sort-tree')) {
return redirect($book->getUrl());
}
// Update models only if there's a change in parent chain or ordering.
if ($model->priority !== $priority || $model->book_id !== $bookId || ($isPage && $model->chapter_id !== $chapterId)) {
- $isPage ? $this->pageRepo->changeBook($bookId, $model) : $this->chapterRepo->changeBook($bookId, $model);
+ $this->entityRepo->changeBook($isPage?'page':'chapter', $bookId, $model);
$model->priority = $priority;
if ($isPage) $model->chapter_id = $chapterId;
$model->save();
// Add activity for books
foreach ($sortedBooks as $bookId) {
+ /** @var Book $updatedBook */
$updatedBook = $this->entityRepo->getById('book', $bookId);
+ $this->entityRepo->buildJointPermissionsForBook($updatedBook);
Activity::add($updatedBook, 'book_sort', $updatedBook->id);
}
- // Update permissions on changed models
- $this->bookRepo->buildJointPermissions($updatedModels);
-
return redirect($book->getUrl());
}
$book = $this->entityRepo->getBySlug('book', $bookSlug);
$this->checkOwnablePermission('book-delete', $book);
Activity::addMessage('book_delete', 0, $book->name);
- Activity::removeEntity($book);
- $this->bookRepo->destroy($book);
+ $this->entityRepo->destroyBook($book);
return redirect('/books');
}
{
$book = $this->entityRepo->getBySlug('book', $bookSlug);
$this->checkOwnablePermission('restrictions-manage', $book);
- $this->bookRepo->updateEntityPermissionsFromRequest($request, $book);
+ $this->entityRepo->updateEntityPermissionsFromRequest($request, $book);
session()->flash('success', trans('entities.books_permissions_updated'));
return redirect($book->getUrl());
}
+
+ /**
+ * Export a book as a PDF file.
+ * @param string $bookSlug
+ * @return mixed
+ */
+ public function exportPdf($bookSlug)
+ {
+ $book = $this->entityRepo->getBySlug('book', $bookSlug);
+ $pdfContent = $this->exportService->bookToPdf($book);
+ return response()->make($pdfContent, 200, [
+ 'Content-Type' => 'application/octet-stream',
+ 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.pdf'
+ ]);
+ }
+
+ /**
+ * Export a book as a contained HTML file.
+ * @param string $bookSlug
+ * @return mixed
+ */
+ public function exportHtml($bookSlug)
+ {
+ $book = $this->entityRepo->getBySlug('book', $bookSlug);
+ $htmlContent = $this->exportService->bookToContainedHtml($book);
+ return response()->make($htmlContent, 200, [
+ 'Content-Type' => 'application/octet-stream',
+ 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.html'
+ ]);
+ }
+
+ /**
+ * Export a book as a plain text file.
+ * @param $bookSlug
+ * @return mixed
+ */
+ public function exportPlainText($bookSlug)
+ {
+ $book = $this->entityRepo->getBySlug('book', $bookSlug);
+ $htmlContent = $this->exportService->bookToPlainText($book);
+ return response()->make($htmlContent, 200, [
+ 'Content-Type' => 'application/octet-stream',
+ 'Content-Disposition' => 'attachment; filename="' . $bookSlug . '.txt'
+ ]);
+ }
}