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 */ 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; 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 */ 33c1010edaSGreg Roachclass FamilyTreeFavoritesModule extends AbstractModule implements ModuleBlockInterface 34c1010edaSGreg Roach{ 3576692c8bSGreg Roach /** 3676692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 3776692c8bSGreg Roach * 3876692c8bSGreg Roach * @return string 3976692c8bSGreg Roach */ 408f53f488SRico Sonntag public function getTitle(): string 41c1010edaSGreg Roach { 42bbb76c12SGreg Roach /* I18N: Name of a module */ 43bbb76c12SGreg Roach return I18N::translate('Favorites'); 448c2e8227SGreg Roach } 458c2e8227SGreg Roach 4676692c8bSGreg Roach /** 4776692c8bSGreg Roach * A sentence describing what this module does. 4876692c8bSGreg Roach * 4976692c8bSGreg Roach * @return string 5076692c8bSGreg 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 family tree’s favorite pages.'); 558c2e8227SGreg Roach } 568c2e8227SGreg Roach 5776692c8bSGreg Roach /** 5876692c8bSGreg Roach * Generate the HTML content of this block. 5976692c8bSGreg Roach * 60e490cd80SGreg Roach * @param Tree $tree 6176692c8bSGreg Roach * @param int $block_id 625f2ae573SGreg Roach * @param string $ctype 63727f238cSGreg Roach * @param string[] $cfg 6476692c8bSGreg Roach * 6576692c8bSGreg Roach * @return string 6676692c8bSGreg Roach */ 675f2ae573SGreg Roach public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 68c1010edaSGreg Roach { 69a4439c01SGreg Roach $content = view('modules/gedcom_favorites/favorites', [ 70a4439c01SGreg Roach 'block_id' => $block_id, 71a4439c01SGreg Roach 'favorites' => $this->getFavorites($tree), 72e490cd80SGreg Roach 'is_manager' => Auth::isManager($tree), 73e490cd80SGreg Roach 'tree' => $tree, 749807cf72SGreg Roach ]); 759807cf72SGreg Roach 76*6a8879feSGreg Roach if ($ctype !== '') { 77147e99aaSGreg Roach return view('modules/block-template', [ 789c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 799c6524dcSGreg Roach 'id' => $block_id, 809c6524dcSGreg Roach 'config_url' => '', 819c6524dcSGreg Roach 'title' => $this->getTitle(), 829c6524dcSGreg Roach 'content' => $content, 839c6524dcSGreg Roach ]); 848c2e8227SGreg Roach } 85b2ce94c6SRico Sonntag 86b2ce94c6SRico Sonntag return $content; 878c2e8227SGreg Roach } 888c2e8227SGreg Roach 8976692c8bSGreg Roach /** 9076692c8bSGreg Roach * Should this block load asynchronously using AJAX? 9176692c8bSGreg Roach * 9276692c8bSGreg Roach * Simple blocks are faster in-line, more comples ones 9376692c8bSGreg Roach * can be loaded later. 9476692c8bSGreg Roach * 9576692c8bSGreg Roach * @return bool 9676692c8bSGreg Roach */ 97c1010edaSGreg Roach public function loadAjax(): bool 98c1010edaSGreg Roach { 998c2e8227SGreg Roach return false; 1008c2e8227SGreg Roach } 1018c2e8227SGreg Roach 10276692c8bSGreg Roach /** 10376692c8bSGreg Roach * Can this block be shown on the user’s home page? 10476692c8bSGreg Roach * 10576692c8bSGreg Roach * @return bool 10676692c8bSGreg Roach */ 107c1010edaSGreg Roach public function isUserBlock(): bool 108c1010edaSGreg Roach { 1098c2e8227SGreg Roach return false; 1108c2e8227SGreg Roach } 1118c2e8227SGreg Roach 11276692c8bSGreg Roach /** 11376692c8bSGreg Roach * Can this block be shown on the tree’s home page? 11476692c8bSGreg Roach * 11576692c8bSGreg Roach * @return bool 11676692c8bSGreg Roach */ 117c1010edaSGreg Roach public function isGedcomBlock(): bool 118c1010edaSGreg Roach { 1198c2e8227SGreg Roach return true; 1208c2e8227SGreg Roach } 1218c2e8227SGreg Roach 12276692c8bSGreg Roach /** 123a45f9889SGreg Roach * Update the configuration for a block. 124a45f9889SGreg Roach * 125a45f9889SGreg Roach * @param Request $request 126a45f9889SGreg Roach * @param int $block_id 127a45f9889SGreg Roach * 128a45f9889SGreg Roach * @return void 129a45f9889SGreg Roach */ 130a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 131a45f9889SGreg Roach { 132a45f9889SGreg Roach } 133a45f9889SGreg Roach 134a45f9889SGreg Roach /** 13576692c8bSGreg Roach * An HTML form to edit block settings 13676692c8bSGreg Roach * 137e490cd80SGreg Roach * @param Tree $tree 13876692c8bSGreg Roach * @param int $block_id 139a9430be8SGreg Roach * 140a9430be8SGreg Roach * @return void 14176692c8bSGreg Roach */ 142a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 143c1010edaSGreg Roach { 1448d68cabeSGreg Roach } 1458c2e8227SGreg Roach 1468c2e8227SGreg Roach /** 147a4439c01SGreg Roach * Get favorites for a family tree 1488c2e8227SGreg Roach * 1499807cf72SGreg Roach * @param Tree $tree 1508c2e8227SGreg Roach * 1519807cf72SGreg Roach * @return stdClass[] 1528c2e8227SGreg Roach */ 1538f53f488SRico Sonntag public function getFavorites(Tree $tree): 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 IS NULL" 158bdb3725aSGreg Roach )->execute([ 15972cf66d4SGreg Roach 'tree_id' => $tree->id(), 160bdb3725aSGreg Roach ])->fetchAll(); 1619807cf72SGreg Roach 1629807cf72SGreg Roach foreach ($favorites as $favorite) { 16325cd7ed9SGreg Roach if ($favorite->xref !== null) { 1649807cf72SGreg Roach $favorite->record = GedcomRecord::getInstance($favorite->xref, $tree); 16525cd7ed9SGreg Roach } else { 16625cd7ed9SGreg Roach $favorite->record = null; 16725cd7ed9SGreg Roach } 1689807cf72SGreg Roach } 1699807cf72SGreg Roach 1709807cf72SGreg Roach return $favorites; 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)) { 217a4439c01SGreg Roach Database::prepare( 218a4439c01SGreg Roach "DELETE FROM `##favorite` WHERE favorite_id = :favorite_id AND gedcom_id = :tree_id" 219a4439c01SGreg Roach )->execute([ 220a4439c01SGreg Roach 'favorite_id' => $favorite_id, 22172cf66d4SGreg Roach 'tree_id' => $tree->id(), 222a4439c01SGreg Roach ]); 223a4439c01SGreg Roach } 224a4439c01SGreg Roach 225aa6f03bbSGreg Roach $url = route('tree-page', ['ged' => $tree->name()]); 226a4439c01SGreg Roach 227a4439c01SGreg Roach return new RedirectResponse($url); 228a4439c01SGreg Roach } 229a4439c01SGreg Roach 230a4439c01SGreg Roach /** 231a4439c01SGreg Roach * @param Tree $tree 232a4439c01SGreg Roach * @param string $url 233a4439c01SGreg Roach * @param string $title 234a4439c01SGreg Roach * @param string $note 23518d7a90dSGreg Roach * 23618d7a90dSGreg Roach * @return void 237a4439c01SGreg Roach */ 238c1010edaSGreg Roach private function addUrlFavorite(Tree $tree, string $url, string $title, string $note) 239c1010edaSGreg Roach { 240a4439c01SGreg Roach $favorite = Database::prepare( 241a4439c01SGreg Roach "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id IS NULL AND url = :url" 242a4439c01SGreg Roach )->execute([ 24372cf66d4SGreg Roach 'gedcom_id' => $tree->id(), 244a4439c01SGreg Roach 'url' => $url, 245a4439c01SGreg Roach ])->fetchOneRow(); 246a4439c01SGreg Roach 247a4439c01SGreg Roach if ($favorite === null) { 248a4439c01SGreg Roach Database::prepare( 24925cd7ed9SGreg Roach "INSERT INTO `##favorite` (gedcom_id, favorite_type, url, note, title) VALUES (:gedcom_id, 'URL', :url, :note, :title)" 250a4439c01SGreg Roach )->execute([ 25172cf66d4SGreg Roach 'gedcom_id' => $tree->id(), 252a4439c01SGreg Roach 'url' => $url, 253a4439c01SGreg Roach 'note' => $note, 254a4439c01SGreg Roach 'title' => $title, 255a4439c01SGreg Roach ]); 256a4439c01SGreg Roach } else { 257a4439c01SGreg Roach Database::prepare( 258a4439c01SGreg Roach "UPDATE `##favorite` SET note = :note, title = :title WHERE favorite_id = :favorite_id" 259a4439c01SGreg Roach )->execute([ 260a4439c01SGreg Roach 'note' => $note, 261a4439c01SGreg Roach 'title' => $title, 262a4439c01SGreg Roach 'favorite_id' => $favorite->favorite_id, 263a4439c01SGreg Roach ]); 264a4439c01SGreg Roach } 265a4439c01SGreg Roach } 266a4439c01SGreg Roach 267a4439c01SGreg Roach /** 268a4439c01SGreg Roach * @param Tree $tree 26925cd7ed9SGreg Roach * @param GedcomRecord $record 270a4439c01SGreg Roach * @param string $note 27118d7a90dSGreg Roach * 27218d7a90dSGreg Roach * @return void 273a4439c01SGreg Roach */ 27425cd7ed9SGreg Roach private function addRecordFavorite(Tree $tree, GedcomRecord $record, string $note) 275c1010edaSGreg Roach { 276a4439c01SGreg Roach $favorite = Database::prepare( 277a4439c01SGreg Roach "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id IS NULL AND xref = :xref" 278a4439c01SGreg Roach )->execute([ 27972cf66d4SGreg Roach 'gedcom_id' => $tree->id(), 28025cd7ed9SGreg Roach 'xref' => $record->xref(), 281a4439c01SGreg Roach ])->fetchOneRow(); 282a4439c01SGreg Roach 283a4439c01SGreg Roach if ($favorite === null) { 284a4439c01SGreg Roach Database::prepare( 28525cd7ed9SGreg Roach "INSERT INTO `##favorite` (gedcom_id, favorite_type, xref, note) VALUES (:gedcom_id, :favorite_type, :xref, :note)" 286a4439c01SGreg Roach )->execute([ 28772cf66d4SGreg Roach 'gedcom_id' => $tree->id(), 28825cd7ed9SGreg Roach 'favorite_type' => $record::RECORD_TYPE, 28925cd7ed9SGreg Roach 'xref' => $record->xref(), 290a4439c01SGreg Roach 'note' => $note, 291a4439c01SGreg Roach ]); 292a4439c01SGreg Roach } else { 293a4439c01SGreg Roach Database::prepare( 294a4439c01SGreg Roach "UPDATE `##favorite` SET note = :note WHERE favorite_id = :favorite_id" 295a4439c01SGreg Roach )->execute([ 296a4439c01SGreg Roach 'note' => $note, 297a4439c01SGreg Roach 'favorite_id' => $favorite->favorite_id, 298a4439c01SGreg Roach ]); 299a4439c01SGreg Roach } 300a4439c01SGreg Roach } 3018c2e8227SGreg Roach} 302