xref: /webtrees/app/Module/ChartsBlockModule.php (revision 36de22acf6348b1059dac63e3cd19589574906ac)
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 user’s home page?
216     *
217     * @return bool
218     */
219    public function isUserBlock(): bool
220    {
221        return true;
222    }
223
224    /**
225     * Can this block be shown on the tree’s home page?
226     *
227     * @return bool
228     */
229    public function isTreeBlock(): bool
230    {
231        return true;
232    }
233
234    /**
235     * Update the configuration for a block.
236     *
237     * @param ServerRequestInterface $request
238     * @param int     $block_id
239     *
240     * @return void
241     */
242    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
243    {
244        $params = (array) $request->getParsedBody();
245
246        $this->setBlockSetting($block_id, 'type', $params['type'] ?? 'pedigree');
247        $this->setBlockSetting($block_id, 'pid', $params['xref'] ?? '');
248    }
249
250    /**
251     * An HTML form to edit block settings
252     *
253     * @param Tree $tree
254     * @param int  $block_id
255     *
256     * @return string
257     */
258    public function editBlockConfiguration(Tree $tree, int $block_id): string
259    {
260        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
261        $gedcomid         = $tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF);
262        $default_xref     = $gedcomid ?: $PEDIGREE_ROOT_ID;
263
264        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
265        $xref  = $this->getBlockSetting($block_id, 'pid', $default_xref);
266
267        $charts = [
268            'pedigree'    => I18N::translate('Pedigree'),
269            'descendants' => I18N::translate('Descendants'),
270            'hourglass'   => I18N::translate('Hourglass chart'),
271            'treenav'     => I18N::translate('Interactive tree'),
272        ];
273        uasort($charts, I18N::comparator());
274
275        $individual = Registry::individualFactory()->make($xref, $tree);
276
277        return view('modules/charts/config', [
278            'charts'     => $charts,
279            'individual' => $individual,
280            'tree'       => $tree,
281            'type'       => $type,
282        ]);
283    }
284}
285