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