18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 5a091ac74SGreg 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; 30a091ac74SGreg 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; 47*69c05a6eSGreg Roachuse Fisharebest\Webtrees\Services\GedcomExportService; 48e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 490e62c4b8SGreg Roachuse Fisharebest\Webtrees\Session; 505a78cd34SGreg Roachuse Fisharebest\Webtrees\Source; 51aee13b6dSGreg Roachuse Fisharebest\Webtrees\Tree; 52*69c05a6eSGreg Roachuse Illuminate\Support\Collection; 535a78cd34SGreg Roachuse League\Flysystem\Filesystem; 54a04bb9a2SGreg Roachuse League\Flysystem\FilesystemInterface; 5561bf91b2SGreg Roachuse League\Flysystem\MountManager; 565a78cd34SGreg Roachuse League\Flysystem\ZipArchive\ZipArchiveAdapter; 57bed27cedSGreg Roachuse Psr\Http\Message\ResponseFactoryInterface; 586ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 596ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 606ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface; 61*69c05a6eSGreg Roachuse RuntimeException; 62*69c05a6eSGreg Roachuse stdClass; 633976b470SGreg Roach 64eb235819SGreg Roachuse function app; 65bf80ec58SGreg Roachuse function array_filter; 66bf80ec58SGreg Roachuse function array_keys; 67bf80ec58SGreg Roachuse function array_map; 685229eadeSGreg Roachuse function assert; 69*69c05a6eSGreg Roachuse function fopen; 70bf80ec58SGreg Roachuse function in_array; 71ddeb3354SGreg Roachuse function is_string; 72bf80ec58SGreg Roachuse function key; 73bf80ec58SGreg Roachuse function preg_match_all; 74bf80ec58SGreg Roachuse function redirect; 75*69c05a6eSGreg Roachuse function rewind; 76bf80ec58SGreg Roachuse function route; 77e5a6b4d4SGreg Roachuse function str_replace; 78bf80ec58SGreg Roachuse function strip_tags; 79bf80ec58SGreg Roachuse function utf8_decode; 808c2e8227SGreg Roach 818c2e8227SGreg Roach/** 828c2e8227SGreg Roach * Class ClippingsCartModule 838c2e8227SGreg Roach */ 8437eb8894SGreg Roachclass ClippingsCartModule extends AbstractModule implements ModuleMenuInterface 85c1010edaSGreg Roach{ 8649a243cbSGreg Roach use ModuleMenuTrait; 8749a243cbSGreg Roach 885a78cd34SGreg Roach // Routes that have a record which can be added to the clipboard 8916d6367aSGreg Roach private const ROUTES_WITH_RECORDS = [ 90f95e0480SGreg Roach 'Family' => FamilyPage::class, 91f95e0480SGreg Roach 'Individual' => IndividualPage::class, 92f95e0480SGreg Roach 'Media' => MediaPage::class, 93f95e0480SGreg Roach 'Note' => NotePage::class, 94f95e0480SGreg Roach 'Repository' => RepositoryPage::class, 95f95e0480SGreg Roach 'Source' => SourcePage::class, 96c1010edaSGreg Roach ]; 975a78cd34SGreg Roach 9849a243cbSGreg Roach /** @var int The default access level for this module. It can be changed in the control panel. */ 9949a243cbSGreg Roach protected $access_level = Auth::PRIV_USER; 10049a243cbSGreg Roach 101*69c05a6eSGreg Roach /** @var GedcomExportService */ 102*69c05a6eSGreg Roach private $gedcom_export_service; 103*69c05a6eSGreg Roach 104*69c05a6eSGreg Roach /** @var UserService */ 105e5a6b4d4SGreg Roach private $user_service; 106e5a6b4d4SGreg Roach 107e5a6b4d4SGreg Roach /** 108e5a6b4d4SGreg Roach * ClippingsCartModule constructor. 109e5a6b4d4SGreg Roach * 110*69c05a6eSGreg Roach * @param GedcomExportService $gedcom_export_service 111e5a6b4d4SGreg Roach * @param UserService $user_service 112e5a6b4d4SGreg Roach */ 113*69c05a6eSGreg Roach public function __construct(GedcomExportService $gedcom_export_service, UserService $user_service) 114e5a6b4d4SGreg Roach { 115*69c05a6eSGreg Roach $this->gedcom_export_service = $gedcom_export_service; 116e5a6b4d4SGreg Roach $this->user_service = $user_service; 117e5a6b4d4SGreg Roach } 118e5a6b4d4SGreg Roach 119e5a6b4d4SGreg Roach /** 1200cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 121961ec755SGreg Roach * 122961ec755SGreg Roach * @return string 123961ec755SGreg Roach */ 12449a243cbSGreg Roach public function title(): string 125c1010edaSGreg Roach { 126bbb76c12SGreg Roach /* I18N: Name of a module */ 127bbb76c12SGreg Roach return I18N::translate('Clippings cart'); 1288c2e8227SGreg Roach } 1298c2e8227SGreg Roach 130961ec755SGreg Roach /** 131961ec755SGreg Roach * A sentence describing what this module does. 132961ec755SGreg Roach * 133961ec755SGreg Roach * @return string 134961ec755SGreg Roach */ 13549a243cbSGreg Roach public function description(): string 136c1010edaSGreg Roach { 137bbb76c12SGreg Roach /* I18N: Description of the “Clippings cart” module */ 138bbb76c12SGreg Roach return I18N::translate('Select records from your family tree and save them as a GEDCOM file.'); 1398c2e8227SGreg Roach } 1408c2e8227SGreg Roach 1410ee13198SGreg Roach /** 14249a243cbSGreg Roach * The default position for this menu. It can be changed in the control panel. 1430ee13198SGreg Roach * 1440ee13198SGreg Roach * @return int 1450ee13198SGreg Roach */ 1468f53f488SRico Sonntag public function defaultMenuOrder(): int 147c1010edaSGreg Roach { 148353b36abSGreg Roach return 6; 1498c2e8227SGreg Roach } 1508c2e8227SGreg Roach 1510ee13198SGreg Roach /** 1520ee13198SGreg Roach * A menu, to be added to the main application menu. 1530ee13198SGreg Roach * 154aee13b6dSGreg Roach * @param Tree $tree 155aee13b6dSGreg Roach * 1560ee13198SGreg Roach * @return Menu|null 1570ee13198SGreg Roach */ 15846295629SGreg Roach public function getMenu(Tree $tree): ?Menu 159c1010edaSGreg Roach { 160eb235819SGreg Roach /** @var ServerRequestInterface $request */ 1616ccdf4f0SGreg Roach $request = app(ServerRequestInterface::class); 1628c2e8227SGreg Roach 163f7ab47b1SGreg Roach $route = $request->getAttribute('route'); 164de2aa325SGreg Roach assert($route instanceof Route); 1655a78cd34SGreg Roach 1665a78cd34SGreg Roach $submenus = [ 16749a243cbSGreg Roach new Menu($this->title(), route('module', [ 16826684e68SGreg Roach 'module' => $this->name(), 169c1010edaSGreg Roach 'action' => 'Show', 170d72b284aSGreg Roach 'tree' => $tree->name(), 171c1010edaSGreg Roach ]), 'menu-clippings-cart', ['rel' => 'nofollow']), 1725a78cd34SGreg Roach ]; 1735a78cd34SGreg Roach 1742b0d92b4SGreg Roach $action = array_search($route->name, self::ROUTES_WITH_RECORDS, true); 175f95e0480SGreg Roach if ($action !== false) { 1762b0d92b4SGreg Roach $xref = $route->attributes['xref']; 177ddeb3354SGreg Roach assert(is_string($xref)); 178ddeb3354SGreg Roach 179c1010edaSGreg Roach $add_route = route('module', [ 18026684e68SGreg Roach 'module' => $this->name(), 181f95e0480SGreg Roach 'action' => 'Add' . $action, 182c1010edaSGreg Roach 'xref' => $xref, 183d72b284aSGreg Roach 'tree' => $tree->name(), 184c1010edaSGreg Roach ]); 1855a78cd34SGreg Roach 18625b2dde3SGreg Roach $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']); 1878c2e8227SGreg Roach } 188cbc1590aSGreg Roach 1895a78cd34SGreg Roach if (!$this->isCartEmpty($tree)) { 190c1010edaSGreg Roach $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [ 19126684e68SGreg Roach 'module' => $this->name(), 192c1010edaSGreg Roach 'action' => 'Empty', 193d72b284aSGreg Roach 'tree' => $tree->name(), 194c1010edaSGreg Roach ]), 'menu-clippings-empty', ['rel' => 'nofollow']); 195f95e0480SGreg Roach 196c1010edaSGreg Roach $submenus[] = new Menu(I18N::translate('Download'), route('module', [ 19726684e68SGreg Roach 'module' => $this->name(), 198c1010edaSGreg Roach 'action' => 'DownloadForm', 199d72b284aSGreg Roach 'tree' => $tree->name(), 200c1010edaSGreg Roach ]), 'menu-clippings-download', ['rel' => 'nofollow']); 2015a78cd34SGreg Roach } 2025a78cd34SGreg Roach 20349a243cbSGreg Roach return new Menu($this->title(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus); 2048c2e8227SGreg Roach } 2058c2e8227SGreg Roach 20676692c8bSGreg Roach /** 2076ccdf4f0SGreg Roach * @param ServerRequestInterface $request 20876692c8bSGreg Roach * 2096ccdf4f0SGreg Roach * @return ResponseInterface 21076692c8bSGreg Roach */ 211f95e0480SGreg Roach public function postDownloadAction(ServerRequestInterface $request): ResponseInterface 212c1010edaSGreg Roach { 21357ab2231SGreg Roach $tree = $request->getAttribute('tree'); 2144ea62551SGreg Roach assert($tree instanceof Tree); 2154ea62551SGreg Roach 216a04bb9a2SGreg Roach $data_filesystem = $request->getAttribute('filesystem.data'); 217a04bb9a2SGreg Roach assert($data_filesystem instanceof FilesystemInterface); 218a04bb9a2SGreg Roach 219b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 220b46c87bdSGreg Roach 221b46c87bdSGreg Roach $privatize_export = $params['privatize_export']; 222e2ed7c79SGreg Roach 223e2ed7c79SGreg Roach if ($privatize_export === 'none' && !Auth::isManager($tree)) { 224e2ed7c79SGreg Roach $privatize_export = 'member'; 225e2ed7c79SGreg Roach } 226e2ed7c79SGreg Roach 227e2ed7c79SGreg Roach if ($privatize_export === 'gedadmin' && !Auth::isManager($tree)) { 228e2ed7c79SGreg Roach $privatize_export = 'member'; 229e2ed7c79SGreg Roach } 230e2ed7c79SGreg Roach 231e2ed7c79SGreg Roach if ($privatize_export === 'user' && !Auth::isMember($tree)) { 232e2ed7c79SGreg Roach $privatize_export = 'visitor'; 233e2ed7c79SGreg Roach } 234e2ed7c79SGreg Roach 235b46c87bdSGreg Roach $convert = (bool) ($params['convert'] ?? false); 2368c2e8227SGreg Roach 23713abd6f3SGreg Roach $cart = Session::get('cart', []); 2388c2e8227SGreg Roach 239aa6f03bbSGreg Roach $xrefs = array_keys($cart[$tree->name()] ?? []); 240c8846facSGreg Roach $xrefs = array_map('strval', $xrefs); // PHP converts numeric keys to integers. 2415a78cd34SGreg Roach 2425a78cd34SGreg Roach // Create a new/empty .ZIP file 243a00baf47SGreg Roach $temp_zip_file = stream_get_meta_data(tmpfile())['uri']; 2447f996f6eSGreg Roach $zip_adapter = new ZipArchiveAdapter($temp_zip_file); 2457f996f6eSGreg Roach $zip_filesystem = new Filesystem($zip_adapter); 2465a78cd34SGreg Roach 24761bf91b2SGreg Roach $manager = new MountManager([ 248a04bb9a2SGreg Roach 'media' => $tree->mediaFilesystem($data_filesystem), 24961bf91b2SGreg Roach 'zip' => $zip_filesystem, 25061bf91b2SGreg Roach ]); 25161bf91b2SGreg Roach 2525a78cd34SGreg Roach // Media file prefix 2535a78cd34SGreg Roach $path = $tree->getPreference('MEDIA_DIRECTORY'); 2545a78cd34SGreg Roach 255*69c05a6eSGreg Roach $encoding = $convert ? 'ANSI' : 'UTF-8'; 256*69c05a6eSGreg Roach 257*69c05a6eSGreg Roach $records = new Collection(); 2585a78cd34SGreg Roach 2595a78cd34SGreg Roach switch ($privatize_export) { 2605a78cd34SGreg Roach case 'gedadmin': 2615a78cd34SGreg Roach $access_level = Auth::PRIV_NONE; 2625a78cd34SGreg Roach break; 2635a78cd34SGreg Roach case 'user': 2645a78cd34SGreg Roach $access_level = Auth::PRIV_USER; 2655a78cd34SGreg Roach break; 2665a78cd34SGreg Roach case 'visitor': 2675a78cd34SGreg Roach $access_level = Auth::PRIV_PRIVATE; 2685a78cd34SGreg Roach break; 2695a78cd34SGreg Roach case 'none': 2705a78cd34SGreg Roach default: 2715a78cd34SGreg Roach $access_level = Auth::PRIV_HIDE; 2725a78cd34SGreg Roach break; 2735a78cd34SGreg Roach } 2745a78cd34SGreg Roach 2755a78cd34SGreg Roach foreach ($xrefs as $xref) { 276a091ac74SGreg Roach $object = Factory::gedcomRecord()->make($xref, $tree); 2775a78cd34SGreg Roach // The object may have been deleted since we added it to the cart.... 278bed27cedSGreg Roach if ($object instanceof GedcomRecord) { 2795a78cd34SGreg Roach $record = $object->privatizeGedcom($access_level); 2805a78cd34SGreg Roach // Remove links to objects that aren't in the cart 2818d0ebef0SGreg Roach preg_match_all('/\n1 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER); 2825a78cd34SGreg Roach foreach ($matches as $match) { 283bf80ec58SGreg Roach if (!in_array($match[1], $xrefs, true)) { 2845a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2855a78cd34SGreg Roach } 2865a78cd34SGreg Roach } 2878d0ebef0SGreg Roach preg_match_all('/\n2 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER); 2885a78cd34SGreg Roach foreach ($matches as $match) { 289bf80ec58SGreg Roach if (!in_array($match[1], $xrefs, true)) { 2905a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2915a78cd34SGreg Roach } 2925a78cd34SGreg Roach } 2938d0ebef0SGreg Roach preg_match_all('/\n3 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER); 2945a78cd34SGreg Roach foreach ($matches as $match) { 295bf80ec58SGreg Roach if (!in_array($match[1], $xrefs, true)) { 2965a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2975a78cd34SGreg Roach } 2985a78cd34SGreg Roach } 2995a78cd34SGreg Roach 30055167344SGreg Roach if ($object instanceof Individual || $object instanceof Family) { 301*69c05a6eSGreg Roach $records->add($record . "\n1 SOUR @WEBTREES@\n2 PAGE " . $object->url()); 30255167344SGreg Roach } elseif ($object instanceof Source) { 303*69c05a6eSGreg Roach $records->add($record . "\n1 NOTE " . $object->url()); 30455167344SGreg Roach } elseif ($object instanceof Media) { 30555167344SGreg Roach // Add the media files to the archive 3065a78cd34SGreg Roach foreach ($object->mediaFiles() as $media_file) { 30761bf91b2SGreg Roach $from = 'media://' . $media_file->filename(); 30861bf91b2SGreg Roach $to = 'zip://' . $path . $media_file->filename(); 3095de4ab51SGreg Roach if (!$media_file->isExternal() && $manager->has($from) && !$manager->has($to)) { 31061bf91b2SGreg Roach $manager->copy($from, $to); 3115a78cd34SGreg Roach } 3125a78cd34SGreg Roach } 313*69c05a6eSGreg Roach $records->add($record); 31455167344SGreg Roach } else { 315*69c05a6eSGreg Roach $records->add($record); 3168c2e8227SGreg Roach } 3178c2e8227SGreg Roach } 3188c2e8227SGreg Roach } 3198c2e8227SGreg Roach 3209b93b7c3SGreg Roach $base_url = $request->getAttribute('base_url'); 3219b93b7c3SGreg Roach 3225a78cd34SGreg Roach // Create a source, to indicate the source of the data. 323*69c05a6eSGreg Roach $record = "0 @WEBTREES@ SOUR\n1 TITL " . $base_url; 324e5a6b4d4SGreg Roach $author = $this->user_service->find((int) $tree->getPreference('CONTACT_USER_ID')); 3255a78cd34SGreg Roach if ($author !== null) { 326*69c05a6eSGreg Roach $record .= "\n1 AUTH " . $author->realName(); 3275a78cd34SGreg Roach } 328*69c05a6eSGreg Roach $records->add($record); 3295a78cd34SGreg Roach 330*69c05a6eSGreg Roach $stream = fopen('php://temp', 'wb+'); 3315a78cd34SGreg Roach 332*69c05a6eSGreg Roach if ($stream === false) { 333*69c05a6eSGreg Roach throw new RuntimeException('Failed to create temporary stream'); 3348c2e8227SGreg Roach } 335cbc1590aSGreg Roach 336*69c05a6eSGreg Roach // We have already applied privacy filtering, so do not do it again. 337*69c05a6eSGreg Roach $this->gedcom_export_service->export($tree, $stream, false, $encoding, Auth::PRIV_HIDE, $path, $records); 338*69c05a6eSGreg Roach rewind($stream); 339*69c05a6eSGreg Roach 3405a78cd34SGreg Roach // Finally add the GEDCOM file to the .ZIP file. 341*69c05a6eSGreg Roach $zip_filesystem->writeStream('clippings.ged', $stream); 3425a78cd34SGreg Roach 34361bf91b2SGreg Roach // Need to force-close ZipArchive filesystems. 3447f996f6eSGreg Roach $zip_adapter->getArchive()->close(); 3455a78cd34SGreg Roach 3466ccdf4f0SGreg Roach // Use a stream, so that we do not have to load the entire file into memory. 3476ccdf4f0SGreg Roach $stream = app(StreamFactoryInterface::class)->createStreamFromFile($temp_zip_file); 3485a78cd34SGreg Roach 349bed27cedSGreg Roach /** @var ResponseFactoryInterface $response_factory */ 350bed27cedSGreg Roach $response_factory = app(ResponseFactoryInterface::class); 351bed27cedSGreg Roach 352bed27cedSGreg Roach return $response_factory->createResponse() 3536ccdf4f0SGreg Roach ->withBody($stream) 3541b3d4731SGreg Roach ->withHeader('Content-Type', 'application/zip') 355bed27cedSGreg Roach ->withHeader('Content-Disposition', 'attachment; filename="clippings.zip'); 3568c2e8227SGreg Roach } 3578c2e8227SGreg Roach 3588c2e8227SGreg Roach /** 35957ab2231SGreg Roach * @param ServerRequestInterface $request 36076692c8bSGreg Roach * 3616ccdf4f0SGreg Roach * @return ResponseInterface 3628c2e8227SGreg Roach */ 36357ab2231SGreg Roach public function getDownloadFormAction(ServerRequestInterface $request): ResponseInterface 364c1010edaSGreg Roach { 36557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 3664ea62551SGreg Roach assert($tree instanceof Tree); 3674ea62551SGreg Roach 36857ab2231SGreg Roach $user = $request->getAttribute('user'); 3695a78cd34SGreg Roach $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download'); 3708c2e8227SGreg Roach 3715a78cd34SGreg Roach return $this->viewResponse('modules/clippings/download', [ 3725a78cd34SGreg Roach 'is_manager' => Auth::isManager($tree, $user), 3735a78cd34SGreg Roach 'is_member' => Auth::isMember($tree, $user), 37471378461SGreg Roach 'module' => $this->name(), 3755a78cd34SGreg Roach 'title' => $title, 376f95e0480SGreg Roach 'tree' => $tree, 3775a78cd34SGreg Roach ]); 3788c2e8227SGreg Roach } 3798c2e8227SGreg Roach 3805a78cd34SGreg Roach /** 38157ab2231SGreg Roach * @param ServerRequestInterface $request 3825a78cd34SGreg Roach * 3836ccdf4f0SGreg Roach * @return ResponseInterface 3845a78cd34SGreg Roach */ 38557ab2231SGreg Roach public function getEmptyAction(ServerRequestInterface $request): ResponseInterface 386c1010edaSGreg Roach { 38757ab2231SGreg Roach $tree = $request->getAttribute('tree'); 3884ea62551SGreg Roach assert($tree instanceof Tree); 3894ea62551SGreg Roach 3905a78cd34SGreg Roach $cart = Session::get('cart', []); 391aa6f03bbSGreg Roach $cart[$tree->name()] = []; 3925a78cd34SGreg Roach Session::put('cart', $cart); 3938c2e8227SGreg Roach 394c1010edaSGreg Roach $url = route('module', [ 39526684e68SGreg Roach 'module' => $this->name(), 396c1010edaSGreg Roach 'action' => 'Show', 397d72b284aSGreg Roach 'tree' => $tree->name(), 398c1010edaSGreg Roach ]); 3995a78cd34SGreg Roach 4006ccdf4f0SGreg Roach return redirect($url); 4015a78cd34SGreg Roach } 4025a78cd34SGreg Roach 4035a78cd34SGreg Roach /** 4046ccdf4f0SGreg Roach * @param ServerRequestInterface $request 4055a78cd34SGreg Roach * 4066ccdf4f0SGreg Roach * @return ResponseInterface 4075a78cd34SGreg Roach */ 40857ab2231SGreg Roach public function postRemoveAction(ServerRequestInterface $request): ResponseInterface 409c1010edaSGreg Roach { 41057ab2231SGreg Roach $tree = $request->getAttribute('tree'); 41175964c75SGreg Roach assert($tree instanceof Tree); 4125229eadeSGreg Roach 413bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 4145a78cd34SGreg Roach 4155a78cd34SGreg Roach $cart = Session::get('cart', []); 416aa6f03bbSGreg Roach unset($cart[$tree->name()][$xref]); 4175a78cd34SGreg Roach Session::put('cart', $cart); 4185a78cd34SGreg Roach 419c1010edaSGreg Roach $url = route('module', [ 42026684e68SGreg Roach 'module' => $this->name(), 421c1010edaSGreg Roach 'action' => 'Show', 422d72b284aSGreg Roach 'tree' => $tree->name(), 423c1010edaSGreg Roach ]); 4245a78cd34SGreg Roach 4256ccdf4f0SGreg Roach return redirect($url); 4265a78cd34SGreg Roach } 4275a78cd34SGreg Roach 4285a78cd34SGreg Roach /** 42957ab2231SGreg Roach * @param ServerRequestInterface $request 4305a78cd34SGreg Roach * 4316ccdf4f0SGreg Roach * @return ResponseInterface 4325a78cd34SGreg Roach */ 43357ab2231SGreg Roach public function getShowAction(ServerRequestInterface $request): ResponseInterface 434c1010edaSGreg Roach { 43557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 43675964c75SGreg Roach assert($tree instanceof Tree); 43757ab2231SGreg Roach 4385a78cd34SGreg Roach return $this->viewResponse('modules/clippings/show', [ 4395a78cd34SGreg Roach 'records' => $this->allRecordsInCart($tree), 4405a78cd34SGreg Roach 'title' => I18N::translate('Family tree clippings cart'), 4415a78cd34SGreg Roach 'tree' => $tree, 4425a78cd34SGreg Roach ]); 4435a78cd34SGreg Roach } 4445a78cd34SGreg Roach 4455a78cd34SGreg Roach /** 4466ccdf4f0SGreg Roach * @param ServerRequestInterface $request 4475a78cd34SGreg Roach * 4486ccdf4f0SGreg Roach * @return ResponseInterface 4495a78cd34SGreg Roach */ 45057ab2231SGreg Roach public function getAddFamilyAction(ServerRequestInterface $request): ResponseInterface 451c1010edaSGreg Roach { 45257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 45375964c75SGreg Roach assert($tree instanceof Tree); 4545229eadeSGreg Roach 455bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 4565a78cd34SGreg Roach 457a091ac74SGreg Roach $family = Factory::family()->make($xref, $tree); 4585a78cd34SGreg Roach 4595a78cd34SGreg Roach if ($family === null) { 46059f2f229SGreg Roach throw new FamilyNotFoundException(); 4615a78cd34SGreg Roach } 4625a78cd34SGreg Roach 4635a78cd34SGreg Roach $options = $this->familyOptions($family); 4645a78cd34SGreg Roach 46539ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $family->fullName()); 4665a78cd34SGreg Roach 4675a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 4685a78cd34SGreg Roach 'options' => $options, 4695a78cd34SGreg Roach 'default' => key($options), 4705a78cd34SGreg Roach 'record' => $family, 4715a78cd34SGreg Roach 'title' => $title, 4725a78cd34SGreg Roach 'tree' => $tree, 4735a78cd34SGreg Roach ]); 4745a78cd34SGreg Roach } 4755a78cd34SGreg Roach 4765a78cd34SGreg Roach /** 4775a78cd34SGreg Roach * @param Family $family 4785a78cd34SGreg Roach * 4795a78cd34SGreg Roach * @return string[] 4805a78cd34SGreg Roach */ 481c1010edaSGreg Roach private function familyOptions(Family $family): array 482c1010edaSGreg Roach { 48339ca88baSGreg Roach $name = strip_tags($family->fullName()); 4845a78cd34SGreg Roach 4855a78cd34SGreg Roach return [ 4865a78cd34SGreg Roach 'parents' => $name, 487bbb76c12SGreg Roach /* I18N: %s is a family (husband + wife) */ 488bbb76c12SGreg Roach 'members' => I18N::translate('%s and their children', $name), 489bbb76c12SGreg Roach /* I18N: %s is a family (husband + wife) */ 490bbb76c12SGreg Roach 'descendants' => I18N::translate('%s and their descendants', $name), 4915a78cd34SGreg Roach ]; 4925a78cd34SGreg Roach } 4935a78cd34SGreg Roach 4945a78cd34SGreg Roach /** 4956ccdf4f0SGreg Roach * @param ServerRequestInterface $request 4965a78cd34SGreg Roach * 4976ccdf4f0SGreg Roach * @return ResponseInterface 4985a78cd34SGreg Roach */ 49957ab2231SGreg Roach public function postAddFamilyAction(ServerRequestInterface $request): ResponseInterface 500c1010edaSGreg Roach { 50157ab2231SGreg Roach $tree = $request->getAttribute('tree'); 5024ea62551SGreg Roach assert($tree instanceof Tree); 5034ea62551SGreg Roach 504b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 505b46c87bdSGreg Roach 506b46c87bdSGreg Roach $xref = $params['xref']; 507b46c87bdSGreg Roach $option = $params['option']; 5085a78cd34SGreg Roach 509a091ac74SGreg Roach $family = Factory::family()->make($xref, $tree); 5105a78cd34SGreg Roach 5115a78cd34SGreg Roach if ($family === null) { 51259f2f229SGreg Roach throw new FamilyNotFoundException(); 5135a78cd34SGreg Roach } 5145a78cd34SGreg Roach 5155a78cd34SGreg Roach switch ($option) { 5165a78cd34SGreg Roach case 'parents': 5175a78cd34SGreg Roach $this->addFamilyToCart($family); 5185a78cd34SGreg Roach break; 5195a78cd34SGreg Roach 5205a78cd34SGreg Roach case 'members': 5215a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 5225a78cd34SGreg Roach break; 5235a78cd34SGreg Roach 5245a78cd34SGreg Roach case 'descendants': 5255a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($family); 5265a78cd34SGreg Roach break; 5275a78cd34SGreg Roach } 5285a78cd34SGreg Roach 5296ccdf4f0SGreg Roach return redirect($family->url()); 5305a78cd34SGreg Roach } 5315a78cd34SGreg Roach 5325a78cd34SGreg Roach /** 5335a78cd34SGreg Roach * @param Family $family 53418d7a90dSGreg Roach * 53518d7a90dSGreg Roach * @return void 5365a78cd34SGreg Roach */ 537e364afe4SGreg Roach private function addFamilyToCart(Family $family): void 538c1010edaSGreg Roach { 5395a78cd34SGreg Roach $this->addRecordToCart($family); 5405a78cd34SGreg Roach 54139ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 5425a78cd34SGreg Roach $this->addRecordToCart($spouse); 5435a78cd34SGreg Roach } 5445a78cd34SGreg Roach } 5455a78cd34SGreg Roach 5465a78cd34SGreg Roach /** 5475a78cd34SGreg Roach * @param Family $family 54818d7a90dSGreg Roach * 54918d7a90dSGreg Roach * @return void 5505a78cd34SGreg Roach */ 551e364afe4SGreg Roach private function addFamilyAndChildrenToCart(Family $family): void 552c1010edaSGreg Roach { 5535a78cd34SGreg Roach $this->addRecordToCart($family); 5545a78cd34SGreg Roach 55539ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 5565a78cd34SGreg Roach $this->addRecordToCart($spouse); 5575a78cd34SGreg Roach } 55839ca88baSGreg Roach foreach ($family->children() as $child) { 5595a78cd34SGreg Roach $this->addRecordToCart($child); 5605a78cd34SGreg Roach } 5615a78cd34SGreg Roach } 5625a78cd34SGreg Roach 5635a78cd34SGreg Roach /** 5645a78cd34SGreg Roach * @param Family $family 56518d7a90dSGreg Roach * 56618d7a90dSGreg Roach * @return void 5675a78cd34SGreg Roach */ 568e364afe4SGreg Roach private function addFamilyAndDescendantsToCart(Family $family): void 569c1010edaSGreg Roach { 5705a78cd34SGreg Roach $this->addRecordToCart($family); 5715a78cd34SGreg Roach 57239ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 5735a78cd34SGreg Roach $this->addRecordToCart($spouse); 5745a78cd34SGreg Roach } 57539ca88baSGreg Roach foreach ($family->children() as $child) { 5765a78cd34SGreg Roach $this->addRecordToCart($child); 57739ca88baSGreg Roach foreach ($child->spouseFamilies() as $child_family) { 5785a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($child_family); 5795a78cd34SGreg Roach } 5805a78cd34SGreg Roach } 5815a78cd34SGreg Roach } 5825a78cd34SGreg Roach 5835a78cd34SGreg Roach /** 5846ccdf4f0SGreg Roach * @param ServerRequestInterface $request 5855a78cd34SGreg Roach * 5866ccdf4f0SGreg Roach * @return ResponseInterface 5875a78cd34SGreg Roach */ 58857ab2231SGreg Roach public function getAddIndividualAction(ServerRequestInterface $request): ResponseInterface 589c1010edaSGreg Roach { 59057ab2231SGreg Roach $tree = $request->getAttribute('tree'); 59175964c75SGreg Roach assert($tree instanceof Tree); 5925229eadeSGreg Roach 593bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 5945a78cd34SGreg Roach 595a091ac74SGreg Roach $individual = Factory::individual()->make($xref, $tree); 5965a78cd34SGreg Roach 5975a78cd34SGreg Roach if ($individual === null) { 59859f2f229SGreg Roach throw new IndividualNotFoundException(); 5995a78cd34SGreg Roach } 6005a78cd34SGreg Roach 6015a78cd34SGreg Roach $options = $this->individualOptions($individual); 6025a78cd34SGreg Roach 60339ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $individual->fullName()); 6045a78cd34SGreg Roach 6055a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 6065a78cd34SGreg Roach 'options' => $options, 6075a78cd34SGreg Roach 'default' => key($options), 6085a78cd34SGreg Roach 'record' => $individual, 6095a78cd34SGreg Roach 'title' => $title, 6105a78cd34SGreg Roach 'tree' => $tree, 6115a78cd34SGreg Roach ]); 6125a78cd34SGreg Roach } 6135a78cd34SGreg Roach 6145a78cd34SGreg Roach /** 6155a78cd34SGreg Roach * @param Individual $individual 6165a78cd34SGreg Roach * 6175a78cd34SGreg Roach * @return string[] 6185a78cd34SGreg Roach */ 619c1010edaSGreg Roach private function individualOptions(Individual $individual): array 620c1010edaSGreg Roach { 62139ca88baSGreg Roach $name = strip_tags($individual->fullName()); 6225a78cd34SGreg Roach 62339ca88baSGreg Roach if ($individual->sex() === 'F') { 6245a78cd34SGreg Roach return [ 6255a78cd34SGreg Roach 'self' => $name, 6265a78cd34SGreg Roach 'parents' => I18N::translate('%s, her parents and siblings', $name), 6275a78cd34SGreg Roach 'spouses' => I18N::translate('%s, her spouses and children', $name), 6285a78cd34SGreg Roach 'ancestors' => I18N::translate('%s and her ancestors', $name), 6295a78cd34SGreg Roach 'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name), 6305a78cd34SGreg Roach 'descendants' => I18N::translate('%s, her spouses and descendants', $name), 6315a78cd34SGreg Roach ]; 632b2ce94c6SRico Sonntag } 633b2ce94c6SRico Sonntag 6345a78cd34SGreg Roach return [ 6355a78cd34SGreg Roach 'self' => $name, 6365a78cd34SGreg Roach 'parents' => I18N::translate('%s, his parents and siblings', $name), 6375a78cd34SGreg Roach 'spouses' => I18N::translate('%s, his spouses and children', $name), 6385a78cd34SGreg Roach 'ancestors' => I18N::translate('%s and his ancestors', $name), 6395a78cd34SGreg Roach 'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name), 6405a78cd34SGreg Roach 'descendants' => I18N::translate('%s, his spouses and descendants', $name), 6415a78cd34SGreg Roach ]; 6425a78cd34SGreg Roach } 6435a78cd34SGreg Roach 6445a78cd34SGreg Roach /** 6456ccdf4f0SGreg Roach * @param ServerRequestInterface $request 6465a78cd34SGreg Roach * 6476ccdf4f0SGreg Roach * @return ResponseInterface 6485a78cd34SGreg Roach */ 64957ab2231SGreg Roach public function postAddIndividualAction(ServerRequestInterface $request): ResponseInterface 650c1010edaSGreg Roach { 65157ab2231SGreg Roach $tree = $request->getAttribute('tree'); 6524ea62551SGreg Roach assert($tree instanceof Tree); 6534ea62551SGreg Roach 654b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 655b46c87bdSGreg Roach 656b46c87bdSGreg Roach $xref = $params['xref']; 657b46c87bdSGreg Roach $option = $params['option']; 6585a78cd34SGreg Roach 659a091ac74SGreg Roach $individual = Factory::individual()->make($xref, $tree); 6605a78cd34SGreg Roach 6615a78cd34SGreg Roach if ($individual === null) { 66259f2f229SGreg Roach throw new IndividualNotFoundException(); 6635a78cd34SGreg Roach } 6645a78cd34SGreg Roach 6655a78cd34SGreg Roach switch ($option) { 6665a78cd34SGreg Roach case 'self': 6675a78cd34SGreg Roach $this->addRecordToCart($individual); 6685a78cd34SGreg Roach break; 6695a78cd34SGreg Roach 6705a78cd34SGreg Roach case 'parents': 67139ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 6725a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 6735a78cd34SGreg Roach } 6745a78cd34SGreg Roach break; 6755a78cd34SGreg Roach 6765a78cd34SGreg Roach case 'spouses': 67739ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 6785a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 6795a78cd34SGreg Roach } 6805a78cd34SGreg Roach break; 6815a78cd34SGreg Roach 6825a78cd34SGreg Roach case 'ancestors': 6835a78cd34SGreg Roach $this->addAncestorsToCart($individual); 6845a78cd34SGreg Roach break; 6855a78cd34SGreg Roach 6865a78cd34SGreg Roach case 'ancestor_families': 6875a78cd34SGreg Roach $this->addAncestorFamiliesToCart($individual); 6885a78cd34SGreg Roach break; 6895a78cd34SGreg Roach 6905a78cd34SGreg Roach case 'descendants': 69139ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 6925a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($family); 6935a78cd34SGreg Roach } 6945a78cd34SGreg Roach break; 6955a78cd34SGreg Roach } 6965a78cd34SGreg Roach 6976ccdf4f0SGreg Roach return redirect($individual->url()); 6985a78cd34SGreg Roach } 6995a78cd34SGreg Roach 7005a78cd34SGreg Roach /** 7015a78cd34SGreg Roach * @param Individual $individual 70218d7a90dSGreg Roach * 70318d7a90dSGreg Roach * @return void 7045a78cd34SGreg Roach */ 705e364afe4SGreg Roach private function addAncestorsToCart(Individual $individual): void 706c1010edaSGreg Roach { 7075a78cd34SGreg Roach $this->addRecordToCart($individual); 7085a78cd34SGreg Roach 70939ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 7108df4c68dSGreg Roach $this->addRecordToCart($family); 7118df4c68dSGreg Roach 71239ca88baSGreg Roach foreach ($family->spouses() as $parent) { 7135a78cd34SGreg Roach $this->addAncestorsToCart($parent); 7145a78cd34SGreg Roach } 7155a78cd34SGreg Roach } 7165a78cd34SGreg Roach } 7175a78cd34SGreg Roach 7185a78cd34SGreg Roach /** 7195a78cd34SGreg Roach * @param Individual $individual 72018d7a90dSGreg Roach * 72118d7a90dSGreg Roach * @return void 7225a78cd34SGreg Roach */ 723e364afe4SGreg Roach private function addAncestorFamiliesToCart(Individual $individual): void 724c1010edaSGreg Roach { 72539ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 7265a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 7278df4c68dSGreg Roach 72839ca88baSGreg Roach foreach ($family->spouses() as $parent) { 729cad6d3f3SGreg Roach $this->addAncestorFamiliesToCart($parent); 7305a78cd34SGreg Roach } 7315a78cd34SGreg Roach } 7325a78cd34SGreg Roach } 7335a78cd34SGreg Roach 7345a78cd34SGreg Roach /** 7356ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7365a78cd34SGreg Roach * 7376ccdf4f0SGreg Roach * @return ResponseInterface 7385a78cd34SGreg Roach */ 73957ab2231SGreg Roach public function getAddMediaAction(ServerRequestInterface $request): ResponseInterface 740c1010edaSGreg Roach { 74157ab2231SGreg Roach $tree = $request->getAttribute('tree'); 74275964c75SGreg Roach assert($tree instanceof Tree); 7435229eadeSGreg Roach 744bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 7455a78cd34SGreg Roach 746a091ac74SGreg Roach $media = Factory::media()->make($xref, $tree); 7475a78cd34SGreg Roach 7485a78cd34SGreg Roach if ($media === null) { 74959f2f229SGreg Roach throw new MediaNotFoundException(); 7505a78cd34SGreg Roach } 7515a78cd34SGreg Roach 7525a78cd34SGreg Roach $options = $this->mediaOptions($media); 7535a78cd34SGreg Roach 75439ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $media->fullName()); 7555a78cd34SGreg Roach 7565a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 7575a78cd34SGreg Roach 'options' => $options, 7585a78cd34SGreg Roach 'default' => key($options), 7595a78cd34SGreg Roach 'record' => $media, 7605a78cd34SGreg Roach 'title' => $title, 7615a78cd34SGreg Roach 'tree' => $tree, 7625a78cd34SGreg Roach ]); 7635a78cd34SGreg Roach } 7645a78cd34SGreg Roach 7655a78cd34SGreg Roach /** 7665a78cd34SGreg Roach * @param Media $media 7675a78cd34SGreg Roach * 7685a78cd34SGreg Roach * @return string[] 7695a78cd34SGreg Roach */ 770c1010edaSGreg Roach private function mediaOptions(Media $media): array 771c1010edaSGreg Roach { 77239ca88baSGreg Roach $name = strip_tags($media->fullName()); 7735a78cd34SGreg Roach 7745a78cd34SGreg Roach return [ 7755a78cd34SGreg Roach 'self' => $name, 7765a78cd34SGreg Roach ]; 7775a78cd34SGreg Roach } 7785a78cd34SGreg Roach 7795a78cd34SGreg Roach /** 7806ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7815a78cd34SGreg Roach * 7826ccdf4f0SGreg Roach * @return ResponseInterface 7835a78cd34SGreg Roach */ 78457ab2231SGreg Roach public function postAddMediaAction(ServerRequestInterface $request): ResponseInterface 785c1010edaSGreg Roach { 78657ab2231SGreg Roach $tree = $request->getAttribute('tree'); 78775964c75SGreg Roach assert($tree instanceof Tree); 7885229eadeSGreg Roach 789bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 7905a78cd34SGreg Roach 791a091ac74SGreg Roach $media = Factory::media()->make($xref, $tree); 7925a78cd34SGreg Roach 7935a78cd34SGreg Roach if ($media === null) { 79459f2f229SGreg Roach throw new MediaNotFoundException(); 7955a78cd34SGreg Roach } 7965a78cd34SGreg Roach 7975a78cd34SGreg Roach $this->addRecordToCart($media); 7985a78cd34SGreg Roach 7996ccdf4f0SGreg Roach return redirect($media->url()); 8005a78cd34SGreg Roach } 8015a78cd34SGreg Roach 8025a78cd34SGreg Roach /** 8036ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8045a78cd34SGreg Roach * 8056ccdf4f0SGreg Roach * @return ResponseInterface 8065a78cd34SGreg Roach */ 80757ab2231SGreg Roach public function getAddNoteAction(ServerRequestInterface $request): ResponseInterface 808c1010edaSGreg Roach { 80957ab2231SGreg Roach $tree = $request->getAttribute('tree'); 81075964c75SGreg Roach assert($tree instanceof Tree); 8115229eadeSGreg Roach 812bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8135a78cd34SGreg Roach 814a091ac74SGreg Roach $note = Factory::note()->make($xref, $tree); 8155a78cd34SGreg Roach 8165a78cd34SGreg Roach if ($note === null) { 81759f2f229SGreg Roach throw new NoteNotFoundException(); 8185a78cd34SGreg Roach } 8195a78cd34SGreg Roach 8205a78cd34SGreg Roach $options = $this->noteOptions($note); 8215a78cd34SGreg Roach 82239ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $note->fullName()); 8235a78cd34SGreg Roach 8245a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 8255a78cd34SGreg Roach 'options' => $options, 8265a78cd34SGreg Roach 'default' => key($options), 8275a78cd34SGreg Roach 'record' => $note, 8285a78cd34SGreg Roach 'title' => $title, 8295a78cd34SGreg Roach 'tree' => $tree, 8305a78cd34SGreg Roach ]); 8315a78cd34SGreg Roach } 8325a78cd34SGreg Roach 8335a78cd34SGreg Roach /** 8345a78cd34SGreg Roach * @param Note $note 8355a78cd34SGreg Roach * 8365a78cd34SGreg Roach * @return string[] 8375a78cd34SGreg Roach */ 838c1010edaSGreg Roach private function noteOptions(Note $note): array 839c1010edaSGreg Roach { 84039ca88baSGreg Roach $name = strip_tags($note->fullName()); 8415a78cd34SGreg Roach 8425a78cd34SGreg Roach return [ 8435a78cd34SGreg Roach 'self' => $name, 8445a78cd34SGreg Roach ]; 8455a78cd34SGreg Roach } 8465a78cd34SGreg Roach 8475a78cd34SGreg Roach /** 8486ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8495a78cd34SGreg Roach * 8506ccdf4f0SGreg Roach * @return ResponseInterface 8515a78cd34SGreg Roach */ 85257ab2231SGreg Roach public function postAddNoteAction(ServerRequestInterface $request): ResponseInterface 853c1010edaSGreg Roach { 85457ab2231SGreg Roach $tree = $request->getAttribute('tree'); 85575964c75SGreg Roach assert($tree instanceof Tree); 8565229eadeSGreg Roach 857bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8585a78cd34SGreg Roach 859a091ac74SGreg Roach $note = Factory::note()->make($xref, $tree); 8605a78cd34SGreg Roach 8615a78cd34SGreg Roach if ($note === null) { 86259f2f229SGreg Roach throw new NoteNotFoundException(); 8635a78cd34SGreg Roach } 8645a78cd34SGreg Roach 8655a78cd34SGreg Roach $this->addRecordToCart($note); 8665a78cd34SGreg Roach 8676ccdf4f0SGreg Roach return redirect($note->url()); 8685a78cd34SGreg Roach } 8695a78cd34SGreg Roach 8705a78cd34SGreg Roach /** 8716ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8725a78cd34SGreg Roach * 8736ccdf4f0SGreg Roach * @return ResponseInterface 8745a78cd34SGreg Roach */ 87557ab2231SGreg Roach public function getAddRepositoryAction(ServerRequestInterface $request): ResponseInterface 876c1010edaSGreg Roach { 87757ab2231SGreg Roach $tree = $request->getAttribute('tree'); 87875964c75SGreg Roach assert($tree instanceof Tree); 8795229eadeSGreg Roach 880bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8815a78cd34SGreg Roach 882a091ac74SGreg Roach $repository = Factory::repository()->make($xref, $tree); 8835a78cd34SGreg Roach 8845a78cd34SGreg Roach if ($repository === null) { 88559f2f229SGreg Roach throw new RepositoryNotFoundException(); 8865a78cd34SGreg Roach } 8875a78cd34SGreg Roach 8885a78cd34SGreg Roach $options = $this->repositoryOptions($repository); 8895a78cd34SGreg Roach 89039ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $repository->fullName()); 8915a78cd34SGreg Roach 8925a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 8935a78cd34SGreg Roach 'options' => $options, 8945a78cd34SGreg Roach 'default' => key($options), 8955a78cd34SGreg Roach 'record' => $repository, 8965a78cd34SGreg Roach 'title' => $title, 8975a78cd34SGreg Roach 'tree' => $tree, 8985a78cd34SGreg Roach ]); 8995a78cd34SGreg Roach } 9005a78cd34SGreg Roach 9015a78cd34SGreg Roach /** 9025a78cd34SGreg Roach * @param Repository $repository 9035a78cd34SGreg Roach * 9045a78cd34SGreg Roach * @return string[] 9055a78cd34SGreg Roach */ 906c1010edaSGreg Roach private function repositoryOptions(Repository $repository): array 907c1010edaSGreg Roach { 90839ca88baSGreg Roach $name = strip_tags($repository->fullName()); 9095a78cd34SGreg Roach 9105a78cd34SGreg Roach return [ 9115a78cd34SGreg Roach 'self' => $name, 9125a78cd34SGreg Roach ]; 9135a78cd34SGreg Roach } 9145a78cd34SGreg Roach 9155a78cd34SGreg Roach /** 9166ccdf4f0SGreg Roach * @param ServerRequestInterface $request 9175a78cd34SGreg Roach * 9186ccdf4f0SGreg Roach * @return ResponseInterface 9195a78cd34SGreg Roach */ 92057ab2231SGreg Roach public function postAddRepositoryAction(ServerRequestInterface $request): ResponseInterface 921c1010edaSGreg Roach { 92257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 92375964c75SGreg Roach assert($tree instanceof Tree); 9245229eadeSGreg Roach 925bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 9265a78cd34SGreg Roach 927a091ac74SGreg Roach $repository = Factory::repository()->make($xref, $tree); 9285a78cd34SGreg Roach 9295a78cd34SGreg Roach if ($repository === null) { 93059f2f229SGreg Roach throw new RepositoryNotFoundException(); 9315a78cd34SGreg Roach } 9325a78cd34SGreg Roach 9335a78cd34SGreg Roach $this->addRecordToCart($repository); 9345a78cd34SGreg Roach 9356ccdf4f0SGreg Roach return redirect($repository->url()); 9365a78cd34SGreg Roach } 9375a78cd34SGreg Roach 9385a78cd34SGreg Roach /** 9396ccdf4f0SGreg Roach * @param ServerRequestInterface $request 9405a78cd34SGreg Roach * 9416ccdf4f0SGreg Roach * @return ResponseInterface 9425a78cd34SGreg Roach */ 94357ab2231SGreg Roach public function getAddSourceAction(ServerRequestInterface $request): ResponseInterface 944c1010edaSGreg Roach { 94557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 94675964c75SGreg Roach assert($tree instanceof Tree); 9475229eadeSGreg Roach 948bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 9495a78cd34SGreg Roach 950a091ac74SGreg Roach $source = Factory::source()->make($xref, $tree); 9515a78cd34SGreg Roach 9525a78cd34SGreg Roach if ($source === null) { 95359f2f229SGreg Roach throw new SourceNotFoundException(); 9545a78cd34SGreg Roach } 9555a78cd34SGreg Roach 9565a78cd34SGreg Roach $options = $this->sourceOptions($source); 9575a78cd34SGreg Roach 95839ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $source->fullName()); 9595a78cd34SGreg Roach 9605a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 9615a78cd34SGreg Roach 'options' => $options, 9625a78cd34SGreg Roach 'default' => key($options), 9635a78cd34SGreg Roach 'record' => $source, 9645a78cd34SGreg Roach 'title' => $title, 9655a78cd34SGreg Roach 'tree' => $tree, 9665a78cd34SGreg Roach ]); 9675a78cd34SGreg Roach } 9685a78cd34SGreg Roach 9695a78cd34SGreg Roach /** 9705a78cd34SGreg Roach * @param Source $source 9715a78cd34SGreg Roach * 9725a78cd34SGreg Roach * @return string[] 9735a78cd34SGreg Roach */ 974c1010edaSGreg Roach private function sourceOptions(Source $source): array 975c1010edaSGreg Roach { 97639ca88baSGreg Roach $name = strip_tags($source->fullName()); 9775a78cd34SGreg Roach 9785a78cd34SGreg Roach return [ 97939ca88baSGreg Roach 'only' => strip_tags($source->fullName()), 9805a78cd34SGreg Roach 'linked' => I18N::translate('%s and the individuals that reference it.', $name), 9815a78cd34SGreg Roach ]; 9825a78cd34SGreg Roach } 9835a78cd34SGreg Roach 9845a78cd34SGreg Roach /** 9856ccdf4f0SGreg Roach * @param ServerRequestInterface $request 9865a78cd34SGreg Roach * 9876ccdf4f0SGreg Roach * @return ResponseInterface 9885a78cd34SGreg Roach */ 98957ab2231SGreg Roach public function postAddSourceAction(ServerRequestInterface $request): ResponseInterface 990c1010edaSGreg Roach { 99157ab2231SGreg Roach $tree = $request->getAttribute('tree'); 99275964c75SGreg Roach assert($tree instanceof Tree); 9935229eadeSGreg Roach 994b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 995b46c87bdSGreg Roach 996b46c87bdSGreg Roach $xref = $params['xref']; 997b46c87bdSGreg Roach $option = $params['option']; 9985a78cd34SGreg Roach 999a091ac74SGreg Roach $source = Factory::source()->make($xref, $tree); 10005a78cd34SGreg Roach 10015a78cd34SGreg Roach if ($source === null) { 100259f2f229SGreg Roach throw new SourceNotFoundException(); 10035a78cd34SGreg Roach } 10045a78cd34SGreg Roach 10055a78cd34SGreg Roach $this->addRecordToCart($source); 10065a78cd34SGreg Roach 10075a78cd34SGreg Roach if ($option === 'linked') { 10085a78cd34SGreg Roach foreach ($source->linkedIndividuals('SOUR') as $individual) { 10095a78cd34SGreg Roach $this->addRecordToCart($individual); 10105a78cd34SGreg Roach } 10115a78cd34SGreg Roach foreach ($source->linkedFamilies('SOUR') as $family) { 10125a78cd34SGreg Roach $this->addRecordToCart($family); 10135a78cd34SGreg Roach } 10145a78cd34SGreg Roach } 10155a78cd34SGreg Roach 10166ccdf4f0SGreg Roach return redirect($source->url()); 10175a78cd34SGreg Roach } 10185a78cd34SGreg Roach 10195a78cd34SGreg Roach /** 10205a78cd34SGreg Roach * Get all the records in the cart. 10215a78cd34SGreg Roach * 10225a78cd34SGreg Roach * @param Tree $tree 10235a78cd34SGreg Roach * 10245a78cd34SGreg Roach * @return GedcomRecord[] 10255a78cd34SGreg Roach */ 1026c1010edaSGreg Roach private function allRecordsInCart(Tree $tree): array 1027c1010edaSGreg Roach { 10285a78cd34SGreg Roach $cart = Session::get('cart', []); 10295a78cd34SGreg Roach 1030aa6f03bbSGreg Roach $xrefs = array_keys($cart[$tree->name()] ?? []); 1031c8846facSGreg Roach $xrefs = array_map('strval', $xrefs); // PHP converts numeric keys to integers. 10325a78cd34SGreg Roach 10335a78cd34SGreg Roach // Fetch all the records in the cart. 1034bed27cedSGreg Roach $records = array_map(static function (string $xref) use ($tree): ?GedcomRecord { 1035a091ac74SGreg Roach return Factory::gedcomRecord()->make($xref, $tree); 10365a78cd34SGreg Roach }, $xrefs); 10375a78cd34SGreg Roach 10385a78cd34SGreg Roach // Some records may have been deleted after they were added to the cart. 10395a78cd34SGreg Roach $records = array_filter($records); 10405a78cd34SGreg Roach 10415a78cd34SGreg Roach // Group and sort. 10420b5fd0a6SGreg Roach uasort($records, static function (GedcomRecord $x, GedcomRecord $y): int { 1043c156e8f5SGreg Roach return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::nameComparator()($x, $y); 10445a78cd34SGreg Roach }); 10455a78cd34SGreg Roach 10465a78cd34SGreg Roach return $records; 10475a78cd34SGreg Roach } 10485a78cd34SGreg Roach 10495a78cd34SGreg Roach /** 10505a78cd34SGreg Roach * Add a record (and direclty linked sources, notes, etc. to the cart. 10515a78cd34SGreg Roach * 10525a78cd34SGreg Roach * @param GedcomRecord $record 105318d7a90dSGreg Roach * 105418d7a90dSGreg Roach * @return void 10555a78cd34SGreg Roach */ 1056e364afe4SGreg Roach private function addRecordToCart(GedcomRecord $record): void 1057c1010edaSGreg Roach { 10585a78cd34SGreg Roach $cart = Session::get('cart', []); 10595a78cd34SGreg Roach 1060f4afa648SGreg Roach $tree_name = $record->tree()->name(); 10615a78cd34SGreg Roach 10625a78cd34SGreg Roach // Add this record 1063c0935879SGreg Roach $cart[$tree_name][$record->xref()] = true; 10645a78cd34SGreg Roach 10655a78cd34SGreg Roach // Add directly linked media, notes, repositories and sources. 10668d0ebef0SGreg Roach preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches); 10675a78cd34SGreg Roach 10685a78cd34SGreg Roach foreach ($matches[1] as $match) { 10695a78cd34SGreg Roach $cart[$tree_name][$match] = true; 10705a78cd34SGreg Roach } 10715a78cd34SGreg Roach 10725a78cd34SGreg Roach Session::put('cart', $cart); 10735a78cd34SGreg Roach } 10745a78cd34SGreg Roach 10755a78cd34SGreg Roach /** 10765a78cd34SGreg Roach * @param Tree $tree 10775a78cd34SGreg Roach * 10785a78cd34SGreg Roach * @return bool 10795a78cd34SGreg Roach */ 1080c1010edaSGreg Roach private function isCartEmpty(Tree $tree): bool 1081c1010edaSGreg Roach { 10825a78cd34SGreg Roach $cart = Session::get('cart', []); 1083a91af26aSGreg Roach $contents = $cart[$tree->name()] ?? []; 10845a78cd34SGreg Roach 1085a91af26aSGreg Roach return $contents === []; 10865a78cd34SGreg Roach } 10878c2e8227SGreg Roach} 1088