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