]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/HomeController.php
Fixed issue causing text overlap in sort select box
[bookstack] / app / Http / Controllers / HomeController.php
1 <?php namespace BookStack\Http\Controllers;
2
3 use Activity;
4 use BookStack\Entities\Repos\EntityRepo;
5 use Illuminate\Http\Response;
6 use Views;
7
8 class HomeController extends Controller
9 {
10     protected $entityRepo;
11
12     /**
13      * HomeController constructor.
14      * @param EntityRepo $entityRepo
15      */
16     public function __construct(EntityRepo $entityRepo)
17     {
18         $this->entityRepo = $entityRepo;
19         parent::__construct();
20     }
21
22     /**
23      * Display the homepage.
24      * @return Response
25      */
26     public function index()
27     {
28         $activity = Activity::latest(10);
29         $draftPages = $this->signedIn ? $this->entityRepo->getUserDraftPages(6) : [];
30         $recentFactor = count($draftPages) > 0 ? 0.5 : 1;
31         $recents = $this->signedIn ? Views::getUserRecentlyViewed(12*$recentFactor, 0) : $this->entityRepo->getRecentlyCreated('book', 12*$recentFactor);
32         $recentlyUpdatedPages = $this->entityRepo->getRecentlyUpdated('page', 12);
33
34         $homepageOptions = ['default', 'books', 'bookshelves', 'page'];
35         $homepageOption = setting('app-homepage-type', 'default');
36         if (!in_array($homepageOption, $homepageOptions)) {
37             $homepageOption = 'default';
38         }
39
40         $commonData = [
41             'activity' => $activity,
42             'recents' => $recents,
43             'recentlyUpdatedPages' => $recentlyUpdatedPages,
44             'draftPages' => $draftPages,
45         ];
46
47         // Add required list ordering & sorting for books & shelves views.
48         if ($homepageOption === 'bookshelves' || $homepageOption === 'books') {
49             $key = $homepageOption;
50             $view = setting()->getUser($this->currentUser, $key . '_view_type', config('app.views.' . $key));
51             $sort = setting()->getUser($this->currentUser, $key . '_sort', 'name');
52             $order = setting()->getUser($this->currentUser, $key . '_sort_order', 'asc');
53
54             $sortOptions = [
55                 'name' => trans('common.sort_name'),
56                 'created_at' => trans('common.sort_created_at'),
57                 'updated_at' => trans('common.sort_updated_at'),
58             ];
59
60             $commonData = array_merge($commonData, [
61                 'view' => $view,
62                 'sort' => $sort,
63                 'order' => $order,
64                 'sortOptions' => $sortOptions,
65             ]);
66         }
67
68         if ($homepageOption === 'bookshelves') {
69             $shelves = $this->entityRepo->getAllPaginated('bookshelf', 18, $commonData['sort'], $commonData['order']);
70             foreach ($shelves as $shelf) {
71                 $shelf->books = $this->entityRepo->getBookshelfChildren($shelf);
72             }
73             $data = array_merge($commonData, ['shelves' => $shelves]);
74             return view('common.home-shelves', $data);
75         }
76
77         if ($homepageOption === 'books') {
78             $books = $this->entityRepo->getAllPaginated('book', 18, $commonData['sort'], $commonData['order']);
79             $data = array_merge($commonData, ['books' => $books]);
80             return view('common.home-book', $data);
81         }
82
83         if ($homepageOption === 'page') {
84             $homepageSetting = setting('app-homepage', '0:');
85             $id = intval(explode(':', $homepageSetting)[0]);
86             $customHomepage = $this->entityRepo->getById('page', $id, false, true);
87             $this->entityRepo->renderPage($customHomepage, true);
88             return view('common.home-custom', array_merge($commonData, ['customHomepage' => $customHomepage]));
89         }
90
91         return view('common.home', $commonData);
92     }
93
94     /**
95      * Get custom head HTML, Used in ajax calls to show in editor.
96      * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
97      */
98     public function customHeadContent()
99     {
100         return view('partials.custom-head-content');
101     }
102
103     /**
104      * Show the view for /robots.txt
105      * @return $this
106      */
107     public function getRobots()
108     {
109         $sitePublic = setting('app-public', false);
110         $allowRobots = config('app.allow_robots');
111         if ($allowRobots === null) {
112             $allowRobots = $sitePublic;
113         }
114         return response()
115             ->view('common.robots', ['allowRobots' => $allowRobots])
116             ->header('Content-Type', 'text/plain');
117     }
118
119     /**
120      * Show the route for 404 responses.
121      */
122     public function getNotFound()
123     {
124         return response()->view('errors.404', [], 404);
125     }
126 }