1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chuckbe\Chuckcms\Controllers; |
4
|
|
|
|
5
|
|
|
use Chuckbe\Chuckcms\Chuck\PageBlockRepository; |
6
|
|
|
use Chuckbe\Chuckcms\Models\Page; |
7
|
|
|
use Chuckbe\Chuckcms\Models\PageBlock; |
8
|
|
|
use Chuckbe\Chuckcms\Models\Redirect; |
9
|
|
|
use Chuckbe\Chuckcms\Models\Repeater; |
10
|
|
|
use Chuckbe\Chuckcms\Models\Template; |
11
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
12
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
13
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests; |
14
|
|
|
use Illuminate\Routing\Controller as BaseController; |
15
|
|
|
use Illuminate\Support\Facades\Auth; |
16
|
|
|
use Spatie\Permission\Exceptions\UnauthorizedException; |
17
|
|
|
use Spatie\Permission\Models\Role; |
18
|
|
|
|
19
|
|
|
class FrontEndController extends BaseController |
20
|
|
|
{ |
21
|
|
|
use AuthorizesRequests; |
22
|
|
|
use DispatchesJobs; |
23
|
|
|
use ValidatesRequests; |
24
|
|
|
|
25
|
|
|
private $page; |
26
|
|
|
private $pageblock; |
27
|
|
|
private $pageBlockRepository; |
28
|
|
|
private $redirect; |
29
|
|
|
private $repeater; |
30
|
|
|
private $role; |
|
|
|
|
31
|
|
|
private $template; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Create a new controller instance. |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
public function __construct( |
39
|
|
|
Page $page, |
40
|
|
|
PageBlock $pageblock, |
41
|
|
|
PageBlockRepository $pageBlockRepository, |
42
|
|
|
Redirect $redirect, |
43
|
|
|
Repeater $repeater, |
44
|
|
|
Role $role, |
|
|
|
|
45
|
|
|
Template $template |
46
|
|
|
) { |
47
|
|
|
$this->page = $page; |
48
|
|
|
$this->pageblock = $pageblock; |
49
|
|
|
$this->pageBlockRepository = $pageBlockRepository; |
50
|
|
|
$this->redirect = $redirect; |
51
|
|
|
$this->repeater = $repeater; |
52
|
|
|
$this->template = $template; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function index($slug = null) |
56
|
|
|
{ |
57
|
|
|
if ($slug == null) { |
58
|
|
|
$page = $this->page->where('isHp', 1)->firstOrFail(); |
59
|
|
|
} elseif ($slug !== null) { |
60
|
|
|
$redirect = $this->redirect->where('slug', $slug)->first(); |
61
|
|
|
if ($redirect !== null) { |
62
|
|
|
return redirect($redirect->to, $redirect->type); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$repeater = $this->repeater->where('url', $slug)->first(); |
66
|
|
|
if ($repeater !== null) { |
67
|
|
|
$templateHintpath = explode('::', (string) $repeater->page)[0]; |
68
|
|
|
$template = $this->template->where('active', 1)->where('hintpath', $templateHintpath)->first(); |
69
|
|
|
|
70
|
|
|
return view((string) $repeater->page, compact('template', 'repeater')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$page = $this->page->where('slug->'.app()->getLocale(), $slug)->first(); |
|
|
|
|
74
|
|
|
if ($page == null) { |
75
|
|
|
foreach (\LaravelLocalization::getSupportedLocales() as $localeCode => $properties) { |
76
|
|
|
$page = $this->page->where('slug->'.$localeCode, $slug)->first(); |
77
|
|
|
if ($page !== null && $localeCode == app()->getLocale()) { |
78
|
|
|
break; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($page !== null && $localeCode !== app()->getLocale()) { |
82
|
|
|
//dd(app()->getLocale()); |
83
|
|
|
app()->setLocale($localeCode); |
|
|
|
|
84
|
|
|
\LaravelLocalization::setLocale($localeCode); |
|
|
|
|
85
|
|
|
|
86
|
|
|
return redirect($localeCode.'/'.$slug); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($page == null) { |
92
|
|
|
abort(404); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($page->roles !== null) { |
|
|
|
|
97
|
|
|
return $this->authorizedIndex($page); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->getView($page); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function concept($slug = null) |
104
|
|
|
{ |
105
|
|
|
if ($slug == null) { |
106
|
|
|
return redirect('/'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$page = $this->page->where('slug->'.app()->getLocale(), $slug)->first(); |
110
|
|
|
if ($page == null) { |
111
|
|
|
foreach (\LaravelLocalization::getSupportedLocales() as $localeCode => $properties) { |
112
|
|
|
$page = $this->page->where('slug->'.$localeCode, $slug)->first(); |
113
|
|
|
if ($page !== null && $localeCode == app()->getLocale()) { |
114
|
|
|
break; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ($page !== null && $localeCode !== app()->getLocale()) { |
118
|
|
|
//dd(app()->getLocale()); |
119
|
|
|
app()->setLocale($localeCode); |
120
|
|
|
\LaravelLocalization::setLocale($localeCode); |
121
|
|
|
|
122
|
|
|
return redirect($localeCode.'/concept/'.$slug); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ($page == null) { |
128
|
|
|
abort(404); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if ($page->roles !== null) { |
132
|
|
|
return $this->authorizedIndex($page); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $this->getView($page); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function authorizedIndex(Page $page) |
139
|
|
|
{ |
140
|
|
|
if (!Auth::check()) { |
141
|
|
|
return redirect()->guest('login'); |
142
|
|
|
} |
143
|
|
|
$roles = Role::whereIn('id', explode('|', $page->roles))->select('name')->get()->toArray(); |
144
|
|
|
if (!Auth::user()->hasAnyRole($roles)) { |
|
|
|
|
145
|
|
|
throw UnauthorizedException::forRoles($roles); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $this->getView($page); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function getView(Page $page) |
152
|
|
|
{ |
153
|
|
|
$ogpageblocks = $this->pageblock->getAllByPageId($page->id); |
154
|
|
|
$pageblocks = $this->pageBlockRepository->getRenderedByPageBlocks($ogpageblocks); |
155
|
|
|
|
156
|
|
|
if ($page->page !== null) { |
157
|
|
|
$template = $this->template->where('active', 1)->where('hintpath', explode('::', $page->page)[0])->first(); |
158
|
|
|
|
159
|
|
|
return view($page->page, compact('template', 'page', 'pageblocks')); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$template = $this->template->where('active', 1)->where('id', $page->template_id)->first(); |
163
|
|
|
|
164
|
|
|
return view($template->hintpath.'::templates.'.$template->slug.'.page', compact('template', 'page', 'pageblocks')); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function css(Page $page) |
168
|
|
|
{ |
169
|
|
|
$response = response()->make($page->css); |
170
|
|
|
$response->header('Content-Type', 'text/css'); |
171
|
|
|
|
172
|
|
|
return $response; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function js(Page $page) |
176
|
|
|
{ |
177
|
|
|
$response = response()->make($page->js); |
178
|
|
|
$response->header('Content-Type', 'text/javascript'); |
179
|
|
|
|
180
|
|
|
return $response; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|