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\PageQueries;
9 use BookStack\Entities\Queries\RecentlyViewed;
10 use BookStack\Entities\Queries\TopFavourites;
11 use BookStack\Entities\Repos\BookRepo;
12 use BookStack\Entities\Repos\BookshelfRepo;
13 use BookStack\Entities\Tools\PageContent;
14 use BookStack\Http\Controller;
15 use BookStack\Uploads\FaviconHandler;
16 use BookStack\Util\SimpleListOptions;
17 use Illuminate\Http\Request;
19 class HomeController extends Controller
22 * Display the homepage.
24 public function index(Request $request, ActivityQueries $activities)
26 $activity = $activities->latest(10);
29 if ($this->isSignedIn()) {
30 $draftPages = PageQueries::currentUserDraftsForList()
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 = PageQueries::visibleForList()
43 ->where('draft', false)
44 ->orderBy('updated_at', 'desc')
45 ->take($favourites->count() > 0 ? 5 : 10)
48 $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
49 $homepageOption = setting('app-homepage-type', 'default');
50 if (!in_array($homepageOption, $homepageOptions)) {
51 $homepageOption = 'default';
55 'activity' => $activity,
56 'recents' => $recents,
57 'recentlyUpdatedPages' => $recentlyUpdatedPages,
58 'draftPages' => $draftPages,
59 'favourites' => $favourites,
62 // Add required list ordering & sorting for books & shelves views.
63 if ($homepageOption === 'bookshelves' || $homepageOption === 'books') {
64 $key = $homepageOption;
65 $view = setting()->getForCurrentUser($key . '_view_type');
66 $listOptions = SimpleListOptions::fromRequest($request, $key)->withSortOptions([
67 'name' => trans('common.sort_name'),
68 'created_at' => trans('common.sort_created_at'),
69 'updated_at' => trans('common.sort_updated_at'),
72 $commonData = array_merge($commonData, [
74 'listOptions' => $listOptions,
78 if ($homepageOption === 'bookshelves') {
79 $shelves = app()->make(BookshelfRepo::class)->getAllPaginated(18, $commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder());
80 $data = array_merge($commonData, ['shelves' => $shelves]);
82 return view('home.shelves', $data);
85 if ($homepageOption === 'books') {
86 $books = app()->make(BookRepo::class)->getAllPaginated(18, $commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder());
87 $data = array_merge($commonData, ['books' => $books]);
89 return view('home.books', $data);
92 if ($homepageOption === 'page') {
93 $homepageSetting = setting('app-homepage', '0:');
94 $id = intval(explode(':', $homepageSetting)[0]);
95 /** @var Page $customHomepage */
96 $customHomepage = PageQueries::start()->where('draft', '=', false)->findOrFail($id);
97 $pageContent = new PageContent($customHomepage);
98 $customHomepage->html = $pageContent->render(false);
100 return view('home.specific-page', array_merge($commonData, ['customHomepage' => $customHomepage]));
103 return view('home.default', $commonData);
107 * Show the view for /robots.txt.
109 public function robots()
111 $sitePublic = setting('app-public', false);
112 $allowRobots = config('app.allow_robots');
114 if ($allowRobots === null) {
115 $allowRobots = $sitePublic;
119 ->view('misc.robots', ['allowRobots' => $allowRobots])
120 ->header('Content-Type', 'text/plain');
124 * Show the route for 404 responses.
126 public function notFound()
128 return response()->view('errors.404', [], 404);
132 * Serve the application favicon.
133 * Ensures a 'favicon.ico' file exists at the web root location (if writable) to be served
134 * directly by the webserver in the future.
136 public function favicon(FaviconHandler $favicons)
138 $exists = $favicons->restoreOriginalIfNotExists();
139 return response()->file($exists ? $favicons->getPath() : $favicons->getOriginalPath());
143 * Serve a PWA application manifest.
145 public function pwaManifest(PwaManifestBuilder $manifestBuilder)
147 return response()->json($manifestBuilder->build());