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; 326ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 336ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 34a4439c01SGreg Roachuse stdClass; 358c2e8227SGreg Roach 368c2e8227SGreg Roach/** 378c2e8227SGreg Roach * Class UserFavoritesModule 388c2e8227SGreg Roach */ 3937eb8894SGreg Roachclass UserFavoritesModule extends AbstractModule implements ModuleBlockInterface 40c1010edaSGreg Roach{ 4149a243cbSGreg Roach use ModuleBlockTrait; 4249a243cbSGreg Roach 43a4439c01SGreg Roach /** 440cfd6963SGreg Roach * How should this module be identified in the control panel, etc.? 45a4439c01SGreg Roach * 46a4439c01SGreg Roach * @return string 47a4439c01SGreg Roach */ 4849a243cbSGreg Roach public function title(): string 49c1010edaSGreg Roach { 50bbb76c12SGreg Roach /* I18N: Name of a module */ 51bbb76c12SGreg Roach return I18N::translate('Favorites'); 52a4439c01SGreg Roach } 53a4439c01SGreg Roach 54a4439c01SGreg Roach /** 55a4439c01SGreg Roach * A sentence describing what this module does. 56a4439c01SGreg Roach * 57a4439c01SGreg Roach * @return string 58a4439c01SGreg 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 user’s favorite pages.'); 638c2e8227SGreg Roach } 648c2e8227SGreg Roach 6576692c8bSGreg Roach /** 66a4439c01SGreg Roach * Generate the HTML content of this block. 67a4439c01SGreg Roach * 68a4439c01SGreg Roach * @param Tree $tree 69a4439c01SGreg Roach * @param int $block_id 705f2ae573SGreg Roach * @param string $ctype 71a4439c01SGreg Roach * @param string[] $cfg 72a4439c01SGreg Roach * 73a4439c01SGreg Roach * @return string 74a4439c01SGreg 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' => true, 80a4439c01SGreg Roach 'favorites' => $this->getFavorites($tree, Auth::user()), 818ec20abdSGreg Roach 'module_name' => $this->name(), 82a4439c01SGreg Roach 'tree' => $tree, 83a4439c01SGreg Roach ]); 84a4439c01SGreg Roach 856a8879feSGreg Roach if ($ctype !== '') { 86147e99aaSGreg Roach return view('modules/block-template', [ 871e7a7a28SGreg Roach 'block' => Str::kebab($this->name()), 88a4439c01SGreg Roach 'id' => $block_id, 89a4439c01SGreg Roach 'config_url' => '', 9049a243cbSGreg Roach 'title' => $this->title(), 91a4439c01SGreg Roach 'content' => $content, 92a4439c01SGreg Roach ]); 93a4439c01SGreg Roach } 94b2ce94c6SRico Sonntag 95b2ce94c6SRico Sonntag return $content; 96a4439c01SGreg Roach } 97a4439c01SGreg Roach 98a4439c01SGreg Roach /** 99a4439c01SGreg Roach * Should this block load asynchronously using AJAX? 100a4439c01SGreg Roach * Simple blocks are faster in-line, more comples ones 101a4439c01SGreg Roach * can be loaded later. 102a4439c01SGreg Roach * 103a4439c01SGreg Roach * @return bool 104a4439c01SGreg Roach */ 105c1010edaSGreg Roach public function loadAjax(): bool 106c1010edaSGreg Roach { 107a4439c01SGreg Roach return false; 108a4439c01SGreg Roach } 109a4439c01SGreg Roach 110a4439c01SGreg 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 true; 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 false; 1288c2e8227SGreg Roach } 1298c2e8227SGreg Roach 1308c2e8227SGreg Roach /** 131a45f9889SGreg Roach * Update the configuration for a block. 132a45f9889SGreg Roach * 1336ccdf4f0SGreg Roach * @param ServerRequestInterface $request 134a45f9889SGreg Roach * @param int $block_id 135a45f9889SGreg Roach * 136a45f9889SGreg Roach * @return void 137a45f9889SGreg Roach */ 1386ccdf4f0SGreg Roach public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void 139a45f9889SGreg Roach { 140a45f9889SGreg Roach } 141a45f9889SGreg Roach 142a45f9889SGreg Roach /** 143a4439c01SGreg Roach * An HTML form to edit block settings 144a4439c01SGreg Roach * 145a4439c01SGreg Roach * @param Tree $tree 146a4439c01SGreg Roach * @param int $block_id 147a4439c01SGreg Roach * 148a4439c01SGreg Roach * @return void 149a4439c01SGreg Roach */ 150e364afe4SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id): void 151c1010edaSGreg Roach { 152a4439c01SGreg Roach } 153a4439c01SGreg Roach 154a4439c01SGreg Roach /** 155a4439c01SGreg Roach * Get the favorites for a user 1568c2e8227SGreg Roach * 1579807cf72SGreg Roach * @param Tree $tree 158e5a6b4d4SGreg Roach * @param UserInterface $user 1598c2e8227SGreg Roach * 160a4439c01SGreg Roach * @return stdClass[] 1618c2e8227SGreg Roach */ 162e5a6b4d4SGreg Roach public function getFavorites(Tree $tree, UserInterface $user): array 163c1010edaSGreg Roach { 1640587a5b3SGreg Roach return DB::table('favorite') 1650587a5b3SGreg Roach ->where('gedcom_id', '=', $tree->id()) 166895230eeSGreg Roach ->where('user_id', '=', $user->id()) 1670587a5b3SGreg Roach ->get() 1680b5fd0a6SGreg Roach ->map(static function (stdClass $row) use ($tree): stdClass { 1690587a5b3SGreg Roach if ($row->xref !== null) { 1700587a5b3SGreg Roach $row->record = GedcomRecord::getInstance($row->xref, $tree); 17125cd7ed9SGreg Roach } else { 1720587a5b3SGreg Roach $row->record = null; 1739807cf72SGreg Roach } 1749807cf72SGreg Roach 1750587a5b3SGreg Roach return $row; 1760587a5b3SGreg Roach }) 1770587a5b3SGreg Roach ->all(); 1788c2e8227SGreg Roach } 1798c2e8227SGreg Roach 18076692c8bSGreg Roach /** 1816ccdf4f0SGreg Roach * @param ServerRequestInterface $request 182b6db7c1fSGreg Roach * @param Tree $tree 183e5a6b4d4SGreg Roach * @param UserInterface $user 18476692c8bSGreg Roach * 1856ccdf4f0SGreg Roach * @return ResponseInterface 18676692c8bSGreg Roach */ 1876ccdf4f0SGreg Roach public function postAddFavoriteAction(ServerRequestInterface $request, Tree $tree, UserInterface $user): ResponseInterface 188c1010edaSGreg Roach { 189*c50a90beSGreg Roach $note = $request->getParsedBody()['note']; 190*c50a90beSGreg Roach $title = $request->getParsedBody()['title']; 191*c50a90beSGreg Roach $url = $request->getParsedBody()['url']; 192*c50a90beSGreg Roach $type = $request->getParsedBody()['type']; 193*c50a90beSGreg Roach $xref = $request->getParsedBody()[$type . '-xref'] ?? ''; 1948840c547SGreg Roach 1958ec20abdSGreg Roach $record = $this->getRecordForType($type, $xref, $tree); 19625cd7ed9SGreg Roach 197a4439c01SGreg Roach if (Auth::check()) { 1988ec20abdSGreg Roach if ($type === 'url' && $url !== '') { 199a4439c01SGreg Roach $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note); 2005c007fdfSGreg Roach } 2015c007fdfSGreg Roach 2028ec20abdSGreg Roach if ($record instanceof GedcomRecord && $record->canShow()) { 20325cd7ed9SGreg Roach $this->addRecordFavorite($tree, $user, $record, $note); 204a4439c01SGreg Roach } 2058c2e8227SGreg Roach } 2068840c547SGreg Roach 207aa6f03bbSGreg Roach $url = route('user-page', ['ged' => $tree->name()]); 208a4439c01SGreg Roach 2096ccdf4f0SGreg Roach return redirect($url); 210a4439c01SGreg Roach } 211a4439c01SGreg Roach 212a4439c01SGreg Roach /** 2136ccdf4f0SGreg Roach * @param ServerRequestInterface $request 214b6db7c1fSGreg Roach * @param Tree $tree 215e5a6b4d4SGreg Roach * @param UserInterface $user 216a4439c01SGreg Roach * 2176ccdf4f0SGreg Roach * @return ResponseInterface 218a4439c01SGreg Roach */ 2196ccdf4f0SGreg Roach public function postDeleteFavoriteAction(ServerRequestInterface $request, Tree $tree, UserInterface $user): ResponseInterface 220c1010edaSGreg Roach { 221*c50a90beSGreg Roach $favorite_id = $request->getQueryParams()['favorite_id']; 222a4439c01SGreg Roach 2238ec20abdSGreg Roach if (Auth::check()) { 2240587a5b3SGreg Roach DB::table('favorite') 2250587a5b3SGreg Roach ->where('favorite_id', '=', $favorite_id) 226895230eeSGreg Roach ->where('user_id', '=', $user->id()) 2270587a5b3SGreg Roach ->delete(); 2288ec20abdSGreg Roach } 229a4439c01SGreg Roach 230aa6f03bbSGreg Roach $url = route('user-page', ['ged' => $tree->name()]); 231a4439c01SGreg Roach 2326ccdf4f0SGreg Roach return redirect($url); 233a4439c01SGreg Roach } 234a4439c01SGreg Roach 235a4439c01SGreg Roach /** 236a4439c01SGreg Roach * @param Tree $tree 237e5a6b4d4SGreg Roach * @param UserInterface $user 238a4439c01SGreg Roach * @param string $url 239a4439c01SGreg Roach * @param string $title 240a4439c01SGreg Roach * @param string $note 24118d7a90dSGreg Roach * 24218d7a90dSGreg Roach * @return void 243a4439c01SGreg Roach */ 244e364afe4SGreg Roach private function addUrlFavorite(Tree $tree, UserInterface $user, string $url, string $title, string $note): void 245c1010edaSGreg Roach { 2460587a5b3SGreg Roach DB::table('favorite')->updateOrInsert([ 24772cf66d4SGreg Roach 'gedcom_id' => $tree->id(), 248895230eeSGreg Roach 'user_id' => $user->id(), 249a4439c01SGreg Roach 'url' => $url, 2500587a5b3SGreg Roach ], [ 2510587a5b3SGreg Roach 'favorite_type' => 'URL', 252a4439c01SGreg Roach 'note' => $note, 253a4439c01SGreg Roach 'title' => $title, 254a4439c01SGreg Roach ]); 255a4439c01SGreg Roach } 256a4439c01SGreg Roach 257a4439c01SGreg Roach /** 258a4439c01SGreg Roach * @param Tree $tree 259e5a6b4d4SGreg Roach * @param UserInterface $user 26025cd7ed9SGreg Roach * @param GedcomRecord $record 261a4439c01SGreg Roach * @param string $note 26218d7a90dSGreg Roach * 26318d7a90dSGreg Roach * @return void 264a4439c01SGreg Roach */ 265e364afe4SGreg Roach private function addRecordFavorite(Tree $tree, UserInterface $user, GedcomRecord $record, string $note): void 266c1010edaSGreg Roach { 2670587a5b3SGreg Roach DB::table('favorite')->updateOrInsert([ 26872cf66d4SGreg Roach 'gedcom_id' => $tree->id(), 269895230eeSGreg Roach 'user_id' => $user->id(), 27025cd7ed9SGreg Roach 'xref' => $record->xref(), 2710587a5b3SGreg Roach ], [ 27225cd7ed9SGreg Roach 'favorite_type' => $record::RECORD_TYPE, 273a4439c01SGreg Roach 'note' => $note, 274a4439c01SGreg Roach ]); 2758c2e8227SGreg Roach } 2768ec20abdSGreg Roach 2778ec20abdSGreg Roach /** 2788ec20abdSGreg Roach * @param string $type 2798ec20abdSGreg Roach * @param string $xref 2808ec20abdSGreg Roach * @param Tree $tree 2818ec20abdSGreg Roach * 2828ec20abdSGreg Roach * @return GedcomRecord|null 2838ec20abdSGreg Roach */ 2848ec20abdSGreg Roach private function getRecordForType(string $type, string $xref, Tree $tree): ?GedcomRecord 2858ec20abdSGreg Roach { 2868ec20abdSGreg Roach switch ($type) { 2878ec20abdSGreg Roach case 'indi': 2888ec20abdSGreg Roach return Individual::getInstance($xref, $tree); 2898ec20abdSGreg Roach 2908ec20abdSGreg Roach case 'fam': 2918ec20abdSGreg Roach return Family::getInstance($xref, $tree); 2928ec20abdSGreg Roach 2938ec20abdSGreg Roach case 'sour': 2948ec20abdSGreg Roach return Source::getInstance($xref, $tree); 2958ec20abdSGreg Roach 2968ec20abdSGreg Roach case 'repo': 2978ec20abdSGreg Roach return Repository::getInstance($xref, $tree); 2988ec20abdSGreg Roach 2998ec20abdSGreg Roach case 'obje': 3008ec20abdSGreg Roach return Media::getInstance($xref, $tree); 3018ec20abdSGreg Roach 3028ec20abdSGreg Roach default: 3038ec20abdSGreg Roach return null; 3048ec20abdSGreg Roach } 3058ec20abdSGreg Roach } 3068c2e8227SGreg Roach} 307