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