-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPages.php
More file actions
309 lines (270 loc) · 8.94 KB
/
Pages.php
File metadata and controls
309 lines (270 loc) · 8.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
namespace Neuron\Cms\Controllers\Admin;
use Neuron\Cms\Auth\SessionManager;
use Neuron\Cms\Controllers\Content;
use Neuron\Cms\Enums\FlashMessageType;
use Neuron\Cms\Models\Page;
use Neuron\Cms\Repositories\IPageRepository;
use Neuron\Cms\Services\Page\IPageCreator;
use Neuron\Cms\Services\Page\IPageUpdater;
use Neuron\Cms\Services\Auth\CsrfToken;
use Neuron\Data\Settings\SettingManager;
use Neuron\Mvc\IMvcApplication;
use Neuron\Mvc\Requests\Request;
use Neuron\Mvc\Responses\HttpResponseStatus;
use Neuron\Log\Log;
use Neuron\Cms\Enums\ContentStatus;
use Neuron\Cms\Enums\PageTemplate;
use Neuron\Routing\Attributes\Get;
use Neuron\Routing\Attributes\Post;
use Neuron\Routing\Attributes\Put;
use Neuron\Routing\Attributes\Delete;
use Neuron\Routing\Attributes\RouteGroup;
/**
* Admin page management controller.
*
* @package Neuron\Cms\Controllers\Admin
*/
#[RouteGroup(prefix: '/admin', filters: ['auth'])]
class Pages extends Content
{
private IPageRepository $_pageRepository;
private IPageCreator $_pageCreator;
private IPageUpdater $_pageUpdater;
/**
* @param IMvcApplication $app
* @param SettingManager $settings
* @param SessionManager $sessionManager
* @param IPageRepository $pageRepository
* @param IPageCreator $pageCreator
* @param IPageUpdater $pageUpdater
*/
public function __construct(
IMvcApplication $app,
SettingManager $settings,
SessionManager $sessionManager,
IPageRepository $pageRepository,
IPageCreator $pageCreator,
IPageUpdater $pageUpdater
)
{
parent::__construct( $app, $settings, $sessionManager );
$this->_pageRepository = $pageRepository;
$this->_pageCreator = $pageCreator;
$this->_pageUpdater = $pageUpdater;
}
/**
* List all pages
* @param Request $request
* @return string
* @throws \Exception
*/
#[Get('/pages', name: 'admin_pages')]
public function index( Request $request ): string
{
$this->initializeCsrfToken();
// Get all pages or filter by author if not admin
$sessionManager = $this->getSessionManager();
if( is_admin() || is_editor() )
{
$pages = $this->_pageRepository->all();
}
else
{
$pages = $this->_pageRepository->getByAuthor( user_id() );
}
return $this->view()
->title( 'Pages' )
->description( 'Manage pages' )
->withCurrentUser()
->withCsrfToken()
->with([
'pages' => $pages,
FlashMessageType::SUCCESS->viewKey() => $sessionManager->getFlash( FlashMessageType::SUCCESS->value ),
FlashMessageType::ERROR->viewKey() => $sessionManager->getFlash( FlashMessageType::ERROR->value )
])
->render( 'index', 'admin' );
}
/**
* Show create page form
* @param Request $request
* @return string
* @throws \Exception
*/
#[Get('/pages/create', name: 'admin_pages_create')]
public function create( Request $request ): string
{
$this->initializeCsrfToken();
return $this->view()
->title( 'Create Page' )
->description( 'Create a new page' )
->withCurrentUser()
->withCsrfToken()
->render( 'create', 'admin' );
}
/**
* Store new page
* @param Request $request
* @return never
* @throws \Exception
*/
#[Post('/pages', name: 'admin_pages_store', filters: ['csrf'])]
public function store( Request $request ): never
{
// Create DTO from YAML configuration
$dto = $this->createDto( 'pages/create-page-request.yaml' );
// Map request data to DTO
$this->mapRequestToDto( $dto, $request );
// Set author from current user
$dto->author_id = user_id();
// Validate DTO
if( !$dto->validate() )
{
$this->validationError( 'admin_pages_create', $dto->getErrors() );
}
try
{
// Pass DTO to service
$page = $this->_pageCreator->create( $dto );
if( !$page )
{
Log::error( "Page creation failed for user " . user_id() . ", title: {$dto->title}" );
$this->redirect( 'admin_pages_create', [], [FlashMessageType::ERROR->value, 'Failed to create page. Please try again.'] );
}
Log::info( "Page created successfully: ID {$page->getId()}, title: {$dto->title}, by user " . user_id() );
$this->redirect( 'admin_pages', [], [FlashMessageType::SUCCESS->value, 'Page created successfully'] );
}
catch( \Exception $e )
{
Log::error( "Exception during page creation by user " . user_id() . ": {$e->getMessage()}", [
'exception' => $e,
'trace' => $e->getTraceAsString()
] );
$this->redirect( 'admin_pages_create', [], [FlashMessageType::ERROR->value, $e->getMessage()] );
}
}
/**
* Show edit page form
* @param Request $request
* @return string
* @throws \Exception
*/
#[Get('/pages/:id/edit', name: 'admin_pages_edit')]
public function edit( Request $request ): string
{
$pageId = (int)$request->getRouteParameter( 'id' );
$page = $this->_pageRepository->findById( $pageId );
if( !$page )
{
$this->redirect( 'admin_pages', [], [FlashMessageType::ERROR->value, 'Page not found'] );
}
// Check permissions
if( !is_admin() && !is_editor() && $page->getAuthorId() !== user_id() )
{
Log::warning( "Unauthorized edit attempt by user " . user_id() . " on page {$pageId}" );
$this->redirect( 'admin_pages', [], [FlashMessageType::ERROR->value, 'Unauthorized to edit this page'] );
}
$this->initializeCsrfToken();
return $this->view()
->title( 'Edit Page' )
->description( 'Edit page' )
->withCurrentUser()
->withCsrfToken()
->with( 'page', $page )
->render( 'edit', 'admin' );
}
/**
* Update page
* @param Request $request
* @return never
* @throws \Exception
*/
#[Put('/pages/:id', name: 'admin_pages_update', filters: ['csrf'])]
public function update( Request $request ): never
{
$pageId = (int)$request->getRouteParameter( 'id' );
$page = $this->_pageRepository->findById( $pageId );
if( !$page )
{
$this->redirect( 'admin_pages', [], [FlashMessageType::ERROR->value, 'Page not found'] );
}
// Check permissions
if( !is_admin() && !is_editor() && $page->getAuthorId() !== user_id() )
{
Log::warning( "Unauthorized page update attempt: User " . user_id() . " tried to edit page {$pageId}" );
$this->redirect( 'admin_pages', [], [FlashMessageType::ERROR->value, 'Unauthorized to edit this page'] );
}
// Create DTO from YAML configuration
$dto = $this->createDto( 'pages/update-page-request.yaml' );
// Map request data to DTO
$this->mapRequestToDto( $dto, $request );
// Set ID from route parameter
$dto->id = $pageId;
// Validate DTO
if( !$dto->validate() )
{
$this->validationError( 'admin_pages_edit', $dto->getErrors(), ['id' => $pageId] );
}
try
{
// Pass DTO to service
$success = $this->_pageUpdater->update( $dto );
if( !$success )
{
Log::error( "Page update failed: Page {$pageId}, user " . user_id() . ", title: {$dto->title}" );
$this->redirect( 'admin_pages_edit', ['id' => $pageId], [FlashMessageType::ERROR->value, 'Failed to update page. Please try again.'] );
}
Log::info( "Page updated successfully: Page {$pageId}, title: {$dto->title}, by user " . user_id() );
$this->redirect( 'admin_pages', [], [FlashMessageType::SUCCESS->value, 'Page updated successfully'] );
}
catch( \Exception $e )
{
Log::error( "Exception during page update: Page {$pageId}, user " . user_id() . ": {$e->getMessage()}", [
'exception' => $e,
'trace' => $e->getTraceAsString()
] );
$this->redirect( 'admin_pages_edit', ['id' => $pageId], [FlashMessageType::ERROR->value, $e->getMessage()] );
}
}
/**
* Delete page
* @param Request $request
* @return never
*/
#[Delete('/pages/:id', name: 'admin_pages_destroy', filters: ['csrf'])]
public function destroy( Request $request ): never
{
$pageId = (int)$request->getRouteParameter( 'id' );
$page = $this->_pageRepository->findById( $pageId );
if( !$page )
{
$this->redirect( 'admin_pages', [], [FlashMessageType::ERROR->value, 'Page not found'] );
}
// Check permissions
if( !is_admin() && !is_editor() && $page->getAuthorId() !== user_id() )
{
Log::warning( "Unauthorized page deletion attempt: User " . user_id() . " tried to delete page {$pageId}" );
$this->redirect( 'admin_pages', [], [FlashMessageType::ERROR->value, 'Unauthorized to delete this page'] );
}
try
{
$pageTitle = $page->getTitle(); // Store for logging before deletion
$success = $this->_pageRepository->delete( $pageId );
if( !$success )
{
Log::error( "Page deletion failed: Page {$pageId}, user " . user_id() );
$this->redirect( 'admin_pages', [], [FlashMessageType::ERROR->value, 'Failed to delete page. Please try again.'] );
}
Log::info( "Page deleted successfully: Page {$pageId}, title: {$pageTitle}, by user " . user_id() );
$this->redirect( 'admin_pages', [], [FlashMessageType::SUCCESS->value, 'Page deleted successfully'] );
}
catch( \Exception $e )
{
Log::error( "Exception during page deletion: Page {$pageId}, user " . user_id() . ": {$e->getMessage()}", [
'exception' => $e,
'trace' => $e->getTraceAsString()
] );
$this->redirect( 'admin_pages', [], [FlashMessageType::ERROR->value, 'Failed to delete page. Please try again.'] );
}
}
}