18c2e8227SGreg Roach<?php 23976b470SGreg Roach 38c2e8227SGreg Roach/** 48c2e8227SGreg Roach * webtrees: online genealogy 5a091ac74SGreg Roach * Copyright (C) 2020 webtrees development team 68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify 78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by 88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or 98c2e8227SGreg Roach * (at your option) any later version. 108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful, 118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 138c2e8227SGreg Roach * GNU General Public License for more details. 148c2e8227SGreg Roach * You should have received a copy of the GNU General Public License 158c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 168c2e8227SGreg Roach */ 17fcfa147eSGreg Roach 18e7f56f2aSGreg Roachdeclare(strict_types=1); 19e7f56f2aSGreg Roach 2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 218c2e8227SGreg Roach 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 23a091ac74SGreg Roachuse Fisharebest\Webtrees\Factory; 240e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 258e0e1b25SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\TreePage; 260e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 279807cf72SGreg Roachuse Fisharebest\Webtrees\Tree; 280587a5b3SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 291e7a7a28SGreg Roachuse Illuminate\Support\Str; 306ccdf4f0SGreg Roachuse Psr\Http\Message\ResponseInterface; 316ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 329807cf72SGreg Roachuse stdClass; 338c2e8227SGreg Roach 344ea62551SGreg Roachuse function assert; 354ea62551SGreg 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 703caaa4d2SGreg Roach * @param string $context 713caaa4d2SGreg Roach * @param string[] $config 7276692c8bSGreg Roach * 7376692c8bSGreg Roach * @return string 7476692c8bSGreg Roach */ 753caaa4d2SGreg Roach public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): 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 853caaa4d2SGreg Roach if ($context !== self::CONTEXT_EMBED) { 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? 1003caaa4d2SGreg Roach * Simple blocks are faster in-line, more complex ones can be loaded later. 10176692c8bSGreg Roach * 10276692c8bSGreg Roach * @return bool 10376692c8bSGreg Roach */ 104c1010edaSGreg Roach public function loadAjax(): bool 105c1010edaSGreg Roach { 1068c2e8227SGreg Roach return false; 1078c2e8227SGreg Roach } 1088c2e8227SGreg Roach 10976692c8bSGreg Roach /** 11076692c8bSGreg Roach * Can this block be shown on the user’s home page? 11176692c8bSGreg Roach * 11276692c8bSGreg Roach * @return bool 11376692c8bSGreg Roach */ 114c1010edaSGreg Roach public function isUserBlock(): bool 115c1010edaSGreg Roach { 1168c2e8227SGreg Roach return false; 1178c2e8227SGreg Roach } 1188c2e8227SGreg Roach 11976692c8bSGreg Roach /** 12076692c8bSGreg Roach * Can this block be shown on the tree’s home page? 12176692c8bSGreg Roach * 12276692c8bSGreg Roach * @return bool 12376692c8bSGreg Roach */ 12463276d8fSGreg Roach public function isTreeBlock(): bool 125c1010edaSGreg Roach { 1268c2e8227SGreg Roach return true; 1278c2e8227SGreg Roach } 1288c2e8227SGreg Roach 12976692c8bSGreg Roach /** 1308ec20abdSGreg Roach * Get the favorites for a family tree 1318c2e8227SGreg Roach * 1329807cf72SGreg Roach * @param Tree $tree 1338c2e8227SGreg Roach * 1349807cf72SGreg Roach * @return stdClass[] 1358c2e8227SGreg Roach */ 1368f53f488SRico Sonntag public function getFavorites(Tree $tree): array 137c1010edaSGreg Roach { 1380587a5b3SGreg Roach return DB::table('favorite') 1390587a5b3SGreg Roach ->where('gedcom_id', '=', $tree->id()) 1400587a5b3SGreg Roach ->whereNull('user_id') 1410587a5b3SGreg Roach ->get() 1420b5fd0a6SGreg Roach ->map(static function (stdClass $row) use ($tree): stdClass { 1430587a5b3SGreg Roach if ($row->xref !== null) { 144a091ac74SGreg Roach $row->record = Factory::gedcomRecord()->make($row->xref, $tree); 14525cd7ed9SGreg Roach } else { 1460587a5b3SGreg Roach $row->record = null; 1479807cf72SGreg Roach } 1489807cf72SGreg Roach 1490587a5b3SGreg Roach return $row; 1500587a5b3SGreg Roach }) 1510587a5b3SGreg Roach ->all(); 1528c2e8227SGreg Roach } 153a4439c01SGreg Roach 154a4439c01SGreg Roach /** 1556ccdf4f0SGreg Roach * @param ServerRequestInterface $request 156a4439c01SGreg Roach * 1576ccdf4f0SGreg Roach * @return ResponseInterface 158a4439c01SGreg Roach */ 15957ab2231SGreg Roach public function postAddFavoriteAction(ServerRequestInterface $request): ResponseInterface 160c1010edaSGreg Roach { 16157ab2231SGreg Roach $tree = $request->getAttribute('tree'); 1624ea62551SGreg Roach assert($tree instanceof Tree); 1634ea62551SGreg Roach 16457ab2231SGreg Roach $user = $request->getAttribute('user'); 165b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 166eb235819SGreg Roach 167eb235819SGreg Roach $note = $params['note']; 168eb235819SGreg Roach $title = $params['title']; 169eb235819SGreg Roach $url = $params['url']; 170eb235819SGreg Roach $type = $params['type']; 171eb235819SGreg Roach $xref = $params[$type . '-xref'] ?? ''; 172a4439c01SGreg Roach 1738ec20abdSGreg Roach $record = $this->getRecordForType($type, $xref, $tree); 17425cd7ed9SGreg Roach 175a4439c01SGreg Roach if (Auth::isManager($tree, $user)) { 1768ec20abdSGreg Roach if ($type === 'url' && $url !== '') { 177a4439c01SGreg Roach $this->addUrlFavorite($tree, $url, $title ?: $url, $note); 1785c007fdfSGreg Roach } 1795c007fdfSGreg Roach 1808ec20abdSGreg Roach if ($record instanceof GedcomRecord && $record->canShow()) { 18125cd7ed9SGreg Roach $this->addRecordFavorite($tree, $record, $note); 182a4439c01SGreg Roach } 183a4439c01SGreg Roach } 184a4439c01SGreg Roach 1858e0e1b25SGreg Roach $url = route(TreePage::class, ['tree' => $tree->name()]); 186a4439c01SGreg Roach 1876ccdf4f0SGreg Roach return redirect($url); 188a4439c01SGreg Roach } 189a4439c01SGreg Roach 190a4439c01SGreg Roach /** 1916ccdf4f0SGreg Roach * @param ServerRequestInterface $request 192a4439c01SGreg Roach * 1936ccdf4f0SGreg Roach * @return ResponseInterface 194a4439c01SGreg Roach */ 19557ab2231SGreg Roach public function postDeleteFavoriteAction(ServerRequestInterface $request): ResponseInterface 196c1010edaSGreg Roach { 19757ab2231SGreg Roach $tree = $request->getAttribute('tree'); 1984ea62551SGreg Roach assert($tree instanceof Tree); 1994ea62551SGreg Roach 20057ab2231SGreg Roach $user = $request->getAttribute('user'); 201eb235819SGreg Roach $favorite_id = $request->getQueryParams()['favorite_id']; 202a4439c01SGreg Roach 203a4439c01SGreg Roach if (Auth::isManager($tree, $user)) { 2040587a5b3SGreg Roach DB::table('favorite') 2050587a5b3SGreg Roach ->where('favorite_id', '=', $favorite_id) 2060587a5b3SGreg Roach ->whereNull('user_id') 2070587a5b3SGreg Roach ->delete(); 208a4439c01SGreg Roach } 209a4439c01SGreg Roach 2108e0e1b25SGreg Roach $url = route(TreePage::class, ['tree' => $tree->name()]); 211a4439c01SGreg Roach 2126ccdf4f0SGreg Roach return redirect($url); 213a4439c01SGreg Roach } 214a4439c01SGreg Roach 215a4439c01SGreg Roach /** 216a4439c01SGreg Roach * @param Tree $tree 217a4439c01SGreg Roach * @param string $url 218a4439c01SGreg Roach * @param string $title 219a4439c01SGreg Roach * @param string $note 22018d7a90dSGreg Roach * 22118d7a90dSGreg Roach * @return void 222a4439c01SGreg Roach */ 223e364afe4SGreg Roach private function addUrlFavorite(Tree $tree, string $url, string $title, string $note): void 224c1010edaSGreg Roach { 2250587a5b3SGreg Roach DB::table('favorite')->updateOrInsert([ 22672cf66d4SGreg Roach 'gedcom_id' => $tree->id(), 2270587a5b3SGreg Roach 'user_id' => null, 228a4439c01SGreg Roach 'url' => $url, 2290587a5b3SGreg Roach ], [ 2300587a5b3SGreg Roach 'favorite_type' => 'URL', 231a4439c01SGreg Roach 'note' => $note, 232a4439c01SGreg Roach 'title' => $title, 233a4439c01SGreg Roach ]); 234a4439c01SGreg Roach } 235a4439c01SGreg Roach 236a4439c01SGreg Roach /** 237a4439c01SGreg Roach * @param Tree $tree 23825cd7ed9SGreg Roach * @param GedcomRecord $record 239a4439c01SGreg Roach * @param string $note 24018d7a90dSGreg Roach * 24118d7a90dSGreg Roach * @return void 242a4439c01SGreg Roach */ 243e364afe4SGreg Roach private function addRecordFavorite(Tree $tree, GedcomRecord $record, string $note): void 244c1010edaSGreg Roach { 2450587a5b3SGreg Roach DB::table('favorite')->updateOrInsert([ 24672cf66d4SGreg Roach 'gedcom_id' => $tree->id(), 2470587a5b3SGreg Roach 'user_id' => null, 24825cd7ed9SGreg Roach 'xref' => $record->xref(), 2490587a5b3SGreg Roach ], [ 250*02467d32SGreg Roach 'favorite_type' => $record->tag(), 251a4439c01SGreg Roach 'note' => $note, 252a4439c01SGreg Roach ]); 253a4439c01SGreg Roach } 2548ec20abdSGreg Roach 2558ec20abdSGreg Roach /** 2568ec20abdSGreg Roach * @param string $type 2578ec20abdSGreg Roach * @param string $xref 2588ec20abdSGreg Roach * @param Tree $tree 2598ec20abdSGreg Roach * 2608ec20abdSGreg Roach * @return GedcomRecord|null 2618ec20abdSGreg Roach */ 2628ec20abdSGreg Roach private function getRecordForType(string $type, string $xref, Tree $tree): ?GedcomRecord 2638ec20abdSGreg Roach { 2648ec20abdSGreg Roach switch ($type) { 2658ec20abdSGreg Roach case 'indi': 266a091ac74SGreg Roach return Factory::individual()->make($xref, $tree); 2678ec20abdSGreg Roach 2688ec20abdSGreg Roach case 'fam': 269a091ac74SGreg Roach return Factory::family()->make($xref, $tree); 2708ec20abdSGreg Roach 2718ec20abdSGreg Roach case 'sour': 272a091ac74SGreg Roach return Factory::source()->make($xref, $tree); 2738ec20abdSGreg Roach 2748ec20abdSGreg Roach case 'repo': 275a091ac74SGreg Roach return Factory::repository()->make($xref, $tree); 2768ec20abdSGreg Roach 2778ec20abdSGreg Roach case 'obje': 278a091ac74SGreg Roach return Factory::media()->make($xref, $tree); 2798ec20abdSGreg Roach 2808ec20abdSGreg Roach default: 2818ec20abdSGreg Roach return null; 2828ec20abdSGreg Roach } 2838ec20abdSGreg Roach } 2848c2e8227SGreg Roach} 285