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