-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathExtendedRelationshipModule.php
1223 lines (1026 loc) · 45.3 KB
/
ExtendedRelationshipModule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Cissee\Webtrees\Module\ExtendedRelationships;
use Cissee\Webtrees\Module\ExtendedRelationships\AjaxRequests;
use Cissee\Webtrees\Module\ExtendedRelationships\ExtendedRelationshipController;
use Cissee\Webtrees\Module\ExtendedRelationships\ExtendedRelationshipModuleTrait;
use Cissee\Webtrees\Module\ExtendedRelationships\HelpTexts;
use Cissee\Webtrees\Module\ExtendedRelationships\Sync;
use Cissee\WebtreesExt\Module\ModuleMetaInterface;
use Cissee\WebtreesExt\Module\ModuleMetaTrait;
use Cissee\WebtreesExt\Modules\RelationshipPath;
use Cissee\WebtreesExt\Modules\RelationshipUtils;
use Cissee\WebtreesExt\MoreI18N;
use Cissee\WebtreesExt\Requests;
use Fig\Http\Message\RequestMethodInterface;
use Fisharebest\Localization\Translation;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Date;
use Fisharebest\Webtrees\Fact;
use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\GedcomRecord;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Menu;
use Fisharebest\Webtrees\Module\ModuleBlockInterface;
use Fisharebest\Webtrees\Module\ModuleBlockTrait;
use Fisharebest\Webtrees\Module\ModuleChartInterface;
use Fisharebest\Webtrees\Module\ModuleChartTrait;
use Fisharebest\Webtrees\Module\ModuleConfigInterface;
use Fisharebest\Webtrees\Module\ModuleConfigTrait;
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
use Fisharebest\Webtrees\Module\ModuleCustomTrait;
use Fisharebest\Webtrees\Module\ModuleGlobalInterface;
use Fisharebest\Webtrees\Module\ModuleGlobalTrait;
use Fisharebest\Webtrees\Module\ModuleListInterface;
use Fisharebest\Webtrees\Module\ModuleListTrait;
use Fisharebest\Webtrees\Module\ModuleMenuInterface;
use Fisharebest\Webtrees\Module\ModuleMenuTrait;
use Fisharebest\Webtrees\Module\RelationshipsChartModule;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Services\RelationshipService;
use Fisharebest\Webtrees\Services\TimeoutService;
use Fisharebest\Webtrees\Services\TreeService;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\User;
use Fisharebest\Webtrees\Validator;
use Fisharebest\Webtrees\View;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use ReflectionClass;
use Vesta\CommonI18N;
use Vesta\Hook\HookInterfaces\EmptyIndividualFactsTabExtender;
use Vesta\Hook\HookInterfaces\EmptyRelativesTabExtender;
use Vesta\Hook\HookInterfaces\IndividualFactsTabExtenderInterface;
use Vesta\Hook\HookInterfaces\RelativesTabExtenderInterface;
use Vesta\Model\GenericViewElement;
use Vesta\VestaModuleTrait;
use const CAL_GREGORIAN;
use function cal_from_jd;
use function redirect;
use function response;
use function route;
use function view;
// we extend RelationshipsChartModule so that links to this chart are used even in non-extended tabs etc.
class ExtendedRelationshipModule extends RelationshipsChartModule implements
ModuleCustomInterface,
ModuleMetaInterface,
ModuleConfigInterface,
ModuleGlobalInterface,
ModuleChartInterface,
ModuleListInterface,
ModuleBlockInterface,
//ModuleContainerInterface,
ModuleMenuInterface, //more charts
RequestHandlerInterface,
IndividualFactsTabExtenderInterface,
RelativesTabExtenderInterface {
use ModuleCustomTrait,
ModuleMetaTrait,
ModuleConfigTrait,
ModuleGlobalTrait,
ModuleChartTrait,
ModuleListTrait,
ModuleBlockTrait,
//ModuleContainerTrait,
ModuleMenuTrait,
VestaModuleTrait {
VestaModuleTrait::customTranslations insteadof ModuleCustomTrait;
VestaModuleTrait::getAssetAction insteadof ModuleCustomTrait;
VestaModuleTrait::assetUrl insteadof ModuleCustomTrait;
VestaModuleTrait::getConfigLink insteadof ModuleConfigTrait;
ModuleMetaTrait::customModuleVersion insteadof ModuleCustomTrait;
ModuleMetaTrait::customModuleLatestVersion insteadof ModuleCustomTrait;
}
use EmptyIndividualFactsTabExtender;
use EmptyRelativesTabExtender;
use ExtendedRelationshipModuleTrait {
ExtendedRelationshipModuleTrait::editConfigAfterFaq insteadof VestaModuleTrait;
}
protected const VESTA_ROUTE_URL = '/tree/{tree}/vesta-relationships-{ancestors}-{recursion}/{xref}{/xref2}';
protected const VESTA_ROUTE_URL_LIST = '/tree/{tree}/individual-patriarchs-list';
/** It would be more correct to use PHP_INT_MAX, but this isn't friendly in URLs */
//public const VESTA_UNLIMITED_RECURSION = 99;
/** By default new trees allow unlimited recursion */
public const VESTA_DEFAULT_RECURSION = '99';
//not as constant: depends on module configuration
/*
public const DEFAULT_ANCESTORS = '1'; //should we use '1' here even if in case this option isn't configured?
public const DEFAULT_PARAMETERS = [
'ancestors' => self::DEFAULT_ANCESTORS,
'recursion' => self::DEFAULT_RECURSION,
];
*/
/** @var ExtendedIndividualListRequestHandler */
protected $listRequestHandler;
public function __construct(
//ModuleService $module_service, //COMMENT OUT for 2.1.x
RelationshipService $relationship_service,
TreeService $tree_service) {
parent::__construct(
//$module_service, //COMMENT OUT for 2.1.x
$relationship_service,
$tree_service);
$this->listRequestHandler = new ExtendedIndividualListRequestHandler(
$this);
}
//test only!
public function containedModules(): Collection {
return new Collection([new ExtendedRelationshipModuleSub1()]);
}
protected function defaultParameters(): array {
$chart1 = boolval($this->getPreference('CHART_1', '1'));
$chart2 = boolval($this->getPreference('CHART_2', '0'));
$chart3 = boolval($this->getPreference('CHART_3', '1'));
$chart4 = boolval($this->getPreference('CHART_4', '1'));
$chart5 = boolval($this->getPreference('CHART_5', '1'));
$chart6 = boolval($this->getPreference('CHART_6', '0'));
$chart7 = boolval($this->getPreference('CHART_7', '0'));
//fixed priority (we don't want to make this configurable as well)
if ($chart7) {
$defaultMode = 7;
} else if ($chart4) {
$defaultMode = 4;
} else if ($chart1) {
$defaultMode = 1;
} else if ($chart2) {
$defaultMode = 2;
} else if ($chart3) {
$defaultMode = 3;
} else if ($chart5) {
$defaultMode = 5;
} else if ($chart6) {
$defaultMode = 6;
} else {
//nothing selected!
$defaultMode = 1;
}
return [
'ancestors' => $defaultMode,
'recursion' => self::VESTA_DEFAULT_RECURSION,
];
}
public function customModuleAuthorName(): string {
return 'Richard Cissée';
}
public function customModuleMetaDatasJson(): string {
return file_get_contents(__DIR__ . '/metadata.json');
}
public function customModuleLatestMetaDatasJsonUrl(): string {
return 'https://raw.githubusercontent.com/vesta-webtrees-2-custom-modules/vesta_extended_relationships/master/metadata.json';
}
public function customModuleSupportUrl(): string {
return 'https://cissee.de';
}
public function resourcesFolder(): string {
return __DIR__ . '/resources/';
}
public function customTranslations(string $language): array {
$languageFile1 = $this->resourcesFolder() . 'lang/' . $language . '.mo';
$languageFile2 = $this->resourcesFolder() . 'lang/' . $language . '.csv';
$languageFile3 = $this->resourcesFolder() . 'lang/ext/' . $language . '.mo';
$languageFile4 = $this->resourcesFolder() . 'lang/ext/' . $language . '.csv';
$ret = [];
if (file_exists($languageFile1)) {
$ret = (new Translation($languageFile1))->asArray();
}
if (file_exists($languageFile2)) {
$ret = array_merge($ret, (new Translation($languageFile2))->asArray());
}
if (file_exists($languageFile3)) {
$ret = array_merge($ret, (new Translation($languageFile3))->asArray());
}
if (file_exists($languageFile4)) {
$ret = array_merge($ret, (new Translation($languageFile4))->asArray());
}
return $ret;
}
public function onBoot(): void {
//define our 'pretty' routes
//note: potentially problematic in case of name clashes;
//webtrees isn't interested in solving this properly, see
//https://www.webtrees.net/index.php/en/forum/2-open-discussion/33687-pretty-urls-in-2-x
/*
$router_container = \Vesta\VestaUtils::get(RouterContainer::class);
assert($router_container instanceof RouterContainer);
$router = $router_container->getMap();
*/
$router = Registry::routeFactory()->routeMap();
$router
->get(static::class, static::VESTA_ROUTE_URL, $this)
->allows(RequestMethodInterface::METHOD_POST)
->tokens([
'ancestors' => '\d+',
'generations' => '\d+',
]);
$router
->get(ExtendedIndividualListRequestHandler::class, static::VESTA_ROUTE_URL_LIST, $this->listRequestHandler);
View::registerCustomView('::lists/individuals-table-with-patriarchs', $this->name() . '::lists/individuals-table-with-patriarchs');
View::registerCustomView('::lists/surnames-table-with-patriarchs', $this->name() . '::lists/surnames-table-with-patriarchs');
View::registerCustomView('::vesta-chart-box', $this->name() . '::chart-box');
$submenus = new Collection();
$submenus->put(1, new ExtendedPedigreeChartModule($this, 'PLACEHOLDER'));
$submenus->put(2, new LCAChartModule($this));
//only need to boot once per class
//$submenus->put(2, new ExtendedPedigreeChartModule($this, ExtendedPedigreeChartModule::KIND_COLLAPSE));
foreach ($submenus as $submenu) {
$submenu->boot();
}
$this->flashWhatsNew('\Cissee\Webtrees\Module\ExtendedRelationships\WhatsNew', 2);
}
protected function editConfigAfterFaq() {
$url = route('module', [
'module' => $this->name(),
'action' => 'AdminSync'
]);
?>
<h1><?php echo I18N::translate('Synchronization'); ?></h1>
<ol class="breadcrumb small">
<li>
<a href="<?php echo $url; ?>">
<?php echo I18N::translate('Synchronize trees to obtain dated relationship links'); ?>
</a>
<?php echo I18N::translate(' (see below for details).'); ?>
</li>
</ol>
<?php
}
public static function getRelationshipLink(
$moduleName,
Tree $tree,
$text,
$xref1,
$xref2,
$mode,
$beforeJD = null) {
if ($text === null) {
//handle this case via special $path?
if ($xref1 === $xref2) {
$rs = \Vesta\VestaUtils::get(RelationshipService::class);
$class = new ReflectionClass($rs);
$reflexivePronounMethod = $class->getMethod('reflexivePronoun');
$reflexivePronounMethod->setAccessible(true);
$indi = Registry::individualFactory()->make($xref1, $tree);
$text = $reflexivePronounMethod->invoke($rs, $indi);
} else {
$slcaController = new ExtendedRelationshipController;
$paths = $slcaController->x_calculateRelationships_123456(
$tree,
$xref1,
$xref2,
$mode,
1,
$beforeJD);
foreach ($paths as $path) {
$relationshipPath = RelationshipPath::create($tree, $path, $beforeJD);
if ($relationshipPath === null) {
// Cannot see one of the families/individuals, due to privacy;
continue;
}
$text = RelationshipUtils::getRelationshipName($relationshipPath);
if ($text === '') {
$text = null;
continue;
}
/*
//TODO: 'getRelationshipName' requires a variant using $beforeJD,
//because 'ex-husband' etc. is not correct at all dates!
//also, 'husband' may not always be correct either, if the marriage e.g. occured after the birth of a child
//once we use additional events to establish family (such as ENGA), it gets more complicated
//should use 'fiancée' etc. at certain dates
*/
break;
}
}
}
if ($text === null) {
$text = CommonI18N::noRelationshipFound();
}
$parameters = [
'ancestors' => $mode
];
if ($beforeJD !== null) {
$parameters['beforeJD'] = $beforeJD;
}
$url = route(static::class, [
'xref' => $xref1,
'xref2' => $xref2,
'tree' => $tree->name(),
] + $parameters + [
'recursion' => self::VESTA_DEFAULT_RECURSION,
]);
return '<a href="' . $url . '" title="' . MoreI18N::xlate('Relationships') . '">' . $text . '</a>';
}
public function getRelationshipLinkForFactsTabFillViaAjax(
$text,
$xref1,
$xref2,
$tree,
$mode,
$beforeJD,
$prefix,
$suffix) {
$toggleableRels = boolval($this->getPreference('FTAB_TOGGLEABLE_RELS', '1'));
$parameters = [
'module' => $this->name(),
'action' => 'Rel',
'xref1' => $xref1,
'xref2' => $xref2,
'mode' => $mode,
'tree' => $tree->name()
];
if ($beforeJD !== null) {
$parameters['beforeJD'] = $beforeJD;
}
if ($text) {
$parameters['text'] = $text;
}
$url = route('module', $parameters);
//escape newlines (e.g. from Individual.getSexImage()), also
//escape prefix/suffix for cases such as
//$suffix = "<i class=\"icon-sex_m_9x9\"></i>";
$prefix = str_replace(array("\n", "\r"), "", addslashes($prefix));
$suffix = str_replace(array("\n", "\r"), "", addslashes($suffix));
//must disambiguate with $beforeJD - may show up multiple times!
//(and technically with everything else that goes into the url)
//also with prefix/suffix, otherwise these get mixed up if same rel is used with different prefix/suffix!
//hash alone would be sufficient, explicit xrefs here only for easier debugging!
$rel = 'rel_' . $xref1 . '_' . $xref2 . '_' . md5($url . $prefix . $suffix);
$main = '';
if (!$toggleableRels) {
$main = "<span class=\"" . $rel . "\"></span>";
} else {
//make toggleable, collapse initially
$main = "<span class=\"toggleableRelsFactstab " . $rel . " collapse\"></span>";
}
ob_start();
if (!$toggleableRels) {
?>
<script>
//load via ajax
console.log("init via ajax <?php echo $rel ?>");
var ajaxRequest = $.get("<?php echo $url ?>");
ajaxRequest.done(function (content) {
$(".<?php echo $rel ?>").html("<?php echo $prefix ?>" + content + "<?php echo $suffix ?>");
})
</script>
<?php
} else {
//print if checkbox is checked (change via persistent toggle)
?>
<script>
$('.<?php echo $rel ?>').on('shown.bs.collapse', function () {
console.log("on shown: check <?php echo $rel ?>");
if ("" === $(".<?php echo $rel ?>").text()) {
//load once via ajax
console.log("on shown: init via ajax <?php echo $rel ?>");
var ajaxRequest = $.get("<?php echo $url ?>");
ajaxRequest.done(function (content) {
$(".<?php echo $rel ?>").html("<?php echo $prefix ?>" + content + "<?php echo $suffix ?>");
});
}
});
</script>
<?php
}
return new GenericViewElement($main, ob_get_clean());
}
//Families Tab
protected function getOutputAfterTab(
$toggleableRels,
$toggle) {
$post = '';
if ($toggleableRels) {
$post = $this->getScript($toggle);
}
return new GenericViewElement('', $post);
}
protected function getScript(
string $toggle) {
ob_start();
?>
<script>
webtrees.persistentToggle(document.querySelector('#<?php echo $toggle; ?>'));
</script>
<?php
return ob_get_clean();
}
protected function getOutputInDescriptionBox(
bool $toggleableRels,
string $id,
string $targetClass,
string $label) {
ob_start();
if ($toggleableRels) {
?>
<label>
<input id="<?php echo $id; ?>" type="checkbox" data-bs-toggle="collapse" data-bs-target=".<?php echo $targetClass; ?>" data-wt-persist="<?php echo $id; ?>" autocomplete="off">
<?php echo I18N::translate($label); ?>
</label>
<?php
}
return new GenericViewElement(ob_get_clean(), '');
}
protected function getOutputAfterDescriptionBox(
Individual $person,
$settingsPrefix,
$mainRels,
$className) {
$mode = intval($this->getPreference($settingsPrefix . 'TAB_REL_TO_DEFAULT_INDI', '1'));
$recursion = intval($this->getPreference('RELATIONSHIP_RECURSION', self::VESTA_DEFAULT_RECURSION));
$showCa = boolval($this->getPreference($settingsPrefix . 'TAB_REL_TO_DEFAULT_INDI_SHOW_CA', '1'));
if ($mode === 0) {
return new GenericViewElement('', '');
}
$toggleableRels = boolval($this->getPreference($settingsPrefix . 'TAB_TOGGLEABLE_RELS', '1'));
//expensive - load async (and only if visible)
//(we have to print via ajax call because we have to indirectly read local storage to determine visibility,
//but it's preferable for faster tab display anyway)
//FunctionsPrintRels::printSlcasWrtDefaultIndividual($controller->record, $mode, $recursion, $showCa);
$xref = $person->xref();
$url = route('module', [
'module' => $this->name(),
'action' => 'MainRels',
'tree' => $person->tree()->name(), //always set the tree (2.x doesn't have default tree via Session class)!
'pid' => $xref,
'mode' => $mode,
'recursion' => $recursion,
'showCa' => $showCa
]);
$main = '';
if (!$toggleableRels) {
$main = "<div class=\"" . $mainRels . "\"><span/></div>";
} else {
//make toggleable, collapse initially
$main = "<div class=\"" . $className . " " . $mainRels . " collapse\"><span/></div>";
}
ob_start();
if (!$toggleableRels) {
?>
<script>
//load via ajax
console.log("init via ajax <?php echo $mainRels ?>");
var ajaxRequest = $.get("<?php echo $url ?>");
ajaxRequest.done(function (content) {
$(".<?php echo $mainRels ?> > span").html(content);
})
</script>
<?php
} else {
//print if checkbox is checked (change via persistent toggle)
?>
<script>
$('.<?php echo $mainRels ?>').on('shown.bs.collapse', function () {
console.log("on shown: check <?php echo $mainRels ?>");
if ("" === $(".<?php echo $mainRels ?>").text()) {
//load once via ajax
console.log("on shown: init via ajax <?php echo $mainRels ?>");
var ajaxRequest = $.get("<?php echo $url ?>");
ajaxRequest.done(function (content) {
$(".<?php echo $mainRels ?> > span").html(content);
});
}
});
</script>
<?php
}
return new GenericViewElement($main, ob_get_clean());
}
protected function getOutputFamilyAfterSubHeaders(Family $family, $type) {
if ('FAMC' === $type) {
$mode = intval($this->getPreference('TAB_REL_OF_PARENTS', '1'));
$recursion = intval($this->getPreference('RELATIONSHIP_RECURSION', self::VESTA_DEFAULT_RECURSION));
$showCa = boolval($this->getPreference('TAB_REL_OF_PARENTS_SHOW_CA', '1'));
} else {
$mode = intval($this->getPreference('TAB_REL_TO_SPOUSE', '1'));
$recursion = intval($this->getPreference('RELATIONSHIP_RECURSION', self::VESTA_DEFAULT_RECURSION));
$showCa = boolval($this->getPreference('TAB_REL_TO_SPOUSE_SHOW_CA', '1'));
}
$useBeforeJD = ($mode == 4) || ($mode == 5) || ($mode == 6) || ($mode == 7);
$beforeJD = null;
if ($useBeforeJD) {
//same strategy as in Sync.php
//'f_from' = 'family established no later than' (= minimum of date of marriage, first childbirth).
$date = ExtendedRelationshipUtils::getFamilyEstablishedNoLaterThan($family);
if ($date->isOK()) {
$beforeJD = $date->minimumJulianDay();
} else {
//no need to load anything!
return GenericViewElement::createEmpty();
}
/*
$date = $family->getMarriageDate();
if ($date->isOK()) {
$beforeJD = $date->minimumJulianDay();
}
*/
}
if ($mode === 0) {
return;
}
$toggleableRels = boolval($this->getPreference('TAB_TOGGLEABLE_RELS', '1'));
//expensive - load async (and only if visible)
//(we have to print via ajax call because we have to indirectly read local storage to determine visibility,
//but it's preferable for faster tab display anyway)
//FunctionsPrintRels::printSlcas($moduleName, $family, $access_level, $mode, $recursion, $showCa);
//TODO: where is the $access_level checked now?
$xref = $family->xref();
$parameters = [
'module' => $this->name(),
'action' => 'FamRels',
'tree' => $family->tree()->name(), //always set the tree (2.x doesn't have default tree via Session class)!
'pid' => $xref,
'mode' => $mode,
'recursion' => $recursion,
'showCa' => $showCa
];
if ($beforeJD) {
$parameters['beforeJD'] = $beforeJD;
}
$url = route('module', $parameters);
//must disambiguate with $beforeJD - may show up multiple times!
//(and technically with everything else that goes into the url)
//hash alone would be sufficient, explicit xrefs here only for easier debugging!
$rel = 'famRels_' . $xref . '_' . md5($url);
$main = '';
if (!$toggleableRels) {
$main = "<span class=\"" . $rel . "\"></span>";
} else {
//make toggleable, collapse initially
$main = "<span class=\"toggleableRels " . $rel . " collapse\"></span>";
}
ob_start();
if (!$toggleableRels) {
?>
<script>
//load via ajax
console.log("init via ajax <?php echo $rel ?>");
var ajaxRequest = $.get("<?php echo $url ?>");
ajaxRequest.done(function (content) {
$(".<?php echo $rel ?>").html(content);
})
</script>
<?php
} else {
//print if checkbox is checked (change via persistent toggle)
?>
<script>
$('.<?php echo $rel ?>').on('shown.bs.collapse', function () {
console.log("on shown: check <?php echo $rel ?>");
if ("" === $(".<?php echo $rel ?>").text()) {
//load once via ajax
console.log("on shown: init via ajax <?php echo $rel ?>");
var ajaxRequest = $.get("<?php echo $url ?>");
ajaxRequest.done(function (content) {
$(".<?php echo $rel ?>").html(content);
});
}
});
</script>
<?php
}
return new GenericViewElement($main, ob_get_clean());
}
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
//Chart
public function chartUrl(Individual $individual, array $parameters = []): string {
return route(static::class, [
'xref' => $individual->xref(),
'tree' => $individual->tree()->name(),
] + $parameters + $this->defaultParameters());
}
public function chartBoxMenu(Individual $individual): ?Menu {
return $this->chartMenu($individual);
}
public function chartMenuClass(): string {
return 'menu-chart-relationship';
}
public function chartMenu(Individual $individual): Menu {
$my_xref = $individual->tree()->getUserPreference(Auth::user(), User::PREF_TREE_ACCOUNT_XREF);
if ($my_xref !== '' && $my_xref !== $individual->xref()) {
$my_record = Registry::individualFactory()->make($my_xref, $individual->tree());
if ($my_record instanceof Individual) {
return new Menu(
$this->getChartTitle(MoreI18N::xlate('Relationship to me')),
$this->chartUrl($my_record, ['xref2' => $individual->xref()]),
$this->chartMenuClass(),
$this->chartUrlAttributes()
);
}
}
return new Menu(
$this->getChartTitle(MoreI18N::xlate('Relationships')),
$this->chartUrl($individual),
$this->chartMenuClass(),
$this->chartUrlAttributes()
);
}
//ok to use this class for ajax requests as long as we fully initialize (session.php) anyway!
//(still ~100ms slower (local server) than using moduleAjax.php directly though, just for resolving via module.php grr)
//otherwise debatable (initialization may be too expensive for larger number of ajax requests, cf gov4webtrees)
//otoh, this approach is expected to be safer wrt rewrite rules etc
public function getMainRelsAction(ServerRequestInterface $request): ResponseInterface {
//'tree' is handled specifically in Router.php
$tree = $request->getAttribute('tree');
assert($tree instanceof Tree);
ob_start();
AjaxRequests::printMainSlcas($this->name(), $request, $tree);
return response(ob_get_clean());
}
public function getFamRelsAction(ServerRequestInterface $request): ResponseInterface {
//'tree' is handled specifically in Router.php
$tree = $request->getAttribute('tree');
assert($tree instanceof Tree);
ob_start();
AjaxRequests::printFamilySlcas($this->name(), $request, $tree);
return response(ob_get_clean());
}
public function getRelAction(ServerRequestInterface $request): ResponseInterface {
//'tree' is handled specifically in Router.php
$tree = $request->getAttribute('tree');
assert($tree instanceof Tree);
$link = AjaxRequests::getRelationshipLink($this->name(), $request, $tree);
return response($link);
}
public function getAdminSyncAction(): ResponseInterface {
return response($this->syncConfig());
}
public function postAdminSyncAction(ServerRequestInterface $request): ResponseInterface {
$timeout_service = \Vesta\VestaUtils::get(TimeoutService::class);
$sync = new Sync($this->name());
return $sync->sync($request, $timeout_service);
}
/*
public function chartUrl(Individual $individual, array $parameters = []): string {
}
*/
public function handle(ServerRequestInterface $request): ResponseInterface {
$tree = $request->getAttribute('tree');
assert($tree instanceof Tree);
$xref = $request->getAttribute('xref');
assert(is_string($xref));
$xref2 = $request->getAttribute('xref2') ?? '';
$ajax = $request->getQueryParams()['ajax'] ?? '';
$ancestors = (int) $request->getAttribute('ancestors');
$recursion = (int) $request->getAttribute('recursion');
$user = $request->getAttribute('user');
Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, $user);
//[RC] block added start
$beforeJD = Requests::getIntOrNull($request, 'beforeJD');
$dateDisplay = null;
if ($beforeJD) {
$ymd = cal_from_jd($beforeJD, CAL_GREGORIAN);
$date = new Date($ymd["day"] . ' ' . strtoupper($ymd["abbrevmonth"]) . ' ' . $ymd["year"]);
$dateDisplay = $date->display();
}
//[RC] block added end
// Convert POST requests into GET requests for pretty URLs.
if ($request->getMethod() === RequestMethodInterface::METHOD_POST) {
$params = (array) $request->getParsedBody();
$parameters = [
'ancestors' => $params['ancestors'],
'recursion' => $params['recursion'],
'tree' => $tree->name(),
'xref' => $params['xref'],
'xref2' => $params['xref2'],
];
if ($beforeJD !== null) {
$parameters['beforeJD'] = $beforeJD;
}
return redirect(route(static::class, $parameters));
}
$individual1 = Registry::individualFactory()->make($xref, $tree);
$individual2 = Registry::individualFactory()->make($xref2, $tree);
//$ancestors_only = (int) $tree->getPreference('RELATIONSHIP_ANCESTORS', static::DEFAULT_ANCESTORS);
//$max_recursion = (int) $tree->getPreference('RELATIONSHIP_RECURSION', static::DEFAULT_RECURSION);
$max_recursion = intval($this->getPreference('RELATIONSHIP_RECURSION', RelationshipsChartModule::DEFAULT_RECURSION));
$recursion = min($recursion, $max_recursion);
if ($individual1 instanceof Individual) {
$individual1 = Auth::checkIndividualAccess($individual1, false, true);
}
if ($individual2 instanceof Individual) {
$individual2 = Auth::checkIndividualAccess($individual2, false, true);
}
if ($individual1 instanceof Individual && $individual2 instanceof Individual) {
if ($ajax === '1') {
$controller = new ExtendedRelationshipsChartController($this);
return $controller->chart($individual1, $individual2, $recursion, $ancestors, $beforeJD);
}
/* I18N: %s are individual’s names */
$title = MoreI18N::xlate('Relationships between %1$s and %2$s', $individual1->fullName(), $individual2->fullName());
$parameters = [
'ajax' => true,
'ancestors' => $ancestors,
'recursion' => $recursion,
'xref2' => $individual2->xref(),
];
if ($beforeJD !== null) {
$parameters['beforeJD'] = $beforeJD;
}
$ajax_url = $this->chartUrl($individual1, $parameters);
} else {
$title = MoreI18N::xlate('Relationships');
$ajax_url = '';
}
//[RC] block added start
$chart1 = ($ancestors == 1) || (boolval($this->getPreference('CHART_1', '1')));
$chart2 = ($ancestors == 2) || (boolval($this->getPreference('CHART_2', '0')));
$chart3 = ($ancestors == 3) || (boolval($this->getPreference('CHART_3', '1')));
$chart4 = ($ancestors == 4) || (boolval($this->getPreference('CHART_4', '1')));
$chart5 = ($ancestors == 5) || (boolval($this->getPreference('CHART_5', '1')));
$chart6 = ($ancestors == 6) || (boolval($this->getPreference('CHART_6', '0')));
$chart7 = ($ancestors == 7) || (boolval($this->getPreference('CHART_7', '0')));
$options1 = [];
$options2 = [];
if ($beforeJD && ($chart4 || $chart5 || $chart6 || $chart7)) {
//use separate options
$this->addAncestorsOptions1($options1, $chart1, $chart2, $chart3);
$this->addAncestorsOptions2($options2, $chart4, $chart5, $chart6, $chart7, $max_recursion);
} else {
//merge options
$this->addAncestorsOptions1($options1, $chart1, $chart2, $chart3);
$this->addAncestorsOptions2($options1, $chart4, $chart5, $chart6, $chart7, $max_recursion);
}
//[RC] block added end
return $this->viewResponse($this->name() . '::page', [
'ajax_url' => $ajax_url,
'ancestors' => $ancestors,
//'ancestors_only' => $ancestors_only,
//'ancestors_options' => $this->ancestorsOptions(),
'ancestors_options1' => $options1,
'ancestors_options2' => $options2,
'individual1' => $individual1,
'individual2' => $individual2,
'max_recursion' => $max_recursion,
'module' => $this->name(),
'recursion' => $recursion,
//'recursion_options' => $this->recursionOptions($max_recursion),
'title' => $title,
'tree' => $tree,
'beforeJD' => $beforeJD,
'dateDisplay' => $dateDisplay,
]);
}
/*
public function getChartAction(ServerRequestInterface $request): ResponseInterface {
//if null, initialized elsewhere if required
$user = $request->getAttribute('user');
//'tree' is handled specifically in Router.php
$tree = $request->getAttribute('tree');
assert($tree instanceof Tree);
$controller = new ExtendedRelationshipsChartController($this);
return $controller->page($request, $tree, $user);
}
//note that RelationshipsChartModule doesn't have separate actions any longer ...
public function getChartActualAction(ServerRequestInterface $request): ResponseInterface {
//'tree' is handled specifically in Router.php
$tree = $request->getAttribute('tree');
assert($tree instanceof Tree);
$controller = new ExtendedRelationshipsChartController($this);
return $controller->chart($request, $tree);
}
*/
public function getHelpAction(ServerRequestInterface $request): ResponseInterface {
$topic = Requests::getString($request, 'topic');
return response(HelpTexts::helpText($topic));
}
private function syncConfig() {
$url = route('module', [
'module' => $this->name(),
'action' => 'AdminSync',
'phase' => 1
]);
// Render the view
$innerHtml = view($this->name() . '::sync', [
'title' => $this->title() . ' — ' . I18N::translate('Synchronization'),
'post' => $url
]);
// Insert the view into the (main) layout
$html = view('layouts/administration', [
'title' => $this->title() . ' — ' . I18N::translate('Synchronization'),
'content' => $innerHtml
]);
return $html;
}
//IndividualFactsTabExtenderInterface
public function hFactsTabGetOutputForAssoRel(
Fact $event,
Individual $person,
Individual $associate,
$relationship_prefix,
$relationship_name,
$relationship_suffix,
$inverse) {
$tag = explode(':', $event->tag())[1];
if ($inverse) {
$restricted = (boolean) $this->getPreference('TAB_REL_TO_ASSO_RESTRICTED', '0');
if ($restricted) {
$parent = $event->record();
if ($parent instanceof Family) {
$restrictedTo = preg_split("/[, ;:]+/", $this->getPreference('TAB_REL_TO_ASSO_RESTRICTED_FAM', 'MARR'), -1, PREG_SPLIT_NO_EMPTY);
if (!in_array($tag, $restrictedTo, true)) {
return null;
}
} else {
$restrictedTo = preg_split("/[, ;:]+/", $this->getPreference('TAB_REL_TO_ASSO_RESTRICTED_INDI', 'CHR,BAPM'), -1, PREG_SPLIT_NO_EMPTY);
if (!in_array($tag, $restrictedTo, true)) {
return null;
}
}
}
}
//TODO: check if chart is available! otherwise, don't link to it.
//(also check if respective option is available in chart?)
//$relationship_name NOT used: we may use different reationship here!
//for now, allow the day of the event as well "+1"
//otherwise, we wouldn't obtain anything e.g. for baptisms on the date of birth
//but only for certain events:
//this is undesirable for events establishing additional relationships, i.e. MARR
//e.g. we don't want trivial relation to best man of husband as brother-in-law
$offset = 1;
if ('MARR' === $tag) {
$offset = 0;
}
$mode = (int) $this->getPreference('TAB_REL_TO_ASSO', '15');