]> BookStack Code Mirror - bookstack/blob - app/App/HomeController.php
DB: Started update of entity loading to avoid global selects
[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\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;
18
19 class HomeController extends Controller
20 {
21     /**
22      * Display the homepage.
23      */
24     public function index(Request $request, ActivityQueries $activities)
25     {
26         $activity = $activities->latest(10);
27         $draftPages = [];
28
29         if ($this->isSignedIn()) {
30             $draftPages = PageQueries::currentUserDraftsForList()
31                 ->orderBy('updated_at', 'desc')
32                 ->with('book')
33                 ->take(6)
34                 ->get();
35         }
36
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)
46             ->get();
47
48         $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
49         $homepageOption = setting('app-homepage-type', 'default');
50         if (!in_array($homepageOption, $homepageOptions)) {
51             $homepageOption = 'default';
52         }
53
54         $commonData = [
55             'activity'             => $activity,
56             'recents'              => $recents,
57             'recentlyUpdatedPages' => $recentlyUpdatedPages,
58             'draftPages'           => $draftPages,
59             'favourites'           => $favourites,
60         ];
61
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'),
70             ]);
71
72             $commonData = array_merge($commonData, [
73                 'view'        => $view,
74                 'listOptions' => $listOptions,
75             ]);
76         }
77
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]);
81
82             return view('home.shelves', $data);
83         }
84
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]);
88
89             return view('home.books', $data);
90         }
91
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);
99
100             return view('home.specific-page', array_merge($commonData, ['customHomepage' => $customHomepage]));
101         }
102
103         return view('home.default', $commonData);
104     }
105
106     /**
107      * Show the view for /robots.txt.
108      */
109     public function robots()
110     {
111         $sitePublic = setting('app-public', false);
112         $allowRobots = config('app.allow_robots');
113
114         if ($allowRobots === null) {
115             $allowRobots = $sitePublic;
116         }
117
118         return response()
119             ->view('misc.robots', ['allowRobots' => $allowRobots])
120             ->header('Content-Type', 'text/plain');
121     }
122
123     /**
124      * Show the route for 404 responses.
125      */
126     public function notFound()
127     {
128         return response()->view('errors.404', [], 404);
129     }
130
131     /**
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.
135      */
136     public function favicon(FaviconHandler $favicons)
137     {
138         $exists = $favicons->restoreOriginalIfNotExists();
139         return response()->file($exists ? $favicons->getPath() : $favicons->getOriginalPath());
140     }
141
142     /**
143      * Serve a PWA application manifest.
144      */
145     public function pwaManifest(PwaManifestBuilder $manifestBuilder)
146     {
147         return response()->json($manifestBuilder->build());
148     }
149 }