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\RecentlyViewed;
9 use BookStack\Entities\Queries\TopFavourites;
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(Request $request, ActivityQueries $activities)
28 $activity = $activities->latest(10);
31 if ($this->isSignedIn()) {
32 $draftPages = $this->queries->pages->currentUserDraftsForList()
33 ->orderBy('updated_at', 'desc')
39 $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
40 $recents = $this->isSignedIn() ?
41 (new RecentlyViewed())->run(12 * $recentFactor, 1)
42 : $this->queries->books->visibleForList()->orderBy('created_at', 'desc')->take(12 * $recentFactor)->get();
43 $favourites = (new TopFavourites())->run(6);
44 $recentlyUpdatedPages = $this->queries->pages->visibleForList()
45 ->where('draft', false)
46 ->orderBy('updated_at', 'desc')
47 ->take($favourites->count() > 0 ? 5 : 10)
50 $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
51 $homepageOption = setting('app-homepage-type', 'default');
52 if (!in_array($homepageOption, $homepageOptions)) {
53 $homepageOption = 'default';
57 'activity' => $activity,
58 'recents' => $recents,
59 'recentlyUpdatedPages' => $recentlyUpdatedPages,
60 'draftPages' => $draftPages,
61 'favourites' => $favourites,
64 // Add required list ordering & sorting for books & shelves views.
65 if ($homepageOption === 'bookshelves' || $homepageOption === 'books') {
66 $key = $homepageOption;
67 $view = setting()->getForCurrentUser($key . '_view_type');
68 $listOptions = SimpleListOptions::fromRequest($request, $key)->withSortOptions([
69 'name' => trans('common.sort_name'),
70 'created_at' => trans('common.sort_created_at'),
71 'updated_at' => trans('common.sort_updated_at'),
74 $commonData = array_merge($commonData, [
76 'listOptions' => $listOptions,
80 if ($homepageOption === 'bookshelves') {
81 $shelves = $this->queries->shelves->visibleForListWithCover()
82 ->orderBy($commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder())
84 $data = array_merge($commonData, ['shelves' => $shelves]);
86 return view('home.shelves', $data);
89 if ($homepageOption === 'books') {
90 $books = $this->queries->books->visibleForListWithCover()
91 ->orderBy($commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder())
93 $data = array_merge($commonData, ['books' => $books]);
95 return view('home.books', $data);
98 if ($homepageOption === 'page') {
99 $homepageSetting = setting('app-homepage', '0:');
100 $id = intval(explode(':', $homepageSetting)[0]);
101 /** @var Page $customHomepage */
102 $customHomepage = $this->queries->pages->start()->where('draft', '=', false)->findOrFail($id);
103 $pageContent = new PageContent($customHomepage);
104 $customHomepage->html = $pageContent->render(false);
106 return view('home.specific-page', array_merge($commonData, ['customHomepage' => $customHomepage]));
109 return view('home.default', $commonData);
113 * Show the view for /robots.txt.
115 public function robots()
117 $sitePublic = setting('app-public', false);
118 $allowRobots = config('app.allow_robots');
120 if ($allowRobots === null) {
121 $allowRobots = $sitePublic;
125 ->view('misc.robots', ['allowRobots' => $allowRobots])
126 ->header('Content-Type', 'text/plain');
130 * Show the route for 404 responses.
132 public function notFound()
134 return response()->view('errors.404', [], 404);
138 * Serve the application favicon.
139 * Ensures a 'favicon.ico' file exists at the web root location (if writable) to be served
140 * directly by the webserver in the future.
142 public function favicon(FaviconHandler $favicons)
144 $exists = $favicons->restoreOriginalIfNotExists();
145 return response()->file($exists ? $favicons->getPath() : $favicons->getOriginalPath());
149 * Serve a PWA application manifest.
151 public function pwaManifest(PwaManifestBuilder $manifestBuilder)
153 return response()->json($manifestBuilder->build());