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