]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/HomeController.php
Merge pull request #1 from BookStackApp/master
[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      * Display the homepage.
24      * @return Response
25      */
26     public function index()
27     {
28         $activity = Activity::latest(10);
29         $draftPages = $this->signedIn ? $this->entityRepo->getUserDraftPages(6) : [];
30         $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
31         $recents = $this->signedIn ? Views::getUserRecentlyViewed(12*$recentFactor, 0) : $this->entityRepo->getRecentlyCreated('book', 12*$recentFactor);
32         $recentlyUpdatedPages = $this->entityRepo->getRecentlyUpdated('page', 12);
33
34         $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
35         $homepageOption = setting('app-homepage-type', 'default');
36         if (!in_array($homepageOption, $homepageOptions)) {
37             $homepageOption = 'default';
38         }
39
40         $commonData = [
41             'activity' => $activity,
42             'recents' => $recents,
43             'recentlyUpdatedPages' => $recentlyUpdatedPages,
44             'draftPages' => $draftPages,
45         ];
46
47         // Add required list ordering & sorting for books & shelves views.
48         if ($homepageOption === 'bookshelves' || $homepageOption === 'books') {
49             $key = $homepageOption;
50             $view = setting()->getUser($this->currentUser, $key . '_view_type', config('app.views.' . $key));
51             $sort = setting()->getUser($this->currentUser, $key . '_sort', 'name');
52             $order = setting()->getUser($this->currentUser, $key . '_sort_order', 'asc');
53
54             $sortOptions = [
55                 'name' => trans('common.sort_name'),
56                 'created_at' => trans('common.sort_created_at'),
57                 'updated_at' => trans('common.sort_updated_at'),
58             ];
59
60             $commonData = array_merge($commonData, [
61                 'view' => $view,
62                 'sort' => $sort,
63                 'order' => $order,
64                 'sortOptions' => $sortOptions,
65             ]);
66         }
67
68         if ($homepageOption === 'bookshelves') {
69             $shelves = $this->entityRepo->getAllPaginated('bookshelf', 18, $commonData['sort'], $commonData['order']);
70             $data = array_merge($commonData, ['shelves' => $shelves]);
71             return view('common.home-shelves', $data);
72         }
73
74         if ($homepageOption === 'books') {
75             $books = $this->entityRepo->getAllPaginated('book', 18, $commonData['sort'], $commonData['order']);
76             $data = array_merge($commonData, ['books' => $books]);
77             return view('common.home-book', $data);
78         }
79
80         if ($homepageOption === 'page') {
81             $homepageSetting = setting('app-homepage', '0:');
82             $id = intval(explode(':', $homepageSetting)[0]);
83             $customHomepage = $this->entityRepo->getById('page', $id, false, true);
84             $this->entityRepo->renderPage($customHomepage, true);
85             return view('common.home-custom', array_merge($commonData, ['customHomepage' => $customHomepage]));
86         }
87
88         return view('common.home', $commonData);
89     }
90
91     /**
92      * Get a js representation of the current translations
93      * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
94      * @throws \Exception
95      */
96     public function getTranslations()
97     {
98         $locale = app()->getLocale();
99         $cacheKey = 'GLOBAL_TRANSLATIONS_' . $locale;
100
101         if (cache()->has($cacheKey) && config('app.env') !== 'development') {
102             $resp = cache($cacheKey);
103         } else {
104             $translations = [
105                 // Get only translations which might be used in JS
106                 'common' => trans('common'),
107                 'components' => trans('components'),
108                 'entities' => trans('entities'),
109                 'errors' => trans('errors')
110             ];
111             $resp = 'window.translations = ' . json_encode($translations);
112             cache()->put($cacheKey, $resp, 120);
113         }
114
115         return response($resp, 200, [
116             'Content-Type' => 'application/javascript'
117         ]);
118     }
119
120     /**
121      * Get custom head HTML, Used in ajax calls to show in editor.
122      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
123      */
124     public function customHeadContent()
125     {
126         return view('partials.custom-head-content');
127     }
128
129     /**
130      * Show the view for /robots.txt
131      * @return $this
132      */
133     public function getRobots()
134     {
135         $sitePublic = setting('app-public', false);
136         $allowRobots = config('app.allow_robots');
137         if ($allowRobots === null) {
138             $allowRobots = $sitePublic;
139         }
140         return response()
141             ->view('common.robots', ['allowRobots' => $allowRobots])
142             ->header('Content-Type', 'text/plain');
143     }
144
145     /**
146      * Show the route for 404 responses.
147      */
148     public function getNotFound()
149     {
150         return response()->view('errors.404', [], 404);
151     }
152 }