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