18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 4*8fcd0d32SGreg 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\Database; 220e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 249807cf72SGreg Roachuse Fisharebest\Webtrees\Tree; 25a6afca4cSGreg Roachuse Fisharebest\Webtrees\User; 26a4439c01SGreg Roachuse stdClass; 27a4439c01SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse; 288840c547SGreg Roachuse Symfony\Component\HttpFoundation\Request; 298c2e8227SGreg Roach 308c2e8227SGreg Roach/** 318c2e8227SGreg Roach * Class UserFavoritesModule 328c2e8227SGreg Roach */ 33c1010edaSGreg Roachclass UserFavoritesModule extends AbstractModule implements ModuleBlockInterface 34c1010edaSGreg Roach{ 35a4439c01SGreg Roach /** 36a4439c01SGreg Roach * How should this module be labelled on tabs, menus, etc.? 37a4439c01SGreg Roach * 38a4439c01SGreg Roach * @return string 39a4439c01SGreg Roach */ 408f53f488SRico Sonntag public function getTitle(): string 41c1010edaSGreg Roach { 42bbb76c12SGreg Roach /* I18N: Name of a module */ 43bbb76c12SGreg Roach return I18N::translate('Favorites'); 44a4439c01SGreg Roach } 45a4439c01SGreg Roach 46a4439c01SGreg Roach /** 47a4439c01SGreg Roach * A sentence describing what this module does. 48a4439c01SGreg Roach * 49a4439c01SGreg Roach * @return string 50a4439c01SGreg Roach */ 518f53f488SRico Sonntag public function getDescription(): string 52c1010edaSGreg Roach { 53bbb76c12SGreg Roach /* I18N: Description of the “Favorites” module */ 54bbb76c12SGreg Roach return I18N::translate('Display and manage a user’s favorite pages.'); 558c2e8227SGreg Roach } 568c2e8227SGreg Roach 5776692c8bSGreg Roach /** 58a4439c01SGreg Roach * Generate the HTML content of this block. 59a4439c01SGreg Roach * 60a4439c01SGreg Roach * @param Tree $tree 61a4439c01SGreg Roach * @param int $block_id 625f2ae573SGreg Roach * @param string $ctype 63a4439c01SGreg Roach * @param string[] $cfg 64a4439c01SGreg Roach * 65a4439c01SGreg Roach * @return string 66a4439c01SGreg Roach */ 675f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 68c1010edaSGreg Roach { 69a4439c01SGreg Roach $content = view('modules/user_favorites/favorites', [ 70a4439c01SGreg Roach 'block_id' => $block_id, 71a4439c01SGreg Roach 'favorites' => $this->getFavorites($tree, Auth::user()), 72a4439c01SGreg Roach 'tree' => $tree, 73a4439c01SGreg Roach ]); 74a4439c01SGreg Roach 756a8879feSGreg Roach if ($ctype !== '') { 76147e99aaSGreg Roach return view('modules/block-template', [ 77a4439c01SGreg Roach 'block' => str_replace('_', '-', $this->getName()), 78a4439c01SGreg Roach 'id' => $block_id, 79a4439c01SGreg Roach 'config_url' => '', 80a4439c01SGreg Roach 'title' => $this->getTitle(), 81a4439c01SGreg Roach 'content' => $content, 82a4439c01SGreg Roach ]); 83a4439c01SGreg Roach } 84b2ce94c6SRico Sonntag 85b2ce94c6SRico Sonntag return $content; 86a4439c01SGreg Roach } 87a4439c01SGreg Roach 88a4439c01SGreg Roach /** 89a4439c01SGreg Roach * Should this block load asynchronously using AJAX? 90a4439c01SGreg Roach * 91a4439c01SGreg Roach * Simple blocks are faster in-line, more comples ones 92a4439c01SGreg Roach * can be loaded later. 93a4439c01SGreg Roach * 94a4439c01SGreg Roach * @return bool 95a4439c01SGreg Roach */ 96c1010edaSGreg Roach public function loadAjax(): bool 97c1010edaSGreg Roach { 98a4439c01SGreg Roach return false; 99a4439c01SGreg Roach } 100a4439c01SGreg Roach 101a4439c01SGreg Roach /** 10276692c8bSGreg Roach * Can this block be shown on the user’s home page? 10376692c8bSGreg Roach * 10476692c8bSGreg Roach * @return bool 10576692c8bSGreg Roach */ 106c1010edaSGreg Roach public function isUserBlock(): bool 107c1010edaSGreg Roach { 1088c2e8227SGreg Roach return true; 1098c2e8227SGreg Roach } 1108c2e8227SGreg Roach 11176692c8bSGreg Roach /** 11276692c8bSGreg Roach * Can this block be shown on the tree’s home page? 11376692c8bSGreg Roach * 11476692c8bSGreg Roach * @return bool 11576692c8bSGreg Roach */ 116c1010edaSGreg Roach public function isGedcomBlock(): bool 117c1010edaSGreg Roach { 1188c2e8227SGreg Roach return false; 1198c2e8227SGreg Roach } 1208c2e8227SGreg Roach 1218c2e8227SGreg Roach /** 122a45f9889SGreg Roach * Update the configuration for a block. 123a45f9889SGreg Roach * 124a45f9889SGreg Roach * @param Request $request 125a45f9889SGreg Roach * @param int $block_id 126a45f9889SGreg Roach * 127a45f9889SGreg Roach * @return void 128a45f9889SGreg Roach */ 129a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 130a45f9889SGreg Roach { 131a45f9889SGreg Roach } 132a45f9889SGreg Roach 133a45f9889SGreg Roach /** 134a4439c01SGreg Roach * An HTML form to edit block settings 135a4439c01SGreg Roach * 136a4439c01SGreg Roach * @param Tree $tree 137a4439c01SGreg Roach * @param int $block_id 138a4439c01SGreg Roach * 139a4439c01SGreg Roach * @return void 140a4439c01SGreg Roach */ 141a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 142c1010edaSGreg Roach { 143a4439c01SGreg Roach } 144a4439c01SGreg Roach 145a4439c01SGreg Roach /** 146a4439c01SGreg Roach * Get the favorites for a user 1478c2e8227SGreg Roach * 1489807cf72SGreg Roach * @param Tree $tree 149a6afca4cSGreg Roach * @param User $user 1508c2e8227SGreg Roach * 151a4439c01SGreg Roach * @return stdClass[] 1528c2e8227SGreg Roach */ 1538f53f488SRico Sonntag public function getFavorites(Tree $tree, User $user): array 154c1010edaSGreg Roach { 155bdb3725aSGreg Roach $favorites = Database::prepare( 156e5588fb0SGreg Roach "SELECT favorite_id, user_id, gedcom_id, xref, favorite_type, title, note, url" . 157bdb3725aSGreg Roach " FROM `##favorite` WHERE gedcom_id = :tree_id AND user_id = :user_id" 158bdb3725aSGreg Roach )->execute([ 15972cf66d4SGreg Roach 'tree_id' => $tree->id(), 160a6afca4cSGreg Roach 'user_id' => $user->getUserId(), 161bdb3725aSGreg Roach ])->fetchAll(); 1629807cf72SGreg Roach 1639807cf72SGreg Roach foreach ($favorites as $favorite) { 16425cd7ed9SGreg Roach if ($favorite->xref !== null) { 1659807cf72SGreg Roach $favorite->record = GedcomRecord::getInstance($favorite->xref, $tree); 16625cd7ed9SGreg Roach } else { 16725cd7ed9SGreg Roach $favorite->record = null; 16825cd7ed9SGreg Roach } 1699807cf72SGreg Roach } 1709807cf72SGreg Roach 1719807cf72SGreg Roach return $favorites; 1728c2e8227SGreg Roach } 1738c2e8227SGreg Roach 17476692c8bSGreg Roach /** 1758840c547SGreg Roach * @param Request $request 176b6db7c1fSGreg Roach * @param Tree $tree 177b6db7c1fSGreg Roach * @param User $user 17876692c8bSGreg Roach * 179a4439c01SGreg Roach * @return RedirectResponse 18076692c8bSGreg Roach */ 181b6db7c1fSGreg Roach public function postAddFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse 182c1010edaSGreg Roach { 183a4439c01SGreg Roach $note = $request->get('note', ''); 184a4439c01SGreg Roach $title = $request->get('title', ''); 185a4439c01SGreg Roach $url = $request->get('url', ''); 186a4439c01SGreg Roach $xref = $request->get('xref', ''); 1875c007fdfSGreg Roach $fav_category = $request->get('fav_category', ''); 1888840c547SGreg Roach 18925cd7ed9SGreg Roach $record = GedcomRecord::getInstance($xref, $tree); 19025cd7ed9SGreg Roach 191a4439c01SGreg Roach if (Auth::check()) { 1925c007fdfSGreg Roach if ($fav_category === 'url' && $url !== '') { 193a4439c01SGreg Roach $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note); 1945c007fdfSGreg Roach } 1955c007fdfSGreg Roach 1965c007fdfSGreg Roach if ($fav_category === 'record' && $record instanceof GedcomRecord && $record->canShow()) { 19725cd7ed9SGreg Roach $this->addRecordFavorite($tree, $user, $record, $note); 198a4439c01SGreg Roach } 1998c2e8227SGreg Roach } 2008840c547SGreg Roach 201aa6f03bbSGreg Roach $url = route('user-page', ['ged' => $tree->name()]); 202a4439c01SGreg Roach 203a4439c01SGreg Roach return new RedirectResponse($url); 204a4439c01SGreg Roach } 205a4439c01SGreg Roach 206a4439c01SGreg Roach /** 207a4439c01SGreg Roach * @param Request $request 208b6db7c1fSGreg Roach * @param Tree $tree 209b6db7c1fSGreg Roach * @param User $user 210a4439c01SGreg Roach * 211a4439c01SGreg Roach * @return RedirectResponse 212a4439c01SGreg Roach */ 213b6db7c1fSGreg Roach public function postDeleteFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse 214c1010edaSGreg Roach { 215a4439c01SGreg Roach $favorite_id = (int) $request->get('favorite_id'); 216a4439c01SGreg Roach 217a4439c01SGreg Roach if (Auth::check()) { 218a4439c01SGreg Roach Database::prepare( 219a4439c01SGreg Roach "DELETE FROM `##favorite` WHERE favorite_id = :favorite_id AND user_id = :user_id" 220a4439c01SGreg Roach )->execute([ 221a4439c01SGreg Roach 'favorite_id' => $favorite_id, 222a4439c01SGreg Roach 'user_id' => $user->getUserId(), 223a4439c01SGreg Roach ]); 224a4439c01SGreg Roach } 225a4439c01SGreg Roach 226aa6f03bbSGreg Roach $url = route('user-page', ['ged' => $tree->name()]); 227a4439c01SGreg Roach 228a4439c01SGreg Roach return new RedirectResponse($url); 229a4439c01SGreg Roach } 230a4439c01SGreg Roach 231a4439c01SGreg Roach /** 232a4439c01SGreg Roach * @param Tree $tree 233a4439c01SGreg Roach * @param User $user 234a4439c01SGreg Roach * @param string $url 235a4439c01SGreg Roach * @param string $title 236a4439c01SGreg Roach * @param string $note 23718d7a90dSGreg Roach * 23818d7a90dSGreg Roach * @return void 239a4439c01SGreg Roach */ 240c1010edaSGreg Roach private function addUrlFavorite(Tree $tree, User $user, string $url, string $title, string $note) 241c1010edaSGreg Roach { 242a4439c01SGreg Roach $favorite = Database::prepare( 243a4439c01SGreg Roach "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND url = :url" 244a4439c01SGreg Roach )->execute([ 24572cf66d4SGreg Roach 'gedcom_id' => $tree->id(), 246a4439c01SGreg Roach 'user_id' => $user->getUserId(), 247a4439c01SGreg Roach 'url' => $url, 248a4439c01SGreg Roach ])->fetchOneRow(); 249a4439c01SGreg Roach 250a4439c01SGreg Roach if ($favorite === null) { 251a4439c01SGreg Roach Database::prepare( 25225cd7ed9SGreg Roach "INSERT INTO `##favorite` (gedcom_id, favorite_type, user_id, url, note, title) VALUES (:gedcom_id, 'URL', :user_id, :url, :note, :title)" 253a4439c01SGreg Roach )->execute([ 25472cf66d4SGreg Roach 'gedcom_id' => $tree->id(), 255a4439c01SGreg Roach 'user_id' => $user->getUserId(), 256a4439c01SGreg Roach 'url' => $url, 257a4439c01SGreg Roach 'note' => $note, 258a4439c01SGreg Roach 'title' => $title, 259a4439c01SGreg Roach ]); 260a4439c01SGreg Roach } else { 261a4439c01SGreg Roach Database::prepare( 262a4439c01SGreg Roach "UPDATE `##favorite` SET note = :note, title = :title WHERE favorite_id = :favorite_id" 263a4439c01SGreg Roach )->execute([ 264a4439c01SGreg Roach 'note' => $note, 265a4439c01SGreg Roach 'title' => $title, 266a4439c01SGreg Roach 'favorite_id' => $favorite->favorite_id, 267a4439c01SGreg Roach ]); 268a4439c01SGreg Roach } 269a4439c01SGreg Roach } 270a4439c01SGreg Roach 271a4439c01SGreg Roach /** 272a4439c01SGreg Roach * @param Tree $tree 273a4439c01SGreg Roach * @param User $user 27425cd7ed9SGreg Roach * @param GedcomRecord $record 275a4439c01SGreg Roach * @param string $note 27618d7a90dSGreg Roach * 27718d7a90dSGreg Roach * @return void 278a4439c01SGreg Roach */ 27925cd7ed9SGreg Roach private function addRecordFavorite(Tree $tree, User $user, GedcomRecord $record, string $note) 280c1010edaSGreg Roach { 281a4439c01SGreg Roach $favorite = Database::prepare( 282a4439c01SGreg Roach "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND xref = :xref" 283a4439c01SGreg Roach )->execute([ 28472cf66d4SGreg Roach 'gedcom_id' => $tree->id(), 285a4439c01SGreg Roach 'user_id' => $user->getUserId(), 28625cd7ed9SGreg Roach 'xref' => $record->xref(), 287a4439c01SGreg Roach ])->fetchOneRow(); 288a4439c01SGreg Roach 289a4439c01SGreg Roach if ($favorite === null) { 290a4439c01SGreg Roach Database::prepare( 29125cd7ed9SGreg Roach "INSERT INTO `##favorite` (gedcom_id, user_id, favorite_type, xref, note) VALUES (:gedcom_id, :user_id, :favorite_type, :xref, :note)" 292a4439c01SGreg Roach )->execute([ 29372cf66d4SGreg Roach 'gedcom_id' => $tree->id(), 294a4439c01SGreg Roach 'user_id' => $user->getUserId(), 29525cd7ed9SGreg Roach 'favorite_type' => $record::RECORD_TYPE, 29625cd7ed9SGreg Roach 'xref' => $record->xref(), 297a4439c01SGreg Roach 'note' => $note, 298a4439c01SGreg Roach ]); 299a4439c01SGreg Roach } else { 300a4439c01SGreg Roach Database::prepare( 301a4439c01SGreg Roach "UPDATE `##favorite` SET note = :note WHERE favorite_id = :favorite_id" 302a4439c01SGreg Roach )->execute([ 303a4439c01SGreg Roach 'note' => $note, 304a4439c01SGreg Roach 'favorite_id' => $favorite->favorite_id, 305a4439c01SGreg Roach ]); 306a4439c01SGreg Roach } 3078c2e8227SGreg Roach } 3088c2e8227SGreg Roach} 309