xref: /webtrees/app/Module/ChartsBlockModule.php (revision 8d0ebef0d075981bd943e8256e2c81a3b1e92b4b)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 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;
26*8d0ebef0SGreg Roachuse Fisharebest\Webtrees\Webtrees;
27a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
288c2e8227SGreg Roach
298c2e8227SGreg Roach/**
308c2e8227SGreg Roach * Class ChartsBlockModule
318c2e8227SGreg Roach */
32c1010edaSGreg Roachclass ChartsBlockModule extends AbstractModule implements ModuleBlockInterface
33c1010edaSGreg Roach{
348c2e8227SGreg Roach    /** {@inheritdoc} */
358f53f488SRico Sonntag    public function getTitle(): string
36c1010edaSGreg Roach    {
37bbb76c12SGreg Roach        /* I18N: Name of a module/block */
38bbb76c12SGreg Roach        return I18N::translate('Charts');
398c2e8227SGreg Roach    }
408c2e8227SGreg Roach
418c2e8227SGreg Roach    /** {@inheritdoc} */
428f53f488SRico Sonntag    public function getDescription(): string
43c1010edaSGreg Roach    {
44bbb76c12SGreg Roach        /* I18N: Description of the “Charts” module */
45bbb76c12SGreg Roach        return I18N::translate('An alternative way to display charts.');
468c2e8227SGreg Roach    }
478c2e8227SGreg Roach
4876692c8bSGreg Roach    /**
4976692c8bSGreg Roach     * Generate the HTML content of this block.
5076692c8bSGreg Roach     *
51e490cd80SGreg Roach     * @param Tree     $tree
5276692c8bSGreg Roach     * @param int      $block_id
5376692c8bSGreg Roach     * @param bool     $template
54727f238cSGreg Roach     * @param string[] $cfg
5576692c8bSGreg Roach     *
5676692c8bSGreg Roach     * @return string
5776692c8bSGreg Roach     */
58c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
59c1010edaSGreg Roach    {
60e490cd80SGreg Roach        global $ctype;
618c2e8227SGreg Roach
62e490cd80SGreg Roach        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
63e490cd80SGreg Roach        $gedcomid         = $tree->getUserPreference(Auth::user(), 'gedcomid');
648c2e8227SGreg Roach
65e2a378d3SGreg Roach        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
66bd44f43fSGreg Roach        $pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
678c2e8227SGreg Roach
68c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
698c2e8227SGreg Roach
70e490cd80SGreg Roach        $person = Individual::getInstance($pid, $tree);
718c2e8227SGreg Roach        if (!$person) {
728c2e8227SGreg Roach            $pid = $PEDIGREE_ROOT_ID;
73e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'pid', $pid);
74e490cd80SGreg Roach            $person = Individual::getInstance($pid, $tree);
758c2e8227SGreg Roach        }
768c2e8227SGreg Roach
779c6524dcSGreg Roach        $title = $this->getTitle();
788c2e8227SGreg Roach
798c2e8227SGreg Roach        if ($person) {
80f679d3acSGreg Roach            $content = '';
818c2e8227SGreg Roach            switch ($type) {
828c2e8227SGreg Roach                case 'pedigree':
839c6524dcSGreg Roach                    $title     = I18N::translate('Pedigree of %s', $person->getFullName());
8402fe1c37SGreg Roach                    $chart_url = route('pedigree-chart', [
85c0935879SGreg Roach                        'xref'        => $person->xref(),
86f4afa648SGreg Roach                        'ged'         => $person->tree()->name(),
8702fe1c37SGreg Roach                        'generations' => 3,
8802fe1c37SGreg Roach                        'layout'      => PedigreeChartController::PORTRAIT,
8902fe1c37SGreg Roach                    ]);
90147e99aaSGreg Roach                    $content = view('modules/charts/chart', [
9102fe1c37SGreg Roach                        'block_id'  => $block_id,
9202fe1c37SGreg Roach                        'chart_url' => $chart_url,
9302fe1c37SGreg Roach                    ]);
948c2e8227SGreg Roach                    break;
958c2e8227SGreg Roach                case 'descendants':
969c6524dcSGreg Roach                    $title     = I18N::translate('Descendants of %s', $person->getFullName());
9702fe1c37SGreg Roach                    $chart_url = route('descendants-chart', [
98c0935879SGreg Roach                        'xref'        => $person->xref(),
99f4afa648SGreg Roach                        'ged'         => $person->tree()->name(),
10002fe1c37SGreg Roach                        'generations' => 2,
10102fe1c37SGreg Roach                        'chart_style' => 0,
10202fe1c37SGreg Roach                    ]);
103147e99aaSGreg Roach                    $content = view('modules/charts/chart', [
10402fe1c37SGreg Roach                        'block_id'  => $block_id,
10502fe1c37SGreg Roach                        'chart_url' => $chart_url,
10602fe1c37SGreg Roach                    ]);
1078c2e8227SGreg Roach                    break;
1088c2e8227SGreg Roach                case 'hourglass':
1099c6524dcSGreg Roach                    $title     = I18N::translate('Hourglass chart of %s', $person->getFullName());
11002fe1c37SGreg Roach                    $chart_url = route('hourglass-chart', [
111c0935879SGreg Roach                        'xref'        => $person->xref(),
112f4afa648SGreg Roach                        'ged'         => $person->tree()->name(),
11302fe1c37SGreg Roach                        'generations' => 2,
11402fe1c37SGreg Roach                        'layout'      => PedigreeChartController::PORTRAIT,
11502fe1c37SGreg Roach                    ]);
116147e99aaSGreg Roach                    $content = view('modules/charts/chart', [
11702fe1c37SGreg Roach                        'block_id'  => $block_id,
11802fe1c37SGreg Roach                        'chart_url' => $chart_url,
11902fe1c37SGreg Roach                    ]);
1208c2e8227SGreg Roach                    break;
1218c2e8227SGreg Roach                case 'treenav':
1229c6524dcSGreg Roach                    $title   = I18N::translate('Interactive tree of %s', $person->getFullName());
123*8d0ebef0SGreg Roach                    $mod     = new InteractiveTreeModule(Webtrees::MODULES_PATH . 'tree');
12459f2f229SGreg Roach                    $tv      = new TreeView();
125564ae2d7SGreg Roach                    $content .= '<script>$("head").append(\'<link rel="stylesheet" href="' . $mod->css() . '" type="text/css" />\');</script>';
1268c2e8227SGreg Roach                    $content .= '<script src="' . $mod->js() . '"></script>';
1278c2e8227SGreg Roach                    list($html, $js) = $tv->drawViewport($person, 2);
1288c2e8227SGreg Roach                    $content .= $html . '<script>' . $js . '</script>';
1298c2e8227SGreg Roach                    break;
1308c2e8227SGreg Roach            }
1318c2e8227SGreg Roach        } else {
1326e7bf391SGreg Roach            $content = I18N::translate('You must select an individual and a chart type in the block preferences');
1338c2e8227SGreg Roach        }
1348c2e8227SGreg Roach
1358c2e8227SGreg Roach        if ($template) {
136e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
13702fe1c37SGreg Roach                $config_url = route('tree-page-block-edit', [
13802fe1c37SGreg Roach                    'block_id' => $block_id,
139aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
14002fe1c37SGreg Roach                ]);
141397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
14202fe1c37SGreg Roach                $config_url = route('user-page-block-edit', [
14302fe1c37SGreg Roach                    'block_id' => $block_id,
144aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
14502fe1c37SGreg Roach                ]);
1469c6524dcSGreg Roach            } else {
1479c6524dcSGreg Roach                $config_url = '';
1489c6524dcSGreg Roach            }
1499c6524dcSGreg Roach
150147e99aaSGreg Roach            return view('modules/block-template', [
1519c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1529c6524dcSGreg Roach                'id'         => $block_id,
1539c6524dcSGreg Roach                'config_url' => $config_url,
154ea46934eSGreg Roach                'title'      => strip_tags($title),
1559c6524dcSGreg Roach                'content'    => $content,
1569c6524dcSGreg Roach            ]);
1578c2e8227SGreg Roach        }
158b2ce94c6SRico Sonntag
159b2ce94c6SRico Sonntag        return $content;
1608c2e8227SGreg Roach    }
1618c2e8227SGreg Roach
1628c2e8227SGreg Roach    /** {@inheritdoc} */
163c1010edaSGreg Roach    public function loadAjax(): bool
164c1010edaSGreg Roach    {
1658c2e8227SGreg Roach        return true;
1668c2e8227SGreg Roach    }
1678c2e8227SGreg Roach
1688c2e8227SGreg Roach    /** {@inheritdoc} */
169c1010edaSGreg Roach    public function isUserBlock(): bool
170c1010edaSGreg Roach    {
1718c2e8227SGreg Roach        return true;
1728c2e8227SGreg Roach    }
1738c2e8227SGreg Roach
1748c2e8227SGreg Roach    /** {@inheritdoc} */
175c1010edaSGreg Roach    public function isGedcomBlock(): bool
176c1010edaSGreg Roach    {
1778c2e8227SGreg Roach        return true;
1788c2e8227SGreg Roach    }
1798c2e8227SGreg Roach
18076692c8bSGreg Roach    /**
181a45f9889SGreg Roach     * Update the configuration for a block.
182a45f9889SGreg Roach     *
183a45f9889SGreg Roach     * @param Request $request
184a45f9889SGreg Roach     * @param int     $block_id
185a45f9889SGreg Roach     *
186a45f9889SGreg Roach     * @return void
187a45f9889SGreg Roach     */
188a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
189a45f9889SGreg Roach    {
190a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'type', $request->get('type', 'pedigree'));
191a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'pid', $request->get('pid', ''));
192a45f9889SGreg Roach    }
193a45f9889SGreg Roach
194a45f9889SGreg Roach    /**
19576692c8bSGreg Roach     * An HTML form to edit block settings
19676692c8bSGreg Roach     *
197e490cd80SGreg Roach     * @param Tree $tree
19876692c8bSGreg Roach     * @param int  $block_id
199a9430be8SGreg Roach     *
200988d5e9cSGreg Roach     * @return void
20176692c8bSGreg Roach     */
202a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
203c1010edaSGreg Roach    {
204e490cd80SGreg Roach        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
205e490cd80SGreg Roach        $gedcomid         = $tree->getUserPreference(Auth::user(), 'gedcomid');
2068c2e8227SGreg Roach
207e2a378d3SGreg Roach        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
208bd44f43fSGreg Roach        $pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
2098c2e8227SGreg Roach
21013abd6f3SGreg Roach        $charts = [
211fddfbb14SGreg Roach            'pedigree'    => I18N::translate('Pedigree'),
212fddfbb14SGreg Roach            'descendants' => I18N::translate('Descendants'),
213fddfbb14SGreg Roach            'hourglass'   => I18N::translate('Hourglass chart'),
214fddfbb14SGreg Roach            'treenav'     => I18N::translate('Interactive tree'),
21513abd6f3SGreg Roach        ];
216fddfbb14SGreg Roach        uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp');
217fddfbb14SGreg Roach
218e490cd80SGreg Roach        $individual = Individual::getInstance($pid, $tree);
219c385536dSGreg Roach
220147e99aaSGreg Roach        echo view('modules/charts/config', [
221c385536dSGreg Roach            'charts'     => $charts,
222c385536dSGreg Roach            'individual' => $individual,
223e490cd80SGreg Roach            'tree'       => $tree,
224c385536dSGreg Roach            'type'       => $type,
225c385536dSGreg Roach        ]);
2268c2e8227SGreg Roach    }
2278c2e8227SGreg Roach}
228