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;
13 use BookStack\Uploads\FaviconHandler;
14 use BookStack\Util\SimpleListOptions;
15 use Illuminate\Http\Request;
17 class HomeController extends Controller
20 * Display the homepage.
22 public function index(Request $request, ActivityQueries $activities)
24 $activity = $activities->latest(10);
27 if ($this->isSignedIn()) {
28 $draftPages = Page::visible()
29 ->where('draft', '=', true)
30 ->where('created_by', '=', user()->id)
31 ->orderBy('updated_at', 'desc')
37 $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
38 $recents = $this->isSignedIn() ?
39 (new RecentlyViewed())->run(12 * $recentFactor, 1)
40 : Book::visible()->orderBy('created_at', 'desc')->take(12 * $recentFactor)->get();
41 $favourites = (new TopFavourites())->run(6);
42 $recentlyUpdatedPages = Page::visible()->with('book')
43 ->where('draft', false)
44 ->orderBy('updated_at', 'desc')
45 ->take($favourites->count() > 0 ? 5 : 10)
46 ->select(Page::$listAttributes)
49 $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
50 $homepageOption = setting('app-homepage-type', 'default');
51 if (!in_array($homepageOption, $homepageOptions)) {
52 $homepageOption = 'default';
56 'activity' => $activity,
57 'recents' => $recents,
58 'recentlyUpdatedPages' => $recentlyUpdatedPages,
59 'draftPages' => $draftPages,
60 'favourites' => $favourites,
63 // Add required list ordering & sorting for books & shelves views.
64 if ($homepageOption === 'bookshelves' || $homepageOption === 'books') {
65 $key = $homepageOption;
66 $view = setting()->getForCurrentUser($key . '_view_type');
67 $listOptions = SimpleListOptions::fromRequest($request, $key)->withSortOptions([
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, [
75 'listOptions' => $listOptions,
79 if ($homepageOption === 'bookshelves') {
80 $shelves = app(BookshelfRepo::class)->getAllPaginated(18, $commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder());
81 $data = array_merge($commonData, ['shelves' => $shelves]);
83 return view('home.shelves', $data);
86 if ($homepageOption === 'books') {
87 $books = app(BookRepo::class)->getAllPaginated(18, $commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder());
88 $data = array_merge($commonData, ['books' => $books]);
90 return view('home.books', $data);
93 if ($homepageOption === 'page') {
94 $homepageSetting = setting('app-homepage', '0:');
95 $id = intval(explode(':', $homepageSetting)[0]);
96 /** @var Page $customHomepage */
97 $customHomepage = Page::query()->where('draft', '=', false)->findOrFail($id);
98 $pageContent = new PageContent($customHomepage);
99 $customHomepage->html = $pageContent->render(false);
101 return view('home.specific-page', array_merge($commonData, ['customHomepage' => $customHomepage]));
104 return view('home.default', $commonData);
108 * Show the view for /robots.txt.
110 public function robots()
112 $sitePublic = setting('app-public', false);
113 $allowRobots = config('app.allow_robots');
115 if ($allowRobots === null) {
116 $allowRobots = $sitePublic;
120 ->view('misc.robots', ['allowRobots' => $allowRobots])
121 ->header('Content-Type', 'text/plain');
125 * Show the route for 404 responses.
127 public function notFound()
129 return response()->view('errors.404', [], 404);
133 * Serve the application favicon.
134 * Ensures a 'favicon.ico' file exists at the web root location (if writable) to be served
135 * directly by the webserver in the future.
137 public function favicon(FaviconHandler $favicons)
139 $favicons->restoreOriginalIfNotExists();
140 return response()->file($favicons->getPath());