Issues (2564)

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