3 namespace BookStack\App;
5 use BookStack\Activity\ActivityQueries;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Entities\Queries\EntityQueries;
8 use BookStack\Entities\Queries\QueryRecentlyViewed;
9 use BookStack\Entities\Queries\QueryTopFavourites;
10 use BookStack\Entities\Tools\PageContent;
11 use BookStack\Http\Controller;
12 use BookStack\Uploads\FaviconHandler;
13 use BookStack\Util\SimpleListOptions;
14 use Illuminate\Http\Request;
16 class HomeController extends Controller
18 public function __construct(
19 protected EntityQueries $queries,
24 * Display the homepage.
26 public function index(
28 ActivityQueries $activities,
29 QueryRecentlyViewed $recentlyViewed,
30 QueryTopFavourites $topFavourites,
32 $activity = $activities->latest(10);
35 if ($this->isSignedIn()) {
36 $draftPages = $this->queries->pages->currentUserDraftsForList()
37 ->orderBy('updated_at', 'desc')
43 $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
44 $recents = $this->isSignedIn() ?
45 $recentlyViewed->run(12 * $recentFactor, 1)
46 : $this->queries->books->visibleForList()->orderBy('created_at', 'desc')->take(12 * $recentFactor)->get();
47 $favourites = $topFavourites->run(6);
48 $recentlyUpdatedPages = $this->queries->pages->visibleForList()
49 ->where('draft', false)
50 ->orderBy('updated_at', 'desc')
51 ->take($favourites->count() > 0 ? 5 : 10)
54 $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
55 $homepageOption = setting('app-homepage-type', 'default');
56 if (!in_array($homepageOption, $homepageOptions)) {
57 $homepageOption = 'default';
61 'activity' => $activity,
62 'recents' => $recents,
63 'recentlyUpdatedPages' => $recentlyUpdatedPages,
64 'draftPages' => $draftPages,
65 'favourites' => $favourites,
68 // Add required list ordering & sorting for books & shelves views.
69 if ($homepageOption === 'bookshelves' || $homepageOption === 'books') {
70 $key = $homepageOption;
71 $view = setting()->getForCurrentUser($key . '_view_type');
72 $listOptions = SimpleListOptions::fromRequest($request, $key)->withSortOptions([
73 'name' => trans('common.sort_name'),
74 'created_at' => trans('common.sort_created_at'),
75 'updated_at' => trans('common.sort_updated_at'),
78 $commonData = array_merge($commonData, [
80 'listOptions' => $listOptions,
84 if ($homepageOption === 'bookshelves') {
85 $shelves = $this->queries->shelves->visibleForListWithCover()
86 ->orderBy($commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder())
88 $data = array_merge($commonData, ['shelves' => $shelves]);
90 return view('home.shelves', $data);
93 if ($homepageOption === 'books') {
94 $books = $this->queries->books->visibleForListWithCover()
95 ->orderBy($commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder())
97 $data = array_merge($commonData, ['books' => $books]);
99 return view('home.books', $data);
102 if ($homepageOption === 'page') {
103 $homepageSetting = setting('app-homepage', '0:');
104 $id = intval(explode(':', $homepageSetting)[0]);
105 /** @var Page $customHomepage */
106 $customHomepage = $this->queries->pages->start()->where('draft', '=', false)->findOrFail($id);
107 $pageContent = new PageContent($customHomepage);
108 $customHomepage->html = $pageContent->render(false);
110 return view('home.specific-page', array_merge($commonData, ['customHomepage' => $customHomepage]));
113 return view('home.default', $commonData);
117 * Show the view for /robots.txt.
119 public function robots()
121 $sitePublic = setting('app-public', false);
122 $allowRobots = config('app.allow_robots');
124 if ($allowRobots === null) {
125 $allowRobots = $sitePublic;
129 ->view('misc.robots', ['allowRobots' => $allowRobots])
130 ->header('Content-Type', 'text/plain');
134 * Show the route for 404 responses.
136 public function notFound()
138 return response()->view('errors.404', [], 404);
142 * Serve the application favicon.
143 * Ensures a 'favicon.ico' file exists at the web root location (if writable) to be served
144 * directly by the webserver in the future.
146 public function favicon(FaviconHandler $favicons)
148 $exists = $favicons->restoreOriginalIfNotExists();
149 return response()->file($exists ? $favicons->getPath() : $favicons->getOriginalPath());
153 * Serve a PWA application manifest.
155 public function pwaManifest(PwaManifestBuilder $manifestBuilder)
157 return response()->json($manifestBuilder->build());