xref: /webtrees/app/Module/ChartsBlockModule.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
18c2e8227SGreg Roach<?php
2*3976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
168c2e8227SGreg Roach */
17e7f56f2aSGreg Roachdeclare(strict_types=1);
18e7f56f2aSGreg Roach
1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2076692c8bSGreg Roach
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
254ca7e03cSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
26e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
271e7a7a28SGreg Roachuse Illuminate\Support\Str;
286ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
298c2e8227SGreg Roach
308c2e8227SGreg Roach/**
318c2e8227SGreg Roach * Class ChartsBlockModule
328c2e8227SGreg Roach */
3337eb8894SGreg Roachclass ChartsBlockModule extends AbstractModule implements ModuleBlockInterface
34c1010edaSGreg Roach{
3549a243cbSGreg Roach    use ModuleBlockTrait;
3649a243cbSGreg Roach
37961ec755SGreg Roach    /**
384ca7e03cSGreg Roach     * @var ModuleService
394ca7e03cSGreg Roach     */
404ca7e03cSGreg Roach    private $module_service;
414ca7e03cSGreg Roach
424ca7e03cSGreg Roach    /**
434ca7e03cSGreg Roach     * ChartsBlockModule constructor.
444ca7e03cSGreg Roach     *
454ca7e03cSGreg Roach     * @param ModuleService $module_service
464ca7e03cSGreg Roach     */
475bdbe281SGreg Roach    public function __construct(ModuleService $module_service)
485bdbe281SGreg Roach    {
494ca7e03cSGreg Roach        $this->module_service = $module_service;
504ca7e03cSGreg Roach    }
514ca7e03cSGreg Roach
524ca7e03cSGreg Roach    /**
530cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
54961ec755SGreg Roach     *
55961ec755SGreg Roach     * @return string
56961ec755SGreg Roach     */
5749a243cbSGreg Roach    public function title(): string
58c1010edaSGreg Roach    {
59bbb76c12SGreg Roach        /* I18N: Name of a module/block */
60bbb76c12SGreg Roach        return I18N::translate('Charts');
618c2e8227SGreg Roach    }
628c2e8227SGreg Roach
63961ec755SGreg Roach    /**
64961ec755SGreg Roach     * A sentence describing what this module does.
65961ec755SGreg Roach     *
66961ec755SGreg Roach     * @return string
67961ec755SGreg Roach     */
6849a243cbSGreg Roach    public function description(): string
69c1010edaSGreg Roach    {
70bbb76c12SGreg Roach        /* I18N: Description of the “Charts” module */
71bbb76c12SGreg Roach        return I18N::translate('An alternative way to display charts.');
728c2e8227SGreg Roach    }
738c2e8227SGreg Roach
7476692c8bSGreg Roach    /**
7576692c8bSGreg Roach     * Generate the HTML content of this block.
7676692c8bSGreg Roach     *
77e490cd80SGreg Roach     * @param Tree     $tree
7876692c8bSGreg Roach     * @param int      $block_id
793caaa4d2SGreg Roach     * @param string   $context
803caaa4d2SGreg Roach     * @param string[] $config
8176692c8bSGreg Roach     *
8276692c8bSGreg Roach     * @return string
8376692c8bSGreg Roach     */
843caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
85c1010edaSGreg Roach    {
86e490cd80SGreg Roach        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
87e490cd80SGreg Roach        $gedcomid         = $tree->getUserPreference(Auth::user(), 'gedcomid');
88575707d1SGreg Roach        $default_xref     = $gedcomid ?: $PEDIGREE_ROOT_ID;
898c2e8227SGreg Roach
90e2a378d3SGreg Roach        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
91575707d1SGreg Roach        $xref = $this->getBlockSetting($block_id, 'pid', $default_xref);
928c2e8227SGreg Roach
933caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
948c2e8227SGreg Roach
95575707d1SGreg Roach        $individual = Individual::getInstance($xref, $tree);
968c2e8227SGreg Roach
9749a243cbSGreg Roach        $title = $this->title();
988c2e8227SGreg Roach
99575707d1SGreg Roach        if ($individual instanceof Individual) {
1008c2e8227SGreg Roach            switch ($type) {
101677aaceaSGreg Roach                default:
1028c2e8227SGreg Roach                case 'pedigree':
103d7f3769dSGreg Roach                    /** @var PedigreeChartModule $module */
104ed9ba438SGreg Roach                    $module    = $this->module_service->findByInterface(PedigreeChartModule::class)->first();
105575707d1SGreg Roach                    $title     = $module->chartTitle($individual);
106575707d1SGreg Roach                    $chart_url = $module->chartUrl($individual, [
1079b5537c3SGreg Roach                        'ajax'        => true,
10802fe1c37SGreg Roach                        'generations' => 3,
109aa7659ebSGreg Roach                        'layout'      => PedigreeChartModule::ORIENTATION_RIGHT,
11002fe1c37SGreg Roach                    ]);
111147e99aaSGreg Roach                    $content   = view('modules/charts/chart', [
11202fe1c37SGreg Roach                        'block_id'  => $block_id,
11302fe1c37SGreg Roach                        'chart_url' => $chart_url,
11441f309deSGreg Roach                        'class'     => 'wt-chart-pedigree',
11502fe1c37SGreg Roach                    ]);
1168c2e8227SGreg Roach                    break;
117677aaceaSGreg Roach
1188c2e8227SGreg Roach                case 'descendants':
119d7f3769dSGreg Roach                    /** @var DescendancyChartModule $module */
120ed9ba438SGreg Roach                    $module    = $this->module_service->findByInterface(DescendancyChartModule::class)->first();
121575707d1SGreg Roach                    $title     = $module->chartTitle($individual);
122575707d1SGreg Roach                    $chart_url = $module->chartUrl($individual, [
1239b5537c3SGreg Roach                        'ajax'        => true,
12402fe1c37SGreg Roach                        'generations' => 2,
1250f1e0f10SGreg Roach                        'chart_style' => DescendancyChartModule::CHART_STYLE_TREE,
12602fe1c37SGreg Roach                    ]);
127147e99aaSGreg Roach                    $content   = view('modules/charts/chart', [
12802fe1c37SGreg Roach                        'block_id'  => $block_id,
12902fe1c37SGreg Roach                        'chart_url' => $chart_url,
13041f309deSGreg Roach                        'class'     => 'wt-chart-descendants',
13102fe1c37SGreg Roach                    ]);
1328c2e8227SGreg Roach                    break;
133677aaceaSGreg Roach
1348c2e8227SGreg Roach                case 'hourglass':
135d7f3769dSGreg Roach                    /** @var HourglassChartModule $module */
136ed9ba438SGreg Roach                    $module    = $this->module_service->findByInterface(HourglassChartModule::class)->first();
137575707d1SGreg Roach                    $title     = $module->chartTitle($individual);
138575707d1SGreg Roach                    $chart_url = $module->chartUrl($individual, [
1399b5537c3SGreg Roach                        'ajax'        => true,
14002fe1c37SGreg Roach                        'generations' => 2,
14102fe1c37SGreg Roach                    ]);
142147e99aaSGreg Roach                    $content   = view('modules/charts/chart', [
14302fe1c37SGreg Roach                        'block_id'  => $block_id,
14402fe1c37SGreg Roach                        'chart_url' => $chart_url,
14541f309deSGreg Roach                        'class'     => 'wt-chart-hourglass',
14602fe1c37SGreg Roach                    ]);
1478c2e8227SGreg Roach                    break;
148677aaceaSGreg Roach
1498c2e8227SGreg Roach                case 'treenav':
150d7f3769dSGreg Roach                    /** @var InteractiveTreeModule $module */
15141f309deSGreg Roach                    $module = $this->module_service->findByInterface(InteractiveTreeModule::class)->first();
152575707d1SGreg Roach                    $title  = I18N::translate('Interactive tree of %s', $individual->fullName());
15359f2f229SGreg Roach                    $tv     = new TreeView();
154575707d1SGreg Roach                    [$html, $js] = $tv->drawViewport($individual, 2);
155575707d1SGreg Roach                    $content = $html . '<script>' . $js . '</script>';
1568c2e8227SGreg Roach                    break;
1578c2e8227SGreg Roach            }
1588c2e8227SGreg Roach        } else {
1596e7bf391SGreg Roach            $content = I18N::translate('You must select an individual and a chart type in the block preferences');
1608c2e8227SGreg Roach        }
1618c2e8227SGreg Roach
1623caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
163147e99aaSGreg Roach            return view('modules/block-template', [
1641e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1659c6524dcSGreg Roach                'id'         => $block_id,
1663caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
167ea46934eSGreg Roach                'title'      => strip_tags($title),
1689c6524dcSGreg Roach                'content'    => $content,
1699c6524dcSGreg Roach            ]);
1708c2e8227SGreg Roach        }
171b2ce94c6SRico Sonntag
172b2ce94c6SRico Sonntag        return $content;
1738c2e8227SGreg Roach    }
1748c2e8227SGreg Roach
1753caaa4d2SGreg Roach    /**
1763caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
1773caaa4d2SGreg Roach     *
1783caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
1793caaa4d2SGreg Roach     *
1803caaa4d2SGreg Roach     * @return bool
1813caaa4d2SGreg Roach     */
182c1010edaSGreg Roach    public function loadAjax(): bool
183c1010edaSGreg Roach    {
1848c2e8227SGreg Roach        return true;
1858c2e8227SGreg Roach    }
1868c2e8227SGreg Roach
1873caaa4d2SGreg Roach    /**
1883caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
1893caaa4d2SGreg Roach     *
1903caaa4d2SGreg Roach     * @return bool
1913caaa4d2SGreg Roach     */
192c1010edaSGreg Roach    public function isUserBlock(): bool
193c1010edaSGreg Roach    {
1948c2e8227SGreg Roach        return true;
1958c2e8227SGreg Roach    }
1968c2e8227SGreg Roach
1973caaa4d2SGreg Roach    /**
1983caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
1993caaa4d2SGreg Roach     *
2003caaa4d2SGreg Roach     * @return bool
2013caaa4d2SGreg Roach     */
20263276d8fSGreg Roach    public function isTreeBlock(): bool
203c1010edaSGreg Roach    {
2048c2e8227SGreg Roach        return true;
2058c2e8227SGreg Roach    }
2068c2e8227SGreg Roach
20776692c8bSGreg Roach    /**
208a45f9889SGreg Roach     * Update the configuration for a block.
209a45f9889SGreg Roach     *
2106ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
211a45f9889SGreg Roach     * @param int     $block_id
212a45f9889SGreg Roach     *
213a45f9889SGreg Roach     * @return void
214a45f9889SGreg Roach     */
2156ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
216a45f9889SGreg Roach    {
217575707d1SGreg Roach        $this->setBlockSetting($block_id, 'type', $request->getParsedBody()['type'] ?? 'pedigree');
218575707d1SGreg Roach        $this->setBlockSetting($block_id, 'pid', $request->getParsedBody()['xref'] ?? '');
219a45f9889SGreg Roach    }
220a45f9889SGreg Roach
221a45f9889SGreg Roach    /**
22276692c8bSGreg Roach     * An HTML form to edit block settings
22376692c8bSGreg Roach     *
224e490cd80SGreg Roach     * @param Tree $tree
22576692c8bSGreg Roach     * @param int  $block_id
226a9430be8SGreg Roach     *
2273caaa4d2SGreg Roach     * @return string
22876692c8bSGreg Roach     */
2293caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
230c1010edaSGreg Roach    {
231e490cd80SGreg Roach        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
232e490cd80SGreg Roach        $gedcomid         = $tree->getUserPreference(Auth::user(), 'gedcomid');
233575707d1SGreg Roach        $default_xref     = $gedcomid ?: $PEDIGREE_ROOT_ID;
2348c2e8227SGreg Roach
235e2a378d3SGreg Roach        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
236575707d1SGreg Roach        $xref  = $this->getBlockSetting($block_id, 'pid', $default_xref);
2378c2e8227SGreg Roach
23813abd6f3SGreg Roach        $charts = [
239fddfbb14SGreg Roach            'pedigree'    => I18N::translate('Pedigree'),
240fddfbb14SGreg Roach            'descendants' => I18N::translate('Descendants'),
241fddfbb14SGreg Roach            'hourglass'   => I18N::translate('Hourglass chart'),
242fddfbb14SGreg Roach            'treenav'     => I18N::translate('Interactive tree'),
24313abd6f3SGreg Roach        ];
244fddfbb14SGreg Roach        uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp');
245fddfbb14SGreg Roach
246575707d1SGreg Roach        $individual = Individual::getInstance($xref, $tree);
247c385536dSGreg Roach
2483caaa4d2SGreg Roach        return view('modules/charts/config', [
249c385536dSGreg Roach            'charts'     => $charts,
250c385536dSGreg Roach            'individual' => $individual,
251e490cd80SGreg Roach            'tree'       => $tree,
252c385536dSGreg Roach            'type'       => $type,
253c385536dSGreg Roach        ]);
2548c2e8227SGreg Roach    }
2558c2e8227SGreg Roach}
256