18c2e8227SGreg Roach<?php 28c2e8227SGreg Roach/** 38c2e8227SGreg Roach * webtrees: online genealogy 41062a142SGreg Roach * Copyright (C) 2018 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 */ 1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module; 178c2e8227SGreg Roach 180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth; 190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database; 200e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 210e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N; 229807cf72SGreg Roachuse Fisharebest\Webtrees\Tree; 23a6afca4cSGreg Roachuse Fisharebest\Webtrees\User; 24a4439c01SGreg Roachuse stdClass; 25a4439c01SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse; 268840c547SGreg Roachuse Symfony\Component\HttpFoundation\Request; 278c2e8227SGreg Roach 288c2e8227SGreg Roach/** 298c2e8227SGreg Roach * Class UserFavoritesModule 308c2e8227SGreg Roach */ 31c1010edaSGreg Roachclass UserFavoritesModule extends AbstractModule implements ModuleBlockInterface 32c1010edaSGreg Roach{ 33a4439c01SGreg Roach /** 34a4439c01SGreg Roach * How should this module be labelled on tabs, menus, etc.? 35a4439c01SGreg Roach * 36a4439c01SGreg Roach * @return string 37a4439c01SGreg Roach */ 38c1010edaSGreg Roach public function getTitle() 39c1010edaSGreg Roach { 40bbb76c12SGreg Roach /* I18N: Name of a module */ 41bbb76c12SGreg Roach return I18N::translate('Favorites'); 42a4439c01SGreg Roach } 43a4439c01SGreg Roach 44a4439c01SGreg Roach /** 45a4439c01SGreg Roach * A sentence describing what this module does. 46a4439c01SGreg Roach * 47a4439c01SGreg Roach * @return string 48a4439c01SGreg Roach */ 49c1010edaSGreg Roach public function getDescription() 50c1010edaSGreg Roach { 51bbb76c12SGreg Roach /* I18N: Description of the “Favorites” module */ 52bbb76c12SGreg Roach return I18N::translate('Display and manage a user’s favorite pages.'); 538c2e8227SGreg Roach } 548c2e8227SGreg Roach 5576692c8bSGreg Roach /** 56a4439c01SGreg Roach * Generate the HTML content of this block. 57a4439c01SGreg Roach * 58a4439c01SGreg Roach * @param Tree $tree 59a4439c01SGreg Roach * @param int $block_id 60a4439c01SGreg Roach * @param bool $template 61a4439c01SGreg Roach * @param string[] $cfg 62a4439c01SGreg Roach * 63a4439c01SGreg Roach * @return string 64a4439c01SGreg Roach */ 65c1010edaSGreg Roach public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 66c1010edaSGreg Roach { 67a4439c01SGreg Roach $content = view('modules/user_favorites/favorites', [ 68a4439c01SGreg Roach 'block_id' => $block_id, 69a4439c01SGreg Roach 'favorites' => $this->getFavorites($tree, Auth::user()), 70a4439c01SGreg Roach 'tree' => $tree, 71a4439c01SGreg Roach ]); 72a4439c01SGreg Roach 73a4439c01SGreg Roach if ($template) { 74147e99aaSGreg Roach return view('modules/block-template', [ 75a4439c01SGreg Roach 'block' => str_replace('_', '-', $this->getName()), 76a4439c01SGreg Roach 'id' => $block_id, 77a4439c01SGreg Roach 'config_url' => '', 78a4439c01SGreg Roach 'title' => $this->getTitle(), 79a4439c01SGreg Roach 'content' => $content, 80a4439c01SGreg Roach ]); 81a4439c01SGreg Roach } else { 82a4439c01SGreg Roach return $content; 83a4439c01SGreg Roach } 84a4439c01SGreg Roach } 85a4439c01SGreg Roach 86a4439c01SGreg Roach /** 87a4439c01SGreg Roach * Should this block load asynchronously using AJAX? 88a4439c01SGreg Roach * 89a4439c01SGreg Roach * Simple blocks are faster in-line, more comples ones 90a4439c01SGreg Roach * can be loaded later. 91a4439c01SGreg Roach * 92a4439c01SGreg Roach * @return bool 93a4439c01SGreg Roach */ 94c1010edaSGreg Roach public function loadAjax(): bool 95c1010edaSGreg Roach { 96a4439c01SGreg Roach return false; 97a4439c01SGreg Roach } 98a4439c01SGreg Roach 99a4439c01SGreg Roach /** 10076692c8bSGreg Roach * Can this block be shown on the user’s home page? 10176692c8bSGreg Roach * 10276692c8bSGreg Roach * @return bool 10376692c8bSGreg Roach */ 104c1010edaSGreg Roach public function isUserBlock(): bool 105c1010edaSGreg Roach { 1068c2e8227SGreg Roach return true; 1078c2e8227SGreg Roach } 1088c2e8227SGreg Roach 10976692c8bSGreg Roach /** 11076692c8bSGreg Roach * Can this block be shown on the tree’s home page? 11176692c8bSGreg Roach * 11276692c8bSGreg Roach * @return bool 11376692c8bSGreg Roach */ 114c1010edaSGreg Roach public function isGedcomBlock(): bool 115c1010edaSGreg Roach { 1168c2e8227SGreg Roach return false; 1178c2e8227SGreg Roach } 1188c2e8227SGreg Roach 1198c2e8227SGreg Roach /** 120*a45f9889SGreg Roach * Update the configuration for a block. 121*a45f9889SGreg Roach * 122*a45f9889SGreg Roach * @param Request $request 123*a45f9889SGreg Roach * @param int $block_id 124*a45f9889SGreg Roach * 125*a45f9889SGreg Roach * @return void 126*a45f9889SGreg Roach */ 127*a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 128*a45f9889SGreg Roach { 129*a45f9889SGreg Roach } 130*a45f9889SGreg Roach 131*a45f9889SGreg Roach /** 132a4439c01SGreg Roach * An HTML form to edit block settings 133a4439c01SGreg Roach * 134a4439c01SGreg Roach * @param Tree $tree 135a4439c01SGreg Roach * @param int $block_id 136a4439c01SGreg Roach * 137a4439c01SGreg Roach * @return void 138a4439c01SGreg Roach */ 139*a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 140c1010edaSGreg Roach { 141a4439c01SGreg Roach } 142a4439c01SGreg Roach 143a4439c01SGreg Roach /** 144a4439c01SGreg Roach * Get the favorites for a user 1458c2e8227SGreg Roach * 1469807cf72SGreg Roach * @param Tree $tree 147a6afca4cSGreg Roach * @param User $user 1488c2e8227SGreg Roach * 149a4439c01SGreg Roach * @return stdClass[] 1508c2e8227SGreg Roach */ 151c1010edaSGreg Roach public function getFavorites(Tree $tree, User $user) 152c1010edaSGreg Roach { 153bdb3725aSGreg Roach $favorites = Database::prepare( 154e5588fb0SGreg Roach "SELECT favorite_id, user_id, gedcom_id, xref, favorite_type, title, note, url" . 155bdb3725aSGreg Roach " FROM `##favorite` WHERE gedcom_id = :tree_id AND user_id = :user_id" 156bdb3725aSGreg Roach )->execute([ 1579807cf72SGreg Roach 'tree_id' => $tree->getTreeId(), 158a6afca4cSGreg Roach 'user_id' => $user->getUserId(), 159bdb3725aSGreg Roach ])->fetchAll(); 1609807cf72SGreg Roach 1619807cf72SGreg Roach foreach ($favorites as $favorite) { 1629807cf72SGreg Roach $favorite->record = GedcomRecord::getInstance($favorite->xref, $tree); 1639807cf72SGreg Roach } 1649807cf72SGreg Roach 1659807cf72SGreg Roach return $favorites; 1668c2e8227SGreg Roach } 1678c2e8227SGreg Roach 16876692c8bSGreg Roach /** 1698840c547SGreg Roach * @param Request $request 170b6db7c1fSGreg Roach * @param Tree $tree 171b6db7c1fSGreg Roach * @param User $user 17276692c8bSGreg Roach * 173a4439c01SGreg Roach * @return RedirectResponse 17476692c8bSGreg Roach */ 175b6db7c1fSGreg Roach public function postAddFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse 176c1010edaSGreg Roach { 177a4439c01SGreg Roach $note = $request->get('note', ''); 178a4439c01SGreg Roach $title = $request->get('title', ''); 179a4439c01SGreg Roach $url = $request->get('url', ''); 180a4439c01SGreg Roach $xref = $request->get('xref', ''); 1818840c547SGreg Roach 182a4439c01SGreg Roach if (Auth::check()) { 183a4439c01SGreg Roach if ($url !== '') { 184a4439c01SGreg Roach $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note); 185a4439c01SGreg Roach } else { 186a4439c01SGreg Roach $this->addRecordFavorite($tree, $user, $xref, $note); 187a4439c01SGreg Roach } 1888c2e8227SGreg Roach } 1898840c547SGreg Roach 190a4439c01SGreg Roach $url = route('user-page', ['ged' => $tree->getName()]); 191a4439c01SGreg Roach 192a4439c01SGreg Roach return new RedirectResponse($url); 193a4439c01SGreg Roach } 194a4439c01SGreg Roach 195a4439c01SGreg Roach /** 196a4439c01SGreg Roach * @param Request $request 197b6db7c1fSGreg Roach * @param Tree $tree 198b6db7c1fSGreg Roach * @param User $user 199a4439c01SGreg Roach * 200a4439c01SGreg Roach * @return RedirectResponse 201a4439c01SGreg Roach */ 202b6db7c1fSGreg Roach public function postDeleteFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse 203c1010edaSGreg Roach { 204a4439c01SGreg Roach $favorite_id = (int)$request->get('favorite_id'); 205a4439c01SGreg Roach 206a4439c01SGreg Roach if (Auth::check()) { 207a4439c01SGreg Roach Database::prepare( 208a4439c01SGreg Roach "DELETE FROM `##favorite` WHERE favorite_id = :favorite_id AND user_id = :user_id" 209a4439c01SGreg Roach )->execute([ 210a4439c01SGreg Roach 'favorite_id' => $favorite_id, 211a4439c01SGreg Roach 'user_id' => $user->getUserId(), 212a4439c01SGreg Roach ]); 213a4439c01SGreg Roach } 214a4439c01SGreg Roach 215a4439c01SGreg Roach $url = route('user-page', ['ged' => $tree->getName()]); 216a4439c01SGreg Roach 217a4439c01SGreg Roach return new RedirectResponse($url); 218a4439c01SGreg Roach } 219a4439c01SGreg Roach 220a4439c01SGreg Roach /** 221a4439c01SGreg Roach * @param Tree $tree 222a4439c01SGreg Roach * @param User $user 223a4439c01SGreg Roach * @param string $url 224a4439c01SGreg Roach * @param string $title 225a4439c01SGreg Roach * @param string $note 226a4439c01SGreg Roach */ 227c1010edaSGreg Roach private function addUrlFavorite(Tree $tree, User $user, string $url, string $title, string $note) 228c1010edaSGreg Roach { 229a4439c01SGreg Roach $favorite = Database::prepare( 230a4439c01SGreg Roach "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND url = :url" 231a4439c01SGreg Roach )->execute([ 232a4439c01SGreg Roach 'gedcom_id' => $tree->getTreeId(), 233a4439c01SGreg Roach 'user_id' => $user->getUserId(), 234a4439c01SGreg Roach 'url' => $url, 235a4439c01SGreg Roach ])->fetchOneRow(); 236a4439c01SGreg Roach 237a4439c01SGreg Roach if ($favorite === null) { 238a4439c01SGreg Roach Database::prepare( 239a4439c01SGreg Roach "INSERT INTO `##favorite` (gedcom_id, user_id, url, note, title) VALUES (:gedcom_id, :user_id, :url, :note, :title)" 240a4439c01SGreg Roach )->execute([ 241a4439c01SGreg Roach 'gedcom_id' => $tree->getTreeId(), 242a4439c01SGreg Roach 'user_id' => $user->getUserId(), 243a4439c01SGreg Roach 'url' => $url, 244a4439c01SGreg Roach 'note' => $note, 245a4439c01SGreg Roach 'title' => $title, 246a4439c01SGreg Roach ]); 247a4439c01SGreg Roach } else { 248a4439c01SGreg Roach Database::prepare( 249a4439c01SGreg Roach "UPDATE `##favorite` SET note = :note, title = :title WHERE favorite_id = :favorite_id" 250a4439c01SGreg Roach )->execute([ 251a4439c01SGreg Roach 'note' => $note, 252a4439c01SGreg Roach 'title' => $title, 253a4439c01SGreg Roach 'favorite_id' => $favorite->favorite_id, 254a4439c01SGreg Roach ]); 255a4439c01SGreg Roach } 256a4439c01SGreg Roach } 257a4439c01SGreg Roach 258a4439c01SGreg Roach /** 259a4439c01SGreg Roach * @param Tree $tree 260a4439c01SGreg Roach * @param User $user 261a4439c01SGreg Roach * @param string $xref 262a4439c01SGreg Roach * @param string $note 263a4439c01SGreg Roach */ 264c1010edaSGreg Roach private function addRecordFavorite(Tree $tree, User $user, string $xref, string $note) 265c1010edaSGreg Roach { 266a4439c01SGreg Roach $favorite = Database::prepare( 267a4439c01SGreg Roach "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND xref = :xref" 268a4439c01SGreg Roach )->execute([ 269a4439c01SGreg Roach 'gedcom_id' => $tree->getTreeId(), 270a4439c01SGreg Roach 'user_id' => $user->getUserId(), 271a4439c01SGreg Roach 'xref' => $xref, 272a4439c01SGreg Roach ])->fetchOneRow(); 273a4439c01SGreg Roach 274a4439c01SGreg Roach if ($favorite === null) { 275a4439c01SGreg Roach Database::prepare( 276a4439c01SGreg Roach "INSERT INTO `##favorite` (gedcom_id, user_id, xref, note) VALUES (:gedcom_id, :user_id, :xref, :note)" 277a4439c01SGreg Roach )->execute([ 278a4439c01SGreg Roach 'gedcom_id' => $tree->getTreeId(), 279a4439c01SGreg Roach 'user_id' => $user->getUserId(), 280a4439c01SGreg Roach 'xref' => $xref, 281a4439c01SGreg Roach 'note' => $note, 282a4439c01SGreg Roach ]); 283a4439c01SGreg Roach } else { 284a4439c01SGreg Roach Database::prepare( 285a4439c01SGreg Roach "UPDATE `##favorite` SET note = :note WHERE favorite_id = :favorite_id" 286a4439c01SGreg Roach )->execute([ 287a4439c01SGreg Roach 'note' => $note, 288a4439c01SGreg Roach 'favorite_id' => $favorite->favorite_id, 289a4439c01SGreg Roach ]); 290a4439c01SGreg Roach } 2918c2e8227SGreg Roach } 2928c2e8227SGreg Roach} 293