1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Repos\EntityRepo;
5 use Illuminate\Http\Request;
6 use Illuminate\Http\Response;
9 class HomeController extends Controller
11 protected $entityRepo;
14 * HomeController constructor.
15 * @param EntityRepo $entityRepo
17 public function __construct(EntityRepo $entityRepo)
19 $this->entityRepo = $entityRepo;
20 parent::__construct();
25 * Display the homepage.
28 public function index()
30 $activity = Activity::latest(10);
31 $draftPages = $this->signedIn ? $this->entityRepo->getUserDraftPages(6) : [];
32 $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
33 $recents = $this->signedIn ? Views::getUserRecentlyViewed(12*$recentFactor, 0) : $this->entityRepo->getRecentlyCreated('book', 12*$recentFactor);
34 $recentlyUpdatedPages = $this->entityRepo->getRecentlyUpdated('page', 12);
36 $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
37 $homepageOption = setting('app-homepage-type', 'default');
38 if (!in_array($homepageOption, $homepageOptions)) {
39 $homepageOption = 'default';
43 'activity' => $activity,
44 'recents' => $recents,
45 'recentlyUpdatedPages' => $recentlyUpdatedPages,
46 'draftPages' => $draftPages,
49 if ($homepageOption === 'bookshelves') {
50 $shelves = $this->entityRepo->getAllPaginated('bookshelf', 18);
51 $shelvesViewType = setting()->getUser($this->currentUser, 'bookshelves_view_type', config('app.views.bookshelves', 'grid'));
52 $data = array_merge($commonData, ['shelves' => $shelves, 'shelvesViewType' => $shelvesViewType]);
53 return view('common.home-shelves', $data);
56 if ($homepageOption === 'books') {
57 $books = $this->entityRepo->getAllPaginated('book', 18);
58 $booksViewType = setting()->getUser($this->currentUser, 'books_view_type', config('app.views.books', 'list'));
59 $data = array_merge($commonData, ['books' => $books, 'booksViewType' => $booksViewType]);
60 return view('common.home-book', $data);
63 if ($homepageOption === 'page') {
64 $homepageSetting = setting('app-homepage', '0:');
65 $id = intval(explode(':', $homepageSetting)[0]);
66 $customHomepage = $this->entityRepo->getById('page', $id, false, true);
67 $this->entityRepo->renderPage($customHomepage, true);
68 return view('common.home-custom', array_merge($commonData, ['customHomepage' => $customHomepage]));
71 return view('common.home', $commonData);
75 * Get a js representation of the current translations
76 * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
79 public function getTranslations()
81 $locale = app()->getLocale();
82 $cacheKey = 'GLOBAL_TRANSLATIONS_' . $locale;
83 if (cache()->has($cacheKey) && config('app.env') !== 'development') {
84 $resp = cache($cacheKey);
87 // Get only translations which might be used in JS
88 'common' => trans('common'),
89 'components' => trans('components'),
90 'entities' => trans('entities'),
91 'errors' => trans('errors')
93 if ($locale !== 'en') {
95 'common' => trans('common', [], 'en'),
96 'components' => trans('components', [], 'en'),
97 'entities' => trans('entities', [], 'en'),
98 'errors' => trans('errors', [], 'en')
100 $translations = array_replace_recursive($enTrans, $translations);
102 $resp = 'window.translations = ' . json_encode($translations);
103 cache()->put($cacheKey, $resp, 120);
106 return response($resp, 200, [
107 'Content-Type' => 'application/javascript'
112 * Get custom head HTML, Used in ajax calls to show in editor.
113 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
115 public function customHeadContent()
117 return view('partials/custom-head-content');
121 * Show the view for /robots.txt
124 public function getRobots()
126 $sitePublic = setting('app-public', false);
127 $allowRobots = config('app.allow_robots');
128 if ($allowRobots === null) {
129 $allowRobots = $sitePublic;
132 ->view('common/robots', ['allowRobots' => $allowRobots])
133 ->header('Content-Type', 'text/plain');
137 * Show the route for 404 responses.
139 public function getNotFound()
141 return response()->view('errors/404', [], 404);