]> BookStack Code Mirror - bookstack/blob - app/App/HomeController.php
Queries: Migrated BookRepo queries to new query class
[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\Repos\BookshelfRepo;
12 use BookStack\Entities\Tools\PageContent;
13 use BookStack\Http\Controller;
14 use BookStack\Uploads\FaviconHandler;
15 use BookStack\Util\SimpleListOptions;
16 use Illuminate\Http\Request;
17
18 class HomeController extends Controller
19 {
20     public function __construct(
21         protected EntityQueries $queries,
22     ) {
23     }
24
25     /**
26      * Display the homepage.
27      */
28     public function index(Request $request, ActivityQueries $activities)
29     {
30         $activity = $activities->latest(10);
31         $draftPages = [];
32
33         if ($this->isSignedIn()) {
34             $draftPages = $this->queries->pages->currentUserDraftsForList()
35                 ->orderBy('updated_at', 'desc')
36                 ->with('book')
37                 ->take(6)
38                 ->get();
39         }
40
41         $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
42         $recents = $this->isSignedIn() ?
43             (new RecentlyViewed())->run(12 * $recentFactor, 1)
44             : Book::visible()->orderBy('created_at', 'desc')->take(12 * $recentFactor)->get();
45         $favourites = (new TopFavourites())->run(6);
46         $recentlyUpdatedPages = $this->queries->pages->visibleForList()
47             ->where('draft', false)
48             ->orderBy('updated_at', 'desc')
49             ->take($favourites->count() > 0 ? 5 : 10)
50             ->get();
51
52         $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
53         $homepageOption = setting('app-homepage-type', 'default');
54         if (!in_array($homepageOption, $homepageOptions)) {
55             $homepageOption = 'default';
56         }
57
58         $commonData = [
59             'activity'             => $activity,
60             'recents'              => $recents,
61             'recentlyUpdatedPages' => $recentlyUpdatedPages,
62             'draftPages'           => $draftPages,
63             'favourites'           => $favourites,
64         ];
65
66         // Add required list ordering & sorting for books & shelves views.
67         if ($homepageOption === 'bookshelves' || $homepageOption === 'books') {
68             $key = $homepageOption;
69             $view = setting()->getForCurrentUser($key . '_view_type');
70             $listOptions = SimpleListOptions::fromRequest($request, $key)->withSortOptions([
71                 'name' => trans('common.sort_name'),
72                 'created_at' => trans('common.sort_created_at'),
73                 'updated_at' => trans('common.sort_updated_at'),
74             ]);
75
76             $commonData = array_merge($commonData, [
77                 'view'        => $view,
78                 'listOptions' => $listOptions,
79             ]);
80         }
81
82         if ($homepageOption === 'bookshelves') {
83             $shelves = app()->make(BookshelfRepo::class)->getAllPaginated(18, $commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder());
84             $data = array_merge($commonData, ['shelves' => $shelves]);
85
86             return view('home.shelves', $data);
87         }
88
89         if ($homepageOption === 'books') {
90             $books = $this->queries->books->visibleForListWithCover()
91                 ->orderBy($commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder())
92                 ->paginate(18);
93             $data = array_merge($commonData, ['books' => $books]);
94
95             return view('home.books', $data);
96         }
97
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);
105
106             return view('home.specific-page', array_merge($commonData, ['customHomepage' => $customHomepage]));
107         }
108
109         return view('home.default', $commonData);
110     }
111
112     /**
113      * Show the view for /robots.txt.
114      */
115     public function robots()
116     {
117         $sitePublic = setting('app-public', false);
118         $allowRobots = config('app.allow_robots');
119
120         if ($allowRobots === null) {
121             $allowRobots = $sitePublic;
122         }
123
124         return response()
125             ->view('misc.robots', ['allowRobots' => $allowRobots])
126             ->header('Content-Type', 'text/plain');
127     }
128
129     /**
130      * Show the route for 404 responses.
131      */
132     public function notFound()
133     {
134         return response()->view('errors.404', [], 404);
135     }
136
137     /**
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.
141      */
142     public function favicon(FaviconHandler $favicons)
143     {
144         $exists = $favicons->restoreOriginalIfNotExists();
145         return response()->file($exists ? $favicons->getPath() : $favicons->getOriginalPath());
146     }
147
148     /**
149      * Serve a PWA application manifest.
150      */
151     public function pwaManifest(PwaManifestBuilder $manifestBuilder)
152     {
153         return response()->json($manifestBuilder->build());
154     }
155 }