1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16namespace Fisharebest\Webtrees\Module; 17 18use Fisharebest\Webtrees\Auth; 19use Fisharebest\Webtrees\Database; 20use Fisharebest\Webtrees\GedcomRecord; 21use Fisharebest\Webtrees\I18N; 22use Fisharebest\Webtrees\Tree; 23use Fisharebest\Webtrees\User; 24use stdClass; 25use Symfony\Component\HttpFoundation\RedirectResponse; 26use Symfony\Component\HttpFoundation\Request; 27 28/** 29 * Class FamilyTreeFavoritesModule 30 */ 31class FamilyTreeFavoritesModule extends AbstractModule implements ModuleBlockInterface 32{ 33 /** 34 * How should this module be labelled on tabs, menus, etc.? 35 * 36 * @return string 37 */ 38 public function getTitle() 39 { 40 return /* I18N: Name of a module */ 41 I18N::translate('Favorites'); 42 } 43 44 /** 45 * A sentence describing what this module does. 46 * 47 * @return string 48 */ 49 public function getDescription() 50 { 51 return /* I18N: Description of the “Favorites” module */ 52 I18N::translate('Display and manage a family tree’s favorite pages.'); 53 } 54 55 /** 56 * Generate the HTML content of this block. 57 * 58 * @param Tree $tree 59 * @param int $block_id 60 * @param bool $template 61 * @param string[] $cfg 62 * 63 * @return string 64 */ 65 public function getBlock(Tree $tree, int $block_id, bool $template = true, array $cfg = []): string 66 { 67 $content = view('modules/gedcom_favorites/favorites', [ 68 'block_id' => $block_id, 69 'favorites' => $this->getFavorites($tree), 70 'is_manager' => Auth::isManager($tree), 71 'tree' => $tree, 72 ]); 73 74 if ($template) { 75 return view('modules/block-template', [ 76 'block' => str_replace('_', '-', $this->getName()), 77 'id' => $block_id, 78 'config_url' => '', 79 'title' => $this->getTitle(), 80 'content' => $content, 81 ]); 82 } else { 83 return $content; 84 } 85 } 86 87 /** 88 * Should this block load asynchronously using AJAX? 89 * 90 * Simple blocks are faster in-line, more comples ones 91 * can be loaded later. 92 * 93 * @return bool 94 */ 95 public function loadAjax(): bool 96 { 97 return false; 98 } 99 100 /** 101 * Can this block be shown on the user’s home page? 102 * 103 * @return bool 104 */ 105 public function isUserBlock(): bool 106 { 107 return false; 108 } 109 110 /** 111 * Can this block be shown on the tree’s home page? 112 * 113 * @return bool 114 */ 115 public function isGedcomBlock(): bool 116 { 117 return true; 118 } 119 120 /** 121 * An HTML form to edit block settings 122 * 123 * @param Tree $tree 124 * @param int $block_id 125 * 126 * @return void 127 */ 128 public function configureBlock(Tree $tree, int $block_id) 129 { 130 } 131 132 /** 133 * Get favorites for a family tree 134 * 135 * @param Tree $tree 136 * 137 * @return stdClass[] 138 */ 139 public function getFavorites(Tree $tree) 140 { 141 $favorites = 142 Database::prepare( 143 "SELECT favorite_id, user_id, gedcom_id, xref, favorite_type, title, note, url" . 144 " FROM `##favorite` WHERE gedcom_id = :tree_id AND user_id IS NULL") 145 ->execute([ 146 'tree_id' => $tree->getTreeId(), 147 ]) 148 ->fetchAll(); 149 150 foreach ($favorites as $favorite) { 151 $favorite->record = GedcomRecord::getInstance($favorite->xref, $tree); 152 } 153 154 return $favorites; 155 } 156 157 /** 158 * @param Request $request 159 * 160 * @return RedirectResponse 161 */ 162 public function postAddFavoriteAction(Request $request): RedirectResponse 163 { 164 /** @var Tree $tree */ 165 $tree = $request->attributes->get('tree'); 166 167 /** @var User $user */ 168 $user = $request->attributes->get('user'); 169 170 $note = $request->get('note', ''); 171 $title = $request->get('title', ''); 172 $url = $request->get('url', ''); 173 $xref = $request->get('xref', ''); 174 175 if (Auth::isManager($tree, $user)) { 176 if ($url !== '') { 177 $this->addUrlFavorite($tree, $url, $title ?: $url, $note); 178 } else { 179 $this->addRecordFavorite($tree, $xref, $note); 180 } 181 } 182 183 $url = route('tree-page', ['ged' => $tree->getName()]); 184 185 return new RedirectResponse($url); 186 } 187 188 /** 189 * @param Request $request 190 * 191 * @return RedirectResponse 192 */ 193 public function postDeleteFavoriteAction(Request $request): RedirectResponse 194 { 195 /** @var Tree $tree */ 196 $tree = $request->attributes->get('tree'); 197 198 /** @var User $user */ 199 $user = $request->attributes->get('user'); 200 201 $favorite_id = (int)$request->get('favorite_id'); 202 203 if (Auth::isManager($tree, $user)) { 204 Database::prepare( 205 "DELETE FROM `##favorite` WHERE favorite_id = :favorite_id AND gedcom_id = :tree_id" 206 )->execute([ 207 'favorite_id' => $favorite_id, 208 'tree_id' => $tree->getTreeId(), 209 ]); 210 } 211 212 $url = route('tree-page', ['ged' => $tree->getName()]); 213 214 return new RedirectResponse($url); 215 } 216 217 /** 218 * @param Tree $tree 219 * @param string $url 220 * @param string $title 221 * @param string $note 222 */ 223 private function addUrlFavorite(Tree $tree, string $url, string $title, string $note) 224 { 225 $favorite = Database::prepare( 226 "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id IS NULL AND url = :url" 227 )->execute([ 228 'gedcom_id' => $tree->getTreeId(), 229 'url' => $url, 230 ])->fetchOneRow(); 231 232 if ($favorite === null) { 233 Database::prepare( 234 "INSERT INTO `##favorite` (gedcom_id, url, note, title) VALUES (:gedcom_id, :user_id, :url, :note, :title)" 235 )->execute([ 236 'gedcom_id' => $tree->getTreeId(), 237 'url' => $url, 238 'note' => $note, 239 'title' => $title, 240 ]); 241 } else { 242 Database::prepare( 243 "UPDATE `##favorite` SET note = :note, title = :title WHERE favorite_id = :favorite_id" 244 )->execute([ 245 'note' => $note, 246 'title' => $title, 247 'favorite_id' => $favorite->favorite_id, 248 ]); 249 } 250 } 251 252 /** 253 * @param Tree $tree 254 * @param string $xref 255 * @param string $note 256 */ 257 private function addRecordFavorite(Tree $tree, string $xref, string $note) 258 { 259 $favorite = Database::prepare( 260 "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id IS NULL AND xref = :xref" 261 )->execute([ 262 'gedcom_id' => $tree->getTreeId(), 263 'xref' => $xref, 264 ])->fetchOneRow(); 265 266 if ($favorite === null) { 267 Database::prepare( 268 "INSERT INTO `##favorite` (gedcom_id, xref, note) VALUES (:gedcom_id, :xref, :note)" 269 )->execute([ 270 'gedcom_id' => $tree->getTreeId(), 271 'xref' => $xref, 272 'note' => $note, 273 ]); 274 } else { 275 Database::prepare( 276 "UPDATE `##favorite` SET note = :note WHERE favorite_id = :favorite_id" 277 )->execute([ 278 'note' => $note, 279 'favorite_id' => $favorite->favorite_id, 280 ]); 281 } 282 } 283} 284