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