]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/HomeController.php
Merge pull request #802 from marcusforsberg/master
[bookstack] / app / Http / Controllers / HomeController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Repos\EntityRepo;
5 use Illuminate\Http\Request;
6 use Illuminate\Http\Response;
7 use Views;
8
9 class HomeController extends Controller
10 {
11     protected $entityRepo;
12
13     /**
14      * HomeController constructor.
15      * @param EntityRepo $entityRepo
16      */
17     public function __construct(EntityRepo $entityRepo)
18     {
19         $this->entityRepo = $entityRepo;
20         parent::__construct();
21     }
22
23
24     /**
25      * Display the homepage.
26      * @return Response
27      */
28     public function index()
29     {
30         $activity = Activity::latest(10);
31         $draftPages = $this->signedIn ? $this->entityRepo->getUserDraftPages(6) : [];
32         $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
33         $recents = $this->signedIn ? Views::getUserRecentlyViewed(12*$recentFactor, 0) : $this->entityRepo->getRecentlyCreated('book', 12*$recentFactor);
34         $recentlyUpdatedPages = $this->entityRepo->getRecentlyUpdated('page', 12);
35
36
37         $customHomepage = false;
38         $books = false;
39         $booksViewType = false;
40
41         // Check book homepage
42         $bookHomepageSetting = setting('app-book-homepage');
43         if ($bookHomepageSetting) {
44             $books = $this->entityRepo->getAllPaginated('book', 18);
45             $booksViewType = setting()->getUser($this->currentUser, 'books_view_type', config('app.views.books', 'list'));
46         } else {
47             // Check custom homepage
48             $homepageSetting = setting('app-homepage');
49             if ($homepageSetting) {
50                 $id = intval(explode(':', $homepageSetting)[0]);
51                 $customHomepage = $this->entityRepo->getById('page', $id, false, true);
52                 $this->entityRepo->renderPage($customHomepage, true);
53             }
54         }
55
56         $view = 'home';
57         if ($bookHomepageSetting) {
58             $view = 'home-book';
59         } else if ($customHomepage) {
60             $view = 'home-custom';
61         }
62
63         return view('common/' . $view, [
64             'activity' => $activity,
65             'recents' => $recents,
66             'recentlyUpdatedPages' => $recentlyUpdatedPages,
67             'draftPages' => $draftPages,
68             'customHomepage' => $customHomepage,
69             'books' => $books,
70             'booksViewType' => $booksViewType
71         ]);
72     }
73
74     /**
75      * Get a js representation of the current translations
76      * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
77      * @throws \Exception
78      */
79     public function getTranslations()
80     {
81         $locale = app()->getLocale();
82         $cacheKey = 'GLOBAL_TRANSLATIONS_' . $locale;
83         if (cache()->has($cacheKey) && config('app.env') !== 'development') {
84             $resp = cache($cacheKey);
85         } else {
86             $translations = [
87                 // Get only translations which might be used in JS
88                 'common' => trans('common'),
89                 'components' => trans('components'),
90                 'entities' => trans('entities'),
91                 'errors' => trans('errors')
92             ];
93             if ($locale !== 'en') {
94                 $enTrans = [
95                     'common' => trans('common', [], 'en'),
96                     'components' => trans('components', [], 'en'),
97                     'entities' => trans('entities', [], 'en'),
98                     'errors' => trans('errors', [], 'en')
99                 ];
100                 $translations = array_replace_recursive($enTrans, $translations);
101             }
102             $resp = 'window.translations = ' . json_encode($translations);
103             cache()->put($cacheKey, $resp, 120);
104         }
105
106         return response($resp, 200, [
107             'Content-Type' => 'application/javascript'
108         ]);
109     }
110
111     /**
112      * Get custom head HTML, Used in ajax calls to show in editor.
113      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
114      */
115     public function customHeadContent()
116     {
117         return view('partials/custom-head-content');
118     }
119
120     /**
121      * Show the view for /robots.txt
122      * @return $this
123      */
124     public function getRobots()
125     {
126         $sitePublic = setting('app-public', false);
127         $allowRobots = config('app.allow_robots');
128         if ($allowRobots === null) {
129             $allowRobots = $sitePublic;
130         }
131         return response()
132             ->view('common/robots', ['allowRobots' => $allowRobots])
133             ->header('Content-Type', 'text/plain');
134     }
135
136     /**
137      * Show the route for 404 responses.
138      */
139     public function getNotFound()
140     {
141         return response()->view('errors/404', [], 404);
142     }
143 }