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);
37 $customHomepage = false;
39 $booksViewType = false;
41 // Check book homepage
42 $bookHomepageSetting = setting('app-book-homepage');
43 if ($bookHomepageSetting) {
44 $books = $this->entityRepo->getAllPaginated('book', 18);
45 $booksViewType = setting()->getUser($this->currentUser, 'books_view_type', config('app.views.books', 'list'));
47 // Check custom homepage
48 $homepageSetting = setting('app-homepage');
49 if ($homepageSetting) {
50 $id = intval(explode(':', $homepageSetting)[0]);
51 $customHomepage = $this->entityRepo->getById('page', $id, false, true);
52 $this->entityRepo->renderPage($customHomepage, true);
57 if ($bookHomepageSetting) {
59 } else if ($customHomepage) {
60 $view = 'home-custom';
63 return view('common/' . $view, [
64 'activity' => $activity,
65 'recents' => $recents,
66 'recentlyUpdatedPages' => $recentlyUpdatedPages,
67 'draftPages' => $draftPages,
68 'customHomepage' => $customHomepage,
70 'booksViewType' => $booksViewType
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 an icon via image request.
113 * Can provide a 'color' parameter with hex value to color the icon.
115 * @param Request $request
116 * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
118 public function getIcon($iconName, Request $request)
121 if ($request->filled('color')) {
122 $attrs['fill'] = '#' . $request->get('color');
125 $icon = icon($iconName, $attrs);
126 return response($icon, 200, [
127 'Content-Type' => 'image/svg+xml',
128 'Cache-Control' => 'max-age=3600',
133 * Get custom head HTML, Used in ajax calls to show in editor.
134 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
136 public function customHeadContent()
138 return view('partials/custom-head-content');
142 * Show the view for /robots.txt
145 public function getRobots()
147 $sitePublic = setting('app-public', false);
148 $allowRobots = config('app.allow_robots');
149 if ($allowRobots === null) {
150 $allowRobots = $sitePublic;
153 ->view('common/robots', ['allowRobots' => $allowRobots])
154 ->header('Content-Type', 'text/plain');
158 * Show the route for 404 responses.
160 public function getNotFound()
162 return response()->view('errors/404', [], 404);