Skip to content

Commit 0d8f8bb

Browse files
committed
Add config options for charts blocks
1 parent 171dbd7 commit 0d8f8bb

File tree

5 files changed

+146
-22
lines changed

5 files changed

+146
-22
lines changed

app/Module/ChartsBlockModule.php

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,12 @@ public function getBlock(Tree $tree, int $block_id, string $context, array $conf
112112
$title = $module->chartTitle($individual);
113113
$chart_url = $module->chartUrl($individual, [
114114
'ajax' => true,
115-
'generations' => 3,
116-
'layout' => PedigreeChartModule::STYLE_RIGHT,
115+
'generations' => $this->getBlockSetting($block_id, 'pedigree_generations', '3'),
116+
'layout' => $this->getBlockSetting($block_id, 'pedigree_style',
117+
PedigreeChartModule::DEFAULT_STYLE),
118+
'style' => $this->getBlockSetting($block_id, 'pedigree_style',
119+
PedigreeChartModule::DEFAULT_STYLE),
120+
// Note: some modules use 'layout', others 'style'
117121
]);
118122
$content = view('modules/charts/chart', [
119123
'block_id' => $block_id,
@@ -132,7 +136,7 @@ public function getBlock(Tree $tree, int $block_id, string $context, array $conf
132136
$title = $module->chartTitle($individual);
133137
$chart_url = $module->chartUrl($individual, [
134138
'ajax' => true,
135-
'generations' => 2,
139+
'generations' => $this->getBlockSetting($block_id, 'descendants_generations', '2'),
136140
'chart_style' => DescendancyChartModule::CHART_STYLE_TREE,
137141
]);
138142
$content = view('modules/charts/chart', [
@@ -153,7 +157,7 @@ public function getBlock(Tree $tree, int $block_id, string $context, array $conf
153157
$title = $module->chartTitle($individual);
154158
$chart_url = $module->chartUrl($individual, [
155159
'ajax' => true,
156-
'generations' => 2,
160+
'generations' => $this->getBlockSetting($block_id, 'hourglass_generations', '2'),
157161
]);
158162
$content = view('modules/charts/chart', [
159163
'block_id' => $block_id,
@@ -231,9 +235,17 @@ public function saveBlockConfiguration(ServerRequestInterface $request, int $blo
231235
{
232236
$type = Validator::parsedBody($request)->string('type');
233237
$xref = Validator::parsedBody($request)->isXref()->string('xref');
238+
$pedigree_generations = Validator::parsedBody($request)->integer('pedigree_generations');
239+
$pedigree_style = Validator::parsedBody($request)->string('pedigree_style');
240+
$descendants_generations = Validator::parsedBody($request)->integer('descendants_generations');
241+
$hourglass_generations = Validator::parsedBody($request)->integer('hourglass_generations');
234242

235243
$this->setBlockSetting($block_id, 'type', $type);
236244
$this->setBlockSetting($block_id, 'pid', $xref);
245+
$this->setBlockSetting($block_id, 'pedigree_generations', (string) $pedigree_generations);
246+
$this->setBlockSetting($block_id, 'pedigree_style', $pedigree_style);
247+
$this->setBlockSetting($block_id, 'descendants_generations', (string) $descendants_generations);
248+
$this->setBlockSetting($block_id, 'hourglass_generations', (string) $hourglass_generations);
237249
}
238250

239251
/**
@@ -253,21 +265,56 @@ public function editBlockConfiguration(Tree $tree, int $block_id): string
253265
$type = $this->getBlockSetting($block_id, 'type', 'pedigree');
254266
$xref = $this->getBlockSetting($block_id, 'pid', $default_xref);
255267

256-
$charts = [
257-
'pedigree' => I18N::translate('Pedigree'),
258-
'descendants' => I18N::translate('Descendants'),
259-
'hourglass' => I18N::translate('Hourglass chart'),
260-
'treenav' => I18N::translate('Interactive tree'),
261-
];
268+
$charts = [];
269+
// Only add charts that are available
270+
$pedigreeModule = $this->module_service->findByInterface(PedigreeChartModule::class)->first();
271+
if ($pedigreeModule instanceof PedigreeChartModule) {
272+
$charts['pedigree'] = I18N::translate('Pedigree');
273+
$pedigree_max_generations = $pedigreeModule::MAXIMUM_GENERATIONS;
274+
$pedigree_min_generations = $pedigreeModule::MINIMUM_GENERATIONS;
275+
$pedigree_styles = $pedigreeModule->styles(I18N::direction());
276+
}
277+
$descendantsModule = $this->module_service->findByInterface(DescendancyChartModule::class)->first();
278+
if ($descendantsModule instanceof DescendancyChartModule) {
279+
$charts['descendants'] = I18N::translate('Descendants');
280+
$descendants_max_generations = $descendantsModule::MAXIMUM_GENERATIONS;
281+
$descendants_min_generations = $descendantsModule::MINIMUM_GENERATIONS;
282+
}
283+
$hourglassModule = $this->module_service->findByInterface(HourglassChartModule::class)->first();
284+
if ($hourglassModule instanceof HourglassChartModule) {
285+
$charts['hourglass'] = I18N::translate('Hourglass chart');
286+
$hourglass_max_generations = $hourglassModule::MAXIMUM_GENERATIONS;
287+
$hourglass_min_generations = $hourglassModule::MINIMUM_GENERATIONS;
288+
}
289+
$treeModule = $this->module_service->findByInterface(InteractiveTreeModule::class)->first();
290+
if ($treeModule instanceof InteractiveTreeModule) {
291+
$charts['treenav'] = I18N::translate('Interactive tree');
292+
}
262293
uasort($charts, I18N::comparator());
263294

295+
$pedigree_generations = $this->getBlockSetting($block_id, 'pedigree_generations', '3');
296+
$pedigree_style = $this->getBlockSetting($block_id, 'pedigree_style', $pedigreeModule::DEFAULT_STYLE);
297+
$descendants_generations = $this->getBlockSetting($block_id, 'descendants_generations', '2');
298+
$hourglass_generations = $this->getBlockSetting($block_id, 'hourglass_generations', '2');
299+
264300
$individual = Registry::individualFactory()->make($xref, $tree);
265301

266302
return view('modules/charts/config', [
267-
'charts' => $charts,
268-
'individual' => $individual,
269-
'tree' => $tree,
270-
'type' => $type,
303+
'charts' => $charts,
304+
'individual' => $individual,
305+
'tree' => $tree,
306+
'type' => $type,
307+
'pedigree_generations' => $pedigree_generations ?? null,
308+
'pedigree_max_generations' => $pedigree_max_generations ?? null,
309+
'pedigree_min_generations' => $pedigree_min_generations ?? null,
310+
'pedigree_style' => $pedigree_style ?? null,
311+
'pedigree_styles' => $pedigree_styles ?? null,
312+
'descendants_generations' => $descendants_generations ?? null,
313+
'descendants_max_generations' => $descendants_max_generations ?? null,
314+
'descendants_min_generations' => $descendants_min_generations ?? null,
315+
'hourglass_generations' => $hourglass_generations ?? null,
316+
'hourglass_max_generations' => $hourglass_max_generations ?? null,
317+
'hourglass_min_generations' => $hourglass_min_generations ?? null,
271318
]);
272319
}
273320
}

app/Module/DescendancyChartModule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ class DescendancyChartModule extends AbstractModule implements ModuleChartInterf
5656
];
5757

5858
// Limits
59-
protected const MINIMUM_GENERATIONS = 2;
60-
protected const MAXIMUM_GENERATIONS = 10;
59+
public const MINIMUM_GENERATIONS = 2;
60+
public const MAXIMUM_GENERATIONS = 10;
6161

6262
private ChartService $chart_service;
6363

app/Module/HourglassChartModule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ class HourglassChartModule extends AbstractModule implements ModuleChartInterfac
5353
];
5454

5555
// Limits
56-
protected const MINIMUM_GENERATIONS = 2;
57-
protected const MAXIMUM_GENERATIONS = 10;
56+
public const MINIMUM_GENERATIONS = 2;
57+
public const MAXIMUM_GENERATIONS = 10;
5858

5959
/**
6060
* Initialization.

app/Module/PedigreeChartModule.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class PedigreeChartModule extends AbstractModule implements ModuleChartInterface
5858
];
5959

6060
// Limits
61-
protected const MINIMUM_GENERATIONS = 2;
62-
protected const MAXIMUM_GENERATIONS = 12;
61+
public const MINIMUM_GENERATIONS = 2;
62+
public const MAXIMUM_GENERATIONS = 12;
6363

6464
// For RTL languages
6565
protected const MIRROR_STYLE = [
@@ -356,7 +356,7 @@ protected function individualLink(Individual $individual, string $style, int $ge
356356
*
357357
* @return array<string>
358358
*/
359-
protected function styles(string $direction): array
359+
public function styles(string $direction): array
360360
{
361361
// On right-to-left pages, the CSS will mirror the chart, so we need to mirror the label.
362362
if ($direction === 'rtl') {

resources/views/modules/charts/config.phtml

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,18 @@ use Fisharebest\Webtrees\Tree;
88

99
/**
1010
* @var array<string,string> $charts
11-
* @var Individual|null $individual
11+
* @var Individual $individual
12+
* @var int $pedigree_generations
13+
* @var int $pedigree_max_generations
14+
* @var int $pedigree_min_generations
15+
* @var string $pedigree_style
16+
* @var array<string,string> $pedigree_styles
17+
* @var int $descendants_generations
18+
* @var int $descendants_max_generations
19+
* @var int $descendants_min_generations
20+
* @var int $hourglass_generations
21+
* @var int $hourglass_max_generations
22+
* @var int $hourglass_min_generations
1223
* @var Tree $tree
1324
* @var string $type
1425
*/
@@ -34,3 +45,69 @@ use Fisharebest\Webtrees\Tree;
3445
<?= view('components/select-individual', ['name' => 'xref', 'individual' => $individual, 'tree' => $tree]) ?>
3546
</div>
3647
</div>
48+
49+
<div id="options-pedigree"<?= $type !== 'pedigree' ? ' style="display: none"' : '' ?>>
50+
<div class="row mb-3">
51+
<label class="col-sm-3 col-form-label wt-page-options-label" for="pedigree_generations">
52+
<?= I18N::translate('Generations') ?>
53+
</label>
54+
<div class="col-sm-9 wt-page-options-value">
55+
<?= view('components/select-number', ['name' => 'pedigree_generations', 'selected' => $pedigree_generations, 'options' => range($pedigree_min_generations, $pedigree_max_generations)]) ?>
56+
</div>
57+
</div>
58+
59+
<div class="row mb-3">
60+
<label class="col-sm-3 col-form-label wt-page-options-label" for="style">
61+
<?= I18N::translate('Layout') ?>
62+
</label>
63+
<div class="col-sm-9 wt-page-options-value">
64+
<?= view('components/radios-inline', ['name' => 'pedigree_style', 'options' => $pedigree_styles, 'selected' => $pedigree_style]) ?>
65+
</div>
66+
</div>
67+
</div>
68+
69+
<div id="options-descendants"<?= $type !== 'descendants' ? ' style="display: none"' : '' ?>>
70+
<div class="row mb-3">
71+
<label class="col-sm-3 col-form-label wt-page-options-label" for="descendants_generations">
72+
<?= I18N::translate('Generations') ?>
73+
</label>
74+
<div class="col-sm-9 wt-page-options-value">
75+
<?= view('components/select-number', ['name' => 'descendants_generations', 'selected' => $descendants_generations, 'options' => range($descendants_min_generations, $descendants_max_generations)]) ?>
76+
</div>
77+
</div>
78+
</div>
79+
80+
<div id="options-hourglass"<?= $type !== 'hourglass' ? ' style="display: none"' : '' ?>>
81+
<div class="row mb-3">
82+
<label class="col-sm-3 col-form-label wt-page-options-label" for="hourglass_generations">
83+
<?= I18N::translate('Generations') ?>
84+
</label>
85+
<div class="col-sm-9 wt-page-options-value">
86+
<?= view('components/select-number', ['name' => 'hourglass_generations', 'selected' => $hourglass_generations, 'options' => range($hourglass_min_generations, $hourglass_max_generations)]) ?>
87+
</div>
88+
</div>
89+
</div>
90+
91+
<script type="text/javascript">
92+
const typeEl = document.getElementById('type');
93+
typeEl.addEventListener('change', () => {
94+
const selectedOption = typeEl.options[typeEl.selectedIndex].value;
95+
if (selectedOption === 'pedigree') {
96+
document.getElementById('options-pedigree').style.removeProperty('display');
97+
document.getElementById('options-descendants').style.display = 'none';
98+
document.getElementById('options-hourglass').style.display = 'none';
99+
} else if (selectedOption === 'descendants') {
100+
document.getElementById('options-pedigree').style.display = 'none';
101+
document.getElementById('options-descendants').style.removeProperty('display');
102+
document.getElementById('options-hourglass').style.display = 'none';
103+
} else if (selectedOption === 'hourglass') {
104+
document.getElementById('options-pedigree').style.display = 'none';
105+
document.getElementById('options-descendants').style.display = 'none';
106+
document.getElementById('options-hourglass').style.removeProperty('display');
107+
} else {
108+
document.getElementById('options-pedigree').style.display = 'none';
109+
document.getElementById('options-descendants').style.display = 'none';
110+
document.getElementById('options-hourglass').style.display = 'none';
111+
}
112+
});
113+
</script>

0 commit comments

Comments
 (0)