]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/HomeController.php
Merge pull request #334 from diegoseso/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\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', 10*$recentFactor);
33         $recentlyCreatedPages = $this->entityRepo->getRecentlyCreated('page', 5);
34         $recentlyUpdatedPages = $this->entityRepo->getRecentlyUpdated('page', 5);
35         return view('home', [
36             'activity' => $activity,
37             'recents' => $recents,
38             'recentlyCreatedPages' => $recentlyCreatedPages,
39             'recentlyUpdatedPages' => $recentlyUpdatedPages,
40             'draftPages' => $draftPages
41         ]);
42     }
43
44     /**
45      * Get a js representation of the current translations
46      * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
47      */
48     public function getTranslations() {
49         $locale = trans()->getLocale();
50         $cacheKey = 'GLOBAL_TRANSLATIONS_' . $locale;
51         if (cache()->has($cacheKey) && config('app.env') !== 'development') {
52             $resp = cache($cacheKey);
53         } else {
54             $translations = [
55                 // Get only translations which might be used in JS
56                 'common' => trans('common'),
57                 'components' => trans('components'),
58                 'entities' => trans('entities'),
59                 'errors' => trans('errors')
60             ];
61             if ($locale !== 'en') {
62                 $enTrans = [
63                     'common' => trans('common', [], 'en'),
64                     'components' => trans('components', [], 'en'),
65                     'entities' => trans('entities', [], 'en'),
66                     'errors' => trans('errors', [], 'en')
67                 ];
68                 $translations = array_replace_recursive($enTrans, $translations);
69             }
70             $resp = 'window.translations = ' . json_encode($translations);
71             cache()->put($cacheKey, $resp, 120);
72         }
73
74         return response($resp, 200, [
75             'Content-Type' => 'application/javascript'
76         ]);
77     }
78
79 }