18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98c2e8227SGreg Roach * (at your option) any later version. 108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138c2e8227SGreg Roach * GNU General Public License for more details. 148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2176692c8bSGreg Roach 22de2aa325SGreg Roachuse Aura\Router\Route; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 240bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\FamilyNotFoundException; 250bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException; 260bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\MediaNotFoundException; 270bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\NoteNotFoundException; 280bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\RepositoryNotFoundException; 290bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\SourceNotFoundException; 300e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family; 315a78cd34SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport; 325a78cd34SGreg Roachuse Fisharebest\Webtrees\Gedcom; 330e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 34f95e0480SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\FamilyPage; 35f95e0480SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\IndividualPage; 36f95e0480SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\MediaPage; 37f95e0480SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\NotePage; 38f95e0480SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\RepositoryPage; 39f95e0480SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\SourcePage; 400e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 410e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 425a78cd34SGreg Roachuse Fisharebest\Webtrees\Media; 430e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu; 445a78cd34SGreg Roachuse Fisharebest\Webtrees\Note; 455a78cd34SGreg Roachuse Fisharebest\Webtrees\Repository; 46e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 470e62c4b8SGreg Roachuse Fisharebest\Webtrees\Session; 485a78cd34SGreg Roachuse Fisharebest\Webtrees\Source; 49aee13b6dSGreg Roachuse Fisharebest\Webtrees\Tree; 505a78cd34SGreg Roachuse League\Flysystem\Filesystem; 51a04bb9a2SGreg Roachuse League\Flysystem\FilesystemInterface; 5261bf91b2SGreg Roachuse League\Flysystem\MountManager; 535a78cd34SGreg Roachuse League\Flysystem\ZipArchive\ZipArchiveAdapter; 54bed27cedSGreg Roachuse Psr\Http\Message\ResponseFactoryInterface; 556ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 566ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 576ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface; 583976b470SGreg Roach 59eb235819SGreg Roachuse function app; 60bf80ec58SGreg Roachuse function array_filter; 61bf80ec58SGreg Roachuse function array_keys; 62bf80ec58SGreg Roachuse function array_map; 635229eadeSGreg Roachuse function assert; 64bf80ec58SGreg Roachuse function in_array; 65ddeb3354SGreg Roachuse function is_string; 66bf80ec58SGreg Roachuse function key; 67bf80ec58SGreg Roachuse function preg_match_all; 68bf80ec58SGreg Roachuse function redirect; 69bf80ec58SGreg Roachuse function route; 70e5a6b4d4SGreg Roachuse function str_replace; 71bf80ec58SGreg Roachuse function strip_tags; 72bf80ec58SGreg Roachuse function sys_get_temp_dir; 73bf80ec58SGreg Roachuse function tempnam; 74bf80ec58SGreg Roachuse function utf8_decode; 758c2e8227SGreg Roach 768c2e8227SGreg Roach/** 778c2e8227SGreg Roach * Class ClippingsCartModule 788c2e8227SGreg Roach */ 7937eb8894SGreg Roachclass ClippingsCartModule extends AbstractModule implements ModuleMenuInterface 80c1010edaSGreg Roach{ 8149a243cbSGreg Roach use ModuleMenuTrait; 8249a243cbSGreg Roach 835a78cd34SGreg Roach // Routes that have a record which can be added to the clipboard 8416d6367aSGreg Roach private const ROUTES_WITH_RECORDS = [ 85f95e0480SGreg Roach 'Family' => FamilyPage::class, 86f95e0480SGreg Roach 'Individual' => IndividualPage::class, 87f95e0480SGreg Roach 'Media' => MediaPage::class, 88f95e0480SGreg Roach 'Note' => NotePage::class, 89f95e0480SGreg Roach 'Repository' => RepositoryPage::class, 90f95e0480SGreg Roach 'Source' => SourcePage::class, 91c1010edaSGreg Roach ]; 925a78cd34SGreg Roach 9349a243cbSGreg Roach /** @var int The default access level for this module. It can be changed in the control panel. */ 9449a243cbSGreg Roach protected $access_level = Auth::PRIV_USER; 9549a243cbSGreg Roach 96961ec755SGreg Roach /** 97e5a6b4d4SGreg Roach * @var UserService 98e5a6b4d4SGreg Roach */ 99e5a6b4d4SGreg Roach private $user_service; 100e5a6b4d4SGreg Roach 101e5a6b4d4SGreg Roach /** 102e5a6b4d4SGreg Roach * ClippingsCartModule constructor. 103e5a6b4d4SGreg Roach * 104e5a6b4d4SGreg Roach * @param UserService $user_service 105e5a6b4d4SGreg Roach */ 106e5a6b4d4SGreg Roach public function __construct(UserService $user_service) 107e5a6b4d4SGreg Roach { 108e5a6b4d4SGreg Roach $this->user_service = $user_service; 109e5a6b4d4SGreg Roach } 110e5a6b4d4SGreg Roach 111e5a6b4d4SGreg Roach /** 1120cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 113961ec755SGreg Roach * 114961ec755SGreg Roach * @return string 115961ec755SGreg Roach */ 11649a243cbSGreg Roach public function title(): string 117c1010edaSGreg Roach { 118bbb76c12SGreg Roach /* I18N: Name of a module */ 119bbb76c12SGreg Roach return I18N::translate('Clippings cart'); 1208c2e8227SGreg Roach } 1218c2e8227SGreg Roach 122961ec755SGreg Roach /** 123961ec755SGreg Roach * A sentence describing what this module does. 124961ec755SGreg Roach * 125961ec755SGreg Roach * @return string 126961ec755SGreg Roach */ 12749a243cbSGreg Roach public function description(): string 128c1010edaSGreg Roach { 129bbb76c12SGreg Roach /* I18N: Description of the “Clippings cart” module */ 130bbb76c12SGreg Roach return I18N::translate('Select records from your family tree and save them as a GEDCOM file.'); 1318c2e8227SGreg Roach } 1328c2e8227SGreg Roach 1330ee13198SGreg Roach /** 13449a243cbSGreg Roach * The default position for this menu. It can be changed in the control panel. 1350ee13198SGreg Roach * 1360ee13198SGreg Roach * @return int 1370ee13198SGreg Roach */ 1388f53f488SRico Sonntag public function defaultMenuOrder(): int 139c1010edaSGreg Roach { 140353b36abSGreg Roach return 6; 1418c2e8227SGreg Roach } 1428c2e8227SGreg Roach 1430ee13198SGreg Roach /** 1440ee13198SGreg Roach * A menu, to be added to the main application menu. 1450ee13198SGreg Roach * 146aee13b6dSGreg Roach * @param Tree $tree 147aee13b6dSGreg Roach * 1480ee13198SGreg Roach * @return Menu|null 1490ee13198SGreg Roach */ 15046295629SGreg Roach public function getMenu(Tree $tree): ?Menu 151c1010edaSGreg Roach { 152eb235819SGreg Roach /** @var ServerRequestInterface $request */ 1536ccdf4f0SGreg Roach $request = app(ServerRequestInterface::class); 1548c2e8227SGreg Roach 155f7ab47b1SGreg Roach $route = $request->getAttribute('route'); 156de2aa325SGreg Roach assert($route instanceof Route); 1575a78cd34SGreg Roach 1585a78cd34SGreg Roach $submenus = [ 15949a243cbSGreg Roach new Menu($this->title(), route('module', [ 16026684e68SGreg Roach 'module' => $this->name(), 161c1010edaSGreg Roach 'action' => 'Show', 162d72b284aSGreg Roach 'tree' => $tree->name(), 163c1010edaSGreg Roach ]), 'menu-clippings-cart', ['rel' => 'nofollow']), 1645a78cd34SGreg Roach ]; 1655a78cd34SGreg Roach 1662b0d92b4SGreg Roach $action = array_search($route->name, self::ROUTES_WITH_RECORDS, true); 167f95e0480SGreg Roach if ($action !== false) { 1682b0d92b4SGreg Roach $xref = $route->attributes['xref']; 169ddeb3354SGreg Roach assert(is_string($xref)); 170ddeb3354SGreg Roach 171c1010edaSGreg Roach $add_route = route('module', [ 17226684e68SGreg Roach 'module' => $this->name(), 173f95e0480SGreg Roach 'action' => 'Add' . $action, 174c1010edaSGreg Roach 'xref' => $xref, 175d72b284aSGreg Roach 'tree' => $tree->name(), 176c1010edaSGreg Roach ]); 1775a78cd34SGreg Roach 17825b2dde3SGreg Roach $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']); 1798c2e8227SGreg Roach } 180cbc1590aSGreg Roach 1815a78cd34SGreg Roach if (!$this->isCartEmpty($tree)) { 182c1010edaSGreg Roach $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [ 18326684e68SGreg Roach 'module' => $this->name(), 184c1010edaSGreg Roach 'action' => 'Empty', 185d72b284aSGreg Roach 'tree' => $tree->name(), 186c1010edaSGreg Roach ]), 'menu-clippings-empty', ['rel' => 'nofollow']); 187f95e0480SGreg Roach 188c1010edaSGreg Roach $submenus[] = new Menu(I18N::translate('Download'), route('module', [ 18926684e68SGreg Roach 'module' => $this->name(), 190c1010edaSGreg Roach 'action' => 'DownloadForm', 191d72b284aSGreg Roach 'tree' => $tree->name(), 192c1010edaSGreg Roach ]), 'menu-clippings-download', ['rel' => 'nofollow']); 1935a78cd34SGreg Roach } 1945a78cd34SGreg Roach 19549a243cbSGreg Roach return new Menu($this->title(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus); 1968c2e8227SGreg Roach } 1978c2e8227SGreg Roach 19876692c8bSGreg Roach /** 1996ccdf4f0SGreg Roach * @param ServerRequestInterface $request 20076692c8bSGreg Roach * 2016ccdf4f0SGreg Roach * @return ResponseInterface 20276692c8bSGreg Roach */ 203f95e0480SGreg Roach public function postDownloadAction(ServerRequestInterface $request): ResponseInterface 204c1010edaSGreg Roach { 20557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 2064ea62551SGreg Roach assert($tree instanceof Tree); 2074ea62551SGreg Roach 208a04bb9a2SGreg Roach $data_filesystem = $request->getAttribute('filesystem.data'); 209a04bb9a2SGreg Roach assert($data_filesystem instanceof FilesystemInterface); 210a04bb9a2SGreg Roach 211b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 212b46c87bdSGreg Roach 213b46c87bdSGreg Roach $privatize_export = $params['privatize_export']; 214b46c87bdSGreg Roach $convert = (bool) ($params['convert'] ?? false); 2158c2e8227SGreg Roach 21613abd6f3SGreg Roach $cart = Session::get('cart', []); 2178c2e8227SGreg Roach 218aa6f03bbSGreg Roach $xrefs = array_keys($cart[$tree->name()] ?? []); 2195a78cd34SGreg Roach 2205a78cd34SGreg Roach // Create a new/empty .ZIP file 221a00baf47SGreg Roach $temp_zip_file = stream_get_meta_data(tmpfile())['uri']; 2227f996f6eSGreg Roach $zip_adapter = new ZipArchiveAdapter($temp_zip_file); 2237f996f6eSGreg Roach $zip_filesystem = new Filesystem($zip_adapter); 2245a78cd34SGreg Roach 22561bf91b2SGreg Roach $manager = new MountManager([ 226a04bb9a2SGreg Roach 'media' => $tree->mediaFilesystem($data_filesystem), 22761bf91b2SGreg Roach 'zip' => $zip_filesystem, 22861bf91b2SGreg Roach ]); 22961bf91b2SGreg Roach 2305a78cd34SGreg Roach // Media file prefix 2315a78cd34SGreg Roach $path = $tree->getPreference('MEDIA_DIRECTORY'); 2325a78cd34SGreg Roach 2335a78cd34SGreg Roach // GEDCOM file header 234a3d8780cSGreg Roach $filetext = FunctionsExport::gedcomHeader($tree, $convert ? 'ANSI' : 'UTF-8'); 2355a78cd34SGreg Roach 2365a78cd34SGreg Roach switch ($privatize_export) { 2375a78cd34SGreg Roach case 'gedadmin': 2385a78cd34SGreg Roach $access_level = Auth::PRIV_NONE; 2395a78cd34SGreg Roach break; 2405a78cd34SGreg Roach case 'user': 2415a78cd34SGreg Roach $access_level = Auth::PRIV_USER; 2425a78cd34SGreg Roach break; 2435a78cd34SGreg Roach case 'visitor': 2445a78cd34SGreg Roach $access_level = Auth::PRIV_PRIVATE; 2455a78cd34SGreg Roach break; 2465a78cd34SGreg Roach case 'none': 2475a78cd34SGreg Roach default: 2485a78cd34SGreg Roach $access_level = Auth::PRIV_HIDE; 2495a78cd34SGreg Roach break; 2505a78cd34SGreg Roach } 2515a78cd34SGreg Roach 2525a78cd34SGreg Roach foreach ($xrefs as $xref) { 2535a78cd34SGreg Roach $object = GedcomRecord::getInstance($xref, $tree); 2545a78cd34SGreg Roach // The object may have been deleted since we added it to the cart.... 255bed27cedSGreg Roach if ($object instanceof GedcomRecord) { 2565a78cd34SGreg Roach $record = $object->privatizeGedcom($access_level); 2575a78cd34SGreg Roach // Remove links to objects that aren't in the cart 2588d0ebef0SGreg Roach preg_match_all('/\n1 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER); 2595a78cd34SGreg Roach foreach ($matches as $match) { 260bf80ec58SGreg Roach if (!in_array($match[1], $xrefs, true)) { 2615a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2625a78cd34SGreg Roach } 2635a78cd34SGreg Roach } 2648d0ebef0SGreg Roach preg_match_all('/\n2 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER); 2655a78cd34SGreg Roach foreach ($matches as $match) { 266bf80ec58SGreg Roach if (!in_array($match[1], $xrefs, true)) { 2675a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2685a78cd34SGreg Roach } 2695a78cd34SGreg Roach } 2708d0ebef0SGreg Roach preg_match_all('/\n3 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER); 2715a78cd34SGreg Roach foreach ($matches as $match) { 272bf80ec58SGreg Roach if (!in_array($match[1], $xrefs, true)) { 2735a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2745a78cd34SGreg Roach } 2755a78cd34SGreg Roach } 2765a78cd34SGreg Roach 27755167344SGreg Roach if ($object instanceof Individual || $object instanceof Family) { 2785a78cd34SGreg Roach $filetext .= $record . "\n"; 2795a78cd34SGreg Roach $filetext .= "1 SOUR @WEBTREES@\n"; 2801f273236SGreg Roach $filetext .= '2 PAGE ' . $object->url() . "\n"; 28155167344SGreg Roach } elseif ($object instanceof Source) { 2825a78cd34SGreg Roach $filetext .= $record . "\n"; 2831f273236SGreg Roach $filetext .= '1 NOTE ' . $object->url() . "\n"; 28455167344SGreg Roach } elseif ($object instanceof Media) { 28555167344SGreg Roach // Add the media files to the archive 2865a78cd34SGreg Roach foreach ($object->mediaFiles() as $media_file) { 28761bf91b2SGreg Roach $from = 'media://' . $media_file->filename(); 28861bf91b2SGreg Roach $to = 'zip://' . $path . $media_file->filename(); 289*5de4ab51SGreg Roach if (!$media_file->isExternal() && $manager->has($from) && !$manager->has($to)) { 29061bf91b2SGreg Roach $manager->copy($from, $to); 2915a78cd34SGreg Roach } 2925a78cd34SGreg Roach } 2935a78cd34SGreg Roach $filetext .= $record . "\n"; 29455167344SGreg Roach } else { 2955a78cd34SGreg Roach $filetext .= $record . "\n"; 2968c2e8227SGreg Roach } 2978c2e8227SGreg Roach } 2988c2e8227SGreg Roach } 2998c2e8227SGreg Roach 3009b93b7c3SGreg Roach $base_url = $request->getAttribute('base_url'); 3019b93b7c3SGreg Roach 3025a78cd34SGreg Roach // Create a source, to indicate the source of the data. 3039b93b7c3SGreg Roach $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . $base_url . "\n"; 304e5a6b4d4SGreg Roach $author = $this->user_service->find((int) $tree->getPreference('CONTACT_USER_ID')); 3055a78cd34SGreg Roach if ($author !== null) { 306e5a6b4d4SGreg Roach $filetext .= '1 AUTH ' . $author->realName() . "\n"; 3075a78cd34SGreg Roach } 3085a78cd34SGreg Roach $filetext .= "0 TRLR\n"; 3095a78cd34SGreg Roach 3105a78cd34SGreg Roach // Make sure the preferred line endings are used 311679203a4SGreg Roach $filetext = strtr($filetext, ["\n" => Gedcom::EOL]); 3125a78cd34SGreg Roach 31355167344SGreg Roach if ($convert) { 3145a78cd34SGreg Roach $filetext = utf8_decode($filetext); 3158c2e8227SGreg Roach } 316cbc1590aSGreg Roach 3175a78cd34SGreg Roach // Finally add the GEDCOM file to the .ZIP file. 3185a78cd34SGreg Roach $zip_filesystem->write('clippings.ged', $filetext); 3195a78cd34SGreg Roach 32061bf91b2SGreg Roach // Need to force-close ZipArchive filesystems. 3217f996f6eSGreg Roach $zip_adapter->getArchive()->close(); 3225a78cd34SGreg Roach 3236ccdf4f0SGreg Roach // Use a stream, so that we do not have to load the entire file into memory. 3246ccdf4f0SGreg Roach $stream = app(StreamFactoryInterface::class)->createStreamFromFile($temp_zip_file); 3255a78cd34SGreg Roach 326bed27cedSGreg Roach /** @var ResponseFactoryInterface $response_factory */ 327bed27cedSGreg Roach $response_factory = app(ResponseFactoryInterface::class); 328bed27cedSGreg Roach 329bed27cedSGreg Roach return $response_factory->createResponse() 3306ccdf4f0SGreg Roach ->withBody($stream) 3311b3d4731SGreg Roach ->withHeader('Content-Type', 'application/zip') 332bed27cedSGreg Roach ->withHeader('Content-Disposition', 'attachment; filename="clippings.zip'); 3338c2e8227SGreg Roach } 3348c2e8227SGreg Roach 3358c2e8227SGreg Roach /** 33657ab2231SGreg Roach * @param ServerRequestInterface $request 33776692c8bSGreg Roach * 3386ccdf4f0SGreg Roach * @return ResponseInterface 3398c2e8227SGreg Roach */ 34057ab2231SGreg Roach public function getDownloadFormAction(ServerRequestInterface $request): ResponseInterface 341c1010edaSGreg Roach { 34257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 3434ea62551SGreg Roach assert($tree instanceof Tree); 3444ea62551SGreg Roach 34557ab2231SGreg Roach $user = $request->getAttribute('user'); 3465a78cd34SGreg Roach $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download'); 3478c2e8227SGreg Roach 3485a78cd34SGreg Roach return $this->viewResponse('modules/clippings/download', [ 3495a78cd34SGreg Roach 'is_manager' => Auth::isManager($tree, $user), 3505a78cd34SGreg Roach 'is_member' => Auth::isMember($tree, $user), 35171378461SGreg Roach 'module' => $this->name(), 3525a78cd34SGreg Roach 'title' => $title, 353f95e0480SGreg Roach 'tree' => $tree, 3545a78cd34SGreg Roach ]); 3558c2e8227SGreg Roach } 3568c2e8227SGreg Roach 3575a78cd34SGreg Roach /** 35857ab2231SGreg Roach * @param ServerRequestInterface $request 3595a78cd34SGreg Roach * 3606ccdf4f0SGreg Roach * @return ResponseInterface 3615a78cd34SGreg Roach */ 36257ab2231SGreg Roach public function getEmptyAction(ServerRequestInterface $request): ResponseInterface 363c1010edaSGreg Roach { 36457ab2231SGreg Roach $tree = $request->getAttribute('tree'); 3654ea62551SGreg Roach assert($tree instanceof Tree); 3664ea62551SGreg Roach 3675a78cd34SGreg Roach $cart = Session::get('cart', []); 368aa6f03bbSGreg Roach $cart[$tree->name()] = []; 3695a78cd34SGreg Roach Session::put('cart', $cart); 3708c2e8227SGreg Roach 371c1010edaSGreg Roach $url = route('module', [ 37226684e68SGreg Roach 'module' => $this->name(), 373c1010edaSGreg Roach 'action' => 'Show', 374d72b284aSGreg Roach 'tree' => $tree->name(), 375c1010edaSGreg Roach ]); 3765a78cd34SGreg Roach 3776ccdf4f0SGreg Roach return redirect($url); 3785a78cd34SGreg Roach } 3795a78cd34SGreg Roach 3805a78cd34SGreg Roach /** 3816ccdf4f0SGreg Roach * @param ServerRequestInterface $request 3825a78cd34SGreg Roach * 3836ccdf4f0SGreg Roach * @return ResponseInterface 3845a78cd34SGreg Roach */ 38557ab2231SGreg Roach public function postRemoveAction(ServerRequestInterface $request): ResponseInterface 386c1010edaSGreg Roach { 38757ab2231SGreg Roach $tree = $request->getAttribute('tree'); 38875964c75SGreg Roach assert($tree instanceof Tree); 3895229eadeSGreg Roach 390bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 3915a78cd34SGreg Roach 3925a78cd34SGreg Roach $cart = Session::get('cart', []); 393aa6f03bbSGreg Roach unset($cart[$tree->name()][$xref]); 3945a78cd34SGreg Roach Session::put('cart', $cart); 3955a78cd34SGreg Roach 396c1010edaSGreg Roach $url = route('module', [ 39726684e68SGreg Roach 'module' => $this->name(), 398c1010edaSGreg Roach 'action' => 'Show', 399d72b284aSGreg Roach 'tree' => $tree->name(), 400c1010edaSGreg Roach ]); 4015a78cd34SGreg Roach 4026ccdf4f0SGreg Roach return redirect($url); 4035a78cd34SGreg Roach } 4045a78cd34SGreg Roach 4055a78cd34SGreg Roach /** 40657ab2231SGreg Roach * @param ServerRequestInterface $request 4075a78cd34SGreg Roach * 4086ccdf4f0SGreg Roach * @return ResponseInterface 4095a78cd34SGreg Roach */ 41057ab2231SGreg Roach public function getShowAction(ServerRequestInterface $request): ResponseInterface 411c1010edaSGreg Roach { 41257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 41375964c75SGreg Roach assert($tree instanceof Tree); 41457ab2231SGreg Roach 4155a78cd34SGreg Roach return $this->viewResponse('modules/clippings/show', [ 4165a78cd34SGreg Roach 'records' => $this->allRecordsInCart($tree), 4175a78cd34SGreg Roach 'title' => I18N::translate('Family tree clippings cart'), 4185a78cd34SGreg Roach 'tree' => $tree, 4195a78cd34SGreg Roach ]); 4205a78cd34SGreg Roach } 4215a78cd34SGreg Roach 4225a78cd34SGreg Roach /** 4236ccdf4f0SGreg Roach * @param ServerRequestInterface $request 4245a78cd34SGreg Roach * 4256ccdf4f0SGreg Roach * @return ResponseInterface 4265a78cd34SGreg Roach */ 42757ab2231SGreg Roach public function getAddFamilyAction(ServerRequestInterface $request): ResponseInterface 428c1010edaSGreg Roach { 42957ab2231SGreg Roach $tree = $request->getAttribute('tree'); 43075964c75SGreg Roach assert($tree instanceof Tree); 4315229eadeSGreg Roach 432bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 4335a78cd34SGreg Roach 4345a78cd34SGreg Roach $family = Family::getInstance($xref, $tree); 4355a78cd34SGreg Roach 4365a78cd34SGreg Roach if ($family === null) { 43759f2f229SGreg Roach throw new FamilyNotFoundException(); 4385a78cd34SGreg Roach } 4395a78cd34SGreg Roach 4405a78cd34SGreg Roach $options = $this->familyOptions($family); 4415a78cd34SGreg Roach 44239ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $family->fullName()); 4435a78cd34SGreg Roach 4445a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 4455a78cd34SGreg Roach 'options' => $options, 4465a78cd34SGreg Roach 'default' => key($options), 4475a78cd34SGreg Roach 'record' => $family, 4485a78cd34SGreg Roach 'title' => $title, 4495a78cd34SGreg Roach 'tree' => $tree, 4505a78cd34SGreg Roach ]); 4515a78cd34SGreg Roach } 4525a78cd34SGreg Roach 4535a78cd34SGreg Roach /** 4545a78cd34SGreg Roach * @param Family $family 4555a78cd34SGreg Roach * 4565a78cd34SGreg Roach * @return string[] 4575a78cd34SGreg Roach */ 458c1010edaSGreg Roach private function familyOptions(Family $family): array 459c1010edaSGreg Roach { 46039ca88baSGreg Roach $name = strip_tags($family->fullName()); 4615a78cd34SGreg Roach 4625a78cd34SGreg Roach return [ 4635a78cd34SGreg Roach 'parents' => $name, 464bbb76c12SGreg Roach /* I18N: %s is a family (husband + wife) */ 465bbb76c12SGreg Roach 'members' => I18N::translate('%s and their children', $name), 466bbb76c12SGreg Roach /* I18N: %s is a family (husband + wife) */ 467bbb76c12SGreg Roach 'descendants' => I18N::translate('%s and their descendants', $name), 4685a78cd34SGreg Roach ]; 4695a78cd34SGreg Roach } 4705a78cd34SGreg Roach 4715a78cd34SGreg Roach /** 4726ccdf4f0SGreg Roach * @param ServerRequestInterface $request 4735a78cd34SGreg Roach * 4746ccdf4f0SGreg Roach * @return ResponseInterface 4755a78cd34SGreg Roach */ 47657ab2231SGreg Roach public function postAddFamilyAction(ServerRequestInterface $request): ResponseInterface 477c1010edaSGreg Roach { 47857ab2231SGreg Roach $tree = $request->getAttribute('tree'); 4794ea62551SGreg Roach assert($tree instanceof Tree); 4804ea62551SGreg Roach 481b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 482b46c87bdSGreg Roach 483b46c87bdSGreg Roach $xref = $params['xref']; 484b46c87bdSGreg Roach $option = $params['option']; 4855a78cd34SGreg Roach 4865a78cd34SGreg Roach $family = Family::getInstance($xref, $tree); 4875a78cd34SGreg Roach 4885a78cd34SGreg Roach if ($family === null) { 48959f2f229SGreg Roach throw new FamilyNotFoundException(); 4905a78cd34SGreg Roach } 4915a78cd34SGreg Roach 4925a78cd34SGreg Roach switch ($option) { 4935a78cd34SGreg Roach case 'parents': 4945a78cd34SGreg Roach $this->addFamilyToCart($family); 4955a78cd34SGreg Roach break; 4965a78cd34SGreg Roach 4975a78cd34SGreg Roach case 'members': 4985a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 4995a78cd34SGreg Roach break; 5005a78cd34SGreg Roach 5015a78cd34SGreg Roach case 'descendants': 5025a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($family); 5035a78cd34SGreg Roach break; 5045a78cd34SGreg Roach } 5055a78cd34SGreg Roach 5066ccdf4f0SGreg Roach return redirect($family->url()); 5075a78cd34SGreg Roach } 5085a78cd34SGreg Roach 5095a78cd34SGreg Roach /** 5105a78cd34SGreg Roach * @param Family $family 51118d7a90dSGreg Roach * 51218d7a90dSGreg Roach * @return void 5135a78cd34SGreg Roach */ 514e364afe4SGreg Roach private function addFamilyToCart(Family $family): void 515c1010edaSGreg Roach { 5165a78cd34SGreg Roach $this->addRecordToCart($family); 5175a78cd34SGreg Roach 51839ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 5195a78cd34SGreg Roach $this->addRecordToCart($spouse); 5205a78cd34SGreg Roach } 5215a78cd34SGreg Roach } 5225a78cd34SGreg Roach 5235a78cd34SGreg Roach /** 5245a78cd34SGreg Roach * @param Family $family 52518d7a90dSGreg Roach * 52618d7a90dSGreg Roach * @return void 5275a78cd34SGreg Roach */ 528e364afe4SGreg Roach private function addFamilyAndChildrenToCart(Family $family): void 529c1010edaSGreg Roach { 5305a78cd34SGreg Roach $this->addRecordToCart($family); 5315a78cd34SGreg Roach 53239ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 5335a78cd34SGreg Roach $this->addRecordToCart($spouse); 5345a78cd34SGreg Roach } 53539ca88baSGreg Roach foreach ($family->children() as $child) { 5365a78cd34SGreg Roach $this->addRecordToCart($child); 5375a78cd34SGreg Roach } 5385a78cd34SGreg Roach } 5395a78cd34SGreg Roach 5405a78cd34SGreg Roach /** 5415a78cd34SGreg Roach * @param Family $family 54218d7a90dSGreg Roach * 54318d7a90dSGreg Roach * @return void 5445a78cd34SGreg Roach */ 545e364afe4SGreg Roach private function addFamilyAndDescendantsToCart(Family $family): void 546c1010edaSGreg Roach { 5475a78cd34SGreg Roach $this->addRecordToCart($family); 5485a78cd34SGreg Roach 54939ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 5505a78cd34SGreg Roach $this->addRecordToCart($spouse); 5515a78cd34SGreg Roach } 55239ca88baSGreg Roach foreach ($family->children() as $child) { 5535a78cd34SGreg Roach $this->addRecordToCart($child); 55439ca88baSGreg Roach foreach ($child->spouseFamilies() as $child_family) { 5555a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($child_family); 5565a78cd34SGreg Roach } 5575a78cd34SGreg Roach } 5585a78cd34SGreg Roach } 5595a78cd34SGreg Roach 5605a78cd34SGreg Roach /** 5616ccdf4f0SGreg Roach * @param ServerRequestInterface $request 5625a78cd34SGreg Roach * 5636ccdf4f0SGreg Roach * @return ResponseInterface 5645a78cd34SGreg Roach */ 56557ab2231SGreg Roach public function getAddIndividualAction(ServerRequestInterface $request): ResponseInterface 566c1010edaSGreg Roach { 56757ab2231SGreg Roach $tree = $request->getAttribute('tree'); 56875964c75SGreg Roach assert($tree instanceof Tree); 5695229eadeSGreg Roach 570bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 5715a78cd34SGreg Roach 5725a78cd34SGreg Roach $individual = Individual::getInstance($xref, $tree); 5735a78cd34SGreg Roach 5745a78cd34SGreg Roach if ($individual === null) { 57559f2f229SGreg Roach throw new IndividualNotFoundException(); 5765a78cd34SGreg Roach } 5775a78cd34SGreg Roach 5785a78cd34SGreg Roach $options = $this->individualOptions($individual); 5795a78cd34SGreg Roach 58039ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $individual->fullName()); 5815a78cd34SGreg Roach 5825a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 5835a78cd34SGreg Roach 'options' => $options, 5845a78cd34SGreg Roach 'default' => key($options), 5855a78cd34SGreg Roach 'record' => $individual, 5865a78cd34SGreg Roach 'title' => $title, 5875a78cd34SGreg Roach 'tree' => $tree, 5885a78cd34SGreg Roach ]); 5895a78cd34SGreg Roach } 5905a78cd34SGreg Roach 5915a78cd34SGreg Roach /** 5925a78cd34SGreg Roach * @param Individual $individual 5935a78cd34SGreg Roach * 5945a78cd34SGreg Roach * @return string[] 5955a78cd34SGreg Roach */ 596c1010edaSGreg Roach private function individualOptions(Individual $individual): array 597c1010edaSGreg Roach { 59839ca88baSGreg Roach $name = strip_tags($individual->fullName()); 5995a78cd34SGreg Roach 60039ca88baSGreg Roach if ($individual->sex() === 'F') { 6015a78cd34SGreg Roach return [ 6025a78cd34SGreg Roach 'self' => $name, 6035a78cd34SGreg Roach 'parents' => I18N::translate('%s, her parents and siblings', $name), 6045a78cd34SGreg Roach 'spouses' => I18N::translate('%s, her spouses and children', $name), 6055a78cd34SGreg Roach 'ancestors' => I18N::translate('%s and her ancestors', $name), 6065a78cd34SGreg Roach 'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name), 6075a78cd34SGreg Roach 'descendants' => I18N::translate('%s, her spouses and descendants', $name), 6085a78cd34SGreg Roach ]; 609b2ce94c6SRico Sonntag } 610b2ce94c6SRico Sonntag 6115a78cd34SGreg Roach return [ 6125a78cd34SGreg Roach 'self' => $name, 6135a78cd34SGreg Roach 'parents' => I18N::translate('%s, his parents and siblings', $name), 6145a78cd34SGreg Roach 'spouses' => I18N::translate('%s, his spouses and children', $name), 6155a78cd34SGreg Roach 'ancestors' => I18N::translate('%s and his ancestors', $name), 6165a78cd34SGreg Roach 'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name), 6175a78cd34SGreg Roach 'descendants' => I18N::translate('%s, his spouses and descendants', $name), 6185a78cd34SGreg Roach ]; 6195a78cd34SGreg Roach } 6205a78cd34SGreg Roach 6215a78cd34SGreg Roach /** 6226ccdf4f0SGreg Roach * @param ServerRequestInterface $request 6235a78cd34SGreg Roach * 6246ccdf4f0SGreg Roach * @return ResponseInterface 6255a78cd34SGreg Roach */ 62657ab2231SGreg Roach public function postAddIndividualAction(ServerRequestInterface $request): ResponseInterface 627c1010edaSGreg Roach { 62857ab2231SGreg Roach $tree = $request->getAttribute('tree'); 6294ea62551SGreg Roach assert($tree instanceof Tree); 6304ea62551SGreg Roach 631b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 632b46c87bdSGreg Roach 633b46c87bdSGreg Roach $xref = $params['xref']; 634b46c87bdSGreg Roach $option = $params['option']; 6355a78cd34SGreg Roach 6365a78cd34SGreg Roach $individual = Individual::getInstance($xref, $tree); 6375a78cd34SGreg Roach 6385a78cd34SGreg Roach if ($individual === null) { 63959f2f229SGreg Roach throw new IndividualNotFoundException(); 6405a78cd34SGreg Roach } 6415a78cd34SGreg Roach 6425a78cd34SGreg Roach switch ($option) { 6435a78cd34SGreg Roach case 'self': 6445a78cd34SGreg Roach $this->addRecordToCart($individual); 6455a78cd34SGreg Roach break; 6465a78cd34SGreg Roach 6475a78cd34SGreg Roach case 'parents': 64839ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 6495a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 6505a78cd34SGreg Roach } 6515a78cd34SGreg Roach break; 6525a78cd34SGreg Roach 6535a78cd34SGreg Roach case 'spouses': 65439ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 6555a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 6565a78cd34SGreg Roach } 6575a78cd34SGreg Roach break; 6585a78cd34SGreg Roach 6595a78cd34SGreg Roach case 'ancestors': 6605a78cd34SGreg Roach $this->addAncestorsToCart($individual); 6615a78cd34SGreg Roach break; 6625a78cd34SGreg Roach 6635a78cd34SGreg Roach case 'ancestor_families': 6645a78cd34SGreg Roach $this->addAncestorFamiliesToCart($individual); 6655a78cd34SGreg Roach break; 6665a78cd34SGreg Roach 6675a78cd34SGreg Roach case 'descendants': 66839ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 6695a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($family); 6705a78cd34SGreg Roach } 6715a78cd34SGreg Roach break; 6725a78cd34SGreg Roach } 6735a78cd34SGreg Roach 6746ccdf4f0SGreg Roach return redirect($individual->url()); 6755a78cd34SGreg Roach } 6765a78cd34SGreg Roach 6775a78cd34SGreg Roach /** 6785a78cd34SGreg Roach * @param Individual $individual 67918d7a90dSGreg Roach * 68018d7a90dSGreg Roach * @return void 6815a78cd34SGreg Roach */ 682e364afe4SGreg Roach private function addAncestorsToCart(Individual $individual): void 683c1010edaSGreg Roach { 6845a78cd34SGreg Roach $this->addRecordToCart($individual); 6855a78cd34SGreg Roach 68639ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 68739ca88baSGreg Roach foreach ($family->spouses() as $parent) { 6885a78cd34SGreg Roach $this->addAncestorsToCart($parent); 6895a78cd34SGreg Roach } 6905a78cd34SGreg Roach } 6915a78cd34SGreg Roach } 6925a78cd34SGreg Roach 6935a78cd34SGreg Roach /** 6945a78cd34SGreg Roach * @param Individual $individual 69518d7a90dSGreg Roach * 69618d7a90dSGreg Roach * @return void 6975a78cd34SGreg Roach */ 698e364afe4SGreg Roach private function addAncestorFamiliesToCart(Individual $individual): void 699c1010edaSGreg Roach { 70039ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 7015a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 70239ca88baSGreg Roach foreach ($family->spouses() as $parent) { 703cad6d3f3SGreg Roach $this->addAncestorFamiliesToCart($parent); 7045a78cd34SGreg Roach } 7055a78cd34SGreg Roach } 7065a78cd34SGreg Roach } 7075a78cd34SGreg Roach 7085a78cd34SGreg Roach /** 7096ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7105a78cd34SGreg Roach * 7116ccdf4f0SGreg Roach * @return ResponseInterface 7125a78cd34SGreg Roach */ 71357ab2231SGreg Roach public function getAddMediaAction(ServerRequestInterface $request): ResponseInterface 714c1010edaSGreg Roach { 71557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 71675964c75SGreg Roach assert($tree instanceof Tree); 7175229eadeSGreg Roach 718bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 7195a78cd34SGreg Roach 7205a78cd34SGreg Roach $media = Media::getInstance($xref, $tree); 7215a78cd34SGreg Roach 7225a78cd34SGreg Roach if ($media === null) { 72359f2f229SGreg Roach throw new MediaNotFoundException(); 7245a78cd34SGreg Roach } 7255a78cd34SGreg Roach 7265a78cd34SGreg Roach $options = $this->mediaOptions($media); 7275a78cd34SGreg Roach 72839ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $media->fullName()); 7295a78cd34SGreg Roach 7305a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 7315a78cd34SGreg Roach 'options' => $options, 7325a78cd34SGreg Roach 'default' => key($options), 7335a78cd34SGreg Roach 'record' => $media, 7345a78cd34SGreg Roach 'title' => $title, 7355a78cd34SGreg Roach 'tree' => $tree, 7365a78cd34SGreg Roach ]); 7375a78cd34SGreg Roach } 7385a78cd34SGreg Roach 7395a78cd34SGreg Roach /** 7405a78cd34SGreg Roach * @param Media $media 7415a78cd34SGreg Roach * 7425a78cd34SGreg Roach * @return string[] 7435a78cd34SGreg Roach */ 744c1010edaSGreg Roach private function mediaOptions(Media $media): array 745c1010edaSGreg Roach { 74639ca88baSGreg Roach $name = strip_tags($media->fullName()); 7475a78cd34SGreg Roach 7485a78cd34SGreg Roach return [ 7495a78cd34SGreg Roach 'self' => $name, 7505a78cd34SGreg Roach ]; 7515a78cd34SGreg Roach } 7525a78cd34SGreg Roach 7535a78cd34SGreg Roach /** 7546ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7555a78cd34SGreg Roach * 7566ccdf4f0SGreg Roach * @return ResponseInterface 7575a78cd34SGreg Roach */ 75857ab2231SGreg Roach public function postAddMediaAction(ServerRequestInterface $request): ResponseInterface 759c1010edaSGreg Roach { 76057ab2231SGreg Roach $tree = $request->getAttribute('tree'); 76175964c75SGreg Roach assert($tree instanceof Tree); 7625229eadeSGreg Roach 763bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 7645a78cd34SGreg Roach 7655a78cd34SGreg Roach $media = Media::getInstance($xref, $tree); 7665a78cd34SGreg Roach 7675a78cd34SGreg Roach if ($media === null) { 76859f2f229SGreg Roach throw new MediaNotFoundException(); 7695a78cd34SGreg Roach } 7705a78cd34SGreg Roach 7715a78cd34SGreg Roach $this->addRecordToCart($media); 7725a78cd34SGreg Roach 7736ccdf4f0SGreg Roach return redirect($media->url()); 7745a78cd34SGreg Roach } 7755a78cd34SGreg Roach 7765a78cd34SGreg Roach /** 7776ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7785a78cd34SGreg Roach * 7796ccdf4f0SGreg Roach * @return ResponseInterface 7805a78cd34SGreg Roach */ 78157ab2231SGreg Roach public function getAddNoteAction(ServerRequestInterface $request): ResponseInterface 782c1010edaSGreg Roach { 78357ab2231SGreg Roach $tree = $request->getAttribute('tree'); 78475964c75SGreg Roach assert($tree instanceof Tree); 7855229eadeSGreg Roach 786bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 7875a78cd34SGreg Roach 7885a78cd34SGreg Roach $note = Note::getInstance($xref, $tree); 7895a78cd34SGreg Roach 7905a78cd34SGreg Roach if ($note === null) { 79159f2f229SGreg Roach throw new NoteNotFoundException(); 7925a78cd34SGreg Roach } 7935a78cd34SGreg Roach 7945a78cd34SGreg Roach $options = $this->noteOptions($note); 7955a78cd34SGreg Roach 79639ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $note->fullName()); 7975a78cd34SGreg Roach 7985a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 7995a78cd34SGreg Roach 'options' => $options, 8005a78cd34SGreg Roach 'default' => key($options), 8015a78cd34SGreg Roach 'record' => $note, 8025a78cd34SGreg Roach 'title' => $title, 8035a78cd34SGreg Roach 'tree' => $tree, 8045a78cd34SGreg Roach ]); 8055a78cd34SGreg Roach } 8065a78cd34SGreg Roach 8075a78cd34SGreg Roach /** 8085a78cd34SGreg Roach * @param Note $note 8095a78cd34SGreg Roach * 8105a78cd34SGreg Roach * @return string[] 8115a78cd34SGreg Roach */ 812c1010edaSGreg Roach private function noteOptions(Note $note): array 813c1010edaSGreg Roach { 81439ca88baSGreg Roach $name = strip_tags($note->fullName()); 8155a78cd34SGreg Roach 8165a78cd34SGreg Roach return [ 8175a78cd34SGreg Roach 'self' => $name, 8185a78cd34SGreg Roach ]; 8195a78cd34SGreg Roach } 8205a78cd34SGreg Roach 8215a78cd34SGreg Roach /** 8226ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8235a78cd34SGreg Roach * 8246ccdf4f0SGreg Roach * @return ResponseInterface 8255a78cd34SGreg Roach */ 82657ab2231SGreg Roach public function postAddNoteAction(ServerRequestInterface $request): ResponseInterface 827c1010edaSGreg Roach { 82857ab2231SGreg Roach $tree = $request->getAttribute('tree'); 82975964c75SGreg Roach assert($tree instanceof Tree); 8305229eadeSGreg Roach 831bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8325a78cd34SGreg Roach 8335a78cd34SGreg Roach $note = Note::getInstance($xref, $tree); 8345a78cd34SGreg Roach 8355a78cd34SGreg Roach if ($note === null) { 83659f2f229SGreg Roach throw new NoteNotFoundException(); 8375a78cd34SGreg Roach } 8385a78cd34SGreg Roach 8395a78cd34SGreg Roach $this->addRecordToCart($note); 8405a78cd34SGreg Roach 8416ccdf4f0SGreg Roach return redirect($note->url()); 8425a78cd34SGreg Roach } 8435a78cd34SGreg Roach 8445a78cd34SGreg Roach /** 8456ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8465a78cd34SGreg Roach * 8476ccdf4f0SGreg Roach * @return ResponseInterface 8485a78cd34SGreg Roach */ 84957ab2231SGreg Roach public function getAddRepositoryAction(ServerRequestInterface $request): ResponseInterface 850c1010edaSGreg Roach { 85157ab2231SGreg Roach $tree = $request->getAttribute('tree'); 85275964c75SGreg Roach assert($tree instanceof Tree); 8535229eadeSGreg Roach 854bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8555a78cd34SGreg Roach 8565a78cd34SGreg Roach $repository = Repository::getInstance($xref, $tree); 8575a78cd34SGreg Roach 8585a78cd34SGreg Roach if ($repository === null) { 85959f2f229SGreg Roach throw new RepositoryNotFoundException(); 8605a78cd34SGreg Roach } 8615a78cd34SGreg Roach 8625a78cd34SGreg Roach $options = $this->repositoryOptions($repository); 8635a78cd34SGreg Roach 86439ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $repository->fullName()); 8655a78cd34SGreg Roach 8665a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 8675a78cd34SGreg Roach 'options' => $options, 8685a78cd34SGreg Roach 'default' => key($options), 8695a78cd34SGreg Roach 'record' => $repository, 8705a78cd34SGreg Roach 'title' => $title, 8715a78cd34SGreg Roach 'tree' => $tree, 8725a78cd34SGreg Roach ]); 8735a78cd34SGreg Roach } 8745a78cd34SGreg Roach 8755a78cd34SGreg Roach /** 8765a78cd34SGreg Roach * @param Repository $repository 8775a78cd34SGreg Roach * 8785a78cd34SGreg Roach * @return string[] 8795a78cd34SGreg Roach */ 880c1010edaSGreg Roach private function repositoryOptions(Repository $repository): array 881c1010edaSGreg Roach { 88239ca88baSGreg Roach $name = strip_tags($repository->fullName()); 8835a78cd34SGreg Roach 8845a78cd34SGreg Roach return [ 8855a78cd34SGreg Roach 'self' => $name, 8865a78cd34SGreg Roach ]; 8875a78cd34SGreg Roach } 8885a78cd34SGreg Roach 8895a78cd34SGreg Roach /** 8906ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8915a78cd34SGreg Roach * 8926ccdf4f0SGreg Roach * @return ResponseInterface 8935a78cd34SGreg Roach */ 89457ab2231SGreg Roach public function postAddRepositoryAction(ServerRequestInterface $request): ResponseInterface 895c1010edaSGreg Roach { 89657ab2231SGreg Roach $tree = $request->getAttribute('tree'); 89775964c75SGreg Roach assert($tree instanceof Tree); 8985229eadeSGreg Roach 899bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 9005a78cd34SGreg Roach 9015a78cd34SGreg Roach $repository = Repository::getInstance($xref, $tree); 9025a78cd34SGreg Roach 9035a78cd34SGreg Roach if ($repository === null) { 90459f2f229SGreg Roach throw new RepositoryNotFoundException(); 9055a78cd34SGreg Roach } 9065a78cd34SGreg Roach 9075a78cd34SGreg Roach $this->addRecordToCart($repository); 9085a78cd34SGreg Roach 9096ccdf4f0SGreg Roach return redirect($repository->url()); 9105a78cd34SGreg Roach } 9115a78cd34SGreg Roach 9125a78cd34SGreg Roach /** 9136ccdf4f0SGreg Roach * @param ServerRequestInterface $request 9145a78cd34SGreg Roach * 9156ccdf4f0SGreg Roach * @return ResponseInterface 9165a78cd34SGreg Roach */ 91757ab2231SGreg Roach public function getAddSourceAction(ServerRequestInterface $request): ResponseInterface 918c1010edaSGreg Roach { 91957ab2231SGreg Roach $tree = $request->getAttribute('tree'); 92075964c75SGreg Roach assert($tree instanceof Tree); 9215229eadeSGreg Roach 922bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 9235a78cd34SGreg Roach 9245a78cd34SGreg Roach $source = Source::getInstance($xref, $tree); 9255a78cd34SGreg Roach 9265a78cd34SGreg Roach if ($source === null) { 92759f2f229SGreg Roach throw new SourceNotFoundException(); 9285a78cd34SGreg Roach } 9295a78cd34SGreg Roach 9305a78cd34SGreg Roach $options = $this->sourceOptions($source); 9315a78cd34SGreg Roach 93239ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $source->fullName()); 9335a78cd34SGreg Roach 9345a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 9355a78cd34SGreg Roach 'options' => $options, 9365a78cd34SGreg Roach 'default' => key($options), 9375a78cd34SGreg Roach 'record' => $source, 9385a78cd34SGreg Roach 'title' => $title, 9395a78cd34SGreg Roach 'tree' => $tree, 9405a78cd34SGreg Roach ]); 9415a78cd34SGreg Roach } 9425a78cd34SGreg Roach 9435a78cd34SGreg Roach /** 9445a78cd34SGreg Roach * @param Source $source 9455a78cd34SGreg Roach * 9465a78cd34SGreg Roach * @return string[] 9475a78cd34SGreg Roach */ 948c1010edaSGreg Roach private function sourceOptions(Source $source): array 949c1010edaSGreg Roach { 95039ca88baSGreg Roach $name = strip_tags($source->fullName()); 9515a78cd34SGreg Roach 9525a78cd34SGreg Roach return [ 95339ca88baSGreg Roach 'only' => strip_tags($source->fullName()), 9545a78cd34SGreg Roach 'linked' => I18N::translate('%s and the individuals that reference it.', $name), 9555a78cd34SGreg Roach ]; 9565a78cd34SGreg Roach } 9575a78cd34SGreg Roach 9585a78cd34SGreg Roach /** 9596ccdf4f0SGreg Roach * @param ServerRequestInterface $request 9605a78cd34SGreg Roach * 9616ccdf4f0SGreg Roach * @return ResponseInterface 9625a78cd34SGreg Roach */ 96357ab2231SGreg Roach public function postAddSourceAction(ServerRequestInterface $request): ResponseInterface 964c1010edaSGreg Roach { 96557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 96675964c75SGreg Roach assert($tree instanceof Tree); 9675229eadeSGreg Roach 968b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 969b46c87bdSGreg Roach 970b46c87bdSGreg Roach $xref = $params['xref']; 971b46c87bdSGreg Roach $option = $params['option']; 9725a78cd34SGreg Roach 9735a78cd34SGreg Roach $source = Source::getInstance($xref, $tree); 9745a78cd34SGreg Roach 9755a78cd34SGreg Roach if ($source === null) { 97659f2f229SGreg Roach throw new SourceNotFoundException(); 9775a78cd34SGreg Roach } 9785a78cd34SGreg Roach 9795a78cd34SGreg Roach $this->addRecordToCart($source); 9805a78cd34SGreg Roach 9815a78cd34SGreg Roach if ($option === 'linked') { 9825a78cd34SGreg Roach foreach ($source->linkedIndividuals('SOUR') as $individual) { 9835a78cd34SGreg Roach $this->addRecordToCart($individual); 9845a78cd34SGreg Roach } 9855a78cd34SGreg Roach foreach ($source->linkedFamilies('SOUR') as $family) { 9865a78cd34SGreg Roach $this->addRecordToCart($family); 9875a78cd34SGreg Roach } 9885a78cd34SGreg Roach } 9895a78cd34SGreg Roach 9906ccdf4f0SGreg Roach return redirect($source->url()); 9915a78cd34SGreg Roach } 9925a78cd34SGreg Roach 9935a78cd34SGreg Roach /** 9945a78cd34SGreg Roach * Get all the records in the cart. 9955a78cd34SGreg Roach * 9965a78cd34SGreg Roach * @param Tree $tree 9975a78cd34SGreg Roach * 9985a78cd34SGreg Roach * @return GedcomRecord[] 9995a78cd34SGreg Roach */ 1000c1010edaSGreg Roach private function allRecordsInCart(Tree $tree): array 1001c1010edaSGreg Roach { 10025a78cd34SGreg Roach $cart = Session::get('cart', []); 10035a78cd34SGreg Roach 1004aa6f03bbSGreg Roach $xrefs = array_keys($cart[$tree->name()] ?? []); 10055a78cd34SGreg Roach 10065a78cd34SGreg Roach // Fetch all the records in the cart. 1007bed27cedSGreg Roach $records = array_map(static function (string $xref) use ($tree): ?GedcomRecord { 10085a78cd34SGreg Roach return GedcomRecord::getInstance($xref, $tree); 10095a78cd34SGreg Roach }, $xrefs); 10105a78cd34SGreg Roach 10115a78cd34SGreg Roach // Some records may have been deleted after they were added to the cart. 10125a78cd34SGreg Roach $records = array_filter($records); 10135a78cd34SGreg Roach 10145a78cd34SGreg Roach // Group and sort. 10150b5fd0a6SGreg Roach uasort($records, static function (GedcomRecord $x, GedcomRecord $y): int { 1016c156e8f5SGreg Roach return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::nameComparator()($x, $y); 10175a78cd34SGreg Roach }); 10185a78cd34SGreg Roach 10195a78cd34SGreg Roach return $records; 10205a78cd34SGreg Roach } 10215a78cd34SGreg Roach 10225a78cd34SGreg Roach /** 10235a78cd34SGreg Roach * Add a record (and direclty linked sources, notes, etc. to the cart. 10245a78cd34SGreg Roach * 10255a78cd34SGreg Roach * @param GedcomRecord $record 102618d7a90dSGreg Roach * 102718d7a90dSGreg Roach * @return void 10285a78cd34SGreg Roach */ 1029e364afe4SGreg Roach private function addRecordToCart(GedcomRecord $record): void 1030c1010edaSGreg Roach { 10315a78cd34SGreg Roach $cart = Session::get('cart', []); 10325a78cd34SGreg Roach 1033f4afa648SGreg Roach $tree_name = $record->tree()->name(); 10345a78cd34SGreg Roach 10355a78cd34SGreg Roach // Add this record 1036c0935879SGreg Roach $cart[$tree_name][$record->xref()] = true; 10375a78cd34SGreg Roach 10385a78cd34SGreg Roach // Add directly linked media, notes, repositories and sources. 10398d0ebef0SGreg Roach preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches); 10405a78cd34SGreg Roach 10415a78cd34SGreg Roach foreach ($matches[1] as $match) { 10425a78cd34SGreg Roach $cart[$tree_name][$match] = true; 10435a78cd34SGreg Roach } 10445a78cd34SGreg Roach 10455a78cd34SGreg Roach Session::put('cart', $cart); 10465a78cd34SGreg Roach } 10475a78cd34SGreg Roach 10485a78cd34SGreg Roach /** 10495a78cd34SGreg Roach * @param Tree $tree 10505a78cd34SGreg Roach * 10515a78cd34SGreg Roach * @return bool 10525a78cd34SGreg Roach */ 1053c1010edaSGreg Roach private function isCartEmpty(Tree $tree): bool 1054c1010edaSGreg Roach { 10555a78cd34SGreg Roach $cart = Session::get('cart', []); 1056a91af26aSGreg Roach $contents = $cart[$tree->name()] ?? []; 10575a78cd34SGreg Roach 1058a91af26aSGreg Roach return $contents === []; 10595a78cd34SGreg Roach } 10608c2e8227SGreg Roach} 1061