xref: /webtrees/app/Module/ChartsBlockModule.php (revision f4afa648fcb1eff2558e414d35a921dbfbf02078)
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;
26a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class ChartsBlockModule
308c2e8227SGreg Roach */
31c1010edaSGreg Roachclass ChartsBlockModule extends AbstractModule implements ModuleBlockInterface
32c1010edaSGreg Roach{
338c2e8227SGreg Roach    /** {@inheritdoc} */
348f53f488SRico Sonntag    public function getTitle(): string
35c1010edaSGreg Roach    {
36bbb76c12SGreg Roach        /* I18N: Name of a module/block */
37bbb76c12SGreg Roach        return I18N::translate('Charts');
388c2e8227SGreg Roach    }
398c2e8227SGreg Roach
408c2e8227SGreg Roach    /** {@inheritdoc} */
418f53f488SRico Sonntag    public function getDescription(): string
42c1010edaSGreg Roach    {
43bbb76c12SGreg Roach        /* I18N: Description of the “Charts” module */
44bbb76c12SGreg Roach        return I18N::translate('An alternative way to display charts.');
458c2e8227SGreg Roach    }
468c2e8227SGreg Roach
4776692c8bSGreg Roach    /**
4876692c8bSGreg Roach     * Generate the HTML content of this block.
4976692c8bSGreg Roach     *
50e490cd80SGreg Roach     * @param Tree     $tree
5176692c8bSGreg Roach     * @param int      $block_id
5276692c8bSGreg Roach     * @param bool     $template
53727f238cSGreg Roach     * @param string[] $cfg
5476692c8bSGreg Roach     *
5576692c8bSGreg Roach     * @return string
5676692c8bSGreg Roach     */
57c1010edaSGreg Roach    public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string
58c1010edaSGreg Roach    {
59e490cd80SGreg Roach        global $ctype;
608c2e8227SGreg Roach
61e490cd80SGreg Roach        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
62e490cd80SGreg Roach        $gedcomid         = $tree->getUserPreference(Auth::user(), 'gedcomid');
638c2e8227SGreg Roach
64e2a378d3SGreg Roach        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
65bd44f43fSGreg Roach        $pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
668c2e8227SGreg Roach
67c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
688c2e8227SGreg Roach
69e490cd80SGreg Roach        $person = Individual::getInstance($pid, $tree);
708c2e8227SGreg Roach        if (!$person) {
718c2e8227SGreg Roach            $pid = $PEDIGREE_ROOT_ID;
72e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'pid', $pid);
73e490cd80SGreg Roach            $person = Individual::getInstance($pid, $tree);
748c2e8227SGreg Roach        }
758c2e8227SGreg Roach
769c6524dcSGreg Roach        $title = $this->getTitle();
778c2e8227SGreg Roach
788c2e8227SGreg Roach        if ($person) {
79f679d3acSGreg Roach            $content = '';
808c2e8227SGreg Roach            switch ($type) {
818c2e8227SGreg Roach                case 'pedigree':
829c6524dcSGreg Roach                    $title     = I18N::translate('Pedigree of %s', $person->getFullName());
8302fe1c37SGreg Roach                    $chart_url = route('pedigree-chart', [
84c0935879SGreg Roach                        'xref'        => $person->xref(),
85*f4afa648SGreg Roach                        'ged'         => $person->tree()->name(),
8602fe1c37SGreg Roach                        'generations' => 3,
8702fe1c37SGreg Roach                        'layout'      => PedigreeChartController::PORTRAIT,
8802fe1c37SGreg Roach                    ]);
89147e99aaSGreg Roach                    $content = view('modules/charts/chart', [
9002fe1c37SGreg Roach                        'block_id'  => $block_id,
9102fe1c37SGreg Roach                        'chart_url' => $chart_url,
9202fe1c37SGreg Roach                    ]);
938c2e8227SGreg Roach                    break;
948c2e8227SGreg Roach                case 'descendants':
959c6524dcSGreg Roach                    $title     = I18N::translate('Descendants of %s', $person->getFullName());
9602fe1c37SGreg Roach                    $chart_url = route('descendants-chart', [
97c0935879SGreg Roach                        'xref'        => $person->xref(),
98*f4afa648SGreg Roach                        'ged'         => $person->tree()->name(),
9902fe1c37SGreg Roach                        'generations' => 2,
10002fe1c37SGreg Roach                        'chart_style' => 0,
10102fe1c37SGreg Roach                    ]);
102147e99aaSGreg Roach                    $content = view('modules/charts/chart', [
10302fe1c37SGreg Roach                        'block_id'  => $block_id,
10402fe1c37SGreg Roach                        'chart_url' => $chart_url,
10502fe1c37SGreg Roach                    ]);
1068c2e8227SGreg Roach                    break;
1078c2e8227SGreg Roach                case 'hourglass':
1089c6524dcSGreg Roach                    $title     = I18N::translate('Hourglass chart of %s', $person->getFullName());
10902fe1c37SGreg Roach                    $chart_url = route('hourglass-chart', [
110c0935879SGreg Roach                        'xref'        => $person->xref(),
111*f4afa648SGreg Roach                        'ged'         => $person->tree()->name(),
11202fe1c37SGreg Roach                        'generations' => 2,
11302fe1c37SGreg Roach                        'layout'      => PedigreeChartController::PORTRAIT,
11402fe1c37SGreg Roach                    ]);
115147e99aaSGreg Roach                    $content = view('modules/charts/chart', [
11602fe1c37SGreg Roach                        'block_id'  => $block_id,
11702fe1c37SGreg Roach                        'chart_url' => $chart_url,
11802fe1c37SGreg Roach                    ]);
1198c2e8227SGreg Roach                    break;
1208c2e8227SGreg Roach                case 'treenav':
1219c6524dcSGreg Roach                    $title   = I18N::translate('Interactive tree of %s', $person->getFullName());
1228c2e8227SGreg Roach                    $mod     = new InteractiveTreeModule(WT_MODULES_DIR . 'tree');
12359f2f229SGreg Roach                    $tv      = new TreeView();
124564ae2d7SGreg Roach                    $content .= '<script>$("head").append(\'<link rel="stylesheet" href="' . $mod->css() . '" type="text/css" />\');</script>';
1258c2e8227SGreg Roach                    $content .= '<script src="' . $mod->js() . '"></script>';
1268c2e8227SGreg Roach                    list($html, $js) = $tv->drawViewport($person, 2);
1278c2e8227SGreg Roach                    $content .= $html . '<script>' . $js . '</script>';
1288c2e8227SGreg Roach                    break;
1298c2e8227SGreg Roach            }
1308c2e8227SGreg Roach        } else {
1316e7bf391SGreg Roach            $content = I18N::translate('You must select an individual and a chart type in the block preferences');
1328c2e8227SGreg Roach        }
1338c2e8227SGreg Roach
1348c2e8227SGreg Roach        if ($template) {
135e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
13602fe1c37SGreg Roach                $config_url = route('tree-page-block-edit', [
13702fe1c37SGreg Roach                    'block_id' => $block_id,
138aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
13902fe1c37SGreg Roach                ]);
140397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
14102fe1c37SGreg Roach                $config_url = route('user-page-block-edit', [
14202fe1c37SGreg Roach                    'block_id' => $block_id,
143aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
14402fe1c37SGreg Roach                ]);
1459c6524dcSGreg Roach            } else {
1469c6524dcSGreg Roach                $config_url = '';
1479c6524dcSGreg Roach            }
1489c6524dcSGreg Roach
149147e99aaSGreg Roach            return view('modules/block-template', [
1509c6524dcSGreg Roach                'block'      => str_replace('_', '-', $this->getName()),
1519c6524dcSGreg Roach                'id'         => $block_id,
1529c6524dcSGreg Roach                'config_url' => $config_url,
153ea46934eSGreg Roach                'title'      => strip_tags($title),
1549c6524dcSGreg Roach                'content'    => $content,
1559c6524dcSGreg Roach            ]);
1568c2e8227SGreg Roach        }
157b2ce94c6SRico Sonntag
158b2ce94c6SRico Sonntag        return $content;
1598c2e8227SGreg Roach    }
1608c2e8227SGreg Roach
1618c2e8227SGreg Roach    /** {@inheritdoc} */
162c1010edaSGreg Roach    public function loadAjax(): bool
163c1010edaSGreg Roach    {
1648c2e8227SGreg Roach        return true;
1658c2e8227SGreg Roach    }
1668c2e8227SGreg Roach
1678c2e8227SGreg Roach    /** {@inheritdoc} */
168c1010edaSGreg Roach    public function isUserBlock(): bool
169c1010edaSGreg Roach    {
1708c2e8227SGreg Roach        return true;
1718c2e8227SGreg Roach    }
1728c2e8227SGreg Roach
1738c2e8227SGreg Roach    /** {@inheritdoc} */
174c1010edaSGreg Roach    public function isGedcomBlock(): bool
175c1010edaSGreg Roach    {
1768c2e8227SGreg Roach        return true;
1778c2e8227SGreg Roach    }
1788c2e8227SGreg Roach
17976692c8bSGreg Roach    /**
180a45f9889SGreg Roach     * Update the configuration for a block.
181a45f9889SGreg Roach     *
182a45f9889SGreg Roach     * @param Request $request
183a45f9889SGreg Roach     * @param int     $block_id
184a45f9889SGreg Roach     *
185a45f9889SGreg Roach     * @return void
186a45f9889SGreg Roach     */
187a45f9889SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id)
188a45f9889SGreg Roach    {
189a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'type', $request->get('type', 'pedigree'));
190a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'pid', $request->get('pid', ''));
191a45f9889SGreg Roach    }
192a45f9889SGreg Roach
193a45f9889SGreg Roach    /**
19476692c8bSGreg Roach     * An HTML form to edit block settings
19576692c8bSGreg Roach     *
196e490cd80SGreg Roach     * @param Tree $tree
19776692c8bSGreg Roach     * @param int  $block_id
198a9430be8SGreg Roach     *
199988d5e9cSGreg Roach     * @return void
20076692c8bSGreg Roach     */
201a45f9889SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id)
202c1010edaSGreg Roach    {
203e490cd80SGreg Roach        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
204e490cd80SGreg Roach        $gedcomid         = $tree->getUserPreference(Auth::user(), 'gedcomid');
2058c2e8227SGreg Roach
206e2a378d3SGreg Roach        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
207bd44f43fSGreg Roach        $pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
2088c2e8227SGreg Roach
20913abd6f3SGreg Roach        $charts = [
210fddfbb14SGreg Roach            'pedigree'    => I18N::translate('Pedigree'),
211fddfbb14SGreg Roach            'descendants' => I18N::translate('Descendants'),
212fddfbb14SGreg Roach            'hourglass'   => I18N::translate('Hourglass chart'),
213fddfbb14SGreg Roach            'treenav'     => I18N::translate('Interactive tree'),
21413abd6f3SGreg Roach        ];
215fddfbb14SGreg Roach        uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp');
216fddfbb14SGreg Roach
217e490cd80SGreg Roach        $individual = Individual::getInstance($pid, $tree);
218c385536dSGreg Roach
219147e99aaSGreg Roach        echo view('modules/charts/config', [
220c385536dSGreg Roach            'charts'     => $charts,
221c385536dSGreg Roach            'individual' => $individual,
222e490cd80SGreg Roach            'tree'       => $tree,
223c385536dSGreg Roach            'type'       => $type,
224c385536dSGreg Roach        ]);
2258c2e8227SGreg Roach    }
2268c2e8227SGreg Roach}
227