1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jidaikobo\Kontiki\Controllers; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
6
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
7
|
|
|
use Slim\App; |
8
|
|
|
use Slim\Views\PhpRenderer; |
9
|
|
|
|
10
|
|
|
use Jidaikobo\Kontiki\Core\Auth; |
11
|
|
|
use Jidaikobo\Kontiki\Managers\CsrfManager; |
12
|
|
|
use Jidaikobo\Kontiki\Managers\FlashManager; |
13
|
|
|
use Jidaikobo\Kontiki\Models\AccountModel; |
14
|
|
|
use Jidaikobo\Kontiki\Services\FormService; |
15
|
|
|
use Jidaikobo\Kontiki\Services\RoutesService; |
16
|
|
|
|
17
|
|
|
class AccountController extends BaseController |
18
|
|
|
{ |
19
|
|
|
protected string $adminDirName = 'account'; |
20
|
|
|
protected string $label = 'account'; |
21
|
|
|
private Auth $auth; |
22
|
|
|
private FormService $formService; |
23
|
|
|
private AccountModel $model; |
24
|
|
|
|
25
|
|
|
use Traits\CreateEditTrait; |
26
|
|
|
|
27
|
|
|
public function __construct( |
28
|
|
|
CsrfManager $csrfManager, |
29
|
|
|
FlashManager $flashManager, |
30
|
|
|
PhpRenderer $view, |
31
|
|
|
RoutesService $routesService, |
32
|
|
|
Auth $auth, |
33
|
|
|
FormService $formService, |
34
|
|
|
AccountModel $model |
35
|
|
|
) { |
36
|
|
|
parent::__construct( |
37
|
|
|
$csrfManager, |
38
|
|
|
$flashManager, |
39
|
|
|
$view, |
40
|
|
|
$routesService |
41
|
|
|
); |
42
|
|
|
$this->auth = $auth; |
43
|
|
|
$this->formService = $formService; |
44
|
|
|
$this->formService->setModel($model); |
45
|
|
|
$this->model = $model; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public static function registerRoutes(App $app, string $basePath = ''): void |
49
|
|
|
{ |
50
|
|
|
$app->get('/account/settings', AccountController::class . ':handleRenderEditForm'); |
51
|
|
|
$app->post("/account/edit/{id}", AccountController::class . ':handleEdit'); |
52
|
|
|
|
53
|
|
|
// redirect |
54
|
|
|
$app->get('/account/index', AccountController::class . ':accoutEditRedirect'); |
55
|
|
|
$app->get("/account/edit/{id}", AccountController::class . ':accoutEditRedirect'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function accoutEditRedirect( |
59
|
|
|
Request $request, |
60
|
|
|
Response $response, |
61
|
|
|
array $args |
|
|
|
|
62
|
|
|
): Response { |
63
|
|
|
return $this->redirectResponse( |
64
|
|
|
$request, |
65
|
|
|
$response, |
66
|
|
|
"/account/settings" |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function handleRenderEditForm( |
71
|
|
|
Request $request, |
72
|
|
|
Response $response, |
73
|
|
|
array $args |
74
|
|
|
): Response { |
75
|
|
|
$args['id'] = $this->auth->getCurrentUser()['id'] ?? 0; |
76
|
|
|
if ($args['id'] == 0) { |
77
|
|
|
die(); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
return $this->renderEditForm($request, $response, $args); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function handleEdit( |
83
|
|
|
Request $request, |
84
|
|
|
Response $response, |
85
|
|
|
array $args |
86
|
|
|
): Response { |
87
|
|
|
$id = $args['id']; |
88
|
|
|
return $this->handleSave($request, $response, 'edit', $id); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.