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(): string 39 { 40 /* I18N: Name of a module */ 41 return I18N::translate('Favorites'); 42 } 43 44 /** 45 * A sentence describing what this module does. 46 * 47 * @return string 48 */ 49 public function getDescription(): string 50 { 51 /* I18N: Description of the “Favorites” module */ 52 return 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 } 82 83 return $content; 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 * Update the configuration for a block. 121 * 122 * @param Request $request 123 * @param int $block_id 124 * 125 * @return void 126 */ 127 public function saveBlockConfiguration(Request $request, int $block_id) 128 { 129 } 130 131 /** 132 * An HTML form to edit block settings 133 * 134 * @param Tree $tree 135 * @param int $block_id 136 * 137 * @return void 138 */ 139 public function editBlockConfiguration(Tree $tree, int $block_id) 140 { 141 } 142 143 /** 144 * Get the favorites for a user 145 * 146 * @param Tree $tree 147 * @param User $user 148 * 149 * @return stdClass[] 150 */ 151 public function getFavorites(Tree $tree, User $user): array 152 { 153 $favorites = Database::prepare( 154 "SELECT favorite_id, user_id, gedcom_id, xref, favorite_type, title, note, url" . 155 " FROM `##favorite` WHERE gedcom_id = :tree_id AND user_id = :user_id" 156 )->execute([ 157 'tree_id' => $tree->getTreeId(), 158 'user_id' => $user->getUserId(), 159 ])->fetchAll(); 160 161 foreach ($favorites as $favorite) { 162 $favorite->record = GedcomRecord::getInstance($favorite->xref, $tree); 163 } 164 165 return $favorites; 166 } 167 168 /** 169 * @param Request $request 170 * @param Tree $tree 171 * @param User $user 172 * 173 * @return RedirectResponse 174 */ 175 public function postAddFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse 176 { 177 $note = $request->get('note', ''); 178 $title = $request->get('title', ''); 179 $url = $request->get('url', ''); 180 $xref = $request->get('xref', ''); 181 182 if (Auth::check()) { 183 if ($url !== '') { 184 $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note); 185 } else { 186 $this->addRecordFavorite($tree, $user, $xref, $note); 187 } 188 } 189 190 $url = route('user-page', ['ged' => $tree->getName()]); 191 192 return new RedirectResponse($url); 193 } 194 195 /** 196 * @param Request $request 197 * @param Tree $tree 198 * @param User $user 199 * 200 * @return RedirectResponse 201 */ 202 public function postDeleteFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse 203 { 204 $favorite_id = (int) $request->get('favorite_id'); 205 206 if (Auth::check()) { 207 Database::prepare( 208 "DELETE FROM `##favorite` WHERE favorite_id = :favorite_id AND user_id = :user_id" 209 )->execute([ 210 'favorite_id' => $favorite_id, 211 'user_id' => $user->getUserId(), 212 ]); 213 } 214 215 $url = route('user-page', ['ged' => $tree->getName()]); 216 217 return new RedirectResponse($url); 218 } 219 220 /** 221 * @param Tree $tree 222 * @param User $user 223 * @param string $url 224 * @param string $title 225 * @param string $note 226 * 227 * @return void 228 */ 229 private function addUrlFavorite(Tree $tree, User $user, string $url, string $title, string $note) 230 { 231 $favorite = Database::prepare( 232 "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND url = :url" 233 )->execute([ 234 'gedcom_id' => $tree->getTreeId(), 235 'user_id' => $user->getUserId(), 236 'url' => $url, 237 ])->fetchOneRow(); 238 239 if ($favorite === null) { 240 Database::prepare( 241 "INSERT INTO `##favorite` (gedcom_id, user_id, url, note, title) VALUES (:gedcom_id, :user_id, :url, :note, :title)" 242 )->execute([ 243 'gedcom_id' => $tree->getTreeId(), 244 'user_id' => $user->getUserId(), 245 'url' => $url, 246 'note' => $note, 247 'title' => $title, 248 ]); 249 } else { 250 Database::prepare( 251 "UPDATE `##favorite` SET note = :note, title = :title WHERE favorite_id = :favorite_id" 252 )->execute([ 253 'note' => $note, 254 'title' => $title, 255 'favorite_id' => $favorite->favorite_id, 256 ]); 257 } 258 } 259 260 /** 261 * @param Tree $tree 262 * @param User $user 263 * @param string $xref 264 * @param string $note 265 * 266 * @return void 267 */ 268 private function addRecordFavorite(Tree $tree, User $user, string $xref, string $note) 269 { 270 $favorite = Database::prepare( 271 "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND xref = :xref" 272 )->execute([ 273 'gedcom_id' => $tree->getTreeId(), 274 'user_id' => $user->getUserId(), 275 'xref' => $xref, 276 ])->fetchOneRow(); 277 278 if ($favorite === null) { 279 Database::prepare( 280 "INSERT INTO `##favorite` (gedcom_id, user_id, xref, note) VALUES (:gedcom_id, :user_id, :xref, :note)" 281 )->execute([ 282 'gedcom_id' => $tree->getTreeId(), 283 'user_id' => $user->getUserId(), 284 'xref' => $xref, 285 'note' => $note, 286 ]); 287 } else { 288 Database::prepare( 289 "UPDATE `##favorite` SET note = :note WHERE favorite_id = :favorite_id" 290 )->execute([ 291 'note' => $note, 292 'favorite_id' => $favorite->favorite_id, 293 ]); 294 } 295 } 296} 297