xref: /webtrees/app/Module/ChartsBlockModule.php (revision 575707d10f8c6929c3e1baa59b4fc51d1be11ae1)
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');
87*575707d1SGreg Roach        $default_xref     = $gedcomid ?: $PEDIGREE_ROOT_ID;
888c2e8227SGreg Roach
89e2a378d3SGreg Roach        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
90*575707d1SGreg Roach        $xref = $this->getBlockSetting($block_id, 'pid', $default_xref);
918c2e8227SGreg Roach
92c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
938c2e8227SGreg Roach
94*575707d1SGreg Roach        $individual = Individual::getInstance($xref, $tree);
958c2e8227SGreg Roach
9649a243cbSGreg Roach        $title = $this->title();
978c2e8227SGreg Roach
98*575707d1SGreg Roach        if ($individual instanceof Individual) {
998c2e8227SGreg Roach            switch ($type) {
100677aaceaSGreg Roach                default:
1018c2e8227SGreg Roach                case 'pedigree':
102d7f3769dSGreg Roach                    /** @var PedigreeChartModule $module */
103ed9ba438SGreg Roach                    $module    = $this->module_service->findByInterface(PedigreeChartModule::class)->first();
104*575707d1SGreg Roach                    $title     = $module->chartTitle($individual);
105*575707d1SGreg Roach                    $chart_url = $module->chartUrl($individual, [
1069b5537c3SGreg Roach                        'ajax'        => true,
10702fe1c37SGreg Roach                        'generations' => 3,
108aa7659ebSGreg Roach                        'layout'      => PedigreeChartModule::ORIENTATION_RIGHT,
10902fe1c37SGreg Roach                    ]);
110147e99aaSGreg Roach                    $content   = view('modules/charts/chart', [
11102fe1c37SGreg Roach                        'block_id'  => $block_id,
11202fe1c37SGreg Roach                        'chart_url' => $chart_url,
11341f309deSGreg Roach                        'class'     => 'wt-chart-pedigree',
11402fe1c37SGreg Roach                    ]);
1158c2e8227SGreg Roach                    break;
116677aaceaSGreg Roach
1178c2e8227SGreg Roach                case 'descendants':
118d7f3769dSGreg Roach                    /** @var DescendancyChartModule $module */
119ed9ba438SGreg Roach                    $module    = $this->module_service->findByInterface(DescendancyChartModule::class)->first();
120*575707d1SGreg Roach                    $title     = $module->chartTitle($individual);
121*575707d1SGreg Roach                    $chart_url = $module->chartUrl($individual, [
1229b5537c3SGreg Roach                        'ajax'        => true,
12302fe1c37SGreg Roach                        'generations' => 2,
1240f1e0f10SGreg Roach                        'chart_style' => DescendancyChartModule::CHART_STYLE_TREE,
12502fe1c37SGreg Roach                    ]);
126147e99aaSGreg Roach                    $content   = view('modules/charts/chart', [
12702fe1c37SGreg Roach                        'block_id'  => $block_id,
12802fe1c37SGreg Roach                        'chart_url' => $chart_url,
12941f309deSGreg Roach                        'class'     => 'wt-chart-descendants',
13002fe1c37SGreg Roach                    ]);
1318c2e8227SGreg Roach                    break;
132677aaceaSGreg Roach
1338c2e8227SGreg Roach                case 'hourglass':
134d7f3769dSGreg Roach                    /** @var HourglassChartModule $module */
135ed9ba438SGreg Roach                    $module    = $this->module_service->findByInterface(HourglassChartModule::class)->first();
136*575707d1SGreg Roach                    $title     = $module->chartTitle($individual);
137*575707d1SGreg Roach                    $chart_url = $module->chartUrl($individual, [
1389b5537c3SGreg Roach                        'ajax'        => true,
13902fe1c37SGreg Roach                        'generations' => 2,
14002fe1c37SGreg Roach                    ]);
141147e99aaSGreg Roach                    $content   = view('modules/charts/chart', [
14202fe1c37SGreg Roach                        'block_id'  => $block_id,
14302fe1c37SGreg Roach                        'chart_url' => $chart_url,
14441f309deSGreg Roach                        'class'     => 'wt-chart-hourglass',
14502fe1c37SGreg Roach                    ]);
1468c2e8227SGreg Roach                    break;
147677aaceaSGreg Roach
1488c2e8227SGreg Roach                case 'treenav':
149d7f3769dSGreg Roach                    /** @var InteractiveTreeModule $module */
15041f309deSGreg Roach                    $module = $this->module_service->findByInterface(InteractiveTreeModule::class)->first();
151*575707d1SGreg Roach                    $title  = I18N::translate('Interactive tree of %s', $individual->fullName());
15259f2f229SGreg Roach                    $tv     = new TreeView();
153*575707d1SGreg Roach                    [$html, $js] = $tv->drawViewport($individual, 2);
154*575707d1SGreg Roach                    $content = $html . '<script>' . $js . '</script>';
1558c2e8227SGreg Roach                    break;
1568c2e8227SGreg Roach            }
1578c2e8227SGreg Roach        } else {
1586e7bf391SGreg Roach            $content = I18N::translate('You must select an individual and a chart type in the block preferences');
1598c2e8227SGreg Roach        }
1608c2e8227SGreg Roach
1616a8879feSGreg Roach        if ($ctype !== '') {
162e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
16302fe1c37SGreg Roach                $config_url = route('tree-page-block-edit', [
16402fe1c37SGreg Roach                    'block_id' => $block_id,
165aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
16602fe1c37SGreg Roach                ]);
167397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
16802fe1c37SGreg Roach                $config_url = route('user-page-block-edit', [
16902fe1c37SGreg Roach                    'block_id' => $block_id,
170aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
17102fe1c37SGreg Roach                ]);
1729c6524dcSGreg Roach            } else {
1739c6524dcSGreg Roach                $config_url = '';
1749c6524dcSGreg Roach            }
1759c6524dcSGreg Roach
176147e99aaSGreg Roach            return view('modules/block-template', [
1771e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1789c6524dcSGreg Roach                'id'         => $block_id,
1799c6524dcSGreg Roach                'config_url' => $config_url,
180ea46934eSGreg Roach                'title'      => strip_tags($title),
1819c6524dcSGreg Roach                'content'    => $content,
1829c6524dcSGreg Roach            ]);
1838c2e8227SGreg Roach        }
184b2ce94c6SRico Sonntag
185b2ce94c6SRico Sonntag        return $content;
1868c2e8227SGreg Roach    }
1878c2e8227SGreg Roach
1888c2e8227SGreg Roach    /** {@inheritdoc} */
189c1010edaSGreg Roach    public function loadAjax(): bool
190c1010edaSGreg Roach    {
1918c2e8227SGreg Roach        return true;
1928c2e8227SGreg Roach    }
1938c2e8227SGreg Roach
1948c2e8227SGreg Roach    /** {@inheritdoc} */
195c1010edaSGreg Roach    public function isUserBlock(): bool
196c1010edaSGreg Roach    {
1978c2e8227SGreg Roach        return true;
1988c2e8227SGreg Roach    }
1998c2e8227SGreg Roach
2008c2e8227SGreg Roach    /** {@inheritdoc} */
20163276d8fSGreg Roach    public function isTreeBlock(): bool
202c1010edaSGreg Roach    {
2038c2e8227SGreg Roach        return true;
2048c2e8227SGreg Roach    }
2058c2e8227SGreg Roach
20676692c8bSGreg Roach    /**
207a45f9889SGreg Roach     * Update the configuration for a block.
208a45f9889SGreg Roach     *
2096ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
210a45f9889SGreg Roach     * @param int     $block_id
211a45f9889SGreg Roach     *
212a45f9889SGreg Roach     * @return void
213a45f9889SGreg Roach     */
2146ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
215a45f9889SGreg Roach    {
216*575707d1SGreg Roach        $this->setBlockSetting($block_id, 'type', $request->getParsedBody()['type'] ?? 'pedigree');
217*575707d1SGreg Roach        $this->setBlockSetting($block_id, 'pid', $request->getParsedBody()['xref'] ?? '');
218a45f9889SGreg Roach    }
219a45f9889SGreg Roach
220a45f9889SGreg Roach    /**
22176692c8bSGreg Roach     * An HTML form to edit block settings
22276692c8bSGreg Roach     *
223e490cd80SGreg Roach     * @param Tree $tree
22476692c8bSGreg Roach     * @param int  $block_id
225a9430be8SGreg Roach     *
226988d5e9cSGreg Roach     * @return void
22776692c8bSGreg Roach     */
228e364afe4SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): void
229c1010edaSGreg Roach    {
230e490cd80SGreg Roach        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
231e490cd80SGreg Roach        $gedcomid         = $tree->getUserPreference(Auth::user(), 'gedcomid');
232*575707d1SGreg Roach        $default_xref     = $gedcomid ?: $PEDIGREE_ROOT_ID;
2338c2e8227SGreg Roach
234e2a378d3SGreg Roach        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
235*575707d1SGreg Roach        $xref  = $this->getBlockSetting($block_id, 'pid', $default_xref);
2368c2e8227SGreg Roach
23713abd6f3SGreg Roach        $charts = [
238fddfbb14SGreg Roach            'pedigree'    => I18N::translate('Pedigree'),
239fddfbb14SGreg Roach            'descendants' => I18N::translate('Descendants'),
240fddfbb14SGreg Roach            'hourglass'   => I18N::translate('Hourglass chart'),
241fddfbb14SGreg Roach            'treenav'     => I18N::translate('Interactive tree'),
24213abd6f3SGreg Roach        ];
243fddfbb14SGreg Roach        uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp');
244fddfbb14SGreg Roach
245*575707d1SGreg Roach        $individual = Individual::getInstance($xref, $tree);
246c385536dSGreg Roach
247147e99aaSGreg Roach        echo view('modules/charts/config', [
248c385536dSGreg Roach            'charts'     => $charts,
249c385536dSGreg Roach            'individual' => $individual,
250e490cd80SGreg Roach            'tree'       => $tree,
251c385536dSGreg Roach            'type'       => $type,
252c385536dSGreg Roach        ]);
2538c2e8227SGreg Roach    }
2548c2e8227SGreg Roach}
255