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; 21*e5a6b4d4SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 249807cf72SGreg Roachuse Fisharebest\Webtrees\Tree; 250587a5b3SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 269807cf72SGreg Roachuse stdClass; 27a4439c01SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse; 28a4439c01SGreg Roachuse Symfony\Component\HttpFoundation\Request; 298c2e8227SGreg Roach 308c2e8227SGreg Roach/** 318c2e8227SGreg Roach * Class FamilyTreeFavoritesModule 328c2e8227SGreg Roach */ 3337eb8894SGreg Roachclass FamilyTreeFavoritesModule extends AbstractModule implements ModuleBlockInterface 34c1010edaSGreg Roach{ 3549a243cbSGreg Roach use ModuleBlockTrait; 3649a243cbSGreg Roach 3776692c8bSGreg Roach /** 3876692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 3976692c8bSGreg Roach * 4076692c8bSGreg Roach * @return string 4176692c8bSGreg Roach */ 4249a243cbSGreg Roach public function title(): string 43c1010edaSGreg Roach { 44bbb76c12SGreg Roach /* I18N: Name of a module */ 45bbb76c12SGreg Roach return I18N::translate('Favorites'); 468c2e8227SGreg Roach } 478c2e8227SGreg Roach 4876692c8bSGreg Roach /** 4976692c8bSGreg Roach * A sentence describing what this module does. 5076692c8bSGreg Roach * 5176692c8bSGreg Roach * @return string 5276692c8bSGreg Roach */ 5349a243cbSGreg Roach public function description(): string 54c1010edaSGreg Roach { 55bbb76c12SGreg Roach /* I18N: Description of the “Favorites” module */ 56bbb76c12SGreg Roach return I18N::translate('Display and manage a family tree’s favorite pages.'); 578c2e8227SGreg Roach } 588c2e8227SGreg Roach 5976692c8bSGreg Roach /** 6076692c8bSGreg Roach * Generate the HTML content of this block. 6176692c8bSGreg Roach * 62e490cd80SGreg Roach * @param Tree $tree 6376692c8bSGreg Roach * @param int $block_id 645f2ae573SGreg Roach * @param string $ctype 65727f238cSGreg Roach * @param string[] $cfg 6676692c8bSGreg Roach * 6776692c8bSGreg Roach * @return string 6876692c8bSGreg Roach */ 695f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 70c1010edaSGreg Roach { 71a4439c01SGreg Roach $content = view('modules/gedcom_favorites/favorites', [ 72a4439c01SGreg Roach 'block_id' => $block_id, 73a4439c01SGreg Roach 'favorites' => $this->getFavorites($tree), 74e490cd80SGreg Roach 'is_manager' => Auth::isManager($tree), 75e490cd80SGreg Roach 'tree' => $tree, 769807cf72SGreg Roach ]); 779807cf72SGreg Roach 786a8879feSGreg Roach if ($ctype !== '') { 79147e99aaSGreg Roach return view('modules/block-template', [ 8026684e68SGreg Roach 'block' => str_replace('_', '-', $this->name()), 819c6524dcSGreg Roach 'id' => $block_id, 829c6524dcSGreg Roach 'config_url' => '', 8349a243cbSGreg Roach 'title' => $this->title(), 849c6524dcSGreg Roach 'content' => $content, 859c6524dcSGreg Roach ]); 868c2e8227SGreg Roach } 87b2ce94c6SRico Sonntag 88b2ce94c6SRico Sonntag return $content; 898c2e8227SGreg Roach } 908c2e8227SGreg Roach 9176692c8bSGreg Roach /** 9276692c8bSGreg Roach * Should this block load asynchronously using AJAX? 9376692c8bSGreg Roach * Simple blocks are faster in-line, more comples ones 9476692c8bSGreg Roach * can be loaded later. 9576692c8bSGreg Roach * 9676692c8bSGreg Roach * @return bool 9776692c8bSGreg Roach */ 98c1010edaSGreg Roach public function loadAjax(): bool 99c1010edaSGreg Roach { 1008c2e8227SGreg Roach return false; 1018c2e8227SGreg Roach } 1028c2e8227SGreg Roach 10376692c8bSGreg Roach /** 10476692c8bSGreg Roach * Can this block be shown on the user’s home page? 10576692c8bSGreg Roach * 10676692c8bSGreg Roach * @return bool 10776692c8bSGreg Roach */ 108c1010edaSGreg Roach public function isUserBlock(): bool 109c1010edaSGreg Roach { 1108c2e8227SGreg Roach return false; 1118c2e8227SGreg Roach } 1128c2e8227SGreg Roach 11376692c8bSGreg Roach /** 11476692c8bSGreg Roach * Can this block be shown on the tree’s home page? 11576692c8bSGreg Roach * 11676692c8bSGreg Roach * @return bool 11776692c8bSGreg Roach */ 11863276d8fSGreg Roach public function isTreeBlock(): bool 119c1010edaSGreg Roach { 1208c2e8227SGreg Roach return true; 1218c2e8227SGreg Roach } 1228c2e8227SGreg Roach 12376692c8bSGreg Roach /** 124a45f9889SGreg Roach * Update the configuration for a block. 125a45f9889SGreg Roach * 126a45f9889SGreg Roach * @param Request $request 127a45f9889SGreg Roach * @param int $block_id 128a45f9889SGreg Roach * 129a45f9889SGreg Roach * @return void 130a45f9889SGreg Roach */ 131a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 132a45f9889SGreg Roach { 133a45f9889SGreg Roach } 134a45f9889SGreg Roach 135a45f9889SGreg Roach /** 13676692c8bSGreg Roach * An HTML form to edit block settings 13776692c8bSGreg Roach * 138e490cd80SGreg Roach * @param Tree $tree 13976692c8bSGreg Roach * @param int $block_id 140a9430be8SGreg Roach * 141a9430be8SGreg Roach * @return void 14276692c8bSGreg Roach */ 143a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 144c1010edaSGreg Roach { 1458d68cabeSGreg Roach } 1468c2e8227SGreg Roach 1478c2e8227SGreg Roach /** 148a4439c01SGreg Roach * Get favorites for a family tree 1498c2e8227SGreg Roach * 1509807cf72SGreg Roach * @param Tree $tree 1518c2e8227SGreg Roach * 1529807cf72SGreg Roach * @return stdClass[] 1538c2e8227SGreg Roach */ 1548f53f488SRico Sonntag public function getFavorites(Tree $tree): array 155c1010edaSGreg Roach { 1560587a5b3SGreg Roach return DB::table('favorite') 1570587a5b3SGreg Roach ->where('gedcom_id', '=', $tree->id()) 1580587a5b3SGreg Roach ->whereNull('user_id') 1590587a5b3SGreg Roach ->get() 1600587a5b3SGreg Roach ->map(function (stdClass $row) use ($tree): stdClass { 1610587a5b3SGreg Roach if ($row->xref !== null) { 1620587a5b3SGreg Roach $row->record = GedcomRecord::getInstance($row->xref, $tree); 16325cd7ed9SGreg Roach } else { 1640587a5b3SGreg Roach $row->record = null; 1659807cf72SGreg Roach } 1669807cf72SGreg Roach 1670587a5b3SGreg Roach return $row; 1680587a5b3SGreg Roach }) 1690587a5b3SGreg Roach ->all(); 1708c2e8227SGreg Roach } 171a4439c01SGreg Roach 172a4439c01SGreg Roach /** 173a4439c01SGreg Roach * @param Request $request 174b6db7c1fSGreg Roach * @param Tree $tree 175*e5a6b4d4SGreg Roach * @param UserInterface $user 176a4439c01SGreg Roach * 177a4439c01SGreg Roach * @return RedirectResponse 178a4439c01SGreg Roach */ 179*e5a6b4d4SGreg Roach public function postAddFavoriteAction(Request $request, Tree $tree, UserInterface $user): RedirectResponse 180c1010edaSGreg Roach { 181a4439c01SGreg Roach $note = $request->get('note', ''); 182a4439c01SGreg Roach $title = $request->get('title', ''); 183a4439c01SGreg Roach $url = $request->get('url', ''); 184a4439c01SGreg Roach $xref = $request->get('xref', ''); 1855c007fdfSGreg Roach $fav_category = $request->get('fav_category', ''); 186a4439c01SGreg Roach 18725cd7ed9SGreg Roach $record = GedcomRecord::getInstance($xref, $tree); 18825cd7ed9SGreg Roach 189a4439c01SGreg Roach if (Auth::isManager($tree, $user)) { 1905c007fdfSGreg Roach if ($fav_category === 'url' && $url !== '') { 191a4439c01SGreg Roach $this->addUrlFavorite($tree, $url, $title ?: $url, $note); 1925c007fdfSGreg Roach } 1935c007fdfSGreg Roach 1945c007fdfSGreg Roach if ($fav_category === 'record' && $record instanceof GedcomRecord && $record->canShow()) { 19525cd7ed9SGreg Roach $this->addRecordFavorite($tree, $record, $note); 196a4439c01SGreg Roach } 197a4439c01SGreg Roach } 198a4439c01SGreg Roach 199aa6f03bbSGreg Roach $url = route('tree-page', ['ged' => $tree->name()]); 200a4439c01SGreg Roach 201a4439c01SGreg Roach return new RedirectResponse($url); 202a4439c01SGreg Roach } 203a4439c01SGreg Roach 204a4439c01SGreg Roach /** 205a4439c01SGreg Roach * @param Request $request 206b6db7c1fSGreg Roach * @param Tree $tree 207*e5a6b4d4SGreg Roach * @param UserInterface $user 208a4439c01SGreg Roach * 209a4439c01SGreg Roach * @return RedirectResponse 210a4439c01SGreg Roach */ 211*e5a6b4d4SGreg Roach public function postDeleteFavoriteAction(Request $request, Tree $tree, UserInterface $user): RedirectResponse 212c1010edaSGreg Roach { 213a4439c01SGreg Roach $favorite_id = (int) $request->get('favorite_id'); 214a4439c01SGreg Roach 215a4439c01SGreg Roach if (Auth::isManager($tree, $user)) { 2160587a5b3SGreg Roach DB::table('favorite') 2170587a5b3SGreg Roach ->where('favorite_id', '=', $favorite_id) 2180587a5b3SGreg Roach ->whereNull('user_id') 2190587a5b3SGreg Roach ->delete(); 220a4439c01SGreg Roach } 221a4439c01SGreg Roach 222aa6f03bbSGreg Roach $url = route('tree-page', ['ged' => $tree->name()]); 223a4439c01SGreg Roach 224a4439c01SGreg Roach return new RedirectResponse($url); 225a4439c01SGreg Roach } 226a4439c01SGreg Roach 227a4439c01SGreg Roach /** 228a4439c01SGreg Roach * @param Tree $tree 229a4439c01SGreg Roach * @param string $url 230a4439c01SGreg Roach * @param string $title 231a4439c01SGreg Roach * @param string $note 23218d7a90dSGreg Roach * 23318d7a90dSGreg Roach * @return void 234a4439c01SGreg Roach */ 235c1010edaSGreg Roach private function addUrlFavorite(Tree $tree, string $url, string $title, string $note) 236c1010edaSGreg Roach { 2370587a5b3SGreg Roach DB::table('favorite')->updateOrInsert([ 23872cf66d4SGreg Roach 'gedcom_id' => $tree->id(), 2390587a5b3SGreg Roach 'user_id' => null, 240a4439c01SGreg Roach 'url' => $url, 2410587a5b3SGreg Roach ], [ 2420587a5b3SGreg Roach 'favorite_type' => 'URL', 243a4439c01SGreg Roach 'note' => $note, 244a4439c01SGreg Roach 'title' => $title, 245a4439c01SGreg Roach ]); 246a4439c01SGreg Roach } 247a4439c01SGreg Roach 248a4439c01SGreg Roach /** 249a4439c01SGreg Roach * @param Tree $tree 25025cd7ed9SGreg Roach * @param GedcomRecord $record 251a4439c01SGreg Roach * @param string $note 25218d7a90dSGreg Roach * 25318d7a90dSGreg Roach * @return void 254a4439c01SGreg Roach */ 25525cd7ed9SGreg Roach private function addRecordFavorite(Tree $tree, GedcomRecord $record, string $note) 256c1010edaSGreg Roach { 2570587a5b3SGreg Roach DB::table('favorite')->updateOrInsert([ 25872cf66d4SGreg Roach 'gedcom_id' => $tree->id(), 2590587a5b3SGreg Roach 'user_id' => null, 26025cd7ed9SGreg Roach 'xref' => $record->xref(), 2610587a5b3SGreg Roach ], [ 26225cd7ed9SGreg Roach 'favorite_type' => $record::RECORD_TYPE, 263a4439c01SGreg Roach 'note' => $note, 264a4439c01SGreg Roach ]); 265a4439c01SGreg Roach } 2668c2e8227SGreg Roach} 267