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