aboutsummaryrefslogtreecommitdiffhomepage
path: root/services/app/src/Forms/AdminUserEditForm.php
blob: 1a17f6c029db33882446d0a698bb02f20435abca (plain)
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
<?php

declare(strict_types=1);

namespace Nsfisis\Albatross\Forms;

use Nsfisis\Albatross\Form\FormBase;
use Nsfisis\Albatross\Form\FormItem;
use Nsfisis\Albatross\Form\FormState;
use Nsfisis\Albatross\Models\User;
use Nsfisis\Albatross\Repositories\UserRepository;
use Slim\Interfaces\RouteParserInterface;

final class AdminUserEditForm extends FormBase
{
    public function __construct(
        ?FormState $state,
        private readonly User $user,
        private readonly RouteParserInterface $routeParser,
        private readonly UserRepository $userRepo,
    ) {
        if (!isset($state)) {
            $state = new FormState([
                'username' => $user->username,
                'is_admin' => $user->is_admin ? 'on' : '',
            ]);
        }
        parent::__construct($state);
    }

    public function pageTitle(): string
    {
        return "管理画面 - ユーザ {$this->user->username} 編集";
    }

    public function redirectUrl(): string
    {
        return $this->routeParser->urlFor('admin_user_list');
    }

    protected function submitLabel(): string
    {
        return '保存';
    }

    /**
     * @return list<FormItem>
     */
    protected function items(): array
    {
        return [
            new FormItem(
                name: 'username',
                type: 'text',
                label: 'ユーザ名',
                isDisabled: true,
            ),
            new FormItem(
                name: 'is_admin',
                type: 'checkbox',
                label: '管理者',
            ),
        ];
    }

    /**
     * @return array{user: User}
     */
    public function getRenderContext(): array
    {
        return [
            'user' => $this->user,
        ];
    }

    public function submit(): void
    {
        $is_admin = $this->state->get('is_admin') === 'on';

        $this->userRepo->update(
            $this->user->user_id,
            is_admin: $is_admin,
        );
    }
}