3 namespace BookStack\Http\Controllers;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Entities\Queries\RecentlyViewed;
9 use BookStack\Entities\Queries\TopFavourites;
10 use BookStack\Entities\Repos\BookRepo;
11 use BookStack\Entities\Repos\BookshelfRepo;
12 use BookStack\Entities\Tools\PageContent;
15 class HomeController extends Controller
18 * Display the homepage.
20 public function index()
22 $activity = Activity::latest(10);
25 if ($this->isSignedIn()) {
26 $draftPages = Page::visible()
27 ->where('draft', '=', true)
28 ->where('created_by', '=', user()->id)
29 ->orderBy('updated_at', 'desc')
35 $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
36 $recents = $this->isSignedIn() ?
37 (new RecentlyViewed())->run(12 * $recentFactor, 1)
38 : Book::visible()->orderBy('created_at', 'desc')->take(12 * $recentFactor)->get();
39 $favourites = (new TopFavourites())->run(6);
40 $recentlyUpdatedPages = Page::visible()->with('book')
41 ->where('draft', false)
42 ->orderBy('updated_at', 'desc')
43 ->take($favourites->count() > 0 ? 6 : 12)
46 $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
47 $homepageOption = setting('app-homepage-type', 'default');
48 if (!in_array($homepageOption, $homepageOptions)) {
49 $homepageOption = 'default';
53 'activity' => $activity,
54 'recents' => $recents,
55 'recentlyUpdatedPages' => $recentlyUpdatedPages,
56 'draftPages' => $draftPages,
57 'favourites' => $favourites,
60 // Add required list ordering & sorting for books & shelves views.
61 if ($homepageOption === 'bookshelves' || $homepageOption === 'books') {
62 $key = $homepageOption;
63 $view = setting()->getForCurrentUser($key . '_view_type');
64 $sort = setting()->getForCurrentUser($key . '_sort', 'name');
65 $order = setting()->getForCurrentUser($key . '_sort_order', 'asc');
68 'name' => trans('common.sort_name'),
69 'created_at' => trans('common.sort_created_at'),
70 'updated_at' => trans('common.sort_updated_at'),
73 $commonData = array_merge($commonData, [
77 'sortOptions' => $sortOptions,
81 if ($homepageOption === 'bookshelves') {
82 $shelves = app(BookshelfRepo::class)->getAllPaginated(18, $commonData['sort'], $commonData['order']);
83 $data = array_merge($commonData, ['shelves' => $shelves]);
85 return view('common.home-shelves', $data);
88 if ($homepageOption === 'books') {
89 $bookRepo = app(BookRepo::class);
90 $books = $bookRepo->getAllPaginated(18, $commonData['sort'], $commonData['order']);
91 $data = array_merge($commonData, ['books' => $books]);
93 return view('common.home-book', $data);
96 if ($homepageOption === 'page') {
97 $homepageSetting = setting('app-homepage', '0:');
98 $id = intval(explode(':', $homepageSetting)[0]);
99 $customHomepage = Page::query()->where('draft', '=', false)->findOrFail($id);
100 $pageContent = new PageContent($customHomepage);
101 $customHomepage->html = $pageContent->render(true);
103 return view('common.home-custom', array_merge($commonData, ['customHomepage' => $customHomepage]));
106 return view('common.home', $commonData);
110 * Get custom head HTML, Used in ajax calls to show in editor.
112 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
114 public function customHeadContent()
116 return view('partials.custom-head');
120 * Show the view for /robots.txt.
122 public function getRobots()
124 $sitePublic = setting('app-public', false);
125 $allowRobots = config('app.allow_robots');
127 if ($allowRobots === null) {
128 $allowRobots = $sitePublic;
132 ->view('common.robots', ['allowRobots' => $allowRobots])
133 ->header('Content-Type', 'text/plain');
137 * Show the route for 404 responses.
139 public function getNotFound()
141 return response()->view('errors.404', [], 404);