]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/HomeController.php
f38bd71dfc7ea456a9e5e46f74140035ba777c91
[bookstack] / app / Http / Controllers / HomeController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Actions\ActivityQueries;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Entities\Queries\RecentlyViewed;
9 use BookStack\Entities\Queries\TopFavourites;
10 use BookStack\Entities\Repos\BookRepo;
11 use BookStack\Entities\Repos\BookshelfRepo;
12 use BookStack\Entities\Tools\PageContent;
13
14 class HomeController extends Controller
15 {
16     /**
17      * Display the homepage.
18      */
19     public function index(ActivityQueries $activities)
20     {
21         $activity = $activities->latest(10);
22         $draftPages = [];
23
24         if ($this->isSignedIn()) {
25             $draftPages = Page::visible()
26                 ->where('draft', '=', true)
27                 ->where('created_by', '=', user()->id)
28                 ->orderBy('updated_at', 'desc')
29                 ->with('book')
30                 ->take(6)
31                 ->get();
32         }
33
34         $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
35         $recents = $this->isSignedIn() ?
36             (new RecentlyViewed())->run(12 * $recentFactor, 1)
37             : Book::visible()->orderBy('created_at', 'desc')->take(12 * $recentFactor)->get();
38         $favourites = (new TopFavourites())->run(6);
39         $recentlyUpdatedPages = Page::visible()->with('book')
40             ->where('draft', false)
41             ->orderBy('updated_at', 'desc')
42             ->take($favourites->count() > 0 ? 5 : 10)
43             ->select(Page::$listAttributes)
44             ->get();
45
46         $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
47         $homepageOption = setting('app-homepage-type', 'default');
48         if (!in_array($homepageOption, $homepageOptions)) {
49             $homepageOption = 'default';
50         }
51
52         $commonData = [
53             'activity'             => $activity,
54             'recents'              => $recents,
55             'recentlyUpdatedPages' => $recentlyUpdatedPages,
56             'draftPages'           => $draftPages,
57             'favourites'           => $favourites,
58         ];
59
60         // Add required list ordering & sorting for books & shelves views.
61         if ($homepageOption === 'bookshelves' || $homepageOption === 'books') {
62             $key = $homepageOption;
63             $view = setting()->getForCurrentUser($key . '_view_type');
64             $sort = setting()->getForCurrentUser($key . '_sort', 'name');
65             $order = setting()->getForCurrentUser($key . '_sort_order', 'asc');
66
67             $sortOptions = [
68                 'name'       => trans('common.sort_name'),
69                 'created_at' => trans('common.sort_created_at'),
70                 'updated_at' => trans('common.sort_updated_at'),
71             ];
72
73             $commonData = array_merge($commonData, [
74                 'view'        => $view,
75                 'sort'        => $sort,
76                 'order'       => $order,
77                 'sortOptions' => $sortOptions,
78             ]);
79         }
80
81         if ($homepageOption === 'bookshelves') {
82             $shelves = app(BookshelfRepo::class)->getAllPaginated(18, $commonData['sort'], $commonData['order']);
83             $data = array_merge($commonData, ['shelves' => $shelves]);
84
85             return view('home.shelves', $data);
86         }
87
88         if ($homepageOption === 'books') {
89             $bookRepo = app(BookRepo::class);
90             $books = $bookRepo->getAllPaginated(18, $commonData['sort'], $commonData['order']);
91             $data = array_merge($commonData, ['books' => $books]);
92
93             return view('home.books', $data);
94         }
95
96         if ($homepageOption === 'page') {
97             $homepageSetting = setting('app-homepage', '0:');
98             $id = intval(explode(':', $homepageSetting)[0]);
99             /** @var Page $customHomepage */
100             $customHomepage = Page::query()->where('draft', '=', false)->findOrFail($id);
101             $pageContent = new PageContent($customHomepage);
102             $customHomepage->html = $pageContent->render(false);
103
104             return view('home.specific-page', array_merge($commonData, ['customHomepage' => $customHomepage]));
105         }
106
107         return view('home.default', $commonData);
108     }
109
110     /**
111      * Show the view for /robots.txt.
112      */
113     public function robots()
114     {
115         $sitePublic = setting('app-public', false);
116         $allowRobots = config('app.allow_robots');
117
118         if ($allowRobots === null) {
119             $allowRobots = $sitePublic;
120         }
121
122         return response()
123             ->view('misc.robots', ['allowRobots' => $allowRobots])
124             ->header('Content-Type', 'text/plain');
125     }
126
127     /**
128      * Show the route for 404 responses.
129      */
130     public function notFound()
131     {
132         return response()->view('errors.404', [], 404);
133     }
134 }