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; 249807cf72SGreg Roachuse stdClass; 25a4439c01SGreg Roachuse Symfony\Component\HttpFoundation\RedirectResponse; 26a4439c01SGreg Roachuse Symfony\Component\HttpFoundation\Request; 278c2e8227SGreg Roach 288c2e8227SGreg Roach/** 298c2e8227SGreg Roach * Class FamilyTreeFavoritesModule 308c2e8227SGreg Roach */ 31c1010edaSGreg Roachclass FamilyTreeFavoritesModule extends AbstractModule implements ModuleBlockInterface 32c1010edaSGreg Roach{ 3376692c8bSGreg Roach /** 3476692c8bSGreg Roach * How should this module be labelled on tabs, menus, etc.? 3576692c8bSGreg Roach * 3676692c8bSGreg Roach * @return string 3776692c8bSGreg Roach */ 38c1010edaSGreg Roach public function getTitle() 39c1010edaSGreg Roach { 40bbb76c12SGreg Roach /* I18N: Name of a module */ 41bbb76c12SGreg Roach return I18N::translate('Favorites'); 428c2e8227SGreg Roach } 438c2e8227SGreg Roach 4476692c8bSGreg Roach /** 4576692c8bSGreg Roach * A sentence describing what this module does. 4676692c8bSGreg Roach * 4776692c8bSGreg Roach * @return string 4876692c8bSGreg 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 family tree’s favorite pages.'); 538c2e8227SGreg Roach } 548c2e8227SGreg Roach 5576692c8bSGreg Roach /** 5676692c8bSGreg Roach * Generate the HTML content of this block. 5776692c8bSGreg Roach * 58e490cd80SGreg Roach * @param Tree $tree 5976692c8bSGreg Roach * @param int $block_id 6076692c8bSGreg Roach * @param bool $template 61727f238cSGreg Roach * @param string[] $cfg 6276692c8bSGreg Roach * 6376692c8bSGreg Roach * @return string 6476692c8bSGreg Roach */ 65c1010edaSGreg Roach public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 66c1010edaSGreg Roach { 67a4439c01SGreg Roach $content = view('modules/gedcom_favorites/favorites', [ 68a4439c01SGreg Roach 'block_id' => $block_id, 69a4439c01SGreg Roach 'favorites' => $this->getFavorites($tree), 70e490cd80SGreg Roach 'is_manager' => Auth::isManager($tree), 71e490cd80SGreg Roach 'tree' => $tree, 729807cf72SGreg Roach ]); 739807cf72SGreg Roach 748c2e8227SGreg Roach if ($template) { 75147e99aaSGreg Roach return view('modules/block-template', [ 769c6524dcSGreg Roach 'block' => str_replace('_', '-', $this->getName()), 779c6524dcSGreg Roach 'id' => $block_id, 789c6524dcSGreg Roach 'config_url' => '', 799c6524dcSGreg Roach 'title' => $this->getTitle(), 809c6524dcSGreg Roach 'content' => $content, 819c6524dcSGreg Roach ]); 828c2e8227SGreg Roach } else { 838c2e8227SGreg Roach return $content; 848c2e8227SGreg Roach } 858c2e8227SGreg Roach } 868c2e8227SGreg Roach 8776692c8bSGreg Roach /** 8876692c8bSGreg Roach * Should this block load asynchronously using AJAX? 8976692c8bSGreg Roach * 9076692c8bSGreg Roach * Simple blocks are faster in-line, more comples ones 9176692c8bSGreg Roach * can be loaded later. 9276692c8bSGreg Roach * 9376692c8bSGreg Roach * @return bool 9476692c8bSGreg Roach */ 95c1010edaSGreg Roach public function loadAjax(): bool 96c1010edaSGreg Roach { 978c2e8227SGreg Roach return false; 988c2e8227SGreg Roach } 998c2e8227SGreg Roach 10076692c8bSGreg Roach /** 10176692c8bSGreg Roach * Can this block be shown on the user’s home page? 10276692c8bSGreg Roach * 10376692c8bSGreg Roach * @return bool 10476692c8bSGreg Roach */ 105c1010edaSGreg Roach public function isUserBlock(): bool 106c1010edaSGreg Roach { 1078c2e8227SGreg Roach return false; 1088c2e8227SGreg Roach } 1098c2e8227SGreg Roach 11076692c8bSGreg Roach /** 11176692c8bSGreg Roach * Can this block be shown on the tree’s home page? 11276692c8bSGreg Roach * 11376692c8bSGreg Roach * @return bool 11476692c8bSGreg Roach */ 115c1010edaSGreg Roach public function isGedcomBlock(): bool 116c1010edaSGreg Roach { 1178c2e8227SGreg Roach return true; 1188c2e8227SGreg Roach } 1198c2e8227SGreg Roach 12076692c8bSGreg Roach /** 121*a45f9889SGreg Roach * Update the configuration for a block. 122*a45f9889SGreg Roach * 123*a45f9889SGreg Roach * @param Request $request 124*a45f9889SGreg Roach * @param int $block_id 125*a45f9889SGreg Roach * 126*a45f9889SGreg Roach * @return void 127*a45f9889SGreg Roach */ 128*a45f9889SGreg Roach public function saveBlockConfiguration(Request $request, int $block_id) 129*a45f9889SGreg Roach { 130*a45f9889SGreg Roach } 131*a45f9889SGreg Roach 132*a45f9889SGreg Roach /** 13376692c8bSGreg Roach * An HTML form to edit block settings 13476692c8bSGreg Roach * 135e490cd80SGreg Roach * @param Tree $tree 13676692c8bSGreg Roach * @param int $block_id 137a9430be8SGreg Roach * 138a9430be8SGreg Roach * @return void 13976692c8bSGreg Roach */ 140*a45f9889SGreg Roach public function editBlockConfiguration(Tree $tree, int $block_id) 141c1010edaSGreg Roach { 1428d68cabeSGreg Roach } 1438c2e8227SGreg Roach 1448c2e8227SGreg Roach /** 145a4439c01SGreg Roach * Get favorites for a family tree 1468c2e8227SGreg Roach * 1479807cf72SGreg Roach * @param Tree $tree 1488c2e8227SGreg Roach * 1499807cf72SGreg Roach * @return stdClass[] 1508c2e8227SGreg Roach */ 151c1010edaSGreg Roach public function getFavorites(Tree $tree) 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 IS NULL" 156bdb3725aSGreg Roach )->execute([ 1579807cf72SGreg Roach 'tree_id' => $tree->getTreeId(), 158bdb3725aSGreg Roach ])->fetchAll(); 1599807cf72SGreg Roach 1609807cf72SGreg Roach foreach ($favorites as $favorite) { 1619807cf72SGreg Roach $favorite->record = GedcomRecord::getInstance($favorite->xref, $tree); 1629807cf72SGreg Roach } 1639807cf72SGreg Roach 1649807cf72SGreg Roach return $favorites; 1658c2e8227SGreg Roach } 166a4439c01SGreg Roach 167a4439c01SGreg Roach /** 168a4439c01SGreg Roach * @param Request $request 169b6db7c1fSGreg Roach * @param Tree $tree 170b6db7c1fSGreg Roach * @param User $user 171a4439c01SGreg Roach * 172a4439c01SGreg Roach * @return RedirectResponse 173a4439c01SGreg Roach */ 174b6db7c1fSGreg Roach public function postAddFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse 175c1010edaSGreg Roach { 176a4439c01SGreg Roach $note = $request->get('note', ''); 177a4439c01SGreg Roach $title = $request->get('title', ''); 178a4439c01SGreg Roach $url = $request->get('url', ''); 179a4439c01SGreg Roach $xref = $request->get('xref', ''); 180a4439c01SGreg Roach 181a4439c01SGreg Roach if (Auth::isManager($tree, $user)) { 182a4439c01SGreg Roach if ($url !== '') { 183a4439c01SGreg Roach $this->addUrlFavorite($tree, $url, $title ?: $url, $note); 184a4439c01SGreg Roach } else { 185a4439c01SGreg Roach $this->addRecordFavorite($tree, $xref, $note); 186a4439c01SGreg Roach } 187a4439c01SGreg Roach } 188a4439c01SGreg Roach 189a4439c01SGreg Roach $url = route('tree-page', ['ged' => $tree->getName()]); 190a4439c01SGreg Roach 191a4439c01SGreg Roach return new RedirectResponse($url); 192a4439c01SGreg Roach } 193a4439c01SGreg Roach 194a4439c01SGreg Roach /** 195a4439c01SGreg Roach * @param Request $request 196b6db7c1fSGreg Roach * @param Tree $tree 197b6db7c1fSGreg Roach * @param User $user 198a4439c01SGreg Roach * 199a4439c01SGreg Roach * @return RedirectResponse 200a4439c01SGreg Roach */ 201b6db7c1fSGreg Roach public function postDeleteFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse 202c1010edaSGreg Roach { 203a4439c01SGreg Roach $favorite_id = (int)$request->get('favorite_id'); 204a4439c01SGreg Roach 205a4439c01SGreg Roach if (Auth::isManager($tree, $user)) { 206a4439c01SGreg Roach Database::prepare( 207a4439c01SGreg Roach "DELETE FROM `##favorite` WHERE favorite_id = :favorite_id AND gedcom_id = :tree_id" 208a4439c01SGreg Roach )->execute([ 209a4439c01SGreg Roach 'favorite_id' => $favorite_id, 210a4439c01SGreg Roach 'tree_id' => $tree->getTreeId(), 211a4439c01SGreg Roach ]); 212a4439c01SGreg Roach } 213a4439c01SGreg Roach 214a4439c01SGreg Roach $url = route('tree-page', ['ged' => $tree->getName()]); 215a4439c01SGreg Roach 216a4439c01SGreg Roach return new RedirectResponse($url); 217a4439c01SGreg Roach } 218a4439c01SGreg Roach 219a4439c01SGreg Roach /** 220a4439c01SGreg Roach * @param Tree $tree 221a4439c01SGreg Roach * @param string $url 222a4439c01SGreg Roach * @param string $title 223a4439c01SGreg Roach * @param string $note 224a4439c01SGreg Roach */ 225c1010edaSGreg Roach private function addUrlFavorite(Tree $tree, string $url, string $title, string $note) 226c1010edaSGreg Roach { 227a4439c01SGreg Roach $favorite = Database::prepare( 228a4439c01SGreg Roach "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id IS NULL AND url = :url" 229a4439c01SGreg Roach )->execute([ 230a4439c01SGreg Roach 'gedcom_id' => $tree->getTreeId(), 231a4439c01SGreg Roach 'url' => $url, 232a4439c01SGreg Roach ])->fetchOneRow(); 233a4439c01SGreg Roach 234a4439c01SGreg Roach if ($favorite === null) { 235a4439c01SGreg Roach Database::prepare( 236a4439c01SGreg Roach "INSERT INTO `##favorite` (gedcom_id, url, note, title) VALUES (:gedcom_id, :user_id, :url, :note, :title)" 237a4439c01SGreg Roach )->execute([ 238a4439c01SGreg Roach 'gedcom_id' => $tree->getTreeId(), 239a4439c01SGreg Roach 'url' => $url, 240a4439c01SGreg Roach 'note' => $note, 241a4439c01SGreg Roach 'title' => $title, 242a4439c01SGreg Roach ]); 243a4439c01SGreg Roach } else { 244a4439c01SGreg Roach Database::prepare( 245a4439c01SGreg Roach "UPDATE `##favorite` SET note = :note, title = :title WHERE favorite_id = :favorite_id" 246a4439c01SGreg Roach )->execute([ 247a4439c01SGreg Roach 'note' => $note, 248a4439c01SGreg Roach 'title' => $title, 249a4439c01SGreg Roach 'favorite_id' => $favorite->favorite_id, 250a4439c01SGreg Roach ]); 251a4439c01SGreg Roach } 252a4439c01SGreg Roach } 253a4439c01SGreg Roach 254a4439c01SGreg Roach /** 255a4439c01SGreg Roach * @param Tree $tree 256a4439c01SGreg Roach * @param string $xref 257a4439c01SGreg Roach * @param string $note 258a4439c01SGreg Roach */ 259c1010edaSGreg Roach private function addRecordFavorite(Tree $tree, string $xref, string $note) 260c1010edaSGreg Roach { 261a4439c01SGreg Roach $favorite = Database::prepare( 262a4439c01SGreg Roach "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id IS NULL AND xref = :xref" 263a4439c01SGreg Roach )->execute([ 264a4439c01SGreg Roach 'gedcom_id' => $tree->getTreeId(), 265a4439c01SGreg Roach 'xref' => $xref, 266a4439c01SGreg Roach ])->fetchOneRow(); 267a4439c01SGreg Roach 268a4439c01SGreg Roach if ($favorite === null) { 269a4439c01SGreg Roach Database::prepare( 270a4439c01SGreg Roach "INSERT INTO `##favorite` (gedcom_id, xref, note) VALUES (:gedcom_id, :xref, :note)" 271a4439c01SGreg Roach )->execute([ 272a4439c01SGreg Roach 'gedcom_id' => $tree->getTreeId(), 273a4439c01SGreg Roach 'xref' => $xref, 274a4439c01SGreg Roach 'note' => $note, 275a4439c01SGreg Roach ]); 276a4439c01SGreg Roach } else { 277a4439c01SGreg Roach Database::prepare( 278a4439c01SGreg Roach "UPDATE `##favorite` SET note = :note WHERE favorite_id = :favorite_id" 279a4439c01SGreg Roach )->execute([ 280a4439c01SGreg Roach 'note' => $note, 281a4439c01SGreg Roach 'favorite_id' => $favorite->favorite_id, 282a4439c01SGreg Roach ]); 283a4439c01SGreg Roach } 284a4439c01SGreg Roach } 2858c2e8227SGreg Roach} 286