|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\Content; |
|
6
|
|
|
use App\Models\User; |
|
7
|
|
|
use Illuminate\Http\Request; |
|
8
|
|
|
|
|
9
|
|
|
class ContentController extends BasePageController |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Display content page(s). |
|
13
|
|
|
* |
|
14
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View |
|
15
|
|
|
* @throws \Exception |
|
16
|
|
|
*/ |
|
17
|
|
|
public function show(Request $request) |
|
18
|
|
|
{ |
|
19
|
|
|
$role = $this->userdata->role ?? 0; |
|
20
|
|
|
|
|
21
|
|
|
/* The role column in the content table values are: |
|
22
|
|
|
* 1 = logged in users |
|
23
|
|
|
* 2 = admins |
|
24
|
|
|
* |
|
25
|
|
|
* The user role values are: |
|
26
|
|
|
* 1 = user |
|
27
|
|
|
* 2 = admin |
|
28
|
|
|
* 3 = disabled |
|
29
|
|
|
* 4 = moderator |
|
30
|
|
|
* |
|
31
|
|
|
* Admins and mods should be the only ones to see admin content. |
|
32
|
|
|
*/ |
|
33
|
|
|
$isAdmin = \in_array($role, [User::ROLE_ADMIN, User::ROLE_MODERATOR], true); |
|
34
|
|
|
|
|
35
|
|
|
$contentId = $request->input('id', 0); |
|
36
|
|
|
$contentPage = $request->input('page', false); |
|
37
|
|
|
|
|
38
|
|
|
if ($contentId === 0 && $contentPage === 'content') { |
|
39
|
|
|
// Show all content except front page |
|
40
|
|
|
$content = $this->getAllButFront()->all(); |
|
41
|
|
|
$isFront = false; |
|
42
|
|
|
$meta_title = 'Contents page'; |
|
43
|
|
|
$meta_keywords = 'contents'; |
|
44
|
|
|
$meta_description = 'This is the contents page.'; |
|
45
|
|
|
} elseif ($contentId !== 0 && $contentPage !== false) { |
|
46
|
|
|
// Show specific content by ID |
|
47
|
|
|
$contentItem = $this->getContentById($contentId, $role); |
|
|
|
|
|
|
48
|
|
|
$content = $contentItem ? [$contentItem] : []; |
|
49
|
|
|
$isFront = false; |
|
50
|
|
|
$meta_title = 'Contents page'; |
|
51
|
|
|
$meta_keywords = 'contents'; |
|
52
|
|
|
$meta_description = 'This is the contents page.'; |
|
53
|
|
|
} else { |
|
54
|
|
|
// Show front page content |
|
55
|
|
|
$content = $this->getFrontPageContent()->all(); |
|
56
|
|
|
$index = $this->getIndexContent(); |
|
57
|
|
|
$isFront = true; |
|
58
|
|
|
$meta_title = $index?->title ?? 'Contents page'; |
|
59
|
|
|
$meta_keywords = $index?->metakeywords ?? 'contents'; |
|
60
|
|
|
$meta_description = $index?->metadescription ?? 'This is the contents page.'; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (empty($content)) { |
|
64
|
|
|
return response()->json(['message' => 'There is nothing to see here, no content provided.'], 404); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->viewData = array_merge($this->viewData, [ |
|
68
|
|
|
'content' => $content, |
|
69
|
|
|
'admin' => $isAdmin, |
|
70
|
|
|
'front' => $isFront, |
|
71
|
|
|
'meta_title' => $meta_title, |
|
72
|
|
|
'meta_keywords' => $meta_keywords, |
|
73
|
|
|
'meta_description' => $meta_description, |
|
74
|
|
|
]); |
|
75
|
|
|
|
|
76
|
|
|
return view('content.index', $this->viewData); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Get all active content ordered by type and ordinal. |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function getActiveContent(): \Illuminate\Database\Eloquent\Collection |
|
83
|
|
|
{ |
|
84
|
|
|
return Content::active() |
|
85
|
|
|
->orderByRaw('contenttype, COALESCE(ordinal, 1000000)') |
|
86
|
|
|
->get(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get all content except the front page. |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function getAllButFront(): \Illuminate\Database\Eloquent\Collection |
|
93
|
|
|
{ |
|
94
|
|
|
return Content::query() |
|
95
|
|
|
->where('id', '<>', 1) |
|
96
|
|
|
->orderByRaw('contenttype, COALESCE(ordinal, 1000000)') |
|
97
|
|
|
->get(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get content by ID with role-based access control. |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function getContentById(int $id, int $role): ?Content |
|
104
|
|
|
{ |
|
105
|
|
|
return Content::query() |
|
106
|
|
|
->where('id', $id) |
|
107
|
|
|
->forRole($role) |
|
108
|
|
|
->first(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get front page content. |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function getFrontPageContent(): \Illuminate\Database\Eloquent\Collection |
|
115
|
|
|
{ |
|
116
|
|
|
return Content::frontPage()->get(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Get index content metadata. |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function getIndexContent(): ?Content |
|
123
|
|
|
{ |
|
124
|
|
|
return Content::active()->ofType(Content::TYPE_INDEX)->first(); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|