xref: /webtrees/app/Module/ChartsBlockModule.php (revision 241a1636dee0487eb56818cbf3b6a79ceaf02090)
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\Http\Controllers\PedigreeChartController;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Individual;
24use Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
25use Fisharebest\Webtrees\Tree;
26use Symfony\Component\HttpFoundation\Request;
27
28/**
29 * Class ChartsBlockModule
30 */
31class ChartsBlockModule extends AbstractModule implements ModuleInterface, ModuleBlockInterface
32{
33    use ModuleBlockTrait;
34
35    /**
36     * How should this module be labelled on tabs, menus, etc.?
37     *
38     * @return string
39     */
40    public function title(): string
41    {
42        /* I18N: Name of a module/block */
43        return I18N::translate('Charts');
44    }
45
46    /**
47     * A sentence describing what this module does.
48     *
49     * @return string
50     */
51    public function description(): string
52    {
53        /* I18N: Description of the “Charts” module */
54        return I18N::translate('An alternative way to display charts.');
55    }
56
57    /**
58     * Generate the HTML content of this block.
59     *
60     * @param Tree     $tree
61     * @param int      $block_id
62     * @param string   $ctype
63     * @param string[] $cfg
64     *
65     * @return string
66     */
67    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
68    {
69        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
70        $gedcomid         = $tree->getUserPreference(Auth::user(), 'gedcomid');
71
72        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
73        $pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
74
75        extract($cfg, EXTR_OVERWRITE);
76
77        $person = Individual::getInstance($pid, $tree);
78        if (!$person) {
79            $pid = $PEDIGREE_ROOT_ID;
80            $this->setBlockSetting($block_id, 'pid', $pid);
81            $person = Individual::getInstance($pid, $tree);
82        }
83
84        $title = $this->title();
85
86        if ($person) {
87            $content = '';
88            switch ($type) {
89                case 'pedigree':
90                    $title     = I18N::translate('Pedigree of %s', $person->getFullName());
91                    $chart_url = route('pedigree-chart', [
92                        'xref'        => $person->xref(),
93                        'ged'         => $person->tree()->name(),
94                        'generations' => 3,
95                        'layout'      => PedigreeChartController::PORTRAIT,
96                    ]);
97                    $content = view('modules/charts/chart', [
98                        'block_id'  => $block_id,
99                        'chart_url' => $chart_url,
100                    ]);
101                    break;
102                case 'descendants':
103                    $title     = I18N::translate('Descendants of %s', $person->getFullName());
104                    $chart_url = route('descendants-chart', [
105                        'xref'        => $person->xref(),
106                        'ged'         => $person->tree()->name(),
107                        'generations' => 2,
108                        'chart_style' => 0,
109                    ]);
110                    $content = view('modules/charts/chart', [
111                        'block_id'  => $block_id,
112                        'chart_url' => $chart_url,
113                    ]);
114                    break;
115                case 'hourglass':
116                    $title     = I18N::translate('Hourglass chart of %s', $person->getFullName());
117                    $chart_url = route('hourglass-chart', [
118                        'xref'        => $person->xref(),
119                        'ged'         => $person->tree()->name(),
120                        'generations' => 2,
121                        'layout'      => PedigreeChartController::PORTRAIT,
122                    ]);
123                    $content = view('modules/charts/chart', [
124                        'block_id'  => $block_id,
125                        'chart_url' => $chart_url,
126                    ]);
127                    break;
128                case 'treenav':
129                    $title   = I18N::translate('Interactive tree of %s', $person->getFullName());
130                    $mod     = new InteractiveTreeModule();
131                    $tv      = new TreeView();
132                    $content .= '<script>$("head").append(\'<link rel="stylesheet" href="' . $mod->css() . '" type="text/css" />\');</script>';
133                    $content .= '<script src="' . $mod->js() . '"></script>';
134                    [$html, $js] = $tv->drawViewport($person, 2);
135                    $content .= $html . '<script>' . $js . '</script>';
136                    break;
137            }
138        } else {
139            $content = I18N::translate('You must select an individual and a chart type in the block preferences');
140        }
141
142        if ($ctype !== '') {
143            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
144                $config_url = route('tree-page-block-edit', [
145                    'block_id' => $block_id,
146                    'ged'      => $tree->name(),
147                ]);
148            } elseif ($ctype === 'user' && Auth::check()) {
149                $config_url = route('user-page-block-edit', [
150                    'block_id' => $block_id,
151                    'ged'      => $tree->name(),
152                ]);
153            } else {
154                $config_url = '';
155            }
156
157            return view('modules/block-template', [
158                'block'      => str_replace('_', '-', $this->name()),
159                'id'         => $block_id,
160                'config_url' => $config_url,
161                'title'      => strip_tags($title),
162                'content'    => $content,
163            ]);
164        }
165
166        return $content;
167    }
168
169    /** {@inheritdoc} */
170    public function loadAjax(): bool
171    {
172        return true;
173    }
174
175    /** {@inheritdoc} */
176    public function isUserBlock(): bool
177    {
178        return true;
179    }
180
181    /** {@inheritdoc} */
182    public function isGedcomBlock(): bool
183    {
184        return true;
185    }
186
187    /**
188     * Update the configuration for a block.
189     *
190     * @param Request $request
191     * @param int     $block_id
192     *
193     * @return void
194     */
195    public function saveBlockConfiguration(Request $request, int $block_id)
196    {
197        $this->setBlockSetting($block_id, 'type', $request->get('type', 'pedigree'));
198        $this->setBlockSetting($block_id, 'pid', $request->get('pid', ''));
199    }
200
201    /**
202     * An HTML form to edit block settings
203     *
204     * @param Tree $tree
205     * @param int  $block_id
206     *
207     * @return void
208     */
209    public function editBlockConfiguration(Tree $tree, int $block_id)
210    {
211        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
212        $gedcomid         = $tree->getUserPreference(Auth::user(), 'gedcomid');
213
214        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
215        $pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
216
217        $charts = [
218            'pedigree'    => I18N::translate('Pedigree'),
219            'descendants' => I18N::translate('Descendants'),
220            'hourglass'   => I18N::translate('Hourglass chart'),
221            'treenav'     => I18N::translate('Interactive tree'),
222        ];
223        uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp');
224
225        $individual = Individual::getInstance($pid, $tree);
226
227        echo view('modules/charts/config', [
228            'charts'     => $charts,
229            'individual' => $individual,
230            'tree'       => $tree,
231            'type'       => $type,
232        ]);
233    }
234}
235