xref: /webtrees/app/Module/ChartsBlockModule.php (revision 4c3563c0d95e22997e247ca4944dff997416f927)
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 <http://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                            'class'     => 'wt-chart-pedigree',
124                        ]);
125                    } else {
126                        $title   = I18N::translate('Pedigree');
127                        $content = I18N::translate('The module “%s” has been disabled.', $title);
128                    }
129                    break;
130
131                case 'descendants':
132                    $module = $this->module_service->findByInterface(DescendancyChartModule::class)->first();
133
134                    if ($module instanceof DescendancyChartModule) {
135                        $title     = $module->chartTitle($individual);
136                        $chart_url = $module->chartUrl($individual, [
137                            'ajax'        => true,
138                            'generations' => 2,
139                            'chart_style' => DescendancyChartModule::CHART_STYLE_TREE,
140                        ]);
141                        $content   = view('modules/charts/chart', [
142                            'block_id'  => $block_id,
143                            'chart_url' => $chart_url,
144                            'class'     => 'wt-chart-descendants',
145                        ]);
146                    } else {
147                        $title   = I18N::translate('Descendants');
148                        $content = I18N::translate('The module “%s” has been disabled.', $title);
149                    }
150
151                    break;
152
153                case 'hourglass':
154                    $module    = $this->module_service->findByInterface(HourglassChartModule::class)->first();
155
156                    if ($module instanceof HourglassChartModule) {
157                        $title     = $module->chartTitle($individual);
158                        $chart_url = $module->chartUrl($individual, [
159                            'ajax'        => true,
160                            'generations' => 2,
161                        ]);
162                        $content   = view('modules/charts/chart', [
163                            'block_id'  => $block_id,
164                            'chart_url' => $chart_url,
165                            'class'     => 'wt-chart-hourglass',
166                        ]);
167                    } else {
168                        $title   = I18N::translate('Hourglass chart');
169                        $content = I18N::translate('The module “%s” has been disabled.', $title);
170                    }
171                    break;
172
173                case 'treenav':
174                    $module = $this->module_service->findByInterface(InteractiveTreeModule::class)->first();
175
176                    if ($module instanceof InteractiveTreeModule) {
177                        $title  = I18N::translate('Interactive tree of %s', $individual->fullName());
178                        $tv     = new TreeView();
179                        [$html, $js] = $tv->drawViewport($individual, 2);
180                        $content = $html . '<script>' . $js . '</script>';
181                    } else {
182                        $title   = I18N::translate('Interactive tree');
183                        $content = I18N::translate('The module “%s” has been disabled.', $title);
184                    }
185
186                    break;
187            }
188        } else {
189            $content = I18N::translate('You must select an individual and a chart type in the block preferences');
190        }
191
192        if ($context !== self::CONTEXT_EMBED) {
193            return view('modules/block-template', [
194                'block'      => Str::kebab($this->name()),
195                'id'         => $block_id,
196                'config_url' => $this->configUrl($tree, $context, $block_id),
197                'title'      => $title,
198                'content'    => $content,
199            ]);
200        }
201
202        return $content;
203    }
204
205    /**
206     * Should this block load asynchronously using AJAX?
207     *
208     * Simple blocks are faster in-line, more complex ones can be loaded later.
209     *
210     * @return bool
211     */
212    public function loadAjax(): bool
213    {
214        return true;
215    }
216
217    /**
218     * Can this block be shown on the user’s home page?
219     *
220     * @return bool
221     */
222    public function isUserBlock(): bool
223    {
224        return true;
225    }
226
227    /**
228     * Can this block be shown on the tree’s home page?
229     *
230     * @return bool
231     */
232    public function isTreeBlock(): bool
233    {
234        return true;
235    }
236
237    /**
238     * Update the configuration for a block.
239     *
240     * @param ServerRequestInterface $request
241     * @param int     $block_id
242     *
243     * @return void
244     */
245    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
246    {
247        $params = (array) $request->getParsedBody();
248
249        $this->setBlockSetting($block_id, 'type', $params['type'] ?? 'pedigree');
250        $this->setBlockSetting($block_id, 'pid', $params['xref'] ?? '');
251    }
252
253    /**
254     * An HTML form to edit block settings
255     *
256     * @param Tree $tree
257     * @param int  $block_id
258     *
259     * @return string
260     */
261    public function editBlockConfiguration(Tree $tree, int $block_id): string
262    {
263        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
264        $gedcomid         = $tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF);
265        $default_xref     = $gedcomid ?: $PEDIGREE_ROOT_ID;
266
267        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
268        $xref  = $this->getBlockSetting($block_id, 'pid', $default_xref);
269
270        $charts = [
271            'pedigree'    => I18N::translate('Pedigree'),
272            'descendants' => I18N::translate('Descendants'),
273            'hourglass'   => I18N::translate('Hourglass chart'),
274            'treenav'     => I18N::translate('Interactive tree'),
275        ];
276        uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp');
277
278        $individual = Registry::individualFactory()->make($xref, $tree);
279
280        return view('modules/charts/config', [
281            'charts'     => $charts,
282            'individual' => $individual,
283            'tree'       => $tree,
284            'type'       => $type,
285        ]);
286    }
287}
288