1 <?php namespace BookStack\Http\Controllers;
4 use BookStack\Entities\Models\Book;
5 use BookStack\Entities\Tools\PageContent;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Entities\Repos\BookRepo;
8 use BookStack\Entities\Repos\BookshelfRepo;
9 use Illuminate\Http\Response;
12 class HomeController extends Controller
16 * Display the homepage.
18 public function index()
20 $activity = Activity::latest(10);
23 if ($this->isSignedIn()) {
24 $draftPages = Page::visible()
25 ->where('draft', '=', true)
26 ->where('created_by', '=', user()->id)
27 ->orderBy('updated_at', 'desc')
33 $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
34 $recents = $this->isSignedIn() ?
35 Views::getUserRecentlyViewed(12*$recentFactor, 1)
36 : Book::visible()->orderBy('created_at', 'desc')->take(12 * $recentFactor)->get();
37 $recentlyUpdatedPages = Page::visible()->with('book')
38 ->where('draft', false)
39 ->orderBy('updated_at', 'desc')
43 $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
44 $homepageOption = setting('app-homepage-type', 'default');
45 if (!in_array($homepageOption, $homepageOptions)) {
46 $homepageOption = 'default';
50 'activity' => $activity,
51 'recents' => $recents,
52 'recentlyUpdatedPages' => $recentlyUpdatedPages,
53 'draftPages' => $draftPages,
56 // Add required list ordering & sorting for books & shelves views.
57 if ($homepageOption === 'bookshelves' || $homepageOption === 'books') {
58 $key = $homepageOption;
59 $view = setting()->getForCurrentUser($key . '_view_type', config('app.views.' . $key));
60 $sort = setting()->getForCurrentUser($key . '_sort', 'name');
61 $order = setting()->getForCurrentUser($key . '_sort_order', 'asc');
64 'name' => trans('common.sort_name'),
65 'created_at' => trans('common.sort_created_at'),
66 'updated_at' => trans('common.sort_updated_at'),
69 $commonData = array_merge($commonData, [
73 'sortOptions' => $sortOptions,
77 if ($homepageOption === 'bookshelves') {
78 $shelves = app(BookshelfRepo::class)->getAllPaginated(18, $commonData['sort'], $commonData['order']);
79 $data = array_merge($commonData, ['shelves' => $shelves]);
80 return view('common.home-shelves', $data);
83 if ($homepageOption === 'books') {
84 $bookRepo = app(BookRepo::class);
85 $books = $bookRepo->getAllPaginated(18, $commonData['sort'], $commonData['order']);
86 $data = array_merge($commonData, ['books' => $books]);
87 return view('common.home-book', $data);
90 if ($homepageOption === 'page') {
91 $homepageSetting = setting('app-homepage', '0:');
92 $id = intval(explode(':', $homepageSetting)[0]);
93 $customHomepage = Page::query()->where('draft', '=', false)->findOrFail($id);
94 $pageContent = new PageContent($customHomepage);
95 $customHomepage->html = $pageContent->render(true);
96 return view('common.home-custom', array_merge($commonData, ['customHomepage' => $customHomepage]));
99 return view('common.home', $commonData);
103 * Get custom head HTML, Used in ajax calls to show in editor.
104 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
106 public function customHeadContent()
108 return view('partials.custom-head-content');
112 * Show the view for /robots.txt
115 public function getRobots()
117 $sitePublic = setting('app-public', false);
118 $allowRobots = config('app.allow_robots');
119 if ($allowRobots === null) {
120 $allowRobots = $sitePublic;
123 ->view('common.robots', ['allowRobots' => $allowRobots])
124 ->header('Content-Type', 'text/plain');
128 * Show the route for 404 responses.
130 public function getNotFound()
132 return response()->view('errors.404', [], 404);