3 namespace BookStack\Http\Controllers;
5 use BookStack\Actions\ActivityQueries;
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;
14 class HomeController extends Controller
17 * Display the homepage.
19 public function index(ActivityQueries $activities)
21 $activity = $activities->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 ? 5 : 10)
43 ->select(Page::$listAttributes)
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('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('home.books', $data);
96 if ($homepageOption === 'page') {
97 $homepageSetting = setting('app-homepage', '0:');
98 $id = intval(explode(':', $homepageSetting)[0]);
99 /** @var Page $customHomepage */
100 $customHomepage = Page::query()->where('draft', '=', false)->findOrFail($id);
101 $pageContent = new PageContent($customHomepage);
102 $customHomepage->html = $pageContent->render(false);
104 return view('home.specific-page', array_merge($commonData, ['customHomepage' => $customHomepage]));
107 return view('home.default', $commonData);
111 * Show the view for /robots.txt.
113 public function robots()
115 $sitePublic = setting('app-public', false);
116 $allowRobots = config('app.allow_robots');
118 if ($allowRobots === null) {
119 $allowRobots = $sitePublic;
123 ->view('misc.robots', ['allowRobots' => $allowRobots])
124 ->header('Content-Type', 'text/plain');
128 * Show the route for 404 responses.
130 public function notFound()
132 return response()->view('errors.404', [], 404);