]> BookStack Code Mirror - bookstack/blob - app/App/HomeController.php
Queries: Updated all app book static query uses
[bookstack] / app / App / HomeController.php
1 <?php
2
3 namespace BookStack\App;
4
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;
16
17 class HomeController extends Controller
18 {
19     public function __construct(
20         protected EntityQueries $queries,
21     ) {
22     }
23
24     /**
25      * Display the homepage.
26      */
27     public function index(Request $request, ActivityQueries $activities)
28     {
29         $activity = $activities->latest(10);
30         $draftPages = [];
31
32         if ($this->isSignedIn()) {
33             $draftPages = $this->queries->pages->currentUserDraftsForList()
34                 ->orderBy('updated_at', 'desc')
35                 ->with('book')
36                 ->take(6)
37                 ->get();
38         }
39
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)
49             ->get();
50
51         $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
52         $homepageOption = setting('app-homepage-type', 'default');
53         if (!in_array($homepageOption, $homepageOptions)) {
54             $homepageOption = 'default';
55         }
56
57         $commonData = [
58             'activity'             => $activity,
59             'recents'              => $recents,
60             'recentlyUpdatedPages' => $recentlyUpdatedPages,
61             'draftPages'           => $draftPages,
62             'favourites'           => $favourites,
63         ];
64
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'),
73             ]);
74
75             $commonData = array_merge($commonData, [
76                 'view'        => $view,
77                 'listOptions' => $listOptions,
78             ]);
79         }
80
81         if ($homepageOption === 'bookshelves') {
82             $shelves = $this->queries->shelves->visibleForListWithCover()
83                 ->orderBy($commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder())
84                 ->paginate(18);
85             $data = array_merge($commonData, ['shelves' => $shelves]);
86
87             return view('home.shelves', $data);
88         }
89
90         if ($homepageOption === 'books') {
91             $books = $this->queries->books->visibleForListWithCover()
92                 ->orderBy($commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder())
93                 ->paginate(18);
94             $data = array_merge($commonData, ['books' => $books]);
95
96             return view('home.books', $data);
97         }
98
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);
106
107             return view('home.specific-page', array_merge($commonData, ['customHomepage' => $customHomepage]));
108         }
109
110         return view('home.default', $commonData);
111     }
112
113     /**
114      * Show the view for /robots.txt.
115      */
116     public function robots()
117     {
118         $sitePublic = setting('app-public', false);
119         $allowRobots = config('app.allow_robots');
120
121         if ($allowRobots === null) {
122             $allowRobots = $sitePublic;
123         }
124
125         return response()
126             ->view('misc.robots', ['allowRobots' => $allowRobots])
127             ->header('Content-Type', 'text/plain');
128     }
129
130     /**
131      * Show the route for 404 responses.
132      */
133     public function notFound()
134     {
135         return response()->view('errors.404', [], 404);
136     }
137
138     /**
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.
142      */
143     public function favicon(FaviconHandler $favicons)
144     {
145         $exists = $favicons->restoreOriginalIfNotExists();
146         return response()->file($exists ? $favicons->getPath() : $favicons->getOriginalPath());
147     }
148
149     /**
150      * Serve a PWA application manifest.
151      */
152     public function pwaManifest(PwaManifestBuilder $manifestBuilder)
153     {
154         return response()->json($manifestBuilder->build());
155     }
156 }