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; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 239807cf72SGreg Roachuse Fisharebest\Webtrees\Tree; 24a6afca4cSGreg Roachuse Fisharebest\Webtrees\User; 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 */ 3349a243cbSGreg Roachclass FamilyTreeFavoritesModule extends AbstractModule implements ModuleInterface, 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', [ 80*26684e68SGreg 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 * 9476692c8bSGreg Roach * Simple blocks are faster in-line, more comples ones 9576692c8bSGreg Roach * can be loaded later. 9676692c8bSGreg Roach * 9776692c8bSGreg Roach * @return bool 9876692c8bSGreg Roach */ 99c1010edaSGreg Roach public function loadAjax(): bool 100c1010edaSGreg Roach { 1018c2e8227SGreg Roach return false; 1028c2e8227SGreg Roach } 1038c2e8227SGreg Roach 10476692c8bSGreg Roach /** 10576692c8bSGreg Roach * Can this block be shown on the user’s home page? 10676692c8bSGreg Roach * 10776692c8bSGreg Roach * @return bool 10876692c8bSGreg Roach */ 109c1010edaSGreg Roach public function isUserBlock(): bool 110c1010edaSGreg Roach { 1118c2e8227SGreg Roach return false; 1128c2e8227SGreg Roach } 1138c2e8227SGreg Roach 11476692c8bSGreg Roach /** 11576692c8bSGreg Roach * Can this block be shown on the tree’s home page? 11676692c8bSGreg Roach * 11776692c8bSGreg Roach * @return bool 11876692c8bSGreg Roach */ 119c1010edaSGreg Roach public function isGedcomBlock(): bool 120c1010edaSGreg Roach { 1218c2e8227SGreg Roach return true; 1228c2e8227SGreg Roach } 1238c2e8227SGreg Roach 12476692c8bSGreg Roach /** 125a45f9889SGreg Roach * Update the configuration for a block. 126a45f9889SGreg Roach * 127a45f9889SGreg Roach * @param Request $request 128a45f9889SGreg Roach * @param int $block_id 129a45f9889SGreg Roach * 130a45f9889SGreg Roach * @return void 131a45f9889SGreg Roach */ 132a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 133a45f9889SGreg Roach { 134a45f9889SGreg Roach } 135a45f9889SGreg Roach 136a45f9889SGreg Roach /** 13776692c8bSGreg Roach * An HTML form to edit block settings 13876692c8bSGreg Roach * 139e490cd80SGreg Roach * @param Tree $tree 14076692c8bSGreg Roach * @param int $block_id 141a9430be8SGreg Roach * 142a9430be8SGreg Roach * @return void 14376692c8bSGreg Roach */ 144a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 145c1010edaSGreg Roach { 1468d68cabeSGreg Roach } 1478c2e8227SGreg Roach 1488c2e8227SGreg Roach /** 149a4439c01SGreg Roach * Get favorites for a family tree 1508c2e8227SGreg Roach * 1519807cf72SGreg Roach * @param Tree $tree 1528c2e8227SGreg Roach * 1539807cf72SGreg Roach * @return stdClass[] 1548c2e8227SGreg Roach */ 1558f53f488SRico Sonntag public function getFavorites(Tree $tree): array 156c1010edaSGreg Roach { 1570587a5b3SGreg Roach return DB::table('favorite') 1580587a5b3SGreg Roach ->where('gedcom_id', '=', $tree->id()) 1590587a5b3SGreg Roach ->whereNull('user_id') 1600587a5b3SGreg Roach ->get() 1610587a5b3SGreg Roach ->map(function (stdClass $row) use ($tree): stdClass { 1620587a5b3SGreg Roach if ($row->xref !== null) { 1630587a5b3SGreg Roach $row->record = GedcomRecord::getInstance($row->xref, $tree); 16425cd7ed9SGreg Roach } else { 1650587a5b3SGreg Roach $row->record = null; 1669807cf72SGreg Roach } 1679807cf72SGreg Roach 1680587a5b3SGreg Roach return $row; 1690587a5b3SGreg Roach }) 1700587a5b3SGreg Roach ->all(); 1718c2e8227SGreg Roach } 172a4439c01SGreg Roach 173a4439c01SGreg Roach /** 174a4439c01SGreg Roach * @param Request $request 175b6db7c1fSGreg Roach * @param Tree $tree 176b6db7c1fSGreg Roach * @param User $user 177a4439c01SGreg Roach * 178a4439c01SGreg Roach * @return RedirectResponse 179a4439c01SGreg Roach */ 180b6db7c1fSGreg Roach public function postAddFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse 181c1010edaSGreg Roach { 182a4439c01SGreg Roach $note = $request->get('note', ''); 183a4439c01SGreg Roach $title = $request->get('title', ''); 184a4439c01SGreg Roach $url = $request->get('url', ''); 185a4439c01SGreg Roach $xref = $request->get('xref', ''); 1865c007fdfSGreg Roach $fav_category = $request->get('fav_category', ''); 187a4439c01SGreg Roach 18825cd7ed9SGreg Roach $record = GedcomRecord::getInstance($xref, $tree); 18925cd7ed9SGreg Roach 190a4439c01SGreg Roach if (Auth::isManager($tree, $user)) { 1915c007fdfSGreg Roach if ($fav_category === 'url' && $url !== '') { 192a4439c01SGreg Roach $this->addUrlFavorite($tree, $url, $title ?: $url, $note); 1935c007fdfSGreg Roach } 1945c007fdfSGreg Roach 1955c007fdfSGreg Roach if ($fav_category === 'record' && $record instanceof GedcomRecord && $record->canShow()) { 19625cd7ed9SGreg Roach $this->addRecordFavorite($tree, $record, $note); 197a4439c01SGreg Roach } 198a4439c01SGreg Roach } 199a4439c01SGreg Roach 200aa6f03bbSGreg Roach $url = route('tree-page', ['ged' => $tree->name()]); 201a4439c01SGreg Roach 202a4439c01SGreg Roach return new RedirectResponse($url); 203a4439c01SGreg Roach } 204a4439c01SGreg Roach 205a4439c01SGreg Roach /** 206a4439c01SGreg Roach * @param Request $request 207b6db7c1fSGreg Roach * @param Tree $tree 208b6db7c1fSGreg Roach * @param User $user 209a4439c01SGreg Roach * 210a4439c01SGreg Roach * @return RedirectResponse 211a4439c01SGreg Roach */ 212b6db7c1fSGreg Roach public function postDeleteFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse 213c1010edaSGreg Roach { 214a4439c01SGreg Roach $favorite_id = (int) $request->get('favorite_id'); 215a4439c01SGreg Roach 216a4439c01SGreg Roach if (Auth::isManager($tree, $user)) { 2170587a5b3SGreg Roach DB::table('favorite') 2180587a5b3SGreg Roach ->where('favorite_id', '=', $favorite_id) 2190587a5b3SGreg Roach ->whereNull('user_id') 2200587a5b3SGreg Roach ->delete(); 221a4439c01SGreg Roach } 222a4439c01SGreg Roach 223aa6f03bbSGreg Roach $url = route('tree-page', ['ged' => $tree->name()]); 224a4439c01SGreg Roach 225a4439c01SGreg Roach return new RedirectResponse($url); 226a4439c01SGreg Roach } 227a4439c01SGreg Roach 228a4439c01SGreg Roach /** 229a4439c01SGreg Roach * @param Tree $tree 230a4439c01SGreg Roach * @param string $url 231a4439c01SGreg Roach * @param string $title 232a4439c01SGreg Roach * @param string $note 23318d7a90dSGreg Roach * 23418d7a90dSGreg Roach * @return void 235a4439c01SGreg Roach */ 236c1010edaSGreg Roach private function addUrlFavorite(Tree $tree, string $url, string $title, string $note) 237c1010edaSGreg Roach { 2380587a5b3SGreg Roach DB::table('favorite')->updateOrInsert([ 23972cf66d4SGreg Roach 'gedcom_id' => $tree->id(), 2400587a5b3SGreg Roach 'user_id' => null, 241a4439c01SGreg Roach 'url' => $url, 2420587a5b3SGreg Roach ], [ 2430587a5b3SGreg Roach 'favorite_type' => 'URL', 244a4439c01SGreg Roach 'note' => $note, 245a4439c01SGreg Roach 'title' => $title, 246a4439c01SGreg Roach ]); 247a4439c01SGreg Roach } 248a4439c01SGreg Roach 249a4439c01SGreg Roach /** 250a4439c01SGreg Roach * @param Tree $tree 25125cd7ed9SGreg Roach * @param GedcomRecord $record 252a4439c01SGreg Roach * @param string $note 25318d7a90dSGreg Roach * 25418d7a90dSGreg Roach * @return void 255a4439c01SGreg Roach */ 25625cd7ed9SGreg Roach private function addRecordFavorite(Tree $tree, GedcomRecord $record, string $note) 257c1010edaSGreg Roach { 2580587a5b3SGreg Roach DB::table('favorite')->updateOrInsert([ 25972cf66d4SGreg Roach 'gedcom_id' => $tree->id(), 2600587a5b3SGreg Roach 'user_id' => null, 26125cd7ed9SGreg Roach 'xref' => $record->xref(), 2620587a5b3SGreg Roach ], [ 26325cd7ed9SGreg Roach 'favorite_type' => $record::RECORD_TYPE, 264a4439c01SGreg Roach 'note' => $note, 265a4439c01SGreg Roach ]); 266a4439c01SGreg Roach } 2678c2e8227SGreg Roach} 268