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