1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Entities\Repos\EntityRepo;
5 use Illuminate\Http\Response;
8 class HomeController extends Controller
10 protected $entityRepo;
13 * HomeController constructor.
14 * @param EntityRepo $entityRepo
16 public function __construct(EntityRepo $entityRepo)
18 $this->entityRepo = $entityRepo;
19 parent::__construct();
24 * Display the homepage.
27 public function index()
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);
35 $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
36 $homepageOption = setting('app-homepage-type', 'default');
37 if (!in_array($homepageOption, $homepageOptions)) {
38 $homepageOption = 'default';
42 'activity' => $activity,
43 'recents' => $recents,
44 'recentlyUpdatedPages' => $recentlyUpdatedPages,
45 'draftPages' => $draftPages,
48 if ($homepageOption === 'bookshelves') {
49 $shelves = $this->entityRepo->getAllPaginated('bookshelf', 18);
50 $shelvesViewType = setting()->getUser($this->currentUser, 'bookshelves_view_type', config('app.views.bookshelves', 'grid'));
51 $data = array_merge($commonData, ['shelves' => $shelves, 'shelvesViewType' => $shelvesViewType]);
52 return view('common.home-shelves', $data);
55 if ($homepageOption === 'books') {
56 $books = $this->entityRepo->getAllPaginated('book', 18);
57 $booksViewType = setting()->getUser($this->currentUser, 'books_view_type', config('app.views.books', 'list'));
58 $data = array_merge($commonData, ['books' => $books, 'booksViewType' => $booksViewType]);
59 return view('common.home-book', $data);
62 if ($homepageOption === 'page') {
63 $homepageSetting = setting('app-homepage', '0:');
64 $id = intval(explode(':', $homepageSetting)[0]);
65 $customHomepage = $this->entityRepo->getById('page', $id, false, true);
66 $this->entityRepo->renderPage($customHomepage, true);
67 return view('common.home-custom', array_merge($commonData, ['customHomepage' => $customHomepage]));
70 return view('common.home', $commonData);
74 * Get a js representation of the current translations
75 * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
78 public function getTranslations()
80 $locale = app()->getLocale();
81 $cacheKey = 'GLOBAL_TRANSLATIONS_' . $locale;
82 if (cache()->has($cacheKey) && config('app.env') !== 'development') {
83 $resp = cache($cacheKey);
86 // Get only translations which might be used in JS
87 'common' => trans('common'),
88 'components' => trans('components'),
89 'entities' => trans('entities'),
90 'errors' => trans('errors')
92 if ($locale !== 'en') {
94 'common' => trans('common', [], 'en'),
95 'components' => trans('components', [], 'en'),
96 'entities' => trans('entities', [], 'en'),
97 'errors' => trans('errors', [], 'en')
99 $translations = array_replace_recursive($enTrans, $translations);
101 $resp = 'window.translations = ' . json_encode($translations);
102 cache()->put($cacheKey, $resp, 120);
105 return response($resp, 200, [
106 'Content-Type' => 'application/javascript'
111 * Get custom head HTML, Used in ajax calls to show in editor.
112 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
114 public function customHeadContent()
116 return view('partials/custom-head-content');
120 * Show the view for /robots.txt
123 public function getRobots()
125 $sitePublic = setting('app-public', false);
126 $allowRobots = config('app.allow_robots');
127 if ($allowRobots === null) {
128 $allowRobots = $sitePublic;
131 ->view('common/robots', ['allowRobots' => $allowRobots])
132 ->header('Content-Type', 'text/plain');
136 * Show the route for 404 responses.
138 public function getNotFound()
140 return response()->view('errors/404', [], 404);