18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 48fcd0d32SGreg Roach * Copyright (C) 2019 webtrees development team 58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 88c2e8227SGreg Roach * (at your option) any later version. 98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 128c2e8227SGreg Roach * GNU General Public License for more details. 138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 158c2e8227SGreg Roach */ 16e7f56f2aSGreg Roachdeclare(strict_types=1); 17e7f56f2aSGreg Roach 1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1976692c8bSGreg Roach 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 21e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 220bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\FamilyNotFoundException; 230bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException; 240bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\MediaNotFoundException; 250bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\NoteNotFoundException; 260bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\RepositoryNotFoundException; 270bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\SourceNotFoundException; 280e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family; 295a78cd34SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport; 305a78cd34SGreg Roachuse Fisharebest\Webtrees\Gedcom; 310e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 320e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 330e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 345a78cd34SGreg Roachuse Fisharebest\Webtrees\Media; 350e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu; 365a78cd34SGreg Roachuse Fisharebest\Webtrees\Note; 375a78cd34SGreg Roachuse Fisharebest\Webtrees\Repository; 38e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 390e62c4b8SGreg Roachuse Fisharebest\Webtrees\Session; 405a78cd34SGreg Roachuse Fisharebest\Webtrees\Source; 41aee13b6dSGreg Roachuse Fisharebest\Webtrees\Tree; 425a78cd34SGreg Roachuse League\Flysystem\Filesystem; 435a78cd34SGreg Roachuse League\Flysystem\ZipArchive\ZipArchiveAdapter; 44bed27cedSGreg Roachuse Psr\Http\Message\ResponseFactoryInterface; 456ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 466ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 476ccdf4f0SGreg Roachuse Psr\Http\Message\StreamFactoryInterface; 48eb235819SGreg Roachuse function app; 49e5a6b4d4SGreg Roachuse function str_replace; 508c2e8227SGreg Roach 518c2e8227SGreg Roach/** 528c2e8227SGreg Roach * Class ClippingsCartModule 538c2e8227SGreg Roach */ 5437eb8894SGreg Roachclass ClippingsCartModule extends AbstractModule implements ModuleMenuInterface 55c1010edaSGreg Roach{ 5649a243cbSGreg Roach use ModuleMenuTrait; 5749a243cbSGreg Roach 585a78cd34SGreg Roach // Routes that have a record which can be added to the clipboard 5916d6367aSGreg Roach private const ROUTES_WITH_RECORDS = [ 60c1010edaSGreg Roach 'family', 61c1010edaSGreg Roach 'individual', 62c1010edaSGreg Roach 'media', 63c1010edaSGreg Roach 'note', 64c1010edaSGreg Roach 'repository', 65c1010edaSGreg Roach 'source', 66c1010edaSGreg Roach ]; 675a78cd34SGreg Roach 6849a243cbSGreg Roach /** @var int The default access level for this module. It can be changed in the control panel. */ 6949a243cbSGreg Roach protected $access_level = Auth::PRIV_USER; 7049a243cbSGreg Roach 71961ec755SGreg Roach /** 72e5a6b4d4SGreg Roach * @var UserService 73e5a6b4d4SGreg Roach */ 74e5a6b4d4SGreg Roach private $user_service; 75e5a6b4d4SGreg Roach 76e5a6b4d4SGreg Roach /** 77e5a6b4d4SGreg Roach * ClippingsCartModule constructor. 78e5a6b4d4SGreg Roach * 79e5a6b4d4SGreg Roach * @param UserService $user_service 80e5a6b4d4SGreg Roach */ 81e5a6b4d4SGreg Roach public function __construct(UserService $user_service) 82e5a6b4d4SGreg Roach { 83e5a6b4d4SGreg Roach $this->user_service = $user_service; 84e5a6b4d4SGreg Roach } 85e5a6b4d4SGreg Roach 86e5a6b4d4SGreg Roach /** 870cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 88961ec755SGreg Roach * 89961ec755SGreg Roach * @return string 90961ec755SGreg Roach */ 9149a243cbSGreg Roach public function title(): string 92c1010edaSGreg Roach { 93bbb76c12SGreg Roach /* I18N: Name of a module */ 94bbb76c12SGreg Roach return I18N::translate('Clippings cart'); 958c2e8227SGreg Roach } 968c2e8227SGreg Roach 97961ec755SGreg Roach /** 98961ec755SGreg Roach * A sentence describing what this module does. 99961ec755SGreg Roach * 100961ec755SGreg Roach * @return string 101961ec755SGreg Roach */ 10249a243cbSGreg Roach public function description(): string 103c1010edaSGreg Roach { 104bbb76c12SGreg Roach /* I18N: Description of the “Clippings cart” module */ 105bbb76c12SGreg Roach return I18N::translate('Select records from your family tree and save them as a GEDCOM file.'); 1068c2e8227SGreg Roach } 1078c2e8227SGreg Roach 1080ee13198SGreg Roach /** 10949a243cbSGreg Roach * The default position for this menu. It can be changed in the control panel. 1100ee13198SGreg Roach * 1110ee13198SGreg Roach * @return int 1120ee13198SGreg Roach */ 1138f53f488SRico Sonntag public function defaultMenuOrder(): int 114c1010edaSGreg Roach { 115353b36abSGreg Roach return 6; 1168c2e8227SGreg Roach } 1178c2e8227SGreg Roach 1180ee13198SGreg Roach /** 1190ee13198SGreg Roach * A menu, to be added to the main application menu. 1200ee13198SGreg Roach * 121aee13b6dSGreg Roach * @param Tree $tree 122aee13b6dSGreg Roach * 1230ee13198SGreg Roach * @return Menu|null 1240ee13198SGreg Roach */ 12546295629SGreg Roach public function getMenu(Tree $tree): ?Menu 126c1010edaSGreg Roach { 127eb235819SGreg Roach /** @var ServerRequestInterface $request */ 1286ccdf4f0SGreg Roach $request = app(ServerRequestInterface::class); 1298c2e8227SGreg Roach 130eb235819SGreg Roach $route = $request->getQueryParams()['route'] ?? ''; 1315a78cd34SGreg Roach 1325a78cd34SGreg Roach $submenus = [ 13349a243cbSGreg Roach new Menu($this->title(), route('module', [ 13426684e68SGreg Roach 'module' => $this->name(), 135c1010edaSGreg Roach 'action' => 'Show', 136aa6f03bbSGreg Roach 'ged' => $tree->name(), 137c1010edaSGreg Roach ]), 'menu-clippings-cart', ['rel' => 'nofollow']), 1385a78cd34SGreg Roach ]; 1395a78cd34SGreg Roach 14022d65e5aSGreg Roach if (in_array($route, self::ROUTES_WITH_RECORDS, true)) { 141bed27cedSGreg Roach $xref = $request->getQueryParams()['xref'] ?? ''; 1425a78cd34SGreg Roach $action = 'Add' . ucfirst($route); 143c1010edaSGreg Roach $add_route = route('module', [ 14426684e68SGreg Roach 'module' => $this->name(), 145c1010edaSGreg Roach 'action' => $action, 146c1010edaSGreg Roach 'xref' => $xref, 147aa6f03bbSGreg Roach 'ged' => $tree->name(), 148c1010edaSGreg Roach ]); 1495a78cd34SGreg Roach 15025b2dde3SGreg Roach $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), $add_route, 'menu-clippings-add', ['rel' => 'nofollow']); 1518c2e8227SGreg Roach } 152cbc1590aSGreg Roach 1535a78cd34SGreg Roach if (!$this->isCartEmpty($tree)) { 154c1010edaSGreg Roach $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), route('module', [ 15526684e68SGreg Roach 'module' => $this->name(), 156c1010edaSGreg Roach 'action' => 'Empty', 157aa6f03bbSGreg Roach 'ged' => $tree->name(), 158c1010edaSGreg Roach ]), 'menu-clippings-empty', ['rel' => 'nofollow']); 159c1010edaSGreg Roach $submenus[] = new Menu(I18N::translate('Download'), route('module', [ 16026684e68SGreg Roach 'module' => $this->name(), 161c1010edaSGreg Roach 'action' => 'DownloadForm', 162aa6f03bbSGreg Roach 'ged' => $tree->name(), 163c1010edaSGreg Roach ]), 'menu-clippings-download', ['rel' => 'nofollow']); 1645a78cd34SGreg Roach } 1655a78cd34SGreg Roach 16649a243cbSGreg Roach return new Menu($this->title(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus); 1678c2e8227SGreg Roach } 1688c2e8227SGreg Roach 16976692c8bSGreg Roach /** 1706ccdf4f0SGreg Roach * @param ServerRequestInterface $request 171b6db7c1fSGreg Roach * @param Tree $tree 17276692c8bSGreg Roach * 1736ccdf4f0SGreg Roach * @return ResponseInterface 17476692c8bSGreg Roach */ 1756ccdf4f0SGreg Roach public function getDownloadAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 176c1010edaSGreg Roach { 177bed27cedSGreg Roach $params = $request->getQueryParams(); 178bed27cedSGreg Roach 179bed27cedSGreg Roach $privatize_export = $params['privatize_export']; 180bed27cedSGreg Roach $convert = (bool) ($params['convert'] ?? false); 1818c2e8227SGreg Roach 18213abd6f3SGreg Roach $cart = Session::get('cart', []); 1838c2e8227SGreg Roach 184aa6f03bbSGreg Roach $xrefs = array_keys($cart[$tree->name()] ?? []); 1855a78cd34SGreg Roach 1865a78cd34SGreg Roach // Create a new/empty .ZIP file 1875a78cd34SGreg Roach $temp_zip_file = tempnam(sys_get_temp_dir(), 'webtrees-zip-'); 1885a78cd34SGreg Roach $zip_filesystem = new Filesystem(new ZipArchiveAdapter($temp_zip_file)); 1895a78cd34SGreg Roach 1905a78cd34SGreg Roach // Media file prefix 1915a78cd34SGreg Roach $path = $tree->getPreference('MEDIA_DIRECTORY'); 1925a78cd34SGreg Roach 1935a78cd34SGreg Roach // GEDCOM file header 194a3d8780cSGreg Roach $filetext = FunctionsExport::gedcomHeader($tree, $convert ? 'ANSI' : 'UTF-8'); 1955a78cd34SGreg Roach 1965a78cd34SGreg Roach switch ($privatize_export) { 1975a78cd34SGreg Roach case 'gedadmin': 1985a78cd34SGreg Roach $access_level = Auth::PRIV_NONE; 1995a78cd34SGreg Roach break; 2005a78cd34SGreg Roach case 'user': 2015a78cd34SGreg Roach $access_level = Auth::PRIV_USER; 2025a78cd34SGreg Roach break; 2035a78cd34SGreg Roach case 'visitor': 2045a78cd34SGreg Roach $access_level = Auth::PRIV_PRIVATE; 2055a78cd34SGreg Roach break; 2065a78cd34SGreg Roach case 'none': 2075a78cd34SGreg Roach default: 2085a78cd34SGreg Roach $access_level = Auth::PRIV_HIDE; 2095a78cd34SGreg Roach break; 2105a78cd34SGreg Roach } 2115a78cd34SGreg Roach 2125a78cd34SGreg Roach foreach ($xrefs as $xref) { 2135a78cd34SGreg Roach $object = GedcomRecord::getInstance($xref, $tree); 2145a78cd34SGreg Roach // The object may have been deleted since we added it to the cart.... 215bed27cedSGreg Roach if ($object instanceof GedcomRecord) { 2165a78cd34SGreg Roach $record = $object->privatizeGedcom($access_level); 2175a78cd34SGreg Roach // Remove links to objects that aren't in the cart 2188d0ebef0SGreg Roach preg_match_all('/\n1 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER); 2195a78cd34SGreg Roach foreach ($matches as $match) { 2205a78cd34SGreg Roach if (!array_key_exists($match[1], $xrefs)) { 2215a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2225a78cd34SGreg Roach } 2235a78cd34SGreg Roach } 2248d0ebef0SGreg Roach preg_match_all('/\n2 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER); 2255a78cd34SGreg Roach foreach ($matches as $match) { 2265a78cd34SGreg Roach if (!array_key_exists($match[1], $xrefs)) { 2275a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2285a78cd34SGreg Roach } 2295a78cd34SGreg Roach } 2308d0ebef0SGreg Roach preg_match_all('/\n3 ' . Gedcom::REGEX_TAG . ' @(' . Gedcom::REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER); 2315a78cd34SGreg Roach foreach ($matches as $match) { 2325a78cd34SGreg Roach if (!array_key_exists($match[1], $xrefs)) { 2335a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2345a78cd34SGreg Roach } 2355a78cd34SGreg Roach } 2365a78cd34SGreg Roach 23755167344SGreg Roach if ($object instanceof Individual || $object instanceof Family) { 2385a78cd34SGreg Roach $filetext .= $record . "\n"; 2395a78cd34SGreg Roach $filetext .= "1 SOUR @WEBTREES@\n"; 2401f273236SGreg Roach $filetext .= '2 PAGE ' . $object->url() . "\n"; 24155167344SGreg Roach } elseif ($object instanceof Source) { 2425a78cd34SGreg Roach $filetext .= $record . "\n"; 2431f273236SGreg Roach $filetext .= '1 NOTE ' . $object->url() . "\n"; 24455167344SGreg Roach } elseif ($object instanceof Media) { 24555167344SGreg Roach // Add the media files to the archive 2465a78cd34SGreg Roach foreach ($object->mediaFiles() as $media_file) { 2475a78cd34SGreg Roach if (file_exists($media_file->getServerFilename())) { 248e364afe4SGreg Roach $fp = fopen($media_file->getServerFilename(), 'rb'); 2495a78cd34SGreg Roach $zip_filesystem->writeStream($path . $media_file->filename(), $fp); 2505a78cd34SGreg Roach fclose($fp); 2515a78cd34SGreg Roach } 2525a78cd34SGreg Roach } 2535a78cd34SGreg Roach $filetext .= $record . "\n"; 25455167344SGreg Roach } else { 2555a78cd34SGreg Roach $filetext .= $record . "\n"; 2568c2e8227SGreg Roach } 2578c2e8227SGreg Roach } 2588c2e8227SGreg Roach } 2598c2e8227SGreg Roach 260*9b93b7c3SGreg Roach $base_url = $request->getAttribute('base_url'); 261*9b93b7c3SGreg Roach 2625a78cd34SGreg Roach // Create a source, to indicate the source of the data. 263*9b93b7c3SGreg Roach $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . $base_url . "\n"; 264e5a6b4d4SGreg Roach $author = $this->user_service->find((int) $tree->getPreference('CONTACT_USER_ID')); 2655a78cd34SGreg Roach if ($author !== null) { 266e5a6b4d4SGreg Roach $filetext .= '1 AUTH ' . $author->realName() . "\n"; 2675a78cd34SGreg Roach } 2685a78cd34SGreg Roach $filetext .= "0 TRLR\n"; 2695a78cd34SGreg Roach 2705a78cd34SGreg Roach // Make sure the preferred line endings are used 271a3d8780cSGreg Roach $filetext = str_replace('\n', Gedcom::EOL, $filetext); 2725a78cd34SGreg Roach 27355167344SGreg Roach if ($convert) { 2745a78cd34SGreg Roach $filetext = utf8_decode($filetext); 2758c2e8227SGreg Roach } 276cbc1590aSGreg Roach 2775a78cd34SGreg Roach // Finally add the GEDCOM file to the .ZIP file. 2785a78cd34SGreg Roach $zip_filesystem->write('clippings.ged', $filetext); 2795a78cd34SGreg Roach 2805a78cd34SGreg Roach // Need to force-close the filesystem 28102a92f80SGreg Roach unset($zip_filesystem); 2825a78cd34SGreg Roach 2836ccdf4f0SGreg Roach // Use a stream, so that we do not have to load the entire file into memory. 2846ccdf4f0SGreg Roach $stream = app(StreamFactoryInterface::class)->createStreamFromFile($temp_zip_file); 2855a78cd34SGreg Roach 286bed27cedSGreg Roach /** @var ResponseFactoryInterface $response_factory */ 287bed27cedSGreg Roach $response_factory = app(ResponseFactoryInterface::class); 288bed27cedSGreg Roach 289bed27cedSGreg Roach return $response_factory->createResponse() 2906ccdf4f0SGreg Roach ->withBody($stream) 2911b3d4731SGreg Roach ->withHeader('Content-Type', 'application/zip') 292bed27cedSGreg Roach ->withHeader('Content-Disposition', 'attachment; filename="clippings.zip'); 2938c2e8227SGreg Roach } 2948c2e8227SGreg Roach 2958c2e8227SGreg Roach /** 296b6db7c1fSGreg Roach * @param Tree $tree 297e5a6b4d4SGreg Roach * @param UserInterface $user 29876692c8bSGreg Roach * 2996ccdf4f0SGreg Roach * @return ResponseInterface 3008c2e8227SGreg Roach */ 3016ccdf4f0SGreg Roach public function getDownloadFormAction(Tree $tree, UserInterface $user): ResponseInterface 302c1010edaSGreg Roach { 3035a78cd34SGreg Roach $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download'); 3048c2e8227SGreg Roach 3055a78cd34SGreg Roach return $this->viewResponse('modules/clippings/download', [ 3065a78cd34SGreg Roach 'is_manager' => Auth::isManager($tree, $user), 3075a78cd34SGreg Roach 'is_member' => Auth::isMember($tree, $user), 3085a78cd34SGreg Roach 'title' => $title, 3095a78cd34SGreg Roach ]); 3108c2e8227SGreg Roach } 3118c2e8227SGreg Roach 3125a78cd34SGreg Roach /** 313b6db7c1fSGreg Roach * @param Tree $tree 3145a78cd34SGreg Roach * 3156ccdf4f0SGreg Roach * @return ResponseInterface 3165a78cd34SGreg Roach */ 3176ccdf4f0SGreg Roach public function getEmptyAction(Tree $tree): ResponseInterface 318c1010edaSGreg Roach { 3195a78cd34SGreg Roach $cart = Session::get('cart', []); 320aa6f03bbSGreg Roach $cart[$tree->name()] = []; 3215a78cd34SGreg Roach Session::put('cart', $cart); 3228c2e8227SGreg Roach 323c1010edaSGreg Roach $url = route('module', [ 32426684e68SGreg Roach 'module' => $this->name(), 325c1010edaSGreg Roach 'action' => 'Show', 326aa6f03bbSGreg Roach 'ged' => $tree->name(), 327c1010edaSGreg Roach ]); 3285a78cd34SGreg Roach 3296ccdf4f0SGreg Roach return redirect($url); 3305a78cd34SGreg Roach } 3315a78cd34SGreg Roach 3325a78cd34SGreg Roach /** 3336ccdf4f0SGreg Roach * @param ServerRequestInterface $request 334b6db7c1fSGreg Roach * @param Tree $tree 3355a78cd34SGreg Roach * 3366ccdf4f0SGreg Roach * @return ResponseInterface 3375a78cd34SGreg Roach */ 3386ccdf4f0SGreg Roach public function postRemoveAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 339c1010edaSGreg Roach { 340bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 3415a78cd34SGreg Roach 3425a78cd34SGreg Roach $cart = Session::get('cart', []); 343aa6f03bbSGreg Roach unset($cart[$tree->name()][$xref]); 3445a78cd34SGreg Roach Session::put('cart', $cart); 3455a78cd34SGreg Roach 346c1010edaSGreg Roach $url = route('module', [ 34726684e68SGreg Roach 'module' => $this->name(), 348c1010edaSGreg Roach 'action' => 'Show', 349aa6f03bbSGreg Roach 'ged' => $tree->name(), 350c1010edaSGreg Roach ]); 3515a78cd34SGreg Roach 3526ccdf4f0SGreg Roach return redirect($url); 3535a78cd34SGreg Roach } 3545a78cd34SGreg Roach 3555a78cd34SGreg Roach /** 356b6db7c1fSGreg Roach * @param Tree $tree 3575a78cd34SGreg Roach * 3586ccdf4f0SGreg Roach * @return ResponseInterface 3595a78cd34SGreg Roach */ 3606ccdf4f0SGreg Roach public function getShowAction(Tree $tree): ResponseInterface 361c1010edaSGreg Roach { 3625a78cd34SGreg Roach return $this->viewResponse('modules/clippings/show', [ 3635a78cd34SGreg Roach 'records' => $this->allRecordsInCart($tree), 3645a78cd34SGreg Roach 'title' => I18N::translate('Family tree clippings cart'), 3655a78cd34SGreg Roach 'tree' => $tree, 3665a78cd34SGreg Roach ]); 3675a78cd34SGreg Roach } 3685a78cd34SGreg Roach 3695a78cd34SGreg Roach /** 3706ccdf4f0SGreg Roach * @param ServerRequestInterface $request 371b6db7c1fSGreg Roach * @param Tree $tree 3725a78cd34SGreg Roach * 3736ccdf4f0SGreg Roach * @return ResponseInterface 3745a78cd34SGreg Roach */ 3756ccdf4f0SGreg Roach public function getAddFamilyAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 376c1010edaSGreg Roach { 377bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 3785a78cd34SGreg Roach 3795a78cd34SGreg Roach $family = Family::getInstance($xref, $tree); 3805a78cd34SGreg Roach 3815a78cd34SGreg Roach if ($family === null) { 38259f2f229SGreg Roach throw new FamilyNotFoundException(); 3835a78cd34SGreg Roach } 3845a78cd34SGreg Roach 3855a78cd34SGreg Roach $options = $this->familyOptions($family); 3865a78cd34SGreg Roach 38739ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $family->fullName()); 3885a78cd34SGreg Roach 3895a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 3905a78cd34SGreg Roach 'options' => $options, 3915a78cd34SGreg Roach 'default' => key($options), 3925a78cd34SGreg Roach 'record' => $family, 3935a78cd34SGreg Roach 'title' => $title, 3945a78cd34SGreg Roach 'tree' => $tree, 3955a78cd34SGreg Roach ]); 3965a78cd34SGreg Roach } 3975a78cd34SGreg Roach 3985a78cd34SGreg Roach /** 3995a78cd34SGreg Roach * @param Family $family 4005a78cd34SGreg Roach * 4015a78cd34SGreg Roach * @return string[] 4025a78cd34SGreg Roach */ 403c1010edaSGreg Roach private function familyOptions(Family $family): array 404c1010edaSGreg Roach { 40539ca88baSGreg Roach $name = strip_tags($family->fullName()); 4065a78cd34SGreg Roach 4075a78cd34SGreg Roach return [ 4085a78cd34SGreg Roach 'parents' => $name, 409bbb76c12SGreg Roach /* I18N: %s is a family (husband + wife) */ 410bbb76c12SGreg Roach 'members' => I18N::translate('%s and their children', $name), 411bbb76c12SGreg Roach /* I18N: %s is a family (husband + wife) */ 412bbb76c12SGreg Roach 'descendants' => I18N::translate('%s and their descendants', $name), 4135a78cd34SGreg Roach ]; 4145a78cd34SGreg Roach } 4155a78cd34SGreg Roach 4165a78cd34SGreg Roach /** 4176ccdf4f0SGreg Roach * @param ServerRequestInterface $request 418b6db7c1fSGreg Roach * @param Tree $tree 4195a78cd34SGreg Roach * 4206ccdf4f0SGreg Roach * @return ResponseInterface 4215a78cd34SGreg Roach */ 4226ccdf4f0SGreg Roach public function postAddFamilyAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 423c1010edaSGreg Roach { 424bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 425bed27cedSGreg Roach $option = $request->getParsedBody()['option']; 4265a78cd34SGreg Roach 4275a78cd34SGreg Roach $family = Family::getInstance($xref, $tree); 4285a78cd34SGreg Roach 4295a78cd34SGreg Roach if ($family === null) { 43059f2f229SGreg Roach throw new FamilyNotFoundException(); 4315a78cd34SGreg Roach } 4325a78cd34SGreg Roach 4335a78cd34SGreg Roach switch ($option) { 4345a78cd34SGreg Roach case 'parents': 4355a78cd34SGreg Roach $this->addFamilyToCart($family); 4365a78cd34SGreg Roach break; 4375a78cd34SGreg Roach 4385a78cd34SGreg Roach case 'members': 4395a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 4405a78cd34SGreg Roach break; 4415a78cd34SGreg Roach 4425a78cd34SGreg Roach case 'descendants': 4435a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($family); 4445a78cd34SGreg Roach break; 4455a78cd34SGreg Roach } 4465a78cd34SGreg Roach 4476ccdf4f0SGreg Roach return redirect($family->url()); 4485a78cd34SGreg Roach } 4495a78cd34SGreg Roach 4505a78cd34SGreg Roach /** 4515a78cd34SGreg Roach * @param Family $family 45218d7a90dSGreg Roach * 45318d7a90dSGreg Roach * @return void 4545a78cd34SGreg Roach */ 455e364afe4SGreg Roach private function addFamilyToCart(Family $family): void 456c1010edaSGreg Roach { 4575a78cd34SGreg Roach $this->addRecordToCart($family); 4585a78cd34SGreg Roach 45939ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 4605a78cd34SGreg Roach $this->addRecordToCart($spouse); 4615a78cd34SGreg Roach } 4625a78cd34SGreg Roach } 4635a78cd34SGreg Roach 4645a78cd34SGreg Roach /** 4655a78cd34SGreg Roach * @param Family $family 46618d7a90dSGreg Roach * 46718d7a90dSGreg Roach * @return void 4685a78cd34SGreg Roach */ 469e364afe4SGreg Roach private function addFamilyAndChildrenToCart(Family $family): void 470c1010edaSGreg Roach { 4715a78cd34SGreg Roach $this->addRecordToCart($family); 4725a78cd34SGreg Roach 47339ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 4745a78cd34SGreg Roach $this->addRecordToCart($spouse); 4755a78cd34SGreg Roach } 47639ca88baSGreg Roach foreach ($family->children() as $child) { 4775a78cd34SGreg Roach $this->addRecordToCart($child); 4785a78cd34SGreg Roach } 4795a78cd34SGreg Roach } 4805a78cd34SGreg Roach 4815a78cd34SGreg Roach /** 4825a78cd34SGreg Roach * @param Family $family 48318d7a90dSGreg Roach * 48418d7a90dSGreg Roach * @return void 4855a78cd34SGreg Roach */ 486e364afe4SGreg Roach private function addFamilyAndDescendantsToCart(Family $family): void 487c1010edaSGreg Roach { 4885a78cd34SGreg Roach $this->addRecordToCart($family); 4895a78cd34SGreg Roach 49039ca88baSGreg Roach foreach ($family->spouses() as $spouse) { 4915a78cd34SGreg Roach $this->addRecordToCart($spouse); 4925a78cd34SGreg Roach } 49339ca88baSGreg Roach foreach ($family->children() as $child) { 4945a78cd34SGreg Roach $this->addRecordToCart($child); 49539ca88baSGreg Roach foreach ($child->spouseFamilies() as $child_family) { 4965a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($child_family); 4975a78cd34SGreg Roach } 4985a78cd34SGreg Roach } 4995a78cd34SGreg Roach } 5005a78cd34SGreg Roach 5015a78cd34SGreg Roach /** 5026ccdf4f0SGreg Roach * @param ServerRequestInterface $request 503b6db7c1fSGreg Roach * @param Tree $tree 5045a78cd34SGreg Roach * 5056ccdf4f0SGreg Roach * @return ResponseInterface 5065a78cd34SGreg Roach */ 5076ccdf4f0SGreg Roach public function getAddIndividualAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 508c1010edaSGreg Roach { 509bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 5105a78cd34SGreg Roach 5115a78cd34SGreg Roach $individual = Individual::getInstance($xref, $tree); 5125a78cd34SGreg Roach 5135a78cd34SGreg Roach if ($individual === null) { 51459f2f229SGreg Roach throw new IndividualNotFoundException(); 5155a78cd34SGreg Roach } 5165a78cd34SGreg Roach 5175a78cd34SGreg Roach $options = $this->individualOptions($individual); 5185a78cd34SGreg Roach 51939ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $individual->fullName()); 5205a78cd34SGreg Roach 5215a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 5225a78cd34SGreg Roach 'options' => $options, 5235a78cd34SGreg Roach 'default' => key($options), 5245a78cd34SGreg Roach 'record' => $individual, 5255a78cd34SGreg Roach 'title' => $title, 5265a78cd34SGreg Roach 'tree' => $tree, 5275a78cd34SGreg Roach ]); 5285a78cd34SGreg Roach } 5295a78cd34SGreg Roach 5305a78cd34SGreg Roach /** 5315a78cd34SGreg Roach * @param Individual $individual 5325a78cd34SGreg Roach * 5335a78cd34SGreg Roach * @return string[] 5345a78cd34SGreg Roach */ 535c1010edaSGreg Roach private function individualOptions(Individual $individual): array 536c1010edaSGreg Roach { 53739ca88baSGreg Roach $name = strip_tags($individual->fullName()); 5385a78cd34SGreg Roach 53939ca88baSGreg Roach if ($individual->sex() === 'F') { 5405a78cd34SGreg Roach return [ 5415a78cd34SGreg Roach 'self' => $name, 5425a78cd34SGreg Roach 'parents' => I18N::translate('%s, her parents and siblings', $name), 5435a78cd34SGreg Roach 'spouses' => I18N::translate('%s, her spouses and children', $name), 5445a78cd34SGreg Roach 'ancestors' => I18N::translate('%s and her ancestors', $name), 5455a78cd34SGreg Roach 'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name), 5465a78cd34SGreg Roach 'descendants' => I18N::translate('%s, her spouses and descendants', $name), 5475a78cd34SGreg Roach ]; 548b2ce94c6SRico Sonntag } 549b2ce94c6SRico Sonntag 5505a78cd34SGreg Roach return [ 5515a78cd34SGreg Roach 'self' => $name, 5525a78cd34SGreg Roach 'parents' => I18N::translate('%s, his parents and siblings', $name), 5535a78cd34SGreg Roach 'spouses' => I18N::translate('%s, his spouses and children', $name), 5545a78cd34SGreg Roach 'ancestors' => I18N::translate('%s and his ancestors', $name), 5555a78cd34SGreg Roach 'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name), 5565a78cd34SGreg Roach 'descendants' => I18N::translate('%s, his spouses and descendants', $name), 5575a78cd34SGreg Roach ]; 5585a78cd34SGreg Roach } 5595a78cd34SGreg Roach 5605a78cd34SGreg Roach /** 5616ccdf4f0SGreg Roach * @param ServerRequestInterface $request 562b6db7c1fSGreg Roach * @param Tree $tree 5635a78cd34SGreg Roach * 5646ccdf4f0SGreg Roach * @return ResponseInterface 5655a78cd34SGreg Roach */ 5666ccdf4f0SGreg Roach public function postAddIndividualAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 567c1010edaSGreg Roach { 568bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 569bed27cedSGreg Roach $option = $request->getParsedBody()['option']; 5705a78cd34SGreg Roach 5715a78cd34SGreg Roach $individual = Individual::getInstance($xref, $tree); 5725a78cd34SGreg Roach 5735a78cd34SGreg Roach if ($individual === null) { 57459f2f229SGreg Roach throw new IndividualNotFoundException(); 5755a78cd34SGreg Roach } 5765a78cd34SGreg Roach 5775a78cd34SGreg Roach switch ($option) { 5785a78cd34SGreg Roach case 'self': 5795a78cd34SGreg Roach $this->addRecordToCart($individual); 5805a78cd34SGreg Roach break; 5815a78cd34SGreg Roach 5825a78cd34SGreg Roach case 'parents': 58339ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 5845a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 5855a78cd34SGreg Roach } 5865a78cd34SGreg Roach break; 5875a78cd34SGreg Roach 5885a78cd34SGreg Roach case 'spouses': 58939ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 5905a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 5915a78cd34SGreg Roach } 5925a78cd34SGreg Roach break; 5935a78cd34SGreg Roach 5945a78cd34SGreg Roach case 'ancestors': 5955a78cd34SGreg Roach $this->addAncestorsToCart($individual); 5965a78cd34SGreg Roach break; 5975a78cd34SGreg Roach 5985a78cd34SGreg Roach case 'ancestor_families': 5995a78cd34SGreg Roach $this->addAncestorFamiliesToCart($individual); 6005a78cd34SGreg Roach break; 6015a78cd34SGreg Roach 6025a78cd34SGreg Roach case 'descendants': 60339ca88baSGreg Roach foreach ($individual->spouseFamilies() as $family) { 6045a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($family); 6055a78cd34SGreg Roach } 6065a78cd34SGreg Roach break; 6075a78cd34SGreg Roach } 6085a78cd34SGreg Roach 6096ccdf4f0SGreg Roach return redirect($individual->url()); 6105a78cd34SGreg Roach } 6115a78cd34SGreg Roach 6125a78cd34SGreg Roach /** 6135a78cd34SGreg Roach * @param Individual $individual 61418d7a90dSGreg Roach * 61518d7a90dSGreg Roach * @return void 6165a78cd34SGreg Roach */ 617e364afe4SGreg Roach private function addAncestorsToCart(Individual $individual): void 618c1010edaSGreg Roach { 6195a78cd34SGreg Roach $this->addRecordToCart($individual); 6205a78cd34SGreg Roach 62139ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 62239ca88baSGreg Roach foreach ($family->spouses() as $parent) { 6235a78cd34SGreg Roach $this->addAncestorsToCart($parent); 6245a78cd34SGreg Roach } 6255a78cd34SGreg Roach } 6265a78cd34SGreg Roach } 6275a78cd34SGreg Roach 6285a78cd34SGreg Roach /** 6295a78cd34SGreg Roach * @param Individual $individual 63018d7a90dSGreg Roach * 63118d7a90dSGreg Roach * @return void 6325a78cd34SGreg Roach */ 633e364afe4SGreg Roach private function addAncestorFamiliesToCart(Individual $individual): void 634c1010edaSGreg Roach { 63539ca88baSGreg Roach foreach ($individual->childFamilies() as $family) { 6365a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 63739ca88baSGreg Roach foreach ($family->spouses() as $parent) { 6385a78cd34SGreg Roach $this->addAncestorsToCart($parent); 6395a78cd34SGreg Roach } 6405a78cd34SGreg Roach } 6415a78cd34SGreg Roach } 6425a78cd34SGreg Roach 6435a78cd34SGreg Roach /** 6446ccdf4f0SGreg Roach * @param ServerRequestInterface $request 645b6db7c1fSGreg Roach * @param Tree $tree 6465a78cd34SGreg Roach * 6476ccdf4f0SGreg Roach * @return ResponseInterface 6485a78cd34SGreg Roach */ 6496ccdf4f0SGreg Roach public function getAddMediaAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 650c1010edaSGreg Roach { 651bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 6525a78cd34SGreg Roach 6535a78cd34SGreg Roach $media = Media::getInstance($xref, $tree); 6545a78cd34SGreg Roach 6555a78cd34SGreg Roach if ($media === null) { 65659f2f229SGreg Roach throw new MediaNotFoundException(); 6575a78cd34SGreg Roach } 6585a78cd34SGreg Roach 6595a78cd34SGreg Roach $options = $this->mediaOptions($media); 6605a78cd34SGreg Roach 66139ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $media->fullName()); 6625a78cd34SGreg Roach 6635a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 6645a78cd34SGreg Roach 'options' => $options, 6655a78cd34SGreg Roach 'default' => key($options), 6665a78cd34SGreg Roach 'record' => $media, 6675a78cd34SGreg Roach 'title' => $title, 6685a78cd34SGreg Roach 'tree' => $tree, 6695a78cd34SGreg Roach ]); 6705a78cd34SGreg Roach } 6715a78cd34SGreg Roach 6725a78cd34SGreg Roach /** 6735a78cd34SGreg Roach * @param Media $media 6745a78cd34SGreg Roach * 6755a78cd34SGreg Roach * @return string[] 6765a78cd34SGreg Roach */ 677c1010edaSGreg Roach private function mediaOptions(Media $media): array 678c1010edaSGreg Roach { 67939ca88baSGreg Roach $name = strip_tags($media->fullName()); 6805a78cd34SGreg Roach 6815a78cd34SGreg Roach return [ 6825a78cd34SGreg Roach 'self' => $name, 6835a78cd34SGreg Roach ]; 6845a78cd34SGreg Roach } 6855a78cd34SGreg Roach 6865a78cd34SGreg Roach /** 6876ccdf4f0SGreg Roach * @param ServerRequestInterface $request 688b6db7c1fSGreg Roach * @param Tree $tree 6895a78cd34SGreg Roach * 6906ccdf4f0SGreg Roach * @return ResponseInterface 6915a78cd34SGreg Roach */ 6926ccdf4f0SGreg Roach public function postAddMediaAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 693c1010edaSGreg Roach { 694bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 6955a78cd34SGreg Roach 6965a78cd34SGreg Roach $media = Media::getInstance($xref, $tree); 6975a78cd34SGreg Roach 6985a78cd34SGreg Roach if ($media === null) { 69959f2f229SGreg Roach throw new MediaNotFoundException(); 7005a78cd34SGreg Roach } 7015a78cd34SGreg Roach 7025a78cd34SGreg Roach $this->addRecordToCart($media); 7035a78cd34SGreg Roach 7046ccdf4f0SGreg Roach return redirect($media->url()); 7055a78cd34SGreg Roach } 7065a78cd34SGreg Roach 7075a78cd34SGreg Roach /** 7086ccdf4f0SGreg Roach * @param ServerRequestInterface $request 709b6db7c1fSGreg Roach * @param Tree $tree 7105a78cd34SGreg Roach * 7116ccdf4f0SGreg Roach * @return ResponseInterface 7125a78cd34SGreg Roach */ 7136ccdf4f0SGreg Roach public function getAddNoteAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 714c1010edaSGreg Roach { 715bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 7165a78cd34SGreg Roach 7175a78cd34SGreg Roach $note = Note::getInstance($xref, $tree); 7185a78cd34SGreg Roach 7195a78cd34SGreg Roach if ($note === null) { 72059f2f229SGreg Roach throw new NoteNotFoundException(); 7215a78cd34SGreg Roach } 7225a78cd34SGreg Roach 7235a78cd34SGreg Roach $options = $this->noteOptions($note); 7245a78cd34SGreg Roach 72539ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $note->fullName()); 7265a78cd34SGreg Roach 7275a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 7285a78cd34SGreg Roach 'options' => $options, 7295a78cd34SGreg Roach 'default' => key($options), 7305a78cd34SGreg Roach 'record' => $note, 7315a78cd34SGreg Roach 'title' => $title, 7325a78cd34SGreg Roach 'tree' => $tree, 7335a78cd34SGreg Roach ]); 7345a78cd34SGreg Roach } 7355a78cd34SGreg Roach 7365a78cd34SGreg Roach /** 7375a78cd34SGreg Roach * @param Note $note 7385a78cd34SGreg Roach * 7395a78cd34SGreg Roach * @return string[] 7405a78cd34SGreg Roach */ 741c1010edaSGreg Roach private function noteOptions(Note $note): array 742c1010edaSGreg Roach { 74339ca88baSGreg Roach $name = strip_tags($note->fullName()); 7445a78cd34SGreg Roach 7455a78cd34SGreg Roach return [ 7465a78cd34SGreg Roach 'self' => $name, 7475a78cd34SGreg Roach ]; 7485a78cd34SGreg Roach } 7495a78cd34SGreg Roach 7505a78cd34SGreg Roach /** 7516ccdf4f0SGreg Roach * @param ServerRequestInterface $request 752b6db7c1fSGreg Roach * @param Tree $tree 7535a78cd34SGreg Roach * 7546ccdf4f0SGreg Roach * @return ResponseInterface 7555a78cd34SGreg Roach */ 7566ccdf4f0SGreg Roach public function postAddNoteAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 757c1010edaSGreg Roach { 758bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 7595a78cd34SGreg Roach 7605a78cd34SGreg Roach $note = Note::getInstance($xref, $tree); 7615a78cd34SGreg Roach 7625a78cd34SGreg Roach if ($note === null) { 76359f2f229SGreg Roach throw new NoteNotFoundException(); 7645a78cd34SGreg Roach } 7655a78cd34SGreg Roach 7665a78cd34SGreg Roach $this->addRecordToCart($note); 7675a78cd34SGreg Roach 7686ccdf4f0SGreg Roach return redirect($note->url()); 7695a78cd34SGreg Roach } 7705a78cd34SGreg Roach 7715a78cd34SGreg Roach /** 7726ccdf4f0SGreg Roach * @param ServerRequestInterface $request 773b6db7c1fSGreg Roach * @param Tree $tree 7745a78cd34SGreg Roach * 7756ccdf4f0SGreg Roach * @return ResponseInterface 7765a78cd34SGreg Roach */ 7776ccdf4f0SGreg Roach public function getAddRepositoryAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 778c1010edaSGreg Roach { 779bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 7805a78cd34SGreg Roach 7815a78cd34SGreg Roach $repository = Repository::getInstance($xref, $tree); 7825a78cd34SGreg Roach 7835a78cd34SGreg Roach if ($repository === null) { 78459f2f229SGreg Roach throw new RepositoryNotFoundException(); 7855a78cd34SGreg Roach } 7865a78cd34SGreg Roach 7875a78cd34SGreg Roach $options = $this->repositoryOptions($repository); 7885a78cd34SGreg Roach 78939ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $repository->fullName()); 7905a78cd34SGreg Roach 7915a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 7925a78cd34SGreg Roach 'options' => $options, 7935a78cd34SGreg Roach 'default' => key($options), 7945a78cd34SGreg Roach 'record' => $repository, 7955a78cd34SGreg Roach 'title' => $title, 7965a78cd34SGreg Roach 'tree' => $tree, 7975a78cd34SGreg Roach ]); 7985a78cd34SGreg Roach } 7995a78cd34SGreg Roach 8005a78cd34SGreg Roach /** 8015a78cd34SGreg Roach * @param Repository $repository 8025a78cd34SGreg Roach * 8035a78cd34SGreg Roach * @return string[] 8045a78cd34SGreg Roach */ 805c1010edaSGreg Roach private function repositoryOptions(Repository $repository): array 806c1010edaSGreg Roach { 80739ca88baSGreg Roach $name = strip_tags($repository->fullName()); 8085a78cd34SGreg Roach 8095a78cd34SGreg Roach return [ 8105a78cd34SGreg Roach 'self' => $name, 8115a78cd34SGreg Roach ]; 8125a78cd34SGreg Roach } 8135a78cd34SGreg Roach 8145a78cd34SGreg Roach /** 8156ccdf4f0SGreg Roach * @param ServerRequestInterface $request 816b6db7c1fSGreg Roach * @param Tree $tree 8175a78cd34SGreg Roach * 8186ccdf4f0SGreg Roach * @return ResponseInterface 8195a78cd34SGreg Roach */ 8206ccdf4f0SGreg Roach public function postAddRepositoryAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 821c1010edaSGreg Roach { 822bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8235a78cd34SGreg Roach 8245a78cd34SGreg Roach $repository = Repository::getInstance($xref, $tree); 8255a78cd34SGreg Roach 8265a78cd34SGreg Roach if ($repository === null) { 82759f2f229SGreg Roach throw new RepositoryNotFoundException(); 8285a78cd34SGreg Roach } 8295a78cd34SGreg Roach 8305a78cd34SGreg Roach $this->addRecordToCart($repository); 8315a78cd34SGreg Roach 8326ccdf4f0SGreg Roach return redirect($repository->url()); 8335a78cd34SGreg Roach } 8345a78cd34SGreg Roach 8355a78cd34SGreg Roach /** 8366ccdf4f0SGreg Roach * @param ServerRequestInterface $request 837b6db7c1fSGreg Roach * @param Tree $tree 8385a78cd34SGreg Roach * 8396ccdf4f0SGreg Roach * @return ResponseInterface 8405a78cd34SGreg Roach */ 8416ccdf4f0SGreg Roach public function getAddSourceAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 842c1010edaSGreg Roach { 843bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 8445a78cd34SGreg Roach 8455a78cd34SGreg Roach $source = Source::getInstance($xref, $tree); 8465a78cd34SGreg Roach 8475a78cd34SGreg Roach if ($source === null) { 84859f2f229SGreg Roach throw new SourceNotFoundException(); 8495a78cd34SGreg Roach } 8505a78cd34SGreg Roach 8515a78cd34SGreg Roach $options = $this->sourceOptions($source); 8525a78cd34SGreg Roach 85339ca88baSGreg Roach $title = I18N::translate('Add %s to the clippings cart', $source->fullName()); 8545a78cd34SGreg Roach 8555a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 8565a78cd34SGreg Roach 'options' => $options, 8575a78cd34SGreg Roach 'default' => key($options), 8585a78cd34SGreg Roach 'record' => $source, 8595a78cd34SGreg Roach 'title' => $title, 8605a78cd34SGreg Roach 'tree' => $tree, 8615a78cd34SGreg Roach ]); 8625a78cd34SGreg Roach } 8635a78cd34SGreg Roach 8645a78cd34SGreg Roach /** 8655a78cd34SGreg Roach * @param Source $source 8665a78cd34SGreg Roach * 8675a78cd34SGreg Roach * @return string[] 8685a78cd34SGreg Roach */ 869c1010edaSGreg Roach private function sourceOptions(Source $source): array 870c1010edaSGreg Roach { 87139ca88baSGreg Roach $name = strip_tags($source->fullName()); 8725a78cd34SGreg Roach 8735a78cd34SGreg Roach return [ 87439ca88baSGreg Roach 'only' => strip_tags($source->fullName()), 8755a78cd34SGreg Roach 'linked' => I18N::translate('%s and the individuals that reference it.', $name), 8765a78cd34SGreg Roach ]; 8775a78cd34SGreg Roach } 8785a78cd34SGreg Roach 8795a78cd34SGreg Roach /** 8806ccdf4f0SGreg Roach * @param ServerRequestInterface $request 881b6db7c1fSGreg Roach * @param Tree $tree 8825a78cd34SGreg Roach * 8836ccdf4f0SGreg Roach * @return ResponseInterface 8845a78cd34SGreg Roach */ 8856ccdf4f0SGreg Roach public function postAddSourceAction(ServerRequestInterface $request, Tree $tree): ResponseInterface 886c1010edaSGreg Roach { 887bed27cedSGreg Roach $xref = $request->getQueryParams()['xref']; 888bed27cedSGreg Roach $option = $request->getParsedBody()['option']; 8895a78cd34SGreg Roach 8905a78cd34SGreg Roach $source = Source::getInstance($xref, $tree); 8915a78cd34SGreg Roach 8925a78cd34SGreg Roach if ($source === null) { 89359f2f229SGreg Roach throw new SourceNotFoundException(); 8945a78cd34SGreg Roach } 8955a78cd34SGreg Roach 8965a78cd34SGreg Roach $this->addRecordToCart($source); 8975a78cd34SGreg Roach 8985a78cd34SGreg Roach if ($option === 'linked') { 8995a78cd34SGreg Roach foreach ($source->linkedIndividuals('SOUR') as $individual) { 9005a78cd34SGreg Roach $this->addRecordToCart($individual); 9015a78cd34SGreg Roach } 9025a78cd34SGreg Roach foreach ($source->linkedFamilies('SOUR') as $family) { 9035a78cd34SGreg Roach $this->addRecordToCart($family); 9045a78cd34SGreg Roach } 9055a78cd34SGreg Roach } 9065a78cd34SGreg Roach 9076ccdf4f0SGreg Roach return redirect($source->url()); 9085a78cd34SGreg Roach } 9095a78cd34SGreg Roach 9105a78cd34SGreg Roach /** 9115a78cd34SGreg Roach * Get all the records in the cart. 9125a78cd34SGreg Roach * 9135a78cd34SGreg Roach * @param Tree $tree 9145a78cd34SGreg Roach * 9155a78cd34SGreg Roach * @return GedcomRecord[] 9165a78cd34SGreg Roach */ 917c1010edaSGreg Roach private function allRecordsInCart(Tree $tree): array 918c1010edaSGreg Roach { 9195a78cd34SGreg Roach $cart = Session::get('cart', []); 9205a78cd34SGreg Roach 921aa6f03bbSGreg Roach $xrefs = array_keys($cart[$tree->name()] ?? []); 9225a78cd34SGreg Roach 9235a78cd34SGreg Roach // Fetch all the records in the cart. 924bed27cedSGreg Roach $records = array_map(static function (string $xref) use ($tree): ?GedcomRecord { 9255a78cd34SGreg Roach return GedcomRecord::getInstance($xref, $tree); 9265a78cd34SGreg Roach }, $xrefs); 9275a78cd34SGreg Roach 9285a78cd34SGreg Roach // Some records may have been deleted after they were added to the cart. 9295a78cd34SGreg Roach $records = array_filter($records); 9305a78cd34SGreg Roach 9315a78cd34SGreg Roach // Group and sort. 9320b5fd0a6SGreg Roach uasort($records, static function (GedcomRecord $x, GedcomRecord $y): int { 933c156e8f5SGreg Roach return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::nameComparator()($x, $y); 9345a78cd34SGreg Roach }); 9355a78cd34SGreg Roach 9365a78cd34SGreg Roach return $records; 9375a78cd34SGreg Roach } 9385a78cd34SGreg Roach 9395a78cd34SGreg Roach /** 9405a78cd34SGreg Roach * Add a record (and direclty linked sources, notes, etc. to the cart. 9415a78cd34SGreg Roach * 9425a78cd34SGreg Roach * @param GedcomRecord $record 94318d7a90dSGreg Roach * 94418d7a90dSGreg Roach * @return void 9455a78cd34SGreg Roach */ 946e364afe4SGreg Roach private function addRecordToCart(GedcomRecord $record): void 947c1010edaSGreg Roach { 9485a78cd34SGreg Roach $cart = Session::get('cart', []); 9495a78cd34SGreg Roach 950f4afa648SGreg Roach $tree_name = $record->tree()->name(); 9515a78cd34SGreg Roach 9525a78cd34SGreg Roach // Add this record 953c0935879SGreg Roach $cart[$tree_name][$record->xref()] = true; 9545a78cd34SGreg Roach 9555a78cd34SGreg Roach // Add directly linked media, notes, repositories and sources. 9568d0ebef0SGreg Roach preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . Gedcom::REGEX_XREF . ')@/', $record->gedcom(), $matches); 9575a78cd34SGreg Roach 9585a78cd34SGreg Roach foreach ($matches[1] as $match) { 9595a78cd34SGreg Roach $cart[$tree_name][$match] = true; 9605a78cd34SGreg Roach } 9615a78cd34SGreg Roach 9625a78cd34SGreg Roach Session::put('cart', $cart); 9635a78cd34SGreg Roach } 9645a78cd34SGreg Roach 9655a78cd34SGreg Roach /** 9665a78cd34SGreg Roach * @param Tree $tree 9675a78cd34SGreg Roach * 9685a78cd34SGreg Roach * @return bool 9695a78cd34SGreg Roach */ 970c1010edaSGreg Roach private function isCartEmpty(Tree $tree): bool 971c1010edaSGreg Roach { 9725a78cd34SGreg Roach $cart = Session::get('cart', []); 9735a78cd34SGreg Roach 974aa6f03bbSGreg Roach return empty($cart[$tree->name()]); 9755a78cd34SGreg Roach } 9768c2e8227SGreg Roach} 977