Skip to content

Commit 6771bca

Browse files
committed
Add config options for charts blocks
1 parent 4867704 commit 6771bca

File tree

5 files changed

+152
-22
lines changed

5 files changed

+152
-22
lines changed

app/Module/ChartsBlockModule.php

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,18 @@ 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(
117+
$block_id,
118+
'pedigree_style',
119+
PedigreeChartModule::DEFAULT_STYLE
120+
),
121+
'style' => $this->getBlockSetting(
122+
$block_id,
123+
'pedigree_style',
124+
PedigreeChartModule::DEFAULT_STYLE
125+
),
126+
// Note: some modules use 'layout', others 'style'
117127
]);
118128
$content = view('modules/charts/chart', [
119129
'block_id' => $block_id,
@@ -132,7 +142,7 @@ public function getBlock(Tree $tree, int $block_id, string $context, array $conf
132142
$title = $module->chartTitle($individual);
133143
$chart_url = $module->chartUrl($individual, [
134144
'ajax' => true,
135-
'generations' => 2,
145+
'generations' => $this->getBlockSetting($block_id, 'descendants_generations', '2'),
136146
'chart_style' => DescendancyChartModule::CHART_STYLE_TREE,
137147
]);
138148
$content = view('modules/charts/chart', [
@@ -153,7 +163,7 @@ public function getBlock(Tree $tree, int $block_id, string $context, array $conf
153163
$title = $module->chartTitle($individual);
154164
$chart_url = $module->chartUrl($individual, [
155165
'ajax' => true,
156-
'generations' => 2,
166+
'generations' => $this->getBlockSetting($block_id, 'hourglass_generations', '2'),
157167
]);
158168
$content = view('modules/charts/chart', [
159169
'block_id' => $block_id,
@@ -231,9 +241,17 @@ public function saveBlockConfiguration(ServerRequestInterface $request, int $blo
231241
{
232242
$type = Validator::parsedBody($request)->string('type');
233243
$xref = Validator::parsedBody($request)->isXref()->string('xref');
244+
$pedigree_generations = Validator::parsedBody($request)->integer('pedigree_generations');
245+
$pedigree_style = Validator::parsedBody($request)->string('pedigree_style');
246+
$descendants_generations = Validator::parsedBody($request)->integer('descendants_generations');
247+
$hourglass_generations = Validator::parsedBody($request)->integer('hourglass_generations');
234248

235249
$this->setBlockSetting($block_id, 'type', $type);
236250
$this->setBlockSetting($block_id, 'pid', $xref);
251+
$this->setBlockSetting($block_id, 'pedigree_generations', (string) $pedigree_generations);
252+
$this->setBlockSetting($block_id, 'pedigree_style', $pedigree_style);
253+
$this->setBlockSetting($block_id, 'descendants_generations', (string) $descendants_generations);
254+
$this->setBlockSetting($block_id, 'hourglass_generations', (string) $hourglass_generations);
237255
}
238256

239257
/**
@@ -253,21 +271,56 @@ public function editBlockConfiguration(Tree $tree, int $block_id): string
253271
$type = $this->getBlockSetting($block_id, 'type', 'pedigree');
254272
$xref = $this->getBlockSetting($block_id, 'pid', $default_xref);
255273

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-
];
274+
$charts = [];
275+
// Only add charts that are available
276+
$pedigreeModule = $this->module_service->findByInterface(PedigreeChartModule::class)->first();
277+
if ($pedigreeModule instanceof PedigreeChartModule) {
278+
$charts['pedigree'] = I18N::translate('Pedigree');
279+
$pedigree_max_generations = $pedigreeModule::MAXIMUM_GENERATIONS;
280+
$pedigree_min_generations = $pedigreeModule::MINIMUM_GENERATIONS;
281+
$pedigree_styles = $pedigreeModule->styles(I18N::direction());
282+
}
283+
$descendantsModule = $this->module_service->findByInterface(DescendancyChartModule::class)->first();
284+
if ($descendantsModule instanceof DescendancyChartModule) {
285+
$charts['descendants'] = I18N::translate('Descendants');
286+
$descendants_max_generations = $descendantsModule::MAXIMUM_GENERATIONS;
287+
$descendants_min_generations = $descendantsModule::MINIMUM_GENERATIONS;
288+
}
289+
$hourglassModule = $this->module_service->findByInterface(HourglassChartModule::class)->first();
290+
if ($hourglassModule instanceof HourglassChartModule) {
291+
$charts['hourglass'] = I18N::translate('Hourglass chart');
292+
$hourglass_max_generations = $hourglassModule::MAXIMUM_GENERATIONS;
293+
$hourglass_min_generations = $hourglassModule::MINIMUM_GENERATIONS;
294+
}
295+
$treeModule = $this->module_service->findByInterface(InteractiveTreeModule::class)->first();
296+
if ($treeModule instanceof InteractiveTreeModule) {
297+
$charts['treenav'] = I18N::translate('Interactive tree');
298+
}
262299
uasort($charts, I18N::comparator());
263300

301+
$pedigree_generations = $this->getBlockSetting($block_id, 'pedigree_generations', '3');
302+
$pedigree_style = $this->getBlockSetting($block_id, 'pedigree_style', $pedigreeModule::DEFAULT_STYLE);
303+
$descendants_generations = $this->getBlockSetting($block_id, 'descendants_generations', '2');
304+
$hourglass_generations = $this->getBlockSetting($block_id, 'hourglass_generations', '2');
305+
264306
$individual = Registry::individualFactory()->make($xref, $tree);
265307

266308
return view('modules/charts/config', [
267-
'charts' => $charts,
268-
'individual' => $individual,
269-
'tree' => $tree,
270-
'type' => $type,
309+
'charts' => $charts,
310+
'individual' => $individual,
311+
'tree' => $tree,
312+
'type' => $type,
313+
'pedigree_generations' => $pedigree_generations ?? null,
314+
'pedigree_max_generations' => $pedigree_max_generations ?? null,
315+
'pedigree_min_generations' => $pedigree_min_generations ?? null,
316+
'pedigree_style' => $pedigree_style ?? null,
317+
'pedigree_styles' => $pedigree_styles ?? null,
318+
'descendants_generations' => $descendants_generations ?? null,
319+
'descendants_max_generations' => $descendants_max_generations ?? null,
320+
'descendants_min_generations' => $descendants_min_generations ?? null,
321+
'hourglass_generations' => $hourglass_generations ?? null,
322+
'hourglass_max_generations' => $hourglass_max_generations ?? null,
323+
'hourglass_min_generations' => $hourglass_min_generations ?? null,
271324
]);
272325
}
273326
}

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)