summaryrefslogtreecommitdiff
path: root/app/Cli/Commands/TreeEdit.php
blob: e2d62ae32633a4366df338f3e9ef848a92603004 (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
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
<?php

/**
 * webtrees: online genealogy
 * Copyright (C) 2026 webtrees development team
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

declare(strict_types=1);

namespace Fisharebest\Webtrees\Cli\Commands;

use Fisharebest\Webtrees\Services\TreeService;
use Fisharebest\Webtrees\Tree;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

final class TreeEdit extends AbstractCommand
{
    public function __construct(private readonly TreeService $tree_service)
    {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->setName(name: 'tree')
            ->setDescription(description: 'Create/delete/edit a tree')
            ->addArgument(name: 'name', mode: InputArgument::REQUIRED, description: 'The name of the tree')
            ->addOption(name: 'create', mode: InputOption::VALUE_NONE, description: 'Create a new tree')
            ->addOption(name: 'delete', mode: InputOption::VALUE_NONE, description: 'Delete an existing tree')
            ->addOption(name: 'title', mode: InputOption::VALUE_REQUIRED, description: 'Set the title of the tree');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle(input: $input, output: $output);

        $name   = $this->stringArgument(input: $input, name: 'name');
        $title  = $this->stringOption(input: $input, name: 'title');
        $create = $this->boolOption(input: $input, name: 'create');
        $delete = $this->boolOption(input: $input, name: 'delete');

        if ($name === '') {
            $io->error(message: 'The tree name cannot be empty.');

            return Command::INVALID;
        }

        if ($delete && $create) {
            $io->error(message: 'Invalid options: cannot use --delete and --create at the same time.');

            return Command::INVALID;
        }

        if ($delete && $title !== '') {
            $io->error(message: 'Invalid options: cannot use --delete and --title at the same time.');

            return Command::INVALID;
        }

        $tree = $this->tree_service->all()->get($name);

        if ($create) {
            if ($tree instanceof Tree) {
                $io->error(message: 'A tree with the name "' . $name . '" already exists.');

                return Command::FAILURE;
            }

            if ($title === '') {
                $io->error(message: 'Invalid options: --title is required when using --create.');

                return Command::FAILURE;
            }

            $this->tree_service->create(name: $name, title: $title);
            $io->info(message: 'Tree ‘' . $name . '’ was created with title ‘' . $title . '’.');

            return self::SUCCESS;
        }

        if ($tree === null) {
            $io->error(message: 'Tree ‘' . $name . '’ does not exist.');

            return Command::FAILURE;
        }

        if ($delete) {
            $this->tree_service->delete($tree);
            $io->success(message: 'Tree ‘' . $name . '’ was deleted.');

            return self::SUCCESS;
        }

        if ($title === '') {
            $io->info(message: 'Nothing to do. Specify --title, --create or --delete.');

            return Command::INVALID;
        }

        $tree->setPreference('title', $title);
        $io->info(message: 'Tree title set to ‘' . $title . '’.');

        return self::SUCCESS;
    }
}