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 UserFavoritesModule 30 */ 31class UserFavoritesModule 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 user’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/user_favorites/favorites', [ 68 'block_id' => $block_id, 69 'favorites' => $this->getFavorites($tree, Auth::user()), 70 'tree' => $tree, 71 ]); 72 73 if ($template) { 74 return view('modules/block-template', [ 75 'block' => str_replace('_', '-', $this->getName()), 76 'id' => $block_id, 77 'config_url' => '', 78 'title' => $this->getTitle(), 79 'content' => $content, 80 ]); 81 } else { 82 return $content; 83 } 84 } 85 86 /** 87 * Should this block load asynchronously using AJAX? 88 * 89 * Simple blocks are faster in-line, more comples ones 90 * can be loaded later. 91 * 92 * @return bool 93 */ 94 public function loadAjax(): bool 95 { 96 return false; 97 } 98 99 /** 100 * Can this block be shown on the user’s home page? 101 * 102 * @return bool 103 */ 104 public function isUserBlock(): bool 105 { 106 return true; 107 } 108 109 /** 110 * Can this block be shown on the tree’s home page? 111 * 112 * @return bool 113 */ 114 public function isGedcomBlock(): bool 115 { 116 return false; 117 } 118 119 /** 120 * An HTML form to edit block settings 121 * 122 * @param Tree $tree 123 * @param int $block_id 124 * 125 * @return void 126 */ 127 public function configureBlock(Tree $tree, int $block_id) 128 { 129 } 130 131 /** 132 * Get the favorites for a user 133 * 134 * @param Tree $tree 135 * @param User $user 136 * 137 * @return stdClass[] 138 */ 139 public function getFavorites(Tree $tree, User $user) 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 = :user_id") 145 ->execute([ 146 'tree_id' => $tree->getTreeId(), 147 'user_id' => $user->getUserId(), 148 ]) 149 ->fetchAll(); 150 151 foreach ($favorites as $favorite) { 152 $favorite->record = GedcomRecord::getInstance($favorite->xref, $tree); 153 } 154 155 return $favorites; 156 } 157 158 /** 159 * @param Request $request 160 * 161 * @return RedirectResponse 162 */ 163 public function postAddFavoriteAction(Request $request): RedirectResponse 164 { 165 /** @var Tree $tree */ 166 $tree = $request->attributes->get('tree'); 167 168 /** @var User $user */ 169 $user = $request->attributes->get('user'); 170 171 $note = $request->get('note', ''); 172 $title = $request->get('title', ''); 173 $url = $request->get('url', ''); 174 $xref = $request->get('xref', ''); 175 176 if (Auth::check()) { 177 if ($url !== '') { 178 $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note); 179 } else { 180 $this->addRecordFavorite($tree, $user, $xref, $note); 181 } 182 } 183 184 $url = route('user-page', ['ged' => $tree->getName()]); 185 186 return new RedirectResponse($url); 187 } 188 189 /** 190 * @param Request $request 191 * 192 * @return RedirectResponse 193 */ 194 public function postDeleteFavoriteAction(Request $request): RedirectResponse 195 { 196 /** @var Tree $tree */ 197 $tree = $request->attributes->get('tree'); 198 199 /** @var User $user */ 200 $user = $request->attributes->get('user'); 201 202 $favorite_id = (int)$request->get('favorite_id'); 203 204 if (Auth::check()) { 205 Database::prepare( 206 "DELETE FROM `##favorite` WHERE favorite_id = :favorite_id AND user_id = :user_id" 207 )->execute([ 208 'favorite_id' => $favorite_id, 209 'user_id' => $user->getUserId(), 210 ]); 211 } 212 213 $url = route('user-page', ['ged' => $tree->getName()]); 214 215 return new RedirectResponse($url); 216 } 217 218 /** 219 * @param Tree $tree 220 * @param User $user 221 * @param string $url 222 * @param string $title 223 * @param string $note 224 */ 225 private function addUrlFavorite(Tree $tree, User $user, string $url, string $title, string $note) 226 { 227 $favorite = Database::prepare( 228 "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND url = :url" 229 )->execute([ 230 'gedcom_id' => $tree->getTreeId(), 231 'user_id' => $user->getUserId(), 232 'url' => $url, 233 ])->fetchOneRow(); 234 235 if ($favorite === null) { 236 Database::prepare( 237 "INSERT INTO `##favorite` (gedcom_id, user_id, url, note, title) VALUES (:gedcom_id, :user_id, :url, :note, :title)" 238 )->execute([ 239 'gedcom_id' => $tree->getTreeId(), 240 'user_id' => $user->getUserId(), 241 'url' => $url, 242 'note' => $note, 243 'title' => $title, 244 ]); 245 } else { 246 Database::prepare( 247 "UPDATE `##favorite` SET note = :note, title = :title WHERE favorite_id = :favorite_id" 248 )->execute([ 249 'note' => $note, 250 'title' => $title, 251 'favorite_id' => $favorite->favorite_id, 252 ]); 253 } 254 } 255 256 /** 257 * @param Tree $tree 258 * @param User $user 259 * @param string $xref 260 * @param string $note 261 */ 262 private function addRecordFavorite(Tree $tree, User $user, string $xref, string $note) 263 { 264 $favorite = Database::prepare( 265 "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND xref = :xref" 266 )->execute([ 267 'gedcom_id' => $tree->getTreeId(), 268 'user_id' => $user->getUserId(), 269 'xref' => $xref, 270 ])->fetchOneRow(); 271 272 if ($favorite === null) { 273 Database::prepare( 274 "INSERT INTO `##favorite` (gedcom_id, user_id, xref, note) VALUES (:gedcom_id, :user_id, :xref, :note)" 275 )->execute([ 276 'gedcom_id' => $tree->getTreeId(), 277 'user_id' => $user->getUserId(), 278 'xref' => $xref, 279 'note' => $note, 280 ]); 281 } else { 282 Database::prepare( 283 "UPDATE `##favorite` SET note = :note WHERE favorite_id = :favorite_id" 284 )->execute([ 285 'note' => $note, 286 'favorite_id' => $favorite->favorite_id, 287 ]); 288 } 289 } 290} 291