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