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