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 148eb235819SGreg Roach $route = $request->getQueryParams()['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)) { 159bed27cedSGreg Roach $xref = $request->getQueryParams()['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-'); 206*7f996f6eSGreg Roach $zip_adapter = new ZipArchiveAdapter($temp_zip_file); 207*7f996f6eSGreg 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. 305*7f996f6eSGreg 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', [ 42483615acfSGreg Roach 'action' => route('module', ['module' => $this->name(), 'action' => 'AddFamily']), 4255a78cd34SGreg Roach 'options' => $options, 4265a78cd34SGreg Roach 'default' => key($options), 4275a78cd34SGreg Roach 'record' => $family, 4285a78cd34SGreg Roach 'title' => $title, 4295a78cd34SGreg Roach 'tree' => $tree, 4305a78cd34SGreg Roach ]); 4315a78cd34SGreg Roach } 4325a78cd34SGreg Roach 4335a78cd34SGreg Roach /** 4345a78cd34SGreg Roach * @param Family $family 4355a78cd34SGreg Roach * 4365a78cd34SGreg Roach * @return string[] 4375a78cd34SGreg Roach */ 438c1010edaSGreg Roach private function familyOptions(Family $family): array 439c1010edaSGreg Roach { 44039ca88baSGreg Roach $name = strip_tags($family->fullName()); 4415a78cd34SGreg Roach 4425a78cd34SGreg Roach return [ 4435a78cd34SGreg Roach 'parents' => $name, 444bbb76c12SGreg Roach /* I18N: %s is a family (husband + wife) */ 445bbb76c12SGreg Roach 'members' => I18N::translate('%s and their children', $name), 446bbb76c12SGreg Roach /* I18N: %s is a family (husband + wife) */ 447bbb76c12SGreg Roach 'descendants' => I18N::translate('%s and their descendants', $name), 4485a78cd34SGreg Roach ]; 4495a78cd34SGreg Roach } 4505a78cd34SGreg Roach 4515a78cd34SGreg Roach /** 4526ccdf4f0SGreg Roach * @param ServerRequestInterface $request 4535a78cd34SGreg Roach * 4546ccdf4f0SGreg Roach * @return ResponseInterface 4555a78cd34SGreg Roach */ 45657ab2231SGreg Roach public function postAddFamilyAction(ServerRequestInterface $request): ResponseInterface 457c1010edaSGreg Roach { 45857ab2231SGreg Roach $tree = $request->getAttribute('tree'); 459bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 460bed27cedSGreg Roach $option = $request->getParsedBody()['option']; 4615a78cd34SGreg Roach 4625a78cd34SGreg Roach $family = Family::getInstance($xref, $tree); 4635a78cd34SGreg Roach 4645a78cd34SGreg Roach if ($family === null) { 46559f2f229SGreg Roach throw new FamilyNotFoundException(); 4665a78cd34SGreg Roach } 4675a78cd34SGreg Roach 4685a78cd34SGreg Roach switch ($option) { 4695a78cd34SGreg Roach case 'parents': 4705a78cd34SGreg Roach $this->addFamilyToCart($family); 4715a78cd34SGreg Roach break; 4725a78cd34SGreg Roach 4735a78cd34SGreg Roach case 'members': 4745a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 4755a78cd34SGreg Roach break; 4765a78cd34SGreg Roach 4775a78cd34SGreg Roach case 'descendants': 4785a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($family); 4795a78cd34SGreg Roach break; 4805a78cd34SGreg Roach } 4815a78cd34SGreg Roach 4826ccdf4f0SGreg Roach return redirect($family->url()); 4835a78cd34SGreg Roach } 4845a78cd34SGreg Roach 4855a78cd34SGreg Roach /** 4865a78cd34SGreg Roach * @param Family $family 48718d7a90dSGreg Roach * 48818d7a90dSGreg Roach * @return void 4895a78cd34SGreg Roach */ 490e364afe4SGreg Roach private function addFamilyToCart(Family $family): void 491c1010edaSGreg Roach { 4925a78cd34SGreg Roach $this->addRecordToCart($family); 4935a78cd34SGreg Roach 49439ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 4955a78cd34SGreg Roach $this->addRecordToCart($spouse); 4965a78cd34SGreg Roach } 4975a78cd34SGreg Roach } 4985a78cd34SGreg Roach 4995a78cd34SGreg Roach /** 5005a78cd34SGreg Roach * @param Family $family 50118d7a90dSGreg Roach * 50218d7a90dSGreg Roach * @return void 5035a78cd34SGreg Roach */ 504e364afe4SGreg Roach private function addFamilyAndChildrenToCart(Family $family): void 505c1010edaSGreg Roach { 5065a78cd34SGreg Roach $this->addRecordToCart($family); 5075a78cd34SGreg Roach 50839ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 5095a78cd34SGreg Roach $this->addRecordToCart($spouse); 5105a78cd34SGreg Roach } 51139ca88baSGreg Roach foreach ($family->children() as $child) { 5125a78cd34SGreg Roach $this->addRecordToCart($child); 5135a78cd34SGreg Roach } 5145a78cd34SGreg Roach } 5155a78cd34SGreg Roach 5165a78cd34SGreg Roach /** 5175a78cd34SGreg Roach * @param Family $family 51818d7a90dSGreg Roach * 51918d7a90dSGreg Roach * @return void 5205a78cd34SGreg Roach */ 521e364afe4SGreg Roach private function addFamilyAndDescendantsToCart(Family $family): void 522c1010edaSGreg Roach { 5235a78cd34SGreg Roach $this->addRecordToCart($family); 5245a78cd34SGreg Roach 52539ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 5265a78cd34SGreg Roach $this->addRecordToCart($spouse); 5275a78cd34SGreg Roach } 52839ca88baSGreg Roach foreach ($family->children() as $child) { 5295a78cd34SGreg Roach $this->addRecordToCart($child); 53039ca88baSGreg Roach foreach ($child->spouseFamilies() as $child_family) { 5315a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($child_family); 5325a78cd34SGreg Roach } 5335a78cd34SGreg Roach } 5345a78cd34SGreg Roach } 5355a78cd34SGreg Roach 5365a78cd34SGreg Roach /** 5376ccdf4f0SGreg Roach * @param ServerRequestInterface $request 5385a78cd34SGreg Roach * 5396ccdf4f0SGreg Roach * @return ResponseInterface 5405a78cd34SGreg Roach */ 54157ab2231SGreg Roach public function getAddIndividualAction(ServerRequestInterface $request): ResponseInterface 542c1010edaSGreg Roach { 54357ab2231SGreg Roach $tree = $request->getAttribute('tree'); 5445229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 5455229eadeSGreg Roach 546bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 5475a78cd34SGreg Roach 5485a78cd34SGreg Roach $individual = Individual::getInstance($xref, $tree); 5495a78cd34SGreg Roach 5505a78cd34SGreg Roach if ($individual === null) { 55159f2f229SGreg Roach throw new IndividualNotFoundException(); 5525a78cd34SGreg Roach } 5535a78cd34SGreg Roach 5545a78cd34SGreg Roach $options = $this->individualOptions($individual); 5555a78cd34SGreg Roach 55639ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $individual->fullName()); 5575a78cd34SGreg Roach 5585a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 55983615acfSGreg Roach 'action' => route('module', ['module' => $this->name(), 'action' => 'AddIndividual']), 5605a78cd34SGreg Roach 'options' => $options, 5615a78cd34SGreg Roach 'default' => key($options), 5625a78cd34SGreg Roach 'record' => $individual, 5635a78cd34SGreg Roach 'title' => $title, 5645a78cd34SGreg Roach 'tree' => $tree, 5655a78cd34SGreg Roach ]); 5665a78cd34SGreg Roach } 5675a78cd34SGreg Roach 5685a78cd34SGreg Roach /** 5695a78cd34SGreg Roach * @param Individual $individual 5705a78cd34SGreg Roach * 5715a78cd34SGreg Roach * @return string[] 5725a78cd34SGreg Roach */ 573c1010edaSGreg Roach private function individualOptions(Individual $individual): array 574c1010edaSGreg Roach { 57539ca88baSGreg Roach $name = strip_tags($individual->fullName()); 5765a78cd34SGreg Roach 57739ca88baSGreg Roach if ($individual->sex() === 'F') { 5785a78cd34SGreg Roach return [ 5795a78cd34SGreg Roach 'self' => $name, 5805a78cd34SGreg Roach 'parents' => I18N::translate('%s, her parents and siblings', $name), 5815a78cd34SGreg Roach 'spouses' => I18N::translate('%s, her spouses and children', $name), 5825a78cd34SGreg Roach 'ancestors' => I18N::translate('%s and her ancestors', $name), 5835a78cd34SGreg Roach 'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name), 5845a78cd34SGreg Roach 'descendants' => I18N::translate('%s, her spouses and descendants', $name), 5855a78cd34SGreg Roach ]; 586b2ce94c6SRico Sonntag } 587b2ce94c6SRico Sonntag 5885a78cd34SGreg Roach return [ 5895a78cd34SGreg Roach 'self' => $name, 5905a78cd34SGreg Roach 'parents' => I18N::translate('%s, his parents and siblings', $name), 5915a78cd34SGreg Roach 'spouses' => I18N::translate('%s, his spouses and children', $name), 5925a78cd34SGreg Roach 'ancestors' => I18N::translate('%s and his ancestors', $name), 5935a78cd34SGreg Roach 'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name), 5945a78cd34SGreg Roach 'descendants' => I18N::translate('%s, his spouses and descendants', $name), 5955a78cd34SGreg Roach ]; 5965a78cd34SGreg Roach } 5975a78cd34SGreg Roach 5985a78cd34SGreg Roach /** 5996ccdf4f0SGreg Roach * @param ServerRequestInterface $request 6005a78cd34SGreg Roach * 6016ccdf4f0SGreg Roach * @return ResponseInterface 6025a78cd34SGreg Roach */ 60357ab2231SGreg Roach public function postAddIndividualAction(ServerRequestInterface $request): ResponseInterface 604c1010edaSGreg Roach { 60557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 606bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 607bed27cedSGreg Roach $option = $request->getParsedBody()['option']; 6085a78cd34SGreg Roach 6095a78cd34SGreg Roach $individual = Individual::getInstance($xref, $tree); 6105a78cd34SGreg Roach 6115a78cd34SGreg Roach if ($individual === null) { 61259f2f229SGreg Roach throw new IndividualNotFoundException(); 6135a78cd34SGreg Roach } 6145a78cd34SGreg Roach 6155a78cd34SGreg Roach switch ($option) { 6165a78cd34SGreg Roach case 'self': 6175a78cd34SGreg Roach $this->addRecordToCart($individual); 6185a78cd34SGreg Roach break; 6195a78cd34SGreg Roach 6205a78cd34SGreg Roach case 'parents': 62139ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 6225a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 6235a78cd34SGreg Roach } 6245a78cd34SGreg Roach break; 6255a78cd34SGreg Roach 6265a78cd34SGreg Roach case 'spouses': 62739ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 6285a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 6295a78cd34SGreg Roach } 6305a78cd34SGreg Roach break; 6315a78cd34SGreg Roach 6325a78cd34SGreg Roach case 'ancestors': 6335a78cd34SGreg Roach $this->addAncestorsToCart($individual); 6345a78cd34SGreg Roach break; 6355a78cd34SGreg Roach 6365a78cd34SGreg Roach case 'ancestor_families': 6375a78cd34SGreg Roach $this->addAncestorFamiliesToCart($individual); 6385a78cd34SGreg Roach break; 6395a78cd34SGreg Roach 6405a78cd34SGreg Roach case 'descendants': 64139ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 6425a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($family); 6435a78cd34SGreg Roach } 6445a78cd34SGreg Roach break; 6455a78cd34SGreg Roach } 6465a78cd34SGreg Roach 6476ccdf4f0SGreg Roach return redirect($individual->url()); 6485a78cd34SGreg Roach } 6495a78cd34SGreg Roach 6505a78cd34SGreg Roach /** 6515a78cd34SGreg Roach * @param Individual $individual 65218d7a90dSGreg Roach * 65318d7a90dSGreg Roach * @return void 6545a78cd34SGreg Roach */ 655e364afe4SGreg Roach private function addAncestorsToCart(Individual $individual): void 656c1010edaSGreg Roach { 6575a78cd34SGreg Roach $this->addRecordToCart($individual); 6585a78cd34SGreg Roach 65939ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 66039ca88baSGreg Roach foreach ($family->spouses() as $parent) { 6615a78cd34SGreg Roach $this->addAncestorsToCart($parent); 6625a78cd34SGreg Roach } 6635a78cd34SGreg Roach } 6645a78cd34SGreg Roach } 6655a78cd34SGreg Roach 6665a78cd34SGreg Roach /** 6675a78cd34SGreg Roach * @param Individual $individual 66818d7a90dSGreg Roach * 66918d7a90dSGreg Roach * @return void 6705a78cd34SGreg Roach */ 671e364afe4SGreg Roach private function addAncestorFamiliesToCart(Individual $individual): void 672c1010edaSGreg Roach { 67339ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 6745a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 67539ca88baSGreg Roach foreach ($family->spouses() as $parent) { 6765a78cd34SGreg Roach $this->addAncestorsToCart($parent); 6775a78cd34SGreg Roach } 6785a78cd34SGreg Roach } 6795a78cd34SGreg Roach } 6805a78cd34SGreg Roach 6815a78cd34SGreg Roach /** 6826ccdf4f0SGreg Roach * @param ServerRequestInterface $request 6835a78cd34SGreg Roach * 6846ccdf4f0SGreg Roach * @return ResponseInterface 6855a78cd34SGreg Roach */ 68657ab2231SGreg Roach public function getAddMediaAction(ServerRequestInterface $request): ResponseInterface 687c1010edaSGreg Roach { 68857ab2231SGreg Roach $tree = $request->getAttribute('tree'); 6895229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 6905229eadeSGreg Roach 691bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 6925a78cd34SGreg Roach 6935a78cd34SGreg Roach $media = Media::getInstance($xref, $tree); 6945a78cd34SGreg Roach 6955a78cd34SGreg Roach if ($media === null) { 69659f2f229SGreg Roach throw new MediaNotFoundException(); 6975a78cd34SGreg Roach } 6985a78cd34SGreg Roach 6995a78cd34SGreg Roach $options = $this->mediaOptions($media); 7005a78cd34SGreg Roach 70139ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $media->fullName()); 7025a78cd34SGreg Roach 7035a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 70483615acfSGreg Roach 'action' => route('module', ['module' => $this->name(), 'action' => 'AddMedia']), 7055a78cd34SGreg Roach 'options' => $options, 7065a78cd34SGreg Roach 'default' => key($options), 7075a78cd34SGreg Roach 'record' => $media, 7085a78cd34SGreg Roach 'title' => $title, 7095a78cd34SGreg Roach 'tree' => $tree, 7105a78cd34SGreg Roach ]); 7115a78cd34SGreg Roach } 7125a78cd34SGreg Roach 7135a78cd34SGreg Roach /** 7145a78cd34SGreg Roach * @param Media $media 7155a78cd34SGreg Roach * 7165a78cd34SGreg Roach * @return string[] 7175a78cd34SGreg Roach */ 718c1010edaSGreg Roach private function mediaOptions(Media $media): array 719c1010edaSGreg Roach { 72039ca88baSGreg Roach $name = strip_tags($media->fullName()); 7215a78cd34SGreg Roach 7225a78cd34SGreg Roach return [ 7235a78cd34SGreg Roach 'self' => $name, 7245a78cd34SGreg Roach ]; 7255a78cd34SGreg Roach } 7265a78cd34SGreg Roach 7275a78cd34SGreg Roach /** 7286ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7295a78cd34SGreg Roach * 7306ccdf4f0SGreg Roach * @return ResponseInterface 7315a78cd34SGreg Roach */ 73257ab2231SGreg Roach public function postAddMediaAction(ServerRequestInterface $request): ResponseInterface 733c1010edaSGreg Roach { 73457ab2231SGreg Roach $tree = $request->getAttribute('tree'); 7355229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 7365229eadeSGreg Roach 737bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 7385a78cd34SGreg Roach 7395a78cd34SGreg Roach $media = Media::getInstance($xref, $tree); 7405a78cd34SGreg Roach 7415a78cd34SGreg Roach if ($media === null) { 74259f2f229SGreg Roach throw new MediaNotFoundException(); 7435a78cd34SGreg Roach } 7445a78cd34SGreg Roach 7455a78cd34SGreg Roach $this->addRecordToCart($media); 7465a78cd34SGreg Roach 7476ccdf4f0SGreg Roach return redirect($media->url()); 7485a78cd34SGreg Roach } 7495a78cd34SGreg Roach 7505a78cd34SGreg Roach /** 7516ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7525a78cd34SGreg Roach * 7536ccdf4f0SGreg Roach * @return ResponseInterface 7545a78cd34SGreg Roach */ 75557ab2231SGreg Roach public function getAddNoteAction(ServerRequestInterface $request): ResponseInterface 756c1010edaSGreg Roach { 75757ab2231SGreg Roach $tree = $request->getAttribute('tree'); 7585229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 7595229eadeSGreg Roach 760bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 7615a78cd34SGreg Roach 7625a78cd34SGreg Roach $note = Note::getInstance($xref, $tree); 7635a78cd34SGreg Roach 7645a78cd34SGreg Roach if ($note === null) { 76559f2f229SGreg Roach throw new NoteNotFoundException(); 7665a78cd34SGreg Roach } 7675a78cd34SGreg Roach 7685a78cd34SGreg Roach $options = $this->noteOptions($note); 7695a78cd34SGreg Roach 77039ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $note->fullName()); 7715a78cd34SGreg Roach 7725a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 77383615acfSGreg Roach 'action' => route('module', ['module' => $this->name(), 'action' => 'AddNote']), 7745a78cd34SGreg Roach 'options' => $options, 7755a78cd34SGreg Roach 'default' => key($options), 7765a78cd34SGreg Roach 'record' => $note, 7775a78cd34SGreg Roach 'title' => $title, 7785a78cd34SGreg Roach 'tree' => $tree, 7795a78cd34SGreg Roach ]); 7805a78cd34SGreg Roach } 7815a78cd34SGreg Roach 7825a78cd34SGreg Roach /** 7835a78cd34SGreg Roach * @param Note $note 7845a78cd34SGreg Roach * 7855a78cd34SGreg Roach * @return string[] 7865a78cd34SGreg Roach */ 787c1010edaSGreg Roach private function noteOptions(Note $note): array 788c1010edaSGreg Roach { 78939ca88baSGreg Roach $name = strip_tags($note->fullName()); 7905a78cd34SGreg Roach 7915a78cd34SGreg Roach return [ 7925a78cd34SGreg Roach 'self' => $name, 7935a78cd34SGreg Roach ]; 7945a78cd34SGreg Roach } 7955a78cd34SGreg Roach 7965a78cd34SGreg Roach /** 7976ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7985a78cd34SGreg Roach * 7996ccdf4f0SGreg Roach * @return ResponseInterface 8005a78cd34SGreg Roach */ 80157ab2231SGreg Roach public function postAddNoteAction(ServerRequestInterface $request): ResponseInterface 802c1010edaSGreg Roach { 80357ab2231SGreg Roach $tree = $request->getAttribute('tree'); 8045229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 8055229eadeSGreg Roach 806bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8075a78cd34SGreg Roach 8085a78cd34SGreg Roach $note = Note::getInstance($xref, $tree); 8095a78cd34SGreg Roach 8105a78cd34SGreg Roach if ($note === null) { 81159f2f229SGreg Roach throw new NoteNotFoundException(); 8125a78cd34SGreg Roach } 8135a78cd34SGreg Roach 8145a78cd34SGreg Roach $this->addRecordToCart($note); 8155a78cd34SGreg Roach 8166ccdf4f0SGreg Roach return redirect($note->url()); 8175a78cd34SGreg Roach } 8185a78cd34SGreg Roach 8195a78cd34SGreg Roach /** 8206ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8215a78cd34SGreg Roach * 8226ccdf4f0SGreg Roach * @return ResponseInterface 8235a78cd34SGreg Roach */ 82457ab2231SGreg Roach public function getAddRepositoryAction(ServerRequestInterface $request): ResponseInterface 825c1010edaSGreg Roach { 82657ab2231SGreg Roach $tree = $request->getAttribute('tree'); 8275229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 8285229eadeSGreg Roach 829bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8305a78cd34SGreg Roach 8315a78cd34SGreg Roach $repository = Repository::getInstance($xref, $tree); 8325a78cd34SGreg Roach 8335a78cd34SGreg Roach if ($repository === null) { 83459f2f229SGreg Roach throw new RepositoryNotFoundException(); 8355a78cd34SGreg Roach } 8365a78cd34SGreg Roach 8375a78cd34SGreg Roach $options = $this->repositoryOptions($repository); 8385a78cd34SGreg Roach 83939ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $repository->fullName()); 8405a78cd34SGreg Roach 8415a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 84283615acfSGreg Roach 'action' => route('module', ['module' => $this->name(), 'action' => 'AddRepository']), 8435a78cd34SGreg Roach 'options' => $options, 8445a78cd34SGreg Roach 'default' => key($options), 8455a78cd34SGreg Roach 'record' => $repository, 8465a78cd34SGreg Roach 'title' => $title, 8475a78cd34SGreg Roach 'tree' => $tree, 8485a78cd34SGreg Roach ]); 8495a78cd34SGreg Roach } 8505a78cd34SGreg Roach 8515a78cd34SGreg Roach /** 8525a78cd34SGreg Roach * @param Repository $repository 8535a78cd34SGreg Roach * 8545a78cd34SGreg Roach * @return string[] 8555a78cd34SGreg Roach */ 856c1010edaSGreg Roach private function repositoryOptions(Repository $repository): array 857c1010edaSGreg Roach { 85839ca88baSGreg Roach $name = strip_tags($repository->fullName()); 8595a78cd34SGreg Roach 8605a78cd34SGreg Roach return [ 8615a78cd34SGreg Roach 'self' => $name, 8625a78cd34SGreg Roach ]; 8635a78cd34SGreg Roach } 8645a78cd34SGreg Roach 8655a78cd34SGreg Roach /** 8666ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8675a78cd34SGreg Roach * 8686ccdf4f0SGreg Roach * @return ResponseInterface 8695a78cd34SGreg Roach */ 87057ab2231SGreg Roach public function postAddRepositoryAction(ServerRequestInterface $request): ResponseInterface 871c1010edaSGreg Roach { 87257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 8735229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 8745229eadeSGreg Roach 875bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8765a78cd34SGreg Roach 8775a78cd34SGreg Roach $repository = Repository::getInstance($xref, $tree); 8785a78cd34SGreg Roach 8795a78cd34SGreg Roach if ($repository === null) { 88059f2f229SGreg Roach throw new RepositoryNotFoundException(); 8815a78cd34SGreg Roach } 8825a78cd34SGreg Roach 8835a78cd34SGreg Roach $this->addRecordToCart($repository); 8845a78cd34SGreg Roach 8856ccdf4f0SGreg Roach return redirect($repository->url()); 8865a78cd34SGreg Roach } 8875a78cd34SGreg Roach 8885a78cd34SGreg Roach /** 8896ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8905a78cd34SGreg Roach * 8916ccdf4f0SGreg Roach * @return ResponseInterface 8925a78cd34SGreg Roach */ 89357ab2231SGreg Roach public function getAddSourceAction(ServerRequestInterface $request): ResponseInterface 894c1010edaSGreg Roach { 89557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 8965229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 8975229eadeSGreg Roach 898bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8995a78cd34SGreg Roach 9005a78cd34SGreg Roach $source = Source::getInstance($xref, $tree); 9015a78cd34SGreg Roach 9025a78cd34SGreg Roach if ($source === null) { 90359f2f229SGreg Roach throw new SourceNotFoundException(); 9045a78cd34SGreg Roach } 9055a78cd34SGreg Roach 9065a78cd34SGreg Roach $options = $this->sourceOptions($source); 9075a78cd34SGreg Roach 90839ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $source->fullName()); 9095a78cd34SGreg Roach 9105a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 91183615acfSGreg Roach 'action' => route('module', ['module' => $this->name(), 'action' => 'AddSource']), 9125a78cd34SGreg Roach 'options' => $options, 9135a78cd34SGreg Roach 'default' => key($options), 9145a78cd34SGreg Roach 'record' => $source, 9155a78cd34SGreg Roach 'title' => $title, 9165a78cd34SGreg Roach 'tree' => $tree, 9175a78cd34SGreg Roach ]); 9185a78cd34SGreg Roach } 9195a78cd34SGreg Roach 9205a78cd34SGreg Roach /** 9215a78cd34SGreg Roach * @param Source $source 9225a78cd34SGreg Roach * 9235a78cd34SGreg Roach * @return string[] 9245a78cd34SGreg Roach */ 925c1010edaSGreg Roach private function sourceOptions(Source $source): array 926c1010edaSGreg Roach { 92739ca88baSGreg Roach $name = strip_tags($source->fullName()); 9285a78cd34SGreg Roach 9295a78cd34SGreg Roach return [ 93039ca88baSGreg Roach 'only' => strip_tags($source->fullName()), 9315a78cd34SGreg Roach 'linked' => I18N::translate('%s and the individuals that reference it.', $name), 9325a78cd34SGreg Roach ]; 9335a78cd34SGreg Roach } 9345a78cd34SGreg Roach 9355a78cd34SGreg Roach /** 9366ccdf4f0SGreg Roach * @param ServerRequestInterface $request 9375a78cd34SGreg Roach * 9386ccdf4f0SGreg Roach * @return ResponseInterface 9395a78cd34SGreg Roach */ 94057ab2231SGreg Roach public function postAddSourceAction(ServerRequestInterface $request): ResponseInterface 941c1010edaSGreg Roach { 94257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 9435229eadeSGreg Roach assert($tree instanceof Tree, new InvalidArgumentException()); 9445229eadeSGreg Roach 945bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 946bed27cedSGreg Roach $option = $request->getParsedBody()['option']; 9475a78cd34SGreg Roach 9485a78cd34SGreg Roach $source = Source::getInstance($xref, $tree); 9495a78cd34SGreg Roach 9505a78cd34SGreg Roach if ($source === null) { 95159f2f229SGreg Roach throw new SourceNotFoundException(); 9525a78cd34SGreg Roach } 9535a78cd34SGreg Roach 9545a78cd34SGreg Roach $this->addRecordToCart($source); 9555a78cd34SGreg Roach 9565a78cd34SGreg Roach if ($option === 'linked') { 9575a78cd34SGreg Roach foreach ($source->linkedIndividuals('SOUR') as $individual) { 9585a78cd34SGreg Roach $this->addRecordToCart($individual); 9595a78cd34SGreg Roach } 9605a78cd34SGreg Roach foreach ($source->linkedFamilies('SOUR') as $family) { 9615a78cd34SGreg Roach $this->addRecordToCart($family); 9625a78cd34SGreg Roach } 9635a78cd34SGreg Roach } 9645a78cd34SGreg Roach 9656ccdf4f0SGreg Roach return redirect($source->url()); 9665a78cd34SGreg Roach } 9675a78cd34SGreg Roach 9685a78cd34SGreg Roach /** 9695a78cd34SGreg Roach * Get all the records in the cart. 9705a78cd34SGreg Roach * 9715a78cd34SGreg Roach * @param Tree $tree 9725a78cd34SGreg Roach * 9735a78cd34SGreg Roach * @return GedcomRecord[] 9745a78cd34SGreg Roach */ 975c1010edaSGreg Roach private function allRecordsInCart(Tree $tree): array 976c1010edaSGreg Roach { 9775a78cd34SGreg Roach $cart = Session::get('cart', []); 9785a78cd34SGreg Roach 979aa6f03bbSGreg Roach $xrefs = array_keys($cart[$tree->name()] ?? []); 9805a78cd34SGreg Roach 9815a78cd34SGreg Roach // Fetch all the records in the cart. 982bed27cedSGreg Roach $records = array_map(static function (string $xref) use ($tree): ?GedcomRecord { 9835a78cd34SGreg Roach return GedcomRecord::getInstance($xref, $tree); 9845a78cd34SGreg Roach }, $xrefs); 9855a78cd34SGreg Roach 9865a78cd34SGreg Roach // Some records may have been deleted after they were added to the cart. 9875a78cd34SGreg Roach $records = array_filter($records); 9885a78cd34SGreg Roach 9895a78cd34SGreg Roach // Group and sort. 9900b5fd0a6SGreg Roach uasort($records, static function (GedcomRecord $x, GedcomRecord $y): int { 991c156e8f5SGreg Roach return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::nameComparator()($x, $y); 9925a78cd34SGreg Roach }); 9935a78cd34SGreg Roach 9945a78cd34SGreg Roach return $records; 9955a78cd34SGreg Roach } 9965a78cd34SGreg Roach 9975a78cd34SGreg Roach /** 9985a78cd34SGreg Roach * Add a record (and direclty linked sources, notes, etc. to the cart. 9995a78cd34SGreg Roach * 10005a78cd34SGreg Roach * @param GedcomRecord $record 100118d7a90dSGreg Roach * 100218d7a90dSGreg Roach * @return void 10035a78cd34SGreg Roach */ 1004e364afe4SGreg Roach private function addRecordToCart(GedcomRecord $record): void 1005c1010edaSGreg Roach { 10065a78cd34SGreg Roach $cart = Session::get('cart', []); 10075a78cd34SGreg Roach 1008f4afa648SGreg Roach $tree_name = $record->tree()->name(); 10095a78cd34SGreg Roach 10105a78cd34SGreg Roach // Add this record 1011c0935879SGreg Roach $cart[$tree_name][$record->xref()] = true; 10125a78cd34SGreg Roach 10135a78cd34SGreg Roach // Add directly linked media, notes, repositories and sources. 10148d0ebef0SGreg Roach preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches); 10155a78cd34SGreg Roach 10165a78cd34SGreg Roach foreach ($matches[1] as $match) { 10175a78cd34SGreg Roach $cart[$tree_name][$match] = true; 10185a78cd34SGreg Roach } 10195a78cd34SGreg Roach 10205a78cd34SGreg Roach Session::put('cart', $cart); 10215a78cd34SGreg Roach } 10225a78cd34SGreg Roach 10235a78cd34SGreg Roach /** 10245a78cd34SGreg Roach * @param Tree $tree 10255a78cd34SGreg Roach * 10265a78cd34SGreg Roach * @return bool 10275a78cd34SGreg Roach */ 1028c1010edaSGreg Roach private function isCartEmpty(Tree $tree): bool 1029c1010edaSGreg Roach { 10305a78cd34SGreg Roach $cart = Session::get('cart', []); 10315a78cd34SGreg Roach 1032aa6f03bbSGreg Roach return empty($cart[$tree->name()]); 10335a78cd34SGreg Roach } 10348c2e8227SGreg Roach} 1035