3 namespace BookStack\App;
5 use BookStack\Activity\ActivityQueries;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Entities\Queries\EntityQueries;
9 use BookStack\Entities\Queries\RecentlyViewed;
10 use BookStack\Entities\Queries\TopFavourites;
11 use BookStack\Entities\Tools\PageContent;
12 use BookStack\Http\Controller;
13 use BookStack\Uploads\FaviconHandler;
14 use BookStack\Util\SimpleListOptions;
15 use Illuminate\Http\Request;
17 class HomeController extends Controller
19 public function __construct(
20 protected EntityQueries $queries,
25 * Display the homepage.
27 public function index(Request $request, ActivityQueries $activities)
29 $activity = $activities->latest(10);
32 if ($this->isSignedIn()) {
33 $draftPages = $this->queries->pages->currentUserDraftsForList()
34 ->orderBy('updated_at', 'desc')
40 $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
41 $recents = $this->isSignedIn() ?
42 (new RecentlyViewed())->run(12 * $recentFactor, 1)
43 : $this->queries->books->visibleForList()->orderBy('created_at', 'desc')->take(12 * $recentFactor)->get();
44 $favourites = (new TopFavourites())->run(6);
45 $recentlyUpdatedPages = $this->queries->pages->visibleForList()
46 ->where('draft', false)
47 ->orderBy('updated_at', 'desc')
48 ->take($favourites->count() > 0 ? 5 : 10)
51 $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
52 $homepageOption = setting('app-homepage-type', 'default');
53 if (!in_array($homepageOption, $homepageOptions)) {
54 $homepageOption = 'default';
58 'activity' => $activity,
59 'recents' => $recents,
60 'recentlyUpdatedPages' => $recentlyUpdatedPages,
61 'draftPages' => $draftPages,
62 'favourites' => $favourites,
65 // Add required list ordering & sorting for books & shelves views.
66 if ($homepageOption === 'bookshelves' || $homepageOption === 'books') {
67 $key = $homepageOption;
68 $view = setting()->getForCurrentUser($key . '_view_type');
69 $listOptions = SimpleListOptions::fromRequest($request, $key)->withSortOptions([
70 'name' => trans('common.sort_name'),
71 'created_at' => trans('common.sort_created_at'),
72 'updated_at' => trans('common.sort_updated_at'),
75 $commonData = array_merge($commonData, [
77 'listOptions' => $listOptions,
81 if ($homepageOption === 'bookshelves') {
82 $shelves = $this->queries->shelves->visibleForListWithCover()
83 ->orderBy($commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder())
85 $data = array_merge($commonData, ['shelves' => $shelves]);
87 return view('home.shelves', $data);
90 if ($homepageOption === 'books') {
91 $books = $this->queries->books->visibleForListWithCover()
92 ->orderBy($commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder())
94 $data = array_merge($commonData, ['books' => $books]);
96 return view('home.books', $data);
99 if ($homepageOption === 'page') {
100 $homepageSetting = setting('app-homepage', '0:');
101 $id = intval(explode(':', $homepageSetting)[0]);
102 /** @var Page $customHomepage */
103 $customHomepage = $this->queries->pages->start()->where('draft', '=', false)->findOrFail($id);
104 $pageContent = new PageContent($customHomepage);
105 $customHomepage->html = $pageContent->render(false);
107 return view('home.specific-page', array_merge($commonData, ['customHomepage' => $customHomepage]));
110 return view('home.default', $commonData);
114 * Show the view for /robots.txt.
116 public function robots()
118 $sitePublic = setting('app-public', false);
119 $allowRobots = config('app.allow_robots');
121 if ($allowRobots === null) {
122 $allowRobots = $sitePublic;
126 ->view('misc.robots', ['allowRobots' => $allowRobots])
127 ->header('Content-Type', 'text/plain');
131 * Show the route for 404 responses.
133 public function notFound()
135 return response()->view('errors.404', [], 404);
139 * Serve the application favicon.
140 * Ensures a 'favicon.ico' file exists at the web root location (if writable) to be served
141 * directly by the webserver in the future.
143 public function favicon(FaviconHandler $favicons)
145 $exists = $favicons->restoreOriginalIfNotExists();
146 return response()->file($exists ? $favicons->getPath() : $favicons->getOriginalPath());
150 * Serve a PWA application manifest.
152 public function pwaManifest(PwaManifestBuilder $manifestBuilder)
154 return response()->json($manifestBuilder->build());