1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Aura\Router\Route; 23use Fisharebest\Webtrees\Auth; 24use Fisharebest\Webtrees\Family; 25use Fisharebest\Webtrees\Gedcom; 26use Fisharebest\Webtrees\GedcomRecord; 27use Fisharebest\Webtrees\Http\RequestHandlers\FamilyPage; 28use Fisharebest\Webtrees\Http\RequestHandlers\IndividualPage; 29use Fisharebest\Webtrees\Http\RequestHandlers\LocationPage; 30use Fisharebest\Webtrees\Http\RequestHandlers\MediaPage; 31use Fisharebest\Webtrees\Http\RequestHandlers\NotePage; 32use Fisharebest\Webtrees\Http\RequestHandlers\RepositoryPage; 33use Fisharebest\Webtrees\Http\RequestHandlers\SourcePage; 34use Fisharebest\Webtrees\Http\RequestHandlers\SubmitterPage; 35use Fisharebest\Webtrees\I18N; 36use Fisharebest\Webtrees\Individual; 37use Fisharebest\Webtrees\Location; 38use Fisharebest\Webtrees\Media; 39use Fisharebest\Webtrees\Menu; 40use Fisharebest\Webtrees\Note; 41use Fisharebest\Webtrees\Registry; 42use Fisharebest\Webtrees\Repository; 43use Fisharebest\Webtrees\Services\GedcomExportService; 44use Fisharebest\Webtrees\Services\UserService; 45use Fisharebest\Webtrees\Session; 46use Fisharebest\Webtrees\Source; 47use Fisharebest\Webtrees\Submitter; 48use Fisharebest\Webtrees\Tree; 49use Illuminate\Support\Collection; 50use League\Flysystem\Filesystem; 51use League\Flysystem\FilesystemException; 52use League\Flysystem\ZipArchive\FilesystemZipArchiveProvider; 53use League\Flysystem\ZipArchive\ZipArchiveAdapter; 54use Psr\Http\Message\ResponseInterface; 55use Psr\Http\Message\ServerRequestInterface; 56use RuntimeException; 57 58use function app; 59use function array_filter; 60use function array_keys; 61use function array_map; 62use function array_search; 63use function assert; 64use function fopen; 65use function in_array; 66use function is_string; 67use function preg_match_all; 68use function redirect; 69use function rewind; 70use function route; 71use function str_replace; 72use function stream_get_meta_data; 73use function tmpfile; 74use function uasort; 75use function view; 76 77use const PREG_SET_ORDER; 78 79/** 80 * Class ClippingsCartModule 81 */ 82class ClippingsCartModule extends AbstractModule implements ModuleMenuInterface 83{ 84 use ModuleMenuTrait; 85 86 // What to add to the cart? 87 private const ADD_RECORD_ONLY = 'record'; 88 private const ADD_CHILDREN = 'children'; 89 private const ADD_DESCENDANTS = 'descendants'; 90 private const ADD_PARENT_FAMILIES = 'parents'; 91 private const ADD_SPOUSE_FAMILIES = 'spouses'; 92 private const ADD_ANCESTORS = 'ancestors'; 93 private const ADD_ANCESTOR_FAMILIES = 'families'; 94 private const ADD_LINKED_INDIVIDUALS = 'linked'; 95 96 // Routes that have a record which can be added to the clipboard 97 private const ROUTES_WITH_RECORDS = [ 98 'Family' => FamilyPage::class, 99 'Individual' => IndividualPage::class, 100 'Media' => MediaPage::class, 101 'Location' => LocationPage::class, 102 'Note' => NotePage::class, 103 'Repository' => RepositoryPage::class, 104 'Source' => SourcePage::class, 105 'Submitter' => SubmitterPage::class, 106 ]; 107 108 /** @var int The default access level for this module. It can be changed in the control panel. */ 109 protected $access_level = Auth::PRIV_USER; 110 111 /** @var GedcomExportService */ 112 private $gedcom_export_service; 113 114 /** @var UserService */ 115 private $user_service; 116 117 /** 118 * ClippingsCartModule constructor. 119 * 120 * @param GedcomExportService $gedcom_export_service 121 * @param UserService $user_service 122 */ 123 public function __construct(GedcomExportService $gedcom_export_service, UserService $user_service) 124 { 125 $this->gedcom_export_service = $gedcom_export_service; 126 $this->user_service = $user_service; 127 } 128 129 /** 130 * A sentence describing what this module does. 131 * 132 * @return string 133 */ 134 public function description(): string 135 { 136 /* I18N: Description of the “Clippings cart” module */ 137 return I18N::translate('Select records from your family tree and save them as a GEDCOM file.'); 138 } 139 140 /** 141 * The default position for this menu. It can be changed in the control panel. 142 * 143 * @return int 144 */ 145 public function defaultMenuOrder(): int 146 { 147 return 6; 148 } 149 150 /** 151 * A menu, to be added to the main application menu. 152 * 153 * @param Tree $tree 154 * 155 * @return Menu|null 156 */ 157 public function getMenu(Tree $tree): ?Menu 158 { 159 /** @var ServerRequestInterface $request */ 160 $request = app(ServerRequestInterface::class); 161 162 $route = $request->getAttribute('route'); 163 assert($route instanceof Route); 164 165 $cart = Session::get('cart', []); 166 $count = count($cart[$tree->name()] ?? []); 167 $badge = view('components/badge', ['count' => $count]); 168 169 $submenus = [ 170 new Menu($this->title() . ' ' . $badge, route('module', [ 171 'module' => $this->name(), 172 'action' => 'Show', 173 'tree' => $tree->name(), 174 ]), 'menu-clippings-cart', ['rel' => 'nofollow']), 175 ]; 176 177 $action = array_search($route->name, self::ROUTES_WITH_RECORDS, true); 178 if ($action !== false) { 179 $xref = $route->attributes['xref']; 180 assert(is_string($xref)); 181 182 $add_route = route('module', [ 183 'module' => $this->name(), 184 'action' => 'Add' . $action, 185 'xref' => $xref, 186 'tree' => $tree->name(), 187 ]); 188 189 $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']); 190 } 191 192 if (!$this->isCartEmpty($tree)) { 193 $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [ 194 'module' => $this->name(), 195 'action' => 'Empty', 196 'tree' => $tree->name(), 197 ]), 'menu-clippings-empty', ['rel' => 'nofollow']); 198 199 $submenus[] = new Menu(I18N::translate('Download'), route('module', [ 200 'module' => $this->name(), 201 'action' => 'DownloadForm', 202 'tree' => $tree->name(), 203 ]), 'menu-clippings-download', ['rel' => 'nofollow']); 204 } 205 206 return new Menu($this->title(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus); 207 } 208 209 /** 210 * How should this module be identified in the control panel, etc.? 211 * 212 * @return string 213 */ 214 public function title(): string 215 { 216 /* I18N: Name of a module */ 217 return I18N::translate('Clippings cart'); 218 } 219 220 /** 221 * @param Tree $tree 222 * 223 * @return bool 224 */ 225 private function isCartEmpty(Tree $tree): bool 226 { 227 $cart = Session::get('cart', []); 228 $contents = $cart[$tree->name()] ?? []; 229 230 return $contents === []; 231 } 232 233 /** 234 * @param ServerRequestInterface $request 235 * 236 * @return ResponseInterface 237 */ 238 public function getDownloadFormAction(ServerRequestInterface $request): ResponseInterface 239 { 240 $tree = $request->getAttribute('tree'); 241 assert($tree instanceof Tree); 242 243 $user = $request->getAttribute('user'); 244 $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download'); 245 246 return $this->viewResponse('modules/clippings/download', [ 247 'is_manager' => Auth::isManager($tree, $user), 248 'is_member' => Auth::isMember($tree, $user), 249 'module' => $this->name(), 250 'title' => $title, 251 'tree' => $tree, 252 ]); 253 } 254 255 /** 256 * @param ServerRequestInterface $request 257 * 258 * @return ResponseInterface 259 * @throws FilesystemException 260 */ 261 public function postDownloadAction(ServerRequestInterface $request): ResponseInterface 262 { 263 $tree = $request->getAttribute('tree'); 264 assert($tree instanceof Tree); 265 266 $data_filesystem = Registry::filesystem()->data(); 267 268 $params = (array) $request->getParsedBody(); 269 270 $privatize_export = $params['privatize_export'] ?? 'none'; 271 272 if ($privatize_export === 'none' && !Auth::isManager($tree)) { 273 $privatize_export = 'member'; 274 } 275 276 if ($privatize_export === 'gedadmin' && !Auth::isManager($tree)) { 277 $privatize_export = 'member'; 278 } 279 280 if ($privatize_export === 'user' && !Auth::isMember($tree)) { 281 $privatize_export = 'visitor'; 282 } 283 284 $convert = (bool) ($params['convert'] ?? false); 285 286 $cart = Session::get('cart', []); 287 288 $xrefs = array_keys($cart[$tree->name()] ?? []); 289 $xrefs = array_map('strval', $xrefs); // PHP converts numeric keys to integers. 290 291 // Create a new/empty .ZIP file 292 $temp_zip_file = stream_get_meta_data(tmpfile())['uri']; 293 $zip_provider = new FilesystemZipArchiveProvider($temp_zip_file, 0755); 294 $zip_adapter = new ZipArchiveAdapter($zip_provider); 295 $zip_filesystem = new Filesystem($zip_adapter); 296 297 $media_filesystem = $tree->mediaFilesystem($data_filesystem); 298 299 // Media file prefix 300 $path = $tree->getPreference('MEDIA_DIRECTORY'); 301 302 $encoding = $convert ? 'ANSI' : 'UTF-8'; 303 304 $records = new Collection(); 305 306 switch ($privatize_export) { 307 case 'gedadmin': 308 $access_level = Auth::PRIV_NONE; 309 break; 310 case 'user': 311 $access_level = Auth::PRIV_USER; 312 break; 313 case 'visitor': 314 $access_level = Auth::PRIV_PRIVATE; 315 break; 316 case 'none': 317 default: 318 $access_level = Auth::PRIV_HIDE; 319 break; 320 } 321 322 foreach ($xrefs as $xref) { 323 $object = Registry::gedcomRecordFactory()->make($xref, $tree); 324 // The object may have been deleted since we added it to the cart.... 325 if ($object instanceof GedcomRecord) { 326 $record = $object->privatizeGedcom($access_level); 327 // Remove links to objects that aren't in the cart 328 preg_match_all('/\n1 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER); 329 foreach ($matches as $match) { 330 if (!in_array($match[1], $xrefs, true)) { 331 $record = str_replace($match[0], '', $record); 332 } 333 } 334 preg_match_all('/\n2 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER); 335 foreach ($matches as $match) { 336 if (!in_array($match[1], $xrefs, true)) { 337 $record = str_replace($match[0], '', $record); 338 } 339 } 340 preg_match_all('/\n3 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER); 341 foreach ($matches as $match) { 342 if (!in_array($match[1], $xrefs, true)) { 343 $record = str_replace($match[0], '', $record); 344 } 345 } 346 347 if ($object instanceof Individual || $object instanceof Family) { 348 $records->add($record . "\n1 SOUR @WEBTREES@\n2 PAGE " . $object->url()); 349 } elseif ($object instanceof Source) { 350 $records->add($record . "\n1 NOTE " . $object->url()); 351 } elseif ($object instanceof Media) { 352 // Add the media files to the archive 353 foreach ($object->mediaFiles() as $media_file) { 354 $from = $media_file->filename(); 355 $to = $path . $media_file->filename(); 356 if (!$media_file->isExternal() && $media_filesystem->fileExists($from)) { 357 $zip_filesystem->writeStream($to, $media_filesystem->readStream($from)); 358 } 359 } 360 $records->add($record); 361 } else { 362 $records->add($record); 363 } 364 } 365 } 366 367 $base_url = $request->getAttribute('base_url'); 368 369 // Create a source, to indicate the source of the data. 370 $record = "0 @WEBTREES@ SOUR\n1 TITL " . $base_url; 371 $author = $this->user_service->find((int) $tree->getPreference('CONTACT_USER_ID')); 372 if ($author !== null) { 373 $record .= "\n1 AUTH " . $author->realName(); 374 } 375 $records->add($record); 376 377 $stream = fopen('php://temp', 'wb+'); 378 379 if ($stream === false) { 380 throw new RuntimeException('Failed to create temporary stream'); 381 } 382 383 // We have already applied privacy filtering, so do not do it again. 384 $this->gedcom_export_service->export($tree, $stream, false, $encoding, Auth::PRIV_HIDE, $path, $records); 385 rewind($stream); 386 387 // Finally add the GEDCOM file to the .ZIP file. 388 $zip_filesystem->writeStream('clippings.ged', $stream); 389 390 // Use a stream, so that we do not have to load the entire file into memory. 391 $stream = Registry::streamFactory()->createStreamFromFile($temp_zip_file); 392 393 return Registry::responseFactory() 394 ->createResponse() 395 ->withBody($stream) 396 ->withHeader('Content-Type', 'application/zip') 397 ->withHeader('Content-Disposition', 'attachment; filename="clippings.zip'); 398 } 399 400 /** 401 * @param ServerRequestInterface $request 402 * 403 * @return ResponseInterface 404 */ 405 public function getEmptyAction(ServerRequestInterface $request): ResponseInterface 406 { 407 $tree = $request->getAttribute('tree'); 408 assert($tree instanceof Tree); 409 410 $cart = Session::get('cart', []); 411 $cart[$tree->name()] = []; 412 Session::put('cart', $cart); 413 414 $url = route('module', [ 415 'module' => $this->name(), 416 'action' => 'Show', 417 'tree' => $tree->name(), 418 ]); 419 420 return redirect($url); 421 } 422 423 /** 424 * @param ServerRequestInterface $request 425 * 426 * @return ResponseInterface 427 */ 428 public function postRemoveAction(ServerRequestInterface $request): ResponseInterface 429 { 430 $tree = $request->getAttribute('tree'); 431 assert($tree instanceof Tree); 432 433 $xref = $request->getQueryParams()['xref'] ?? ''; 434 435 $cart = Session::get('cart', []); 436 unset($cart[$tree->name()][$xref]); 437 Session::put('cart', $cart); 438 439 $url = route('module', [ 440 'module' => $this->name(), 441 'action' => 'Show', 442 'tree' => $tree->name(), 443 ]); 444 445 return redirect($url); 446 } 447 448 /** 449 * @param ServerRequestInterface $request 450 * 451 * @return ResponseInterface 452 */ 453 public function getShowAction(ServerRequestInterface $request): ResponseInterface 454 { 455 $tree = $request->getAttribute('tree'); 456 assert($tree instanceof Tree); 457 458 return $this->viewResponse('modules/clippings/show', [ 459 'module' => $this->name(), 460 'records' => $this->allRecordsInCart($tree), 461 'title' => I18N::translate('Family tree clippings cart'), 462 'tree' => $tree, 463 ]); 464 } 465 466 /** 467 * Get all the records in the cart. 468 * 469 * @param Tree $tree 470 * 471 * @return GedcomRecord[] 472 */ 473 private function allRecordsInCart(Tree $tree): array 474 { 475 $cart = Session::get('cart', []); 476 477 $xrefs = array_keys($cart[$tree->name()] ?? []); 478 $xrefs = array_map('strval', $xrefs); // PHP converts numeric keys to integers. 479 480 // Fetch all the records in the cart. 481 $records = array_map(static function (string $xref) use ($tree): ?GedcomRecord { 482 return Registry::gedcomRecordFactory()->make($xref, $tree); 483 }, $xrefs); 484 485 // Some records may have been deleted after they were added to the cart. 486 $records = array_filter($records); 487 488 // Group and sort. 489 uasort($records, static function (GedcomRecord $x, GedcomRecord $y): int { 490 return $x->tag() <=> $y->tag() ?: GedcomRecord::nameComparator()($x, $y); 491 }); 492 493 return $records; 494 } 495 496 /** 497 * @param ServerRequestInterface $request 498 * 499 * @return ResponseInterface 500 */ 501 public function getAddFamilyAction(ServerRequestInterface $request): ResponseInterface 502 { 503 $tree = $request->getAttribute('tree'); 504 assert($tree instanceof Tree); 505 506 $xref = $request->getQueryParams()['xref'] ?? ''; 507 508 $family = Registry::familyFactory()->make($xref, $tree); 509 $family = Auth::checkFamilyAccess($family); 510 $name = $family->fullName(); 511 512 $options = [ 513 self::ADD_RECORD_ONLY => $name, 514 /* I18N: %s is a family (husband + wife) */ 515 self::ADD_CHILDREN => I18N::translate('%s and their children', $name), 516 /* I18N: %s is a family (husband + wife) */ 517 self::ADD_DESCENDANTS => I18N::translate('%s and their descendants', $name), 518 ]; 519 520 $title = I18N::translate('Add %s to the clippings cart', $name); 521 522 return $this->viewResponse('modules/clippings/add-options', [ 523 'options' => $options, 524 'record' => $family, 525 'title' => $title, 526 'tree' => $tree, 527 ]); 528 } 529 530 /** 531 * @param ServerRequestInterface $request 532 * 533 * @return ResponseInterface 534 */ 535 public function postAddFamilyAction(ServerRequestInterface $request): ResponseInterface 536 { 537 $tree = $request->getAttribute('tree'); 538 assert($tree instanceof Tree); 539 540 $params = (array) $request->getParsedBody(); 541 542 $xref = $params['xref'] ?? ''; 543 $option = $params['option'] ?? ''; 544 545 $family = Registry::familyFactory()->make($xref, $tree); 546 $family = Auth::checkFamilyAccess($family); 547 548 switch ($option) { 549 case self::ADD_RECORD_ONLY: 550 $this->addFamilyToCart($family); 551 break; 552 553 case self::ADD_CHILDREN: 554 $this->addFamilyAndChildrenToCart($family); 555 break; 556 557 case self::ADD_DESCENDANTS: 558 $this->addFamilyAndDescendantsToCart($family); 559 break; 560 } 561 562 return redirect($family->url()); 563 } 564 565 566 /** 567 * @param Family $family 568 * 569 * @return void 570 */ 571 protected function addFamilyAndChildrenToCart(Family $family): void 572 { 573 $this->addFamilyToCart($family); 574 575 foreach ($family->children() as $child) { 576 $this->addIndividualToCart($child); 577 } 578 } 579 580 /** 581 * @param Family $family 582 * 583 * @return void 584 */ 585 protected function addFamilyAndDescendantsToCart(Family $family): void 586 { 587 $this->addFamilyAndChildrenToCart($family); 588 589 foreach ($family->children() as $child) { 590 foreach ($child->spouseFamilies() as $child_family) { 591 $this->addFamilyAndDescendantsToCart($child_family); 592 } 593 } 594 } 595 596 /** 597 * @param ServerRequestInterface $request 598 * 599 * @return ResponseInterface 600 */ 601 public function getAddIndividualAction(ServerRequestInterface $request): ResponseInterface 602 { 603 $tree = $request->getAttribute('tree'); 604 assert($tree instanceof Tree); 605 606 $xref = $request->getQueryParams()['xref'] ?? ''; 607 608 $individual = Registry::individualFactory()->make($xref, $tree); 609 $individual = Auth::checkIndividualAccess($individual); 610 $name = $individual->fullName(); 611 612 if ($individual->sex() === 'F') { 613 $options = [ 614 self::ADD_RECORD_ONLY => $name, 615 self::ADD_PARENT_FAMILIES => I18N::translate('%s, her parents and siblings', $name), 616 self::ADD_SPOUSE_FAMILIES => I18N::translate('%s, her spouses and children', $name), 617 self::ADD_ANCESTORS => I18N::translate('%s and her ancestors', $name), 618 self::ADD_ANCESTOR_FAMILIES => I18N::translate('%s, her ancestors and their families', $name), 619 self::ADD_DESCENDANTS => I18N::translate('%s, her spouses and descendants', $name), 620 ]; 621 } else { 622 $options = [ 623 self::ADD_RECORD_ONLY => $name, 624 self::ADD_PARENT_FAMILIES => I18N::translate('%s, his parents and siblings', $name), 625 self::ADD_SPOUSE_FAMILIES => I18N::translate('%s, his spouses and children', $name), 626 self::ADD_ANCESTORS => I18N::translate('%s and his ancestors', $name), 627 self::ADD_ANCESTOR_FAMILIES => I18N::translate('%s, his ancestors and their families', $name), 628 self::ADD_DESCENDANTS => I18N::translate('%s, his spouses and descendants', $name), 629 ]; 630 } 631 632 $title = I18N::translate('Add %s to the clippings cart', $name); 633 634 return $this->viewResponse('modules/clippings/add-options', [ 635 'options' => $options, 636 'record' => $individual, 637 'title' => $title, 638 'tree' => $tree, 639 ]); 640 } 641 642 /** 643 * @param ServerRequestInterface $request 644 * 645 * @return ResponseInterface 646 */ 647 public function postAddIndividualAction(ServerRequestInterface $request): ResponseInterface 648 { 649 $tree = $request->getAttribute('tree'); 650 assert($tree instanceof Tree); 651 652 $params = (array) $request->getParsedBody(); 653 654 $xref = $params['xref'] ?? ''; 655 $option = $params['option'] ?? ''; 656 657 $individual = Registry::individualFactory()->make($xref, $tree); 658 $individual = Auth::checkIndividualAccess($individual); 659 660 switch ($option) { 661 case self::ADD_RECORD_ONLY: 662 $this->addIndividualToCart($individual); 663 break; 664 665 case self::ADD_PARENT_FAMILIES: 666 foreach ($individual->childFamilies() as $family) { 667 $this->addFamilyAndChildrenToCart($family); 668 } 669 break; 670 671 case self::ADD_SPOUSE_FAMILIES: 672 foreach ($individual->spouseFamilies() as $family) { 673 $this->addFamilyAndChildrenToCart($family); 674 } 675 break; 676 677 case self::ADD_ANCESTORS: 678 $this->addAncestorsToCart($individual); 679 break; 680 681 case self::ADD_ANCESTOR_FAMILIES: 682 $this->addAncestorFamiliesToCart($individual); 683 break; 684 685 case self::ADD_DESCENDANTS: 686 foreach ($individual->spouseFamilies() as $family) { 687 $this->addFamilyAndDescendantsToCart($family); 688 } 689 break; 690 } 691 692 return redirect($individual->url()); 693 } 694 695 /** 696 * @param Individual $individual 697 * 698 * @return void 699 */ 700 protected function addAncestorsToCart(Individual $individual): void 701 { 702 $this->addIndividualToCart($individual); 703 704 foreach ($individual->childFamilies() as $family) { 705 $this->addFamilyToCart($family); 706 707 foreach ($family->spouses() as $parent) { 708 $this->addAncestorsToCart($parent); 709 } 710 } 711 } 712 713 /** 714 * @param Individual $individual 715 * 716 * @return void 717 */ 718 protected function addAncestorFamiliesToCart(Individual $individual): void 719 { 720 foreach ($individual->childFamilies() as $family) { 721 $this->addFamilyAndChildrenToCart($family); 722 723 foreach ($family->spouses() as $parent) { 724 $this->addAncestorFamiliesToCart($parent); 725 } 726 } 727 } 728 729 /** 730 * @param ServerRequestInterface $request 731 * 732 * @return ResponseInterface 733 */ 734 public function getAddLocationAction(ServerRequestInterface $request): ResponseInterface 735 { 736 $tree = $request->getAttribute('tree'); 737 assert($tree instanceof Tree); 738 739 $xref = $request->getQueryParams()['xref'] ?? ''; 740 741 $location = Registry::locationFactory()->make($xref, $tree); 742 $location = Auth::checkLocationAccess($location); 743 $name = $location->fullName(); 744 745 $options = [ 746 self::ADD_RECORD_ONLY => $name, 747 ]; 748 749 $title = I18N::translate('Add %s to the clippings cart', $name); 750 751 return $this->viewResponse('modules/clippings/add-options', [ 752 'options' => $options, 753 'record' => $location, 754 'title' => $title, 755 'tree' => $tree, 756 ]); 757 } 758 759 /** 760 * @param ServerRequestInterface $request 761 * 762 * @return ResponseInterface 763 */ 764 public function postAddLocationAction(ServerRequestInterface $request): ResponseInterface 765 { 766 $tree = $request->getAttribute('tree'); 767 assert($tree instanceof Tree); 768 769 $xref = $request->getQueryParams()['xref'] ?? ''; 770 771 $location = Registry::locationFactory()->make($xref, $tree); 772 $location = Auth::checkLocationAccess($location); 773 774 $this->addLocationToCart($location); 775 776 return redirect($location->url()); 777 } 778 779 /** 780 * @param ServerRequestInterface $request 781 * 782 * @return ResponseInterface 783 */ 784 public function getAddMediaAction(ServerRequestInterface $request): ResponseInterface 785 { 786 $tree = $request->getAttribute('tree'); 787 assert($tree instanceof Tree); 788 789 $xref = $request->getQueryParams()['xref'] ?? ''; 790 791 $media = Registry::mediaFactory()->make($xref, $tree); 792 $media = Auth::checkMediaAccess($media); 793 $name = $media->fullName(); 794 795 $options = [ 796 self::ADD_RECORD_ONLY => $name, 797 ]; 798 799 $title = I18N::translate('Add %s to the clippings cart', $name); 800 801 return $this->viewResponse('modules/clippings/add-options', [ 802 'options' => $options, 803 'record' => $media, 804 'title' => $title, 805 'tree' => $tree, 806 ]); 807 } 808 809 /** 810 * @param ServerRequestInterface $request 811 * 812 * @return ResponseInterface 813 */ 814 public function postAddMediaAction(ServerRequestInterface $request): ResponseInterface 815 { 816 $tree = $request->getAttribute('tree'); 817 assert($tree instanceof Tree); 818 819 $xref = $request->getQueryParams()['xref'] ?? ''; 820 821 $media = Registry::mediaFactory()->make($xref, $tree); 822 $media = Auth::checkMediaAccess($media); 823 824 $this->addMediaToCart($media); 825 826 return redirect($media->url()); 827 } 828 829 /** 830 * @param ServerRequestInterface $request 831 * 832 * @return ResponseInterface 833 */ 834 public function getAddNoteAction(ServerRequestInterface $request): ResponseInterface 835 { 836 $tree = $request->getAttribute('tree'); 837 assert($tree instanceof Tree); 838 839 $xref = $request->getQueryParams()['xref'] ?? ''; 840 841 $note = Registry::noteFactory()->make($xref, $tree); 842 $note = Auth::checkNoteAccess($note); 843 $name = $note->fullName(); 844 845 $options = [ 846 self::ADD_RECORD_ONLY => $name, 847 ]; 848 849 $title = I18N::translate('Add %s to the clippings cart', $name); 850 851 return $this->viewResponse('modules/clippings/add-options', [ 852 'options' => $options, 853 'record' => $note, 854 'title' => $title, 855 'tree' => $tree, 856 ]); 857 } 858 859 /** 860 * @param ServerRequestInterface $request 861 * 862 * @return ResponseInterface 863 */ 864 public function postAddNoteAction(ServerRequestInterface $request): ResponseInterface 865 { 866 $tree = $request->getAttribute('tree'); 867 assert($tree instanceof Tree); 868 869 $xref = $request->getQueryParams()['xref'] ?? ''; 870 871 $note = Registry::noteFactory()->make($xref, $tree); 872 $note = Auth::checkNoteAccess($note); 873 874 $this->addNoteToCart($note); 875 876 return redirect($note->url()); 877 } 878 879 /** 880 * @param ServerRequestInterface $request 881 * 882 * @return ResponseInterface 883 */ 884 public function getAddRepositoryAction(ServerRequestInterface $request): ResponseInterface 885 { 886 $tree = $request->getAttribute('tree'); 887 assert($tree instanceof Tree); 888 889 $xref = $request->getQueryParams()['xref'] ?? ''; 890 891 $repository = Registry::repositoryFactory()->make($xref, $tree); 892 $repository = Auth::checkRepositoryAccess($repository); 893 $name = $repository->fullName(); 894 895 $options = [ 896 self::ADD_RECORD_ONLY => $name, 897 ]; 898 899 $title = I18N::translate('Add %s to the clippings cart', $name); 900 901 return $this->viewResponse('modules/clippings/add-options', [ 902 'options' => $options, 903 'record' => $repository, 904 'title' => $title, 905 'tree' => $tree, 906 ]); 907 } 908 909 /** 910 * @param ServerRequestInterface $request 911 * 912 * @return ResponseInterface 913 */ 914 public function postAddRepositoryAction(ServerRequestInterface $request): ResponseInterface 915 { 916 $tree = $request->getAttribute('tree'); 917 assert($tree instanceof Tree); 918 919 $xref = $request->getQueryParams()['xref'] ?? ''; 920 921 $repository = Registry::repositoryFactory()->make($xref, $tree); 922 $repository = Auth::checkRepositoryAccess($repository); 923 924 $this->addRepositoryToCart($repository); 925 926 foreach ($repository->linkedSources('REPO') as $source) { 927 $this->addSourceToCart($source); 928 } 929 930 return redirect($repository->url()); 931 } 932 933 /** 934 * @param ServerRequestInterface $request 935 * 936 * @return ResponseInterface 937 */ 938 public function getAddSourceAction(ServerRequestInterface $request): ResponseInterface 939 { 940 $tree = $request->getAttribute('tree'); 941 assert($tree instanceof Tree); 942 943 $xref = $request->getQueryParams()['xref'] ?? ''; 944 945 $source = Registry::sourceFactory()->make($xref, $tree); 946 $source = Auth::checkSourceAccess($source); 947 $name = $source->fullName(); 948 949 $options = [ 950 self::ADD_RECORD_ONLY => $name, 951 self::ADD_LINKED_INDIVIDUALS => I18N::translate('%s and the individuals that reference it.', $name), 952 ]; 953 954 $title = I18N::translate('Add %s to the clippings cart', $name); 955 956 return $this->viewResponse('modules/clippings/add-options', [ 957 'options' => $options, 958 'record' => $source, 959 'title' => $title, 960 'tree' => $tree, 961 ]); 962 } 963 964 /** 965 * @param ServerRequestInterface $request 966 * 967 * @return ResponseInterface 968 */ 969 public function postAddSourceAction(ServerRequestInterface $request): ResponseInterface 970 { 971 $tree = $request->getAttribute('tree'); 972 assert($tree instanceof Tree); 973 974 $params = (array) $request->getParsedBody(); 975 976 $xref = $params['xref'] ?? ''; 977 $option = $params['option'] ?? ''; 978 979 $source = Registry::sourceFactory()->make($xref, $tree); 980 $source = Auth::checkSourceAccess($source); 981 982 $this->addSourceToCart($source); 983 984 if ($option === self::ADD_LINKED_INDIVIDUALS) { 985 foreach ($source->linkedIndividuals('SOUR') as $individual) { 986 $this->addIndividualToCart($individual); 987 } 988 foreach ($source->linkedFamilies('SOUR') as $family) { 989 $this->addFamilyToCart($family); 990 } 991 } 992 993 return redirect($source->url()); 994 } 995 996 /** 997 * @param ServerRequestInterface $request 998 * 999 * @return ResponseInterface 1000 */ 1001 public function getAddSubmitterAction(ServerRequestInterface $request): ResponseInterface 1002 { 1003 $tree = $request->getAttribute('tree'); 1004 assert($tree instanceof Tree); 1005 1006 $xref = $request->getQueryParams()['xref'] ?? ''; 1007 1008 $submitter = Registry::submitterFactory()->make($xref, $tree); 1009 $submitter = Auth::checkSubmitterAccess($submitter); 1010 $name = $submitter->fullName(); 1011 1012 $options = [ 1013 self::ADD_RECORD_ONLY => $name, 1014 ]; 1015 1016 $title = I18N::translate('Add %s to the clippings cart', $name); 1017 1018 return $this->viewResponse('modules/clippings/add-options', [ 1019 'options' => $options, 1020 'record' => $submitter, 1021 'title' => $title, 1022 'tree' => $tree, 1023 ]); 1024 } 1025 1026 /** 1027 * @param ServerRequestInterface $request 1028 * 1029 * @return ResponseInterface 1030 */ 1031 public function postAddSubmitterAction(ServerRequestInterface $request): ResponseInterface 1032 { 1033 $tree = $request->getAttribute('tree'); 1034 assert($tree instanceof Tree); 1035 1036 $xref = $request->getQueryParams()['xref'] ?? ''; 1037 1038 $submitter = Registry::submitterFactory()->make($xref, $tree); 1039 $submitter = Auth::checkSubmitterAccess($submitter); 1040 1041 $this->addSubmitterToCart($submitter); 1042 1043 return redirect($submitter->url()); 1044 } 1045 1046 /** 1047 * @param Family $family 1048 */ 1049 protected function addFamilyToCart(Family $family): void 1050 { 1051 $cart = Session::get('cart', []); 1052 $tree = $family->tree()->name(); 1053 $xref = $family->xref(); 1054 1055 if (($cart[$tree][$xref] ?? false) === false) { 1056 $cart[$tree][$xref] = true; 1057 1058 Session::put('cart', $cart); 1059 1060 foreach ($family->spouses() as $spouse) { 1061 $this->addIndividualToCart($spouse); 1062 } 1063 1064 $this->addLocationLinksToCart($family); 1065 $this->addMediaLinksToCart($family); 1066 $this->addNoteLinksToCart($family); 1067 $this->addSourceLinksToCart($family); 1068 $this->addSubmitterLinksToCart($family); 1069 } 1070 } 1071 1072 /** 1073 * @param Individual $individual 1074 */ 1075 protected function addIndividualToCart(Individual $individual): void 1076 { 1077 $cart = Session::get('cart', []); 1078 $tree = $individual->tree()->name(); 1079 $xref = $individual->xref(); 1080 1081 if (($cart[$tree][$xref] ?? false) === false) { 1082 $cart[$tree][$xref] = true; 1083 1084 Session::put('cart', $cart); 1085 1086 $this->addLocationLinksToCart($individual); 1087 $this->addMediaLinksToCart($individual); 1088 $this->addNoteLinksToCart($individual); 1089 $this->addSourceLinksToCart($individual); 1090 } 1091 } 1092 1093 /** 1094 * @param Location $location 1095 */ 1096 protected function addLocationToCart(Location $location): void 1097 { 1098 $cart = Session::get('cart', []); 1099 $tree = $location->tree()->name(); 1100 $xref = $location->xref(); 1101 1102 if (($cart[$tree][$xref] ?? false) === false) { 1103 $cart[$tree][$xref] = true; 1104 1105 Session::put('cart', $cart); 1106 1107 $this->addLocationLinksToCart($location); 1108 $this->addMediaLinksToCart($location); 1109 $this->addNoteLinksToCart($location); 1110 $this->addSourceLinksToCart($location); 1111 } 1112 } 1113 1114 /** 1115 * @param GedcomRecord $record 1116 */ 1117 protected function addLocationLinksToCart(GedcomRecord $record): void 1118 { 1119 preg_match_all('/\n\d _LOC @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches); 1120 1121 foreach ($matches[1] as $xref) { 1122 $location = Registry::locationFactory()->make($xref, $record->tree()); 1123 1124 if ($location instanceof Location && $location->canShow()) { 1125 $this->addLocationToCart($location); 1126 } 1127 } 1128 } 1129 1130 /** 1131 * @param Media $media 1132 */ 1133 protected function addMediaToCart(Media $media): void 1134 { 1135 $cart = Session::get('cart', []); 1136 $tree = $media->tree()->name(); 1137 $xref = $media->xref(); 1138 1139 if (($cart[$tree][$xref] ?? false) === false) { 1140 $cart[$tree][$xref] = true; 1141 1142 Session::put('cart', $cart); 1143 1144 $this->addNoteLinksToCart($media); 1145 } 1146 } 1147 1148 /** 1149 * @param GedcomRecord $record 1150 */ 1151 protected function addMediaLinksToCart(GedcomRecord $record): void 1152 { 1153 preg_match_all('/\n\d OBJE @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches); 1154 1155 foreach ($matches[1] as $xref) { 1156 $media = Registry::mediaFactory()->make($xref, $record->tree()); 1157 1158 if ($media instanceof Media && $media->canShow()) { 1159 $this->addMediaToCart($media); 1160 } 1161 } 1162 } 1163 1164 /** 1165 * @param Note $note 1166 */ 1167 protected function addNoteToCart(Note $note): void 1168 { 1169 $cart = Session::get('cart', []); 1170 $tree = $note->tree()->name(); 1171 $xref = $note->xref(); 1172 1173 if (($cart[$tree][$xref] ?? false) === false) { 1174 $cart[$tree][$xref] = true; 1175 1176 Session::put('cart', $cart); 1177 } 1178 } 1179 1180 /** 1181 * @param GedcomRecord $record 1182 */ 1183 protected function addNoteLinksToCart(GedcomRecord $record): void 1184 { 1185 preg_match_all('/\n\d NOTE @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches); 1186 1187 foreach ($matches[1] as $xref) { 1188 $note = Registry::noteFactory()->make($xref, $record->tree()); 1189 1190 if ($note instanceof Note && $note->canShow()) { 1191 $this->addNoteToCart($note); 1192 } 1193 } 1194 } 1195 1196 /** 1197 * @param Source $source 1198 */ 1199 protected function addSourceToCart(Source $source): void 1200 { 1201 $cart = Session::get('cart', []); 1202 $tree = $source->tree()->name(); 1203 $xref = $source->xref(); 1204 1205 if (($cart[$tree][$xref] ?? false) === false) { 1206 $cart[$tree][$xref] = true; 1207 1208 Session::put('cart', $cart); 1209 1210 $this->addNoteLinksToCart($source); 1211 $this->addRepositoryLinksToCart($source); 1212 } 1213 } 1214 1215 /** 1216 * @param GedcomRecord $record 1217 */ 1218 protected function addSourceLinksToCart(GedcomRecord $record): void 1219 { 1220 preg_match_all('/\n\d SOUR @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches); 1221 1222 foreach ($matches[1] as $xref) { 1223 $source = Registry::sourceFactory()->make($xref, $record->tree()); 1224 1225 if ($source instanceof Source && $source->canShow()) { 1226 $this->addSourceToCart($source); 1227 } 1228 } 1229 } 1230 1231 /** 1232 * @param Repository $repository 1233 */ 1234 protected function addRepositoryToCart(Repository $repository): void 1235 { 1236 $cart = Session::get('cart', []); 1237 $tree = $repository->tree()->name(); 1238 $xref = $repository->xref(); 1239 1240 if (($cart[$tree][$xref] ?? false) === false) { 1241 $cart[$tree][$xref] = true; 1242 1243 Session::put('cart', $cart); 1244 1245 $this->addNoteLinksToCart($repository); 1246 } 1247 } 1248 1249 /** 1250 * @param GedcomRecord $record 1251 */ 1252 protected function addRepositoryLinksToCart(GedcomRecord $record): void 1253 { 1254 preg_match_all('/\n\d REPO @(' . Gedcom::REGEX_XREF . '@)/', $record->gedcom(), $matches); 1255 1256 foreach ($matches[1] as $xref) { 1257 $repository = Registry::repositoryFactory()->make($xref, $record->tree()); 1258 1259 if ($repository instanceof Repository && $repository->canShow()) { 1260 $this->addRepositoryToCart($repository); 1261 } 1262 } 1263 } 1264 1265 /** 1266 * @param Submitter $submitter 1267 */ 1268 protected function addSubmitterToCart(Submitter $submitter): void 1269 { 1270 $cart = Session::get('cart', []); 1271 $tree = $submitter->tree()->name(); 1272 $xref = $submitter->xref(); 1273 1274 if (($cart[$tree][$xref] ?? false) === false) { 1275 $cart[$tree][$xref] = true; 1276 1277 Session::put('cart', $cart); 1278 1279 $this->addNoteLinksToCart($submitter); 1280 } 1281 } 1282 1283 /** 1284 * @param GedcomRecord $record 1285 */ 1286 protected function addSubmitterLinksToCart(GedcomRecord $record): void 1287 { 1288 preg_match_all('/\n\d SUBM @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches); 1289 1290 foreach ($matches[1] as $xref) { 1291 $submitter = Registry::submitterFactory()->make($xref, $record->tree()); 1292 1293 if ($submitter instanceof Submitter && $submitter->canShow()) { 1294 $this->addSubmitterToCart($submitter); 1295 } 1296 } 1297 } 1298} 1299