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