1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Fisharebest\Webtrees\Auth; 23use Fisharebest\Webtrees\Contracts\UserInterface; 24use Fisharebest\Webtrees\Family; 25use Fisharebest\Webtrees\GedcomRecord; 26use Fisharebest\Webtrees\I18N; 27use Fisharebest\Webtrees\Individual; 28use Fisharebest\Webtrees\Media; 29use Fisharebest\Webtrees\Repository; 30use Fisharebest\Webtrees\Source; 31use Fisharebest\Webtrees\Tree; 32use Illuminate\Database\Capsule\Manager as DB; 33use Illuminate\Support\Str; 34use Psr\Http\Message\ResponseInterface; 35use Psr\Http\Message\ServerRequestInterface; 36use stdClass; 37 38use function assert; 39 40/** 41 * Class UserFavoritesModule 42 */ 43class UserFavoritesModule extends AbstractModule implements ModuleBlockInterface 44{ 45 use ModuleBlockTrait; 46 47 /** 48 * How should this module be identified in the control panel, etc.? 49 * 50 * @return string 51 */ 52 public function title(): string 53 { 54 /* I18N: Name of a module */ 55 return I18N::translate('Favorites'); 56 } 57 58 /** 59 * A sentence describing what this module does. 60 * 61 * @return string 62 */ 63 public function description(): string 64 { 65 /* I18N: Description of the “Favorites” module */ 66 return I18N::translate('Display and manage a user’s favorite pages.'); 67 } 68 69 /** 70 * Generate the HTML content of this block. 71 * 72 * @param Tree $tree 73 * @param int $block_id 74 * @param string $context 75 * @param string[] $config 76 * 77 * @return string 78 */ 79 public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string 80 { 81 $content = view('modules/favorites/favorites', [ 82 'block_id' => $block_id, 83 'can_edit' => true, 84 'favorites' => $this->getFavorites($tree, Auth::user()), 85 'module_name' => $this->name(), 86 'tree' => $tree, 87 ]); 88 89 if ($context !== self::CONTEXT_EMBED) { 90 return view('modules/block-template', [ 91 'block' => Str::kebab($this->name()), 92 'id' => $block_id, 93 'config_url' => '', 94 'title' => $this->title(), 95 'content' => $content, 96 ]); 97 } 98 99 return $content; 100 } 101 102 /** 103 * Should this block load asynchronously using AJAX? 104 * Simple blocks are faster in-line, more complex ones can be loaded later. 105 * 106 * @return bool 107 */ 108 public function loadAjax(): bool 109 { 110 return false; 111 } 112 113 /** 114 * Can this block be shown on the user’s home page? 115 * 116 * @return bool 117 */ 118 public function isUserBlock(): bool 119 { 120 return true; 121 } 122 123 /** 124 * Can this block be shown on the tree’s home page? 125 * 126 * @return bool 127 */ 128 public function isTreeBlock(): bool 129 { 130 return false; 131 } 132 133 /** 134 * Get the favorites for a user 135 * 136 * @param Tree $tree 137 * @param UserInterface $user 138 * 139 * @return stdClass[] 140 */ 141 public function getFavorites(Tree $tree, UserInterface $user): array 142 { 143 return DB::table('favorite') 144 ->where('gedcom_id', '=', $tree->id()) 145 ->where('user_id', '=', $user->id()) 146 ->get() 147 ->map(static function (stdClass $row) use ($tree): stdClass { 148 if ($row->xref !== null) { 149 $row->record = GedcomRecord::getInstance($row->xref, $tree); 150 } else { 151 $row->record = null; 152 } 153 154 return $row; 155 }) 156 ->all(); 157 } 158 159 /** 160 * @param ServerRequestInterface $request 161 * 162 * @return ResponseInterface 163 */ 164 public function postAddFavoriteAction(ServerRequestInterface $request): ResponseInterface 165 { 166 $tree = $request->getAttribute('tree'); 167 assert($tree instanceof Tree); 168 169 $user = $request->getAttribute('user'); 170 $params = (array) $request->getParsedBody(); 171 172 $note = $params['note']; 173 $title = $params['title']; 174 $url = $params['url']; 175 $type = $params['type']; 176 $xref = $params[$type . '-xref'] ?? ''; 177 178 $record = $this->getRecordForType($type, $xref, $tree); 179 180 if (Auth::check()) { 181 if ($type === 'url' && $url !== '') { 182 $this->addUrlFavorite($tree, $user, $url, $title ?: $url, $note); 183 } 184 185 if ($record instanceof GedcomRecord && $record->canShow()) { 186 $this->addRecordFavorite($tree, $user, $record, $note); 187 } 188 } 189 190 $url = route('user-page', ['tree' => $tree->name()]); 191 192 return redirect($url); 193 } 194 195 /** 196 * @param ServerRequestInterface $request 197 * 198 * @return ResponseInterface 199 */ 200 public function postDeleteFavoriteAction(ServerRequestInterface $request): ResponseInterface 201 { 202 $tree = $request->getAttribute('tree'); 203 assert($tree instanceof Tree); 204 205 $user = $request->getAttribute('user'); 206 $favorite_id = $request->getQueryParams()['favorite_id']; 207 208 if (Auth::check()) { 209 DB::table('favorite') 210 ->where('favorite_id', '=', $favorite_id) 211 ->where('user_id', '=', $user->id()) 212 ->delete(); 213 } 214 215 $url = route('user-page', ['tree' => $tree->name()]); 216 217 return redirect($url); 218 } 219 220 /** 221 * @param Tree $tree 222 * @param UserInterface $user 223 * @param string $url 224 * @param string $title 225 * @param string $note 226 * 227 * @return void 228 */ 229 private function addUrlFavorite(Tree $tree, UserInterface $user, string $url, string $title, string $note): void 230 { 231 DB::table('favorite')->updateOrInsert([ 232 'gedcom_id' => $tree->id(), 233 'user_id' => $user->id(), 234 'url' => $url, 235 ], [ 236 'favorite_type' => 'URL', 237 'note' => $note, 238 'title' => $title, 239 ]); 240 } 241 242 /** 243 * @param Tree $tree 244 * @param UserInterface $user 245 * @param GedcomRecord $record 246 * @param string $note 247 * 248 * @return void 249 */ 250 private function addRecordFavorite(Tree $tree, UserInterface $user, GedcomRecord $record, string $note): void 251 { 252 DB::table('favorite')->updateOrInsert([ 253 'gedcom_id' => $tree->id(), 254 'user_id' => $user->id(), 255 'xref' => $record->xref(), 256 ], [ 257 'favorite_type' => $record::RECORD_TYPE, 258 'note' => $note, 259 ]); 260 } 261 262 /** 263 * @param string $type 264 * @param string $xref 265 * @param Tree $tree 266 * 267 * @return GedcomRecord|null 268 */ 269 private function getRecordForType(string $type, string $xref, Tree $tree): ?GedcomRecord 270 { 271 switch ($type) { 272 case 'indi': 273 return Individual::getInstance($xref, $tree); 274 275 case 'fam': 276 return Family::getInstance($xref, $tree); 277 278 case 'sour': 279 return Source::getInstance($xref, $tree); 280 281 case 'repo': 282 return Repository::getInstance($xref, $tree); 283 284 case 'obje': 285 return Media::getInstance($xref, $tree); 286 287 default: 288 return null; 289 } 290 } 291} 292