xref: /webtrees/app/Module/ChartsBlockModule.php (revision 41f309de4fcf9e448c44b57cedac2c0e0ec4b986)
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;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
244ca7e03cSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
25e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
261e7a7a28SGreg Roachuse Illuminate\Support\Str;
276ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
288c2e8227SGreg Roach
298c2e8227SGreg Roach/**
308c2e8227SGreg Roach * Class ChartsBlockModule
318c2e8227SGreg Roach */
3237eb8894SGreg Roachclass ChartsBlockModule extends AbstractModule implements ModuleBlockInterface
33c1010edaSGreg Roach{
3449a243cbSGreg Roach    use ModuleBlockTrait;
3549a243cbSGreg Roach
36961ec755SGreg Roach    /**
374ca7e03cSGreg Roach     * @var ModuleService
384ca7e03cSGreg Roach     */
394ca7e03cSGreg Roach    private $module_service;
404ca7e03cSGreg Roach
414ca7e03cSGreg Roach    /**
424ca7e03cSGreg Roach     * ChartsBlockModule constructor.
434ca7e03cSGreg Roach     *
444ca7e03cSGreg Roach     * @param ModuleService $module_service
454ca7e03cSGreg Roach     */
465bdbe281SGreg Roach    public function __construct(ModuleService $module_service)
475bdbe281SGreg Roach    {
484ca7e03cSGreg Roach        $this->module_service = $module_service;
494ca7e03cSGreg Roach    }
504ca7e03cSGreg Roach
514ca7e03cSGreg Roach    /**
520cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
53961ec755SGreg Roach     *
54961ec755SGreg Roach     * @return string
55961ec755SGreg Roach     */
5649a243cbSGreg Roach    public function title(): string
57c1010edaSGreg Roach    {
58bbb76c12SGreg Roach        /* I18N: Name of a module/block */
59bbb76c12SGreg Roach        return I18N::translate('Charts');
608c2e8227SGreg Roach    }
618c2e8227SGreg Roach
62961ec755SGreg Roach    /**
63961ec755SGreg Roach     * A sentence describing what this module does.
64961ec755SGreg Roach     *
65961ec755SGreg Roach     * @return string
66961ec755SGreg Roach     */
6749a243cbSGreg Roach    public function description(): string
68c1010edaSGreg Roach    {
69bbb76c12SGreg Roach        /* I18N: Description of the “Charts” module */
70bbb76c12SGreg Roach        return I18N::translate('An alternative way to display charts.');
718c2e8227SGreg Roach    }
728c2e8227SGreg Roach
7376692c8bSGreg Roach    /**
7476692c8bSGreg Roach     * Generate the HTML content of this block.
7576692c8bSGreg Roach     *
76e490cd80SGreg Roach     * @param Tree     $tree
7776692c8bSGreg Roach     * @param int      $block_id
785f2ae573SGreg Roach     * @param string   $ctype
79727f238cSGreg Roach     * @param string[] $cfg
8076692c8bSGreg Roach     *
8176692c8bSGreg Roach     * @return string
8276692c8bSGreg Roach     */
835f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
84c1010edaSGreg Roach    {
85e490cd80SGreg Roach        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
86e490cd80SGreg Roach        $gedcomid         = $tree->getUserPreference(Auth::user(), 'gedcomid');
878c2e8227SGreg Roach
88e2a378d3SGreg Roach        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
89bd44f43fSGreg Roach        $pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
908c2e8227SGreg Roach
91c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
928c2e8227SGreg Roach
93e490cd80SGreg Roach        $person = Individual::getInstance($pid, $tree);
948c2e8227SGreg Roach        if (!$person) {
958c2e8227SGreg Roach            $pid = $PEDIGREE_ROOT_ID;
96e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'pid', $pid);
97e490cd80SGreg Roach            $person = Individual::getInstance($pid, $tree);
988c2e8227SGreg Roach        }
998c2e8227SGreg Roach
10049a243cbSGreg Roach        $title = $this->title();
1018c2e8227SGreg Roach
1028c2e8227SGreg Roach        if ($person) {
1038c2e8227SGreg Roach            switch ($type) {
104677aaceaSGreg Roach                default:
1058c2e8227SGreg Roach                case 'pedigree':
106d7f3769dSGreg Roach                    /** @var PedigreeChartModule $module */
107ed9ba438SGreg Roach                    $module    = $this->module_service->findByInterface(PedigreeChartModule::class)->first();
108677aaceaSGreg Roach                    $title     = $module->chartTitle($person);
109677aaceaSGreg Roach                    $chart_url = $module->chartUrl($person, [
1109b5537c3SGreg Roach                        'ajax'        => true,
11102fe1c37SGreg Roach                        'generations' => 3,
112aa7659ebSGreg Roach                        'layout'      => PedigreeChartModule::ORIENTATION_RIGHT,
11302fe1c37SGreg Roach                    ]);
114147e99aaSGreg Roach                    $content   = view('modules/charts/chart', [
11502fe1c37SGreg Roach                        'block_id'  => $block_id,
11602fe1c37SGreg Roach                        'chart_url' => $chart_url,
117*41f309deSGreg Roach                        'class'     => 'wt-chart-pedigree',
11802fe1c37SGreg Roach                    ]);
1198c2e8227SGreg Roach                    break;
120677aaceaSGreg Roach
1218c2e8227SGreg Roach                case 'descendants':
122d7f3769dSGreg Roach                    /** @var DescendancyChartModule $module */
123ed9ba438SGreg Roach                    $module    = $this->module_service->findByInterface(DescendancyChartModule::class)->first();
124677aaceaSGreg Roach                    $title     = $module->chartTitle($person);
125677aaceaSGreg Roach                    $chart_url = $module->chartUrl($person, [
1269b5537c3SGreg Roach                        'ajax'        => true,
12702fe1c37SGreg Roach                        'generations' => 2,
1280f1e0f10SGreg Roach                        'chart_style' => DescendancyChartModule::CHART_STYLE_TREE,
12902fe1c37SGreg Roach                    ]);
130147e99aaSGreg Roach                    $content   = view('modules/charts/chart', [
13102fe1c37SGreg Roach                        'block_id'  => $block_id,
13202fe1c37SGreg Roach                        'chart_url' => $chart_url,
133*41f309deSGreg Roach                        'class'     => 'wt-chart-descendants',
13402fe1c37SGreg Roach                    ]);
1358c2e8227SGreg Roach                    break;
136677aaceaSGreg Roach
1378c2e8227SGreg Roach                case 'hourglass':
138d7f3769dSGreg Roach                    /** @var HourglassChartModule $module */
139ed9ba438SGreg Roach                    $module    = $this->module_service->findByInterface(HourglassChartModule::class)->first();
140677aaceaSGreg Roach                    $title     = $module->chartTitle($person);
141677aaceaSGreg Roach                    $chart_url = $module->chartUrl($person, [
1429b5537c3SGreg Roach                        'ajax'        => true,
14302fe1c37SGreg Roach                        'generations' => 2,
14402fe1c37SGreg Roach                    ]);
145147e99aaSGreg Roach                    $content   = view('modules/charts/chart', [
14602fe1c37SGreg Roach                        'block_id'  => $block_id,
14702fe1c37SGreg Roach                        'chart_url' => $chart_url,
148*41f309deSGreg Roach                        'class'     => 'wt-chart-hourglass',
14902fe1c37SGreg Roach                    ]);
1508c2e8227SGreg Roach                    break;
151677aaceaSGreg Roach
1528c2e8227SGreg Roach                case 'treenav':
153d7f3769dSGreg Roach                    /** @var InteractiveTreeModule $module */
154*41f309deSGreg Roach                    $module  = $this->module_service->findByInterface(InteractiveTreeModule::class)->first();
15539ca88baSGreg Roach                    $title   = I18N::translate('Interactive tree of %s', $person->fullName());
15659f2f229SGreg Roach                    $tv      = new TreeView();
157677aaceaSGreg Roach                    $content = '<script>$("head").append(\'<link rel="stylesheet" href="' . $module->css() . '" type="text/css" />\');</script>';
158677aaceaSGreg Roach                    $content .= '<script src="' . $module->js() . '"></script>';
15965e02381SGreg Roach                    [$html, $js] = $tv->drawViewport($person, 2);
1608c2e8227SGreg Roach                    $content .= $html . '<script>' . $js . '</script>';
1618c2e8227SGreg Roach                    break;
1628c2e8227SGreg Roach            }
1638c2e8227SGreg Roach        } else {
1646e7bf391SGreg Roach            $content = I18N::translate('You must select an individual and a chart type in the block preferences');
1658c2e8227SGreg Roach        }
1668c2e8227SGreg Roach
1676a8879feSGreg Roach        if ($ctype !== '') {
168e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
16902fe1c37SGreg Roach                $config_url = route('tree-page-block-edit', [
17002fe1c37SGreg Roach                    'block_id' => $block_id,
171aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
17202fe1c37SGreg Roach                ]);
173397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
17402fe1c37SGreg Roach                $config_url = route('user-page-block-edit', [
17502fe1c37SGreg Roach                    'block_id' => $block_id,
176aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
17702fe1c37SGreg Roach                ]);
1789c6524dcSGreg Roach            } else {
1799c6524dcSGreg Roach                $config_url = '';
1809c6524dcSGreg Roach            }
1819c6524dcSGreg Roach
182147e99aaSGreg Roach            return view('modules/block-template', [
1831e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1849c6524dcSGreg Roach                'id'         => $block_id,
1859c6524dcSGreg Roach                'config_url' => $config_url,
186ea46934eSGreg Roach                'title'      => strip_tags($title),
1879c6524dcSGreg Roach                'content'    => $content,
1889c6524dcSGreg Roach            ]);
1898c2e8227SGreg Roach        }
190b2ce94c6SRico Sonntag
191b2ce94c6SRico Sonntag        return $content;
1928c2e8227SGreg Roach    }
1938c2e8227SGreg Roach
1948c2e8227SGreg Roach    /** {@inheritdoc} */
195c1010edaSGreg Roach    public function loadAjax(): bool
196c1010edaSGreg Roach    {
1978c2e8227SGreg Roach        return true;
1988c2e8227SGreg Roach    }
1998c2e8227SGreg Roach
2008c2e8227SGreg Roach    /** {@inheritdoc} */
201c1010edaSGreg Roach    public function isUserBlock(): bool
202c1010edaSGreg Roach    {
2038c2e8227SGreg Roach        return true;
2048c2e8227SGreg Roach    }
2058c2e8227SGreg Roach
2068c2e8227SGreg Roach    /** {@inheritdoc} */
20763276d8fSGreg Roach    public function isTreeBlock(): bool
208c1010edaSGreg Roach    {
2098c2e8227SGreg Roach        return true;
2108c2e8227SGreg Roach    }
2118c2e8227SGreg Roach
21276692c8bSGreg Roach    /**
213a45f9889SGreg Roach     * Update the configuration for a block.
214a45f9889SGreg Roach     *
2156ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
216a45f9889SGreg Roach     * @param int     $block_id
217a45f9889SGreg Roach     *
218a45f9889SGreg Roach     * @return void
219a45f9889SGreg Roach     */
2206ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
221a45f9889SGreg Roach    {
222a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'type', $request->get('type', 'pedigree'));
223a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'pid', $request->get('pid', ''));
224a45f9889SGreg Roach    }
225a45f9889SGreg Roach
226a45f9889SGreg Roach    /**
22776692c8bSGreg Roach     * An HTML form to edit block settings
22876692c8bSGreg Roach     *
229e490cd80SGreg Roach     * @param Tree $tree
23076692c8bSGreg Roach     * @param int  $block_id
231a9430be8SGreg Roach     *
232988d5e9cSGreg Roach     * @return void
23376692c8bSGreg Roach     */
234e364afe4SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): void
235c1010edaSGreg Roach    {
236e490cd80SGreg Roach        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
237e490cd80SGreg Roach        $gedcomid         = $tree->getUserPreference(Auth::user(), 'gedcomid');
2388c2e8227SGreg Roach
239e2a378d3SGreg Roach        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
240bd44f43fSGreg Roach        $pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
2418c2e8227SGreg Roach
24213abd6f3SGreg Roach        $charts = [
243fddfbb14SGreg Roach            'pedigree'    => I18N::translate('Pedigree'),
244fddfbb14SGreg Roach            'descendants' => I18N::translate('Descendants'),
245fddfbb14SGreg Roach            'hourglass'   => I18N::translate('Hourglass chart'),
246fddfbb14SGreg Roach            'treenav'     => I18N::translate('Interactive tree'),
24713abd6f3SGreg Roach        ];
248fddfbb14SGreg Roach        uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp');
249fddfbb14SGreg Roach
250e490cd80SGreg Roach        $individual = Individual::getInstance($pid, $tree);
251c385536dSGreg Roach
252147e99aaSGreg Roach        echo view('modules/charts/config', [
253c385536dSGreg Roach            'charts'     => $charts,
254c385536dSGreg Roach            'individual' => $individual,
255e490cd80SGreg Roach            'tree'       => $tree,
256c385536dSGreg Roach            'type'       => $type,
257c385536dSGreg Roach        ]);
2588c2e8227SGreg Roach    }
2598c2e8227SGreg Roach}
260