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; 435a78cd34SGreg Roachuse League\Flysystem\Filesystem; 4461bf91b2SGreg Roachuse League\Flysystem\MountManager; 455a78cd34SGreg Roachuse League\Flysystem\ZipArchive\ZipArchiveAdapter; 46bed27cedSGreg Roachuse Psr\Http\Message\ResponseFactoryInterface; 476ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 486ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 496ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface; 503976b470SGreg Roach 51eb235819SGreg Roachuse function app; 52bf80ec58SGreg Roachuse function array_filter; 53bf80ec58SGreg Roachuse function array_keys; 54bf80ec58SGreg Roachuse function array_map; 555229eadeSGreg Roachuse function assert; 56bf80ec58SGreg Roachuse function in_array; 57bf80ec58SGreg Roachuse function key; 58bf80ec58SGreg Roachuse function preg_match_all; 59bf80ec58SGreg Roachuse function redirect; 60bf80ec58SGreg Roachuse function route; 61e5a6b4d4SGreg Roachuse function str_replace; 62bf80ec58SGreg Roachuse function strip_tags; 63bf80ec58SGreg Roachuse function sys_get_temp_dir; 64bf80ec58SGreg Roachuse function tempnam; 65bf80ec58SGreg Roachuse function ucfirst; 66bf80ec58SGreg Roachuse function utf8_decode; 678c2e8227SGreg Roach 688c2e8227SGreg Roach/** 698c2e8227SGreg Roach * Class ClippingsCartModule 708c2e8227SGreg Roach */ 7137eb8894SGreg Roachclass ClippingsCartModule extends AbstractModule implements ModuleMenuInterface 72c1010edaSGreg Roach{ 7349a243cbSGreg Roach use ModuleMenuTrait; 7449a243cbSGreg Roach 755a78cd34SGreg Roach // Routes that have a record which can be added to the clipboard 7616d6367aSGreg Roach private const ROUTES_WITH_RECORDS = [ 77c1010edaSGreg Roach 'family', 78c1010edaSGreg Roach 'individual', 79c1010edaSGreg Roach 'media', 80c1010edaSGreg Roach 'note', 81c1010edaSGreg Roach 'repository', 82c1010edaSGreg Roach 'source', 83c1010edaSGreg Roach ]; 845a78cd34SGreg Roach 8549a243cbSGreg Roach /** @var int The default access level for this module. It can be changed in the control panel. */ 8649a243cbSGreg Roach protected $access_level = Auth::PRIV_USER; 8749a243cbSGreg Roach 88961ec755SGreg Roach /** 89e5a6b4d4SGreg Roach * @var UserService 90e5a6b4d4SGreg Roach */ 91e5a6b4d4SGreg Roach private $user_service; 92e5a6b4d4SGreg Roach 93e5a6b4d4SGreg Roach /** 94e5a6b4d4SGreg Roach * ClippingsCartModule constructor. 95e5a6b4d4SGreg Roach * 96e5a6b4d4SGreg Roach * @param UserService $user_service 97e5a6b4d4SGreg Roach */ 98e5a6b4d4SGreg Roach public function __construct(UserService $user_service) 99e5a6b4d4SGreg Roach { 100e5a6b4d4SGreg Roach $this->user_service = $user_service; 101e5a6b4d4SGreg Roach } 102e5a6b4d4SGreg Roach 103e5a6b4d4SGreg Roach /** 1040cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 105961ec755SGreg Roach * 106961ec755SGreg Roach * @return string 107961ec755SGreg Roach */ 10849a243cbSGreg Roach public function title(): string 109c1010edaSGreg Roach { 110bbb76c12SGreg Roach /* I18N: Name of a module */ 111bbb76c12SGreg Roach return I18N::translate('Clippings cart'); 1128c2e8227SGreg Roach } 1138c2e8227SGreg Roach 114961ec755SGreg Roach /** 115961ec755SGreg Roach * A sentence describing what this module does. 116961ec755SGreg Roach * 117961ec755SGreg Roach * @return string 118961ec755SGreg Roach */ 11949a243cbSGreg Roach public function description(): string 120c1010edaSGreg Roach { 121bbb76c12SGreg Roach /* I18N: Description of the “Clippings cart” module */ 122bbb76c12SGreg Roach return I18N::translate('Select records from your family tree and save them as a GEDCOM file.'); 1238c2e8227SGreg Roach } 1248c2e8227SGreg Roach 1250ee13198SGreg Roach /** 12649a243cbSGreg Roach * The default position for this menu. It can be changed in the control panel. 1270ee13198SGreg Roach * 1280ee13198SGreg Roach * @return int 1290ee13198SGreg Roach */ 1308f53f488SRico Sonntag public function defaultMenuOrder(): int 131c1010edaSGreg Roach { 132353b36abSGreg Roach return 6; 1338c2e8227SGreg Roach } 1348c2e8227SGreg Roach 1350ee13198SGreg Roach /** 1360ee13198SGreg Roach * A menu, to be added to the main application menu. 1370ee13198SGreg Roach * 138aee13b6dSGreg Roach * @param Tree $tree 139aee13b6dSGreg Roach * 1400ee13198SGreg Roach * @return Menu|null 1410ee13198SGreg Roach */ 14246295629SGreg Roach public function getMenu(Tree $tree): ?Menu 143c1010edaSGreg Roach { 144eb235819SGreg Roach /** @var ServerRequestInterface $request */ 1456ccdf4f0SGreg Roach $request = app(ServerRequestInterface::class); 1468c2e8227SGreg Roach 147f7ab47b1SGreg Roach $route = $request->getAttribute('route'); 1485a78cd34SGreg Roach 1495a78cd34SGreg Roach $submenus = [ 15049a243cbSGreg Roach new Menu($this->title(), route('module', [ 15126684e68SGreg Roach 'module' => $this->name(), 152c1010edaSGreg Roach 'action' => 'Show', 153d72b284aSGreg Roach 'tree' => $tree->name(), 154c1010edaSGreg Roach ]), 'menu-clippings-cart', ['rel' => 'nofollow']), 1555a78cd34SGreg Roach ]; 1565a78cd34SGreg Roach 15722d65e5aSGreg Roach if (in_array($route, self::ROUTES_WITH_RECORDS, true)) { 158f7ab47b1SGreg Roach $xref = $request->getAttribute('xref'); 1595a78cd34SGreg Roach $action = 'Add' . ucfirst($route); 160c1010edaSGreg Roach $add_route = route('module', [ 16126684e68SGreg Roach 'module' => $this->name(), 162c1010edaSGreg Roach 'action' => $action, 163c1010edaSGreg Roach 'xref' => $xref, 164d72b284aSGreg Roach 'tree' => $tree->name(), 165c1010edaSGreg Roach ]); 1665a78cd34SGreg Roach 16725b2dde3SGreg Roach $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']); 1688c2e8227SGreg Roach } 169cbc1590aSGreg Roach 1705a78cd34SGreg Roach if (!$this->isCartEmpty($tree)) { 171c1010edaSGreg Roach $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [ 17226684e68SGreg Roach 'module' => $this->name(), 173c1010edaSGreg Roach 'action' => 'Empty', 174d72b284aSGreg Roach 'tree' => $tree->name(), 175c1010edaSGreg Roach ]), 'menu-clippings-empty', ['rel' => 'nofollow']); 176c1010edaSGreg Roach $submenus[] = new Menu(I18N::translate('Download'), route('module', [ 17726684e68SGreg Roach 'module' => $this->name(), 178c1010edaSGreg Roach 'action' => 'DownloadForm', 179d72b284aSGreg Roach 'tree' => $tree->name(), 180c1010edaSGreg Roach ]), 'menu-clippings-download', ['rel' => 'nofollow']); 1815a78cd34SGreg Roach } 1825a78cd34SGreg Roach 18349a243cbSGreg Roach return new Menu($this->title(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus); 1848c2e8227SGreg Roach } 1858c2e8227SGreg Roach 18676692c8bSGreg Roach /** 1876ccdf4f0SGreg Roach * @param ServerRequestInterface $request 18876692c8bSGreg Roach * 1896ccdf4f0SGreg Roach * @return ResponseInterface 19076692c8bSGreg Roach */ 19157ab2231SGreg Roach public function getDownloadAction(ServerRequestInterface $request): ResponseInterface 192c1010edaSGreg Roach { 19357ab2231SGreg Roach $tree = $request->getAttribute('tree'); 194bed27cedSGreg Roach $params = $request->getQueryParams(); 195bed27cedSGreg Roach 196bed27cedSGreg Roach $privatize_export = $params['privatize_export']; 197bed27cedSGreg Roach $convert = (bool) ($params['convert'] ?? false); 1988c2e8227SGreg Roach 19913abd6f3SGreg Roach $cart = Session::get('cart', []); 2008c2e8227SGreg Roach 201aa6f03bbSGreg Roach $xrefs = array_keys($cart[$tree->name()] ?? []); 2025a78cd34SGreg Roach 2035a78cd34SGreg Roach // Create a new/empty .ZIP file 2045a78cd34SGreg Roach $temp_zip_file = tempnam(sys_get_temp_dir(), 'webtrees-zip-'); 2057f996f6eSGreg Roach $zip_adapter = new ZipArchiveAdapter($temp_zip_file); 2067f996f6eSGreg Roach $zip_filesystem = new Filesystem($zip_adapter); 2075a78cd34SGreg Roach 20861bf91b2SGreg Roach $manager = new MountManager([ 20961bf91b2SGreg Roach 'media' => $tree->mediaFilesystem(), 21061bf91b2SGreg Roach 'zip' => $zip_filesystem, 21161bf91b2SGreg Roach ]); 21261bf91b2SGreg Roach 2135a78cd34SGreg Roach // Media file prefix 2145a78cd34SGreg Roach $path = $tree->getPreference('MEDIA_DIRECTORY'); 2155a78cd34SGreg Roach 2165a78cd34SGreg Roach // GEDCOM file header 217a3d8780cSGreg Roach $filetext = FunctionsExport::gedcomHeader($tree, $convert ? 'ANSI' : 'UTF-8'); 2185a78cd34SGreg Roach 2195a78cd34SGreg Roach switch ($privatize_export) { 2205a78cd34SGreg Roach case 'gedadmin': 2215a78cd34SGreg Roach $access_level = Auth::PRIV_NONE; 2225a78cd34SGreg Roach break; 2235a78cd34SGreg Roach case 'user': 2245a78cd34SGreg Roach $access_level = Auth::PRIV_USER; 2255a78cd34SGreg Roach break; 2265a78cd34SGreg Roach case 'visitor': 2275a78cd34SGreg Roach $access_level = Auth::PRIV_PRIVATE; 2285a78cd34SGreg Roach break; 2295a78cd34SGreg Roach case 'none': 2305a78cd34SGreg Roach default: 2315a78cd34SGreg Roach $access_level = Auth::PRIV_HIDE; 2325a78cd34SGreg Roach break; 2335a78cd34SGreg Roach } 2345a78cd34SGreg Roach 2355a78cd34SGreg Roach foreach ($xrefs as $xref) { 2365a78cd34SGreg Roach $object = GedcomRecord::getInstance($xref, $tree); 2375a78cd34SGreg Roach // The object may have been deleted since we added it to the cart.... 238bed27cedSGreg Roach if ($object instanceof GedcomRecord) { 2395a78cd34SGreg Roach $record = $object->privatizeGedcom($access_level); 2405a78cd34SGreg Roach // Remove links to objects that aren't in the cart 2418d0ebef0SGreg Roach preg_match_all('/\n1 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER); 2425a78cd34SGreg Roach foreach ($matches as $match) { 243bf80ec58SGreg Roach if (!in_array($match[1], $xrefs, true)) { 2445a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2455a78cd34SGreg Roach } 2465a78cd34SGreg Roach } 2478d0ebef0SGreg Roach preg_match_all('/\n2 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER); 2485a78cd34SGreg Roach foreach ($matches as $match) { 249bf80ec58SGreg Roach if (!in_array($match[1], $xrefs, true)) { 2505a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2515a78cd34SGreg Roach } 2525a78cd34SGreg Roach } 2538d0ebef0SGreg Roach preg_match_all('/\n3 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER); 2545a78cd34SGreg Roach foreach ($matches as $match) { 255bf80ec58SGreg Roach if (!in_array($match[1], $xrefs, true)) { 2565a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2575a78cd34SGreg Roach } 2585a78cd34SGreg Roach } 2595a78cd34SGreg Roach 26055167344SGreg Roach if ($object instanceof Individual || $object instanceof Family) { 2615a78cd34SGreg Roach $filetext .= $record . "\n"; 2625a78cd34SGreg Roach $filetext .= "1 SOUR @WEBTREES@\n"; 2631f273236SGreg Roach $filetext .= '2 PAGE ' . $object->url() . "\n"; 26455167344SGreg Roach } elseif ($object instanceof Source) { 2655a78cd34SGreg Roach $filetext .= $record . "\n"; 2661f273236SGreg Roach $filetext .= '1 NOTE ' . $object->url() . "\n"; 26755167344SGreg Roach } elseif ($object instanceof Media) { 26855167344SGreg Roach // Add the media files to the archive 2695a78cd34SGreg Roach foreach ($object->mediaFiles() as $media_file) { 27061bf91b2SGreg Roach $from = 'media://' . $media_file->filename(); 27161bf91b2SGreg Roach $to = 'zip://' . $path . $media_file->filename(); 27261bf91b2SGreg Roach if (!$media_file->isExternal() && $manager->has($from)) { 27361bf91b2SGreg Roach $manager->copy($from, $to); 2745a78cd34SGreg Roach } 2755a78cd34SGreg Roach } 2765a78cd34SGreg Roach $filetext .= $record . "\n"; 27755167344SGreg Roach } else { 2785a78cd34SGreg Roach $filetext .= $record . "\n"; 2798c2e8227SGreg Roach } 2808c2e8227SGreg Roach } 2818c2e8227SGreg Roach } 2828c2e8227SGreg Roach 2839b93b7c3SGreg Roach $base_url = $request->getAttribute('base_url'); 2849b93b7c3SGreg Roach 2855a78cd34SGreg Roach // Create a source, to indicate the source of the data. 2869b93b7c3SGreg Roach $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . $base_url . "\n"; 287e5a6b4d4SGreg Roach $author = $this->user_service->find((int) $tree->getPreference('CONTACT_USER_ID')); 2885a78cd34SGreg Roach if ($author !== null) { 289e5a6b4d4SGreg Roach $filetext .= '1 AUTH ' . $author->realName() . "\n"; 2905a78cd34SGreg Roach } 2915a78cd34SGreg Roach $filetext .= "0 TRLR\n"; 2925a78cd34SGreg Roach 2935a78cd34SGreg Roach // Make sure the preferred line endings are used 294a3d8780cSGreg Roach $filetext = str_replace('\n', Gedcom::EOL, $filetext); 2955a78cd34SGreg Roach 29655167344SGreg Roach if ($convert) { 2975a78cd34SGreg Roach $filetext = utf8_decode($filetext); 2988c2e8227SGreg Roach } 299cbc1590aSGreg Roach 3005a78cd34SGreg Roach // Finally add the GEDCOM file to the .ZIP file. 3015a78cd34SGreg Roach $zip_filesystem->write('clippings.ged', $filetext); 3025a78cd34SGreg Roach 30361bf91b2SGreg Roach // Need to force-close ZipArchive filesystems. 3047f996f6eSGreg Roach $zip_adapter->getArchive()->close(); 3055a78cd34SGreg Roach 3066ccdf4f0SGreg Roach // Use a stream, so that we do not have to load the entire file into memory. 3076ccdf4f0SGreg Roach $stream = app(StreamFactoryInterface::class)->createStreamFromFile($temp_zip_file); 3085a78cd34SGreg Roach 309bed27cedSGreg Roach /** @var ResponseFactoryInterface $response_factory */ 310bed27cedSGreg Roach $response_factory = app(ResponseFactoryInterface::class); 311bed27cedSGreg Roach 312bed27cedSGreg Roach return $response_factory->createResponse() 3136ccdf4f0SGreg Roach ->withBody($stream) 3141b3d4731SGreg Roach ->withHeader('Content-Type', 'application/zip') 315bed27cedSGreg Roach ->withHeader('Content-Disposition', 'attachment; filename="clippings.zip'); 3168c2e8227SGreg Roach } 3178c2e8227SGreg Roach 3188c2e8227SGreg Roach /** 31957ab2231SGreg Roach * @param ServerRequestInterface $request 32076692c8bSGreg Roach * 3216ccdf4f0SGreg Roach * @return ResponseInterface 3228c2e8227SGreg Roach */ 32357ab2231SGreg Roach public function getDownloadFormAction(ServerRequestInterface $request): ResponseInterface 324c1010edaSGreg Roach { 32557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 32657ab2231SGreg Roach $user = $request->getAttribute('user'); 3275a78cd34SGreg Roach $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download'); 3288c2e8227SGreg Roach 3295a78cd34SGreg Roach return $this->viewResponse('modules/clippings/download', [ 3305a78cd34SGreg Roach 'is_manager' => Auth::isManager($tree, $user), 3315a78cd34SGreg Roach 'is_member' => Auth::isMember($tree, $user), 33271378461SGreg Roach 'module' => $this->name(), 3335a78cd34SGreg Roach 'title' => $title, 3345a78cd34SGreg Roach ]); 3358c2e8227SGreg Roach } 3368c2e8227SGreg Roach 3375a78cd34SGreg Roach /** 33857ab2231SGreg Roach * @param ServerRequestInterface $request 3395a78cd34SGreg Roach * 3406ccdf4f0SGreg Roach * @return ResponseInterface 3415a78cd34SGreg Roach */ 34257ab2231SGreg Roach public function getEmptyAction(ServerRequestInterface $request): ResponseInterface 343c1010edaSGreg Roach { 34457ab2231SGreg Roach $tree = $request->getAttribute('tree'); 3455a78cd34SGreg Roach $cart = Session::get('cart', []); 346aa6f03bbSGreg Roach $cart[$tree->name()] = []; 3475a78cd34SGreg Roach Session::put('cart', $cart); 3488c2e8227SGreg Roach 349c1010edaSGreg Roach $url = route('module', [ 35026684e68SGreg Roach 'module' => $this->name(), 351c1010edaSGreg Roach 'action' => 'Show', 352d72b284aSGreg Roach 'tree' => $tree->name(), 353c1010edaSGreg Roach ]); 3545a78cd34SGreg Roach 3556ccdf4f0SGreg Roach return redirect($url); 3565a78cd34SGreg Roach } 3575a78cd34SGreg Roach 3585a78cd34SGreg Roach /** 3596ccdf4f0SGreg Roach * @param ServerRequestInterface $request 3605a78cd34SGreg Roach * 3616ccdf4f0SGreg Roach * @return ResponseInterface 3625a78cd34SGreg Roach */ 36357ab2231SGreg Roach public function postRemoveAction(ServerRequestInterface $request): ResponseInterface 364c1010edaSGreg Roach { 36557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 366*75964c75SGreg Roach assert($tree instanceof Tree); 3675229eadeSGreg Roach 368bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 3695a78cd34SGreg Roach 3705a78cd34SGreg Roach $cart = Session::get('cart', []); 371aa6f03bbSGreg Roach unset($cart[$tree->name()][$xref]); 3725a78cd34SGreg Roach Session::put('cart', $cart); 3735a78cd34SGreg Roach 374c1010edaSGreg Roach $url = route('module', [ 37526684e68SGreg Roach 'module' => $this->name(), 376c1010edaSGreg Roach 'action' => 'Show', 377d72b284aSGreg Roach 'tree' => $tree->name(), 378c1010edaSGreg Roach ]); 3795a78cd34SGreg Roach 3806ccdf4f0SGreg Roach return redirect($url); 3815a78cd34SGreg Roach } 3825a78cd34SGreg Roach 3835a78cd34SGreg Roach /** 38457ab2231SGreg Roach * @param ServerRequestInterface $request 3855a78cd34SGreg Roach * 3866ccdf4f0SGreg Roach * @return ResponseInterface 3875a78cd34SGreg Roach */ 38857ab2231SGreg Roach public function getShowAction(ServerRequestInterface $request): ResponseInterface 389c1010edaSGreg Roach { 39057ab2231SGreg Roach $tree = $request->getAttribute('tree'); 391*75964c75SGreg Roach assert($tree instanceof Tree); 39257ab2231SGreg Roach 3935a78cd34SGreg Roach return $this->viewResponse('modules/clippings/show', [ 3945a78cd34SGreg Roach 'records' => $this->allRecordsInCart($tree), 3955a78cd34SGreg Roach 'title' => I18N::translate('Family tree clippings cart'), 3965a78cd34SGreg Roach 'tree' => $tree, 3975a78cd34SGreg Roach ]); 3985a78cd34SGreg Roach } 3995a78cd34SGreg Roach 4005a78cd34SGreg Roach /** 4016ccdf4f0SGreg Roach * @param ServerRequestInterface $request 4025a78cd34SGreg Roach * 4036ccdf4f0SGreg Roach * @return ResponseInterface 4045a78cd34SGreg Roach */ 40557ab2231SGreg Roach public function getAddFamilyAction(ServerRequestInterface $request): ResponseInterface 406c1010edaSGreg Roach { 40757ab2231SGreg Roach $tree = $request->getAttribute('tree'); 408*75964c75SGreg Roach assert($tree instanceof Tree); 4095229eadeSGreg Roach 410bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 4115a78cd34SGreg Roach 4125a78cd34SGreg Roach $family = Family::getInstance($xref, $tree); 4135a78cd34SGreg Roach 4145a78cd34SGreg Roach if ($family === null) { 41559f2f229SGreg Roach throw new FamilyNotFoundException(); 4165a78cd34SGreg Roach } 4175a78cd34SGreg Roach 4185a78cd34SGreg Roach $options = $this->familyOptions($family); 4195a78cd34SGreg Roach 42039ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $family->fullName()); 4215a78cd34SGreg Roach 4225a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 4235a78cd34SGreg Roach 'options' => $options, 4245a78cd34SGreg Roach 'default' => key($options), 4255a78cd34SGreg Roach 'record' => $family, 4265a78cd34SGreg Roach 'title' => $title, 4275a78cd34SGreg Roach 'tree' => $tree, 4285a78cd34SGreg Roach ]); 4295a78cd34SGreg Roach } 4305a78cd34SGreg Roach 4315a78cd34SGreg Roach /** 4325a78cd34SGreg Roach * @param Family $family 4335a78cd34SGreg Roach * 4345a78cd34SGreg Roach * @return string[] 4355a78cd34SGreg Roach */ 436c1010edaSGreg Roach private function familyOptions(Family $family): array 437c1010edaSGreg Roach { 43839ca88baSGreg Roach $name = strip_tags($family->fullName()); 4395a78cd34SGreg Roach 4405a78cd34SGreg Roach return [ 4415a78cd34SGreg Roach 'parents' => $name, 442bbb76c12SGreg Roach /* I18N: %s is a family (husband + wife) */ 443bbb76c12SGreg Roach 'members' => I18N::translate('%s and their children', $name), 444bbb76c12SGreg Roach /* I18N: %s is a family (husband + wife) */ 445bbb76c12SGreg Roach 'descendants' => I18N::translate('%s and their descendants', $name), 4465a78cd34SGreg Roach ]; 4475a78cd34SGreg Roach } 4485a78cd34SGreg Roach 4495a78cd34SGreg Roach /** 4506ccdf4f0SGreg Roach * @param ServerRequestInterface $request 4515a78cd34SGreg Roach * 4526ccdf4f0SGreg Roach * @return ResponseInterface 4535a78cd34SGreg Roach */ 45457ab2231SGreg Roach public function postAddFamilyAction(ServerRequestInterface $request): ResponseInterface 455c1010edaSGreg Roach { 45657ab2231SGreg Roach $tree = $request->getAttribute('tree'); 457bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 458bed27cedSGreg Roach $option = $request->getParsedBody()['option']; 4595a78cd34SGreg Roach 4605a78cd34SGreg Roach $family = Family::getInstance($xref, $tree); 4615a78cd34SGreg Roach 4625a78cd34SGreg Roach if ($family === null) { 46359f2f229SGreg Roach throw new FamilyNotFoundException(); 4645a78cd34SGreg Roach } 4655a78cd34SGreg Roach 4665a78cd34SGreg Roach switch ($option) { 4675a78cd34SGreg Roach case 'parents': 4685a78cd34SGreg Roach $this->addFamilyToCart($family); 4695a78cd34SGreg Roach break; 4705a78cd34SGreg Roach 4715a78cd34SGreg Roach case 'members': 4725a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 4735a78cd34SGreg Roach break; 4745a78cd34SGreg Roach 4755a78cd34SGreg Roach case 'descendants': 4765a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($family); 4775a78cd34SGreg Roach break; 4785a78cd34SGreg Roach } 4795a78cd34SGreg Roach 4806ccdf4f0SGreg Roach return redirect($family->url()); 4815a78cd34SGreg Roach } 4825a78cd34SGreg Roach 4835a78cd34SGreg Roach /** 4845a78cd34SGreg Roach * @param Family $family 48518d7a90dSGreg Roach * 48618d7a90dSGreg Roach * @return void 4875a78cd34SGreg Roach */ 488e364afe4SGreg Roach private function addFamilyToCart(Family $family): void 489c1010edaSGreg Roach { 4905a78cd34SGreg Roach $this->addRecordToCart($family); 4915a78cd34SGreg Roach 49239ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 4935a78cd34SGreg Roach $this->addRecordToCart($spouse); 4945a78cd34SGreg Roach } 4955a78cd34SGreg Roach } 4965a78cd34SGreg Roach 4975a78cd34SGreg Roach /** 4985a78cd34SGreg Roach * @param Family $family 49918d7a90dSGreg Roach * 50018d7a90dSGreg Roach * @return void 5015a78cd34SGreg Roach */ 502e364afe4SGreg Roach private function addFamilyAndChildrenToCart(Family $family): void 503c1010edaSGreg Roach { 5045a78cd34SGreg Roach $this->addRecordToCart($family); 5055a78cd34SGreg Roach 50639ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 5075a78cd34SGreg Roach $this->addRecordToCart($spouse); 5085a78cd34SGreg Roach } 50939ca88baSGreg Roach foreach ($family->children() as $child) { 5105a78cd34SGreg Roach $this->addRecordToCart($child); 5115a78cd34SGreg Roach } 5125a78cd34SGreg Roach } 5135a78cd34SGreg Roach 5145a78cd34SGreg Roach /** 5155a78cd34SGreg Roach * @param Family $family 51618d7a90dSGreg Roach * 51718d7a90dSGreg Roach * @return void 5185a78cd34SGreg Roach */ 519e364afe4SGreg Roach private function addFamilyAndDescendantsToCart(Family $family): void 520c1010edaSGreg Roach { 5215a78cd34SGreg Roach $this->addRecordToCart($family); 5225a78cd34SGreg Roach 52339ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 5245a78cd34SGreg Roach $this->addRecordToCart($spouse); 5255a78cd34SGreg Roach } 52639ca88baSGreg Roach foreach ($family->children() as $child) { 5275a78cd34SGreg Roach $this->addRecordToCart($child); 52839ca88baSGreg Roach foreach ($child->spouseFamilies() as $child_family) { 5295a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($child_family); 5305a78cd34SGreg Roach } 5315a78cd34SGreg Roach } 5325a78cd34SGreg Roach } 5335a78cd34SGreg Roach 5345a78cd34SGreg Roach /** 5356ccdf4f0SGreg Roach * @param ServerRequestInterface $request 5365a78cd34SGreg Roach * 5376ccdf4f0SGreg Roach * @return ResponseInterface 5385a78cd34SGreg Roach */ 53957ab2231SGreg Roach public function getAddIndividualAction(ServerRequestInterface $request): ResponseInterface 540c1010edaSGreg Roach { 54157ab2231SGreg Roach $tree = $request->getAttribute('tree'); 542*75964c75SGreg Roach assert($tree instanceof Tree); 5435229eadeSGreg Roach 544bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 5455a78cd34SGreg Roach 5465a78cd34SGreg Roach $individual = Individual::getInstance($xref, $tree); 5475a78cd34SGreg Roach 5485a78cd34SGreg Roach if ($individual === null) { 54959f2f229SGreg Roach throw new IndividualNotFoundException(); 5505a78cd34SGreg Roach } 5515a78cd34SGreg Roach 5525a78cd34SGreg Roach $options = $this->individualOptions($individual); 5535a78cd34SGreg Roach 55439ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $individual->fullName()); 5555a78cd34SGreg Roach 5565a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 5575a78cd34SGreg Roach 'options' => $options, 5585a78cd34SGreg Roach 'default' => key($options), 5595a78cd34SGreg Roach 'record' => $individual, 5605a78cd34SGreg Roach 'title' => $title, 5615a78cd34SGreg Roach 'tree' => $tree, 5625a78cd34SGreg Roach ]); 5635a78cd34SGreg Roach } 5645a78cd34SGreg Roach 5655a78cd34SGreg Roach /** 5665a78cd34SGreg Roach * @param Individual $individual 5675a78cd34SGreg Roach * 5685a78cd34SGreg Roach * @return string[] 5695a78cd34SGreg Roach */ 570c1010edaSGreg Roach private function individualOptions(Individual $individual): array 571c1010edaSGreg Roach { 57239ca88baSGreg Roach $name = strip_tags($individual->fullName()); 5735a78cd34SGreg Roach 57439ca88baSGreg Roach if ($individual->sex() === 'F') { 5755a78cd34SGreg Roach return [ 5765a78cd34SGreg Roach 'self' => $name, 5775a78cd34SGreg Roach 'parents' => I18N::translate('%s, her parents and siblings', $name), 5785a78cd34SGreg Roach 'spouses' => I18N::translate('%s, her spouses and children', $name), 5795a78cd34SGreg Roach 'ancestors' => I18N::translate('%s and her ancestors', $name), 5805a78cd34SGreg Roach 'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name), 5815a78cd34SGreg Roach 'descendants' => I18N::translate('%s, her spouses and descendants', $name), 5825a78cd34SGreg Roach ]; 583b2ce94c6SRico Sonntag } 584b2ce94c6SRico Sonntag 5855a78cd34SGreg Roach return [ 5865a78cd34SGreg Roach 'self' => $name, 5875a78cd34SGreg Roach 'parents' => I18N::translate('%s, his parents and siblings', $name), 5885a78cd34SGreg Roach 'spouses' => I18N::translate('%s, his spouses and children', $name), 5895a78cd34SGreg Roach 'ancestors' => I18N::translate('%s and his ancestors', $name), 5905a78cd34SGreg Roach 'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name), 5915a78cd34SGreg Roach 'descendants' => I18N::translate('%s, his spouses and descendants', $name), 5925a78cd34SGreg Roach ]; 5935a78cd34SGreg Roach } 5945a78cd34SGreg Roach 5955a78cd34SGreg Roach /** 5966ccdf4f0SGreg Roach * @param ServerRequestInterface $request 5975a78cd34SGreg Roach * 5986ccdf4f0SGreg Roach * @return ResponseInterface 5995a78cd34SGreg Roach */ 60057ab2231SGreg Roach public function postAddIndividualAction(ServerRequestInterface $request): ResponseInterface 601c1010edaSGreg Roach { 60257ab2231SGreg Roach $tree = $request->getAttribute('tree'); 603bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 604bed27cedSGreg Roach $option = $request->getParsedBody()['option']; 6055a78cd34SGreg Roach 6065a78cd34SGreg Roach $individual = Individual::getInstance($xref, $tree); 6075a78cd34SGreg Roach 6085a78cd34SGreg Roach if ($individual === null) { 60959f2f229SGreg Roach throw new IndividualNotFoundException(); 6105a78cd34SGreg Roach } 6115a78cd34SGreg Roach 6125a78cd34SGreg Roach switch ($option) { 6135a78cd34SGreg Roach case 'self': 6145a78cd34SGreg Roach $this->addRecordToCart($individual); 6155a78cd34SGreg Roach break; 6165a78cd34SGreg Roach 6175a78cd34SGreg Roach case 'parents': 61839ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 6195a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 6205a78cd34SGreg Roach } 6215a78cd34SGreg Roach break; 6225a78cd34SGreg Roach 6235a78cd34SGreg Roach case 'spouses': 62439ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 6255a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 6265a78cd34SGreg Roach } 6275a78cd34SGreg Roach break; 6285a78cd34SGreg Roach 6295a78cd34SGreg Roach case 'ancestors': 6305a78cd34SGreg Roach $this->addAncestorsToCart($individual); 6315a78cd34SGreg Roach break; 6325a78cd34SGreg Roach 6335a78cd34SGreg Roach case 'ancestor_families': 6345a78cd34SGreg Roach $this->addAncestorFamiliesToCart($individual); 6355a78cd34SGreg Roach break; 6365a78cd34SGreg Roach 6375a78cd34SGreg Roach case 'descendants': 63839ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 6395a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($family); 6405a78cd34SGreg Roach } 6415a78cd34SGreg Roach break; 6425a78cd34SGreg Roach } 6435a78cd34SGreg Roach 6446ccdf4f0SGreg Roach return redirect($individual->url()); 6455a78cd34SGreg Roach } 6465a78cd34SGreg Roach 6475a78cd34SGreg Roach /** 6485a78cd34SGreg Roach * @param Individual $individual 64918d7a90dSGreg Roach * 65018d7a90dSGreg Roach * @return void 6515a78cd34SGreg Roach */ 652e364afe4SGreg Roach private function addAncestorsToCart(Individual $individual): void 653c1010edaSGreg Roach { 6545a78cd34SGreg Roach $this->addRecordToCart($individual); 6555a78cd34SGreg Roach 65639ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 65739ca88baSGreg Roach foreach ($family->spouses() as $parent) { 6585a78cd34SGreg Roach $this->addAncestorsToCart($parent); 6595a78cd34SGreg Roach } 6605a78cd34SGreg Roach } 6615a78cd34SGreg Roach } 6625a78cd34SGreg Roach 6635a78cd34SGreg Roach /** 6645a78cd34SGreg Roach * @param Individual $individual 66518d7a90dSGreg Roach * 66618d7a90dSGreg Roach * @return void 6675a78cd34SGreg Roach */ 668e364afe4SGreg Roach private function addAncestorFamiliesToCart(Individual $individual): void 669c1010edaSGreg Roach { 67039ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 6715a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 67239ca88baSGreg Roach foreach ($family->spouses() as $parent) { 6735a78cd34SGreg Roach $this->addAncestorsToCart($parent); 6745a78cd34SGreg Roach } 6755a78cd34SGreg Roach } 6765a78cd34SGreg Roach } 6775a78cd34SGreg Roach 6785a78cd34SGreg Roach /** 6796ccdf4f0SGreg Roach * @param ServerRequestInterface $request 6805a78cd34SGreg Roach * 6816ccdf4f0SGreg Roach * @return ResponseInterface 6825a78cd34SGreg Roach */ 68357ab2231SGreg Roach public function getAddMediaAction(ServerRequestInterface $request): ResponseInterface 684c1010edaSGreg Roach { 68557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 686*75964c75SGreg Roach assert($tree instanceof Tree); 6875229eadeSGreg Roach 688bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 6895a78cd34SGreg Roach 6905a78cd34SGreg Roach $media = Media::getInstance($xref, $tree); 6915a78cd34SGreg Roach 6925a78cd34SGreg Roach if ($media === null) { 69359f2f229SGreg Roach throw new MediaNotFoundException(); 6945a78cd34SGreg Roach } 6955a78cd34SGreg Roach 6965a78cd34SGreg Roach $options = $this->mediaOptions($media); 6975a78cd34SGreg Roach 69839ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $media->fullName()); 6995a78cd34SGreg Roach 7005a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 7015a78cd34SGreg Roach 'options' => $options, 7025a78cd34SGreg Roach 'default' => key($options), 7035a78cd34SGreg Roach 'record' => $media, 7045a78cd34SGreg Roach 'title' => $title, 7055a78cd34SGreg Roach 'tree' => $tree, 7065a78cd34SGreg Roach ]); 7075a78cd34SGreg Roach } 7085a78cd34SGreg Roach 7095a78cd34SGreg Roach /** 7105a78cd34SGreg Roach * @param Media $media 7115a78cd34SGreg Roach * 7125a78cd34SGreg Roach * @return string[] 7135a78cd34SGreg Roach */ 714c1010edaSGreg Roach private function mediaOptions(Media $media): array 715c1010edaSGreg Roach { 71639ca88baSGreg Roach $name = strip_tags($media->fullName()); 7175a78cd34SGreg Roach 7185a78cd34SGreg Roach return [ 7195a78cd34SGreg Roach 'self' => $name, 7205a78cd34SGreg Roach ]; 7215a78cd34SGreg Roach } 7225a78cd34SGreg Roach 7235a78cd34SGreg Roach /** 7246ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7255a78cd34SGreg Roach * 7266ccdf4f0SGreg Roach * @return ResponseInterface 7275a78cd34SGreg Roach */ 72857ab2231SGreg Roach public function postAddMediaAction(ServerRequestInterface $request): ResponseInterface 729c1010edaSGreg Roach { 73057ab2231SGreg Roach $tree = $request->getAttribute('tree'); 731*75964c75SGreg Roach assert($tree instanceof Tree); 7325229eadeSGreg Roach 733bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 7345a78cd34SGreg Roach 7355a78cd34SGreg Roach $media = Media::getInstance($xref, $tree); 7365a78cd34SGreg Roach 7375a78cd34SGreg Roach if ($media === null) { 73859f2f229SGreg Roach throw new MediaNotFoundException(); 7395a78cd34SGreg Roach } 7405a78cd34SGreg Roach 7415a78cd34SGreg Roach $this->addRecordToCart($media); 7425a78cd34SGreg Roach 7436ccdf4f0SGreg Roach return redirect($media->url()); 7445a78cd34SGreg Roach } 7455a78cd34SGreg Roach 7465a78cd34SGreg Roach /** 7476ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7485a78cd34SGreg Roach * 7496ccdf4f0SGreg Roach * @return ResponseInterface 7505a78cd34SGreg Roach */ 75157ab2231SGreg Roach public function getAddNoteAction(ServerRequestInterface $request): ResponseInterface 752c1010edaSGreg Roach { 75357ab2231SGreg Roach $tree = $request->getAttribute('tree'); 754*75964c75SGreg Roach assert($tree instanceof Tree); 7555229eadeSGreg Roach 756bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 7575a78cd34SGreg Roach 7585a78cd34SGreg Roach $note = Note::getInstance($xref, $tree); 7595a78cd34SGreg Roach 7605a78cd34SGreg Roach if ($note === null) { 76159f2f229SGreg Roach throw new NoteNotFoundException(); 7625a78cd34SGreg Roach } 7635a78cd34SGreg Roach 7645a78cd34SGreg Roach $options = $this->noteOptions($note); 7655a78cd34SGreg Roach 76639ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $note->fullName()); 7675a78cd34SGreg Roach 7685a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 7695a78cd34SGreg Roach 'options' => $options, 7705a78cd34SGreg Roach 'default' => key($options), 7715a78cd34SGreg Roach 'record' => $note, 7725a78cd34SGreg Roach 'title' => $title, 7735a78cd34SGreg Roach 'tree' => $tree, 7745a78cd34SGreg Roach ]); 7755a78cd34SGreg Roach } 7765a78cd34SGreg Roach 7775a78cd34SGreg Roach /** 7785a78cd34SGreg Roach * @param Note $note 7795a78cd34SGreg Roach * 7805a78cd34SGreg Roach * @return string[] 7815a78cd34SGreg Roach */ 782c1010edaSGreg Roach private function noteOptions(Note $note): array 783c1010edaSGreg Roach { 78439ca88baSGreg Roach $name = strip_tags($note->fullName()); 7855a78cd34SGreg Roach 7865a78cd34SGreg Roach return [ 7875a78cd34SGreg Roach 'self' => $name, 7885a78cd34SGreg Roach ]; 7895a78cd34SGreg Roach } 7905a78cd34SGreg Roach 7915a78cd34SGreg Roach /** 7926ccdf4f0SGreg Roach * @param ServerRequestInterface $request 7935a78cd34SGreg Roach * 7946ccdf4f0SGreg Roach * @return ResponseInterface 7955a78cd34SGreg Roach */ 79657ab2231SGreg Roach public function postAddNoteAction(ServerRequestInterface $request): ResponseInterface 797c1010edaSGreg Roach { 79857ab2231SGreg Roach $tree = $request->getAttribute('tree'); 799*75964c75SGreg Roach assert($tree instanceof Tree); 8005229eadeSGreg Roach 801bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8025a78cd34SGreg Roach 8035a78cd34SGreg Roach $note = Note::getInstance($xref, $tree); 8045a78cd34SGreg Roach 8055a78cd34SGreg Roach if ($note === null) { 80659f2f229SGreg Roach throw new NoteNotFoundException(); 8075a78cd34SGreg Roach } 8085a78cd34SGreg Roach 8095a78cd34SGreg Roach $this->addRecordToCart($note); 8105a78cd34SGreg Roach 8116ccdf4f0SGreg Roach return redirect($note->url()); 8125a78cd34SGreg Roach } 8135a78cd34SGreg Roach 8145a78cd34SGreg Roach /** 8156ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8165a78cd34SGreg Roach * 8176ccdf4f0SGreg Roach * @return ResponseInterface 8185a78cd34SGreg Roach */ 81957ab2231SGreg Roach public function getAddRepositoryAction(ServerRequestInterface $request): ResponseInterface 820c1010edaSGreg Roach { 82157ab2231SGreg Roach $tree = $request->getAttribute('tree'); 822*75964c75SGreg Roach assert($tree instanceof Tree); 8235229eadeSGreg Roach 824bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8255a78cd34SGreg Roach 8265a78cd34SGreg Roach $repository = Repository::getInstance($xref, $tree); 8275a78cd34SGreg Roach 8285a78cd34SGreg Roach if ($repository === null) { 82959f2f229SGreg Roach throw new RepositoryNotFoundException(); 8305a78cd34SGreg Roach } 8315a78cd34SGreg Roach 8325a78cd34SGreg Roach $options = $this->repositoryOptions($repository); 8335a78cd34SGreg Roach 83439ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $repository->fullName()); 8355a78cd34SGreg Roach 8365a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 8375a78cd34SGreg Roach 'options' => $options, 8385a78cd34SGreg Roach 'default' => key($options), 8395a78cd34SGreg Roach 'record' => $repository, 8405a78cd34SGreg Roach 'title' => $title, 8415a78cd34SGreg Roach 'tree' => $tree, 8425a78cd34SGreg Roach ]); 8435a78cd34SGreg Roach } 8445a78cd34SGreg Roach 8455a78cd34SGreg Roach /** 8465a78cd34SGreg Roach * @param Repository $repository 8475a78cd34SGreg Roach * 8485a78cd34SGreg Roach * @return string[] 8495a78cd34SGreg Roach */ 850c1010edaSGreg Roach private function repositoryOptions(Repository $repository): array 851c1010edaSGreg Roach { 85239ca88baSGreg Roach $name = strip_tags($repository->fullName()); 8535a78cd34SGreg Roach 8545a78cd34SGreg Roach return [ 8555a78cd34SGreg Roach 'self' => $name, 8565a78cd34SGreg Roach ]; 8575a78cd34SGreg Roach } 8585a78cd34SGreg Roach 8595a78cd34SGreg Roach /** 8606ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8615a78cd34SGreg Roach * 8626ccdf4f0SGreg Roach * @return ResponseInterface 8635a78cd34SGreg Roach */ 86457ab2231SGreg Roach public function postAddRepositoryAction(ServerRequestInterface $request): ResponseInterface 865c1010edaSGreg Roach { 86657ab2231SGreg Roach $tree = $request->getAttribute('tree'); 867*75964c75SGreg Roach assert($tree instanceof Tree); 8685229eadeSGreg Roach 869bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8705a78cd34SGreg Roach 8715a78cd34SGreg Roach $repository = Repository::getInstance($xref, $tree); 8725a78cd34SGreg Roach 8735a78cd34SGreg Roach if ($repository === null) { 87459f2f229SGreg Roach throw new RepositoryNotFoundException(); 8755a78cd34SGreg Roach } 8765a78cd34SGreg Roach 8775a78cd34SGreg Roach $this->addRecordToCart($repository); 8785a78cd34SGreg Roach 8796ccdf4f0SGreg Roach return redirect($repository->url()); 8805a78cd34SGreg Roach } 8815a78cd34SGreg Roach 8825a78cd34SGreg Roach /** 8836ccdf4f0SGreg Roach * @param ServerRequestInterface $request 8845a78cd34SGreg Roach * 8856ccdf4f0SGreg Roach * @return ResponseInterface 8865a78cd34SGreg Roach */ 88757ab2231SGreg Roach public function getAddSourceAction(ServerRequestInterface $request): ResponseInterface 888c1010edaSGreg Roach { 88957ab2231SGreg Roach $tree = $request->getAttribute('tree'); 890*75964c75SGreg Roach assert($tree instanceof Tree); 8915229eadeSGreg Roach 892bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8935a78cd34SGreg Roach 8945a78cd34SGreg Roach $source = Source::getInstance($xref, $tree); 8955a78cd34SGreg Roach 8965a78cd34SGreg Roach if ($source === null) { 89759f2f229SGreg Roach throw new SourceNotFoundException(); 8985a78cd34SGreg Roach } 8995a78cd34SGreg Roach 9005a78cd34SGreg Roach $options = $this->sourceOptions($source); 9015a78cd34SGreg Roach 90239ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $source->fullName()); 9035a78cd34SGreg Roach 9045a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 9055a78cd34SGreg Roach 'options' => $options, 9065a78cd34SGreg Roach 'default' => key($options), 9075a78cd34SGreg Roach 'record' => $source, 9085a78cd34SGreg Roach 'title' => $title, 9095a78cd34SGreg Roach 'tree' => $tree, 9105a78cd34SGreg Roach ]); 9115a78cd34SGreg Roach } 9125a78cd34SGreg Roach 9135a78cd34SGreg Roach /** 9145a78cd34SGreg Roach * @param Source $source 9155a78cd34SGreg Roach * 9165a78cd34SGreg Roach * @return string[] 9175a78cd34SGreg Roach */ 918c1010edaSGreg Roach private function sourceOptions(Source $source): array 919c1010edaSGreg Roach { 92039ca88baSGreg Roach $name = strip_tags($source->fullName()); 9215a78cd34SGreg Roach 9225a78cd34SGreg Roach return [ 92339ca88baSGreg Roach 'only' => strip_tags($source->fullName()), 9245a78cd34SGreg Roach 'linked' => I18N::translate('%s and the individuals that reference it.', $name), 9255a78cd34SGreg Roach ]; 9265a78cd34SGreg Roach } 9275a78cd34SGreg Roach 9285a78cd34SGreg Roach /** 9296ccdf4f0SGreg Roach * @param ServerRequestInterface $request 9305a78cd34SGreg Roach * 9316ccdf4f0SGreg Roach * @return ResponseInterface 9325a78cd34SGreg Roach */ 93357ab2231SGreg Roach public function postAddSourceAction(ServerRequestInterface $request): ResponseInterface 934c1010edaSGreg Roach { 93557ab2231SGreg Roach $tree = $request->getAttribute('tree'); 936*75964c75SGreg Roach assert($tree instanceof Tree); 9375229eadeSGreg Roach 938bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 939bed27cedSGreg Roach $option = $request->getParsedBody()['option']; 9405a78cd34SGreg Roach 9415a78cd34SGreg Roach $source = Source::getInstance($xref, $tree); 9425a78cd34SGreg Roach 9435a78cd34SGreg Roach if ($source === null) { 94459f2f229SGreg Roach throw new SourceNotFoundException(); 9455a78cd34SGreg Roach } 9465a78cd34SGreg Roach 9475a78cd34SGreg Roach $this->addRecordToCart($source); 9485a78cd34SGreg Roach 9495a78cd34SGreg Roach if ($option === 'linked') { 9505a78cd34SGreg Roach foreach ($source->linkedIndividuals('SOUR') as $individual) { 9515a78cd34SGreg Roach $this->addRecordToCart($individual); 9525a78cd34SGreg Roach } 9535a78cd34SGreg Roach foreach ($source->linkedFamilies('SOUR') as $family) { 9545a78cd34SGreg Roach $this->addRecordToCart($family); 9555a78cd34SGreg Roach } 9565a78cd34SGreg Roach } 9575a78cd34SGreg Roach 9586ccdf4f0SGreg Roach return redirect($source->url()); 9595a78cd34SGreg Roach } 9605a78cd34SGreg Roach 9615a78cd34SGreg Roach /** 9625a78cd34SGreg Roach * Get all the records in the cart. 9635a78cd34SGreg Roach * 9645a78cd34SGreg Roach * @param Tree $tree 9655a78cd34SGreg Roach * 9665a78cd34SGreg Roach * @return GedcomRecord[] 9675a78cd34SGreg Roach */ 968c1010edaSGreg Roach private function allRecordsInCart(Tree $tree): array 969c1010edaSGreg Roach { 9705a78cd34SGreg Roach $cart = Session::get('cart', []); 9715a78cd34SGreg Roach 972aa6f03bbSGreg Roach $xrefs = array_keys($cart[$tree->name()] ?? []); 9735a78cd34SGreg Roach 9745a78cd34SGreg Roach // Fetch all the records in the cart. 975bed27cedSGreg Roach $records = array_map(static function (string $xref) use ($tree): ?GedcomRecord { 9765a78cd34SGreg Roach return GedcomRecord::getInstance($xref, $tree); 9775a78cd34SGreg Roach }, $xrefs); 9785a78cd34SGreg Roach 9795a78cd34SGreg Roach // Some records may have been deleted after they were added to the cart. 9805a78cd34SGreg Roach $records = array_filter($records); 9815a78cd34SGreg Roach 9825a78cd34SGreg Roach // Group and sort. 9830b5fd0a6SGreg Roach uasort($records, static function (GedcomRecord $x, GedcomRecord $y): int { 984c156e8f5SGreg Roach return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::nameComparator()($x, $y); 9855a78cd34SGreg Roach }); 9865a78cd34SGreg Roach 9875a78cd34SGreg Roach return $records; 9885a78cd34SGreg Roach } 9895a78cd34SGreg Roach 9905a78cd34SGreg Roach /** 9915a78cd34SGreg Roach * Add a record (and direclty linked sources, notes, etc. to the cart. 9925a78cd34SGreg Roach * 9935a78cd34SGreg Roach * @param GedcomRecord $record 99418d7a90dSGreg Roach * 99518d7a90dSGreg Roach * @return void 9965a78cd34SGreg Roach */ 997e364afe4SGreg Roach private function addRecordToCart(GedcomRecord $record): void 998c1010edaSGreg Roach { 9995a78cd34SGreg Roach $cart = Session::get('cart', []); 10005a78cd34SGreg Roach 1001f4afa648SGreg Roach $tree_name = $record->tree()->name(); 10025a78cd34SGreg Roach 10035a78cd34SGreg Roach // Add this record 1004c0935879SGreg Roach $cart[$tree_name][$record->xref()] = true; 10055a78cd34SGreg Roach 10065a78cd34SGreg Roach // Add directly linked media, notes, repositories and sources. 10078d0ebef0SGreg Roach preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches); 10085a78cd34SGreg Roach 10095a78cd34SGreg Roach foreach ($matches[1] as $match) { 10105a78cd34SGreg Roach $cart[$tree_name][$match] = true; 10115a78cd34SGreg Roach } 10125a78cd34SGreg Roach 10135a78cd34SGreg Roach Session::put('cart', $cart); 10145a78cd34SGreg Roach } 10155a78cd34SGreg Roach 10165a78cd34SGreg Roach /** 10175a78cd34SGreg Roach * @param Tree $tree 10185a78cd34SGreg Roach * 10195a78cd34SGreg Roach * @return bool 10205a78cd34SGreg Roach */ 1021c1010edaSGreg Roach private function isCartEmpty(Tree $tree): bool 1022c1010edaSGreg Roach { 10235a78cd34SGreg Roach $cart = Session::get('cart', []); 1024a91af26aSGreg Roach $contents = $cart[$tree->name()] ?? []; 10255a78cd34SGreg Roach 1026a91af26aSGreg Roach return $contents === []; 10275a78cd34SGreg Roach } 10288c2e8227SGreg Roach} 1029