xref: /webtrees/app/Module/ChartsBlockModule.php (revision e364afe4ae4e316fc4ebd53eccbaff2d29e419a5)
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;
26a45f9889SGreg Roachuse Symfony\Component\HttpFoundation\Request;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class ChartsBlockModule
308c2e8227SGreg Roach */
3137eb8894SGreg Roachclass ChartsBlockModule extends AbstractModule implements ModuleBlockInterface
32c1010edaSGreg Roach{
3349a243cbSGreg Roach    use ModuleBlockTrait;
3449a243cbSGreg Roach
35961ec755SGreg Roach    /**
364ca7e03cSGreg Roach     * @var ModuleService
374ca7e03cSGreg Roach     */
384ca7e03cSGreg Roach    private $module_service;
394ca7e03cSGreg Roach
404ca7e03cSGreg Roach    /**
414ca7e03cSGreg Roach     * ChartsBlockModule constructor.
424ca7e03cSGreg Roach     *
434ca7e03cSGreg Roach     * @param ModuleService $module_service
444ca7e03cSGreg Roach     */
455bdbe281SGreg Roach    public function __construct(ModuleService $module_service)
465bdbe281SGreg Roach    {
474ca7e03cSGreg Roach        $this->module_service = $module_service;
484ca7e03cSGreg Roach    }
494ca7e03cSGreg Roach
504ca7e03cSGreg Roach    /**
510cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
52961ec755SGreg Roach     *
53961ec755SGreg Roach     * @return string
54961ec755SGreg Roach     */
5549a243cbSGreg Roach    public function title(): string
56c1010edaSGreg Roach    {
57bbb76c12SGreg Roach        /* I18N: Name of a module/block */
58bbb76c12SGreg Roach        return I18N::translate('Charts');
598c2e8227SGreg Roach    }
608c2e8227SGreg Roach
61961ec755SGreg Roach    /**
62961ec755SGreg Roach     * A sentence describing what this module does.
63961ec755SGreg Roach     *
64961ec755SGreg Roach     * @return string
65961ec755SGreg Roach     */
6649a243cbSGreg Roach    public function description(): string
67c1010edaSGreg Roach    {
68bbb76c12SGreg Roach        /* I18N: Description of the “Charts” module */
69bbb76c12SGreg Roach        return I18N::translate('An alternative way to display charts.');
708c2e8227SGreg Roach    }
718c2e8227SGreg Roach
7276692c8bSGreg Roach    /**
7376692c8bSGreg Roach     * Generate the HTML content of this block.
7476692c8bSGreg Roach     *
75e490cd80SGreg Roach     * @param Tree     $tree
7676692c8bSGreg Roach     * @param int      $block_id
775f2ae573SGreg Roach     * @param string   $ctype
78727f238cSGreg Roach     * @param string[] $cfg
7976692c8bSGreg Roach     *
8076692c8bSGreg Roach     * @return string
8176692c8bSGreg Roach     */
825f2ae573SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
83c1010edaSGreg Roach    {
84e490cd80SGreg Roach        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
85e490cd80SGreg Roach        $gedcomid         = $tree->getUserPreference(Auth::user(), 'gedcomid');
868c2e8227SGreg Roach
87e2a378d3SGreg Roach        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
88bd44f43fSGreg Roach        $pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
898c2e8227SGreg Roach
90c385536dSGreg Roach        extract($cfg, EXTR_OVERWRITE);
918c2e8227SGreg Roach
92e490cd80SGreg Roach        $person = Individual::getInstance($pid, $tree);
938c2e8227SGreg Roach        if (!$person) {
948c2e8227SGreg Roach            $pid = $PEDIGREE_ROOT_ID;
95e2a378d3SGreg Roach            $this->setBlockSetting($block_id, 'pid', $pid);
96e490cd80SGreg Roach            $person = Individual::getInstance($pid, $tree);
978c2e8227SGreg Roach        }
988c2e8227SGreg Roach
9949a243cbSGreg Roach        $title = $this->title();
1008c2e8227SGreg Roach
1018c2e8227SGreg Roach        if ($person) {
1028c2e8227SGreg Roach            switch ($type) {
103677aaceaSGreg Roach                default:
1048c2e8227SGreg Roach                case 'pedigree':
105d7f3769dSGreg Roach                    /** @var PedigreeChartModule $module */
106321a89daSGreg Roach                    $module    = $this->module_service->findByInterface(PedigreeChartModule::class);
107677aaceaSGreg Roach                    $title     = $module->chartTitle($person);
108677aaceaSGreg Roach                    $chart_url = $module->chartUrl($person, [
1099b5537c3SGreg Roach                        'ajax'        => true,
11002fe1c37SGreg Roach                        'generations' => 3,
111aa7659ebSGreg Roach                        'layout'      => PedigreeChartModule::ORIENTATION_RIGHT,
11202fe1c37SGreg Roach                    ]);
113147e99aaSGreg Roach                    $content   = view('modules/charts/chart', [
11402fe1c37SGreg Roach                        'block_id'  => $block_id,
11502fe1c37SGreg Roach                        'chart_url' => $chart_url,
11602fe1c37SGreg Roach                    ]);
1178c2e8227SGreg Roach                    break;
118677aaceaSGreg Roach
1198c2e8227SGreg Roach                case 'descendants':
120d7f3769dSGreg Roach                    /** @var DescendancyChartModule $module */
121321a89daSGreg Roach                    $module    = $this->module_service->findByInterface(DescendancyChartModule::class);
122677aaceaSGreg Roach                    $title     = $module->chartTitle($person);
123677aaceaSGreg Roach                    $chart_url = $module->chartUrl($person, [
1249b5537c3SGreg Roach                        'ajax'        => true,
12502fe1c37SGreg Roach                        'generations' => 2,
126677aaceaSGreg Roach                        'chart_style' => DescendancyChartModule::CHART_STYLE_LIST,
12702fe1c37SGreg Roach                    ]);
128147e99aaSGreg Roach                    $content   = view('modules/charts/chart', [
12902fe1c37SGreg Roach                        'block_id'  => $block_id,
13002fe1c37SGreg Roach                        'chart_url' => $chart_url,
13102fe1c37SGreg Roach                    ]);
1328c2e8227SGreg Roach                    break;
133677aaceaSGreg Roach
1348c2e8227SGreg Roach                case 'hourglass':
135d7f3769dSGreg Roach                    /** @var HourglassChartModule $module */
136321a89daSGreg Roach                    $module    = $this->module_service->findByInterface(HourglassChartModule::class);
137677aaceaSGreg Roach                    $title     = $module->chartTitle($person);
138677aaceaSGreg Roach                    $chart_url = $module->chartUrl($person, [
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,
14502fe1c37SGreg Roach                    ]);
1468c2e8227SGreg Roach                    break;
147677aaceaSGreg Roach
1488c2e8227SGreg Roach                case 'treenav':
149d7f3769dSGreg Roach                    /** @var InteractiveTreeModule $module */
150321a89daSGreg Roach                    $module  = $this->module_service->findByInterface(InteractiveTreeModule::class);
15139ca88baSGreg Roach                    $title   = I18N::translate('Interactive tree of %s', $person->fullName());
15259f2f229SGreg Roach                    $tv      = new TreeView();
153677aaceaSGreg Roach                    $content = '<script>$("head").append(\'<link rel="stylesheet" href="' . $module->css() . '" type="text/css" />\');</script>';
154677aaceaSGreg Roach                    $content .= '<script src="' . $module->js() . '"></script>';
15565e02381SGreg Roach                    [$html, $js] = $tv->drawViewport($person, 2);
1568c2e8227SGreg Roach                    $content .= $html . '<script>' . $js . '</script>';
1578c2e8227SGreg Roach                    break;
1588c2e8227SGreg Roach            }
1598c2e8227SGreg Roach        } else {
1606e7bf391SGreg Roach            $content = I18N::translate('You must select an individual and a chart type in the block preferences');
1618c2e8227SGreg Roach        }
1628c2e8227SGreg Roach
1636a8879feSGreg Roach        if ($ctype !== '') {
164e490cd80SGreg Roach            if ($ctype === 'gedcom' && Auth::isManager($tree)) {
16502fe1c37SGreg Roach                $config_url = route('tree-page-block-edit', [
16602fe1c37SGreg Roach                    'block_id' => $block_id,
167aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
16802fe1c37SGreg Roach                ]);
169397e599aSGreg Roach            } elseif ($ctype === 'user' && Auth::check()) {
17002fe1c37SGreg Roach                $config_url = route('user-page-block-edit', [
17102fe1c37SGreg Roach                    'block_id' => $block_id,
172aa6f03bbSGreg Roach                    'ged'      => $tree->name(),
17302fe1c37SGreg Roach                ]);
1749c6524dcSGreg Roach            } else {
1759c6524dcSGreg Roach                $config_url = '';
1769c6524dcSGreg Roach            }
1779c6524dcSGreg Roach
178147e99aaSGreg Roach            return view('modules/block-template', [
17926684e68SGreg Roach                'block'      => str_replace('_', '-', $this->name()),
1809c6524dcSGreg Roach                'id'         => $block_id,
1819c6524dcSGreg Roach                'config_url' => $config_url,
182ea46934eSGreg Roach                'title'      => strip_tags($title),
1839c6524dcSGreg Roach                'content'    => $content,
1849c6524dcSGreg Roach            ]);
1858c2e8227SGreg Roach        }
186b2ce94c6SRico Sonntag
187b2ce94c6SRico Sonntag        return $content;
1888c2e8227SGreg Roach    }
1898c2e8227SGreg Roach
1908c2e8227SGreg Roach    /** {@inheritdoc} */
191c1010edaSGreg Roach    public function loadAjax(): bool
192c1010edaSGreg Roach    {
1938c2e8227SGreg Roach        return true;
1948c2e8227SGreg Roach    }
1958c2e8227SGreg Roach
1968c2e8227SGreg Roach    /** {@inheritdoc} */
197c1010edaSGreg Roach    public function isUserBlock(): bool
198c1010edaSGreg Roach    {
1998c2e8227SGreg Roach        return true;
2008c2e8227SGreg Roach    }
2018c2e8227SGreg Roach
2028c2e8227SGreg Roach    /** {@inheritdoc} */
20363276d8fSGreg Roach    public function isTreeBlock(): bool
204c1010edaSGreg Roach    {
2058c2e8227SGreg Roach        return true;
2068c2e8227SGreg Roach    }
2078c2e8227SGreg Roach
20876692c8bSGreg Roach    /**
209a45f9889SGreg Roach     * Update the configuration for a block.
210a45f9889SGreg Roach     *
211a45f9889SGreg Roach     * @param Request $request
212a45f9889SGreg Roach     * @param int     $block_id
213a45f9889SGreg Roach     *
214a45f9889SGreg Roach     * @return void
215a45f9889SGreg Roach     */
216*e364afe4SGreg Roach    public function saveBlockConfiguration(Request $request, int $block_id): void
217a45f9889SGreg Roach    {
218a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'type', $request->get('type', 'pedigree'));
219a45f9889SGreg Roach        $this->setBlockSetting($block_id, 'pid', $request->get('pid', ''));
220a45f9889SGreg Roach    }
221a45f9889SGreg Roach
222a45f9889SGreg Roach    /**
22376692c8bSGreg Roach     * An HTML form to edit block settings
22476692c8bSGreg Roach     *
225e490cd80SGreg Roach     * @param Tree $tree
22676692c8bSGreg Roach     * @param int  $block_id
227a9430be8SGreg Roach     *
228988d5e9cSGreg Roach     * @return void
22976692c8bSGreg Roach     */
230*e364afe4SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): void
231c1010edaSGreg Roach    {
232e490cd80SGreg Roach        $PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
233e490cd80SGreg Roach        $gedcomid         = $tree->getUserPreference(Auth::user(), 'gedcomid');
2348c2e8227SGreg Roach
235e2a378d3SGreg Roach        $type = $this->getBlockSetting($block_id, 'type', 'pedigree');
236bd44f43fSGreg Roach        $pid  = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
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
246e490cd80SGreg Roach        $individual = Individual::getInstance($pid, $tree);
247c385536dSGreg Roach
248147e99aaSGreg Roach        echo 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