18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98c2e8227SGreg Roach * (at your option) any later version. 108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138c2e8227SGreg Roach * GNU General Public License for more details. 148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17e7f56f2aSGreg Roachdeclare(strict_types=1); 18e7f56f2aSGreg Roach 1976692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2076692c8bSGreg Roach 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 220bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\FamilyNotFoundException; 230bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException; 240bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\MediaNotFoundException; 250bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\NoteNotFoundException; 260bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\RepositoryNotFoundException; 270bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\SourceNotFoundException; 280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family; 295a78cd34SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport; 305a78cd34SGreg Roachuse Fisharebest\Webtrees\Gedcom; 310e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 32*71378461SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\ModuleAction; 330e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 340e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 355a78cd34SGreg Roachuse Fisharebest\Webtrees\Media; 360e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu; 375a78cd34SGreg Roachuse Fisharebest\Webtrees\Note; 385a78cd34SGreg Roachuse Fisharebest\Webtrees\Repository; 39e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 400e62c4b8SGreg Roachuse Fisharebest\Webtrees\Session; 415a78cd34SGreg Roachuse Fisharebest\Webtrees\Source; 42aee13b6dSGreg Roachuse Fisharebest\Webtrees\Tree; 435a78cd34SGreg Roachuse League\Flysystem\Filesystem; 4461bf91b2SGreg Roachuse League\Flysystem\MountManager; 455a78cd34SGreg Roachuse League\Flysystem\ZipArchive\ZipArchiveAdapter; 46bed27cedSGreg Roachuse Psr\Http\Message\ResponseFactoryInterface; 476ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 486ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 496ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface; 503976b470SGreg Roach 51eb235819SGreg Roachuse function app; 52bf80ec58SGreg Roachuse function array_filter; 53bf80ec58SGreg Roachuse function array_keys; 54bf80ec58SGreg Roachuse function array_map; 55bf80ec58SGreg Roachuse function in_array; 56bf80ec58SGreg Roachuse function key; 57bf80ec58SGreg Roachuse function preg_match_all; 58bf80ec58SGreg Roachuse function redirect; 59bf80ec58SGreg Roachuse function route; 60e5a6b4d4SGreg Roachuse function str_replace; 61bf80ec58SGreg Roachuse function strip_tags; 62bf80ec58SGreg Roachuse function sys_get_temp_dir; 63bf80ec58SGreg Roachuse function tempnam; 64bf80ec58SGreg Roachuse function ucfirst; 65bf80ec58SGreg Roachuse function utf8_decode; 668c2e8227SGreg Roach 678c2e8227SGreg Roach/** 688c2e8227SGreg Roach * Class ClippingsCartModule 698c2e8227SGreg Roach */ 7037eb8894SGreg Roachclass ClippingsCartModule extends AbstractModule implements ModuleMenuInterface 71c1010edaSGreg Roach{ 7249a243cbSGreg Roach use ModuleMenuTrait; 7349a243cbSGreg Roach 745a78cd34SGreg Roach // Routes that have a record which can be added to the clipboard 7516d6367aSGreg Roach private const ROUTES_WITH_RECORDS = [ 76c1010edaSGreg Roach 'family', 77c1010edaSGreg Roach 'individual', 78c1010edaSGreg Roach 'media', 79c1010edaSGreg Roach 'note', 80c1010edaSGreg Roach 'repository', 81c1010edaSGreg Roach 'source', 82c1010edaSGreg Roach ]; 835a78cd34SGreg Roach 8449a243cbSGreg Roach /** @var int The default access level for this module. It can be changed in the control panel. */ 8549a243cbSGreg Roach protected $access_level = Auth::PRIV_USER; 8649a243cbSGreg Roach 87961ec755SGreg Roach /** 88e5a6b4d4SGreg Roach * @var UserService 89e5a6b4d4SGreg Roach */ 90e5a6b4d4SGreg Roach private $user_service; 91e5a6b4d4SGreg Roach 92e5a6b4d4SGreg Roach /** 93e5a6b4d4SGreg Roach * ClippingsCartModule constructor. 94e5a6b4d4SGreg Roach * 95e5a6b4d4SGreg Roach * @param UserService $user_service 96e5a6b4d4SGreg Roach */ 97e5a6b4d4SGreg Roach public function __construct(UserService $user_service) 98e5a6b4d4SGreg Roach { 99e5a6b4d4SGreg Roach $this->user_service = $user_service; 100e5a6b4d4SGreg Roach } 101e5a6b4d4SGreg Roach 102e5a6b4d4SGreg Roach /** 1030cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 104961ec755SGreg Roach * 105961ec755SGreg Roach * @return string 106961ec755SGreg Roach */ 10749a243cbSGreg Roach public function title(): string 108c1010edaSGreg Roach { 109bbb76c12SGreg Roach /* I18N: Name of a module */ 110bbb76c12SGreg Roach return I18N::translate('Clippings cart'); 1118c2e8227SGreg Roach } 1128c2e8227SGreg Roach 113961ec755SGreg Roach /** 114961ec755SGreg Roach * A sentence describing what this module does. 115961ec755SGreg Roach * 116961ec755SGreg Roach * @return string 117961ec755SGreg Roach */ 11849a243cbSGreg Roach public function description(): string 119c1010edaSGreg Roach { 120bbb76c12SGreg Roach /* I18N: Description of the “Clippings cart” module */ 121bbb76c12SGreg Roach return I18N::translate('Select records from your family tree and save them as a GEDCOM file.'); 1228c2e8227SGreg Roach } 1238c2e8227SGreg Roach 1240ee13198SGreg Roach /** 12549a243cbSGreg Roach * The default position for this menu. It can be changed in the control panel. 1260ee13198SGreg Roach * 1270ee13198SGreg Roach * @return int 1280ee13198SGreg Roach */ 1298f53f488SRico Sonntag public function defaultMenuOrder(): int 130c1010edaSGreg Roach { 131353b36abSGreg Roach return 6; 1328c2e8227SGreg Roach } 1338c2e8227SGreg Roach 1340ee13198SGreg Roach /** 1350ee13198SGreg Roach * A menu, to be added to the main application menu. 1360ee13198SGreg Roach * 137aee13b6dSGreg Roach * @param Tree $tree 138aee13b6dSGreg Roach * 1390ee13198SGreg Roach * @return Menu|null 1400ee13198SGreg Roach */ 14146295629SGreg Roach public function getMenu(Tree $tree): ?Menu 142c1010edaSGreg Roach { 143eb235819SGreg Roach /** @var ServerRequestInterface $request */ 1446ccdf4f0SGreg Roach $request = app(ServerRequestInterface::class); 1458c2e8227SGreg Roach 146eb235819SGreg Roach $route = $request->getQueryParams()['route'] ?? ''; 1475a78cd34SGreg Roach 1485a78cd34SGreg Roach $submenus = [ 14949a243cbSGreg Roach new Menu($this->title(), route('module', [ 15026684e68SGreg Roach 'module' => $this->name(), 151c1010edaSGreg Roach 'action' => 'Show', 152aa6f03bbSGreg Roach 'ged' => $tree->name(), 153c1010edaSGreg Roach ]), 'menu-clippings-cart', ['rel' => 'nofollow']), 1545a78cd34SGreg Roach ]; 1555a78cd34SGreg Roach 15622d65e5aSGreg Roach if (in_array($route, self::ROUTES_WITH_RECORDS, true)) { 157bed27cedSGreg Roach $xref = $request->getQueryParams()['xref'] ?? ''; 1585a78cd34SGreg Roach $action = 'Add' . ucfirst($route); 159c1010edaSGreg Roach $add_route = route('module', [ 16026684e68SGreg Roach 'module' => $this->name(), 161c1010edaSGreg Roach 'action' => $action, 162c1010edaSGreg Roach 'xref' => $xref, 163aa6f03bbSGreg Roach 'ged' => $tree->name(), 164c1010edaSGreg Roach ]); 1655a78cd34SGreg Roach 16625b2dde3SGreg Roach $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']); 1678c2e8227SGreg Roach } 168cbc1590aSGreg Roach 1695a78cd34SGreg Roach if (!$this->isCartEmpty($tree)) { 170c1010edaSGreg Roach $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [ 17126684e68SGreg Roach 'module' => $this->name(), 172c1010edaSGreg Roach 'action' => 'Empty', 173aa6f03bbSGreg Roach 'ged' => $tree->name(), 174c1010edaSGreg Roach ]), 'menu-clippings-empty', ['rel' => 'nofollow']); 175c1010edaSGreg Roach $submenus[] = new Menu(I18N::translate('Download'), route('module', [ 17626684e68SGreg Roach 'module' => $this->name(), 177c1010edaSGreg Roach 'action' => 'DownloadForm', 178aa6f03bbSGreg Roach 'ged' => $tree->name(), 179c1010edaSGreg Roach ]), 'menu-clippings-download', ['rel' => 'nofollow']); 1805a78cd34SGreg Roach } 1815a78cd34SGreg Roach 18249a243cbSGreg Roach return new Menu($this->title(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus); 1838c2e8227SGreg Roach } 1848c2e8227SGreg Roach 18576692c8bSGreg Roach /** 1866ccdf4f0SGreg Roach * @param ServerRequestInterface $request 18776692c8bSGreg Roach * 1886ccdf4f0SGreg Roach * @return ResponseInterface 18976692c8bSGreg Roach */ 19057ab2231SGreg Roach public function getDownloadAction(ServerRequestInterface $request): ResponseInterface 191c1010edaSGreg Roach { 19257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 193bed27cedSGreg Roach $params = $request->getQueryParams(); 194bed27cedSGreg Roach 195bed27cedSGreg Roach $privatize_export = $params['privatize_export']; 196bed27cedSGreg Roach $convert = (bool) ($params['convert'] ?? false); 1978c2e8227SGreg Roach 19813abd6f3SGreg Roach $cart = Session::get('cart', []); 1998c2e8227SGreg Roach 200aa6f03bbSGreg Roach $xrefs = array_keys($cart[$tree->name()] ?? []); 2015a78cd34SGreg Roach 2025a78cd34SGreg Roach // Create a new/empty .ZIP file 2035a78cd34SGreg Roach $temp_zip_file = tempnam(sys_get_temp_dir(), 'webtrees-zip-'); 2045a78cd34SGreg Roach $zip_filesystem = new Filesystem(new ZipArchiveAdapter($temp_zip_file)); 2055a78cd34SGreg Roach 20661bf91b2SGreg Roach $manager = new MountManager([ 20761bf91b2SGreg Roach 'media' => $tree->mediaFilesystem(), 20861bf91b2SGreg Roach 'zip' => $zip_filesystem, 20961bf91b2SGreg Roach ]); 21061bf91b2SGreg Roach 2115a78cd34SGreg Roach // Media file prefix 2125a78cd34SGreg Roach $path = $tree->getPreference('MEDIA_DIRECTORY'); 2135a78cd34SGreg Roach 2145a78cd34SGreg Roach // GEDCOM file header 215a3d8780cSGreg Roach $filetext = FunctionsExport::gedcomHeader($tree, $convert ? 'ANSI' : 'UTF-8'); 2165a78cd34SGreg Roach 2175a78cd34SGreg Roach switch ($privatize_export) { 2185a78cd34SGreg Roach case 'gedadmin': 2195a78cd34SGreg Roach $access_level = Auth::PRIV_NONE; 2205a78cd34SGreg Roach break; 2215a78cd34SGreg Roach case 'user': 2225a78cd34SGreg Roach $access_level = Auth::PRIV_USER; 2235a78cd34SGreg Roach break; 2245a78cd34SGreg Roach case 'visitor': 2255a78cd34SGreg Roach $access_level = Auth::PRIV_PRIVATE; 2265a78cd34SGreg Roach break; 2275a78cd34SGreg Roach case 'none': 2285a78cd34SGreg Roach default: 2295a78cd34SGreg Roach $access_level = Auth::PRIV_HIDE; 2305a78cd34SGreg Roach break; 2315a78cd34SGreg Roach } 2325a78cd34SGreg Roach 2335a78cd34SGreg Roach foreach ($xrefs as $xref) { 2345a78cd34SGreg Roach $object = GedcomRecord::getInstance($xref, $tree); 2355a78cd34SGreg Roach // The object may have been deleted since we added it to the cart.... 236bed27cedSGreg Roach if ($object instanceof GedcomRecord) { 2375a78cd34SGreg Roach $record = $object->privatizeGedcom($access_level); 2385a78cd34SGreg Roach // Remove links to objects that aren't in the cart 2398d0ebef0SGreg Roach preg_match_all('/\n1 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER); 2405a78cd34SGreg Roach foreach ($matches as $match) { 241bf80ec58SGreg Roach if (!in_array($match[1], $xrefs, true)) { 2425a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2435a78cd34SGreg Roach } 2445a78cd34SGreg Roach } 2458d0ebef0SGreg Roach preg_match_all('/\n2 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER); 2465a78cd34SGreg Roach foreach ($matches as $match) { 247bf80ec58SGreg Roach if (!in_array($match[1], $xrefs, true)) { 2485a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2495a78cd34SGreg Roach } 2505a78cd34SGreg Roach } 2518d0ebef0SGreg Roach preg_match_all('/\n3 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER); 2525a78cd34SGreg Roach foreach ($matches as $match) { 253bf80ec58SGreg Roach if (!in_array($match[1], $xrefs, true)) { 2545a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2555a78cd34SGreg Roach } 2565a78cd34SGreg Roach } 2575a78cd34SGreg Roach 25855167344SGreg Roach if ($object instanceof Individual || $object instanceof Family) { 2595a78cd34SGreg Roach $filetext .= $record . "\n"; 2605a78cd34SGreg Roach $filetext .= "1 SOUR @WEBTREES@\n"; 2611f273236SGreg Roach $filetext .= '2 PAGE ' . $object->url() . "\n"; 26255167344SGreg Roach } elseif ($object instanceof Source) { 2635a78cd34SGreg Roach $filetext .= $record . "\n"; 2641f273236SGreg Roach $filetext .= '1 NOTE ' . $object->url() . "\n"; 26555167344SGreg Roach } elseif ($object instanceof Media) { 26655167344SGreg Roach // Add the media files to the archive 2675a78cd34SGreg Roach foreach ($object->mediaFiles() as $media_file) { 26861bf91b2SGreg Roach $from = 'media://' . $media_file->filename(); 26961bf91b2SGreg Roach $to = 'zip://' . $path . $media_file->filename(); 27061bf91b2SGreg Roach if (!$media_file->isExternal() && $manager->has($from)) { 27161bf91b2SGreg Roach $manager->copy($from, $to); 2725a78cd34SGreg Roach } 2735a78cd34SGreg Roach } 2745a78cd34SGreg Roach $filetext .= $record . "\n"; 27555167344SGreg Roach } else { 2765a78cd34SGreg Roach $filetext .= $record . "\n"; 2778c2e8227SGreg Roach } 2788c2e8227SGreg Roach } 2798c2e8227SGreg Roach } 2808c2e8227SGreg Roach 2819b93b7c3SGreg Roach $base_url = $request->getAttribute('base_url'); 2829b93b7c3SGreg Roach 2835a78cd34SGreg Roach // Create a source, to indicate the source of the data. 2849b93b7c3SGreg Roach $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . $base_url . "\n"; 285e5a6b4d4SGreg Roach $author = $this->user_service->find((int) $tree->getPreference('CONTACT_USER_ID')); 2865a78cd34SGreg Roach if ($author !== null) { 287e5a6b4d4SGreg Roach $filetext .= '1 AUTH ' . $author->realName() . "\n"; 2885a78cd34SGreg Roach } 2895a78cd34SGreg Roach $filetext .= "0 TRLR\n"; 2905a78cd34SGreg Roach 2915a78cd34SGreg Roach // Make sure the preferred line endings are used 292a3d8780cSGreg Roach $filetext = str_replace('\n', Gedcom::EOL, $filetext); 2935a78cd34SGreg Roach 29455167344SGreg Roach if ($convert) { 2955a78cd34SGreg Roach $filetext = utf8_decode($filetext); 2968c2e8227SGreg Roach } 297cbc1590aSGreg Roach 2985a78cd34SGreg Roach // Finally add the GEDCOM file to the .ZIP file. 2995a78cd34SGreg Roach $zip_filesystem->write('clippings.ged', $filetext); 3005a78cd34SGreg Roach 30161bf91b2SGreg Roach // Need to force-close ZipArchive filesystems. 30261bf91b2SGreg Roach $zip_filesystem->getAdapter()->getArchive()->close(); 3035a78cd34SGreg Roach 3046ccdf4f0SGreg Roach // Use a stream, so that we do not have to load the entire file into memory. 3056ccdf4f0SGreg Roach $stream = app(StreamFactoryInterface::class)->createStreamFromFile($temp_zip_file); 3065a78cd34SGreg Roach 307bed27cedSGreg Roach /** @var ResponseFactoryInterface $response_factory */ 308bed27cedSGreg Roach $response_factory = app(ResponseFactoryInterface::class); 309bed27cedSGreg Roach 310bed27cedSGreg Roach return $response_factory->createResponse() 3116ccdf4f0SGreg Roach ->withBody($stream) 3121b3d4731SGreg Roach ->withHeader('Content-Type', 'application/zip') 313bed27cedSGreg Roach ->withHeader('Content-Disposition', 'attachment; filename="clippings.zip'); 3148c2e8227SGreg Roach } 3158c2e8227SGreg Roach 3168c2e8227SGreg Roach /** 31757ab2231SGreg Roach * @param ServerRequestInterface $request 31876692c8bSGreg Roach * 3196ccdf4f0SGreg Roach * @return ResponseInterface 3208c2e8227SGreg Roach */ 32157ab2231SGreg Roach public function getDownloadFormAction(ServerRequestInterface $request): ResponseInterface 322c1010edaSGreg Roach { 32357ab2231SGreg Roach $tree = $request->getAttribute('tree'); 32457ab2231SGreg Roach $user = $request->getAttribute('user'); 3255a78cd34SGreg Roach $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download'); 3268c2e8227SGreg Roach 3275a78cd34SGreg Roach return $this->viewResponse('modules/clippings/download', [ 3285a78cd34SGreg Roach 'is_manager' => Auth::isManager($tree, $user), 3295a78cd34SGreg Roach 'is_member' => Auth::isMember($tree, $user), 330*71378461SGreg Roach 'module' => $this->name(), 3315a78cd34SGreg Roach 'title' => $title, 3325a78cd34SGreg Roach ]); 3338c2e8227SGreg Roach } 3348c2e8227SGreg Roach 3355a78cd34SGreg Roach /** 33657ab2231SGreg Roach * @param ServerRequestInterface $request 3375a78cd34SGreg Roach * 3386ccdf4f0SGreg Roach * @return ResponseInterface 3395a78cd34SGreg Roach */ 34057ab2231SGreg Roach public function getEmptyAction(ServerRequestInterface $request): ResponseInterface 341c1010edaSGreg Roach { 34257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 3435a78cd34SGreg Roach $cart = Session::get('cart', []); 344aa6f03bbSGreg Roach $cart[$tree->name()] = []; 3455a78cd34SGreg Roach Session::put('cart', $cart); 3468c2e8227SGreg Roach 347c1010edaSGreg Roach $url = route('module', [ 34826684e68SGreg Roach 'module' => $this->name(), 349c1010edaSGreg Roach 'action' => 'Show', 350aa6f03bbSGreg Roach 'ged' => $tree->name(), 351c1010edaSGreg Roach ]); 3525a78cd34SGreg Roach 3536ccdf4f0SGreg Roach return redirect($url); 3545a78cd34SGreg Roach } 3555a78cd34SGreg Roach 3565a78cd34SGreg Roach /** 3576ccdf4f0SGreg Roach * @param ServerRequestInterface $request 3585a78cd34SGreg Roach * 3596ccdf4f0SGreg Roach * @return ResponseInterface 3605a78cd34SGreg Roach */ 36157ab2231SGreg Roach public function postRemoveAction(ServerRequestInterface $request): ResponseInterface 362c1010edaSGreg Roach { 36357ab2231SGreg Roach $tree = $request->getAttribute('tree'); 364bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 3655a78cd34SGreg Roach 3665a78cd34SGreg Roach $cart = Session::get('cart', []); 367aa6f03bbSGreg Roach unset($cart[$tree->name()][$xref]); 3685a78cd34SGreg Roach Session::put('cart', $cart); 3695a78cd34SGreg Roach 370c1010edaSGreg Roach $url = route('module', [ 37126684e68SGreg Roach 'module' => $this->name(), 372c1010edaSGreg Roach 'action' => 'Show', 373aa6f03bbSGreg Roach 'ged' => $tree->name(), 374c1010edaSGreg Roach ]); 3755a78cd34SGreg Roach 3766ccdf4f0SGreg Roach return redirect($url); 3775a78cd34SGreg Roach } 3785a78cd34SGreg Roach 3795a78cd34SGreg Roach /** 38057ab2231SGreg Roach * @param ServerRequestInterface $request 3815a78cd34SGreg Roach * 3826ccdf4f0SGreg Roach * @return ResponseInterface 3835a78cd34SGreg Roach */ 38457ab2231SGreg Roach public function getShowAction(ServerRequestInterface $request): ResponseInterface 385c1010edaSGreg Roach { 38657ab2231SGreg Roach $tree = $request->getAttribute('tree'); 38757ab2231SGreg Roach 3885a78cd34SGreg Roach return $this->viewResponse('modules/clippings/show', [ 3895a78cd34SGreg Roach 'records' => $this->allRecordsInCart($tree), 3905a78cd34SGreg Roach 'title' => I18N::translate('Family tree clippings cart'), 3915a78cd34SGreg Roach 'tree' => $tree, 3925a78cd34SGreg Roach ]); 3935a78cd34SGreg Roach } 3945a78cd34SGreg Roach 3955a78cd34SGreg Roach /** 3966ccdf4f0SGreg Roach * @param ServerRequestInterface $request 3975a78cd34SGreg Roach * 3986ccdf4f0SGreg Roach * @return ResponseInterface 3995a78cd34SGreg Roach */ 40057ab2231SGreg Roach public function getAddFamilyAction(ServerRequestInterface $request): ResponseInterface 401c1010edaSGreg Roach { 40257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 403bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 4045a78cd34SGreg Roach 4055a78cd34SGreg Roach $family = Family::getInstance($xref, $tree); 4065a78cd34SGreg Roach 4075a78cd34SGreg Roach if ($family === null) { 40859f2f229SGreg Roach throw new FamilyNotFoundException(); 4095a78cd34SGreg Roach } 4105a78cd34SGreg Roach 4115a78cd34SGreg Roach $options = $this->familyOptions($family); 4125a78cd34SGreg Roach 41339ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $family->fullName()); 4145a78cd34SGreg Roach 4155a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 41683615acfSGreg Roach 'action' => route('module', ['module' => $this->name(), 'action' => 'AddFamily']), 4175a78cd34SGreg Roach 'options' => $options, 4185a78cd34SGreg Roach 'default' => key($options), 4195a78cd34SGreg Roach 'record' => $family, 4205a78cd34SGreg Roach 'title' => $title, 4215a78cd34SGreg Roach 'tree' => $tree, 4225a78cd34SGreg Roach ]); 4235a78cd34SGreg Roach } 4245a78cd34SGreg Roach 4255a78cd34SGreg Roach /** 4265a78cd34SGreg Roach * @param Family $family 4275a78cd34SGreg Roach * 4285a78cd34SGreg Roach * @return string[] 4295a78cd34SGreg Roach */ 430c1010edaSGreg Roach private function familyOptions(Family $family): array 431c1010edaSGreg Roach { 43239ca88baSGreg Roach $name = strip_tags($family->fullName()); 4335a78cd34SGreg Roach 4345a78cd34SGreg Roach return [ 4355a78cd34SGreg Roach 'parents' => $name, 436bbb76c12SGreg Roach /* I18N: %s is a family (husband + wife) */ 437bbb76c12SGreg Roach 'members' => I18N::translate('%s and their children', $name), 438bbb76c12SGreg Roach /* I18N: %s is a family (husband + wife) */ 439bbb76c12SGreg Roach 'descendants' => I18N::translate('%s and their descendants', $name), 4405a78cd34SGreg Roach ]; 4415a78cd34SGreg Roach } 4425a78cd34SGreg Roach 4435a78cd34SGreg Roach /** 4446ccdf4f0SGreg Roach * @param ServerRequestInterface $request 4455a78cd34SGreg Roach * 4466ccdf4f0SGreg Roach * @return ResponseInterface 4475a78cd34SGreg Roach */ 44857ab2231SGreg Roach public function postAddFamilyAction(ServerRequestInterface $request): ResponseInterface 449c1010edaSGreg Roach { 45057ab2231SGreg Roach $tree = $request->getAttribute('tree'); 451bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 452bed27cedSGreg Roach $option = $request->getParsedBody()['option']; 4535a78cd34SGreg Roach 4545a78cd34SGreg Roach $family = Family::getInstance($xref, $tree); 4555a78cd34SGreg Roach 4565a78cd34SGreg Roach if ($family === null) { 45759f2f229SGreg Roach throw new FamilyNotFoundException(); 4585a78cd34SGreg Roach } 4595a78cd34SGreg Roach 4605a78cd34SGreg Roach switch ($option) { 4615a78cd34SGreg Roach case 'parents': 4625a78cd34SGreg Roach $this->addFamilyToCart($family); 4635a78cd34SGreg Roach break; 4645a78cd34SGreg Roach 4655a78cd34SGreg Roach case 'members': 4665a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 4675a78cd34SGreg Roach break; 4685a78cd34SGreg Roach 4695a78cd34SGreg Roach case 'descendants': 4705a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($family); 4715a78cd34SGreg Roach break; 4725a78cd34SGreg Roach } 4735a78cd34SGreg Roach 4746ccdf4f0SGreg Roach return redirect($family->url()); 4755a78cd34SGreg Roach } 4765a78cd34SGreg Roach 4775a78cd34SGreg Roach /** 4785a78cd34SGreg Roach * @param Family $family 47918d7a90dSGreg Roach * 48018d7a90dSGreg Roach * @return void 4815a78cd34SGreg Roach */ 482e364afe4SGreg Roach private function addFamilyToCart(Family $family): void 483c1010edaSGreg Roach { 4845a78cd34SGreg Roach $this->addRecordToCart($family); 4855a78cd34SGreg Roach 48639ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 4875a78cd34SGreg Roach $this->addRecordToCart($spouse); 4885a78cd34SGreg Roach } 4895a78cd34SGreg Roach } 4905a78cd34SGreg Roach 4915a78cd34SGreg Roach /** 4925a78cd34SGreg Roach * @param Family $family 49318d7a90dSGreg Roach * 49418d7a90dSGreg Roach * @return void 4955a78cd34SGreg Roach */ 496e364afe4SGreg Roach private function addFamilyAndChildrenToCart(Family $family): void 497c1010edaSGreg Roach { 4985a78cd34SGreg Roach $this->addRecordToCart($family); 4995a78cd34SGreg Roach 50039ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 5015a78cd34SGreg Roach $this->addRecordToCart($spouse); 5025a78cd34SGreg Roach } 50339ca88baSGreg Roach foreach ($family->children() as $child) { 5045a78cd34SGreg Roach $this->addRecordToCart($child); 5055a78cd34SGreg Roach } 5065a78cd34SGreg Roach } 5075a78cd34SGreg Roach 5085a78cd34SGreg Roach /** 5095a78cd34SGreg Roach * @param Family $family 51018d7a90dSGreg Roach * 51118d7a90dSGreg Roach * @return void 5125a78cd34SGreg Roach */ 513e364afe4SGreg Roach private function addFamilyAndDescendantsToCart(Family $family): void 514c1010edaSGreg Roach { 5155a78cd34SGreg Roach $this->addRecordToCart($family); 5165a78cd34SGreg Roach 51739ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 5185a78cd34SGreg Roach $this->addRecordToCart($spouse); 5195a78cd34SGreg Roach } 52039ca88baSGreg Roach foreach ($family->children() as $child) { 5215a78cd34SGreg Roach $this->addRecordToCart($child); 52239ca88baSGreg Roach foreach ($child->spouseFamilies() as $child_family) { 5235a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($child_family); 5245a78cd34SGreg Roach } 5255a78cd34SGreg Roach } 5265a78cd34SGreg Roach } 5275a78cd34SGreg Roach 5285a78cd34SGreg Roach /** 5296ccdf4f0SGreg Roach * @param ServerRequestInterface $request 5305a78cd34SGreg Roach * 5316ccdf4f0SGreg Roach * @return ResponseInterface 5325a78cd34SGreg Roach */ 53357ab2231SGreg Roach public function getAddIndividualAction(ServerRequestInterface $request): ResponseInterface 534c1010edaSGreg Roach { 53557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 536bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 5375a78cd34SGreg Roach 5385a78cd34SGreg Roach $individual = Individual::getInstance($xref, $tree); 5395a78cd34SGreg Roach 5405a78cd34SGreg Roach if ($individual === null) { 54159f2f229SGreg Roach throw new IndividualNotFoundException(); 5425a78cd34SGreg Roach } 5435a78cd34SGreg Roach 5445a78cd34SGreg Roach $options = $this->individualOptions($individual); 5455a78cd34SGreg Roach 54639ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $individual->fullName()); 5475a78cd34SGreg Roach 5485a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 54983615acfSGreg Roach 'action' => route('module', ['module' => $this->name(), 'action' => 'AddIndividual']), 5505a78cd34SGreg Roach 'options' => $options, 5515a78cd34SGreg Roach 'default' => key($options), 5525a78cd34SGreg Roach 'record' => $individual, 5535a78cd34SGreg Roach 'title' => $title, 5545a78cd34SGreg Roach 'tree' => $tree, 5555a78cd34SGreg Roach ]); 5565a78cd34SGreg Roach } 5575a78cd34SGreg Roach 5585a78cd34SGreg Roach /** 5595a78cd34SGreg Roach * @param Individual $individual 5605a78cd34SGreg Roach * 5615a78cd34SGreg Roach * @return string[] 5625a78cd34SGreg Roach */ 563c1010edaSGreg Roach private function individualOptions(Individual $individual): array 564c1010edaSGreg Roach { 56539ca88baSGreg Roach $name = strip_tags($individual->fullName()); 5665a78cd34SGreg Roach 56739ca88baSGreg Roach if ($individual->sex() === 'F') { 5685a78cd34SGreg Roach return [ 5695a78cd34SGreg Roach 'self' => $name, 5705a78cd34SGreg Roach 'parents' => I18N::translate('%s, her parents and siblings', $name), 5715a78cd34SGreg Roach 'spouses' => I18N::translate('%s, her spouses and children', $name), 5725a78cd34SGreg Roach 'ancestors' => I18N::translate('%s and her ancestors', $name), 5735a78cd34SGreg Roach 'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name), 5745a78cd34SGreg Roach 'descendants' => I18N::translate('%s, her spouses and descendants', $name), 5755a78cd34SGreg Roach ]; 576b2ce94c6SRico Sonntag } 577b2ce94c6SRico Sonntag 5785a78cd34SGreg Roach return [ 5795a78cd34SGreg Roach 'self' => $name, 5805a78cd34SGreg Roach 'parents' => I18N::translate('%s, his parents and siblings', $name), 5815a78cd34SGreg Roach 'spouses' => I18N::translate('%s, his spouses and children', $name), 5825a78cd34SGreg Roach 'ancestors' => I18N::translate('%s and his ancestors', $name), 5835a78cd34SGreg Roach 'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name), 5845a78cd34SGreg Roach 'descendants' => I18N::translate('%s, his spouses and descendants', $name), 5855a78cd34SGreg Roach ]; 5865a78cd34SGreg Roach } 5875a78cd34SGreg Roach 5885a78cd34SGreg Roach /** 5896ccdf4f0SGreg Roach * @param ServerRequestInterface $request 5905a78cd34SGreg Roach * 5916ccdf4f0SGreg Roach * @return ResponseInterface 5925a78cd34SGreg Roach */ 59357ab2231SGreg Roach public function postAddIndividualAction(ServerRequestInterface $request): ResponseInterface 594c1010edaSGreg Roach { 59557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 596bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 597bed27cedSGreg Roach $option = $request->getParsedBody()['option']; 5985a78cd34SGreg Roach 5995a78cd34SGreg Roach $individual = Individual::getInstance($xref, $tree); 6005a78cd34SGreg Roach 6015a78cd34SGreg Roach if ($individual === null) { 60259f2f229SGreg Roach throw new IndividualNotFoundException(); 6035a78cd34SGreg Roach } 6045a78cd34SGreg Roach 6055a78cd34SGreg Roach switch ($option) { 6065a78cd34SGreg Roach case 'self': 6075a78cd34SGreg Roach $this->addRecordToCart($individual); 6085a78cd34SGreg Roach break; 6095a78cd34SGreg Roach 6105a78cd34SGreg Roach case 'parents': 61139ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 6125a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 6135a78cd34SGreg Roach } 6145a78cd34SGreg Roach break; 6155a78cd34SGreg Roach 6165a78cd34SGreg Roach case 'spouses': 61739ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 6185a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 6195a78cd34SGreg Roach } 6205a78cd34SGreg Roach break; 6215a78cd34SGreg Roach 6225a78cd34SGreg Roach case 'ancestors': 6235a78cd34SGreg Roach $this->addAncestorsToCart($individual); 6245a78cd34SGreg Roach break; 6255a78cd34SGreg Roach 6265a78cd34SGreg Roach case 'ancestor_families': 6275a78cd34SGreg Roach $this->addAncestorFamiliesToCart($individual); 6285a78cd34SGreg Roach break; 6295a78cd34SGreg Roach 6305a78cd34SGreg Roach case 'descendants': 63139ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 6325a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($family); 6335a78cd34SGreg Roach } 6345a78cd34SGreg Roach break; 6355a78cd34SGreg Roach } 6365a78cd34SGreg Roach 6376ccdf4f0SGreg Roach return redirect($individual->url()); 6385a78cd34SGreg Roach } 6395a78cd34SGreg Roach 6405a78cd34SGreg Roach /** 6415a78cd34SGreg Roach * @param Individual $individual 64218d7a90dSGreg Roach * 64318d7a90dSGreg Roach * @return void 6445a78cd34SGreg Roach */ 645e364afe4SGreg Roach private function addAncestorsToCart(Individual $individual): void 646c1010edaSGreg Roach { 6475a78cd34SGreg Roach $this->addRecordToCart($individual); 6485a78cd34SGreg Roach 64939ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 65039ca88baSGreg Roach foreach ($family->spouses() as $parent) { 6515a78cd34SGreg Roach $this->addAncestorsToCart($parent); 6525a78cd34SGreg Roach } 6535a78cd34SGreg Roach } 6545a78cd34SGreg Roach } 6555a78cd34SGreg Roach 6565a78cd34SGreg Roach /** 6575a78cd34SGreg Roach * @param Individual $individual 65818d7a90dSGreg Roach * 65918d7a90dSGreg Roach * @return void 6605a78cd34SGreg Roach */ 661e364afe4SGreg Roach private function addAncestorFamiliesToCart(Individual $individual): void 662c1010edaSGreg Roach { 66339ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 6645a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 66539ca88baSGreg Roach foreach ($family->spouses() as $parent) { 6665a78cd34SGreg Roach $this->addAncestorsToCart($parent); 6675a78cd34SGreg Roach } 6685a78cd34SGreg Roach } 6695a78cd34SGreg Roach } 6705a78cd34SGreg Roach 6715a78cd34SGreg Roach /** 6726ccdf4f0SGreg Roach * @param ServerRequestInterface $request 6735a78cd34SGreg Roach * 6746ccdf4f0SGreg Roach * @return ResponseInterface 6755a78cd34SGreg Roach */ 67657ab2231SGreg Roach public function getAddMediaAction(ServerRequestInterface $request): ResponseInterface 677c1010edaSGreg Roach { 67857ab2231SGreg Roach $tree = $request->getAttribute('tree'); 679bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 6805a78cd34SGreg Roach 6815a78cd34SGreg Roach $media = Media::getInstance($xref, $tree); 6825a78cd34SGreg Roach 6835a78cd34SGreg Roach if ($media === null) { 68459f2f229SGreg Roach throw new MediaNotFoundException(); 6855a78cd34SGreg Roach } 6865a78cd34SGreg Roach 6875a78cd34SGreg Roach $options = $this->mediaOptions($media); 6885a78cd34SGreg Roach 68939ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $media->fullName()); 6905a78cd34SGreg Roach 6915a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 69283615acfSGreg Roach 'action' => route('module', ['module' => $this->name(), 'action' => 'AddMedia']), 6935a78cd34SGreg Roach 'options' => $options, 6945a78cd34SGreg Roach 'default' => key($options), 6955a78cd34SGreg Roach 'record' => $media, 6965a78cd34SGreg Roach 'title' => $title, 6975a78cd34SGreg Roach 'tree' => $tree, 6985a78cd34SGreg Roach ]); 6995a78cd34SGreg Roach } 7005a78cd34SGreg Roach 7015a78cd34SGreg Roach /** 7025a78cd34SGreg Roach * @param Media $media 7035a78cd34SGreg Roach * 7045a78cd34SGreg Roach * @return string[] 7055a78cd34SGreg Roach */ 706c1010edaSGreg Roach private function mediaOptions(Media $media): array 707c1010edaSGreg Roach { 70839ca88baSGreg Roach $name = strip_tags($media->fullName()); 7095a78cd34SGreg Roach 7105a78cd34SGreg Roach return [ 7115a78cd34SGreg Roach 'self' => $name, 7125a78cd34SGreg Roach ]; 7135a78cd34SGreg Roach } 7145a78cd34SGreg Roach 7155a78cd34SGreg Roach /** 7166ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7175a78cd34SGreg Roach * 7186ccdf4f0SGreg Roach * @return ResponseInterface 7195a78cd34SGreg Roach */ 72057ab2231SGreg Roach public function postAddMediaAction(ServerRequestInterface $request): ResponseInterface 721c1010edaSGreg Roach { 72257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 723bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 7245a78cd34SGreg Roach 7255a78cd34SGreg Roach $media = Media::getInstance($xref, $tree); 7265a78cd34SGreg Roach 7275a78cd34SGreg Roach if ($media === null) { 72859f2f229SGreg Roach throw new MediaNotFoundException(); 7295a78cd34SGreg Roach } 7305a78cd34SGreg Roach 7315a78cd34SGreg Roach $this->addRecordToCart($media); 7325a78cd34SGreg Roach 7336ccdf4f0SGreg Roach return redirect($media->url()); 7345a78cd34SGreg Roach } 7355a78cd34SGreg Roach 7365a78cd34SGreg Roach /** 7376ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7385a78cd34SGreg Roach * 7396ccdf4f0SGreg Roach * @return ResponseInterface 7405a78cd34SGreg Roach */ 74157ab2231SGreg Roach public function getAddNoteAction(ServerRequestInterface $request): ResponseInterface 742c1010edaSGreg Roach { 74357ab2231SGreg Roach $tree = $request->getAttribute('tree'); 744bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 7455a78cd34SGreg Roach 7465a78cd34SGreg Roach $note = Note::getInstance($xref, $tree); 7475a78cd34SGreg Roach 7485a78cd34SGreg Roach if ($note === null) { 74959f2f229SGreg Roach throw new NoteNotFoundException(); 7505a78cd34SGreg Roach } 7515a78cd34SGreg Roach 7525a78cd34SGreg Roach $options = $this->noteOptions($note); 7535a78cd34SGreg Roach 75439ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $note->fullName()); 7555a78cd34SGreg Roach 7565a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 75783615acfSGreg Roach 'action' => route('module', ['module' => $this->name(), 'action' => 'AddNote']), 7585a78cd34SGreg Roach 'options' => $options, 7595a78cd34SGreg Roach 'default' => key($options), 7605a78cd34SGreg Roach 'record' => $note, 7615a78cd34SGreg Roach 'title' => $title, 7625a78cd34SGreg Roach 'tree' => $tree, 7635a78cd34SGreg Roach ]); 7645a78cd34SGreg Roach } 7655a78cd34SGreg Roach 7665a78cd34SGreg Roach /** 7675a78cd34SGreg Roach * @param Note $note 7685a78cd34SGreg Roach * 7695a78cd34SGreg Roach * @return string[] 7705a78cd34SGreg Roach */ 771c1010edaSGreg Roach private function noteOptions(Note $note): array 772c1010edaSGreg Roach { 77339ca88baSGreg Roach $name = strip_tags($note->fullName()); 7745a78cd34SGreg Roach 7755a78cd34SGreg Roach return [ 7765a78cd34SGreg Roach 'self' => $name, 7775a78cd34SGreg Roach ]; 7785a78cd34SGreg Roach } 7795a78cd34SGreg Roach 7805a78cd34SGreg Roach /** 7816ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7825a78cd34SGreg Roach * 7836ccdf4f0SGreg Roach * @return ResponseInterface 7845a78cd34SGreg Roach */ 78557ab2231SGreg Roach public function postAddNoteAction(ServerRequestInterface $request): ResponseInterface 786c1010edaSGreg Roach { 78757ab2231SGreg Roach $tree = $request->getAttribute('tree'); 788bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 7895a78cd34SGreg Roach 7905a78cd34SGreg Roach $note = Note::getInstance($xref, $tree); 7915a78cd34SGreg Roach 7925a78cd34SGreg Roach if ($note === null) { 79359f2f229SGreg Roach throw new NoteNotFoundException(); 7945a78cd34SGreg Roach } 7955a78cd34SGreg Roach 7965a78cd34SGreg Roach $this->addRecordToCart($note); 7975a78cd34SGreg Roach 7986ccdf4f0SGreg Roach return redirect($note->url()); 7995a78cd34SGreg Roach } 8005a78cd34SGreg Roach 8015a78cd34SGreg Roach /** 8026ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8035a78cd34SGreg Roach * 8046ccdf4f0SGreg Roach * @return ResponseInterface 8055a78cd34SGreg Roach */ 80657ab2231SGreg Roach public function getAddRepositoryAction(ServerRequestInterface $request): ResponseInterface 807c1010edaSGreg Roach { 80857ab2231SGreg Roach $tree = $request->getAttribute('tree'); 809bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8105a78cd34SGreg Roach 8115a78cd34SGreg Roach $repository = Repository::getInstance($xref, $tree); 8125a78cd34SGreg Roach 8135a78cd34SGreg Roach if ($repository === null) { 81459f2f229SGreg Roach throw new RepositoryNotFoundException(); 8155a78cd34SGreg Roach } 8165a78cd34SGreg Roach 8175a78cd34SGreg Roach $options = $this->repositoryOptions($repository); 8185a78cd34SGreg Roach 81939ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $repository->fullName()); 8205a78cd34SGreg Roach 8215a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 82283615acfSGreg Roach 'action' => route('module', ['module' => $this->name(), 'action' => 'AddRepository']), 8235a78cd34SGreg Roach 'options' => $options, 8245a78cd34SGreg Roach 'default' => key($options), 8255a78cd34SGreg Roach 'record' => $repository, 8265a78cd34SGreg Roach 'title' => $title, 8275a78cd34SGreg Roach 'tree' => $tree, 8285a78cd34SGreg Roach ]); 8295a78cd34SGreg Roach } 8305a78cd34SGreg Roach 8315a78cd34SGreg Roach /** 8325a78cd34SGreg Roach * @param Repository $repository 8335a78cd34SGreg Roach * 8345a78cd34SGreg Roach * @return string[] 8355a78cd34SGreg Roach */ 836c1010edaSGreg Roach private function repositoryOptions(Repository $repository): array 837c1010edaSGreg Roach { 83839ca88baSGreg Roach $name = strip_tags($repository->fullName()); 8395a78cd34SGreg Roach 8405a78cd34SGreg Roach return [ 8415a78cd34SGreg Roach 'self' => $name, 8425a78cd34SGreg Roach ]; 8435a78cd34SGreg Roach } 8445a78cd34SGreg Roach 8455a78cd34SGreg Roach /** 8466ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8475a78cd34SGreg Roach * 8486ccdf4f0SGreg Roach * @return ResponseInterface 8495a78cd34SGreg Roach */ 85057ab2231SGreg Roach public function postAddRepositoryAction(ServerRequestInterface $request): ResponseInterface 851c1010edaSGreg Roach { 85257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 853bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8545a78cd34SGreg Roach 8555a78cd34SGreg Roach $repository = Repository::getInstance($xref, $tree); 8565a78cd34SGreg Roach 8575a78cd34SGreg Roach if ($repository === null) { 85859f2f229SGreg Roach throw new RepositoryNotFoundException(); 8595a78cd34SGreg Roach } 8605a78cd34SGreg Roach 8615a78cd34SGreg Roach $this->addRecordToCart($repository); 8625a78cd34SGreg Roach 8636ccdf4f0SGreg Roach return redirect($repository->url()); 8645a78cd34SGreg Roach } 8655a78cd34SGreg Roach 8665a78cd34SGreg Roach /** 8676ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8685a78cd34SGreg Roach * 8696ccdf4f0SGreg Roach * @return ResponseInterface 8705a78cd34SGreg Roach */ 87157ab2231SGreg Roach public function getAddSourceAction(ServerRequestInterface $request): ResponseInterface 872c1010edaSGreg Roach { 87357ab2231SGreg Roach $tree = $request->getAttribute('tree'); 874bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8755a78cd34SGreg Roach 8765a78cd34SGreg Roach $source = Source::getInstance($xref, $tree); 8775a78cd34SGreg Roach 8785a78cd34SGreg Roach if ($source === null) { 87959f2f229SGreg Roach throw new SourceNotFoundException(); 8805a78cd34SGreg Roach } 8815a78cd34SGreg Roach 8825a78cd34SGreg Roach $options = $this->sourceOptions($source); 8835a78cd34SGreg Roach 88439ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $source->fullName()); 8855a78cd34SGreg Roach 8865a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 88783615acfSGreg Roach 'action' => route('module', ['module' => $this->name(), 'action' => 'AddSource']), 8885a78cd34SGreg Roach 'options' => $options, 8895a78cd34SGreg Roach 'default' => key($options), 8905a78cd34SGreg Roach 'record' => $source, 8915a78cd34SGreg Roach 'title' => $title, 8925a78cd34SGreg Roach 'tree' => $tree, 8935a78cd34SGreg Roach ]); 8945a78cd34SGreg Roach } 8955a78cd34SGreg Roach 8965a78cd34SGreg Roach /** 8975a78cd34SGreg Roach * @param Source $source 8985a78cd34SGreg Roach * 8995a78cd34SGreg Roach * @return string[] 9005a78cd34SGreg Roach */ 901c1010edaSGreg Roach private function sourceOptions(Source $source): array 902c1010edaSGreg Roach { 90339ca88baSGreg Roach $name = strip_tags($source->fullName()); 9045a78cd34SGreg Roach 9055a78cd34SGreg Roach return [ 90639ca88baSGreg Roach 'only' => strip_tags($source->fullName()), 9075a78cd34SGreg Roach 'linked' => I18N::translate('%s and the individuals that reference it.', $name), 9085a78cd34SGreg Roach ]; 9095a78cd34SGreg Roach } 9105a78cd34SGreg Roach 9115a78cd34SGreg Roach /** 9126ccdf4f0SGreg Roach * @param ServerRequestInterface $request 9135a78cd34SGreg Roach * 9146ccdf4f0SGreg Roach * @return ResponseInterface 9155a78cd34SGreg Roach */ 91657ab2231SGreg Roach public function postAddSourceAction(ServerRequestInterface $request): ResponseInterface 917c1010edaSGreg Roach { 91857ab2231SGreg Roach $tree = $request->getAttribute('tree'); 919bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 920bed27cedSGreg Roach $option = $request->getParsedBody()['option']; 9215a78cd34SGreg Roach 9225a78cd34SGreg Roach $source = Source::getInstance($xref, $tree); 9235a78cd34SGreg Roach 9245a78cd34SGreg Roach if ($source === null) { 92559f2f229SGreg Roach throw new SourceNotFoundException(); 9265a78cd34SGreg Roach } 9275a78cd34SGreg Roach 9285a78cd34SGreg Roach $this->addRecordToCart($source); 9295a78cd34SGreg Roach 9305a78cd34SGreg Roach if ($option === 'linked') { 9315a78cd34SGreg Roach foreach ($source->linkedIndividuals('SOUR') as $individual) { 9325a78cd34SGreg Roach $this->addRecordToCart($individual); 9335a78cd34SGreg Roach } 9345a78cd34SGreg Roach foreach ($source->linkedFamilies('SOUR') as $family) { 9355a78cd34SGreg Roach $this->addRecordToCart($family); 9365a78cd34SGreg Roach } 9375a78cd34SGreg Roach } 9385a78cd34SGreg Roach 9396ccdf4f0SGreg Roach return redirect($source->url()); 9405a78cd34SGreg Roach } 9415a78cd34SGreg Roach 9425a78cd34SGreg Roach /** 9435a78cd34SGreg Roach * Get all the records in the cart. 9445a78cd34SGreg Roach * 9455a78cd34SGreg Roach * @param Tree $tree 9465a78cd34SGreg Roach * 9475a78cd34SGreg Roach * @return GedcomRecord[] 9485a78cd34SGreg Roach */ 949c1010edaSGreg Roach private function allRecordsInCart(Tree $tree): array 950c1010edaSGreg Roach { 9515a78cd34SGreg Roach $cart = Session::get('cart', []); 9525a78cd34SGreg Roach 953aa6f03bbSGreg Roach $xrefs = array_keys($cart[$tree->name()] ?? []); 9545a78cd34SGreg Roach 9555a78cd34SGreg Roach // Fetch all the records in the cart. 956bed27cedSGreg Roach $records = array_map(static function (string $xref) use ($tree): ?GedcomRecord { 9575a78cd34SGreg Roach return GedcomRecord::getInstance($xref, $tree); 9585a78cd34SGreg Roach }, $xrefs); 9595a78cd34SGreg Roach 9605a78cd34SGreg Roach // Some records may have been deleted after they were added to the cart. 9615a78cd34SGreg Roach $records = array_filter($records); 9625a78cd34SGreg Roach 9635a78cd34SGreg Roach // Group and sort. 9640b5fd0a6SGreg Roach uasort($records, static function (GedcomRecord $x, GedcomRecord $y): int { 965c156e8f5SGreg Roach return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::nameComparator()($x, $y); 9665a78cd34SGreg Roach }); 9675a78cd34SGreg Roach 9685a78cd34SGreg Roach return $records; 9695a78cd34SGreg Roach } 9705a78cd34SGreg Roach 9715a78cd34SGreg Roach /** 9725a78cd34SGreg Roach * Add a record (and direclty linked sources, notes, etc. to the cart. 9735a78cd34SGreg Roach * 9745a78cd34SGreg Roach * @param GedcomRecord $record 97518d7a90dSGreg Roach * 97618d7a90dSGreg Roach * @return void 9775a78cd34SGreg Roach */ 978e364afe4SGreg Roach private function addRecordToCart(GedcomRecord $record): void 979c1010edaSGreg Roach { 9805a78cd34SGreg Roach $cart = Session::get('cart', []); 9815a78cd34SGreg Roach 982f4afa648SGreg Roach $tree_name = $record->tree()->name(); 9835a78cd34SGreg Roach 9845a78cd34SGreg Roach // Add this record 985c0935879SGreg Roach $cart[$tree_name][$record->xref()] = true; 9865a78cd34SGreg Roach 9875a78cd34SGreg Roach // Add directly linked media, notes, repositories and sources. 9888d0ebef0SGreg Roach preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches); 9895a78cd34SGreg Roach 9905a78cd34SGreg Roach foreach ($matches[1] as $match) { 9915a78cd34SGreg Roach $cart[$tree_name][$match] = true; 9925a78cd34SGreg Roach } 9935a78cd34SGreg Roach 9945a78cd34SGreg Roach Session::put('cart', $cart); 9955a78cd34SGreg Roach } 9965a78cd34SGreg Roach 9975a78cd34SGreg Roach /** 9985a78cd34SGreg Roach * @param Tree $tree 9995a78cd34SGreg Roach * 10005a78cd34SGreg Roach * @return bool 10015a78cd34SGreg Roach */ 1002c1010edaSGreg Roach private function isCartEmpty(Tree $tree): bool 1003c1010edaSGreg Roach { 10045a78cd34SGreg Roach $cart = Session::get('cart', []); 10055a78cd34SGreg Roach 1006aa6f03bbSGreg Roach return empty($cart[$tree->name()]); 10075a78cd34SGreg Roach } 10088c2e8227SGreg Roach} 1009