]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/HomeController.php
Apply fixes from StyleCI
[bookstack] / app / Http / Controllers / HomeController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use Activity;
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 use Views;
14
15 class HomeController extends Controller
16 {
17     /**
18      * Display the homepage.
19      */
20     public function index()
21     {
22         $activity = Activity::latest(10);
23         $draftPages = [];
24
25         if ($this->isSignedIn()) {
26             $draftPages = Page::visible()
27                 ->where('draft', '=', true)
28                 ->where('created_by', '=', user()->id)
29                 ->orderBy('updated_at', 'desc')
30                 ->with('book')
31                 ->take(6)
32                 ->get();
33         }
34
35         $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
36         $recents = $this->isSignedIn() ?
37             (new RecentlyViewed())->run(12 * $recentFactor, 1)
38             : Book::visible()->orderBy('created_at', 'desc')->take(12 * $recentFactor)->get();
39         $favourites = (new TopFavourites())->run(6);
40         $recentlyUpdatedPages = Page::visible()->with('book')
41             ->where('draft', false)
42             ->orderBy('updated_at', 'desc')
43             ->take($favourites->count() > 0 ? 6 : 12)
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('common.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('common.home-book', $data);
94         }
95
96         if ($homepageOption === 'page') {
97             $homepageSetting = setting('app-homepage', '0:');
98             $id = intval(explode(':', $homepageSetting)[0]);
99             $customHomepage = Page::query()->where('draft', '=', false)->findOrFail($id);
100             $pageContent = new PageContent($customHomepage);
101             $customHomepage->html = $pageContent->render(true);
102
103             return view('common.home-custom', array_merge($commonData, ['customHomepage' => $customHomepage]));
104         }
105
106         return view('common.home', $commonData);
107     }
108
109     /**
110      * Get custom head HTML, Used in ajax calls to show in editor.
111      *
112      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
113      */
114     public function customHeadContent()
115     {
116         return view('partials.custom-head');
117     }
118
119     /**
120      * Show the view for /robots.txt.
121      */
122     public function getRobots()
123     {
124         $sitePublic = setting('app-public', false);
125         $allowRobots = config('app.allow_robots');
126
127         if ($allowRobots === null) {
128             $allowRobots = $sitePublic;
129         }
130
131         return response()
132             ->view('common.robots', ['allowRobots' => $allowRobots])
133             ->header('Content-Type', 'text/plain');
134     }
135
136     /**
137      * Show the route for 404 responses.
138      */
139     public function getNotFound()
140     {
141         return response()->view('errors.404', [], 404);
142     }
143 }