]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/HomeController.php
Merge pull request #234 from BookStackApp/translations
[bookstack] / app / Http / Controllers / HomeController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use Activity;
6 use BookStack\Repos\EntityRepo;
7 use BookStack\Http\Requests;
8 use Views;
9
10 class HomeController extends Controller
11 {
12     protected $entityRepo;
13
14     /**
15      * HomeController constructor.
16      * @param EntityRepo $entityRepo
17      */
18     public function __construct(EntityRepo $entityRepo)
19     {
20         $this->entityRepo = $entityRepo;
21         parent::__construct();
22     }
23
24
25     /**
26      * Display the homepage.
27      * @return Response
28      */
29     public function index()
30     {
31         $activity = Activity::latest(10);
32         $draftPages = $this->signedIn ? $this->entityRepo->getUserDraftPages(6) : [];
33         $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
34         $recents = $this->signedIn ? Views::getUserRecentlyViewed(12*$recentFactor, 0) : $this->entityRepo->getRecentlyCreatedBooks(10*$recentFactor);
35         $recentlyCreatedPages = $this->entityRepo->getRecentlyCreatedPages(5);
36         $recentlyUpdatedPages = $this->entityRepo->getRecentlyUpdatedPages(5);
37         return view('home', [
38             'activity' => $activity,
39             'recents' => $recents,
40             'recentlyCreatedPages' => $recentlyCreatedPages,
41             'recentlyUpdatedPages' => $recentlyUpdatedPages,
42             'draftPages' => $draftPages
43         ]);
44     }
45
46     /**
47      * Get a js representation of the current translations
48      * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
49      */
50     public function getTranslations() {
51         $locale = trans()->getLocale();
52         $cacheKey = 'GLOBAL_TRANSLATIONS_' . $locale;
53         if (cache()->has($cacheKey) && config('app.env') !== 'development') {
54             $resp = cache($cacheKey);
55         } else {
56             $translations = [
57                 // Get only translations which might be used in JS
58                 'common' => trans('common'),
59                 'components' => trans('components'),
60                 'entities' => trans('entities'),
61                 'errors' => trans('errors')
62             ];
63             if ($locale !== 'en') {
64                 $enTrans = [
65                     'common' => trans('common', [], null, 'en'),
66                     'components' => trans('components', [], null, 'en'),
67                     'entities' => trans('entities', [], null, 'en'),
68                     'errors' => trans('errors', [], null, 'en')
69                 ];
70                 $translations = array_replace_recursive($enTrans, $translations);
71             }
72             $resp = 'window.translations = ' . json_encode($translations);
73             cache()->put($cacheKey, $resp, 120);
74         }
75
76         return response($resp, 200, [
77             'Content-Type' => 'application/javascript'
78         ]);
79     }
80
81 }