.
*/
declare(strict_types=1);
namespace Fisharebest\Webtrees\Module;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Module\InteractiveTree\TreeView;
use Fisharebest\Webtrees\Services\ModuleService;
use Fisharebest\Webtrees\Tree;
use Symfony\Component\HttpFoundation\Request;
/**
* Class ChartsBlockModule
*/
class ChartsBlockModule extends AbstractModule implements ModuleBlockInterface
{
use ModuleBlockTrait;
/**
* @var ModuleService
*/
private $module_service;
/**
* ChartsBlockModule constructor.
*
* @param ModuleService $module_service
*/
public function __construct(ModuleService $module_service)
{
$this->module_service = $module_service;
}
/**
* How should this module be labelled on tabs, menus, etc.?
*
* @return string
*/
public function title(): string
{
/* I18N: Name of a module/block */
return I18N::translate('Charts');
}
/**
* A sentence describing what this module does.
*
* @return string
*/
public function description(): string
{
/* I18N: Description of the “Charts” module */
return I18N::translate('An alternative way to display charts.');
}
/**
* Generate the HTML content of this block.
*
* @param Tree $tree
* @param int $block_id
* @param string $ctype
* @param string[] $cfg
*
* @return string
*/
public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string
{
$PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
$gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid');
$type = $this->getBlockSetting($block_id, 'type', 'pedigree');
$pid = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
extract($cfg, EXTR_OVERWRITE);
$person = Individual::getInstance($pid, $tree);
if (!$person) {
$pid = $PEDIGREE_ROOT_ID;
$this->setBlockSetting($block_id, 'pid', $pid);
$person = Individual::getInstance($pid, $tree);
}
$title = $this->title();
if ($person) {
switch ($type) {
default:
case 'pedigree':
/** @var PedigreeChartModule $module */
$module = $this->module_service->findByClass(PedigreeChartModule::class);
$title = $module->chartTitle($person);
$chart_url = $module->chartUrl($person, [
'ajax' => true,
'generations' => 3,
'layout' => PedigreeChartModule::PORTRAIT,
]);
$content = view('modules/charts/chart', [
'block_id' => $block_id,
'chart_url' => $chart_url,
]);
break;
case 'descendants':
/** @var DescendancyChartModule $module */
$module = $this->module_service->findByClass(DescendancyChartModule::class);
$title = $module->chartTitle($person);
$chart_url = $module->chartUrl($person, [
'ajax' => true,
'generations' => 2,
'chart_style' => DescendancyChartModule::CHART_STYLE_LIST,
]);
$content = view('modules/charts/chart', [
'block_id' => $block_id,
'chart_url' => $chart_url,
]);
break;
case 'hourglass':
/** @var HourglassChartModule $module */
$module = $this->module_service->findByClass(HourglassChartModule::class);
$title = $module->chartTitle($person);
$chart_url = $module->chartUrl($person, [
'ajax' => true,
'generations' => 2,
]);
$content = view('modules/charts/chart', [
'block_id' => $block_id,
'chart_url' => $chart_url,
]);
break;
case 'treenav':
/** @var InteractiveTreeModule $module */
$module = $this->module_service->findByClass(InteractiveTreeModule::class);
$title = I18N::translate('Interactive tree of %s', $person->getFullName());
$tv = new TreeView();
$content = '';
$content .= '';
[$html, $js] = $tv->drawViewport($person, 2);
$content .= $html . '';
break;
}
} else {
$content = I18N::translate('You must select an individual and a chart type in the block preferences');
}
if ($ctype !== '') {
if ($ctype === 'gedcom' && Auth::isManager($tree)) {
$config_url = route('tree-page-block-edit', [
'block_id' => $block_id,
'ged' => $tree->name(),
]);
} elseif ($ctype === 'user' && Auth::check()) {
$config_url = route('user-page-block-edit', [
'block_id' => $block_id,
'ged' => $tree->name(),
]);
} else {
$config_url = '';
}
return view('modules/block-template', [
'block' => str_replace('_', '-', $this->name()),
'id' => $block_id,
'config_url' => $config_url,
'title' => strip_tags($title),
'content' => $content,
]);
}
return $content;
}
/** {@inheritdoc} */
public function loadAjax(): bool
{
return true;
}
/** {@inheritdoc} */
public function isUserBlock(): bool
{
return true;
}
/** {@inheritdoc} */
public function isTreeBlock(): bool
{
return true;
}
/**
* Update the configuration for a block.
*
* @param Request $request
* @param int $block_id
*
* @return void
*/
public function saveBlockConfiguration(Request $request, int $block_id)
{
$this->setBlockSetting($block_id, 'type', $request->get('type', 'pedigree'));
$this->setBlockSetting($block_id, 'pid', $request->get('pid', ''));
}
/**
* An HTML form to edit block settings
*
* @param Tree $tree
* @param int $block_id
*
* @return void
*/
public function editBlockConfiguration(Tree $tree, int $block_id)
{
$PEDIGREE_ROOT_ID = $tree->getPreference('PEDIGREE_ROOT_ID');
$gedcomid = $tree->getUserPreference(Auth::user(), 'gedcomid');
$type = $this->getBlockSetting($block_id, 'type', 'pedigree');
$pid = $this->getBlockSetting($block_id, 'pid', Auth::check() ? ($gedcomid ?: $PEDIGREE_ROOT_ID) : $PEDIGREE_ROOT_ID);
$charts = [
'pedigree' => I18N::translate('Pedigree'),
'descendants' => I18N::translate('Descendants'),
'hourglass' => I18N::translate('Hourglass chart'),
'treenav' => I18N::translate('Interactive tree'),
];
uasort($charts, 'Fisharebest\Webtrees\I18N::strcasecmp');
$individual = Individual::getInstance($pid, $tree);
echo view('modules/charts/config', [
'charts' => $charts,
'individual' => $individual,
'tree' => $tree,
'type' => $type,
]);
}
}