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;
38 $homepageSetting = setting('app-homepage');
39 if ($homepageSetting) {
40 $id = intval(explode(':', $homepageSetting)[0]);
41 $customHomepage = $this->entityRepo->getById('page', $id, false, true);
42 $this->entityRepo->renderPage($customHomepage, true);
45 $view = $customHomepage ? 'home-custom' : 'home';
47 'activity' => $activity,
48 'recents' => $recents,
49 'recentlyUpdatedPages' => $recentlyUpdatedPages,
50 'draftPages' => $draftPages,
51 'customHomepage' => $customHomepage
56 * Get a js representation of the current translations
57 * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
60 public function getTranslations()
62 $locale = app()->getLocale();
63 $cacheKey = 'GLOBAL_TRANSLATIONS_' . $locale;
64 if (cache()->has($cacheKey) && config('app.env') !== 'development') {
65 $resp = cache($cacheKey);
68 // Get only translations which might be used in JS
69 'common' => trans('common'),
70 'components' => trans('components'),
71 'entities' => trans('entities'),
72 'errors' => trans('errors')
74 if ($locale !== 'en') {
76 'common' => trans('common', [], 'en'),
77 'components' => trans('components', [], 'en'),
78 'entities' => trans('entities', [], 'en'),
79 'errors' => trans('errors', [], 'en')
81 $translations = array_replace_recursive($enTrans, $translations);
83 $resp = 'window.translations = ' . json_encode($translations);
84 cache()->put($cacheKey, $resp, 120);
87 return response($resp, 200, [
88 'Content-Type' => 'application/javascript'
93 * Get an icon via image request.
94 * Can provide a 'color' parameter with hex value to color the icon.
96 * @param Request $request
97 * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
99 public function getIcon($iconName, Request $request)
102 if ($request->filled('color')) {
103 $attrs['fill'] = '#' . $request->get('color');
106 $icon = icon($iconName, $attrs);
107 return response($icon, 200, [
108 'Content-Type' => 'image/svg+xml',
109 'Cache-Control' => 'max-age=3600',
114 * Get custom head HTML, Used in ajax calls to show in editor.
115 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
117 public function customHeadContent()
119 return view('partials/custom-head-content');