1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2017 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\Filter; 21use Fisharebest\Webtrees\GedcomRecord; 22use Fisharebest\Webtrees\GedcomTag; 23use Fisharebest\Webtrees\I18N; 24use Fisharebest\Webtrees\Individual; 25use Fisharebest\Webtrees\Theme; 26use Fisharebest\Webtrees\View; 27use PDO; 28use Ramsey\Uuid\Uuid; 29 30/** 31 * Class FamilyTreeFavoritesModule 32 * 33 * Note that the user favorites module simply extends this module, so ensure that the 34 * logic works for both. 35 */ 36class FamilyTreeFavoritesModule extends AbstractModule implements ModuleBlockInterface { 37 // How to update the database schema for this module 38 const SCHEMA_TARGET_VERSION = 4; 39 const SCHEMA_SETTING_NAME = 'FV_SCHEMA_VERSION'; 40 const SCHEMA_MIGRATION_PREFIX = '\Fisharebest\Webtrees\Module\FamilyTreeFavorites\Schema'; 41 42 /** 43 * Create a new module. 44 * 45 * @param string $directory Where is this module installed 46 */ 47 public function __construct($directory) { 48 parent::__construct($directory); 49 50 // Create/update the database tables. 51 // NOTE: if we want to set any module-settings, we'll need to move this. 52 Database::updateSchema(self::SCHEMA_MIGRATION_PREFIX, self::SCHEMA_SETTING_NAME, self::SCHEMA_TARGET_VERSION); 53 } 54 55 /** 56 * How should this module be labelled on tabs, menus, etc.? 57 * 58 * @return string 59 */ 60 public function getTitle() { 61 return /* I18N: Name of a module */ I18N::translate('Favorites'); 62 } 63 64 /** 65 * A sentence describing what this module does. 66 * 67 * @return string 68 */ 69 public function getDescription() { 70 return /* I18N: Description of the “Favorites” module */ I18N::translate('Display and manage a family tree’s favorite pages.'); 71 } 72 73 /** 74 * Generate the HTML content of this block. 75 * 76 * @param int $block_id 77 * @param bool $template 78 * @param string[] $cfg 79 * 80 * @return string 81 */ 82 public function getBlock($block_id, $template = true, $cfg = []) { 83 global $ctype, $WT_TREE; 84 85 $action = Filter::get('action'); 86 switch ($action) { 87 case 'deletefav': 88 $favorite_id = Filter::getInteger('favorite_id'); 89 if ($favorite_id) { 90 self::deleteFavorite($favorite_id); 91 } 92 break; 93 case 'addfav': 94 $gid = Filter::get('gid', WT_REGEX_XREF); 95 $favnote = Filter::get('favnote'); 96 $url = Filter::getUrl('url'); 97 $favtitle = Filter::get('favtitle'); 98 99 if ($gid) { 100 $record = GedcomRecord::getInstance($gid, $WT_TREE); 101 if ($record && $record->canShow()) { 102 self::addFavorite([ 103 'user_id' => $ctype === 'user' ? Auth::id() : null, 104 'gedcom_id' => $WT_TREE->getTreeId(), 105 'gid' => $record->getXref(), 106 'type' => $record::RECORD_TYPE, 107 'url' => null, 108 'note' => $favnote, 109 'title' => $favtitle, 110 ]); 111 } 112 } elseif ($url) { 113 self::addFavorite([ 114 'user_id' => $ctype === 'user' ? Auth::id() : null, 115 'gedcom_id' => $WT_TREE->getTreeId(), 116 'gid' => null, 117 'type' => 'URL', 118 'url' => $url, 119 'note' => $favnote, 120 'title' => $favtitle ? $favtitle : $url, 121 ]); 122 } 123 break; 124 } 125 126 $userfavs = $this->getFavorites($ctype === 'user' ? Auth::id() : $WT_TREE->getTreeId()); 127 if (!is_array($userfavs)) { 128 $userfavs = []; 129 } 130 131 $content = ''; 132 if ($userfavs) { 133 foreach ($userfavs as $key => $favorite) { 134 if (isset($favorite['id'])) { 135 $key = $favorite['id']; 136 } 137 $removeFavourite = '<a class="font9" href="index.php?ctype=' . $ctype . '&ged=' . $WT_TREE->getNameHtml() . '&action=deletefav&favorite_id=' . $key . '" onclick="return confirm(\'' . I18N::translate('Are you sure you want to remove this item from your list of favorites?') . '\');">' . I18N::translate('Remove') . '</a> '; 138 if ($favorite['type'] == 'URL') { 139 $content .= '<div id="boxurl' . $key . '.0" class="person_box">'; 140 if ($ctype == 'user' || Auth::isManager($WT_TREE)) { 141 $content .= $removeFavourite; 142 } 143 $content .= '<a href="' . $favorite['url'] . '"><b>' . $favorite['title'] . '</b></a>'; 144 $content .= '<br>' . $favorite['note']; 145 $content .= '</div>'; 146 } else { 147 $record = GedcomRecord::getInstance($favorite['gid'], $WT_TREE); 148 if ($record && $record->canShow()) { 149 if ($record instanceof Individual) { 150 $content .= '<div id="box' . $favorite['gid'] . '.0" class="person_box action_header'; 151 switch ($record->getSex()) { 152 case 'M': 153 break; 154 case 'F': 155 $content .= 'F'; 156 break; 157 default: 158 $content .= 'NN'; 159 break; 160 } 161 $content .= '">'; 162 if ($ctype == 'user' || Auth::isManager($WT_TREE)) { 163 $content .= $removeFavourite; 164 } 165 $content .= Theme::theme()->individualBoxLarge($record); 166 $content .= $favorite['note']; 167 $content .= '</div>'; 168 } else { 169 $content .= '<div id="box' . $favorite['gid'] . '.0" class="person_box">'; 170 if ($ctype == 'user' || Auth::isManager($WT_TREE)) { 171 $content .= $removeFavourite; 172 } 173 $content .= $record->formatList('span'); 174 $content .= '<br>' . $favorite['note']; 175 $content .= '</div>'; 176 } 177 } 178 } 179 } 180 } 181 if ($ctype == 'user' || Auth::isManager($WT_TREE)) { 182 $uniqueID = Uuid::uuid4(); // This block can theoretically appear multiple times, so use a unique ID. 183 $content .= '<div class="add_fav_head">'; 184 $content .= '<a href="#" onclick="return expand_layer(\'add_fav' . $uniqueID . '\');">' . I18N::translate('Add a favorite') . '<i id="add_fav' . $uniqueID . '_img" class="icon-plus"></i></a>'; 185 $content .= '</div>'; 186 $content .= '<div id="add_fav' . $uniqueID . '" style="display: none;">'; 187 $content .= '<form name="addfavform" action="index.php">'; 188 $content .= '<input type="hidden" name="action" value="addfav">'; 189 $content .= '<input type="hidden" name="ctype" value="' . $ctype . '">'; 190 $content .= '<input type="hidden" name="ged" value="' . $WT_TREE->getNameHtml() . '">'; 191 $content .= '<div class="add_fav_ref">'; 192 $content .= '<input type="radio" name="fav_category" value="record" checked onclick="$(\'#gid' . $uniqueID . '\').removeAttr(\'disabled\'); $(\'#url, #favtitle\').attr(\'disabled\',\'disabled\').val(\'\');">'; 193 $content .= '<label for="gid' . $uniqueID . '">' . I18N::translate('Enter an individual, family, or source ID') . '</label>'; 194 $content .= '<input class="pedigree_form" data-autocomplete-type="IFSRO" type="text" name="gid" id="gid' . $uniqueID . '" size="5" value="">'; 195 $content .= '</div>'; 196 $content .= '<div class="add_fav_url">'; 197 $content .= '<input type="radio" name="fav_category" value="url" onclick="$(\'#url, #favtitle\').removeAttr(\'disabled\'); $(\'#gid' . $uniqueID . '\').attr(\'disabled\',\'disabled\').val(\'\');">'; 198 $content .= '<input type="text" name="url" id="url" size="20" value="" placeholder="' . GedcomTag::getLabel('URL') . '" disabled> '; 199 $content .= '<input type="text" name="favtitle" id="favtitle" size="20" value="" placeholder="' . I18N::translate('Title') . '" disabled>'; 200 $content .= '<p>' . I18N::translate('Enter an optional note about this favorite') . '</p>'; 201 $content .= '<textarea name="favnote" rows="6" cols="50"></textarea>'; 202 $content .= '</div>'; 203 $content .= '<input type="submit" value="' . /* I18N: A button label. */ I18N::translate('add') . '">'; 204 $content .= '</form></div>'; 205 } 206 207 if ($template) { 208 //return Theme::theme()->formatBlock($id, $title, $class, $content); 209 return View::make('blocks/template', [ 210 'block' => str_replace('_', '-', $this->getName()), 211 'id' => $block_id, 212 'config_url' => '', 213 'title' => $this->getTitle(), 214 'content' => $content, 215 ]); 216 } else { 217 return $content; 218 } 219 } 220 221 /** 222 * Should this block load asynchronously using AJAX? 223 * 224 * Simple blocks are faster in-line, more comples ones 225 * can be loaded later. 226 * 227 * @return bool 228 */ 229 public function loadAjax() { 230 return false; 231 } 232 233 /** 234 * Can this block be shown on the user’s home page? 235 * 236 * @return bool 237 */ 238 public function isUserBlock() { 239 return false; 240 } 241 242 /** 243 * Can this block be shown on the tree’s home page? 244 * 245 * @return bool 246 */ 247 public function isGedcomBlock() { 248 return true; 249 } 250 251 /** 252 * An HTML form to edit block settings 253 * 254 * @param int $block_id 255 */ 256 public function configureBlock($block_id) {} 257 258 /** 259 * Delete a favorite from the database 260 * 261 * @param int $favorite_id 262 * 263 * @return bool 264 */ 265 public static function deleteFavorite($favorite_id) { 266 return (bool) 267 Database::prepare("DELETE FROM `##favorite` WHERE favorite_id=?") 268 ->execute([$favorite_id]); 269 } 270 271 /** 272 * Store a new favorite in the database 273 * 274 * @param $favorite 275 * 276 * @return bool 277 */ 278 public static function addFavorite($favorite) { 279 // -- make sure a favorite is added 280 if (empty($favorite['gid']) && empty($favorite['url'])) { 281 return false; 282 } 283 284 //-- make sure this is not a duplicate entry 285 $sql = "SELECT SQL_NO_CACHE 1 FROM `##favorite` WHERE"; 286 if (!empty($favorite['gid'])) { 287 $sql .= " xref=?"; 288 $vars = [$favorite['gid']]; 289 } else { 290 $sql .= " url=?"; 291 $vars = [$favorite['url']]; 292 } 293 $sql .= " AND gedcom_id=?"; 294 $vars[] = $favorite['gedcom_id']; 295 if ($favorite['user_id']) { 296 $sql .= " AND user_id=?"; 297 $vars[] = $favorite['user_id']; 298 } else { 299 $sql .= " AND user_id IS NULL"; 300 } 301 302 if (Database::prepare($sql)->execute($vars)->fetchOne()) { 303 return false; 304 } 305 306 //-- add the favorite to the database 307 return (bool) 308 Database::prepare("INSERT INTO `##favorite` (user_id, gedcom_id, xref, favorite_type, url, title, note) VALUES (? ,? ,? ,? ,? ,? ,?)") 309 ->execute([$favorite['user_id'], $favorite['gedcom_id'], $favorite['gid'], $favorite['type'], $favorite['url'], $favorite['title'], $favorite['note']]); 310 } 311 312 /** 313 * Get favorites for a user or family tree 314 * 315 * @param int $gedcom_id 316 * 317 * @return string[][] 318 */ 319 public static function getFavorites($gedcom_id) { 320 return 321 Database::prepare( 322 "SELECT SQL_CACHE favorite_id AS id, user_id, gedcom_id, xref AS gid, favorite_type AS type, title, note, url" . 323 " FROM `##favorite` WHERE gedcom_id=? AND user_id IS NULL") 324 ->execute([$gedcom_id]) 325 ->fetchAll(PDO::FETCH_ASSOC); 326 } 327} 328