]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/HomeController.php
Added default favicon creation upon access.
[bookstack] / app / Http / Controllers / HomeController.php
1 <?php
2
3 namespace BookStack\Http\Controllers;
4
5 use BookStack\Actions\ActivityQueries;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Entities\Queries\RecentlyViewed;
9 use BookStack\Entities\Queries\TopFavourites;
10 use BookStack\Entities\Repos\BookRepo;
11 use BookStack\Entities\Repos\BookshelfRepo;
12 use BookStack\Entities\Tools\PageContent;
13 use BookStack\Uploads\FaviconHandler;
14 use BookStack\Util\SimpleListOptions;
15 use Illuminate\Http\Request;
16
17 class HomeController extends Controller
18 {
19     /**
20      * Display the homepage.
21      */
22     public function index(Request $request, ActivityQueries $activities)
23     {
24         $activity = $activities->latest(10);
25         $draftPages = [];
26
27         if ($this->isSignedIn()) {
28             $draftPages = Page::visible()
29                 ->where('draft', '=', true)
30                 ->where('created_by', '=', user()->id)
31                 ->orderBy('updated_at', 'desc')
32                 ->with('book')
33                 ->take(6)
34                 ->get();
35         }
36
37         $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
38         $recents = $this->isSignedIn() ?
39             (new RecentlyViewed())->run(12 * $recentFactor, 1)
40             : Book::visible()->orderBy('created_at', 'desc')->take(12 * $recentFactor)->get();
41         $favourites = (new TopFavourites())->run(6);
42         $recentlyUpdatedPages = Page::visible()->with('book')
43             ->where('draft', false)
44             ->orderBy('updated_at', 'desc')
45             ->take($favourites->count() > 0 ? 5 : 10)
46             ->select(Page::$listAttributes)
47             ->get();
48
49         $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
50         $homepageOption = setting('app-homepage-type', 'default');
51         if (!in_array($homepageOption, $homepageOptions)) {
52             $homepageOption = 'default';
53         }
54
55         $commonData = [
56             'activity'             => $activity,
57             'recents'              => $recents,
58             'recentlyUpdatedPages' => $recentlyUpdatedPages,
59             'draftPages'           => $draftPages,
60             'favourites'           => $favourites,
61         ];
62
63         // Add required list ordering & sorting for books & shelves views.
64         if ($homepageOption === 'bookshelves' || $homepageOption === 'books') {
65             $key = $homepageOption;
66             $view = setting()->getForCurrentUser($key . '_view_type');
67             $listOptions = SimpleListOptions::fromRequest($request, $key)->withSortOptions([
68                 'name' => trans('common.sort_name'),
69                 'created_at' => trans('common.sort_created_at'),
70                 'updated_at' => trans('common.sort_updated_at'),
71             ]);
72
73             $commonData = array_merge($commonData, [
74                 'view'        => $view,
75                 'listOptions' => $listOptions,
76             ]);
77         }
78
79         if ($homepageOption === 'bookshelves') {
80             $shelves = app(BookshelfRepo::class)->getAllPaginated(18, $commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder());
81             $data = array_merge($commonData, ['shelves' => $shelves]);
82
83             return view('home.shelves', $data);
84         }
85
86         if ($homepageOption === 'books') {
87             $books = app(BookRepo::class)->getAllPaginated(18, $commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder());
88             $data = array_merge($commonData, ['books' => $books]);
89
90             return view('home.books', $data);
91         }
92
93         if ($homepageOption === 'page') {
94             $homepageSetting = setting('app-homepage', '0:');
95             $id = intval(explode(':', $homepageSetting)[0]);
96             /** @var Page $customHomepage */
97             $customHomepage = Page::query()->where('draft', '=', false)->findOrFail($id);
98             $pageContent = new PageContent($customHomepage);
99             $customHomepage->html = $pageContent->render(false);
100
101             return view('home.specific-page', array_merge($commonData, ['customHomepage' => $customHomepage]));
102         }
103
104         return view('home.default', $commonData);
105     }
106
107     /**
108      * Show the view for /robots.txt.
109      */
110     public function robots()
111     {
112         $sitePublic = setting('app-public', false);
113         $allowRobots = config('app.allow_robots');
114
115         if ($allowRobots === null) {
116             $allowRobots = $sitePublic;
117         }
118
119         return response()
120             ->view('misc.robots', ['allowRobots' => $allowRobots])
121             ->header('Content-Type', 'text/plain');
122     }
123
124     /**
125      * Show the route for 404 responses.
126      */
127     public function notFound()
128     {
129         return response()->view('errors.404', [], 404);
130     }
131
132     /**
133      * Serve the application favicon.
134      * Ensures a 'favicon.ico' file exists at the web root location (if writable) to be served
135      * directly by the webserver in the future.
136      */
137     public function favicon(FaviconHandler $favicons)
138     {
139         $favicons->restoreOriginalIfNotExists();
140         return response()->file($favicons->getPath());
141     }
142 }