xref: /webtrees/app/Module/ChartsBlockModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Contracts\UserInterface;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Individual;
26use Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
27use Fisharebest\Webtrees\Registry;
28use Fisharebest\Webtrees\Services\ModuleService;
29use Fisharebest\Webtrees\Tree;
30use Fisharebest\Webtrees\Validator;
31use Illuminate\Support\Str;
32use Psr\Http\Message\ServerRequestInterface;
33
34use function extract;
35use function uasort;
36use function view;
37
38use const EXTR_OVERWRITE;
39
40/**
41 * Class ChartsBlockModule
42 */
43class ChartsBlockModule extends AbstractModule implements ModuleBlockInterface
44{
45    use ModuleBlockTrait;
46
47    private ModuleService $module_service;
48
49    /**
50     * @param ModuleService $module_service
51     */
52    public function __construct(ModuleService $module_service)
53    {
54        $this->module_service = $module_service;
55    }
56
57    /**
58     * How should this module be identified in the control panel, etc.?
59     *
60     * @return string
61     */
62    public function title(): string
63    {
64        /* I18N: Name of a module/block */
65        return I18N::translate('Charts');
66    }
67
68    public function description(): string
69    {
70        /* I18N: Description of the “Charts” module */
71        return I18N::translate('An alternative way to display charts.');
72    }
73
74    /**
75     * Generate the HTML content of this block.
76     *
77     * @param Tree                 $tree
78     * @param int                  $block_id
79     * @param string               $context
80     * @param array<string,string> $config
81     *
82     * @return string
83     */
84    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
85    {
86        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
87        $gedcomid         = $tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF);
88        $default_xref     = $gedcomid ?: $PEDIGREE_ROOT_ID;
89
90        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
91        $xref = $this->getBlockSetting($block_id, 'pid', $default_xref);
92
93        extract($config, EXTR_OVERWRITE);
94
95        $individual = Registry::individualFactory()->make($xref, $tree);
96
97        $title = $this->title();
98
99        if ($individual instanceof Individual) {
100            switch ($type) {
101                default:
102                case 'pedigree':
103                    $module = $this->module_service->findByInterface(PedigreeChartModule::class)->first();
104                    if ($module instanceof PedigreeChartModule) {
105                        $title     = $module->chartTitle($individual);
106                        $chart_url = $module->chartUrl($individual, [
107                            'ajax'        => true,
108                            'generations' => 3,
109                            'layout'      => PedigreeChartModule::STYLE_RIGHT,
110                        ]);
111                        $content   = view('modules/charts/chart', [
112                            'block_id'  => $block_id,
113                            'chart_url' => $chart_url,
114                        ]);
115                    } else {
116                        $title   = I18N::translate('Pedigree');
117                        $content = I18N::translate('The module “%s” has been disabled.', $title);
118                    }
119                    break;
120
121                case 'descendants':
122                    $module = $this->module_service->findByInterface(DescendancyChartModule::class)->first();
123
124                    if ($module instanceof DescendancyChartModule) {
125                        $title     = $module->chartTitle($individual);
126                        $chart_url = $module->chartUrl($individual, [
127                            'ajax'        => true,
128                            'generations' => 2,
129                            'chart_style' => DescendancyChartModule::CHART_STYLE_TREE,
130                        ]);
131                        $content   = view('modules/charts/chart', [
132                            'block_id'  => $block_id,
133                            'chart_url' => $chart_url,
134                        ]);
135                    } else {
136                        $title   = I18N::translate('Descendants');
137                        $content = I18N::translate('The module “%s” has been disabled.', $title);
138                    }
139
140                    break;
141
142                case 'hourglass':
143                    $module = $this->module_service->findByInterface(HourglassChartModule::class)->first();
144
145                    if ($module instanceof HourglassChartModule) {
146                        $title     = $module->chartTitle($individual);
147                        $chart_url = $module->chartUrl($individual, [
148                            'ajax'        => true,
149                            'generations' => 2,
150                        ]);
151                        $content   = view('modules/charts/chart', [
152                            'block_id'  => $block_id,
153                            'chart_url' => $chart_url,
154                        ]);
155                    } else {
156                        $title   = I18N::translate('Hourglass chart');
157                        $content = I18N::translate('The module “%s” has been disabled.', $title);
158                    }
159                    break;
160
161                case 'treenav':
162                    $module = $this->module_service->findByInterface(InteractiveTreeModule::class)->first();
163
164                    if ($module instanceof InteractiveTreeModule) {
165                        $title  = I18N::translate('Interactive tree of %s', $individual->fullName());
166                        $tv     = new TreeView();
167                        [$html, $js] = $tv->drawViewport($individual, 2);
168                        $content = $html . '<script>' . $js . '</script>';
169                    } else {
170                        $title   = I18N::translate('Interactive tree');
171                        $content = I18N::translate('The module “%s” has been disabled.', $title);
172                    }
173
174                    break;
175            }
176        } else {
177            $content = I18N::translate('You must select an individual and a chart type in the block preferences');
178        }
179
180        if ($context !== self::CONTEXT_EMBED) {
181            return view('modules/block-template', [
182                'block'      => Str::kebab($this->name()),
183                'id'         => $block_id,
184                'config_url' => $this->configUrl($tree, $context, $block_id),
185                'title'      => $title,
186                'content'    => $content,
187            ]);
188        }
189
190        return $content;
191    }
192
193    /**
194     * Should this block load asynchronously using AJAX?
195     *
196     * Simple blocks are faster in-line, more complex ones can be loaded later.
197     *
198     * @return bool
199     */
200    public function loadAjax(): bool
201    {
202        return true;
203    }
204
205    /**
206     * Can this block be shown on the tree’s home page?
207     *
208     * @return bool
209     */
210    public function isTreeBlock(): bool
211    {
212        return true;
213    }
214
215    /**
216     * Update the configuration for a block.
217     *
218     * @param ServerRequestInterface $request
219     * @param int     $block_id
220     *
221     * @return void
222     */
223    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
224    {
225        $type = Validator::parsedBody($request)->string('type');
226        $xref = Validator::parsedBody($request)->isXref()->string('xref');
227
228        $this->setBlockSetting($block_id, 'type', $type);
229        $this->setBlockSetting($block_id, 'pid', $xref);
230    }
231
232    /**
233     * An HTML form to edit block settings
234     *
235     * @param Tree $tree
236     * @param int  $block_id
237     *
238     * @return string
239     */
240    public function editBlockConfiguration(Tree $tree, int $block_id): string
241    {
242        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
243        $gedcomid         = $tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF);
244        $default_xref     = $gedcomid ?: $PEDIGREE_ROOT_ID;
245
246        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
247        $xref = $this->getBlockSetting($block_id, 'pid', $default_xref);
248
249        $charts = [
250            'pedigree'    => I18N::translate('Pedigree'),
251            'descendants' => I18N::translate('Descendants'),
252            'hourglass'   => I18N::translate('Hourglass chart'),
253            'treenav'     => I18N::translate('Interactive tree'),
254        ];
255        uasort($charts, I18N::comparator());
256
257        $individual = Registry::individualFactory()->make($xref, $tree);
258
259        return view('modules/charts/config', [
260            'charts'     => $charts,
261            'individual' => $individual,
262            'tree'       => $tree,
263            'type'       => $type,
264        ]);
265    }
266}
267