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