18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 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 */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 1776692c8bSGreg Roach 180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 195a78cd34SGreg Roachuse Fisharebest\Webtrees\Database; 20*0bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\FamilyNotFoundException; 21*0bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\IndividualNotFoundException; 22*0bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\MediaNotFoundException; 23*0bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\NoteNotFoundException; 24*0bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\RepositoryNotFoundException; 25*0bc54ba3SGreg Roachuse Fisharebest\Webtrees\Exceptions\SourceNotFoundException; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\Family; 275a78cd34SGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsExport; 285a78cd34SGreg Roachuse Fisharebest\Webtrees\Gedcom; 290e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 300e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 310e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual; 325a78cd34SGreg Roachuse Fisharebest\Webtrees\Media; 330e62c4b8SGreg Roachuse Fisharebest\Webtrees\Menu; 341eca16b3SGreg Roachuse Fisharebest\Webtrees\Module; 355a78cd34SGreg Roachuse Fisharebest\Webtrees\Note; 365a78cd34SGreg Roachuse Fisharebest\Webtrees\Repository; 370e62c4b8SGreg Roachuse Fisharebest\Webtrees\Session; 385a78cd34SGreg Roachuse Fisharebest\Webtrees\Source; 39aee13b6dSGreg Roachuse Fisharebest\Webtrees\Tree; 405a78cd34SGreg Roachuse Fisharebest\Webtrees\User; 415a78cd34SGreg Roachuse League\Flysystem\Filesystem; 425a78cd34SGreg Roachuse League\Flysystem\ZipArchive\ZipArchiveAdapter; 435a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\BinaryFileResponse; 445a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse; 455a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\Request; 465a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\Response; 475a78cd34SGreg Roachuse Symfony\Component\HttpFoundation\ResponseHeaderBag; 485a78cd34SGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 498c2e8227SGreg Roach 508c2e8227SGreg Roach/** 518c2e8227SGreg Roach * Class ClippingsCartModule 528c2e8227SGreg Roach */ 535a78cd34SGreg Roachclass ClippingsCartModule extends AbstractModule implements ModuleMenuInterface { 545a78cd34SGreg Roach // Routes that have a record which can be added to the clipboard 555a78cd34SGreg Roach const ROUTES_WITH_RECORDS = ['family', 'individual', 'media', 'note', 'repository', 'source']; 565a78cd34SGreg Roach 578c2e8227SGreg Roach /** {@inheritdoc} */ 588c2e8227SGreg Roach public function getTitle() { 59a86dd8b1SGreg Roach return /* I18N: Name of a module */ 60a86dd8b1SGreg Roach I18N::translate('Clippings cart'); 618c2e8227SGreg Roach } 628c2e8227SGreg Roach 638c2e8227SGreg Roach /** {@inheritdoc} */ 648c2e8227SGreg Roach public function getDescription() { 65a86dd8b1SGreg Roach return /* I18N: Description of the “Clippings cart” module */ 663bf19670SGreg Roach I18N::translate('Select records from your family tree and save them as a GEDCOM file.'); 678c2e8227SGreg Roach } 688c2e8227SGreg Roach 690ee13198SGreg Roach /** 700ee13198SGreg Roach * What is the default access level for this module? 710ee13198SGreg Roach * 720ee13198SGreg Roach * Some modules are aimed at admins or managers, and are not generally shown to users. 730ee13198SGreg Roach * 740ee13198SGreg Roach * @return int 750ee13198SGreg Roach */ 768c2e8227SGreg Roach public function defaultAccessLevel() { 774b9ff166SGreg Roach return Auth::PRIV_USER; 788c2e8227SGreg Roach } 798c2e8227SGreg Roach 8076692c8bSGreg Roach /** 810ee13198SGreg Roach * The user can re-order menus. Until they do, they are shown in this order. 820ee13198SGreg Roach * 830ee13198SGreg Roach * @return int 840ee13198SGreg Roach */ 858c2e8227SGreg Roach public function defaultMenuOrder() { 868c2e8227SGreg Roach return 20; 878c2e8227SGreg Roach } 888c2e8227SGreg Roach 890ee13198SGreg Roach /** 900ee13198SGreg Roach * A menu, to be added to the main application menu. 910ee13198SGreg Roach * 92aee13b6dSGreg Roach * @param Tree $tree 93aee13b6dSGreg Roach * 940ee13198SGreg Roach * @return Menu|null 950ee13198SGreg Roach */ 96aee13b6dSGreg Roach public function getMenu(Tree $tree) { 975a78cd34SGreg Roach $request = Request::createFromGlobals(); 988c2e8227SGreg Roach 995a78cd34SGreg Roach $route = $request->get('route'); 1005a78cd34SGreg Roach 1015a78cd34SGreg Roach $submenus = [ 1025a78cd34SGreg Roach new Menu($this->getTitle(), e(route('module', ['module' => 'clippings', 'action' => 'Show', 'ged' => $tree->getName()])), 'menu-clippings-cart', ['rel' => 'nofollow']), 1035a78cd34SGreg Roach ]; 1045a78cd34SGreg Roach 1055a78cd34SGreg Roach if (in_array($route, self::ROUTES_WITH_RECORDS)) { 1065a78cd34SGreg Roach $xref = $request->get('xref'); 1075a78cd34SGreg Roach $action = 'Add' . ucfirst($route); 1085a78cd34SGreg Roach $add_route = route('module', ['module' => 'clippings', 'action' => $action, 'xref' => $xref, 'ged' => $tree->getName()]); 1095a78cd34SGreg Roach 1105a78cd34SGreg Roach $submenus[] = new Menu(I18N::translate('Add to the clippings cart'), e($add_route), 'menu-clippings-add', ['rel' => 'nofollow']); 1118c2e8227SGreg Roach } 112cbc1590aSGreg Roach 1135a78cd34SGreg Roach if (!$this->isCartEmpty($tree)) { 1145a78cd34SGreg Roach $submenus[] = new Menu(I18N::translate('Empty the clippings cart'), e(route('module', ['module' => 'clippings', 'action' => 'Empty', 'ged' => $tree->getName()])), 'menu-clippings-empty', ['rel' => 'nofollow']); 1155a78cd34SGreg Roach $submenus[] = new Menu(I18N::translate('Download'), e(route('module', ['module' => 'clippings', 'action' => 'DownloadForm', 'ged' => $tree->getName()])), 'menu-clippings-download', ['rel' => 'nofollow']); 1165a78cd34SGreg Roach } 1175a78cd34SGreg Roach 11813abd6f3SGreg Roach return new Menu($this->getTitle(), '#', 'menu-clippings', ['rel' => 'nofollow'], $submenus); 1198c2e8227SGreg Roach } 1208c2e8227SGreg Roach 12176692c8bSGreg Roach /** 1225a78cd34SGreg Roach * @param Request $request 12376692c8bSGreg Roach * 1245a78cd34SGreg Roach * @return BinaryFileResponse 12576692c8bSGreg Roach */ 1265a78cd34SGreg Roach public function getDownloadAction(Request $request): BinaryFileResponse { 1275a78cd34SGreg Roach /** @var Tree $tree */ 1285a78cd34SGreg Roach $tree = $request->attributes->get('tree'); 1298c2e8227SGreg Roach 1305a78cd34SGreg Roach $this->checkModuleAccess($tree); 1318c2e8227SGreg Roach 1325a78cd34SGreg Roach $privatize_export = $request->get('privatize_export'); 1335a78cd34SGreg Roach $convert = (bool) $request->get('convert'); 1348c2e8227SGreg Roach 13513abd6f3SGreg Roach $cart = Session::get('cart', []); 1368c2e8227SGreg Roach 1375a78cd34SGreg Roach $xrefs = array_keys($cart[$tree->getName()] ?? []); 1385a78cd34SGreg Roach 1395a78cd34SGreg Roach // Create a new/empty .ZIP file 1405a78cd34SGreg Roach $temp_zip_file = tempnam(sys_get_temp_dir(), 'webtrees-zip-'); 1415a78cd34SGreg Roach $zip_filesystem = new Filesystem(new ZipArchiveAdapter($temp_zip_file)); 1425a78cd34SGreg Roach 1435a78cd34SGreg Roach // Media file prefix 1445a78cd34SGreg Roach $path = $tree->getPreference('MEDIA_DIRECTORY'); 1455a78cd34SGreg Roach 1465a78cd34SGreg Roach // GEDCOM file header 1475a78cd34SGreg Roach $filetext = FunctionsExport::gedcomHeader($tree); 1485a78cd34SGreg Roach 1495a78cd34SGreg Roach // Include SUBM/SUBN records, if they exist 1505a78cd34SGreg Roach $subn = 1515a78cd34SGreg Roach Database::prepare("SELECT o_gedcom FROM `##other` WHERE o_type=? AND o_file=?") 1525a78cd34SGreg Roach ->execute(['SUBN', $tree->getName()]) 1535a78cd34SGreg Roach ->fetchOne(); 1545a78cd34SGreg Roach if ($subn) { 1555a78cd34SGreg Roach $filetext .= $subn . "\n"; 156b6468bb9SGreg Roach } 1575a78cd34SGreg Roach $subm = 1585a78cd34SGreg Roach Database::prepare("SELECT o_gedcom FROM `##other` WHERE o_type=? AND o_file=?") 1595a78cd34SGreg Roach ->execute(['SUBM', $tree->getName()]) 1605a78cd34SGreg Roach ->fetchOne(); 1615a78cd34SGreg Roach if ($subm) { 1625a78cd34SGreg Roach $filetext .= $subm . "\n"; 1635a78cd34SGreg Roach } 1645a78cd34SGreg Roach 1655a78cd34SGreg Roach switch ($privatize_export) { 1665a78cd34SGreg Roach case 'gedadmin': 1675a78cd34SGreg Roach $access_level = Auth::PRIV_NONE; 1685a78cd34SGreg Roach break; 1695a78cd34SGreg Roach case 'user': 1705a78cd34SGreg Roach $access_level = Auth::PRIV_USER; 1715a78cd34SGreg Roach break; 1725a78cd34SGreg Roach case 'visitor': 1735a78cd34SGreg Roach $access_level = Auth::PRIV_PRIVATE; 1745a78cd34SGreg Roach break; 1755a78cd34SGreg Roach case 'none': 1765a78cd34SGreg Roach default: 1775a78cd34SGreg Roach $access_level = Auth::PRIV_HIDE; 1785a78cd34SGreg Roach break; 1795a78cd34SGreg Roach } 1805a78cd34SGreg Roach 1815a78cd34SGreg Roach foreach ($xrefs as $xref) { 1825a78cd34SGreg Roach $object = GedcomRecord::getInstance($xref, $tree); 1835a78cd34SGreg Roach // The object may have been deleted since we added it to the cart.... 1845a78cd34SGreg Roach if ($object) { 1855a78cd34SGreg Roach $record = $object->privatizeGedcom($access_level); 1865a78cd34SGreg Roach // Remove links to objects that aren't in the cart 1875a78cd34SGreg Roach preg_match_all('/\n1 ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@(\n[2-9].*)*/', $record, $matches, PREG_SET_ORDER); 1885a78cd34SGreg Roach foreach ($matches as $match) { 1895a78cd34SGreg Roach if (!array_key_exists($match[1], $xrefs)) { 1905a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 1915a78cd34SGreg Roach } 1925a78cd34SGreg Roach } 1935a78cd34SGreg Roach preg_match_all('/\n2 ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@(\n[3-9].*)*/', $record, $matches, PREG_SET_ORDER); 1945a78cd34SGreg Roach foreach ($matches as $match) { 1955a78cd34SGreg Roach if (!array_key_exists($match[1], $xrefs)) { 1965a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 1975a78cd34SGreg Roach } 1985a78cd34SGreg Roach } 1995a78cd34SGreg Roach preg_match_all('/\n3 ' . WT_REGEX_TAG . ' @(' . WT_REGEX_XREF . ')@(\n[4-9].*)*/', $record, $matches, PREG_SET_ORDER); 2005a78cd34SGreg Roach foreach ($matches as $match) { 2015a78cd34SGreg Roach if (!array_key_exists($match[1], $xrefs)) { 2025a78cd34SGreg Roach $record = str_replace($match[0], '', $record); 2035a78cd34SGreg Roach } 2045a78cd34SGreg Roach } 2055a78cd34SGreg Roach 2065a78cd34SGreg Roach if ($convert) { 2075a78cd34SGreg Roach $record = utf8_decode($record); 2085a78cd34SGreg Roach } 2095a78cd34SGreg Roach switch ($object::RECORD_TYPE) { 2108c2e8227SGreg Roach case 'INDI': 2118c2e8227SGreg Roach case 'FAM': 2125a78cd34SGreg Roach $filetext .= $record . "\n"; 2135a78cd34SGreg Roach $filetext .= "1 SOUR @WEBTREES@\n"; 2145a78cd34SGreg Roach $filetext .= '2 PAGE ' . WT_BASE_URL . $object->url() . "\n"; 2155a78cd34SGreg Roach break; 2165a78cd34SGreg Roach case 'SOUR': 2175a78cd34SGreg Roach $filetext .= $record . "\n"; 2185a78cd34SGreg Roach $filetext .= '1 NOTE ' . WT_BASE_URL . $object->url() . "\n"; 2195a78cd34SGreg Roach break; 2205a78cd34SGreg Roach case 'OBJE': 2215a78cd34SGreg Roach // Add the file to the archive 2225a78cd34SGreg Roach foreach ($object->mediaFiles() as $media_file) { 2235a78cd34SGreg Roach if (file_exists($media_file->getServerFilename())) { 2245a78cd34SGreg Roach $fp = fopen($media_file->getServerFilename(), 'r'); 2255a78cd34SGreg Roach $zip_filesystem->writeStream($path . $media_file->filename(), $fp); 2265a78cd34SGreg Roach fclose($fp); 2275a78cd34SGreg Roach } 2285a78cd34SGreg Roach } 2295a78cd34SGreg Roach $filetext .= $record . "\n"; 2305a78cd34SGreg Roach break; 2315a78cd34SGreg Roach default: 2325a78cd34SGreg Roach $filetext .= $record . "\n"; 2338c2e8227SGreg Roach break; 2348c2e8227SGreg Roach } 2358c2e8227SGreg Roach } 2368c2e8227SGreg Roach } 2378c2e8227SGreg Roach 2385a78cd34SGreg Roach // Create a source, to indicate the source of the data. 2395a78cd34SGreg Roach $filetext .= "0 @WEBTREES@ SOUR\n1 TITL " . WT_BASE_URL . "\n"; 2405a78cd34SGreg Roach $author = User::find($tree->getPreference('CONTACT_EMAIL')); 2415a78cd34SGreg Roach if ($author !== null) { 2425a78cd34SGreg Roach $filetext .= '1 AUTH ' . $author->getRealName() . "\n"; 2435a78cd34SGreg Roach } 2445a78cd34SGreg Roach $filetext .= "0 TRLR\n"; 2455a78cd34SGreg Roach 2465a78cd34SGreg Roach // Make sure the preferred line endings are used 2475a78cd34SGreg Roach $filetext = preg_replace("/[\r\n]+/", Gedcom::EOL, $filetext); 2485a78cd34SGreg Roach 2495a78cd34SGreg Roach if ($convert === 'yes') { 2505a78cd34SGreg Roach $filetext = str_replace('UTF-8', 'ANSI', $filetext); 2515a78cd34SGreg Roach $filetext = utf8_decode($filetext); 2528c2e8227SGreg Roach } 253cbc1590aSGreg Roach 2545a78cd34SGreg Roach // Finally add the GEDCOM file to the .ZIP file. 2555a78cd34SGreg Roach $zip_filesystem->write('clippings.ged', $filetext); 2565a78cd34SGreg Roach 2575a78cd34SGreg Roach // Need to force-close the filesystem 2585a78cd34SGreg Roach $zip_filesystem = null; 2595a78cd34SGreg Roach 2605a78cd34SGreg Roach $response = new BinaryFileResponse($temp_zip_file); 2615a78cd34SGreg Roach $response->deleteFileAfterSend(true); 2625a78cd34SGreg Roach 2635a78cd34SGreg Roach $response->headers->set('Content-Type', 'application/zip'); 2645a78cd34SGreg Roach $response->setContentDisposition( 2655a78cd34SGreg Roach ResponseHeaderBag::DISPOSITION_ATTACHMENT, 2665a78cd34SGreg Roach 'clippings.zip' 2675a78cd34SGreg Roach ); 2685a78cd34SGreg Roach 2695a78cd34SGreg Roach return $response; 2708c2e8227SGreg Roach } 2718c2e8227SGreg Roach 2728c2e8227SGreg Roach /** 2735a78cd34SGreg Roach * @param Request $request 27476692c8bSGreg Roach * 2755a78cd34SGreg Roach * @return Response 2768c2e8227SGreg Roach */ 2775a78cd34SGreg Roach public function getDownloadFormAction(Request $request): Response { 2785a78cd34SGreg Roach /** @var Tree $tree */ 2795a78cd34SGreg Roach $tree = $request->attributes->get('tree'); 2808c2e8227SGreg Roach 2815a78cd34SGreg Roach /** @var User $user */ 2825a78cd34SGreg Roach $user = $request->attributes->get('user'); 2838c2e8227SGreg Roach 2845a78cd34SGreg Roach $title = I18N::translate('Family tree clippings cart') . ' — ' . I18N::translate('Download'); 2858c2e8227SGreg Roach 2865a78cd34SGreg Roach return $this->viewResponse('modules/clippings/download', [ 2875a78cd34SGreg Roach 'is_manager' => Auth::isManager($tree, $user), 2885a78cd34SGreg Roach 'is_member' => Auth::isMember($tree, $user), 2895a78cd34SGreg Roach 'title' => $title, 2905a78cd34SGreg Roach ]); 2918c2e8227SGreg Roach } 2928c2e8227SGreg Roach 2935a78cd34SGreg Roach /** 2945a78cd34SGreg Roach * @param Request $request 2955a78cd34SGreg Roach * 2965a78cd34SGreg Roach * @return RedirectResponse 2975a78cd34SGreg Roach */ 2985a78cd34SGreg Roach public function getEmptyAction(Request $request): RedirectResponse { 2995a78cd34SGreg Roach /** @var Tree $tree */ 3005a78cd34SGreg Roach $tree = $request->attributes->get('tree'); 3018c2e8227SGreg Roach 3025a78cd34SGreg Roach $cart = Session::get('cart', []); 3035a78cd34SGreg Roach $cart[$tree->getName()] = []; 3045a78cd34SGreg Roach Session::put('cart', $cart); 3058c2e8227SGreg Roach 3065a78cd34SGreg Roach $url = route('module', ['module' => 'clippings', 'action' => 'Show', 'ged' => $tree->getName()]); 3075a78cd34SGreg Roach 3085a78cd34SGreg Roach return new RedirectResponse($url); 3095a78cd34SGreg Roach } 3105a78cd34SGreg Roach 3115a78cd34SGreg Roach /** 3125a78cd34SGreg Roach * @param Request $request 3135a78cd34SGreg Roach * 3145a78cd34SGreg Roach * @return RedirectResponse 3155a78cd34SGreg Roach */ 3165a78cd34SGreg Roach public function postRemoveAction(Request $request): RedirectResponse { 3175a78cd34SGreg Roach /** @var Tree $tree */ 3185a78cd34SGreg Roach $tree = $request->attributes->get('tree'); 3195a78cd34SGreg Roach 3205a78cd34SGreg Roach $xref = $request->get('xref'); 3215a78cd34SGreg Roach 3225a78cd34SGreg Roach $cart = Session::get('cart', []); 3235a78cd34SGreg Roach unset($cart[$tree->getName()][$xref]); 3245a78cd34SGreg Roach Session::put('cart', $cart); 3255a78cd34SGreg Roach 3265a78cd34SGreg Roach $url = route('module', ['module' => 'clippings', 'action' => 'Show', 'ged' => $tree->getName()]); 3275a78cd34SGreg Roach 3285a78cd34SGreg Roach return new RedirectResponse($url); 3295a78cd34SGreg Roach } 3305a78cd34SGreg Roach 3315a78cd34SGreg Roach /** 3325a78cd34SGreg Roach * @param Request $request 3335a78cd34SGreg Roach * 3345a78cd34SGreg Roach * @return Response 3355a78cd34SGreg Roach */ 3365a78cd34SGreg Roach public function getShowAction(Request $request): Response { 3375a78cd34SGreg Roach /** @var Tree $tree */ 3385a78cd34SGreg Roach $tree = $request->attributes->get('tree'); 3395a78cd34SGreg Roach 3405a78cd34SGreg Roach return $this->viewResponse('modules/clippings/show', [ 3415a78cd34SGreg Roach 'records' => $this->allRecordsInCart($tree), 3425a78cd34SGreg Roach 'title' => I18N::translate('Family tree clippings cart'), 3435a78cd34SGreg Roach 'tree' => $tree, 3445a78cd34SGreg Roach ]); 3455a78cd34SGreg Roach } 3465a78cd34SGreg Roach 3475a78cd34SGreg Roach /** 3485a78cd34SGreg Roach * @param Request $request 3495a78cd34SGreg Roach * 3505a78cd34SGreg Roach * @return Response 3515a78cd34SGreg Roach */ 3525a78cd34SGreg Roach public function getAddFamilyAction(Request $request): Response { 3535a78cd34SGreg Roach /** @var Tree $tree */ 3545a78cd34SGreg Roach $tree = $request->attributes->get('tree'); 3555a78cd34SGreg Roach 3565a78cd34SGreg Roach $xref = $request->get('xref'); 3575a78cd34SGreg Roach 3585a78cd34SGreg Roach $family = Family::getInstance($xref, $tree); 3595a78cd34SGreg Roach 3605a78cd34SGreg Roach if ($family === null) { 361*0bc54ba3SGreg Roach throw new FamilyNotFoundException; 3625a78cd34SGreg Roach } 3635a78cd34SGreg Roach 3645a78cd34SGreg Roach $options = $this->familyOptions($family); 3655a78cd34SGreg Roach 3665a78cd34SGreg Roach $title = I18N::translate('Add %s to the clippings cart', $family->getFullName()); 3675a78cd34SGreg Roach 3685a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 3695a78cd34SGreg Roach 'options' => $options, 3705a78cd34SGreg Roach 'default' => key($options), 3715a78cd34SGreg Roach 'record' => $family, 3725a78cd34SGreg Roach 'title' => $title, 3735a78cd34SGreg Roach 'tree' => $tree, 3745a78cd34SGreg Roach ]); 3755a78cd34SGreg Roach } 3765a78cd34SGreg Roach 3775a78cd34SGreg Roach /** 3785a78cd34SGreg Roach * @param Family $family 3795a78cd34SGreg Roach * 3805a78cd34SGreg Roach * @return string[] 3815a78cd34SGreg Roach */ 3825a78cd34SGreg Roach private function familyOptions(Family $family): array { 3835a78cd34SGreg Roach $name = strip_tags($family->getFullName()); 3845a78cd34SGreg Roach 3855a78cd34SGreg Roach return [ 3865a78cd34SGreg Roach 'parents' => $name, 3875a78cd34SGreg Roach 'members' => /* I18N: %s is a family (husband + wife) */ I18N::translate('%s and their children', $name), 3885a78cd34SGreg Roach 'descendants' => /* I18N: %s is a family (husband + wife) */ I18N::translate('%s and their descendants', $name), 3895a78cd34SGreg Roach ]; 3905a78cd34SGreg Roach } 3915a78cd34SGreg Roach 3925a78cd34SGreg Roach /** 3935a78cd34SGreg Roach * @param Request $request 3945a78cd34SGreg Roach * 3955a78cd34SGreg Roach * @return RedirectResponse 3965a78cd34SGreg Roach */ 3975a78cd34SGreg Roach public function postAddFamilyAction(Request $request): RedirectResponse { 3985a78cd34SGreg Roach /** @var Tree $tree */ 3995a78cd34SGreg Roach $tree = $request->attributes->get('tree'); 4005a78cd34SGreg Roach 4015a78cd34SGreg Roach $xref = $request->get('xref'); 4025a78cd34SGreg Roach $option = $request->get('option'); 4035a78cd34SGreg Roach 4045a78cd34SGreg Roach $family = Family::getInstance($xref, $tree); 4055a78cd34SGreg Roach 4065a78cd34SGreg Roach if ($family === null) { 407*0bc54ba3SGreg Roach throw new FamilyNotFoundException; 4085a78cd34SGreg Roach } 4095a78cd34SGreg Roach 4105a78cd34SGreg Roach switch ($option) { 4115a78cd34SGreg Roach case 'parents': 4125a78cd34SGreg Roach $this->addFamilyToCart($family); 4135a78cd34SGreg Roach break; 4145a78cd34SGreg Roach 4155a78cd34SGreg Roach case 'members': 4165a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 4175a78cd34SGreg Roach break; 4185a78cd34SGreg Roach 4195a78cd34SGreg Roach case 'descendants': 4205a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($family); 4215a78cd34SGreg Roach break; 4225a78cd34SGreg Roach } 4235a78cd34SGreg Roach 4245a78cd34SGreg Roach return new RedirectResponse($family->url()); 4255a78cd34SGreg Roach } 4265a78cd34SGreg Roach 4275a78cd34SGreg Roach /** 4285a78cd34SGreg Roach * @param Family $family 4295a78cd34SGreg Roach */ 4305a78cd34SGreg Roach private function addFamilyToCart(Family $family) { 4315a78cd34SGreg Roach $this->addRecordToCart($family); 4325a78cd34SGreg Roach 4335a78cd34SGreg Roach foreach ($family->getSpouses() as $spouse) { 4345a78cd34SGreg Roach $this->addRecordToCart($spouse); 4355a78cd34SGreg Roach } 4365a78cd34SGreg Roach } 4375a78cd34SGreg Roach 4385a78cd34SGreg Roach /** 4395a78cd34SGreg Roach * @param Family $family 4405a78cd34SGreg Roach */ 4415a78cd34SGreg Roach private function addFamilyAndChildrenToCart(Family $family) { 4425a78cd34SGreg Roach $this->addRecordToCart($family); 4435a78cd34SGreg Roach 4445a78cd34SGreg Roach foreach ($family->getSpouses() as $spouse) { 4455a78cd34SGreg Roach $this->addRecordToCart($spouse); 4465a78cd34SGreg Roach } 4475a78cd34SGreg Roach foreach ($family->getChildren() as $child) { 4485a78cd34SGreg Roach $this->addRecordToCart($child); 4495a78cd34SGreg Roach } 4505a78cd34SGreg Roach } 4515a78cd34SGreg Roach 4525a78cd34SGreg Roach /** 4535a78cd34SGreg Roach * @param Family $family 4545a78cd34SGreg Roach */ 4555a78cd34SGreg Roach private function addFamilyAndDescendantsToCart(Family $family) { 4565a78cd34SGreg Roach $this->addRecordToCart($family); 4575a78cd34SGreg Roach 4585a78cd34SGreg Roach foreach ($family->getSpouses() as $spouse) { 4595a78cd34SGreg Roach $this->addRecordToCart($spouse); 4605a78cd34SGreg Roach } 4615a78cd34SGreg Roach foreach ($family->getChildren() as $child) { 4625a78cd34SGreg Roach $this->addRecordToCart($child); 4635a78cd34SGreg Roach foreach ($child->getSpouseFamilies() as $child_family) { 4645a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($child_family); 4655a78cd34SGreg Roach } 4665a78cd34SGreg Roach } 4675a78cd34SGreg Roach } 4685a78cd34SGreg Roach 4695a78cd34SGreg Roach /** 4705a78cd34SGreg Roach * @param Request $request 4715a78cd34SGreg Roach * 4725a78cd34SGreg Roach * @return Response 4735a78cd34SGreg Roach */ 4745a78cd34SGreg Roach public function getAddIndividualAction(Request $request): Response { 4755a78cd34SGreg Roach /** @var Tree $tree */ 4765a78cd34SGreg Roach $tree = $request->attributes->get('tree'); 4775a78cd34SGreg Roach 4785a78cd34SGreg Roach $xref = $request->get('xref'); 4795a78cd34SGreg Roach 4805a78cd34SGreg Roach $individual = Individual::getInstance($xref, $tree); 4815a78cd34SGreg Roach 4825a78cd34SGreg Roach if ($individual === null) { 483*0bc54ba3SGreg Roach throw new IndividualNotFoundException; 4845a78cd34SGreg Roach } 4855a78cd34SGreg Roach 4865a78cd34SGreg Roach $options = $this->individualOptions($individual); 4875a78cd34SGreg Roach 4885a78cd34SGreg Roach $title = I18N::translate('Add %s to the clippings cart', $individual->getFullName()); 4895a78cd34SGreg Roach 4905a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 4915a78cd34SGreg Roach 'options' => $options, 4925a78cd34SGreg Roach 'default' => key($options), 4935a78cd34SGreg Roach 'record' => $individual, 4945a78cd34SGreg Roach 'title' => $title, 4955a78cd34SGreg Roach 'tree' => $tree, 4965a78cd34SGreg Roach ]); 4975a78cd34SGreg Roach } 4985a78cd34SGreg Roach 4995a78cd34SGreg Roach /** 5005a78cd34SGreg Roach * @param Individual $individual 5015a78cd34SGreg Roach * 5025a78cd34SGreg Roach * @return string[] 5035a78cd34SGreg Roach */ 5045a78cd34SGreg Roach private function individualOptions(Individual $individual): array { 5055a78cd34SGreg Roach $name = strip_tags($individual->getFullName()); 5065a78cd34SGreg Roach 5075a78cd34SGreg Roach if ($individual->getSex() === 'F') { 5085a78cd34SGreg Roach return [ 5095a78cd34SGreg Roach 'self' => $name, 5105a78cd34SGreg Roach 'parents' => I18N::translate('%s, her parents and siblings', $name), 5115a78cd34SGreg Roach 'spouses' => I18N::translate('%s, her spouses and children', $name), 5125a78cd34SGreg Roach 'ancestors' => I18N::translate('%s and her ancestors', $name), 5135a78cd34SGreg Roach 'ancestor_families' => I18N::translate('%s, her ancestors and their families', $name), 5145a78cd34SGreg Roach 'descendants' => I18N::translate('%s, her spouses and descendants', $name), 5155a78cd34SGreg Roach ]; 5165a78cd34SGreg Roach } else { 5175a78cd34SGreg Roach return [ 5185a78cd34SGreg Roach 'self' => $name, 5195a78cd34SGreg Roach 'parents' => I18N::translate('%s, his parents and siblings', $name), 5205a78cd34SGreg Roach 'spouses' => I18N::translate('%s, his spouses and children', $name), 5215a78cd34SGreg Roach 'ancestors' => I18N::translate('%s and his ancestors', $name), 5225a78cd34SGreg Roach 'ancestor_families' => I18N::translate('%s, his ancestors and their families', $name), 5235a78cd34SGreg Roach 'descendants' => I18N::translate('%s, his spouses and descendants', $name), 5245a78cd34SGreg Roach ]; 5255a78cd34SGreg Roach } 5265a78cd34SGreg Roach } 5275a78cd34SGreg Roach 5285a78cd34SGreg Roach /** 5295a78cd34SGreg Roach * @param Request $request 5305a78cd34SGreg Roach * 5315a78cd34SGreg Roach * @return RedirectResponse 5325a78cd34SGreg Roach */ 5335a78cd34SGreg Roach public function postAddIndividualAction(Request $request): RedirectResponse { 5345a78cd34SGreg Roach /** @var Tree $tree */ 5355a78cd34SGreg Roach $tree = $request->attributes->get('tree'); 5365a78cd34SGreg Roach 5375a78cd34SGreg Roach $xref = $request->get('xref'); 5385a78cd34SGreg Roach $option = $request->get('option'); 5395a78cd34SGreg Roach 5405a78cd34SGreg Roach $individual = Individual::getInstance($xref, $tree); 5415a78cd34SGreg Roach 5425a78cd34SGreg Roach if ($individual === null) { 543*0bc54ba3SGreg Roach throw new IndividualNotFoundException; 5445a78cd34SGreg Roach } 5455a78cd34SGreg Roach 5465a78cd34SGreg Roach switch ($option) { 5475a78cd34SGreg Roach case 'self': 5485a78cd34SGreg Roach $this->addRecordToCart($individual); 5495a78cd34SGreg Roach break; 5505a78cd34SGreg Roach 5515a78cd34SGreg Roach case 'parents': 5525a78cd34SGreg Roach foreach ($individual->getChildFamilies() as $family) { 5535a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 5545a78cd34SGreg Roach } 5555a78cd34SGreg Roach break; 5565a78cd34SGreg Roach 5575a78cd34SGreg Roach case 'spouses': 5585a78cd34SGreg Roach foreach ($individual->getSpouseFamilies() as $family) { 5595a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 5605a78cd34SGreg Roach } 5615a78cd34SGreg Roach break; 5625a78cd34SGreg Roach 5635a78cd34SGreg Roach case 'ancestors': 5645a78cd34SGreg Roach $this->addAncestorsToCart($individual); 5655a78cd34SGreg Roach break; 5665a78cd34SGreg Roach 5675a78cd34SGreg Roach case 'ancestor_families': 5685a78cd34SGreg Roach $this->addAncestorFamiliesToCart($individual); 5695a78cd34SGreg Roach break; 5705a78cd34SGreg Roach 5715a78cd34SGreg Roach case 'descendants': 5725a78cd34SGreg Roach foreach ($individual->getSpouseFamilies() as $family) { 5735a78cd34SGreg Roach $this->addFamilyAndDescendantsToCart($family); 5745a78cd34SGreg Roach } 5755a78cd34SGreg Roach break; 5765a78cd34SGreg Roach } 5775a78cd34SGreg Roach 5785a78cd34SGreg Roach return new RedirectResponse($individual->url()); 5795a78cd34SGreg Roach } 5805a78cd34SGreg Roach 5815a78cd34SGreg Roach /** 5825a78cd34SGreg Roach * @param Individual $individual 5835a78cd34SGreg Roach */ 5845a78cd34SGreg Roach private function addAncestorsToCart(Individual $individual) { 5855a78cd34SGreg Roach $this->addRecordToCart($individual); 5865a78cd34SGreg Roach 5875a78cd34SGreg Roach foreach ($individual->getChildFamilies() as $family) { 5885a78cd34SGreg Roach foreach ($family->getSpouses() as $parent) { 5895a78cd34SGreg Roach $this->addAncestorsToCart($parent); 5905a78cd34SGreg Roach } 5915a78cd34SGreg Roach } 5925a78cd34SGreg Roach } 5935a78cd34SGreg Roach 5945a78cd34SGreg Roach /** 5955a78cd34SGreg Roach * @param Individual $individual 5965a78cd34SGreg Roach */ 5975a78cd34SGreg Roach private function addAncestorFamiliesToCart(Individual $individual) { 5985a78cd34SGreg Roach foreach ($individual->getChildFamilies() as $family) { 5995a78cd34SGreg Roach $this->addFamilyAndChildrenToCart($family); 6005a78cd34SGreg Roach foreach ($family->getSpouses() as $parent) { 6015a78cd34SGreg Roach $this->addAncestorsToCart($parent); 6025a78cd34SGreg Roach } 6035a78cd34SGreg Roach } 6045a78cd34SGreg Roach } 6055a78cd34SGreg Roach 6065a78cd34SGreg Roach /** 6075a78cd34SGreg Roach * @param Request $request 6085a78cd34SGreg Roach * 6095a78cd34SGreg Roach * @return Response 6105a78cd34SGreg Roach */ 6115a78cd34SGreg Roach public function getAddMediaAction(Request $request): Response { 6125a78cd34SGreg Roach /** @var Tree $tree */ 6135a78cd34SGreg Roach $tree = $request->attributes->get('tree'); 6145a78cd34SGreg Roach 6155a78cd34SGreg Roach $xref = $request->get('xref'); 6165a78cd34SGreg Roach 6175a78cd34SGreg Roach $media = Media::getInstance($xref, $tree); 6185a78cd34SGreg Roach 6195a78cd34SGreg Roach if ($media === null) { 620*0bc54ba3SGreg Roach throw new MediaNotFoundException; 6215a78cd34SGreg Roach } 6225a78cd34SGreg Roach 6235a78cd34SGreg Roach $options = $this->mediaOptions($media); 6245a78cd34SGreg Roach 6255a78cd34SGreg Roach $title = I18N::translate('Add %s to the clippings cart', $media->getFullName()); 6265a78cd34SGreg Roach 6275a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 6285a78cd34SGreg Roach 'options' => $options, 6295a78cd34SGreg Roach 'default' => key($options), 6305a78cd34SGreg Roach 'record' => $media, 6315a78cd34SGreg Roach 'title' => $title, 6325a78cd34SGreg Roach 'tree' => $tree, 6335a78cd34SGreg Roach ]); 6345a78cd34SGreg Roach } 6355a78cd34SGreg Roach 6365a78cd34SGreg Roach /** 6375a78cd34SGreg Roach * @param Media $media 6385a78cd34SGreg Roach * 6395a78cd34SGreg Roach * @return string[] 6405a78cd34SGreg Roach */ 6415a78cd34SGreg Roach private function mediaOptions(Media $media): array { 6425a78cd34SGreg Roach $name = strip_tags($media->getFullName()); 6435a78cd34SGreg Roach 6445a78cd34SGreg Roach return [ 6455a78cd34SGreg Roach 'self' => $name, 6465a78cd34SGreg Roach ]; 6475a78cd34SGreg Roach } 6485a78cd34SGreg Roach 6495a78cd34SGreg Roach /** 6505a78cd34SGreg Roach * @param Request $request 6515a78cd34SGreg Roach * 6525a78cd34SGreg Roach * @return RedirectResponse 6535a78cd34SGreg Roach */ 6545a78cd34SGreg Roach public function postAddMediaAction(Request $request): RedirectResponse { 6555a78cd34SGreg Roach /** @var Tree $tree */ 6565a78cd34SGreg Roach $tree = $request->attributes->get('tree'); 6575a78cd34SGreg Roach 6585a78cd34SGreg Roach $xref = $request->get('xref'); 6595a78cd34SGreg Roach 6605a78cd34SGreg Roach $media = Media::getInstance($xref, $tree); 6615a78cd34SGreg Roach 6625a78cd34SGreg Roach if ($media === null) { 663*0bc54ba3SGreg Roach throw new MediaNotFoundException; 6645a78cd34SGreg Roach } 6655a78cd34SGreg Roach 6665a78cd34SGreg Roach $this->addRecordToCart($media); 6675a78cd34SGreg Roach 6685a78cd34SGreg Roach return new RedirectResponse($media->url()); 6695a78cd34SGreg Roach } 6705a78cd34SGreg Roach 6715a78cd34SGreg Roach /** 6725a78cd34SGreg Roach * @param Request $request 6735a78cd34SGreg Roach * 6745a78cd34SGreg Roach * @return Response 6755a78cd34SGreg Roach */ 6765a78cd34SGreg Roach public function getAddNoteAction(Request $request): Response { 6775a78cd34SGreg Roach /** @var Tree $tree */ 6785a78cd34SGreg Roach $tree = $request->attributes->get('tree'); 6795a78cd34SGreg Roach 6805a78cd34SGreg Roach $xref = $request->get('xref'); 6815a78cd34SGreg Roach 6825a78cd34SGreg Roach $note = Note::getInstance($xref, $tree); 6835a78cd34SGreg Roach 6845a78cd34SGreg Roach if ($note === null) { 685*0bc54ba3SGreg Roach throw new NoteNotFoundException; 6865a78cd34SGreg Roach } 6875a78cd34SGreg Roach 6885a78cd34SGreg Roach $options = $this->noteOptions($note); 6895a78cd34SGreg Roach 6905a78cd34SGreg Roach $title = I18N::translate('Add %s to the clippings cart', $note->getFullName()); 6915a78cd34SGreg Roach 6925a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 6935a78cd34SGreg Roach 'options' => $options, 6945a78cd34SGreg Roach 'default' => key($options), 6955a78cd34SGreg Roach 'record' => $note, 6965a78cd34SGreg Roach 'title' => $title, 6975a78cd34SGreg Roach 'tree' => $tree, 6985a78cd34SGreg Roach ]); 6995a78cd34SGreg Roach } 7005a78cd34SGreg Roach 7015a78cd34SGreg Roach /** 7025a78cd34SGreg Roach * @param Note $note 7035a78cd34SGreg Roach * 7045a78cd34SGreg Roach * @return string[] 7055a78cd34SGreg Roach */ 7065a78cd34SGreg Roach private function noteOptions(Note $note): array { 7075a78cd34SGreg Roach $name = strip_tags($note->getFullName()); 7085a78cd34SGreg Roach 7095a78cd34SGreg Roach return [ 7105a78cd34SGreg Roach 'self' => $name, 7115a78cd34SGreg Roach ]; 7125a78cd34SGreg Roach } 7135a78cd34SGreg Roach 7145a78cd34SGreg Roach /** 7155a78cd34SGreg Roach * @param Request $request 7165a78cd34SGreg Roach * 7175a78cd34SGreg Roach * @return RedirectResponse 7185a78cd34SGreg Roach */ 7195a78cd34SGreg Roach public function postAddNoteAction(Request $request): RedirectResponse { 7205a78cd34SGreg Roach /** @var Tree $tree */ 7215a78cd34SGreg Roach $tree = $request->attributes->get('tree'); 7225a78cd34SGreg Roach 7235a78cd34SGreg Roach $xref = $request->get('xref'); 7245a78cd34SGreg Roach 7255a78cd34SGreg Roach $note = Note::getInstance($xref, $tree); 7265a78cd34SGreg Roach 7275a78cd34SGreg Roach if ($note === null) { 728*0bc54ba3SGreg Roach throw new NoteNotFoundException; 7295a78cd34SGreg Roach } 7305a78cd34SGreg Roach 7315a78cd34SGreg Roach $this->addRecordToCart($note); 7325a78cd34SGreg Roach 7335a78cd34SGreg Roach return new RedirectResponse($note->url()); 7345a78cd34SGreg Roach } 7355a78cd34SGreg Roach 7365a78cd34SGreg Roach /** 7375a78cd34SGreg Roach * @param Request $request 7385a78cd34SGreg Roach * 7395a78cd34SGreg Roach * @return Response 7405a78cd34SGreg Roach */ 7415a78cd34SGreg Roach public function getAddRepositoryAction(Request $request): Response { 7425a78cd34SGreg Roach /** @var Tree $tree */ 7435a78cd34SGreg Roach $tree = $request->attributes->get('tree'); 7445a78cd34SGreg Roach 7455a78cd34SGreg Roach $xref = $request->get('xref'); 7465a78cd34SGreg Roach 7475a78cd34SGreg Roach $repository = Repository::getInstance($xref, $tree); 7485a78cd34SGreg Roach 7495a78cd34SGreg Roach if ($repository === null) { 750*0bc54ba3SGreg Roach throw new RepositoryNotFoundException; 7515a78cd34SGreg Roach } 7525a78cd34SGreg Roach 7535a78cd34SGreg Roach $options = $this->repositoryOptions($repository); 7545a78cd34SGreg Roach 7555a78cd34SGreg Roach $title = I18N::translate('Add %s to the clippings cart', $repository->getFullName()); 7565a78cd34SGreg Roach 7575a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 7585a78cd34SGreg Roach 'options' => $options, 7595a78cd34SGreg Roach 'default' => key($options), 7605a78cd34SGreg Roach 'record' => $repository, 7615a78cd34SGreg Roach 'title' => $title, 7625a78cd34SGreg Roach 'tree' => $tree, 7635a78cd34SGreg Roach ]); 7645a78cd34SGreg Roach } 7655a78cd34SGreg Roach 7665a78cd34SGreg Roach /** 7675a78cd34SGreg Roach * @param Repository $repository 7685a78cd34SGreg Roach * 7695a78cd34SGreg Roach * @return string[] 7705a78cd34SGreg Roach */ 7715a78cd34SGreg Roach private function repositoryOptions(Repository $repository): array { 7725a78cd34SGreg Roach $name = strip_tags($repository->getFullName()); 7735a78cd34SGreg Roach 7745a78cd34SGreg Roach return [ 7755a78cd34SGreg Roach 'self' => $name, 7765a78cd34SGreg Roach ]; 7775a78cd34SGreg Roach } 7785a78cd34SGreg Roach 7795a78cd34SGreg Roach /** 7805a78cd34SGreg Roach * @param Request $request 7815a78cd34SGreg Roach * 7825a78cd34SGreg Roach * @return RedirectResponse 7835a78cd34SGreg Roach */ 7845a78cd34SGreg Roach public function postAddRepositoryAction(Request $request): RedirectResponse { 7855a78cd34SGreg Roach /** @var Tree $tree */ 7865a78cd34SGreg Roach $tree = $request->attributes->get('tree'); 7875a78cd34SGreg Roach 7885a78cd34SGreg Roach $xref = $request->get('xref'); 7895a78cd34SGreg Roach 7905a78cd34SGreg Roach $repository = Repository::getInstance($xref, $tree); 7915a78cd34SGreg Roach 7925a78cd34SGreg Roach if ($repository === null) { 793*0bc54ba3SGreg Roach throw new RepositoryNotFoundException; 7945a78cd34SGreg Roach } 7955a78cd34SGreg Roach 7965a78cd34SGreg Roach $this->addRecordToCart($repository); 7975a78cd34SGreg Roach 7985a78cd34SGreg Roach return new RedirectResponse($repository->url()); 7995a78cd34SGreg Roach } 8005a78cd34SGreg Roach 8015a78cd34SGreg Roach /** 8025a78cd34SGreg Roach * @param Request $request 8035a78cd34SGreg Roach * 8045a78cd34SGreg Roach * @return Response 8055a78cd34SGreg Roach */ 8065a78cd34SGreg Roach public function getAddSourceAction(Request $request): Response { 8075a78cd34SGreg Roach /** @var Tree $tree */ 8085a78cd34SGreg Roach $tree = $request->attributes->get('tree'); 8095a78cd34SGreg Roach 8105a78cd34SGreg Roach $xref = $request->get('xref'); 8115a78cd34SGreg Roach 8125a78cd34SGreg Roach $source = Source::getInstance($xref, $tree); 8135a78cd34SGreg Roach 8145a78cd34SGreg Roach if ($source === null) { 815*0bc54ba3SGreg Roach throw new SourceNotFoundException; 8165a78cd34SGreg Roach } 8175a78cd34SGreg Roach 8185a78cd34SGreg Roach $options = $this->sourceOptions($source); 8195a78cd34SGreg Roach 8205a78cd34SGreg Roach $title = I18N::translate('Add %s to the clippings cart', $source->getFullName()); 8215a78cd34SGreg Roach 8225a78cd34SGreg Roach return $this->viewResponse('modules/clippings/add-options', [ 8235a78cd34SGreg Roach 'options' => $options, 8245a78cd34SGreg Roach 'default' => key($options), 8255a78cd34SGreg Roach 'record' => $source, 8265a78cd34SGreg Roach 'title' => $title, 8275a78cd34SGreg Roach 'tree' => $tree, 8285a78cd34SGreg Roach ]); 8295a78cd34SGreg Roach } 8305a78cd34SGreg Roach 8315a78cd34SGreg Roach /** 8325a78cd34SGreg Roach * @param Source $source 8335a78cd34SGreg Roach * 8345a78cd34SGreg Roach * @return string[] 8355a78cd34SGreg Roach */ 8365a78cd34SGreg Roach private function sourceOptions(Source $source): array { 8375a78cd34SGreg Roach $name = strip_tags($source->getFullName()); 8385a78cd34SGreg Roach 8395a78cd34SGreg Roach return [ 8405a78cd34SGreg Roach 'only' => strip_tags($source->getFullName()), 8415a78cd34SGreg Roach 'linked' => I18N::translate('%s and the individuals that reference it.', $name), 8425a78cd34SGreg Roach ]; 8435a78cd34SGreg Roach } 8445a78cd34SGreg Roach 8455a78cd34SGreg Roach /** 8465a78cd34SGreg Roach * @param Request $request 8475a78cd34SGreg Roach * 8485a78cd34SGreg Roach * @return RedirectResponse 8495a78cd34SGreg Roach */ 8505a78cd34SGreg Roach public function postAddSourceAction(Request $request): RedirectResponse { 8515a78cd34SGreg Roach /** @var Tree $tree */ 8525a78cd34SGreg Roach $tree = $request->attributes->get('tree'); 8535a78cd34SGreg Roach 8545a78cd34SGreg Roach $xref = $request->get('xref'); 8555a78cd34SGreg Roach $option = $request->get('option'); 8565a78cd34SGreg Roach 8575a78cd34SGreg Roach $source = Source::getInstance($xref, $tree); 8585a78cd34SGreg Roach 8595a78cd34SGreg Roach if ($source === null) { 860*0bc54ba3SGreg Roach throw new SourceNotFoundException; 8615a78cd34SGreg Roach } 8625a78cd34SGreg Roach 8635a78cd34SGreg Roach $this->addRecordToCart($source); 8645a78cd34SGreg Roach 8655a78cd34SGreg Roach if ($option === 'linked') { 8665a78cd34SGreg Roach foreach ($source->linkedIndividuals('SOUR') as $individual) { 8675a78cd34SGreg Roach $this->addRecordToCart($individual); 8685a78cd34SGreg Roach } 8695a78cd34SGreg Roach foreach ($source->linkedFamilies('SOUR') as $family) { 8705a78cd34SGreg Roach $this->addRecordToCart($family); 8715a78cd34SGreg Roach } 8725a78cd34SGreg Roach } 8735a78cd34SGreg Roach 8745a78cd34SGreg Roach return new RedirectResponse($source->url()); 8755a78cd34SGreg Roach } 8765a78cd34SGreg Roach 8775a78cd34SGreg Roach /** 8785a78cd34SGreg Roach * Get all the records in the cart. 8795a78cd34SGreg Roach * 8805a78cd34SGreg Roach * @param Tree $tree 8815a78cd34SGreg Roach * 8825a78cd34SGreg Roach * @return GedcomRecord[] 8835a78cd34SGreg Roach */ 8845a78cd34SGreg Roach private function allRecordsInCart(Tree $tree): array { 8855a78cd34SGreg Roach $cart = Session::get('cart', []); 8865a78cd34SGreg Roach 8875a78cd34SGreg Roach $xrefs = array_keys($cart[$tree->getName()] ?? []); 8885a78cd34SGreg Roach 8895a78cd34SGreg Roach // Fetch all the records in the cart. 8905a78cd34SGreg Roach $records = array_map(function (string $xref) use ($tree) { 8915a78cd34SGreg Roach return GedcomRecord::getInstance($xref, $tree); 8925a78cd34SGreg Roach }, $xrefs); 8935a78cd34SGreg Roach 8945a78cd34SGreg Roach // Some records may have been deleted after they were added to the cart. 8955a78cd34SGreg Roach $records = array_filter($records); 8965a78cd34SGreg Roach 8975a78cd34SGreg Roach // Group and sort. 8985a78cd34SGreg Roach uasort($records, function(GedcomRecord $x, GedcomRecord $y) { 8995a78cd34SGreg Roach return $x::RECORD_TYPE <=> $y::RECORD_TYPE ?: GedcomRecord::compare($x, $y); 9005a78cd34SGreg Roach }); 9015a78cd34SGreg Roach 9025a78cd34SGreg Roach return $records; 9035a78cd34SGreg Roach } 9045a78cd34SGreg Roach 9055a78cd34SGreg Roach /** 9065a78cd34SGreg Roach * Add a record (and direclty linked sources, notes, etc. to the cart. 9075a78cd34SGreg Roach * 9085a78cd34SGreg Roach * @param GedcomRecord $record 9095a78cd34SGreg Roach */ 9105a78cd34SGreg Roach private function addRecordToCart(GedcomRecord $record) { 9115a78cd34SGreg Roach $cart = Session::get('cart', []); 9125a78cd34SGreg Roach 9135a78cd34SGreg Roach $tree_name = $record->getTree()->getName(); 9145a78cd34SGreg Roach 9155a78cd34SGreg Roach // Add this record 9165a78cd34SGreg Roach $cart[$tree_name][$record->getXref()] = true; 9175a78cd34SGreg Roach 9185a78cd34SGreg Roach // Add directly linked media, notes, repositories and sources. 9195a78cd34SGreg Roach preg_match_all('/\n\d (?:OBJE|NOTE|SOUR|REPO) @(' . WT_REGEX_XREF . ')@/', $record->getGedcom(), $matches); 9205a78cd34SGreg Roach 9215a78cd34SGreg Roach foreach ($matches[1] as $match) { 9225a78cd34SGreg Roach $cart[$tree_name][$match] = true; 9235a78cd34SGreg Roach } 9245a78cd34SGreg Roach 9255a78cd34SGreg Roach Session::put('cart', $cart); 9265a78cd34SGreg Roach } 9275a78cd34SGreg Roach 9285a78cd34SGreg Roach /** 9295a78cd34SGreg Roach * @param Tree $tree 9305a78cd34SGreg Roach * 9315a78cd34SGreg Roach * @return bool 9325a78cd34SGreg Roach */ 9335a78cd34SGreg Roach private function isCartEmpty(Tree $tree): bool { 9345a78cd34SGreg Roach $cart = Session::get('cart', []); 9355a78cd34SGreg Roach 9365a78cd34SGreg Roach return empty($cart[$tree->getName()]); 9375a78cd34SGreg Roach } 9385a78cd34SGreg Roach 9395a78cd34SGreg Roach /** 9405a78cd34SGreg Roach * Only allow access to the routes/functions if the menu is active 9415a78cd34SGreg Roach * 9425a78cd34SGreg Roach * @param Tree $tree 9435a78cd34SGreg Roach */ 9445a78cd34SGreg Roach private function checkModuleAccess(Tree $tree) { 9455a78cd34SGreg Roach if (!array_key_exists($this->getName(), Module::getActiveMenus($tree))) { 9465a78cd34SGreg Roach throw new NotFoundHttpException; 9475a78cd34SGreg Roach } 9488c2e8227SGreg Roach } 9498c2e8227SGreg Roach} 950