xref: /webtrees/app/Module/ChartsBlockModule.php (revision 1f1ffa65b3b51df2b95b5c68894525436855964a)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Module;
19
20use Fisharebest\Webtrees\Auth;
21use Fisharebest\Webtrees\I18N;
22use Fisharebest\Webtrees\Individual;
23use Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
24use Fisharebest\Webtrees\Services\ModuleService;
25use Fisharebest\Webtrees\Tree;
26use Illuminate\Support\Str;
27use Psr\Http\Message\ServerRequestInterface;
28
29/**
30 * Class ChartsBlockModule
31 */
32class ChartsBlockModule extends AbstractModule implements ModuleBlockInterface
33{
34    use ModuleBlockTrait;
35
36    /**
37     * @var ModuleService
38     */
39    private $module_service;
40
41    /**
42     * ChartsBlockModule constructor.
43     *
44     * @param ModuleService $module_service
45     */
46    public function __construct(ModuleService $module_service)
47    {
48        $this->module_service = $module_service;
49    }
50
51    /**
52     * How should this module be identified in the control panel, etc.?
53     *
54     * @return string
55     */
56    public function title(): string
57    {
58        /* I18N: Name of a module/block */
59        return I18N::translate('Charts');
60    }
61
62    /**
63     * A sentence describing what this module does.
64     *
65     * @return string
66     */
67    public function description(): string
68    {
69        /* I18N: Description of the “Charts” module */
70        return I18N::translate('An alternative way to display charts.');
71    }
72
73    /**
74     * Generate the HTML content of this block.
75     *
76     * @param Tree     $tree
77     * @param int      $block_id
78     * @param string   $ctype
79     * @param string[] $cfg
80     *
81     * @return string
82     */
83    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
84    {
85        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
86        $gedcomid         = $tree->getUserPreference(Auth::user(), 'gedcomid');
87
88        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
89        $pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
90
91        extract($cfg, EXTR_OVERWRITE);
92
93        $person = Individual::getInstance($pid, $tree);
94        if (!$person) {
95            $pid = $PEDIGREE_ROOT_ID;
96            $this->setBlockSetting($block_id, 'pid', $pid);
97            $person = Individual::getInstance($pid, $tree);
98        }
99
100        $title = $this->title();
101
102        if ($person) {
103            switch ($type) {
104                default:
105                case 'pedigree':
106                    /** @var PedigreeChartModule $module */
107                    $module    = $this->module_service->findByInterface(PedigreeChartModule::class)->first();
108                    $title     = $module->chartTitle($person);
109                    $chart_url = $module->chartUrl($person, [
110                        'ajax'        => true,
111                        'generations' => 3,
112                        'layout'      => PedigreeChartModule::ORIENTATION_RIGHT,
113                    ]);
114                    $content   = view('modules/charts/chart', [
115                        'block_id'  => $block_id,
116                        'chart_url' => $chart_url,
117                    ]);
118                    break;
119
120                case 'descendants':
121                    /** @var DescendancyChartModule $module */
122                    $module    = $this->module_service->findByInterface(DescendancyChartModule::class)->first();
123                    $title     = $module->chartTitle($person);
124                    $chart_url = $module->chartUrl($person, [
125                        'ajax'        => true,
126                        'generations' => 2,
127                        'chart_style' => DescendancyChartModule::CHART_STYLE_LIST,
128                    ]);
129                    $content   = view('modules/charts/chart', [
130                        'block_id'  => $block_id,
131                        'chart_url' => $chart_url,
132                    ]);
133                    break;
134
135                case 'hourglass':
136                    /** @var HourglassChartModule $module */
137                    $module    = $this->module_service->findByInterface(HourglassChartModule::class)->first();
138                    $title     = $module->chartTitle($person);
139                    $chart_url = $module->chartUrl($person, [
140                        'ajax'        => true,
141                        'generations' => 2,
142                    ]);
143                    $content   = view('modules/charts/chart', [
144                        'block_id'  => $block_id,
145                        'chart_url' => $chart_url,
146                    ]);
147                    break;
148
149                case 'treenav':
150                    /** @var InteractiveTreeModule $module */
151                    $module  = $this->module_service->findByInterface(InteractiveTreeModule::class);
152                    $title   = I18N::translate('Interactive tree of %s', $person->fullName());
153                    $tv      = new TreeView();
154                    $content = '<script>$("head").append(\'<link rel="stylesheet" href="' . $module->css() . '" type="text/css" />\');</script>';
155                    $content .= '<script src="' . $module->js() . '"></script>';
156                    [$html, $js] = $tv->drawViewport($person, 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 ($ctype !== '') {
165            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
166                $config_url = route('tree-page-block-edit', [
167                    'block_id' => $block_id,
168                    'ged'      => $tree->name(),
169                ]);
170            } elseif ($ctype === 'user' && Auth::check()) {
171                $config_url = route('user-page-block-edit', [
172                    'block_id' => $block_id,
173                    'ged'      => $tree->name(),
174                ]);
175            } else {
176                $config_url = '';
177            }
178
179            return view('modules/block-template', [
180                'block'      => Str::kebab($this->name()),
181                'id'         => $block_id,
182                'config_url' => $config_url,
183                'title'      => strip_tags($title),
184                'content'    => $content,
185            ]);
186        }
187
188        return $content;
189    }
190
191    /** {@inheritdoc} */
192    public function loadAjax(): bool
193    {
194        return true;
195    }
196
197    /** {@inheritdoc} */
198    public function isUserBlock(): bool
199    {
200        return true;
201    }
202
203    /** {@inheritdoc} */
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->get('type', 'pedigree'));
220        $this->setBlockSetting($block_id, 'pid', $request->get('pid', ''));
221    }
222
223    /**
224     * An HTML form to edit block settings
225     *
226     * @param Tree $tree
227     * @param int  $block_id
228     *
229     * @return void
230     */
231    public function editBlockConfiguration(Tree $tree, int $block_id): void
232    {
233        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
234        $gedcomid         = $tree->getUserPreference(Auth::user(), 'gedcomid');
235
236        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
237        $pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
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($pid, $tree);
248
249        echo view('modules/charts/config', [
250            'charts'     => $charts,
251            'individual' => $individual,
252            'tree'       => $tree,
253            'type'       => $type,
254        ]);
255    }
256}
257