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