|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Marky |
|
5
|
|
|
* Date: 31/12/2017 |
|
6
|
|
|
* Time: 00:12. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Classes\Library\PageLoader; |
|
10
|
|
|
|
|
11
|
|
|
use App\Model\Page; |
|
12
|
|
|
use App\Model\Role; |
|
13
|
|
|
use Illuminate\Http\Response; |
|
14
|
|
|
use Illuminate\Support\Collection; |
|
15
|
|
|
use App\Http\Controllers\ErrorController; |
|
16
|
|
|
use App\Classes\Repositories\MenuRepository; |
|
17
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Frontpage |
|
21
|
|
|
* |
|
22
|
|
|
* @package App\Classes\Library\PageLoader |
|
23
|
|
|
*/ |
|
24
|
|
|
class Frontpage |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var Response |
|
28
|
|
|
*/ |
|
29
|
|
|
private $response; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Webpage |
|
33
|
|
|
*/ |
|
34
|
|
|
private $webpage; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var Page |
|
38
|
|
|
*/ |
|
39
|
|
|
private $model; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Frontpage constructor. |
|
43
|
|
|
* @param Page $model |
|
44
|
|
|
* @param Collection $navigationRepository |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(Page $model, Collection $navigationRepository) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->response = app(ResponseFactory::class); |
|
49
|
|
|
|
|
50
|
|
|
$this->webpage = new Webpage($this->model = $model, $navigationRepository); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string|null $template |
|
55
|
|
|
* @param bool $override |
|
56
|
|
|
* @param int $status |
|
57
|
|
|
* @param bool $errorResponse |
|
58
|
|
|
* @return Response |
|
59
|
|
|
*/ |
|
60
|
|
|
public function publish(string $template = null, bool $override = true, int $status = 200, bool $errorResponse = false) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
if (!$errorResponse) { |
|
63
|
|
|
if ($this->isMaintenanceMode() && ! $this->canBypassMaintenance()) { |
|
64
|
|
|
return ErrorController::maintenance(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if ($this->isDisabledPage($this->model)) { |
|
68
|
|
|
return ErrorController::disabled(); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $this->response->view($this->makeBlade($template, $override), ['webpage' => $this->webpage], $status); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* A user can disable a webpage from being viewed. |
|
77
|
|
|
* |
|
78
|
|
|
* @param Page $page |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
|
|
private function isDisabledPage(Page $page) |
|
82
|
|
|
{ |
|
83
|
|
|
return $page->enabled == false; |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Check if the website is in maintenance mode |
|
88
|
|
|
* which is set by the user on the dashboard. |
|
89
|
|
|
* |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
public function isMaintenanceMode() |
|
93
|
|
|
{ |
|
94
|
|
|
return settings()->getValue('maintenance_mode'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Check if the current logged in user if exists, |
|
99
|
|
|
* can bypass the maintenance and view it offline. |
|
100
|
|
|
* |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
|
|
public function canBypassMaintenance() |
|
104
|
|
|
{ |
|
105
|
|
|
return auth()->check() == true; |
|
|
|
|
|
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param string|null $template |
|
110
|
|
|
* @param bool $override |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
private function makeBlade(string $template = null, bool $override = true) |
|
114
|
|
|
{ |
|
115
|
|
|
if ($template == null) { |
|
|
|
|
|
|
116
|
|
|
$template = currentURI() == 'index' ? 'website::index' : 'website::page'; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
if ($override && view()->exists("website::plugin.{$this->model->slug}")) { |
|
|
|
|
|
|
120
|
|
|
$template = "website::plugin.{$this->model->slug}"; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $template; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param string $title |
|
128
|
|
|
* @param string $description |
|
129
|
|
|
* @param string $template |
|
130
|
|
|
* @param int $response |
|
131
|
|
|
* @return Response |
|
132
|
|
|
*/ |
|
133
|
|
|
public static function build(string $title, string $description, string $template, int $response) |
|
134
|
|
|
{ |
|
135
|
|
|
$page = new Page(['seo_title' => $title, 'seo_description' => $description]); |
|
136
|
|
|
|
|
137
|
|
|
$navigation = app(MenuRepository::class)->allParentsWithChildren(); |
|
138
|
|
|
|
|
139
|
|
|
return (new self($page, $navigation))->publish("errors::{$template}", false, $response, true); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Return a drafted page without response headers. |
|
144
|
|
|
* |
|
145
|
|
|
* @return Webpage |
|
146
|
|
|
*/ |
|
147
|
|
|
public function draft() |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->webpage; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.