Issues (2560)

app/Cli/Commands/UserSetting.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Cli\Commands;
21
22
use Fisharebest\Webtrees\DB;
0 ignored issues
show
The type Fisharebest\Webtrees\DB was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Symfony\Component\Console\Helper\Table;
24
use Symfony\Component\Console\Input\InputArgument;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Input\InputOption;
27
use Symfony\Component\Console\Output\OutputInterface;
28
use Symfony\Component\Console\Style\SymfonyStyle;
29
30
final class UserSetting extends AbstractCommand
31
{
32
    protected function configure(): void
33
    {
34
        $this
35
            ->setName(name: 'user-setting')
36
            ->setDescription(description: 'Configure user settings')
37
            ->addOption(name: 'list', shortcut: 'l', mode: InputOption::VALUE_NONE, description: 'List user settings (optionally filtered by setting name)')
38
            ->addOption(name: 'delete', shortcut: 'd', mode: InputOption::VALUE_NONE, description: 'Delete a user setting')
39
            ->addArgument(name: 'user-name', mode: InputArgument::REQUIRED, description: 'The user to update')
40
            ->addArgument(name: 'setting-name', mode: InputArgument::OPTIONAL, description: 'The setting to update')
41
            ->addArgument(name: 'setting-value', mode: InputArgument::OPTIONAL, description: 'The new value of the setting.');
42
    }
43
44
    protected function execute(InputInterface $input, OutputInterface $output): int
45
    {
46
        $quiet  = $this->boolOption(input: $input, name: 'quiet');
47
        $list   = $this->boolOption(input: $input, name: 'list');
48
        $delete = $this->boolOption(input: $input, name: 'delete');
49
50
        $user_name     = $this->stringArgument(input: $input, name: 'user-name');
51
        $setting_name  = $this->stringArgument(input: $input, name: 'setting-name');
52
        $setting_value = $this->stringArgument(input: $input, name: 'setting-value');
53
54
        $io = new SymfonyStyle(input: $input, output: $output);
55
56
        $user_id = DB::table('user')
57
            ->where(column: 'user_name', operator: '=', value: $user_name)
58
            ->value(column: 'user_id');
59
60
        if ($user_id === null) {
61
            $io->error(message: 'User ‘' . $user_name . '’ not found.');
62
63
            return self::FAILURE;
64
        }
65
66
        if ($list) {
67
            if ($delete) {
68
                $io->error(message: 'Cannot specify --list and --delete together.');
69
70
                return self::FAILURE;
71
            }
72
73
            if ($setting_value !== '') {
74
                $io->error(message: 'Cannot specify --list and a new value together.');
75
76
                return self::FAILURE;
77
            }
78
79
            $table = new Table(output: $output);
80
            $table->setHeaders(headers: ['Setting name', 'Setting value']);
81
82
            /** @var array<object{setting_name:string,setting_value:string}> $settings */
83
            $settings = DB::table(table: 'user_setting')
84
                ->where(column: 'user_id', operator: '=', value: $user_id)
85
                ->orderBy(column: 'setting_name')
86
                ->select(columns: ['setting_name', 'setting_value'])
87
                ->get()
88
                ->all();
89
90
            foreach ($settings as $setting) {
91
                if (str_contains(haystack: $setting->setting_name, needle: $setting_name)) {
92
                    $table->addRow(row: [$setting->setting_name, $setting->setting_value]);
93
                }
94
            }
95
96
            $table->render();
97
98
            return self::SUCCESS;
99
        }
100
101
        /** @var string|null $old_setting_value */
102
        $old_setting_value = DB::table('user_setting')
103
            ->where(column: 'user_id', operator: '=', value: $user_id)
104
            ->where(column: 'setting_name', operator: '=', value: $setting_name)
105
            ->value(column: 'setting_value');
106
107
        if ($delete) {
108
            if ($setting_name === '') {
109
                $io->error(message: 'Setting name must be specified for --delete.');
110
111
                return self::FAILURE;
112
            }
113
114
            if ($setting_value !== '') {
115
                $io->error(message: 'Cannot specify --delete and a new value together.');
116
117
                return self::FAILURE;
118
            }
119
120
            if ($old_setting_value === null) {
121
                $io->warning(message: 'User setting ‘' . $setting_name . '’ not found.  Nothing to delete.');
122
            } else {
123
                DB::table('user_setting')
124
                    ->where(column: 'user_id', operator: '=', value: $user_id)
125
                    ->where('setting_name', '=', $setting_name)
126
                    ->delete();
127
128
                $io->success(message: 'User setting ‘' . $setting_name . '’ deleted.  Previous value was ‘' . $old_setting_value . '’.');
129
            }
130
131
            return self::SUCCESS;
132
        }
133
134
135
        if ($setting_name === '') {
136
            $io->error(message: 'A setting name is required, unless the --list option is used.');
137
138
            return self::FAILURE;
139
        }
140
141
        if ($setting_value === '') {
142
            if ($old_setting_value === null) {
143
                $io->info(message: 'User setting ‘' . $setting_name . '’ is not currently set.');
144
            } elseif ($quiet) {
145
                $verbosity = $io->getVerbosity();
146
                $io->setVerbosity(level: OutputInterface::VERBOSITY_NORMAL);
147
                $io->writeln(messages: $old_setting_value);
148
                $io->setVerbosity(level: $verbosity);
149
            } else {
150
                $io->info(message: 'User setting ‘' . $setting_name . '’ is currently set to ‘' . $old_setting_value . '’.');
151
            }
152
153
            return self::SUCCESS;
154
        }
155
156
        if ($old_setting_value === $setting_value) {
157
            $io->warning(message: 'User setting ' . $setting_name . ' is already set to ' . $setting_value);
158
159
            return self::SUCCESS;
160
        }
161
162
        if ($old_setting_value === null) {
163
            DB::table(table: 'user_setting')
164
                ->insert(values: [
165
                    'user_id'       => $user_id,
166
                    'setting_name'  => $setting_name,
167
                    'setting_value' => $setting_value,
168
                ]);
169
170
            $io->success(message: 'User setting ‘' . $setting_name . '’ was created as ‘' . $setting_value . '’.');
171
        } else {
172
            DB::table(table: 'user_setting')
173
                ->where(column: 'user_id', operator: '=', value: $user_id)
174
                ->where(column: 'setting_name', operator: '=', value: $setting_name)
175
                ->update(values: ['setting_value' => $setting_value]);
176
177
            $io->success(message: 'User setting ‘' . $setting_name . '’ was changed from ‘' . $old_setting_value . '’ to ‘' . $setting_value . '’.');
178
        }
179
180
        return self::SUCCESS;
181
    }
182
}
183