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 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Module; 19 20use Fisharebest\Webtrees\Auth; 21use Fisharebest\Webtrees\Database; 22use Fisharebest\Webtrees\GedcomRecord; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Tree; 25use Fisharebest\Webtrees\User; 26use stdClass; 27use Symfony\Component\HttpFoundation\RedirectResponse; 28use Symfony\Component\HttpFoundation\Request; 29 30/** 31 * Class UserFavoritesModule 32 */ 33class UserFavoritesModule extends AbstractModule implements ModuleBlockInterface 34{ 35 /** 36 * How should this module be labelled on tabs, menus, etc.? 37 * 38 * @return string 39 */ 40 public function getTitle(): string 41 { 42 /* I18N: Name of a module */ 43 return I18N::translate('Favorites'); 44 } 45 46 /** 47 * A sentence describing what this module does. 48 * 49 * @return string 50 */ 51 public function getDescription(): string 52 { 53 /* I18N: Description of the “Favorites” module */ 54 return I18N::translate('Display and manage a user’s favorite pages.'); 55 } 56 57 /** 58 * Generate the HTML content of this block. 59 * 60 * @param Tree $tree 61 * @param int $block_id 62 * @param string $ctype 63 * @param string[] $cfg 64 * 65 * @return string 66 */ 67 public function getBlock(Tree $tree, int $block_id, string $ctype = '', array $cfg = []): string 68 { 69 $content = view('modules/user_favorites/favorites', [ 70 'block_id' => $block_id, 71 'favorites' => $this->getFavorites($tree, Auth::user()), 72 'tree' => $tree, 73 ]); 74 75 if ($ctype !== '') { 76 return view('modules/block-template', [ 77 'block' => str_replace('_', '-', $this->getName()), 78 'id' => $block_id, 79 'config_url' => '', 80 'title' => $this->getTitle(), 81 'content' => $content, 82 ]); 83 } 84 85 return $content; 86 } 87 88 /** 89 * Should this block load asynchronously using AJAX? 90 * 91 * Simple blocks are faster in-line, more comples ones 92 * can be loaded later. 93 * 94 * @return bool 95 */ 96 public function loadAjax(): bool 97 { 98 return false; 99 } 100 101 /** 102 * Can this block be shown on the user’s home page? 103 * 104 * @return bool 105 */ 106 public function isUserBlock(): bool 107 { 108 return true; 109 } 110 111 /** 112 * Can this block be shown on the tree’s home page? 113 * 114 * @return bool 115 */ 116 public function isGedcomBlock(): bool 117 { 118 return false; 119 } 120 121 /** 122 * Update the configuration for a block. 123 * 124 * @param Request $request 125 * @param int $block_id 126 * 127 * @return void 128 */ 129 public function saveBlockConfiguration(Request $request, int $block_id) 130 { 131 } 132 133 /** 134 * An HTML form to edit block settings 135 * 136 * @param Tree $tree 137 * @param int $block_id 138 * 139 * @return void 140 */ 141 public function editBlockConfiguration(Tree $tree, int $block_id) 142 { 143 } 144 145 /** 146 * Get the favorites for a user 147 * 148 * @param Tree $tree 149 * @param User $user 150 * 151 * @return stdClass[] 152 */ 153 public function getFavorites(Tree $tree, User $user): array 154 { 155 $favorites = Database::prepare( 156 "SELECT favorite_id, user_id, gedcom_id, xref, favorite_type, title, note, url" . 157 " FROM `##favorite` WHERE gedcom_id = :tree_id AND user_id = :user_id" 158 )->execute([ 159 'tree_id' => $tree->id(), 160 'user_id' => $user->getUserId(), 161 ])->fetchAll(); 162 163 foreach ($favorites as $favorite) { 164 if ($favorite->xref !== null) { 165 $favorite->record = GedcomRecord::getInstance($favorite->xref, $tree); 166 } else { 167 $favorite->record = null; 168 } 169 } 170 171 return $favorites; 172 } 173 174 /** 175 * @param Request $request 176 * @param Tree $tree 177 * @param User $user 178 * 179 * @return RedirectResponse 180 */ 181 public function postAddFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse 182 { 183 $note = $request->get('note', ''); 184 $title = $request->get('title', ''); 185 $url = $request->get('url', ''); 186 $xref = $request->get('xref', ''); 187 $fav_category = $request->get('fav_category', ''); 188 189 $record = GedcomRecord::getInstance($xref, $tree); 190 191 if (Auth::check()) { 192 if ($fav_category === 'url' && $url !== '') { 193 $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note); 194 } 195 196 if ($fav_category === 'record' && $record instanceof GedcomRecord && $record->canShow()) { 197 $this->addRecordFavorite($tree, $user, $record, $note); 198 } 199 } 200 201 $url = route('user-page', ['ged' => $tree->name()]); 202 203 return new RedirectResponse($url); 204 } 205 206 /** 207 * @param Request $request 208 * @param Tree $tree 209 * @param User $user 210 * 211 * @return RedirectResponse 212 */ 213 public function postDeleteFavoriteAction(Request $request, Tree $tree, User $user): RedirectResponse 214 { 215 $favorite_id = (int) $request->get('favorite_id'); 216 217 if (Auth::check()) { 218 Database::prepare( 219 "DELETE FROM `##favorite` WHERE favorite_id = :favorite_id AND user_id = :user_id" 220 )->execute([ 221 'favorite_id' => $favorite_id, 222 'user_id' => $user->getUserId(), 223 ]); 224 } 225 226 $url = route('user-page', ['ged' => $tree->name()]); 227 228 return new RedirectResponse($url); 229 } 230 231 /** 232 * @param Tree $tree 233 * @param User $user 234 * @param string $url 235 * @param string $title 236 * @param string $note 237 * 238 * @return void 239 */ 240 private function addUrlFavorite(Tree $tree, User $user, string $url, string $title, string $note) 241 { 242 $favorite = Database::prepare( 243 "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND url = :url" 244 )->execute([ 245 'gedcom_id' => $tree->id(), 246 'user_id' => $user->getUserId(), 247 'url' => $url, 248 ])->fetchOneRow(); 249 250 if ($favorite === null) { 251 Database::prepare( 252 "INSERT INTO `##favorite` (gedcom_id, favorite_type, user_id, url, note, title) VALUES (:gedcom_id, 'URL', :user_id, :url, :note, :title)" 253 )->execute([ 254 'gedcom_id' => $tree->id(), 255 'user_id' => $user->getUserId(), 256 'url' => $url, 257 'note' => $note, 258 'title' => $title, 259 ]); 260 } else { 261 Database::prepare( 262 "UPDATE `##favorite` SET note = :note, title = :title WHERE favorite_id = :favorite_id" 263 )->execute([ 264 'note' => $note, 265 'title' => $title, 266 'favorite_id' => $favorite->favorite_id, 267 ]); 268 } 269 } 270 271 /** 272 * @param Tree $tree 273 * @param User $user 274 * @param GedcomRecord $record 275 * @param string $note 276 * 277 * @return void 278 */ 279 private function addRecordFavorite(Tree $tree, User $user, GedcomRecord $record, string $note) 280 { 281 $favorite = Database::prepare( 282 "SELECT * FROM `##favorite` WHERE gedcom_id = :gedcom_id AND user_id = :user_id AND xref = :xref" 283 )->execute([ 284 'gedcom_id' => $tree->id(), 285 'user_id' => $user->getUserId(), 286 'xref' => $record->xref(), 287 ])->fetchOneRow(); 288 289 if ($favorite === null) { 290 Database::prepare( 291 "INSERT INTO `##favorite` (gedcom_id, user_id, favorite_type, xref, note) VALUES (:gedcom_id, :user_id, :favorite_type, :xref, :note)" 292 )->execute([ 293 'gedcom_id' => $tree->id(), 294 'user_id' => $user->getUserId(), 295 'favorite_type' => $record::RECORD_TYPE, 296 'xref' => $record->xref(), 297 'note' => $note, 298 ]); 299 } else { 300 Database::prepare( 301 "UPDATE `##favorite` SET note = :note WHERE favorite_id = :favorite_id" 302 )->execute([ 303 'note' => $note, 304 'favorite_id' => $favorite->favorite_id, 305 ]); 306 } 307 } 308} 309