1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Entities\Models\Book;
5 use BookStack\Entities\Queries\RecentlyViewed;
6 use BookStack\Entities\Queries\TopFavourites;
7 use BookStack\Entities\Tools\PageContent;
8 use BookStack\Entities\Models\Page;
9 use BookStack\Entities\Repos\BookRepo;
10 use BookStack\Entities\Repos\BookshelfRepo;
13 class HomeController extends Controller
17 * Display the homepage.
19 public function index()
21 $activity = Activity::latest(10);
24 if ($this->isSignedIn()) {
25 $draftPages = Page::visible()
26 ->where('draft', '=', true)
27 ->where('created_by', '=', user()->id)
28 ->orderBy('updated_at', 'desc')
34 $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
35 $recents = $this->isSignedIn() ?
36 (new RecentlyViewed)->run(12*$recentFactor, 1)
37 : Book::visible()->orderBy('created_at', 'desc')->take(12 * $recentFactor)->get();
38 $favourites = (new TopFavourites)->run(6);
39 $recentlyUpdatedPages = Page::visible()->with('book')
40 ->where('draft', false)
41 ->orderBy('updated_at', 'desc')
42 ->take($favourites->count() > 0 ? 6 : 12)
45 $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
46 $homepageOption = setting('app-homepage-type', 'default');
47 if (!in_array($homepageOption, $homepageOptions)) {
48 $homepageOption = 'default';
52 'activity' => $activity,
53 'recents' => $recents,
54 'recentlyUpdatedPages' => $recentlyUpdatedPages,
55 'draftPages' => $draftPages,
56 'favourites' => $favourites,
59 // Add required list ordering & sorting for books & shelves views.
60 if ($homepageOption === 'bookshelves' || $homepageOption === 'books') {
61 $key = $homepageOption;
62 $view = setting()->getForCurrentUser($key . '_view_type');
63 $sort = setting()->getForCurrentUser($key . '_sort', 'name');
64 $order = setting()->getForCurrentUser($key . '_sort_order', 'asc');
67 'name' => trans('common.sort_name'),
68 'created_at' => trans('common.sort_created_at'),
69 'updated_at' => trans('common.sort_updated_at'),
72 $commonData = array_merge($commonData, [
76 'sortOptions' => $sortOptions,
80 if ($homepageOption === 'bookshelves') {
81 $shelves = app(BookshelfRepo::class)->getAllPaginated(18, $commonData['sort'], $commonData['order']);
82 $data = array_merge($commonData, ['shelves' => $shelves]);
83 return view('common.home-shelves', $data);
86 if ($homepageOption === 'books') {
87 $bookRepo = app(BookRepo::class);
88 $books = $bookRepo->getAllPaginated(18, $commonData['sort'], $commonData['order']);
89 $data = array_merge($commonData, ['books' => $books]);
90 return view('common.home-book', $data);
93 if ($homepageOption === 'page') {
94 $homepageSetting = setting('app-homepage', '0:');
95 $id = intval(explode(':', $homepageSetting)[0]);
96 $customHomepage = Page::query()->where('draft', '=', false)->findOrFail($id);
97 $pageContent = new PageContent($customHomepage);
98 $customHomepage->html = $pageContent->render(true);
99 return view('common.home-custom', array_merge($commonData, ['customHomepage' => $customHomepage]));
102 return view('common.home', $commonData);
106 * Get custom head HTML, Used in ajax calls to show in editor.
107 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
109 public function customHeadContent()
111 return view('partials.custom-head');
115 * Show the view for /robots.txt
117 public function getRobots()
119 $sitePublic = setting('app-public', false);
120 $allowRobots = config('app.allow_robots');
122 if ($allowRobots === null) {
123 $allowRobots = $sitePublic;
127 ->view('common.robots', ['allowRobots' => $allowRobots])
128 ->header('Content-Type', 'text/plain');
132 * Show the route for 404 responses.
134 public function getNotFound()
136 return response()->view('errors.404', [], 404);