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