]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/HomeController.php
Updated php dependancies for minor release
[bookstack] / app / Http / Controllers / HomeController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Entities\Models\Book;
5 use BookStack\Entities\Queries\RecentlyViewed;
6 use BookStack\Entities\Queries\TopFavourites;
7 use BookStack\Entities\Tools\PageContent;
8 use BookStack\Entities\Models\Page;
9 use BookStack\Entities\Repos\BookRepo;
10 use BookStack\Entities\Repos\BookshelfRepo;
11 use Views;
12
13 class HomeController extends Controller
14 {
15
16     /**
17      * Display the homepage.
18      */
19     public function index()
20     {
21         $activity = Activity::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 ? 6 : 12)
43             ->get();
44
45         $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
46         $homepageOption = setting('app-homepage-type', 'default');
47         if (!in_array($homepageOption, $homepageOptions)) {
48             $homepageOption = 'default';
49         }
50
51         $commonData = [
52             'activity' => $activity,
53             'recents' => $recents,
54             'recentlyUpdatedPages' => $recentlyUpdatedPages,
55             'draftPages' => $draftPages,
56             'favourites' => $favourites,
57         ];
58
59         // Add required list ordering & sorting for books & shelves views.
60         if ($homepageOption === 'bookshelves' || $homepageOption === 'books') {
61             $key = $homepageOption;
62             $view = setting()->getForCurrentUser($key . '_view_type');
63             $sort = setting()->getForCurrentUser($key . '_sort', 'name');
64             $order = setting()->getForCurrentUser($key . '_sort_order', 'asc');
65
66             $sortOptions = [
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                 'sort' => $sort,
75                 'order' => $order,
76                 'sortOptions' => $sortOptions,
77             ]);
78         }
79
80         if ($homepageOption === 'bookshelves') {
81             $shelves = app(BookshelfRepo::class)->getAllPaginated(18, $commonData['sort'], $commonData['order']);
82             $data = array_merge($commonData, ['shelves' => $shelves]);
83             return view('common.home-shelves', $data);
84         }
85
86         if ($homepageOption === 'books') {
87             $bookRepo = app(BookRepo::class);
88             $books = $bookRepo->getAllPaginated(18, $commonData['sort'], $commonData['order']);
89             $data = array_merge($commonData, ['books' => $books]);
90             return view('common.home-book', $data);
91         }
92
93         if ($homepageOption === 'page') {
94             $homepageSetting = setting('app-homepage', '0:');
95             $id = intval(explode(':', $homepageSetting)[0]);
96             $customHomepage = Page::query()->where('draft', '=', false)->findOrFail($id);
97             $pageContent = new PageContent($customHomepage);
98             $customHomepage->html = $pageContent->render(true);
99             return view('common.home-custom', array_merge($commonData, ['customHomepage' => $customHomepage]));
100         }
101
102         return view('common.home', $commonData);
103     }
104
105     /**
106      * Get custom head HTML, Used in ajax calls to show in editor.
107      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
108      */
109     public function customHeadContent()
110     {
111         return view('partials.custom-head');
112     }
113
114     /**
115      * Show the view for /robots.txt
116      */
117     public function getRobots()
118     {
119         $sitePublic = setting('app-public', false);
120         $allowRobots = config('app.allow_robots');
121
122         if ($allowRobots === null) {
123             $allowRobots = $sitePublic;
124         }
125
126         return response()
127             ->view('common.robots', ['allowRobots' => $allowRobots])
128             ->header('Content-Type', 'text/plain');
129     }
130
131     /**
132      * Show the route for 404 responses.
133      */
134     public function getNotFound()
135     {
136         return response()->view('errors.404', [], 404);
137     }
138 }