18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 58fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98c2e8227SGreg Roach * (at your option) any later version. 108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138c2e8227SGreg Roach * GNU General Public License for more details. 148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 2176692c8bSGreg Roach 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 230bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\FamilyNotFoundException; 240bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException; 250bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\MediaNotFoundException; 260bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\NoteNotFoundException; 270bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\RepositoryNotFoundException; 280bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\SourceNotFoundException; 290e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family; 305a78cd34SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport; 315a78cd34SGreg Roachuse Fisharebest\Webtrees\Gedcom; 320e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 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; 435229eadeSGreg Roachuse InvalidArgumentException; 445a78cd34SGreg Roachuse League\Flysystem\Filesystem; 4561bf91b2SGreg Roachuse League\Flysystem\MountManager; 465a78cd34SGreg Roachuse League\Flysystem\ZipArchive\ZipArchiveAdapter; 47bed27cedSGreg Roachuse Psr\Http\Message\ResponseFactoryInterface; 486ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 496ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 506ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface; 513976b470SGreg Roach 52eb235819SGreg Roachuse function app; 53bf80ec58SGreg Roachuse function array_filter; 54bf80ec58SGreg Roachuse function array_keys; 55bf80ec58SGreg Roachuse function array_map; 565229eadeSGreg Roachuse function assert; 57bf80ec58SGreg Roachuse function in_array; 58bf80ec58SGreg Roachuse function key; 59bf80ec58SGreg Roachuse function preg_match_all; 60bf80ec58SGreg Roachuse function redirect; 61bf80ec58SGreg Roachuse function route; 62e5a6b4d4SGreg Roachuse function str_replace; 63bf80ec58SGreg Roachuse function strip_tags; 64bf80ec58SGreg Roachuse function sys_get_temp_dir; 65bf80ec58SGreg Roachuse function tempnam; 66bf80ec58SGreg Roachuse function ucfirst; 67bf80ec58SGreg Roachuse function utf8_decode; 688c2e8227SGreg Roach 698c2e8227SGreg Roach/** 708c2e8227SGreg Roach * Class ClippingsCartModule 718c2e8227SGreg Roach */ 7237eb8894SGreg Roachclass ClippingsCartModule extends AbstractModule implements ModuleMenuInterface 73c1010edaSGreg Roach{ 7449a243cbSGreg Roach use ModuleMenuTrait; 7549a243cbSGreg Roach 765a78cd34SGreg Roach // Routes that have a record which can be added to the clipboard 7716d6367aSGreg Roach private const ROUTES_WITH_RECORDS = [ 78c1010edaSGreg Roach 'family', 79c1010edaSGreg Roach 'individual', 80c1010edaSGreg Roach 'media', 81c1010edaSGreg Roach 'note', 82c1010edaSGreg Roach 'repository', 83c1010edaSGreg Roach 'source', 84c1010edaSGreg Roach ]; 855a78cd34SGreg Roach 8649a243cbSGreg Roach /** @var int The default access level for this module. It can be changed in the control panel. */ 8749a243cbSGreg Roach protected $access_level = Auth::PRIV_USER; 8849a243cbSGreg Roach 89961ec755SGreg Roach /** 90e5a6b4d4SGreg Roach * @var UserService 91e5a6b4d4SGreg Roach */ 92e5a6b4d4SGreg Roach private $user_service; 93e5a6b4d4SGreg Roach 94e5a6b4d4SGreg Roach /** 95e5a6b4d4SGreg Roach * ClippingsCartModule constructor. 96e5a6b4d4SGreg Roach * 97e5a6b4d4SGreg Roach * @param UserService $user_service 98e5a6b4d4SGreg Roach */ 99e5a6b4d4SGreg Roach public function __construct(UserService $user_service) 100e5a6b4d4SGreg Roach { 101e5a6b4d4SGreg Roach $this->user_service = $user_service; 102e5a6b4d4SGreg Roach } 103e5a6b4d4SGreg Roach 104e5a6b4d4SGreg Roach /** 1050cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 106961ec755SGreg Roach * 107961ec755SGreg Roach * @return string 108961ec755SGreg Roach */ 10949a243cbSGreg Roach public function title(): string 110c1010edaSGreg Roach { 111bbb76c12SGreg Roach /* I18N: Name of a module */ 112bbb76c12SGreg Roach return I18N::translate('Clippings cart'); 1138c2e8227SGreg Roach } 1148c2e8227SGreg Roach 115961ec755SGreg Roach /** 116961ec755SGreg Roach * A sentence describing what this module does. 117961ec755SGreg Roach * 118961ec755SGreg Roach * @return string 119961ec755SGreg Roach */ 12049a243cbSGreg Roach public function description(): string 121c1010edaSGreg Roach { 122bbb76c12SGreg Roach /* I18N: Description of the “Clippings cart” module */ 123bbb76c12SGreg Roach return I18N::translate('Select records from your family tree and save them as a GEDCOM file.'); 1248c2e8227SGreg Roach } 1258c2e8227SGreg Roach 1260ee13198SGreg Roach /** 12749a243cbSGreg Roach * The default position for this menu. It can be changed in the control panel. 1280ee13198SGreg Roach * 1290ee13198SGreg Roach * @return int 1300ee13198SGreg Roach */ 1318f53f488SRico Sonntag public function defaultMenuOrder(): int 132c1010edaSGreg Roach { 133353b36abSGreg Roach return 6; 1348c2e8227SGreg Roach } 1358c2e8227SGreg Roach 1360ee13198SGreg Roach /** 1370ee13198SGreg Roach * A menu, to be added to the main application menu. 1380ee13198SGreg Roach * 139aee13b6dSGreg Roach * @param Tree $tree 140aee13b6dSGreg Roach * 1410ee13198SGreg Roach * @return Menu|null 1420ee13198SGreg Roach */ 14346295629SGreg Roach public function getMenu(Tree $tree): ?Menu 144c1010edaSGreg Roach { 145eb235819SGreg Roach /** @var ServerRequestInterface $request */ 1466ccdf4f0SGreg Roach $request = app(ServerRequestInterface::class); 1478c2e8227SGreg Roach 148f7ab47b1SGreg Roach $route = $request->getAttribute('route'); 1495a78cd34SGreg Roach 1505a78cd34SGreg Roach $submenus = [ 15149a243cbSGreg Roach new Menu($this->title(), route('module', [ 15226684e68SGreg Roach 'module' => $this->name(), 153c1010edaSGreg Roach 'action' => 'Show', 154d72b284aSGreg Roach 'tree' => $tree->name(), 155c1010edaSGreg Roach ]), 'menu-clippings-cart', ['rel' => 'nofollow']), 1565a78cd34SGreg Roach ]; 1575a78cd34SGreg Roach 15822d65e5aSGreg Roach if (in_array($route, self::ROUTES_WITH_RECORDS, true)) { 159f7ab47b1SGreg Roach $xref = $request->getAttribute('xref'); 1605a78cd34SGreg Roach $action = 'Add' . ucfirst($route); 161c1010edaSGreg Roach $add_route = route('module', [ 16226684e68SGreg Roach 'module' => $this->name(), 163c1010edaSGreg Roach 'action' => $action, 164c1010edaSGreg Roach 'xref' => $xref, 165d72b284aSGreg Roach 'tree' => $tree->name(), 166c1010edaSGreg Roach ]); 1675a78cd34SGreg Roach 16825b2dde3SGreg Roach $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']); 1698c2e8227SGreg Roach } 170cbc1590aSGreg Roach 1715a78cd34SGreg Roach if (!$this->isCartEmpty($tree)) { 172c1010edaSGreg Roach $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [ 17326684e68SGreg Roach 'module' => $this->name(), 174c1010edaSGreg Roach 'action' => 'Empty', 175d72b284aSGreg Roach 'tree' => $tree->name(), 176c1010edaSGreg Roach ]), 'menu-clippings-empty', ['rel' => 'nofollow']); 177c1010edaSGreg Roach $submenus[] = new Menu(I18N::translate('Download'), route('module', [ 17826684e68SGreg Roach 'module' => $this->name(), 179c1010edaSGreg Roach 'action' => 'DownloadForm', 180d72b284aSGreg Roach 'tree' => $tree->name(), 181c1010edaSGreg Roach ]), 'menu-clippings-download', ['rel' => 'nofollow']); 1825a78cd34SGreg Roach } 1835a78cd34SGreg Roach 18449a243cbSGreg Roach return new Menu($this->title(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus); 1858c2e8227SGreg Roach } 1868c2e8227SGreg Roach 18776692c8bSGreg Roach /** 1886ccdf4f0SGreg Roach * @param ServerRequestInterface $request 18976692c8bSGreg Roach * 1906ccdf4f0SGreg Roach * @return ResponseInterface 19176692c8bSGreg Roach */ 19257ab2231SGreg Roach public function getDownloadAction(ServerRequestInterface $request): ResponseInterface 193c1010edaSGreg Roach { 19457ab2231SGreg Roach $tree = $request->getAttribute('tree'); 195bed27cedSGreg Roach $params = $request->getQueryParams(); 196bed27cedSGreg Roach 197bed27cedSGreg Roach $privatize_export = $params['privatize_export']; 198bed27cedSGreg Roach $convert = (bool) ($params['convert'] ?? false); 1998c2e8227SGreg Roach 20013abd6f3SGreg Roach $cart = Session::get('cart', []); 2018c2e8227SGreg Roach 202aa6f03bbSGreg Roach $xrefs = array_keys($cart[$tree->name()] ?? []); 2035a78cd34SGreg Roach 2045a78cd34SGreg Roach // Create a new/empty .ZIP file 2055a78cd34SGreg Roach $temp_zip_file = tempnam(sys_get_temp_dir(), 'webtrees-zip-'); 2067f996f6eSGreg Roach $zip_adapter = new ZipArchiveAdapter($temp_zip_file); 2077f996f6eSGreg Roach $zip_filesystem = new Filesystem($zip_adapter); 2085a78cd34SGreg Roach 20961bf91b2SGreg Roach $manager = new MountManager([ 21061bf91b2SGreg Roach 'media' => $tree->mediaFilesystem(), 21161bf91b2SGreg Roach 'zip' => $zip_filesystem, 21261bf91b2SGreg Roach ]); 21361bf91b2SGreg Roach 2145a78cd34SGreg Roach // Media file prefix 2155a78cd34SGreg Roach $path = $tree->getPreference('MEDIA_DIRECTORY'); 2165a78cd34SGreg Roach 2175a78cd34SGreg Roach // GEDCOM file header 218a3d8780cSGreg Roach $filetext = FunctionsExport::gedcomHeader($tree, $convert ? 'ANSI' : 'UTF-8'); 2195a78cd34SGreg Roach 2205a78cd34SGreg Roach switch ($privatize_export) { 2215a78cd34SGreg Roach case 'gedadmin': 2225a78cd34SGreg Roach $access_level = Auth::PRIV_NONE; 2235a78cd34SGreg Roach break; 2245a78cd34SGreg Roach case 'user': 2255a78cd34SGreg Roach $access_level = Auth::PRIV_USER; 2265a78cd34SGreg Roach break; 2275a78cd34SGreg Roach case 'visitor': 2285a78cd34SGreg Roach $access_level = Auth::PRIV_PRIVATE; 2295a78cd34SGreg Roach break; 2305a78cd34SGreg Roach case 'none': 2315a78cd34SGreg Roach default: 2325a78cd34SGreg Roach $access_level = Auth::PRIV_HIDE; 2335a78cd34SGreg Roach break; 2345a78cd34SGreg Roach } 2355a78cd34SGreg Roach 2365a78cd34SGreg Roach foreach ($xrefs as $xref) { 2375a78cd34SGreg Roach $object = GedcomRecord::getInstance($xref, $tree); 2385a78cd34SGreg Roach // The object may have been deleted since we added it to the cart.... 239bed27cedSGreg Roach if ($object instanceof GedcomRecord) { 2405a78cd34SGreg Roach $record = $object->privatizeGedcom($access_level); 2415a78cd34SGreg Roach // Remove links to objects that aren't in the cart 2428d0ebef0SGreg Roach preg_match_all('/\n1 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER); 2435a78cd34SGreg Roach foreach ($matches as $match) { 244bf80ec58SGreg Roach if (!in_array($match[1], $xrefs, true)) { 2455a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2465a78cd34SGreg Roach } 2475a78cd34SGreg Roach } 2488d0ebef0SGreg Roach preg_match_all('/\n2 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER); 2495a78cd34SGreg Roach foreach ($matches as $match) { 250bf80ec58SGreg Roach if (!in_array($match[1], $xrefs, true)) { 2515a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2525a78cd34SGreg Roach } 2535a78cd34SGreg Roach } 2548d0ebef0SGreg Roach preg_match_all('/\n3 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER); 2555a78cd34SGreg Roach foreach ($matches as $match) { 256bf80ec58SGreg Roach if (!in_array($match[1], $xrefs, true)) { 2575a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2585a78cd34SGreg Roach } 2595a78cd34SGreg Roach } 2605a78cd34SGreg Roach 26155167344SGreg Roach if ($object instanceof Individual || $object instanceof Family) { 2625a78cd34SGreg Roach $filetext .= $record . "\n"; 2635a78cd34SGreg Roach $filetext .= "1 SOUR @WEBTREES@\n"; 2641f273236SGreg Roach $filetext .= '2 PAGE ' . $object->url() . "\n"; 26555167344SGreg Roach } elseif ($object instanceof Source) { 2665a78cd34SGreg Roach $filetext .= $record . "\n"; 2671f273236SGreg Roach $filetext .= '1 NOTE ' . $object->url() . "\n"; 26855167344SGreg Roach } elseif ($object instanceof Media) { 26955167344SGreg Roach // Add the media files to the archive 2705a78cd34SGreg Roach foreach ($object->mediaFiles() as $media_file) { 27161bf91b2SGreg Roach $from = 'media://' . $media_file->filename(); 27261bf91b2SGreg Roach $to = 'zip://' . $path . $media_file->filename(); 27361bf91b2SGreg Roach if (!$media_file->isExternal() && $manager->has($from)) { 27461bf91b2SGreg Roach $manager->copy($from, $to); 2755a78cd34SGreg Roach } 2765a78cd34SGreg Roach } 2775a78cd34SGreg Roach $filetext .= $record . "\n"; 27855167344SGreg Roach } else { 2795a78cd34SGreg Roach $filetext .= $record . "\n"; 2808c2e8227SGreg Roach } 2818c2e8227SGreg Roach } 2828c2e8227SGreg Roach } 2838c2e8227SGreg Roach 2849b93b7c3SGreg Roach $base_url = $request->getAttribute('base_url'); 2859b93b7c3SGreg Roach 2865a78cd34SGreg Roach // Create a source, to indicate the source of the data. 2879b93b7c3SGreg Roach $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . $base_url . "\n"; 288e5a6b4d4SGreg Roach $author = $this->user_service->find((int) $tree->getPreference('CONTACT_USER_ID')); 2895a78cd34SGreg Roach if ($author !== null) { 290e5a6b4d4SGreg Roach $filetext .= '1 AUTH ' . $author->realName() . "\n"; 2915a78cd34SGreg Roach } 2925a78cd34SGreg Roach $filetext .= "0 TRLR\n"; 2935a78cd34SGreg Roach 2945a78cd34SGreg Roach // Make sure the preferred line endings are used 295a3d8780cSGreg Roach $filetext = str_replace('\n', Gedcom::EOL, $filetext); 2965a78cd34SGreg Roach 29755167344SGreg Roach if ($convert) { 2985a78cd34SGreg Roach $filetext = utf8_decode($filetext); 2998c2e8227SGreg Roach } 300cbc1590aSGreg Roach 3015a78cd34SGreg Roach // Finally add the GEDCOM file to the .ZIP file. 3025a78cd34SGreg Roach $zip_filesystem->write('clippings.ged', $filetext); 3035a78cd34SGreg Roach 30461bf91b2SGreg Roach // Need to force-close ZipArchive filesystems. 3057f996f6eSGreg Roach $zip_adapter->getArchive()->close(); 3065a78cd34SGreg Roach 3076ccdf4f0SGreg Roach // Use a stream, so that we do not have to load the entire file into memory. 3086ccdf4f0SGreg Roach $stream = app(StreamFactoryInterface::class)->createStreamFromFile($temp_zip_file); 3095a78cd34SGreg Roach 310bed27cedSGreg Roach /** @var ResponseFactoryInterface $response_factory */ 311bed27cedSGreg Roach $response_factory = app(ResponseFactoryInterface::class); 312bed27cedSGreg Roach 313bed27cedSGreg Roach return $response_factory->createResponse() 3146ccdf4f0SGreg Roach ->withBody($stream) 3151b3d4731SGreg Roach ->withHeader('Content-Type', 'application/zip') 316bed27cedSGreg Roach ->withHeader('Content-Disposition', 'attachment; filename="clippings.zip'); 3178c2e8227SGreg Roach } 3188c2e8227SGreg Roach 3198c2e8227SGreg Roach /** 32057ab2231SGreg Roach * @param ServerRequestInterface $request 32176692c8bSGreg Roach * 3226ccdf4f0SGreg Roach * @return ResponseInterface 3238c2e8227SGreg Roach */ 32457ab2231SGreg Roach public function getDownloadFormAction(ServerRequestInterface $request): ResponseInterface 325c1010edaSGreg Roach { 32657ab2231SGreg Roach $tree = $request->getAttribute('tree'); 32757ab2231SGreg Roach $user = $request->getAttribute('user'); 3285a78cd34SGreg Roach $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download'); 3298c2e8227SGreg Roach 3305a78cd34SGreg Roach return $this->viewResponse('modules/clippings/download', [ 3315a78cd34SGreg Roach 'is_manager' => Auth::isManager($tree, $user), 3325a78cd34SGreg Roach 'is_member' => Auth::isMember($tree, $user), 33371378461SGreg Roach 'module' => $this->name(), 3345a78cd34SGreg Roach 'title' => $title, 3355a78cd34SGreg Roach ]); 3368c2e8227SGreg Roach } 3378c2e8227SGreg Roach 3385a78cd34SGreg Roach /** 33957ab2231SGreg Roach * @param ServerRequestInterface $request 3405a78cd34SGreg Roach * 3416ccdf4f0SGreg Roach * @return ResponseInterface 3425a78cd34SGreg Roach */ 34357ab2231SGreg Roach public function getEmptyAction(ServerRequestInterface $request): ResponseInterface 344c1010edaSGreg Roach { 34557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 3465a78cd34SGreg Roach $cart = Session::get('cart', []); 347aa6f03bbSGreg Roach $cart[$tree->name()] = []; 3485a78cd34SGreg Roach Session::put('cart', $cart); 3498c2e8227SGreg Roach 350c1010edaSGreg Roach $url = route('module', [ 35126684e68SGreg Roach 'module' => $this->name(), 352c1010edaSGreg Roach 'action' => 'Show', 353d72b284aSGreg Roach 'tree' => $tree->name(), 354c1010edaSGreg Roach ]); 3555a78cd34SGreg Roach 3566ccdf4f0SGreg Roach return redirect($url); 3575a78cd34SGreg Roach } 3585a78cd34SGreg Roach 3595a78cd34SGreg Roach /** 3606ccdf4f0SGreg Roach * @param ServerRequestInterface $request 3615a78cd34SGreg Roach * 3626ccdf4f0SGreg Roach * @return ResponseInterface 3635a78cd34SGreg Roach */ 36457ab2231SGreg Roach public function postRemoveAction(ServerRequestInterface $request): ResponseInterface 365c1010edaSGreg Roach { 36657ab2231SGreg Roach $tree = $request->getAttribute('tree'); 3675229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 3685229eadeSGreg Roach 369bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 3705a78cd34SGreg Roach 3715a78cd34SGreg Roach $cart = Session::get('cart', []); 372aa6f03bbSGreg Roach unset($cart[$tree->name()][$xref]); 3735a78cd34SGreg Roach Session::put('cart', $cart); 3745a78cd34SGreg Roach 375c1010edaSGreg Roach $url = route('module', [ 37626684e68SGreg Roach 'module' => $this->name(), 377c1010edaSGreg Roach 'action' => 'Show', 378d72b284aSGreg Roach 'tree' => $tree->name(), 379c1010edaSGreg Roach ]); 3805a78cd34SGreg Roach 3816ccdf4f0SGreg Roach return redirect($url); 3825a78cd34SGreg Roach } 3835a78cd34SGreg Roach 3845a78cd34SGreg Roach /** 38557ab2231SGreg Roach * @param ServerRequestInterface $request 3865a78cd34SGreg Roach * 3876ccdf4f0SGreg Roach * @return ResponseInterface 3885a78cd34SGreg Roach */ 38957ab2231SGreg Roach public function getShowAction(ServerRequestInterface $request): ResponseInterface 390c1010edaSGreg Roach { 39157ab2231SGreg Roach $tree = $request->getAttribute('tree'); 3925229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 39357ab2231SGreg Roach 3945a78cd34SGreg Roach return $this->viewResponse('modules/clippings/show', [ 3955a78cd34SGreg Roach 'records' => $this->allRecordsInCart($tree), 3965a78cd34SGreg Roach 'title' => I18N::translate('Family tree clippings cart'), 3975a78cd34SGreg Roach 'tree' => $tree, 3985a78cd34SGreg Roach ]); 3995a78cd34SGreg Roach } 4005a78cd34SGreg Roach 4015a78cd34SGreg Roach /** 4026ccdf4f0SGreg Roach * @param ServerRequestInterface $request 4035a78cd34SGreg Roach * 4046ccdf4f0SGreg Roach * @return ResponseInterface 4055a78cd34SGreg Roach */ 40657ab2231SGreg Roach public function getAddFamilyAction(ServerRequestInterface $request): ResponseInterface 407c1010edaSGreg Roach { 40857ab2231SGreg Roach $tree = $request->getAttribute('tree'); 4095229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 4105229eadeSGreg Roach 411bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 4125a78cd34SGreg Roach 4135a78cd34SGreg Roach $family = Family::getInstance($xref, $tree); 4145a78cd34SGreg Roach 4155a78cd34SGreg Roach if ($family === null) { 41659f2f229SGreg Roach throw new FamilyNotFoundException(); 4175a78cd34SGreg Roach } 4185a78cd34SGreg Roach 4195a78cd34SGreg Roach $options = $this->familyOptions($family); 4205a78cd34SGreg Roach 42139ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $family->fullName()); 4225a78cd34SGreg Roach 4235a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 4245a78cd34SGreg Roach 'options' => $options, 4255a78cd34SGreg Roach 'default' => key($options), 4265a78cd34SGreg Roach 'record' => $family, 4275a78cd34SGreg Roach 'title' => $title, 4285a78cd34SGreg Roach 'tree' => $tree, 4295a78cd34SGreg Roach ]); 4305a78cd34SGreg Roach } 4315a78cd34SGreg Roach 4325a78cd34SGreg Roach /** 4335a78cd34SGreg Roach * @param Family $family 4345a78cd34SGreg Roach * 4355a78cd34SGreg Roach * @return string[] 4365a78cd34SGreg Roach */ 437c1010edaSGreg Roach private function familyOptions(Family $family): array 438c1010edaSGreg Roach { 43939ca88baSGreg Roach $name = strip_tags($family->fullName()); 4405a78cd34SGreg Roach 4415a78cd34SGreg Roach return [ 4425a78cd34SGreg Roach 'parents' => $name, 443bbb76c12SGreg Roach /* I18N: %s is a family (husband + wife) */ 444bbb76c12SGreg Roach 'members' => I18N::translate('%s and their children', $name), 445bbb76c12SGreg Roach /* I18N: %s is a family (husband + wife) */ 446bbb76c12SGreg Roach 'descendants' => I18N::translate('%s and their descendants', $name), 4475a78cd34SGreg Roach ]; 4485a78cd34SGreg Roach } 4495a78cd34SGreg Roach 4505a78cd34SGreg Roach /** 4516ccdf4f0SGreg Roach * @param ServerRequestInterface $request 4525a78cd34SGreg Roach * 4536ccdf4f0SGreg Roach * @return ResponseInterface 4545a78cd34SGreg Roach */ 45557ab2231SGreg Roach public function postAddFamilyAction(ServerRequestInterface $request): ResponseInterface 456c1010edaSGreg Roach { 45757ab2231SGreg Roach $tree = $request->getAttribute('tree'); 458bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 459bed27cedSGreg Roach $option = $request->getParsedBody()['option']; 4605a78cd34SGreg Roach 4615a78cd34SGreg Roach $family = Family::getInstance($xref, $tree); 4625a78cd34SGreg Roach 4635a78cd34SGreg Roach if ($family === null) { 46459f2f229SGreg Roach throw new FamilyNotFoundException(); 4655a78cd34SGreg Roach } 4665a78cd34SGreg Roach 4675a78cd34SGreg Roach switch ($option) { 4685a78cd34SGreg Roach case 'parents': 4695a78cd34SGreg Roach $this->addFamilyToCart($family); 4705a78cd34SGreg Roach break; 4715a78cd34SGreg Roach 4725a78cd34SGreg Roach case 'members': 4735a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 4745a78cd34SGreg Roach break; 4755a78cd34SGreg Roach 4765a78cd34SGreg Roach case 'descendants': 4775a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($family); 4785a78cd34SGreg Roach break; 4795a78cd34SGreg Roach } 4805a78cd34SGreg Roach 4816ccdf4f0SGreg Roach return redirect($family->url()); 4825a78cd34SGreg Roach } 4835a78cd34SGreg Roach 4845a78cd34SGreg Roach /** 4855a78cd34SGreg Roach * @param Family $family 48618d7a90dSGreg Roach * 48718d7a90dSGreg Roach * @return void 4885a78cd34SGreg Roach */ 489e364afe4SGreg Roach private function addFamilyToCart(Family $family): void 490c1010edaSGreg Roach { 4915a78cd34SGreg Roach $this->addRecordToCart($family); 4925a78cd34SGreg Roach 49339ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 4945a78cd34SGreg Roach $this->addRecordToCart($spouse); 4955a78cd34SGreg Roach } 4965a78cd34SGreg Roach } 4975a78cd34SGreg Roach 4985a78cd34SGreg Roach /** 4995a78cd34SGreg Roach * @param Family $family 50018d7a90dSGreg Roach * 50118d7a90dSGreg Roach * @return void 5025a78cd34SGreg Roach */ 503e364afe4SGreg Roach private function addFamilyAndChildrenToCart(Family $family): void 504c1010edaSGreg Roach { 5055a78cd34SGreg Roach $this->addRecordToCart($family); 5065a78cd34SGreg Roach 50739ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 5085a78cd34SGreg Roach $this->addRecordToCart($spouse); 5095a78cd34SGreg Roach } 51039ca88baSGreg Roach foreach ($family->children() as $child) { 5115a78cd34SGreg Roach $this->addRecordToCart($child); 5125a78cd34SGreg Roach } 5135a78cd34SGreg Roach } 5145a78cd34SGreg Roach 5155a78cd34SGreg Roach /** 5165a78cd34SGreg Roach * @param Family $family 51718d7a90dSGreg Roach * 51818d7a90dSGreg Roach * @return void 5195a78cd34SGreg Roach */ 520e364afe4SGreg Roach private function addFamilyAndDescendantsToCart(Family $family): void 521c1010edaSGreg Roach { 5225a78cd34SGreg Roach $this->addRecordToCart($family); 5235a78cd34SGreg Roach 52439ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 5255a78cd34SGreg Roach $this->addRecordToCart($spouse); 5265a78cd34SGreg Roach } 52739ca88baSGreg Roach foreach ($family->children() as $child) { 5285a78cd34SGreg Roach $this->addRecordToCart($child); 52939ca88baSGreg Roach foreach ($child->spouseFamilies() as $child_family) { 5305a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($child_family); 5315a78cd34SGreg Roach } 5325a78cd34SGreg Roach } 5335a78cd34SGreg Roach } 5345a78cd34SGreg Roach 5355a78cd34SGreg Roach /** 5366ccdf4f0SGreg Roach * @param ServerRequestInterface $request 5375a78cd34SGreg Roach * 5386ccdf4f0SGreg Roach * @return ResponseInterface 5395a78cd34SGreg Roach */ 54057ab2231SGreg Roach public function getAddIndividualAction(ServerRequestInterface $request): ResponseInterface 541c1010edaSGreg Roach { 54257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 5435229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 5445229eadeSGreg Roach 545bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 5465a78cd34SGreg Roach 5475a78cd34SGreg Roach $individual = Individual::getInstance($xref, $tree); 5485a78cd34SGreg Roach 5495a78cd34SGreg Roach if ($individual === null) { 55059f2f229SGreg Roach throw new IndividualNotFoundException(); 5515a78cd34SGreg Roach } 5525a78cd34SGreg Roach 5535a78cd34SGreg Roach $options = $this->individualOptions($individual); 5545a78cd34SGreg Roach 55539ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $individual->fullName()); 5565a78cd34SGreg Roach 5575a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 5585a78cd34SGreg Roach 'options' => $options, 5595a78cd34SGreg Roach 'default' => key($options), 5605a78cd34SGreg Roach 'record' => $individual, 5615a78cd34SGreg Roach 'title' => $title, 5625a78cd34SGreg Roach 'tree' => $tree, 5635a78cd34SGreg Roach ]); 5645a78cd34SGreg Roach } 5655a78cd34SGreg Roach 5665a78cd34SGreg Roach /** 5675a78cd34SGreg Roach * @param Individual $individual 5685a78cd34SGreg Roach * 5695a78cd34SGreg Roach * @return string[] 5705a78cd34SGreg Roach */ 571c1010edaSGreg Roach private function individualOptions(Individual $individual): array 572c1010edaSGreg Roach { 57339ca88baSGreg Roach $name = strip_tags($individual->fullName()); 5745a78cd34SGreg Roach 57539ca88baSGreg Roach if ($individual->sex() === 'F') { 5765a78cd34SGreg Roach return [ 5775a78cd34SGreg Roach 'self' => $name, 5785a78cd34SGreg Roach 'parents' => I18N::translate('%s, her parents and siblings', $name), 5795a78cd34SGreg Roach 'spouses' => I18N::translate('%s, her spouses and children', $name), 5805a78cd34SGreg Roach 'ancestors' => I18N::translate('%s and her ancestors', $name), 5815a78cd34SGreg Roach 'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name), 5825a78cd34SGreg Roach 'descendants' => I18N::translate('%s, her spouses and descendants', $name), 5835a78cd34SGreg Roach ]; 584b2ce94c6SRico Sonntag } 585b2ce94c6SRico Sonntag 5865a78cd34SGreg Roach return [ 5875a78cd34SGreg Roach 'self' => $name, 5885a78cd34SGreg Roach 'parents' => I18N::translate('%s, his parents and siblings', $name), 5895a78cd34SGreg Roach 'spouses' => I18N::translate('%s, his spouses and children', $name), 5905a78cd34SGreg Roach 'ancestors' => I18N::translate('%s and his ancestors', $name), 5915a78cd34SGreg Roach 'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name), 5925a78cd34SGreg Roach 'descendants' => I18N::translate('%s, his spouses and descendants', $name), 5935a78cd34SGreg Roach ]; 5945a78cd34SGreg Roach } 5955a78cd34SGreg Roach 5965a78cd34SGreg Roach /** 5976ccdf4f0SGreg Roach * @param ServerRequestInterface $request 5985a78cd34SGreg Roach * 5996ccdf4f0SGreg Roach * @return ResponseInterface 6005a78cd34SGreg Roach */ 60157ab2231SGreg Roach public function postAddIndividualAction(ServerRequestInterface $request): ResponseInterface 602c1010edaSGreg Roach { 60357ab2231SGreg Roach $tree = $request->getAttribute('tree'); 604bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 605bed27cedSGreg Roach $option = $request->getParsedBody()['option']; 6065a78cd34SGreg Roach 6075a78cd34SGreg Roach $individual = Individual::getInstance($xref, $tree); 6085a78cd34SGreg Roach 6095a78cd34SGreg Roach if ($individual === null) { 61059f2f229SGreg Roach throw new IndividualNotFoundException(); 6115a78cd34SGreg Roach } 6125a78cd34SGreg Roach 6135a78cd34SGreg Roach switch ($option) { 6145a78cd34SGreg Roach case 'self': 6155a78cd34SGreg Roach $this->addRecordToCart($individual); 6165a78cd34SGreg Roach break; 6175a78cd34SGreg Roach 6185a78cd34SGreg Roach case 'parents': 61939ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 6205a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 6215a78cd34SGreg Roach } 6225a78cd34SGreg Roach break; 6235a78cd34SGreg Roach 6245a78cd34SGreg Roach case 'spouses': 62539ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 6265a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 6275a78cd34SGreg Roach } 6285a78cd34SGreg Roach break; 6295a78cd34SGreg Roach 6305a78cd34SGreg Roach case 'ancestors': 6315a78cd34SGreg Roach $this->addAncestorsToCart($individual); 6325a78cd34SGreg Roach break; 6335a78cd34SGreg Roach 6345a78cd34SGreg Roach case 'ancestor_families': 6355a78cd34SGreg Roach $this->addAncestorFamiliesToCart($individual); 6365a78cd34SGreg Roach break; 6375a78cd34SGreg Roach 6385a78cd34SGreg Roach case 'descendants': 63939ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 6405a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($family); 6415a78cd34SGreg Roach } 6425a78cd34SGreg Roach break; 6435a78cd34SGreg Roach } 6445a78cd34SGreg Roach 6456ccdf4f0SGreg Roach return redirect($individual->url()); 6465a78cd34SGreg Roach } 6475a78cd34SGreg Roach 6485a78cd34SGreg Roach /** 6495a78cd34SGreg Roach * @param Individual $individual 65018d7a90dSGreg Roach * 65118d7a90dSGreg Roach * @return void 6525a78cd34SGreg Roach */ 653e364afe4SGreg Roach private function addAncestorsToCart(Individual $individual): void 654c1010edaSGreg Roach { 6555a78cd34SGreg Roach $this->addRecordToCart($individual); 6565a78cd34SGreg Roach 65739ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 65839ca88baSGreg Roach foreach ($family->spouses() as $parent) { 6595a78cd34SGreg Roach $this->addAncestorsToCart($parent); 6605a78cd34SGreg Roach } 6615a78cd34SGreg Roach } 6625a78cd34SGreg Roach } 6635a78cd34SGreg Roach 6645a78cd34SGreg Roach /** 6655a78cd34SGreg Roach * @param Individual $individual 66618d7a90dSGreg Roach * 66718d7a90dSGreg Roach * @return void 6685a78cd34SGreg Roach */ 669e364afe4SGreg Roach private function addAncestorFamiliesToCart(Individual $individual): void 670c1010edaSGreg Roach { 67139ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 6725a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 67339ca88baSGreg Roach foreach ($family->spouses() as $parent) { 6745a78cd34SGreg Roach $this->addAncestorsToCart($parent); 6755a78cd34SGreg Roach } 6765a78cd34SGreg Roach } 6775a78cd34SGreg Roach } 6785a78cd34SGreg Roach 6795a78cd34SGreg Roach /** 6806ccdf4f0SGreg Roach * @param ServerRequestInterface $request 6815a78cd34SGreg Roach * 6826ccdf4f0SGreg Roach * @return ResponseInterface 6835a78cd34SGreg Roach */ 68457ab2231SGreg Roach public function getAddMediaAction(ServerRequestInterface $request): ResponseInterface 685c1010edaSGreg Roach { 68657ab2231SGreg Roach $tree = $request->getAttribute('tree'); 6875229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 6885229eadeSGreg Roach 689bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 6905a78cd34SGreg Roach 6915a78cd34SGreg Roach $media = Media::getInstance($xref, $tree); 6925a78cd34SGreg Roach 6935a78cd34SGreg Roach if ($media === null) { 69459f2f229SGreg Roach throw new MediaNotFoundException(); 6955a78cd34SGreg Roach } 6965a78cd34SGreg Roach 6975a78cd34SGreg Roach $options = $this->mediaOptions($media); 6985a78cd34SGreg Roach 69939ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $media->fullName()); 7005a78cd34SGreg Roach 7015a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 7025a78cd34SGreg Roach 'options' => $options, 7035a78cd34SGreg Roach 'default' => key($options), 7045a78cd34SGreg Roach 'record' => $media, 7055a78cd34SGreg Roach 'title' => $title, 7065a78cd34SGreg Roach 'tree' => $tree, 7075a78cd34SGreg Roach ]); 7085a78cd34SGreg Roach } 7095a78cd34SGreg Roach 7105a78cd34SGreg Roach /** 7115a78cd34SGreg Roach * @param Media $media 7125a78cd34SGreg Roach * 7135a78cd34SGreg Roach * @return string[] 7145a78cd34SGreg Roach */ 715c1010edaSGreg Roach private function mediaOptions(Media $media): array 716c1010edaSGreg Roach { 71739ca88baSGreg Roach $name = strip_tags($media->fullName()); 7185a78cd34SGreg Roach 7195a78cd34SGreg Roach return [ 7205a78cd34SGreg Roach 'self' => $name, 7215a78cd34SGreg Roach ]; 7225a78cd34SGreg Roach } 7235a78cd34SGreg Roach 7245a78cd34SGreg Roach /** 7256ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7265a78cd34SGreg Roach * 7276ccdf4f0SGreg Roach * @return ResponseInterface 7285a78cd34SGreg Roach */ 72957ab2231SGreg Roach public function postAddMediaAction(ServerRequestInterface $request): ResponseInterface 730c1010edaSGreg Roach { 73157ab2231SGreg Roach $tree = $request->getAttribute('tree'); 7325229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 7335229eadeSGreg Roach 734bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 7355a78cd34SGreg Roach 7365a78cd34SGreg Roach $media = Media::getInstance($xref, $tree); 7375a78cd34SGreg Roach 7385a78cd34SGreg Roach if ($media === null) { 73959f2f229SGreg Roach throw new MediaNotFoundException(); 7405a78cd34SGreg Roach } 7415a78cd34SGreg Roach 7425a78cd34SGreg Roach $this->addRecordToCart($media); 7435a78cd34SGreg Roach 7446ccdf4f0SGreg Roach return redirect($media->url()); 7455a78cd34SGreg Roach } 7465a78cd34SGreg Roach 7475a78cd34SGreg Roach /** 7486ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7495a78cd34SGreg Roach * 7506ccdf4f0SGreg Roach * @return ResponseInterface 7515a78cd34SGreg Roach */ 75257ab2231SGreg Roach public function getAddNoteAction(ServerRequestInterface $request): ResponseInterface 753c1010edaSGreg Roach { 75457ab2231SGreg Roach $tree = $request->getAttribute('tree'); 7555229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 7565229eadeSGreg Roach 757bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 7585a78cd34SGreg Roach 7595a78cd34SGreg Roach $note = Note::getInstance($xref, $tree); 7605a78cd34SGreg Roach 7615a78cd34SGreg Roach if ($note === null) { 76259f2f229SGreg Roach throw new NoteNotFoundException(); 7635a78cd34SGreg Roach } 7645a78cd34SGreg Roach 7655a78cd34SGreg Roach $options = $this->noteOptions($note); 7665a78cd34SGreg Roach 76739ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $note->fullName()); 7685a78cd34SGreg Roach 7695a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 7705a78cd34SGreg Roach 'options' => $options, 7715a78cd34SGreg Roach 'default' => key($options), 7725a78cd34SGreg Roach 'record' => $note, 7735a78cd34SGreg Roach 'title' => $title, 7745a78cd34SGreg Roach 'tree' => $tree, 7755a78cd34SGreg Roach ]); 7765a78cd34SGreg Roach } 7775a78cd34SGreg Roach 7785a78cd34SGreg Roach /** 7795a78cd34SGreg Roach * @param Note $note 7805a78cd34SGreg Roach * 7815a78cd34SGreg Roach * @return string[] 7825a78cd34SGreg Roach */ 783c1010edaSGreg Roach private function noteOptions(Note $note): array 784c1010edaSGreg Roach { 78539ca88baSGreg Roach $name = strip_tags($note->fullName()); 7865a78cd34SGreg Roach 7875a78cd34SGreg Roach return [ 7885a78cd34SGreg Roach 'self' => $name, 7895a78cd34SGreg Roach ]; 7905a78cd34SGreg Roach } 7915a78cd34SGreg Roach 7925a78cd34SGreg Roach /** 7936ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7945a78cd34SGreg Roach * 7956ccdf4f0SGreg Roach * @return ResponseInterface 7965a78cd34SGreg Roach */ 79757ab2231SGreg Roach public function postAddNoteAction(ServerRequestInterface $request): ResponseInterface 798c1010edaSGreg Roach { 79957ab2231SGreg Roach $tree = $request->getAttribute('tree'); 8005229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 8015229eadeSGreg Roach 802bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8035a78cd34SGreg Roach 8045a78cd34SGreg Roach $note = Note::getInstance($xref, $tree); 8055a78cd34SGreg Roach 8065a78cd34SGreg Roach if ($note === null) { 80759f2f229SGreg Roach throw new NoteNotFoundException(); 8085a78cd34SGreg Roach } 8095a78cd34SGreg Roach 8105a78cd34SGreg Roach $this->addRecordToCart($note); 8115a78cd34SGreg Roach 8126ccdf4f0SGreg Roach return redirect($note->url()); 8135a78cd34SGreg Roach } 8145a78cd34SGreg Roach 8155a78cd34SGreg Roach /** 8166ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8175a78cd34SGreg Roach * 8186ccdf4f0SGreg Roach * @return ResponseInterface 8195a78cd34SGreg Roach */ 82057ab2231SGreg Roach public function getAddRepositoryAction(ServerRequestInterface $request): ResponseInterface 821c1010edaSGreg Roach { 82257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 8235229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 8245229eadeSGreg Roach 825bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8265a78cd34SGreg Roach 8275a78cd34SGreg Roach $repository = Repository::getInstance($xref, $tree); 8285a78cd34SGreg Roach 8295a78cd34SGreg Roach if ($repository === null) { 83059f2f229SGreg Roach throw new RepositoryNotFoundException(); 8315a78cd34SGreg Roach } 8325a78cd34SGreg Roach 8335a78cd34SGreg Roach $options = $this->repositoryOptions($repository); 8345a78cd34SGreg Roach 83539ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $repository->fullName()); 8365a78cd34SGreg Roach 8375a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 8385a78cd34SGreg Roach 'options' => $options, 8395a78cd34SGreg Roach 'default' => key($options), 8405a78cd34SGreg Roach 'record' => $repository, 8415a78cd34SGreg Roach 'title' => $title, 8425a78cd34SGreg Roach 'tree' => $tree, 8435a78cd34SGreg Roach ]); 8445a78cd34SGreg Roach } 8455a78cd34SGreg Roach 8465a78cd34SGreg Roach /** 8475a78cd34SGreg Roach * @param Repository $repository 8485a78cd34SGreg Roach * 8495a78cd34SGreg Roach * @return string[] 8505a78cd34SGreg Roach */ 851c1010edaSGreg Roach private function repositoryOptions(Repository $repository): array 852c1010edaSGreg Roach { 85339ca88baSGreg Roach $name = strip_tags($repository->fullName()); 8545a78cd34SGreg Roach 8555a78cd34SGreg Roach return [ 8565a78cd34SGreg Roach 'self' => $name, 8575a78cd34SGreg Roach ]; 8585a78cd34SGreg Roach } 8595a78cd34SGreg Roach 8605a78cd34SGreg Roach /** 8616ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8625a78cd34SGreg Roach * 8636ccdf4f0SGreg Roach * @return ResponseInterface 8645a78cd34SGreg Roach */ 86557ab2231SGreg Roach public function postAddRepositoryAction(ServerRequestInterface $request): ResponseInterface 866c1010edaSGreg Roach { 86757ab2231SGreg Roach $tree = $request->getAttribute('tree'); 8685229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 8695229eadeSGreg Roach 870bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8715a78cd34SGreg Roach 8725a78cd34SGreg Roach $repository = Repository::getInstance($xref, $tree); 8735a78cd34SGreg Roach 8745a78cd34SGreg Roach if ($repository === null) { 87559f2f229SGreg Roach throw new RepositoryNotFoundException(); 8765a78cd34SGreg Roach } 8775a78cd34SGreg Roach 8785a78cd34SGreg Roach $this->addRecordToCart($repository); 8795a78cd34SGreg Roach 8806ccdf4f0SGreg Roach return redirect($repository->url()); 8815a78cd34SGreg Roach } 8825a78cd34SGreg Roach 8835a78cd34SGreg Roach /** 8846ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8855a78cd34SGreg Roach * 8866ccdf4f0SGreg Roach * @return ResponseInterface 8875a78cd34SGreg Roach */ 88857ab2231SGreg Roach public function getAddSourceAction(ServerRequestInterface $request): ResponseInterface 889c1010edaSGreg Roach { 89057ab2231SGreg Roach $tree = $request->getAttribute('tree'); 8915229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 8925229eadeSGreg Roach 893bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8945a78cd34SGreg Roach 8955a78cd34SGreg Roach $source = Source::getInstance($xref, $tree); 8965a78cd34SGreg Roach 8975a78cd34SGreg Roach if ($source === null) { 89859f2f229SGreg Roach throw new SourceNotFoundException(); 8995a78cd34SGreg Roach } 9005a78cd34SGreg Roach 9015a78cd34SGreg Roach $options = $this->sourceOptions($source); 9025a78cd34SGreg Roach 90339ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $source->fullName()); 9045a78cd34SGreg Roach 9055a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 9065a78cd34SGreg Roach 'options' => $options, 9075a78cd34SGreg Roach 'default' => key($options), 9085a78cd34SGreg Roach 'record' => $source, 9095a78cd34SGreg Roach 'title' => $title, 9105a78cd34SGreg Roach 'tree' => $tree, 9115a78cd34SGreg Roach ]); 9125a78cd34SGreg Roach } 9135a78cd34SGreg Roach 9145a78cd34SGreg Roach /** 9155a78cd34SGreg Roach * @param Source $source 9165a78cd34SGreg Roach * 9175a78cd34SGreg Roach * @return string[] 9185a78cd34SGreg Roach */ 919c1010edaSGreg Roach private function sourceOptions(Source $source): array 920c1010edaSGreg Roach { 92139ca88baSGreg Roach $name = strip_tags($source->fullName()); 9225a78cd34SGreg Roach 9235a78cd34SGreg Roach return [ 92439ca88baSGreg Roach 'only' => strip_tags($source->fullName()), 9255a78cd34SGreg Roach 'linked' => I18N::translate('%s and the individuals that reference it.', $name), 9265a78cd34SGreg Roach ]; 9275a78cd34SGreg Roach } 9285a78cd34SGreg Roach 9295a78cd34SGreg Roach /** 9306ccdf4f0SGreg Roach * @param ServerRequestInterface $request 9315a78cd34SGreg Roach * 9326ccdf4f0SGreg Roach * @return ResponseInterface 9335a78cd34SGreg Roach */ 93457ab2231SGreg Roach public function postAddSourceAction(ServerRequestInterface $request): ResponseInterface 935c1010edaSGreg Roach { 93657ab2231SGreg Roach $tree = $request->getAttribute('tree'); 9375229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 9385229eadeSGreg Roach 939bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 940bed27cedSGreg Roach $option = $request->getParsedBody()['option']; 9415a78cd34SGreg Roach 9425a78cd34SGreg Roach $source = Source::getInstance($xref, $tree); 9435a78cd34SGreg Roach 9445a78cd34SGreg Roach if ($source === null) { 94559f2f229SGreg Roach throw new SourceNotFoundException(); 9465a78cd34SGreg Roach } 9475a78cd34SGreg Roach 9485a78cd34SGreg Roach $this->addRecordToCart($source); 9495a78cd34SGreg Roach 9505a78cd34SGreg Roach if ($option === 'linked') { 9515a78cd34SGreg Roach foreach ($source->linkedIndividuals('SOUR') as $individual) { 9525a78cd34SGreg Roach $this->addRecordToCart($individual); 9535a78cd34SGreg Roach } 9545a78cd34SGreg Roach foreach ($source->linkedFamilies('SOUR') as $family) { 9555a78cd34SGreg Roach $this->addRecordToCart($family); 9565a78cd34SGreg Roach } 9575a78cd34SGreg Roach } 9585a78cd34SGreg Roach 9596ccdf4f0SGreg Roach return redirect($source->url()); 9605a78cd34SGreg Roach } 9615a78cd34SGreg Roach 9625a78cd34SGreg Roach /** 9635a78cd34SGreg Roach * Get all the records in the cart. 9645a78cd34SGreg Roach * 9655a78cd34SGreg Roach * @param Tree $tree 9665a78cd34SGreg Roach * 9675a78cd34SGreg Roach * @return GedcomRecord[] 9685a78cd34SGreg Roach */ 969c1010edaSGreg Roach private function allRecordsInCart(Tree $tree): array 970c1010edaSGreg Roach { 9715a78cd34SGreg Roach $cart = Session::get('cart', []); 9725a78cd34SGreg Roach 973aa6f03bbSGreg Roach $xrefs = array_keys($cart[$tree->name()] ?? []); 9745a78cd34SGreg Roach 9755a78cd34SGreg Roach // Fetch all the records in the cart. 976bed27cedSGreg Roach $records = array_map(static function (string $xref) use ($tree): ?GedcomRecord { 9775a78cd34SGreg Roach return GedcomRecord::getInstance($xref, $tree); 9785a78cd34SGreg Roach }, $xrefs); 9795a78cd34SGreg Roach 9805a78cd34SGreg Roach // Some records may have been deleted after they were added to the cart. 9815a78cd34SGreg Roach $records = array_filter($records); 9825a78cd34SGreg Roach 9835a78cd34SGreg Roach // Group and sort. 9840b5fd0a6SGreg Roach uasort($records, static function (GedcomRecord $x, GedcomRecord $y): int { 985c156e8f5SGreg Roach return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::nameComparator()($x, $y); 9865a78cd34SGreg Roach }); 9875a78cd34SGreg Roach 9885a78cd34SGreg Roach return $records; 9895a78cd34SGreg Roach } 9905a78cd34SGreg Roach 9915a78cd34SGreg Roach /** 9925a78cd34SGreg Roach * Add a record (and direclty linked sources, notes, etc. to the cart. 9935a78cd34SGreg Roach * 9945a78cd34SGreg Roach * @param GedcomRecord $record 99518d7a90dSGreg Roach * 99618d7a90dSGreg Roach * @return void 9975a78cd34SGreg Roach */ 998e364afe4SGreg Roach private function addRecordToCart(GedcomRecord $record): void 999c1010edaSGreg Roach { 10005a78cd34SGreg Roach $cart = Session::get('cart', []); 10015a78cd34SGreg Roach 1002f4afa648SGreg Roach $tree_name = $record->tree()->name(); 10035a78cd34SGreg Roach 10045a78cd34SGreg Roach // Add this record 1005c0935879SGreg Roach $cart[$tree_name][$record->xref()] = true; 10065a78cd34SGreg Roach 10075a78cd34SGreg Roach // Add directly linked media, notes, repositories and sources. 10088d0ebef0SGreg Roach preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches); 10095a78cd34SGreg Roach 10105a78cd34SGreg Roach foreach ($matches[1] as $match) { 10115a78cd34SGreg Roach $cart[$tree_name][$match] = true; 10125a78cd34SGreg Roach } 10135a78cd34SGreg Roach 10145a78cd34SGreg Roach Session::put('cart', $cart); 10155a78cd34SGreg Roach } 10165a78cd34SGreg Roach 10175a78cd34SGreg Roach /** 10185a78cd34SGreg Roach * @param Tree $tree 10195a78cd34SGreg Roach * 10205a78cd34SGreg Roach * @return bool 10215a78cd34SGreg Roach */ 1022c1010edaSGreg Roach private function isCartEmpty(Tree $tree): bool 1023c1010edaSGreg Roach { 10245a78cd34SGreg Roach $cart = Session::get('cart', []); 1025*a91af26aSGreg Roach $contents = $cart[$tree->name()] ?? []; 10265a78cd34SGreg Roach 1027*a91af26aSGreg Roach return $contents === []; 10285a78cd34SGreg Roach } 10298c2e8227SGreg Roach} 1030