-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodule.php
More file actions
526 lines (461 loc) · 17.6 KB
/
module.php
File metadata and controls
526 lines (461 loc) · 17.6 KB
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
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2025 webtrees development team
* GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
*/
/**
* Module Display Sosa - Copyright (C) 2012 André DUMAS http://www.adumas.1s.fr/wt/
* AD 2014-03-17 sosa-2-5-2 for WT 1-5-2
*/
/**
* http://gustine.eu/mode_emploi/sosa.php
* MR 2014-10 update for webtrees 1.6.0 and addition of local language translation.
* MR 2015-03 update for webtrees 1.7.0 (namespace usage). All functions are combined in a single file.
* MR 2022-01 update for webtrees 2.0 and 2.1 (doesn’t work with webtrees 1.7).
* MR 2023-03 use vesta_extended_relationships if exists.
* MR 2023-07 add close relationship to me (registered user only).
* bernatbanyuls 2024-09 translations into Spanish and Catalan.
┌──────────────────────────────────────────────┐
│ MR 2024-11 webtrees 2.2 compatibility update │
└──────────────────────────────────────────────┘
* MR 2024-11 compact mode : hide the title of the Sosa block.
* MR 2025-02 if img/symbols.png is present, add small fan-shaped diagrams and display the relevant great-grandparent.
* MR 2025-05 the memory used by the update is limited to avoid overflows and the calculation is stopped when all the Sosa numbers have been found.
* MR 2025-06 new algorithm slower but better suited to large numbers of generations.
* MR 2025-06 bug in update.phtml : ($gen+1).
* RickM 2025-09 management of feminine ordinal number suffixes (generation).
* MR 2025-10 if img/image.png is present, display the user's Sosa numbers (requires adding a column to the wt_sosa table).
* MR 2025-10 built-in help page instead of a link to gustine.eu.
* MR 2025-11 a settings page makes it easy to manage multiple trees (profile image, symbols, number of generations).
* MR 2025-11 when neither vesta_extended_relationship nor relationship-chart are enabled.
* MR 2025-12 when memory_limit == -1 (no limit).
* MR 2026-01 when vesta_extended_relationship is enabled but vesta_common is disabled (for example, if the version of vesta is lower than that of webtrees).
*/
declare(strict_types=1);
use Fig\Http\Message\StatusCodeInterface;
use Fisharebest\Localization\Translation;
use Fisharebest\Webtrees\FlashMessages;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Module\AbstractModule;
use Fisharebest\Webtrees\Module\ModuleConfigInterface;
use Fisharebest\Webtrees\Module\ModuleConfigTrait;
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
use Fisharebest\Webtrees\Module\ModuleCustomTrait;
use Fisharebest\Webtrees\Module\ModuleSidebarInterface;
use Fisharebest\Webtrees\Module\ModuleSidebarTrait;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Services\TreeService;
use Fisharebest\Webtrees\Services\UserService;
use Fisharebest\Webtrees\View;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\Webtrees;
use Fisharebest\Webtrees\Auth;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Illuminate\Database\Capsule\Manager as DB;
/**
* A sidebar to show Sosa-Stradonitz informations of an individual
*/
class SosaModule extends AbstractModule implements ModuleConfigInterface, ModuleCustomInterface, ModuleSidebarInterface
{
use ModuleConfigTrait;
use ModuleCustomTrait;
use ModuleSidebarTrait;
// ┌─ webtrees 2.0 to 2.2 ────────────────────────
public const CUSTOM_VERSION = '2025.12.17';
public const GITHUB_REPO = 'Gustine/sosa20';
// └──────────────────────────────────────────────
// Github API URL to get the information about the latest releases
public const GITHUB_API_LATEST_VERSION = 'https://api.github.com/repos/'. self::GITHUB_REPO . '/releases/latest';
public const GITHUB_API_TAG_NAME_PREFIX = '"tag_name":"';
/** @var TreeService */
private $tree_service;
/**
* How should this module be identified in the control panel, etc.?
* @return string
*/
public function title(): string
{
return I18N::translate('Sosa-Stradonitz (Ahnentafel)');
}
/**
* A sentence describing what this module does.
* @return string
*/
public function description(): string
{
return I18N::translate('A sidebar showing Sosa-Stradonitz number of individuals.');
}
/**
* The person or organisation who created this module.
* @return string
*/
public function customModuleAuthorName(): string
{
return 'gustine@ovh.fr';
}
/**
* The version of this module (format : 123.123.123)
* @return string
*/
public function customModuleVersion(): string
{
return self::CUSTOM_VERSION;
}
/**
* Fetch the latest version of this module.
* @return string
*/
public function customModuleLatestVersion(): string
{
// No update URL provided.
if (self::GITHUB_API_LATEST_VERSION === '') {
return $this->customModuleVersion();
}
return Registry::cache()->file()->remember(
$this->name() . '-latest-version',
function (): string {
try {
$client = new Client(
[
'timeout' => 3,
]
);
$response = $client->get(self::GITHUB_API_LATEST_VERSION);
if ($response->getStatusCode() === StatusCodeInterface::STATUS_OK) {
$content = $response->getBody()->getContents();
preg_match_all('/' . self::GITHUB_API_TAG_NAME_PREFIX . '\d+\.\d+\.\d+/', $content, $matches, PREG_OFFSET_CAPTURE);
if(!empty($matches[0]))
{
$version = $matches[0][0][0];
$version = substr($version, strlen(self::GITHUB_API_TAG_NAME_PREFIX));
}
else
{
$version = $this->customModuleVersion();
}
return $version;
}
} catch (GuzzleException $ex) {
// Can't connect to the server?
}
return $this->customModuleVersion();
},
86400
);
}
/**
* Where to get support for this module.
* @return string
*/
public function customModuleSupportUrl(): string
{
return 'https://github.com/' . self::GITHUB_REPO;
}
/**
* Bootstrap the module
*/
public function boot(): void
{
// Register a namespace for our views.
View::registerNamespace($this->name(), $this->resourcesFolder() . 'views/');
}
public function __construct(TreeService $tree_service)
{
$this->tree_service = $tree_service;
}
/**
* Where does this module store its resources
* @return string
*/
public function resourcesFolder(): string
{
return __DIR__ . '/resources/';
}
/**
* The default position for this sidebar. It can be changed in the control panel.
* @return int
*/
public function defaultSidebarOrder(): int
{
return -1;
}
/**
* @param Individual $individual
* @return bool
*/
public function hasSidebarContent(Individual $individual): bool
{
return true;
}
/**
* Load this sidebar synchronously.
*
* @param Individual $individual
* @return string
*/
public function getSidebarContent(Individual $individual): string
{
// Detects if the module vesta_extended_relationships is installed AND enabled, AND if vesta_common is enabled.
$status_extended = DB::table('module')
->where('module_name', '=', '_vesta_extended_relationships_')
->value('status') ;
$status_common = DB::table('module')
->where('module_name', '=', '_vesta_common_')
->value('status') ;
if ( ($status_extended === 'enabled') && ($status_common === 'enabled') && file_exists(__DIR__ . '/../vesta_extended_relationships/module.php') ) $vesta_extended = 1;
else {
$status = DB::table('module')
->where('module_name', '=', 'relationships_chart')
->value('status') ;
if ($status === 'enabled') $vesta_extended = 0; else $vesta_extended = -1;
}
$tree = $individual->tree();
$tree_id = $tree->id();
$ssbranch_gen = (int) $this->getPreference($tree_id . '-ssbranch_level', '3') +1;
$symbols_file = $this->getPreference($tree_id . '-symbols_file', 'symbols8.png');
if ( ($symbols_file !== '') && file_exists(__DIR__ . '/resources/img/' . $symbols_file) ) $urlsymbols = $this->assetUrl('img/' . $symbols_file);
else $urlsymbols = '';
$own_numbers = $this->getPreference($tree_id . '-own_numbers', '1');
$profile_file = $this->getPreference($tree_id . '-profile_file', 'image.webp');
if ( ($profile_file !== '') && file_exists(__DIR__ . '/resources/img/' . $profile_file) ) $urlimage = $this->assetUrl('img/' . $profile_file);
else $urlimage = '';
// Get the signed in user
$user_service = new UserService();
$user = $user_service->find(Auth::id());
return view($this->name() . '::sidebar', [
'individual' => $individual,
'vesta_extended' => $vesta_extended,
'ssbranch_gen' => $ssbranch_gen,
'urlsymbols' => $urlsymbols,
'own_numbers' => (int) $own_numbers,
'urlimage' => $urlimage,
'user' => $user,
]);
}
/**
* The getUpdateAction function replies to "action=Update".
*
* title : (required!) the title of the called view
* bk_gid : identifier of the current gedcom
* bk_xref : xref of the current individual (we want to go back to his page after processing)
* up_gid : identifier of the gedcom to update
* up_xref : root xref of the gedcom to update
* user_id : identifier of the user to update
* @return ResponseInterface
*/
public function getUpdateAction(ServerRequestInterface $request): ResponseInterface
{
$bk_gid = $request->getQueryParams()['bk_gid'];
$bk_xref = $request->getQueryParams()['bk_xref'];
$up_gid = $request->getQueryParams()['up_gid'];
$up_xref = $request->getQueryParams()['up_xref'];
$user_id = $request->getQueryParams()['user_id'];
if (version_compare(Webtrees::VERSION, '2.2.0', '>=')) {
$tree = Registry::container()->get(TreeService::class)->find(intval($bk_gid, 10));
}
else {
$tree = app(TreeService::class)->find(intval($bk_gid, 10));
}
return $this->viewResponse($this->name() . '::update', [
'title' => I18N::translate("Updating Sosa numbers"),
'tree' => $tree,
'bk_xref' => $bk_xref,
'up_gid' => $up_gid,
'up_xref' => $up_xref,
'user_id' => $user_id,
]);
}
/**
* The getCreateAction function replies to "action=Create".
*
* title : (required!) the title of the called view
* bk_gid : identifier of the current gedcom
* bk_xref : xref of the current individual (we want to go back to his page after processing)
* @return ResponseInterface
*/
public function getCreateAction(ServerRequestInterface $request): ResponseInterface
{
$bk_gid = $request->getQueryParams()['bk_gid'];
$bk_xref = $request->getQueryParams()['bk_xref'];
if (version_compare(Webtrees::VERSION, '2.2.0', '>=')) {
$tree = Registry::container()->get(TreeService::class)->find(intval($bk_gid, 10));
} else {
$tree = app(TreeService::class)->find(intval($bk_gid, 10));
}
return $this->viewResponse($this->name() . '::migration', [
'title' => I18N::translate("Creation"),
'tree' => $tree,
'bk_xref' => $bk_xref,
]);
}
/**
* The getMigrateAction function replies to "action=Migrate".
*
* title : (required!) the title of the called view
* bk_gid : identifier of the current gedcom
* bk_xref : xref of the current individual (we want to go back to his page after processing)
* @return ResponseInterface
*/
public function getMigrateAction(ServerRequestInterface $request): ResponseInterface
{
$bk_gid = $request->getQueryParams()['bk_gid'];
$bk_xref = $request->getQueryParams()['bk_xref'];
if (version_compare(Webtrees::VERSION, '2.2.0', '>=')) {
$tree = Registry::container()->get(TreeService::class)->find(intval($bk_gid, 10));
} else {
$tree = app(TreeService::class)->find(intval($bk_gid, 10));
}
return $this->viewResponse($this->name() . '::migration', [
'title' => I18N::translate("Migration"),
'tree' => $tree,
'bk_xref' => $bk_xref,
]);
}
/**
* The getHelpAction function replies to "action=Help".
*
* title : (required!) the title of the called view
* urlsymbols : if absent, grandparents are not displayed
* urlimage : if absent, numbers related to the user are not displayed
* bk_gid : identifier of the current gedcom
* bk_xref : xref of the current individual (we want to go back to his page after processing)
* @return ResponseInterface
*/
public function getHelpAction(ServerRequestInterface $request): ResponseInterface
{
// Back to previous page
$bk_gid = $request->getQueryParams()['bk_gid'];
$bk_xref = $request->getQueryParams()['bk_xref'];
if (version_compare(Webtrees::VERSION, '2.2.0', '>=')) {
$tree = Registry::container()->get(TreeService::class)->find(intval($bk_gid, 10));
} else {
$tree = app(TreeService::class)->find(intval($bk_gid, 10));
}
$ssbranch_gen = (int) $this->getPreference($bk_gid . '-ssbranch_level', '3') +1;
$symbols_file = $this->getPreference($bk_gid . '-symbols_file', 'symbols8.png');
if ( ($symbols_file !== '') && file_exists(__DIR__ . '/resources/img/' . $symbols_file) ) $urlsymbols = $this->assetUrl('img/' . $symbols_file);
else $urlsymbols = '';
$own_numbers = $this->getPreference($bk_gid . '-own_numbers', '1');
$profile_file = $this->getPreference($bk_gid . '-profile_file', 'image.webp');
if ( ($profile_file !== '') && file_exists(__DIR__ . '/resources/img/' . $profile_file) ) $urlimage = $this->assetUrl('img/' . $profile_file);
else $urlimage = '';
// Get the signed in user
$user_service = new UserService();
$user = $user_service->find(Auth::id());
// Check for help.png
if ( file_exists(__DIR__ . '/resources/img/help.png') ) $urlhelp = $this->assetUrl('img/help.png');
else $urlhelp = '';
return $this->viewResponse($this->name() . '::help', [
'title' => I18N::translate("Help"),
'tree' => $tree,
'ssbranch_gen' => $ssbranch_gen,
'urlsymbols' => $urlsymbols,
'own_numbers' => (int) $own_numbers,
'urlimage' => $urlimage,
'urlhelp' => $urlhelp,
'user' => $user,
'bk_xref' => $bk_xref,
]);
}
/**
* This administration page can be accessed from Control panel / All modules
*
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
public function getAdminAction(ServerRequestInterface $request): ResponseInterface
{
$this->layout = 'layouts/administration';
$tree_id = $this->getPreference('last-tree-id', '');
try {
$tree = $this->tree_service->find((int)$tree_id);
} catch (Exception $ex) {
// the last tree saved doesn't exist anymore. Use the first tree instead.
$tree = $this->tree_service->all()->first();
$tree_id = $tree->id();
// remove settings from non existing tree from the database
DB::table('module_setting')
->where('module_name', '=', $this->name())
->where('setting_name', 'LIKE', '' . $this->getPreference('last-tree-id') . '-%' )
->delete();
// reset the last tree id
$this->setPreference('last-tree-id', '');
}
$reset_param = $this->getPreference($tree_id . '-reset_param', '0');
if ($reset_param === '1') {
return $this->viewResponse($this->name() . '::settings', [
'all_trees' => $this->tree_service->all(),
'title' => $this->title(),
'tree_id' => $tree_id,
'ssbranch_level' => '3',
'symbols_file' => 'symbols8.png',
'own_numbers' => '1',
'profile_file' => 'image.webp',
'reset_param' => $reset_param,
]);
}
else {
$ssbranch_level = $this->getPreference($tree_id . '-ssbranch_level', '3');
$symbols_file = $this->getPreference($tree_id . '-symbols_file', 'symbols8.png');
$own_numbers = $this->getPreference($tree_id . '-own_numbers', '1');
$profile_file = $this->getPreference($tree_id . '-profile_file', 'image.webp');
return $this->viewResponse($this->name() . '::settings', [
'all_trees' => $this->tree_service->all(),
'title' => $this->title(),
'tree_id' => $tree_id,
'ssbranch_level' => $ssbranch_level,
'symbols_file' => $symbols_file,
'own_numbers' => $own_numbers,
'profile_file' => $profile_file,
'reset_param' => $reset_param,
]);
}
}
/**
* Save the user preference.
*
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
public function postAdminAction(ServerRequestInterface $request): ResponseInterface
{
$params = (array) $request->getParsedBody();
// store the preferences in the database when editing the form
$tree_id = $params['tree-id'];
$this->setPreference('last-tree-id', $tree_id);
if ($params['refresh'] === '1') {
$this->setPreference($tree_id . '-reset_param', $params['reset_param'] ?? '0');
}
if ($params['save'] === '1') {
$this->setPreference($tree_id . '-reset_param', '0');
$this->setPreference($tree_id . '-ssbranch_level', $params['ssbranch_level']);
$this->setPreference($tree_id . '-symbols_file', $params['symbols_file']);
$this->setPreference($tree_id . '-own_numbers', $params['own_numbers']);
$this->setPreference($tree_id . '-profile_file', $params['profile_file']);
$message = I18N::translate('The settings for the family tree “%s” have been updated.', $this->title());
FlashMessages::addMessage($message, 'success');
}
return redirect($this->getConfigLink());
}
/**
* Additional/updated translations.
* @param string $language
* @return array
*/
public function customTranslations(string $language): array
{
$file = $this->resourcesFolder() . 'lang/' . $language . '.php';
return file_exists($file) ? (new Translation($file))->asArray() : [];
}
}; // end class
if (version_compare(Webtrees::VERSION, '2.2.0', '>=')) {
return Registry::container()->get(SosaModule::class);
}
else {
return app(SosaModule::class);
}