18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 5*a091ac74SGreg Roach * Copyright (C) 2020 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; 30*a091ac74SGreg Roachuse Fisharebest\Webtrees\Factory; 310e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family; 325a78cd34SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport; 335a78cd34SGreg Roachuse Fisharebest\Webtrees\Gedcom; 340e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 35f95e0480SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\FamilyPage; 36f95e0480SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\IndividualPage; 37f95e0480SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\MediaPage; 38f95e0480SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\NotePage; 39f95e0480SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\RepositoryPage; 40f95e0480SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\SourcePage; 410e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 420e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 435a78cd34SGreg Roachuse Fisharebest\Webtrees\Media; 440e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu; 455a78cd34SGreg Roachuse Fisharebest\Webtrees\Note; 465a78cd34SGreg Roachuse Fisharebest\Webtrees\Repository; 47e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 480e62c4b8SGreg Roachuse Fisharebest\Webtrees\Session; 495a78cd34SGreg Roachuse Fisharebest\Webtrees\Source; 50aee13b6dSGreg Roachuse Fisharebest\Webtrees\Tree; 515a78cd34SGreg Roachuse League\Flysystem\Filesystem; 52a04bb9a2SGreg Roachuse League\Flysystem\FilesystemInterface; 5361bf91b2SGreg Roachuse League\Flysystem\MountManager; 545a78cd34SGreg Roachuse League\Flysystem\ZipArchive\ZipArchiveAdapter; 55bed27cedSGreg Roachuse Psr\Http\Message\ResponseFactoryInterface; 566ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 576ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 586ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface; 593976b470SGreg Roach 60eb235819SGreg Roachuse function app; 61bf80ec58SGreg Roachuse function array_filter; 62bf80ec58SGreg Roachuse function array_keys; 63bf80ec58SGreg Roachuse function array_map; 645229eadeSGreg Roachuse function assert; 65bf80ec58SGreg Roachuse function in_array; 66ddeb3354SGreg Roachuse function is_string; 67bf80ec58SGreg Roachuse function key; 68bf80ec58SGreg Roachuse function preg_match_all; 69bf80ec58SGreg Roachuse function redirect; 70bf80ec58SGreg Roachuse function route; 71e5a6b4d4SGreg Roachuse function str_replace; 72bf80ec58SGreg Roachuse function strip_tags; 73bf80ec58SGreg Roachuse function utf8_decode; 748c2e8227SGreg Roach 758c2e8227SGreg Roach/** 768c2e8227SGreg Roach * Class ClippingsCartModule 778c2e8227SGreg Roach */ 7837eb8894SGreg Roachclass ClippingsCartModule extends AbstractModule implements ModuleMenuInterface 79c1010edaSGreg Roach{ 8049a243cbSGreg Roach use ModuleMenuTrait; 8149a243cbSGreg Roach 825a78cd34SGreg Roach // Routes that have a record which can be added to the clipboard 8316d6367aSGreg Roach private const ROUTES_WITH_RECORDS = [ 84f95e0480SGreg Roach 'Family' => FamilyPage::class, 85f95e0480SGreg Roach 'Individual' => IndividualPage::class, 86f95e0480SGreg Roach 'Media' => MediaPage::class, 87f95e0480SGreg Roach 'Note' => NotePage::class, 88f95e0480SGreg Roach 'Repository' => RepositoryPage::class, 89f95e0480SGreg Roach 'Source' => SourcePage::class, 90c1010edaSGreg Roach ]; 915a78cd34SGreg Roach 9249a243cbSGreg Roach /** @var int The default access level for this module. It can be changed in the control panel. */ 9349a243cbSGreg Roach protected $access_level = Auth::PRIV_USER; 9449a243cbSGreg Roach 95961ec755SGreg Roach /** 96e5a6b4d4SGreg Roach * @var UserService 97e5a6b4d4SGreg Roach */ 98e5a6b4d4SGreg Roach private $user_service; 99e5a6b4d4SGreg Roach 100e5a6b4d4SGreg Roach /** 101e5a6b4d4SGreg Roach * ClippingsCartModule constructor. 102e5a6b4d4SGreg Roach * 103e5a6b4d4SGreg Roach * @param UserService $user_service 104e5a6b4d4SGreg Roach */ 105e5a6b4d4SGreg Roach public function __construct(UserService $user_service) 106e5a6b4d4SGreg Roach { 107e5a6b4d4SGreg Roach $this->user_service = $user_service; 108e5a6b4d4SGreg Roach } 109e5a6b4d4SGreg Roach 110e5a6b4d4SGreg Roach /** 1110cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 112961ec755SGreg Roach * 113961ec755SGreg Roach * @return string 114961ec755SGreg Roach */ 11549a243cbSGreg Roach public function title(): string 116c1010edaSGreg Roach { 117bbb76c12SGreg Roach /* I18N: Name of a module */ 118bbb76c12SGreg Roach return I18N::translate('Clippings cart'); 1198c2e8227SGreg Roach } 1208c2e8227SGreg Roach 121961ec755SGreg Roach /** 122961ec755SGreg Roach * A sentence describing what this module does. 123961ec755SGreg Roach * 124961ec755SGreg Roach * @return string 125961ec755SGreg Roach */ 12649a243cbSGreg Roach public function description(): string 127c1010edaSGreg Roach { 128bbb76c12SGreg Roach /* I18N: Description of the “Clippings cart” module */ 129bbb76c12SGreg Roach return I18N::translate('Select records from your family tree and save them as a GEDCOM file.'); 1308c2e8227SGreg Roach } 1318c2e8227SGreg Roach 1320ee13198SGreg Roach /** 13349a243cbSGreg Roach * The default position for this menu. It can be changed in the control panel. 1340ee13198SGreg Roach * 1350ee13198SGreg Roach * @return int 1360ee13198SGreg Roach */ 1378f53f488SRico Sonntag public function defaultMenuOrder(): int 138c1010edaSGreg Roach { 139353b36abSGreg Roach return 6; 1408c2e8227SGreg Roach } 1418c2e8227SGreg Roach 1420ee13198SGreg Roach /** 1430ee13198SGreg Roach * A menu, to be added to the main application menu. 1440ee13198SGreg Roach * 145aee13b6dSGreg Roach * @param Tree $tree 146aee13b6dSGreg Roach * 1470ee13198SGreg Roach * @return Menu|null 1480ee13198SGreg Roach */ 14946295629SGreg Roach public function getMenu(Tree $tree): ?Menu 150c1010edaSGreg Roach { 151eb235819SGreg Roach /** @var ServerRequestInterface $request */ 1526ccdf4f0SGreg Roach $request = app(ServerRequestInterface::class); 1538c2e8227SGreg Roach 154f7ab47b1SGreg Roach $route = $request->getAttribute('route'); 155de2aa325SGreg Roach assert($route instanceof Route); 1565a78cd34SGreg Roach 1575a78cd34SGreg Roach $submenus = [ 15849a243cbSGreg Roach new Menu($this->title(), route('module', [ 15926684e68SGreg Roach 'module' => $this->name(), 160c1010edaSGreg Roach 'action' => 'Show', 161d72b284aSGreg Roach 'tree' => $tree->name(), 162c1010edaSGreg Roach ]), 'menu-clippings-cart', ['rel' => 'nofollow']), 1635a78cd34SGreg Roach ]; 1645a78cd34SGreg Roach 1652b0d92b4SGreg Roach $action = array_search($route->name, self::ROUTES_WITH_RECORDS, true); 166f95e0480SGreg Roach if ($action !== false) { 1672b0d92b4SGreg Roach $xref = $route->attributes['xref']; 168ddeb3354SGreg Roach assert(is_string($xref)); 169ddeb3354SGreg Roach 170c1010edaSGreg Roach $add_route = route('module', [ 17126684e68SGreg Roach 'module' => $this->name(), 172f95e0480SGreg Roach 'action' => 'Add' . $action, 173c1010edaSGreg Roach 'xref' => $xref, 174d72b284aSGreg Roach 'tree' => $tree->name(), 175c1010edaSGreg Roach ]); 1765a78cd34SGreg Roach 17725b2dde3SGreg Roach $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']); 1788c2e8227SGreg Roach } 179cbc1590aSGreg Roach 1805a78cd34SGreg Roach if (!$this->isCartEmpty($tree)) { 181c1010edaSGreg Roach $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [ 18226684e68SGreg Roach 'module' => $this->name(), 183c1010edaSGreg Roach 'action' => 'Empty', 184d72b284aSGreg Roach 'tree' => $tree->name(), 185c1010edaSGreg Roach ]), 'menu-clippings-empty', ['rel' => 'nofollow']); 186f95e0480SGreg Roach 187c1010edaSGreg Roach $submenus[] = new Menu(I18N::translate('Download'), route('module', [ 18826684e68SGreg Roach 'module' => $this->name(), 189c1010edaSGreg Roach 'action' => 'DownloadForm', 190d72b284aSGreg Roach 'tree' => $tree->name(), 191c1010edaSGreg Roach ]), 'menu-clippings-download', ['rel' => 'nofollow']); 1925a78cd34SGreg Roach } 1935a78cd34SGreg Roach 19449a243cbSGreg Roach return new Menu($this->title(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus); 1958c2e8227SGreg Roach } 1968c2e8227SGreg Roach 19776692c8bSGreg Roach /** 1986ccdf4f0SGreg Roach * @param ServerRequestInterface $request 19976692c8bSGreg Roach * 2006ccdf4f0SGreg Roach * @return ResponseInterface 20176692c8bSGreg Roach */ 202f95e0480SGreg Roach public function postDownloadAction(ServerRequestInterface $request): ResponseInterface 203c1010edaSGreg Roach { 20457ab2231SGreg Roach $tree = $request->getAttribute('tree'); 2054ea62551SGreg Roach assert($tree instanceof Tree); 2064ea62551SGreg Roach 207a04bb9a2SGreg Roach $data_filesystem = $request->getAttribute('filesystem.data'); 208a04bb9a2SGreg Roach assert($data_filesystem instanceof FilesystemInterface); 209a04bb9a2SGreg Roach 210b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 211b46c87bdSGreg Roach 212b46c87bdSGreg Roach $privatize_export = $params['privatize_export']; 213e2ed7c79SGreg Roach 214e2ed7c79SGreg Roach if ($privatize_export === 'none' && !Auth::isManager($tree)) { 215e2ed7c79SGreg Roach $privatize_export = 'member'; 216e2ed7c79SGreg Roach } 217e2ed7c79SGreg Roach 218e2ed7c79SGreg Roach if ($privatize_export === 'gedadmin' && !Auth::isManager($tree)) { 219e2ed7c79SGreg Roach $privatize_export = 'member'; 220e2ed7c79SGreg Roach } 221e2ed7c79SGreg Roach 222e2ed7c79SGreg Roach if ($privatize_export === 'user' && !Auth::isMember($tree)) { 223e2ed7c79SGreg Roach $privatize_export = 'visitor'; 224e2ed7c79SGreg Roach } 225e2ed7c79SGreg Roach 226b46c87bdSGreg Roach $convert = (bool) ($params['convert'] ?? false); 2278c2e8227SGreg Roach 22813abd6f3SGreg Roach $cart = Session::get('cart', []); 2298c2e8227SGreg Roach 230aa6f03bbSGreg Roach $xrefs = array_keys($cart[$tree->name()] ?? []); 231c8846facSGreg Roach $xrefs = array_map('strval', $xrefs); // PHP converts numeric keys to integers. 2325a78cd34SGreg Roach 2335a78cd34SGreg Roach // Create a new/empty .ZIP file 234a00baf47SGreg Roach $temp_zip_file = stream_get_meta_data(tmpfile())['uri']; 2357f996f6eSGreg Roach $zip_adapter = new ZipArchiveAdapter($temp_zip_file); 2367f996f6eSGreg Roach $zip_filesystem = new Filesystem($zip_adapter); 2375a78cd34SGreg Roach 23861bf91b2SGreg Roach $manager = new MountManager([ 239a04bb9a2SGreg Roach 'media' => $tree->mediaFilesystem($data_filesystem), 24061bf91b2SGreg Roach 'zip' => $zip_filesystem, 24161bf91b2SGreg Roach ]); 24261bf91b2SGreg Roach 2435a78cd34SGreg Roach // Media file prefix 2445a78cd34SGreg Roach $path = $tree->getPreference('MEDIA_DIRECTORY'); 2455a78cd34SGreg Roach 2465a78cd34SGreg Roach // GEDCOM file header 247a3d8780cSGreg Roach $filetext = FunctionsExport::gedcomHeader($tree, $convert ? 'ANSI' : 'UTF-8'); 2485a78cd34SGreg Roach 2495a78cd34SGreg Roach switch ($privatize_export) { 2505a78cd34SGreg Roach case 'gedadmin': 2515a78cd34SGreg Roach $access_level = Auth::PRIV_NONE; 2525a78cd34SGreg Roach break; 2535a78cd34SGreg Roach case 'user': 2545a78cd34SGreg Roach $access_level = Auth::PRIV_USER; 2555a78cd34SGreg Roach break; 2565a78cd34SGreg Roach case 'visitor': 2575a78cd34SGreg Roach $access_level = Auth::PRIV_PRIVATE; 2585a78cd34SGreg Roach break; 2595a78cd34SGreg Roach case 'none': 2605a78cd34SGreg Roach default: 2615a78cd34SGreg Roach $access_level = Auth::PRIV_HIDE; 2625a78cd34SGreg Roach break; 2635a78cd34SGreg Roach } 2645a78cd34SGreg Roach 2655a78cd34SGreg Roach foreach ($xrefs as $xref) { 266*a091ac74SGreg Roach $object = Factory::gedcomRecord()->make($xref, $tree); 2675a78cd34SGreg Roach // The object may have been deleted since we added it to the cart.... 268bed27cedSGreg Roach if ($object instanceof GedcomRecord) { 2695a78cd34SGreg Roach $record = $object->privatizeGedcom($access_level); 2705a78cd34SGreg Roach // Remove links to objects that aren't in the cart 2718d0ebef0SGreg Roach preg_match_all('/\n1 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER); 2725a78cd34SGreg Roach foreach ($matches as $match) { 273bf80ec58SGreg Roach if (!in_array($match[1], $xrefs, true)) { 2745a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2755a78cd34SGreg Roach } 2765a78cd34SGreg Roach } 2778d0ebef0SGreg Roach preg_match_all('/\n2 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER); 2785a78cd34SGreg Roach foreach ($matches as $match) { 279bf80ec58SGreg Roach if (!in_array($match[1], $xrefs, true)) { 2805a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2815a78cd34SGreg Roach } 2825a78cd34SGreg Roach } 2838d0ebef0SGreg Roach preg_match_all('/\n3 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER); 2845a78cd34SGreg Roach foreach ($matches as $match) { 285bf80ec58SGreg Roach if (!in_array($match[1], $xrefs, true)) { 2865a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2875a78cd34SGreg Roach } 2885a78cd34SGreg Roach } 2895a78cd34SGreg Roach 29055167344SGreg Roach if ($object instanceof Individual || $object instanceof Family) { 2915a78cd34SGreg Roach $filetext .= $record . "\n"; 2925a78cd34SGreg Roach $filetext .= "1 SOUR @WEBTREES@\n"; 2931f273236SGreg Roach $filetext .= '2 PAGE ' . $object->url() . "\n"; 29455167344SGreg Roach } elseif ($object instanceof Source) { 2955a78cd34SGreg Roach $filetext .= $record . "\n"; 2961f273236SGreg Roach $filetext .= '1 NOTE ' . $object->url() . "\n"; 29755167344SGreg Roach } elseif ($object instanceof Media) { 29855167344SGreg Roach // Add the media files to the archive 2995a78cd34SGreg Roach foreach ($object->mediaFiles() as $media_file) { 30061bf91b2SGreg Roach $from = 'media://' . $media_file->filename(); 30161bf91b2SGreg Roach $to = 'zip://' . $path . $media_file->filename(); 3025de4ab51SGreg Roach if (!$media_file->isExternal() && $manager->has($from) && !$manager->has($to)) { 30361bf91b2SGreg Roach $manager->copy($from, $to); 3045a78cd34SGreg Roach } 3055a78cd34SGreg Roach } 3065a78cd34SGreg Roach $filetext .= $record . "\n"; 30755167344SGreg Roach } else { 3085a78cd34SGreg Roach $filetext .= $record . "\n"; 3098c2e8227SGreg Roach } 3108c2e8227SGreg Roach } 3118c2e8227SGreg Roach } 3128c2e8227SGreg Roach 3139b93b7c3SGreg Roach $base_url = $request->getAttribute('base_url'); 3149b93b7c3SGreg Roach 3155a78cd34SGreg Roach // Create a source, to indicate the source of the data. 3169b93b7c3SGreg Roach $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . $base_url . "\n"; 317e5a6b4d4SGreg Roach $author = $this->user_service->find((int) $tree->getPreference('CONTACT_USER_ID')); 3185a78cd34SGreg Roach if ($author !== null) { 319e5a6b4d4SGreg Roach $filetext .= '1 AUTH ' . $author->realName() . "\n"; 3205a78cd34SGreg Roach } 3215a78cd34SGreg Roach $filetext .= "0 TRLR\n"; 3225a78cd34SGreg Roach 3235a78cd34SGreg Roach // Make sure the preferred line endings are used 324679203a4SGreg Roach $filetext = strtr($filetext, ["\n" => Gedcom::EOL]); 3255a78cd34SGreg Roach 32655167344SGreg Roach if ($convert) { 3275a78cd34SGreg Roach $filetext = utf8_decode($filetext); 3288c2e8227SGreg Roach } 329cbc1590aSGreg Roach 3305a78cd34SGreg Roach // Finally add the GEDCOM file to the .ZIP file. 3315a78cd34SGreg Roach $zip_filesystem->write('clippings.ged', $filetext); 3325a78cd34SGreg Roach 33361bf91b2SGreg Roach // Need to force-close ZipArchive filesystems. 3347f996f6eSGreg Roach $zip_adapter->getArchive()->close(); 3355a78cd34SGreg Roach 3366ccdf4f0SGreg Roach // Use a stream, so that we do not have to load the entire file into memory. 3376ccdf4f0SGreg Roach $stream = app(StreamFactoryInterface::class)->createStreamFromFile($temp_zip_file); 3385a78cd34SGreg Roach 339bed27cedSGreg Roach /** @var ResponseFactoryInterface $response_factory */ 340bed27cedSGreg Roach $response_factory = app(ResponseFactoryInterface::class); 341bed27cedSGreg Roach 342bed27cedSGreg Roach return $response_factory->createResponse() 3436ccdf4f0SGreg Roach ->withBody($stream) 3441b3d4731SGreg Roach ->withHeader('Content-Type', 'application/zip') 345bed27cedSGreg Roach ->withHeader('Content-Disposition', 'attachment; filename="clippings.zip'); 3468c2e8227SGreg Roach } 3478c2e8227SGreg Roach 3488c2e8227SGreg Roach /** 34957ab2231SGreg Roach * @param ServerRequestInterface $request 35076692c8bSGreg Roach * 3516ccdf4f0SGreg Roach * @return ResponseInterface 3528c2e8227SGreg Roach */ 35357ab2231SGreg Roach public function getDownloadFormAction(ServerRequestInterface $request): ResponseInterface 354c1010edaSGreg Roach { 35557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 3564ea62551SGreg Roach assert($tree instanceof Tree); 3574ea62551SGreg Roach 35857ab2231SGreg Roach $user = $request->getAttribute('user'); 3595a78cd34SGreg Roach $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download'); 3608c2e8227SGreg Roach 3615a78cd34SGreg Roach return $this->viewResponse('modules/clippings/download', [ 3625a78cd34SGreg Roach 'is_manager' => Auth::isManager($tree, $user), 3635a78cd34SGreg Roach 'is_member' => Auth::isMember($tree, $user), 36471378461SGreg Roach 'module' => $this->name(), 3655a78cd34SGreg Roach 'title' => $title, 366f95e0480SGreg Roach 'tree' => $tree, 3675a78cd34SGreg Roach ]); 3688c2e8227SGreg Roach } 3698c2e8227SGreg Roach 3705a78cd34SGreg Roach /** 37157ab2231SGreg Roach * @param ServerRequestInterface $request 3725a78cd34SGreg Roach * 3736ccdf4f0SGreg Roach * @return ResponseInterface 3745a78cd34SGreg Roach */ 37557ab2231SGreg Roach public function getEmptyAction(ServerRequestInterface $request): ResponseInterface 376c1010edaSGreg Roach { 37757ab2231SGreg Roach $tree = $request->getAttribute('tree'); 3784ea62551SGreg Roach assert($tree instanceof Tree); 3794ea62551SGreg Roach 3805a78cd34SGreg Roach $cart = Session::get('cart', []); 381aa6f03bbSGreg Roach $cart[$tree->name()] = []; 3825a78cd34SGreg Roach Session::put('cart', $cart); 3838c2e8227SGreg Roach 384c1010edaSGreg Roach $url = route('module', [ 38526684e68SGreg Roach 'module' => $this->name(), 386c1010edaSGreg Roach 'action' => 'Show', 387d72b284aSGreg Roach 'tree' => $tree->name(), 388c1010edaSGreg Roach ]); 3895a78cd34SGreg Roach 3906ccdf4f0SGreg Roach return redirect($url); 3915a78cd34SGreg Roach } 3925a78cd34SGreg Roach 3935a78cd34SGreg Roach /** 3946ccdf4f0SGreg Roach * @param ServerRequestInterface $request 3955a78cd34SGreg Roach * 3966ccdf4f0SGreg Roach * @return ResponseInterface 3975a78cd34SGreg Roach */ 39857ab2231SGreg Roach public function postRemoveAction(ServerRequestInterface $request): ResponseInterface 399c1010edaSGreg Roach { 40057ab2231SGreg Roach $tree = $request->getAttribute('tree'); 40175964c75SGreg Roach assert($tree instanceof Tree); 4025229eadeSGreg Roach 403bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 4045a78cd34SGreg Roach 4055a78cd34SGreg Roach $cart = Session::get('cart', []); 406aa6f03bbSGreg Roach unset($cart[$tree->name()][$xref]); 4075a78cd34SGreg Roach Session::put('cart', $cart); 4085a78cd34SGreg Roach 409c1010edaSGreg Roach $url = route('module', [ 41026684e68SGreg Roach 'module' => $this->name(), 411c1010edaSGreg Roach 'action' => 'Show', 412d72b284aSGreg Roach 'tree' => $tree->name(), 413c1010edaSGreg Roach ]); 4145a78cd34SGreg Roach 4156ccdf4f0SGreg Roach return redirect($url); 4165a78cd34SGreg Roach } 4175a78cd34SGreg Roach 4185a78cd34SGreg Roach /** 41957ab2231SGreg Roach * @param ServerRequestInterface $request 4205a78cd34SGreg Roach * 4216ccdf4f0SGreg Roach * @return ResponseInterface 4225a78cd34SGreg Roach */ 42357ab2231SGreg Roach public function getShowAction(ServerRequestInterface $request): ResponseInterface 424c1010edaSGreg Roach { 42557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 42675964c75SGreg Roach assert($tree instanceof Tree); 42757ab2231SGreg Roach 4285a78cd34SGreg Roach return $this->viewResponse('modules/clippings/show', [ 4295a78cd34SGreg Roach 'records' => $this->allRecordsInCart($tree), 4305a78cd34SGreg Roach 'title' => I18N::translate('Family tree clippings cart'), 4315a78cd34SGreg Roach 'tree' => $tree, 4325a78cd34SGreg Roach ]); 4335a78cd34SGreg Roach } 4345a78cd34SGreg Roach 4355a78cd34SGreg Roach /** 4366ccdf4f0SGreg Roach * @param ServerRequestInterface $request 4375a78cd34SGreg Roach * 4386ccdf4f0SGreg Roach * @return ResponseInterface 4395a78cd34SGreg Roach */ 44057ab2231SGreg Roach public function getAddFamilyAction(ServerRequestInterface $request): ResponseInterface 441c1010edaSGreg Roach { 44257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 44375964c75SGreg Roach assert($tree instanceof Tree); 4445229eadeSGreg Roach 445bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 4465a78cd34SGreg Roach 447*a091ac74SGreg Roach $family = Factory::family()->make($xref, $tree); 4485a78cd34SGreg Roach 4495a78cd34SGreg Roach if ($family === null) { 45059f2f229SGreg Roach throw new FamilyNotFoundException(); 4515a78cd34SGreg Roach } 4525a78cd34SGreg Roach 4535a78cd34SGreg Roach $options = $this->familyOptions($family); 4545a78cd34SGreg Roach 45539ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $family->fullName()); 4565a78cd34SGreg Roach 4575a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 4585a78cd34SGreg Roach 'options' => $options, 4595a78cd34SGreg Roach 'default' => key($options), 4605a78cd34SGreg Roach 'record' => $family, 4615a78cd34SGreg Roach 'title' => $title, 4625a78cd34SGreg Roach 'tree' => $tree, 4635a78cd34SGreg Roach ]); 4645a78cd34SGreg Roach } 4655a78cd34SGreg Roach 4665a78cd34SGreg Roach /** 4675a78cd34SGreg Roach * @param Family $family 4685a78cd34SGreg Roach * 4695a78cd34SGreg Roach * @return string[] 4705a78cd34SGreg Roach */ 471c1010edaSGreg Roach private function familyOptions(Family $family): array 472c1010edaSGreg Roach { 47339ca88baSGreg Roach $name = strip_tags($family->fullName()); 4745a78cd34SGreg Roach 4755a78cd34SGreg Roach return [ 4765a78cd34SGreg Roach 'parents' => $name, 477bbb76c12SGreg Roach /* I18N: %s is a family (husband + wife) */ 478bbb76c12SGreg Roach 'members' => I18N::translate('%s and their children', $name), 479bbb76c12SGreg Roach /* I18N: %s is a family (husband + wife) */ 480bbb76c12SGreg Roach 'descendants' => I18N::translate('%s and their descendants', $name), 4815a78cd34SGreg Roach ]; 4825a78cd34SGreg Roach } 4835a78cd34SGreg Roach 4845a78cd34SGreg Roach /** 4856ccdf4f0SGreg Roach * @param ServerRequestInterface $request 4865a78cd34SGreg Roach * 4876ccdf4f0SGreg Roach * @return ResponseInterface 4885a78cd34SGreg Roach */ 48957ab2231SGreg Roach public function postAddFamilyAction(ServerRequestInterface $request): ResponseInterface 490c1010edaSGreg Roach { 49157ab2231SGreg Roach $tree = $request->getAttribute('tree'); 4924ea62551SGreg Roach assert($tree instanceof Tree); 4934ea62551SGreg Roach 494b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 495b46c87bdSGreg Roach 496b46c87bdSGreg Roach $xref = $params['xref']; 497b46c87bdSGreg Roach $option = $params['option']; 4985a78cd34SGreg Roach 499*a091ac74SGreg Roach $family = Factory::family()->make($xref, $tree); 5005a78cd34SGreg Roach 5015a78cd34SGreg Roach if ($family === null) { 50259f2f229SGreg Roach throw new FamilyNotFoundException(); 5035a78cd34SGreg Roach } 5045a78cd34SGreg Roach 5055a78cd34SGreg Roach switch ($option) { 5065a78cd34SGreg Roach case 'parents': 5075a78cd34SGreg Roach $this->addFamilyToCart($family); 5085a78cd34SGreg Roach break; 5095a78cd34SGreg Roach 5105a78cd34SGreg Roach case 'members': 5115a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 5125a78cd34SGreg Roach break; 5135a78cd34SGreg Roach 5145a78cd34SGreg Roach case 'descendants': 5155a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($family); 5165a78cd34SGreg Roach break; 5175a78cd34SGreg Roach } 5185a78cd34SGreg Roach 5196ccdf4f0SGreg Roach return redirect($family->url()); 5205a78cd34SGreg Roach } 5215a78cd34SGreg Roach 5225a78cd34SGreg Roach /** 5235a78cd34SGreg Roach * @param Family $family 52418d7a90dSGreg Roach * 52518d7a90dSGreg Roach * @return void 5265a78cd34SGreg Roach */ 527e364afe4SGreg Roach private function addFamilyToCart(Family $family): void 528c1010edaSGreg Roach { 5295a78cd34SGreg Roach $this->addRecordToCart($family); 5305a78cd34SGreg Roach 53139ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 5325a78cd34SGreg Roach $this->addRecordToCart($spouse); 5335a78cd34SGreg Roach } 5345a78cd34SGreg Roach } 5355a78cd34SGreg Roach 5365a78cd34SGreg Roach /** 5375a78cd34SGreg Roach * @param Family $family 53818d7a90dSGreg Roach * 53918d7a90dSGreg Roach * @return void 5405a78cd34SGreg Roach */ 541e364afe4SGreg Roach private function addFamilyAndChildrenToCart(Family $family): void 542c1010edaSGreg Roach { 5435a78cd34SGreg Roach $this->addRecordToCart($family); 5445a78cd34SGreg Roach 54539ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 5465a78cd34SGreg Roach $this->addRecordToCart($spouse); 5475a78cd34SGreg Roach } 54839ca88baSGreg Roach foreach ($family->children() as $child) { 5495a78cd34SGreg Roach $this->addRecordToCart($child); 5505a78cd34SGreg Roach } 5515a78cd34SGreg Roach } 5525a78cd34SGreg Roach 5535a78cd34SGreg Roach /** 5545a78cd34SGreg Roach * @param Family $family 55518d7a90dSGreg Roach * 55618d7a90dSGreg Roach * @return void 5575a78cd34SGreg Roach */ 558e364afe4SGreg Roach private function addFamilyAndDescendantsToCart(Family $family): void 559c1010edaSGreg Roach { 5605a78cd34SGreg Roach $this->addRecordToCart($family); 5615a78cd34SGreg Roach 56239ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 5635a78cd34SGreg Roach $this->addRecordToCart($spouse); 5645a78cd34SGreg Roach } 56539ca88baSGreg Roach foreach ($family->children() as $child) { 5665a78cd34SGreg Roach $this->addRecordToCart($child); 56739ca88baSGreg Roach foreach ($child->spouseFamilies() as $child_family) { 5685a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($child_family); 5695a78cd34SGreg Roach } 5705a78cd34SGreg Roach } 5715a78cd34SGreg Roach } 5725a78cd34SGreg Roach 5735a78cd34SGreg Roach /** 5746ccdf4f0SGreg Roach * @param ServerRequestInterface $request 5755a78cd34SGreg Roach * 5766ccdf4f0SGreg Roach * @return ResponseInterface 5775a78cd34SGreg Roach */ 57857ab2231SGreg Roach public function getAddIndividualAction(ServerRequestInterface $request): ResponseInterface 579c1010edaSGreg Roach { 58057ab2231SGreg Roach $tree = $request->getAttribute('tree'); 58175964c75SGreg Roach assert($tree instanceof Tree); 5825229eadeSGreg Roach 583bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 5845a78cd34SGreg Roach 585*a091ac74SGreg Roach $individual = Factory::individual()->make($xref, $tree); 5865a78cd34SGreg Roach 5875a78cd34SGreg Roach if ($individual === null) { 58859f2f229SGreg Roach throw new IndividualNotFoundException(); 5895a78cd34SGreg Roach } 5905a78cd34SGreg Roach 5915a78cd34SGreg Roach $options = $this->individualOptions($individual); 5925a78cd34SGreg Roach 59339ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $individual->fullName()); 5945a78cd34SGreg Roach 5955a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 5965a78cd34SGreg Roach 'options' => $options, 5975a78cd34SGreg Roach 'default' => key($options), 5985a78cd34SGreg Roach 'record' => $individual, 5995a78cd34SGreg Roach 'title' => $title, 6005a78cd34SGreg Roach 'tree' => $tree, 6015a78cd34SGreg Roach ]); 6025a78cd34SGreg Roach } 6035a78cd34SGreg Roach 6045a78cd34SGreg Roach /** 6055a78cd34SGreg Roach * @param Individual $individual 6065a78cd34SGreg Roach * 6075a78cd34SGreg Roach * @return string[] 6085a78cd34SGreg Roach */ 609c1010edaSGreg Roach private function individualOptions(Individual $individual): array 610c1010edaSGreg Roach { 61139ca88baSGreg Roach $name = strip_tags($individual->fullName()); 6125a78cd34SGreg Roach 61339ca88baSGreg Roach if ($individual->sex() === 'F') { 6145a78cd34SGreg Roach return [ 6155a78cd34SGreg Roach 'self' => $name, 6165a78cd34SGreg Roach 'parents' => I18N::translate('%s, her parents and siblings', $name), 6175a78cd34SGreg Roach 'spouses' => I18N::translate('%s, her spouses and children', $name), 6185a78cd34SGreg Roach 'ancestors' => I18N::translate('%s and her ancestors', $name), 6195a78cd34SGreg Roach 'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name), 6205a78cd34SGreg Roach 'descendants' => I18N::translate('%s, her spouses and descendants', $name), 6215a78cd34SGreg Roach ]; 622b2ce94c6SRico Sonntag } 623b2ce94c6SRico Sonntag 6245a78cd34SGreg Roach return [ 6255a78cd34SGreg Roach 'self' => $name, 6265a78cd34SGreg Roach 'parents' => I18N::translate('%s, his parents and siblings', $name), 6275a78cd34SGreg Roach 'spouses' => I18N::translate('%s, his spouses and children', $name), 6285a78cd34SGreg Roach 'ancestors' => I18N::translate('%s and his ancestors', $name), 6295a78cd34SGreg Roach 'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name), 6305a78cd34SGreg Roach 'descendants' => I18N::translate('%s, his spouses and descendants', $name), 6315a78cd34SGreg Roach ]; 6325a78cd34SGreg Roach } 6335a78cd34SGreg Roach 6345a78cd34SGreg Roach /** 6356ccdf4f0SGreg Roach * @param ServerRequestInterface $request 6365a78cd34SGreg Roach * 6376ccdf4f0SGreg Roach * @return ResponseInterface 6385a78cd34SGreg Roach */ 63957ab2231SGreg Roach public function postAddIndividualAction(ServerRequestInterface $request): ResponseInterface 640c1010edaSGreg Roach { 64157ab2231SGreg Roach $tree = $request->getAttribute('tree'); 6424ea62551SGreg Roach assert($tree instanceof Tree); 6434ea62551SGreg Roach 644b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 645b46c87bdSGreg Roach 646b46c87bdSGreg Roach $xref = $params['xref']; 647b46c87bdSGreg Roach $option = $params['option']; 6485a78cd34SGreg Roach 649*a091ac74SGreg Roach $individual = Factory::individual()->make($xref, $tree); 6505a78cd34SGreg Roach 6515a78cd34SGreg Roach if ($individual === null) { 65259f2f229SGreg Roach throw new IndividualNotFoundException(); 6535a78cd34SGreg Roach } 6545a78cd34SGreg Roach 6555a78cd34SGreg Roach switch ($option) { 6565a78cd34SGreg Roach case 'self': 6575a78cd34SGreg Roach $this->addRecordToCart($individual); 6585a78cd34SGreg Roach break; 6595a78cd34SGreg Roach 6605a78cd34SGreg Roach case 'parents': 66139ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 6625a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 6635a78cd34SGreg Roach } 6645a78cd34SGreg Roach break; 6655a78cd34SGreg Roach 6665a78cd34SGreg Roach case 'spouses': 66739ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 6685a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 6695a78cd34SGreg Roach } 6705a78cd34SGreg Roach break; 6715a78cd34SGreg Roach 6725a78cd34SGreg Roach case 'ancestors': 6735a78cd34SGreg Roach $this->addAncestorsToCart($individual); 6745a78cd34SGreg Roach break; 6755a78cd34SGreg Roach 6765a78cd34SGreg Roach case 'ancestor_families': 6775a78cd34SGreg Roach $this->addAncestorFamiliesToCart($individual); 6785a78cd34SGreg Roach break; 6795a78cd34SGreg Roach 6805a78cd34SGreg Roach case 'descendants': 68139ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 6825a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($family); 6835a78cd34SGreg Roach } 6845a78cd34SGreg Roach break; 6855a78cd34SGreg Roach } 6865a78cd34SGreg Roach 6876ccdf4f0SGreg Roach return redirect($individual->url()); 6885a78cd34SGreg Roach } 6895a78cd34SGreg Roach 6905a78cd34SGreg Roach /** 6915a78cd34SGreg Roach * @param Individual $individual 69218d7a90dSGreg Roach * 69318d7a90dSGreg Roach * @return void 6945a78cd34SGreg Roach */ 695e364afe4SGreg Roach private function addAncestorsToCart(Individual $individual): void 696c1010edaSGreg Roach { 6975a78cd34SGreg Roach $this->addRecordToCart($individual); 6985a78cd34SGreg Roach 69939ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 7008df4c68dSGreg Roach $this->addRecordToCart($family); 7018df4c68dSGreg Roach 70239ca88baSGreg Roach foreach ($family->spouses() as $parent) { 7035a78cd34SGreg Roach $this->addAncestorsToCart($parent); 7045a78cd34SGreg Roach } 7055a78cd34SGreg Roach } 7065a78cd34SGreg Roach } 7075a78cd34SGreg Roach 7085a78cd34SGreg Roach /** 7095a78cd34SGreg Roach * @param Individual $individual 71018d7a90dSGreg Roach * 71118d7a90dSGreg Roach * @return void 7125a78cd34SGreg Roach */ 713e364afe4SGreg Roach private function addAncestorFamiliesToCart(Individual $individual): void 714c1010edaSGreg Roach { 71539ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 7165a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 7178df4c68dSGreg Roach 71839ca88baSGreg Roach foreach ($family->spouses() as $parent) { 719cad6d3f3SGreg Roach $this->addAncestorFamiliesToCart($parent); 7205a78cd34SGreg Roach } 7215a78cd34SGreg Roach } 7225a78cd34SGreg Roach } 7235a78cd34SGreg Roach 7245a78cd34SGreg Roach /** 7256ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7265a78cd34SGreg Roach * 7276ccdf4f0SGreg Roach * @return ResponseInterface 7285a78cd34SGreg Roach */ 72957ab2231SGreg Roach public function getAddMediaAction(ServerRequestInterface $request): ResponseInterface 730c1010edaSGreg Roach { 73157ab2231SGreg Roach $tree = $request->getAttribute('tree'); 73275964c75SGreg Roach assert($tree instanceof Tree); 7335229eadeSGreg Roach 734bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 7355a78cd34SGreg Roach 736*a091ac74SGreg Roach $media = Factory::media()->make($xref, $tree); 7375a78cd34SGreg Roach 7385a78cd34SGreg Roach if ($media === null) { 73959f2f229SGreg Roach throw new MediaNotFoundException(); 7405a78cd34SGreg Roach } 7415a78cd34SGreg Roach 7425a78cd34SGreg Roach $options = $this->mediaOptions($media); 7435a78cd34SGreg Roach 74439ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $media->fullName()); 7455a78cd34SGreg Roach 7465a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 7475a78cd34SGreg Roach 'options' => $options, 7485a78cd34SGreg Roach 'default' => key($options), 7495a78cd34SGreg Roach 'record' => $media, 7505a78cd34SGreg Roach 'title' => $title, 7515a78cd34SGreg Roach 'tree' => $tree, 7525a78cd34SGreg Roach ]); 7535a78cd34SGreg Roach } 7545a78cd34SGreg Roach 7555a78cd34SGreg Roach /** 7565a78cd34SGreg Roach * @param Media $media 7575a78cd34SGreg Roach * 7585a78cd34SGreg Roach * @return string[] 7595a78cd34SGreg Roach */ 760c1010edaSGreg Roach private function mediaOptions(Media $media): array 761c1010edaSGreg Roach { 76239ca88baSGreg Roach $name = strip_tags($media->fullName()); 7635a78cd34SGreg Roach 7645a78cd34SGreg Roach return [ 7655a78cd34SGreg Roach 'self' => $name, 7665a78cd34SGreg Roach ]; 7675a78cd34SGreg Roach } 7685a78cd34SGreg Roach 7695a78cd34SGreg Roach /** 7706ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7715a78cd34SGreg Roach * 7726ccdf4f0SGreg Roach * @return ResponseInterface 7735a78cd34SGreg Roach */ 77457ab2231SGreg Roach public function postAddMediaAction(ServerRequestInterface $request): ResponseInterface 775c1010edaSGreg Roach { 77657ab2231SGreg Roach $tree = $request->getAttribute('tree'); 77775964c75SGreg Roach assert($tree instanceof Tree); 7785229eadeSGreg Roach 779bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 7805a78cd34SGreg Roach 781*a091ac74SGreg Roach $media = Factory::media()->make($xref, $tree); 7825a78cd34SGreg Roach 7835a78cd34SGreg Roach if ($media === null) { 78459f2f229SGreg Roach throw new MediaNotFoundException(); 7855a78cd34SGreg Roach } 7865a78cd34SGreg Roach 7875a78cd34SGreg Roach $this->addRecordToCart($media); 7885a78cd34SGreg Roach 7896ccdf4f0SGreg Roach return redirect($media->url()); 7905a78cd34SGreg Roach } 7915a78cd34SGreg Roach 7925a78cd34SGreg Roach /** 7936ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7945a78cd34SGreg Roach * 7956ccdf4f0SGreg Roach * @return ResponseInterface 7965a78cd34SGreg Roach */ 79757ab2231SGreg Roach public function getAddNoteAction(ServerRequestInterface $request): ResponseInterface 798c1010edaSGreg Roach { 79957ab2231SGreg Roach $tree = $request->getAttribute('tree'); 80075964c75SGreg Roach assert($tree instanceof Tree); 8015229eadeSGreg Roach 802bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8035a78cd34SGreg Roach 804*a091ac74SGreg Roach $note = Factory::note()->make($xref, $tree); 8055a78cd34SGreg Roach 8065a78cd34SGreg Roach if ($note === null) { 80759f2f229SGreg Roach throw new NoteNotFoundException(); 8085a78cd34SGreg Roach } 8095a78cd34SGreg Roach 8105a78cd34SGreg Roach $options = $this->noteOptions($note); 8115a78cd34SGreg Roach 81239ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $note->fullName()); 8135a78cd34SGreg Roach 8145a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 8155a78cd34SGreg Roach 'options' => $options, 8165a78cd34SGreg Roach 'default' => key($options), 8175a78cd34SGreg Roach 'record' => $note, 8185a78cd34SGreg Roach 'title' => $title, 8195a78cd34SGreg Roach 'tree' => $tree, 8205a78cd34SGreg Roach ]); 8215a78cd34SGreg Roach } 8225a78cd34SGreg Roach 8235a78cd34SGreg Roach /** 8245a78cd34SGreg Roach * @param Note $note 8255a78cd34SGreg Roach * 8265a78cd34SGreg Roach * @return string[] 8275a78cd34SGreg Roach */ 828c1010edaSGreg Roach private function noteOptions(Note $note): array 829c1010edaSGreg Roach { 83039ca88baSGreg Roach $name = strip_tags($note->fullName()); 8315a78cd34SGreg Roach 8325a78cd34SGreg Roach return [ 8335a78cd34SGreg Roach 'self' => $name, 8345a78cd34SGreg Roach ]; 8355a78cd34SGreg Roach } 8365a78cd34SGreg Roach 8375a78cd34SGreg Roach /** 8386ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8395a78cd34SGreg Roach * 8406ccdf4f0SGreg Roach * @return ResponseInterface 8415a78cd34SGreg Roach */ 84257ab2231SGreg Roach public function postAddNoteAction(ServerRequestInterface $request): ResponseInterface 843c1010edaSGreg Roach { 84457ab2231SGreg Roach $tree = $request->getAttribute('tree'); 84575964c75SGreg Roach assert($tree instanceof Tree); 8465229eadeSGreg Roach 847bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8485a78cd34SGreg Roach 849*a091ac74SGreg Roach $note = Factory::note()->make($xref, $tree); 8505a78cd34SGreg Roach 8515a78cd34SGreg Roach if ($note === null) { 85259f2f229SGreg Roach throw new NoteNotFoundException(); 8535a78cd34SGreg Roach } 8545a78cd34SGreg Roach 8555a78cd34SGreg Roach $this->addRecordToCart($note); 8565a78cd34SGreg Roach 8576ccdf4f0SGreg Roach return redirect($note->url()); 8585a78cd34SGreg Roach } 8595a78cd34SGreg Roach 8605a78cd34SGreg Roach /** 8616ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8625a78cd34SGreg Roach * 8636ccdf4f0SGreg Roach * @return ResponseInterface 8645a78cd34SGreg Roach */ 86557ab2231SGreg Roach public function getAddRepositoryAction(ServerRequestInterface $request): ResponseInterface 866c1010edaSGreg Roach { 86757ab2231SGreg Roach $tree = $request->getAttribute('tree'); 86875964c75SGreg Roach assert($tree instanceof Tree); 8695229eadeSGreg Roach 870bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8715a78cd34SGreg Roach 872*a091ac74SGreg Roach $repository = Factory::repository()->make($xref, $tree); 8735a78cd34SGreg Roach 8745a78cd34SGreg Roach if ($repository === null) { 87559f2f229SGreg Roach throw new RepositoryNotFoundException(); 8765a78cd34SGreg Roach } 8775a78cd34SGreg Roach 8785a78cd34SGreg Roach $options = $this->repositoryOptions($repository); 8795a78cd34SGreg Roach 88039ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $repository->fullName()); 8815a78cd34SGreg Roach 8825a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 8835a78cd34SGreg Roach 'options' => $options, 8845a78cd34SGreg Roach 'default' => key($options), 8855a78cd34SGreg Roach 'record' => $repository, 8865a78cd34SGreg Roach 'title' => $title, 8875a78cd34SGreg Roach 'tree' => $tree, 8885a78cd34SGreg Roach ]); 8895a78cd34SGreg Roach } 8905a78cd34SGreg Roach 8915a78cd34SGreg Roach /** 8925a78cd34SGreg Roach * @param Repository $repository 8935a78cd34SGreg Roach * 8945a78cd34SGreg Roach * @return string[] 8955a78cd34SGreg Roach */ 896c1010edaSGreg Roach private function repositoryOptions(Repository $repository): array 897c1010edaSGreg Roach { 89839ca88baSGreg Roach $name = strip_tags($repository->fullName()); 8995a78cd34SGreg Roach 9005a78cd34SGreg Roach return [ 9015a78cd34SGreg Roach 'self' => $name, 9025a78cd34SGreg Roach ]; 9035a78cd34SGreg Roach } 9045a78cd34SGreg Roach 9055a78cd34SGreg Roach /** 9066ccdf4f0SGreg Roach * @param ServerRequestInterface $request 9075a78cd34SGreg Roach * 9086ccdf4f0SGreg Roach * @return ResponseInterface 9095a78cd34SGreg Roach */ 91057ab2231SGreg Roach public function postAddRepositoryAction(ServerRequestInterface $request): ResponseInterface 911c1010edaSGreg Roach { 91257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 91375964c75SGreg Roach assert($tree instanceof Tree); 9145229eadeSGreg Roach 915bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 9165a78cd34SGreg Roach 917*a091ac74SGreg Roach $repository = Factory::repository()->make($xref, $tree); 9185a78cd34SGreg Roach 9195a78cd34SGreg Roach if ($repository === null) { 92059f2f229SGreg Roach throw new RepositoryNotFoundException(); 9215a78cd34SGreg Roach } 9225a78cd34SGreg Roach 9235a78cd34SGreg Roach $this->addRecordToCart($repository); 9245a78cd34SGreg Roach 9256ccdf4f0SGreg Roach return redirect($repository->url()); 9265a78cd34SGreg Roach } 9275a78cd34SGreg Roach 9285a78cd34SGreg Roach /** 9296ccdf4f0SGreg Roach * @param ServerRequestInterface $request 9305a78cd34SGreg Roach * 9316ccdf4f0SGreg Roach * @return ResponseInterface 9325a78cd34SGreg Roach */ 93357ab2231SGreg Roach public function getAddSourceAction(ServerRequestInterface $request): ResponseInterface 934c1010edaSGreg Roach { 93557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 93675964c75SGreg Roach assert($tree instanceof Tree); 9375229eadeSGreg Roach 938bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 9395a78cd34SGreg Roach 940*a091ac74SGreg Roach $source = Factory::source()->make($xref, $tree); 9415a78cd34SGreg Roach 9425a78cd34SGreg Roach if ($source === null) { 94359f2f229SGreg Roach throw new SourceNotFoundException(); 9445a78cd34SGreg Roach } 9455a78cd34SGreg Roach 9465a78cd34SGreg Roach $options = $this->sourceOptions($source); 9475a78cd34SGreg Roach 94839ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $source->fullName()); 9495a78cd34SGreg Roach 9505a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 9515a78cd34SGreg Roach 'options' => $options, 9525a78cd34SGreg Roach 'default' => key($options), 9535a78cd34SGreg Roach 'record' => $source, 9545a78cd34SGreg Roach 'title' => $title, 9555a78cd34SGreg Roach 'tree' => $tree, 9565a78cd34SGreg Roach ]); 9575a78cd34SGreg Roach } 9585a78cd34SGreg Roach 9595a78cd34SGreg Roach /** 9605a78cd34SGreg Roach * @param Source $source 9615a78cd34SGreg Roach * 9625a78cd34SGreg Roach * @return string[] 9635a78cd34SGreg Roach */ 964c1010edaSGreg Roach private function sourceOptions(Source $source): array 965c1010edaSGreg Roach { 96639ca88baSGreg Roach $name = strip_tags($source->fullName()); 9675a78cd34SGreg Roach 9685a78cd34SGreg Roach return [ 96939ca88baSGreg Roach 'only' => strip_tags($source->fullName()), 9705a78cd34SGreg Roach 'linked' => I18N::translate('%s and the individuals that reference it.', $name), 9715a78cd34SGreg Roach ]; 9725a78cd34SGreg Roach } 9735a78cd34SGreg Roach 9745a78cd34SGreg Roach /** 9756ccdf4f0SGreg Roach * @param ServerRequestInterface $request 9765a78cd34SGreg Roach * 9776ccdf4f0SGreg Roach * @return ResponseInterface 9785a78cd34SGreg Roach */ 97957ab2231SGreg Roach public function postAddSourceAction(ServerRequestInterface $request): ResponseInterface 980c1010edaSGreg Roach { 98157ab2231SGreg Roach $tree = $request->getAttribute('tree'); 98275964c75SGreg Roach assert($tree instanceof Tree); 9835229eadeSGreg Roach 984b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 985b46c87bdSGreg Roach 986b46c87bdSGreg Roach $xref = $params['xref']; 987b46c87bdSGreg Roach $option = $params['option']; 9885a78cd34SGreg Roach 989*a091ac74SGreg Roach $source = Factory::source()->make($xref, $tree); 9905a78cd34SGreg Roach 9915a78cd34SGreg Roach if ($source === null) { 99259f2f229SGreg Roach throw new SourceNotFoundException(); 9935a78cd34SGreg Roach } 9945a78cd34SGreg Roach 9955a78cd34SGreg Roach $this->addRecordToCart($source); 9965a78cd34SGreg Roach 9975a78cd34SGreg Roach if ($option === 'linked') { 9985a78cd34SGreg Roach foreach ($source->linkedIndividuals('SOUR') as $individual) { 9995a78cd34SGreg Roach $this->addRecordToCart($individual); 10005a78cd34SGreg Roach } 10015a78cd34SGreg Roach foreach ($source->linkedFamilies('SOUR') as $family) { 10025a78cd34SGreg Roach $this->addRecordToCart($family); 10035a78cd34SGreg Roach } 10045a78cd34SGreg Roach } 10055a78cd34SGreg Roach 10066ccdf4f0SGreg Roach return redirect($source->url()); 10075a78cd34SGreg Roach } 10085a78cd34SGreg Roach 10095a78cd34SGreg Roach /** 10105a78cd34SGreg Roach * Get all the records in the cart. 10115a78cd34SGreg Roach * 10125a78cd34SGreg Roach * @param Tree $tree 10135a78cd34SGreg Roach * 10145a78cd34SGreg Roach * @return GedcomRecord[] 10155a78cd34SGreg Roach */ 1016c1010edaSGreg Roach private function allRecordsInCart(Tree $tree): array 1017c1010edaSGreg Roach { 10185a78cd34SGreg Roach $cart = Session::get('cart', []); 10195a78cd34SGreg Roach 1020aa6f03bbSGreg Roach $xrefs = array_keys($cart[$tree->name()] ?? []); 1021c8846facSGreg Roach $xrefs = array_map('strval', $xrefs); // PHP converts numeric keys to integers. 10225a78cd34SGreg Roach 10235a78cd34SGreg Roach // Fetch all the records in the cart. 1024bed27cedSGreg Roach $records = array_map(static function (string $xref) use ($tree): ?GedcomRecord { 1025*a091ac74SGreg Roach return Factory::gedcomRecord()->make($xref, $tree); 10265a78cd34SGreg Roach }, $xrefs); 10275a78cd34SGreg Roach 10285a78cd34SGreg Roach // Some records may have been deleted after they were added to the cart. 10295a78cd34SGreg Roach $records = array_filter($records); 10305a78cd34SGreg Roach 10315a78cd34SGreg Roach // Group and sort. 10320b5fd0a6SGreg Roach uasort($records, static function (GedcomRecord $x, GedcomRecord $y): int { 1033c156e8f5SGreg Roach return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::nameComparator()($x, $y); 10345a78cd34SGreg Roach }); 10355a78cd34SGreg Roach 10365a78cd34SGreg Roach return $records; 10375a78cd34SGreg Roach } 10385a78cd34SGreg Roach 10395a78cd34SGreg Roach /** 10405a78cd34SGreg Roach * Add a record (and direclty linked sources, notes, etc. to the cart. 10415a78cd34SGreg Roach * 10425a78cd34SGreg Roach * @param GedcomRecord $record 104318d7a90dSGreg Roach * 104418d7a90dSGreg Roach * @return void 10455a78cd34SGreg Roach */ 1046e364afe4SGreg Roach private function addRecordToCart(GedcomRecord $record): void 1047c1010edaSGreg Roach { 10485a78cd34SGreg Roach $cart = Session::get('cart', []); 10495a78cd34SGreg Roach 1050f4afa648SGreg Roach $tree_name = $record->tree()->name(); 10515a78cd34SGreg Roach 10525a78cd34SGreg Roach // Add this record 1053c0935879SGreg Roach $cart[$tree_name][$record->xref()] = true; 10545a78cd34SGreg Roach 10555a78cd34SGreg Roach // Add directly linked media, notes, repositories and sources. 10568d0ebef0SGreg Roach preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches); 10575a78cd34SGreg Roach 10585a78cd34SGreg Roach foreach ($matches[1] as $match) { 10595a78cd34SGreg Roach $cart[$tree_name][$match] = true; 10605a78cd34SGreg Roach } 10615a78cd34SGreg Roach 10625a78cd34SGreg Roach Session::put('cart', $cart); 10635a78cd34SGreg Roach } 10645a78cd34SGreg Roach 10655a78cd34SGreg Roach /** 10665a78cd34SGreg Roach * @param Tree $tree 10675a78cd34SGreg Roach * 10685a78cd34SGreg Roach * @return bool 10695a78cd34SGreg Roach */ 1070c1010edaSGreg Roach private function isCartEmpty(Tree $tree): bool 1071c1010edaSGreg Roach { 10725a78cd34SGreg Roach $cart = Session::get('cart', []); 1073a91af26aSGreg Roach $contents = $cart[$tree->name()] ?? []; 10745a78cd34SGreg Roach 1075a91af26aSGreg Roach return $contents === []; 10765a78cd34SGreg Roach } 10778c2e8227SGreg Roach} 1078