]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/HomeController.php
Update Localization.php in Middleware with "no" tag for estimate.
[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             $shelves = app(BookshelfRepo::class)->getAllPaginated(18, $commonData['sort'], $commonData['order']);
73             $data = array_merge($commonData, ['shelves' => $shelves]);
74             return view('common.home-shelves', $data);
75         }
76
77         if ($homepageOption === 'books') {
78             $bookRepo = app(BookRepo::class);
79             $books = $bookRepo->getAllPaginated(18, $commonData['sort'], $commonData['order']);
80             $data = array_merge($commonData, ['books' => $books]);
81             return view('common.home-book', $data);
82         }
83
84         if ($homepageOption === 'page') {
85             $homepageSetting = setting('app-homepage', '0:');
86             $id = intval(explode(':', $homepageSetting)[0]);
87             $customHomepage = Page::query()->where('draft', '=', false)->findOrFail($id);
88             $pageContent = new PageContent($customHomepage);
89             $customHomepage->html = $pageContent->render(true);
90             return view('common.home-custom', array_merge($commonData, ['customHomepage' => $customHomepage]));
91         }
92
93         return view('common.home', $commonData);
94     }
95
96     /**
97      * Get custom head HTML, Used in ajax calls to show in editor.
98      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
99      */
100     public function customHeadContent()
101     {
102         return view('partials.custom-head-content');
103     }
104
105     /**
106      * Show the view for /robots.txt
107      * @return $this
108      */
109     public function getRobots()
110     {
111         $sitePublic = setting('app-public', false);
112         $allowRobots = config('app.allow_robots');
113         if ($allowRobots === null) {
114             $allowRobots = $sitePublic;
115         }
116         return response()
117             ->view('common.robots', ['allowRobots' => $allowRobots])
118             ->header('Content-Type', 'text/plain');
119     }
120
121     /**
122      * Show the route for 404 responses.
123      */
124     public function getNotFound()
125     {
126         return response()->view('errors.404', [], 404);
127     }
128 }