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; 198c2e8227SGreg Roach 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 21e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 228ec20abdSGreg Roachuse Fisharebest\Webtrees\Family; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 258ec20abdSGreg Roachuse Fisharebest\Webtrees\Individual; 268ec20abdSGreg Roachuse Fisharebest\Webtrees\Media; 278ec20abdSGreg Roachuse Fisharebest\Webtrees\Repository; 288ec20abdSGreg Roachuse Fisharebest\Webtrees\Source; 299807cf72SGreg Roachuse Fisharebest\Webtrees\Tree; 300587a5b3SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 311e7a7a28SGreg Roachuse Illuminate\Support\Str; 329807cf72SGreg Roachuse stdClass; 33a4439c01SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse; 34a4439c01SGreg Roachuse Symfony\Component\HttpFoundation\Request; 358c2e8227SGreg Roach 368c2e8227SGreg Roach/** 378c2e8227SGreg Roach * Class FamilyTreeFavoritesModule 388c2e8227SGreg Roach */ 3937eb8894SGreg Roachclass FamilyTreeFavoritesModule extends AbstractModule implements ModuleBlockInterface 40c1010edaSGreg Roach{ 4149a243cbSGreg Roach use ModuleBlockTrait; 4249a243cbSGreg Roach 4376692c8bSGreg Roach /** 440cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 4576692c8bSGreg Roach * 4676692c8bSGreg Roach * @return string 4776692c8bSGreg Roach */ 4849a243cbSGreg Roach public function title(): string 49c1010edaSGreg Roach { 50bbb76c12SGreg Roach /* I18N: Name of a module */ 51bbb76c12SGreg Roach return I18N::translate('Favorites'); 528c2e8227SGreg Roach } 538c2e8227SGreg Roach 5476692c8bSGreg Roach /** 5576692c8bSGreg Roach * A sentence describing what this module does. 5676692c8bSGreg Roach * 5776692c8bSGreg Roach * @return string 5876692c8bSGreg Roach */ 5949a243cbSGreg Roach public function description(): string 60c1010edaSGreg Roach { 61bbb76c12SGreg Roach /* I18N: Description of the “Favorites” module */ 62bbb76c12SGreg Roach return I18N::translate('Display and manage a family tree’s favorite pages.'); 638c2e8227SGreg Roach } 648c2e8227SGreg Roach 6576692c8bSGreg Roach /** 6676692c8bSGreg Roach * Generate the HTML content of this block. 6776692c8bSGreg Roach * 68e490cd80SGreg Roach * @param Tree $tree 6976692c8bSGreg Roach * @param int $block_id 705f2ae573SGreg Roach * @param string $ctype 71727f238cSGreg Roach * @param string[] $cfg 7276692c8bSGreg Roach * 7376692c8bSGreg Roach * @return string 7476692c8bSGreg Roach */ 755f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 76c1010edaSGreg Roach { 778ec20abdSGreg Roach $content = view('modules/favorites/favorites', [ 78a4439c01SGreg Roach 'block_id' => $block_id, 798ec20abdSGreg Roach 'can_edit' => Auth::isManager($tree), 80a4439c01SGreg Roach 'favorites' => $this->getFavorites($tree), 818ec20abdSGreg Roach 'module_name' => $this->name(), 82e490cd80SGreg Roach 'tree' => $tree, 839807cf72SGreg Roach ]); 849807cf72SGreg Roach 856a8879feSGreg Roach if ($ctype !== '') { 86147e99aaSGreg Roach return view('modules/block-template', [ 871e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 889c6524dcSGreg Roach 'id' => $block_id, 899c6524dcSGreg Roach 'config_url' => '', 9049a243cbSGreg Roach 'title' => $this->title(), 919c6524dcSGreg Roach 'content' => $content, 929c6524dcSGreg Roach ]); 938c2e8227SGreg Roach } 94b2ce94c6SRico Sonntag 95b2ce94c6SRico Sonntag return $content; 968c2e8227SGreg Roach } 978c2e8227SGreg Roach 9876692c8bSGreg Roach /** 9976692c8bSGreg Roach * Should this block load asynchronously using AJAX? 10076692c8bSGreg Roach * Simple blocks are faster in-line, more comples ones 10176692c8bSGreg Roach * can be loaded later. 10276692c8bSGreg Roach * 10376692c8bSGreg Roach * @return bool 10476692c8bSGreg Roach */ 105c1010edaSGreg Roach public function loadAjax(): bool 106c1010edaSGreg Roach { 1078c2e8227SGreg Roach return false; 1088c2e8227SGreg Roach } 1098c2e8227SGreg Roach 11076692c8bSGreg Roach /** 11176692c8bSGreg Roach * Can this block be shown on the user’s home page? 11276692c8bSGreg Roach * 11376692c8bSGreg Roach * @return bool 11476692c8bSGreg Roach */ 115c1010edaSGreg Roach public function isUserBlock(): bool 116c1010edaSGreg Roach { 1178c2e8227SGreg Roach return false; 1188c2e8227SGreg Roach } 1198c2e8227SGreg Roach 12076692c8bSGreg Roach /** 12176692c8bSGreg Roach * Can this block be shown on the tree’s home page? 12276692c8bSGreg Roach * 12376692c8bSGreg Roach * @return bool 12476692c8bSGreg Roach */ 12563276d8fSGreg Roach public function isTreeBlock(): bool 126c1010edaSGreg Roach { 1278c2e8227SGreg Roach return true; 1288c2e8227SGreg Roach } 1298c2e8227SGreg Roach 13076692c8bSGreg Roach /** 131a45f9889SGreg Roach * Update the configuration for a block. 132a45f9889SGreg Roach * 133a45f9889SGreg Roach * @param Request $request 134a45f9889SGreg Roach * @param int $block_id 135a45f9889SGreg Roach * 136a45f9889SGreg Roach * @return void 137a45f9889SGreg Roach */ 138e364afe4SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id): void 139a45f9889SGreg Roach { 140a45f9889SGreg Roach } 141a45f9889SGreg Roach 142a45f9889SGreg Roach /** 14376692c8bSGreg Roach * An HTML form to edit block settings 14476692c8bSGreg Roach * 145e490cd80SGreg Roach * @param Tree $tree 14676692c8bSGreg Roach * @param int $block_id 147a9430be8SGreg Roach * 148a9430be8SGreg Roach * @return void 14976692c8bSGreg Roach */ 150e364afe4SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): void 151c1010edaSGreg Roach { 1528d68cabeSGreg Roach } 1538c2e8227SGreg Roach 1548c2e8227SGreg Roach /** 1558ec20abdSGreg Roach * Get the favorites for a family tree 1568c2e8227SGreg Roach * 1579807cf72SGreg Roach * @param Tree $tree 1588c2e8227SGreg Roach * 1599807cf72SGreg Roach * @return stdClass[] 1608c2e8227SGreg Roach */ 1618f53f488SRico Sonntag public function getFavorites(Tree $tree): array 162c1010edaSGreg Roach { 1630587a5b3SGreg Roach return DB::table('favorite') 1640587a5b3SGreg Roach ->where('gedcom_id', '=', $tree->id()) 1650587a5b3SGreg Roach ->whereNull('user_id') 1660587a5b3SGreg Roach ->get() 167*0b5fd0a6SGreg Roach ->map(static function (stdClass $row) use ($tree): stdClass { 1680587a5b3SGreg Roach if ($row->xref !== null) { 1690587a5b3SGreg Roach $row->record = GedcomRecord::getInstance($row->xref, $tree); 17025cd7ed9SGreg Roach } else { 1710587a5b3SGreg Roach $row->record = null; 1729807cf72SGreg Roach } 1739807cf72SGreg Roach 1740587a5b3SGreg Roach return $row; 1750587a5b3SGreg Roach }) 1760587a5b3SGreg Roach ->all(); 1778c2e8227SGreg Roach } 178a4439c01SGreg Roach 179a4439c01SGreg Roach /** 180a4439c01SGreg Roach * @param Request $request 181b6db7c1fSGreg Roach * @param Tree $tree 182e5a6b4d4SGreg Roach * @param UserInterface $user 183a4439c01SGreg Roach * 184a4439c01SGreg Roach * @return RedirectResponse 185a4439c01SGreg Roach */ 186e5a6b4d4SGreg Roach public function postAddFavoriteAction(Request $request, Tree $tree, UserInterface $user): RedirectResponse 187c1010edaSGreg Roach { 188a4439c01SGreg Roach $note = $request->get('note', ''); 189a4439c01SGreg Roach $title = $request->get('title', ''); 190a4439c01SGreg Roach $url = $request->get('url', ''); 1918ec20abdSGreg Roach $type = $request->get('type', ''); 1928ec20abdSGreg Roach $xref = $request->get($type . '-xref', ''); 193a4439c01SGreg Roach 1948ec20abdSGreg Roach $record = $this->getRecordForType($type, $xref, $tree); 19525cd7ed9SGreg Roach 196a4439c01SGreg Roach if (Auth::isManager($tree, $user)) { 1978ec20abdSGreg Roach if ($type === 'url' && $url !== '') { 198a4439c01SGreg Roach $this->addUrlFavorite($tree, $url, $title ?: $url, $note); 1995c007fdfSGreg Roach } 2005c007fdfSGreg Roach 2018ec20abdSGreg Roach if ($record instanceof GedcomRecord && $record->canShow()) { 20225cd7ed9SGreg Roach $this->addRecordFavorite($tree, $record, $note); 203a4439c01SGreg Roach } 204a4439c01SGreg Roach } 205a4439c01SGreg Roach 206aa6f03bbSGreg Roach $url = route('tree-page', ['ged' => $tree->name()]); 207a4439c01SGreg Roach 208a4439c01SGreg Roach return new RedirectResponse($url); 209a4439c01SGreg Roach } 210a4439c01SGreg Roach 211a4439c01SGreg Roach /** 212a4439c01SGreg Roach * @param Request $request 213b6db7c1fSGreg Roach * @param Tree $tree 214e5a6b4d4SGreg Roach * @param UserInterface $user 215a4439c01SGreg Roach * 216a4439c01SGreg Roach * @return RedirectResponse 217a4439c01SGreg Roach */ 218e5a6b4d4SGreg Roach public function postDeleteFavoriteAction(Request $request, Tree $tree, UserInterface $user): RedirectResponse 219c1010edaSGreg Roach { 220a4439c01SGreg Roach $favorite_id = (int) $request->get('favorite_id'); 221a4439c01SGreg Roach 222a4439c01SGreg Roach if (Auth::isManager($tree, $user)) { 2230587a5b3SGreg Roach DB::table('favorite') 2240587a5b3SGreg Roach ->where('favorite_id', '=', $favorite_id) 2250587a5b3SGreg Roach ->whereNull('user_id') 2260587a5b3SGreg Roach ->delete(); 227a4439c01SGreg Roach } 228a4439c01SGreg Roach 229aa6f03bbSGreg Roach $url = route('tree-page', ['ged' => $tree->name()]); 230a4439c01SGreg Roach 231a4439c01SGreg Roach return new RedirectResponse($url); 232a4439c01SGreg Roach } 233a4439c01SGreg Roach 234a4439c01SGreg Roach /** 235a4439c01SGreg Roach * @param Tree $tree 236a4439c01SGreg Roach * @param string $url 237a4439c01SGreg Roach * @param string $title 238a4439c01SGreg Roach * @param string $note 23918d7a90dSGreg Roach * 24018d7a90dSGreg Roach * @return void 241a4439c01SGreg Roach */ 242e364afe4SGreg Roach private function addUrlFavorite(Tree $tree, string $url, string $title, string $note): void 243c1010edaSGreg Roach { 2440587a5b3SGreg Roach DB::table('favorite')->updateOrInsert([ 24572cf66d4SGreg Roach 'gedcom_id' => $tree->id(), 2460587a5b3SGreg Roach 'user_id' => null, 247a4439c01SGreg Roach 'url' => $url, 2480587a5b3SGreg Roach ], [ 2490587a5b3SGreg Roach 'favorite_type' => 'URL', 250a4439c01SGreg Roach 'note' => $note, 251a4439c01SGreg Roach 'title' => $title, 252a4439c01SGreg Roach ]); 253a4439c01SGreg Roach } 254a4439c01SGreg Roach 255a4439c01SGreg Roach /** 256a4439c01SGreg Roach * @param Tree $tree 25725cd7ed9SGreg Roach * @param GedcomRecord $record 258a4439c01SGreg Roach * @param string $note 25918d7a90dSGreg Roach * 26018d7a90dSGreg Roach * @return void 261a4439c01SGreg Roach */ 262e364afe4SGreg Roach private function addRecordFavorite(Tree $tree, GedcomRecord $record, string $note): void 263c1010edaSGreg Roach { 2640587a5b3SGreg Roach DB::table('favorite')->updateOrInsert([ 26572cf66d4SGreg Roach 'gedcom_id' => $tree->id(), 2660587a5b3SGreg Roach 'user_id' => null, 26725cd7ed9SGreg Roach 'xref' => $record->xref(), 2680587a5b3SGreg Roach ], [ 26925cd7ed9SGreg Roach 'favorite_type' => $record::RECORD_TYPE, 270a4439c01SGreg Roach 'note' => $note, 271a4439c01SGreg Roach ]); 272a4439c01SGreg Roach } 2738ec20abdSGreg Roach 2748ec20abdSGreg Roach /** 2758ec20abdSGreg Roach * @param string $type 2768ec20abdSGreg Roach * @param string $xref 2778ec20abdSGreg Roach * @param Tree $tree 2788ec20abdSGreg Roach * 2798ec20abdSGreg Roach * @return GedcomRecord|null 2808ec20abdSGreg Roach */ 2818ec20abdSGreg Roach private function getRecordForType(string $type, string $xref, Tree $tree): ?GedcomRecord 2828ec20abdSGreg Roach { 2838ec20abdSGreg Roach switch ($type) { 2848ec20abdSGreg Roach case 'indi': 2858ec20abdSGreg Roach return Individual::getInstance($xref, $tree); 2868ec20abdSGreg Roach 2878ec20abdSGreg Roach case 'fam': 2888ec20abdSGreg Roach return Family::getInstance($xref, $tree); 2898ec20abdSGreg Roach 2908ec20abdSGreg Roach case 'sour': 2918ec20abdSGreg Roach return Source::getInstance($xref, $tree); 2928ec20abdSGreg Roach 2938ec20abdSGreg Roach case 'repo': 2948ec20abdSGreg Roach return Repository::getInstance($xref, $tree); 2958ec20abdSGreg Roach 2968ec20abdSGreg Roach case 'obje': 2978ec20abdSGreg Roach return Media::getInstance($xref, $tree); 2988ec20abdSGreg Roach 2998ec20abdSGreg Roach default: 3008ec20abdSGreg Roach return null; 3018ec20abdSGreg Roach } 3028ec20abdSGreg Roach } 3038c2e8227SGreg Roach} 304