xref: /webtrees/app/Cli/Commands/TreeList.php (revision d3aa520f698112f9a079527d6075b329bd948f1b)
1bb87bb9bSGreg Roach<?php
2bb87bb9bSGreg Roach
3bb87bb9bSGreg Roach/**
4bb87bb9bSGreg Roach * webtrees: online genealogy
5bb87bb9bSGreg Roach * Copyright (C) 2023 webtrees development team
6bb87bb9bSGreg Roach * This program is free software: you can redistribute it and/or modify
7bb87bb9bSGreg Roach * it under the terms of the GNU General Public License as published by
8bb87bb9bSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9bb87bb9bSGreg Roach * (at your option) any later version.
10bb87bb9bSGreg Roach * This program is distributed in the hope that it will be useful,
11bb87bb9bSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12bb87bb9bSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13bb87bb9bSGreg Roach * GNU General Public License for more details.
14bb87bb9bSGreg Roach * You should have received a copy of the GNU General Public License
15bb87bb9bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16bb87bb9bSGreg Roach */
17bb87bb9bSGreg Roach
18bb87bb9bSGreg Roachdeclare(strict_types=1);
19bb87bb9bSGreg Roach
20bb87bb9bSGreg Roachnamespace Fisharebest\Webtrees\Cli\Commands;
21bb87bb9bSGreg Roach
22bb87bb9bSGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
23bb87bb9bSGreg Roachuse Symfony\Component\Console\Command\Command;
24bb87bb9bSGreg Roachuse Symfony\Component\Console\Helper\Table;
25bb87bb9bSGreg Roachuse Symfony\Component\Console\Input\InputInterface;
26bb87bb9bSGreg Roachuse Symfony\Component\Console\Output\OutputInterface;
27bb87bb9bSGreg Roach
28bb87bb9bSGreg Roachclass TreeList extends Command
29bb87bb9bSGreg Roach{
30bb87bb9bSGreg Roach    public function __construct(private readonly TreeService $tree_service)
31bb87bb9bSGreg Roach    {
32bb87bb9bSGreg Roach        parent::__construct();
33bb87bb9bSGreg Roach    }
34bb87bb9bSGreg Roach
35bb87bb9bSGreg Roach    protected function configure(): void
36bb87bb9bSGreg Roach    {
37bb87bb9bSGreg Roach        $this
38bb87bb9bSGreg Roach            ->setName(name: 'tree-list')
39bb87bb9bSGreg Roach            ->setDescription(description: 'List trees');
40bb87bb9bSGreg Roach    }
41bb87bb9bSGreg Roach
42bb87bb9bSGreg Roach    protected function execute(InputInterface $input, OutputInterface $output): int
43bb87bb9bSGreg Roach    {
44*d3aa520fSGreg Roach        $trees = $this->tree_service->all()->sort(callback: fn ($a, $b) => $a->id() <=> $b->id());
45bb87bb9bSGreg Roach
46bb87bb9bSGreg Roach        $table = new Table(output: $output);
47bb87bb9bSGreg Roach
48bb87bb9bSGreg Roach        $table->setHeaders(headers: ['ID', 'Name', 'Title', 'Imported']);
49bb87bb9bSGreg Roach
50bb87bb9bSGreg Roach        foreach ($trees as $tree) {
51bb87bb9bSGreg Roach            $table->addRow(row: [
52bb87bb9bSGreg Roach                $tree->id(),
53bb87bb9bSGreg Roach                $tree->name(),
54bb87bb9bSGreg Roach                $tree->title(),
55bb87bb9bSGreg Roach                $tree->getPreference(setting_name: 'imported') ? 'Yes' : 'No'
56bb87bb9bSGreg Roach            ]);
57bb87bb9bSGreg Roach        }
58bb87bb9bSGreg Roach
59bb87bb9bSGreg Roach        $table->render();
60bb87bb9bSGreg Roach
61bb87bb9bSGreg Roach        return Command::SUCCESS;
62bb87bb9bSGreg Roach    }
63bb87bb9bSGreg Roach}
64