]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/HomeController.php
Enabled custom HTML head content to work within editors
[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\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         // Custom homepage
36         $customHomepage = false;
37         $homepageSetting = setting('app-homepage');
38         if ($homepageSetting) {
39             $id = intval(explode(':', $homepageSetting)[0]);
40             $customHomepage = $this->entityRepo->getById('page', $id, false, true);
41             $this->entityRepo->renderPage($customHomepage, true);
42         }
43
44         $view = $customHomepage ? 'home-custom' : 'home';
45         return view($view, [
46             'activity' => $activity,
47             'recents' => $recents,
48             'recentlyUpdatedPages' => $recentlyUpdatedPages,
49             'draftPages' => $draftPages,
50             'customHomepage' => $customHomepage
51         ]);
52     }
53
54     /**
55      * Get a js representation of the current translations
56      * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
57      * @throws \Exception
58      */
59     public function getTranslations() {
60         $locale = app()->getLocale();
61         $cacheKey = 'GLOBAL_TRANSLATIONS_' . $locale;
62         if (cache()->has($cacheKey) && config('app.env') !== 'development') {
63             $resp = cache($cacheKey);
64         } else {
65             $translations = [
66                 // Get only translations which might be used in JS
67                 'common' => trans('common'),
68                 'components' => trans('components'),
69                 'entities' => trans('entities'),
70                 'errors' => trans('errors')
71             ];
72             if ($locale !== 'en') {
73                 $enTrans = [
74                     'common' => trans('common', [], 'en'),
75                     'components' => trans('components', [], 'en'),
76                     'entities' => trans('entities', [], 'en'),
77                     'errors' => trans('errors', [], 'en')
78                 ];
79                 $translations = array_replace_recursive($enTrans, $translations);
80             }
81             $resp = 'window.translations = ' . json_encode($translations);
82             cache()->put($cacheKey, $resp, 120);
83         }
84
85         return response($resp, 200, [
86             'Content-Type' => 'application/javascript'
87         ]);
88     }
89
90     /**
91      * Get custom head HTML, Used in ajax calls to show in editor.
92      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
93      */
94     public function customHeadContent()
95     {
96         return view('partials/custom-head-content');
97     }
98
99 }