xref: /webtrees/app/Module/ChartsBlockModule.php (revision 26684e686fb5ab50ecb57e7e6c6a0a55852d2203)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
1976692c8bSGreg Roach
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
2102fe1c37SGreg Roachuse Fisharebest\Webtrees\Http\Controllers\PedigreeChartController;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
26a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class ChartsBlockModule
308c2e8227SGreg Roach */
3149a243cbSGreg Roachclass ChartsBlockModule extends AbstractModule implements ModuleInterface, ModuleBlockInterface
32c1010edaSGreg Roach{
3349a243cbSGreg Roach    use ModuleBlockTrait;
3449a243cbSGreg Roach
35961ec755SGreg Roach    /**
36961ec755SGreg Roach     * How should this module be labelled on tabs, menus, etc.?
37961ec755SGreg Roach     *
38961ec755SGreg Roach     * @return string
39961ec755SGreg Roach     */
4049a243cbSGreg Roach    public function title(): string
41c1010edaSGreg Roach    {
42bbb76c12SGreg Roach        /* I18N: Name of a module/block */
43bbb76c12SGreg Roach        return I18N::translate('Charts');
448c2e8227SGreg Roach    }
458c2e8227SGreg Roach
46961ec755SGreg Roach    /**
47961ec755SGreg Roach     * A sentence describing what this module does.
48961ec755SGreg Roach     *
49961ec755SGreg Roach     * @return string
50961ec755SGreg Roach     */
5149a243cbSGreg Roach    public function description(): string
52c1010edaSGreg Roach    {
53bbb76c12SGreg Roach        /* I18N: Description of the “Charts” module */
54bbb76c12SGreg Roach        return I18N::translate('An alternative way to display charts.');
558c2e8227SGreg Roach    }
568c2e8227SGreg Roach
5776692c8bSGreg Roach    /**
5876692c8bSGreg Roach     * Generate the HTML content of this block.
5976692c8bSGreg Roach     *
60e490cd80SGreg Roach     * @param Tree     $tree
6176692c8bSGreg Roach     * @param int      $block_id
625f2ae573SGreg Roach     * @param string   $ctype
63727f238cSGreg Roach     * @param string[] $cfg
6476692c8bSGreg Roach     *
6576692c8bSGreg Roach     * @return string
6676692c8bSGreg Roach     */
675f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
68c1010edaSGreg Roach    {
69e490cd80SGreg Roach        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
70e490cd80SGreg Roach        $gedcomid         = $tree->getUserPreference(Auth::user(), 'gedcomid');
718c2e8227SGreg Roach
72e2a378d3SGreg Roach        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
73bd44f43fSGreg Roach        $pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
748c2e8227SGreg Roach
75c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
768c2e8227SGreg Roach
77e490cd80SGreg Roach        $person = Individual::getInstance($pid, $tree);
788c2e8227SGreg Roach        if (!$person) {
798c2e8227SGreg Roach            $pid = $PEDIGREE_ROOT_ID;
80e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'pid', $pid);
81e490cd80SGreg Roach            $person = Individual::getInstance($pid, $tree);
828c2e8227SGreg Roach        }
838c2e8227SGreg Roach
8449a243cbSGreg Roach        $title = $this->title();
858c2e8227SGreg Roach
868c2e8227SGreg Roach        if ($person) {
87f679d3acSGreg Roach            $content = '';
888c2e8227SGreg Roach            switch ($type) {
898c2e8227SGreg Roach                case 'pedigree':
909c6524dcSGreg Roach                    $title     = I18N::translate('Pedigree of %s', $person->getFullName());
9102fe1c37SGreg Roach                    $chart_url = route('pedigree-chart', [
92c0935879SGreg Roach                        'xref'        => $person->xref(),
93f4afa648SGreg Roach                        'ged'         => $person->tree()->name(),
9402fe1c37SGreg Roach                        'generations' => 3,
9502fe1c37SGreg Roach                        'layout'      => PedigreeChartController::PORTRAIT,
9602fe1c37SGreg Roach                    ]);
97147e99aaSGreg Roach                    $content = view('modules/charts/chart', [
9802fe1c37SGreg Roach                        'block_id'  => $block_id,
9902fe1c37SGreg Roach                        'chart_url' => $chart_url,
10002fe1c37SGreg Roach                    ]);
1018c2e8227SGreg Roach                    break;
1028c2e8227SGreg Roach                case 'descendants':
1039c6524dcSGreg Roach                    $title     = I18N::translate('Descendants of %s', $person->getFullName());
10402fe1c37SGreg Roach                    $chart_url = route('descendants-chart', [
105c0935879SGreg Roach                        'xref'        => $person->xref(),
106f4afa648SGreg Roach                        'ged'         => $person->tree()->name(),
10702fe1c37SGreg Roach                        'generations' => 2,
10802fe1c37SGreg Roach                        'chart_style' => 0,
10902fe1c37SGreg Roach                    ]);
110147e99aaSGreg Roach                    $content = view('modules/charts/chart', [
11102fe1c37SGreg Roach                        'block_id'  => $block_id,
11202fe1c37SGreg Roach                        'chart_url' => $chart_url,
11302fe1c37SGreg Roach                    ]);
1148c2e8227SGreg Roach                    break;
1158c2e8227SGreg Roach                case 'hourglass':
1169c6524dcSGreg Roach                    $title     = I18N::translate('Hourglass chart of %s', $person->getFullName());
11702fe1c37SGreg Roach                    $chart_url = route('hourglass-chart', [
118c0935879SGreg Roach                        'xref'        => $person->xref(),
119f4afa648SGreg Roach                        'ged'         => $person->tree()->name(),
12002fe1c37SGreg Roach                        'generations' => 2,
12102fe1c37SGreg Roach                        'layout'      => PedigreeChartController::PORTRAIT,
12202fe1c37SGreg Roach                    ]);
123147e99aaSGreg Roach                    $content = view('modules/charts/chart', [
12402fe1c37SGreg Roach                        'block_id'  => $block_id,
12502fe1c37SGreg Roach                        'chart_url' => $chart_url,
12602fe1c37SGreg Roach                    ]);
1278c2e8227SGreg Roach                    break;
1288c2e8227SGreg Roach                case 'treenav':
1299c6524dcSGreg Roach                    $title   = I18N::translate('Interactive tree of %s', $person->getFullName());
13049a243cbSGreg Roach                    $mod     = new InteractiveTreeModule();
13159f2f229SGreg Roach                    $tv      = new TreeView();
132564ae2d7SGreg Roach                    $content .= '<script>$("head").append(\'<link rel="stylesheet" href="' . $mod->css() . '" type="text/css" />\');</script>';
1338c2e8227SGreg Roach                    $content .= '<script src="' . $mod->js() . '"></script>';
13465e02381SGreg Roach                    [$html, $js] = $tv->drawViewport($person, 2);
1358c2e8227SGreg Roach                    $content .= $html . '<script>' . $js . '</script>';
1368c2e8227SGreg Roach                    break;
1378c2e8227SGreg Roach            }
1388c2e8227SGreg Roach        } else {
1396e7bf391SGreg Roach            $content = I18N::translate('You must select an individual and a chart type in the block preferences');
1408c2e8227SGreg Roach        }
1418c2e8227SGreg Roach
1426a8879feSGreg Roach        if ($ctype !== '') {
143e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
14402fe1c37SGreg Roach                $config_url = route('tree-page-block-edit', [
14502fe1c37SGreg Roach                    'block_id' => $block_id,
146aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
14702fe1c37SGreg Roach                ]);
148397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
14902fe1c37SGreg Roach                $config_url = route('user-page-block-edit', [
15002fe1c37SGreg Roach                    'block_id' => $block_id,
151aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
15202fe1c37SGreg Roach                ]);
1539c6524dcSGreg Roach            } else {
1549c6524dcSGreg Roach                $config_url = '';
1559c6524dcSGreg Roach            }
1569c6524dcSGreg Roach
157147e99aaSGreg Roach            return view('modules/block-template', [
158*26684e68SGreg Roach                'block'      => str_replace('_', '-', $this->name()),
1599c6524dcSGreg Roach                'id'         => $block_id,
1609c6524dcSGreg Roach                'config_url' => $config_url,
161ea46934eSGreg Roach                'title'      => strip_tags($title),
1629c6524dcSGreg Roach                'content'    => $content,
1639c6524dcSGreg Roach            ]);
1648c2e8227SGreg Roach        }
165b2ce94c6SRico Sonntag
166b2ce94c6SRico Sonntag        return $content;
1678c2e8227SGreg Roach    }
1688c2e8227SGreg Roach
1698c2e8227SGreg Roach    /** {@inheritdoc} */
170c1010edaSGreg Roach    public function loadAjax(): bool
171c1010edaSGreg Roach    {
1728c2e8227SGreg Roach        return true;
1738c2e8227SGreg Roach    }
1748c2e8227SGreg Roach
1758c2e8227SGreg Roach    /** {@inheritdoc} */
176c1010edaSGreg Roach    public function isUserBlock(): bool
177c1010edaSGreg Roach    {
1788c2e8227SGreg Roach        return true;
1798c2e8227SGreg Roach    }
1808c2e8227SGreg Roach
1818c2e8227SGreg Roach    /** {@inheritdoc} */
182c1010edaSGreg Roach    public function isGedcomBlock(): bool
183c1010edaSGreg Roach    {
1848c2e8227SGreg Roach        return true;
1858c2e8227SGreg Roach    }
1868c2e8227SGreg Roach
18776692c8bSGreg Roach    /**
188a45f9889SGreg Roach     * Update the configuration for a block.
189a45f9889SGreg Roach     *
190a45f9889SGreg Roach     * @param Request $request
191a45f9889SGreg Roach     * @param int     $block_id
192a45f9889SGreg Roach     *
193a45f9889SGreg Roach     * @return void
194a45f9889SGreg Roach     */
195a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
196a45f9889SGreg Roach    {
197a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'type', $request->get('type', 'pedigree'));
198a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'pid', $request->get('pid', ''));
199a45f9889SGreg Roach    }
200a45f9889SGreg Roach
201a45f9889SGreg Roach    /**
20276692c8bSGreg Roach     * An HTML form to edit block settings
20376692c8bSGreg Roach     *
204e490cd80SGreg Roach     * @param Tree $tree
20576692c8bSGreg Roach     * @param int  $block_id
206a9430be8SGreg Roach     *
207988d5e9cSGreg Roach     * @return void
20876692c8bSGreg Roach     */
209a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
210c1010edaSGreg Roach    {
211e490cd80SGreg Roach        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
212e490cd80SGreg Roach        $gedcomid         = $tree->getUserPreference(Auth::user(), 'gedcomid');
2138c2e8227SGreg Roach
214e2a378d3SGreg Roach        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
215bd44f43fSGreg Roach        $pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
2168c2e8227SGreg Roach
21713abd6f3SGreg Roach        $charts = [
218fddfbb14SGreg Roach            'pedigree'    => I18N::translate('Pedigree'),
219fddfbb14SGreg Roach            'descendants' => I18N::translate('Descendants'),
220fddfbb14SGreg Roach            'hourglass'   => I18N::translate('Hourglass chart'),
221fddfbb14SGreg Roach            'treenav'     => I18N::translate('Interactive tree'),
22213abd6f3SGreg Roach        ];
223fddfbb14SGreg Roach        uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp');
224fddfbb14SGreg Roach
225e490cd80SGreg Roach        $individual = Individual::getInstance($pid, $tree);
226c385536dSGreg Roach
227147e99aaSGreg Roach        echo view('modules/charts/config', [
228c385536dSGreg Roach            'charts'     => $charts,
229c385536dSGreg Roach            'individual' => $individual,
230e490cd80SGreg Roach            'tree'       => $tree,
231c385536dSGreg Roach            'type'       => $type,
232c385536dSGreg Roach        ]);
2338c2e8227SGreg Roach    }
2348c2e8227SGreg Roach}
235