1<?php 2namespace Fisharebest\Webtrees\Module; 3 4/** 5 * webtrees: online genealogy 6 * Copyright (C) 2015 webtrees development team 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19use Fisharebest\Webtrees\Auth; 20use Fisharebest\Webtrees\Database; 21use Fisharebest\Webtrees\Filter; 22use Fisharebest\Webtrees\FlashMessages; 23use Fisharebest\Webtrees\GedcomRecord; 24use Fisharebest\Webtrees\GedcomTag; 25use Fisharebest\Webtrees\I18N; 26use Fisharebest\Webtrees\Individual; 27use Fisharebest\Webtrees\Theme; 28use PDO; 29use PDOException; 30use Rhumsaa\Uuid\Uuid; 31 32/** 33 * Class FamilyTreeFavoritesModule 34 * 35 * Note that the user favorites module simply extends this module, so ensure that the 36 * logic works for both. 37 */ 38class FamilyTreeFavoritesModule extends AbstractModule implements ModuleBlockInterface { 39 /** {@inheritdoc} */ 40 public function __construct($directory) { 41 parent::__construct($directory); 42 43 // Create/update the database tables. 44 Database::updateSchema('\Fisharebest\Webtrees\Module\FamilyTreeFavorites\Schema', 'FV_SCHEMA_VERSION', 4); 45 } 46 47 /** {@inheritdoc} */ 48 public function getTitle() { 49 return /* I18N: Name of a module */ I18N::translate('Favorites'); 50 } 51 52 /** {@inheritdoc} */ 53 public function getDescription() { 54 return /* I18N: Description of the “Favorites” module */ I18N::translate('Display and manage a family tree’s favorite pages.'); 55 } 56 57 /** {@inheritdoc} */ 58 public function getBlock($block_id, $template = true, $cfg = null) { 59 global $ctype, $controller, $WT_TREE; 60 61 $action = Filter::get('action'); 62 switch ($action) { 63 case 'deletefav': 64 $favorite_id = Filter::getInteger('favorite_id'); 65 if ($favorite_id) { 66 self::deleteFavorite($favorite_id); 67 } 68 break; 69 case 'addfav': 70 $gid = Filter::get('gid', WT_REGEX_XREF); 71 $favnote = Filter::get('favnote'); 72 $url = Filter::getUrl('url'); 73 $favtitle = Filter::get('favtitle'); 74 75 if ($gid) { 76 $record = GedcomRecord::getInstance($gid, $WT_TREE); 77 if ($record && $record->canShow()) { 78 self::addFavorite(array( 79 'user_id' => $ctype === 'user' ? Auth::id() : null, 80 'gedcom_id' => $WT_TREE->getTreeId(), 81 'gid' => $record->getXref(), 82 'type' => $record::RECORD_TYPE, 83 'url' => null, 84 'note' => $favnote, 85 'title' => $favtitle, 86 )); 87 } 88 } elseif ($url) { 89 self::addFavorite(array( 90 'user_id' => $ctype === 'user' ? Auth::id() : null, 91 'gedcom_id' => $WT_TREE->getTreeId(), 92 'gid' => null, 93 'type' => 'URL', 94 'url' => $url, 95 'note' => $favnote, 96 'title' => $favtitle ? $favtitle : $url, 97 )); 98 } 99 break; 100 } 101 102 $block = $this->getBlockSetting($block_id, 'block', '0'); 103 104 if ($cfg) { 105 foreach (array('block') as $name) { 106 if (array_key_exists($name, $cfg)) { 107 $$name = $cfg[$name]; 108 } 109 } 110 } 111 112 $userfavs = $this->getFavorites($ctype === 'user' ? Auth::id() : $WT_TREE->getTreeId()); 113 if (!is_array($userfavs)) { 114 $userfavs = array(); 115 } 116 117 $id = $this->getName() . $block_id; 118 $class = $this->getName() . '_block'; 119 $title = $this->getTitle(); 120 121 if (Auth::check()) { 122 $controller 123 ->addExternalJavascript(WT_AUTOCOMPLETE_JS_URL) 124 ->addInlineJavascript('autocomplete();'); 125 } 126 127 $content = ''; 128 if ($userfavs) { 129 foreach ($userfavs as $key => $favorite) { 130 if (isset($favorite['id'])) { 131 $key = $favorite['id']; 132 } 133 $removeFavourite = '<a class="font9" href="index.php?ctype=' . $ctype . '&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> '; 134 if ($favorite['type'] == 'URL') { 135 $content .= '<div id="boxurl' . $key . '.0" class="person_box">'; 136 if ($ctype == 'user' || Auth::isManager($WT_TREE)) { 137 $content .= $removeFavourite; 138 } 139 $content .= '<a href="' . $favorite['url'] . '"><b>' . $favorite['title'] . '</b></a>'; 140 $content .= '<br>' . $favorite['note']; 141 $content .= '</div>'; 142 } else { 143 $record = GedcomRecord::getInstance($favorite['gid'], $WT_TREE); 144 if ($record && $record->canShow()) { 145 if ($record instanceof Individual) { 146 $content .= '<div id="box' . $favorite["gid"] . '.0" class="person_box action_header'; 147 switch ($record->getsex()) { 148 case 'M': 149 break; 150 case 'F': 151 $content .= 'F'; 152 break; 153 default: 154 $content .= 'NN'; 155 break; 156 } 157 $content .= '">'; 158 if ($ctype == "user" || Auth::isManager($WT_TREE)) { 159 $content .= $removeFavourite; 160 } 161 $content .= Theme::theme()->individualBoxLarge($record); 162 $content .= $favorite['note']; 163 $content .= '</div>'; 164 } else { 165 $content .= '<div id="box' . $favorite['gid'] . '.0" class="person_box">'; 166 if ($ctype == 'user' || Auth::isManager($WT_TREE)) { 167 $content .= $removeFavourite; 168 } 169 $content .= $record->formatList('span'); 170 $content .= '<br>' . $favorite['note']; 171 $content .= '</div>'; 172 } 173 } 174 } 175 } 176 } 177 if ($ctype == 'user' || Auth::isManager($WT_TREE)) { 178 $uniqueID = Uuid::uuid4(); // This block can theoretically appear multiple times, so use a unique ID. 179 $content .= '<div class="add_fav_head">'; 180 $content .= '<a href="#" onclick="return expand_layer(\'add_fav' . $uniqueID . '\');">' . I18N::translate('Add a new favorite') . '<i id="add_fav' . $uniqueID . '_img" class="icon-plus"></i></a>'; 181 $content .= '</div>'; 182 $content .= '<div id="add_fav' . $uniqueID . '" style="display: none;">'; 183 $content .= '<form name="addfavform" method="get" action="index.php">'; 184 $content .= '<input type="hidden" name="action" value="addfav">'; 185 $content .= '<input type="hidden" name="ctype" value="' . $ctype . '">'; 186 $content .= '<input type="hidden" name="ged" value="' . $WT_TREE->getNameHtml() . '">'; 187 $content .= '<div class="add_fav_ref">'; 188 $content .= '<input type="radio" name="fav_category" value="record" checked onclick="jQuery(\'#gid' . $uniqueID . '\').removeAttr(\'disabled\'); jQuery(\'#url, #favtitle\').attr(\'disabled\',\'disabled\').val(\'\');">'; 189 $content .= '<label for="gid' . $uniqueID . '">' . I18N::translate('Enter an individual, family, or source ID') . '</label>'; 190 $content .= '<input class="pedigree_form" data-autocomplete-type="IFSRO" type="text" name="gid" id="gid' . $uniqueID . '" size="5" value="">'; 191 $content .= ' ' . print_findindi_link('gid' . $uniqueID); 192 $content .= ' ' . print_findfamily_link('gid' . $uniqueID); 193 $content .= ' ' . print_findsource_link('gid' . $uniqueID); 194 $content .= ' ' . print_findrepository_link('gid' . $uniqueID); 195 $content .= ' ' . print_findnote_link('gid' . $uniqueID); 196 $content .= ' ' . print_findmedia_link('gid' . $uniqueID); 197 $content .= '</div>'; 198 $content .= '<div class="add_fav_url">'; 199 $content .= '<input type="radio" name="fav_category" value="url" onclick="jQuery(\'#url, #favtitle\').removeAttr(\'disabled\'); jQuery(\'#gid' . $uniqueID . '\').attr(\'disabled\',\'disabled\').val(\'\');">'; 200 $content .= '<input type="text" name="url" id="url" size="20" value="" placeholder="' . GedcomTag::getLabel('URL') . '" disabled> '; 201 $content .= '<input type="text" name="favtitle" id="favtitle" size="20" value="" placeholder="' . I18N::translate('Title') . '" disabled>'; 202 $content .= '<p>' . I18N::translate('Enter an optional note about this favorite') . '</p>'; 203 $content .= '<textarea name="favnote" rows="6" cols="50"></textarea>'; 204 $content .= '</div>'; 205 $content .= '<input type="submit" value="' . I18N::translate('Add') . '">'; 206 $content .= '</form></div>'; 207 } 208 209 if ($template) { 210 if ($block) { 211 $class .= ' small_inner_block'; 212 } 213 214 return Theme::theme()->formatBlock($id, $title, $class, $content); 215 } else { 216 return $content; 217 } 218 } 219 220 /** {@inheritdoc} */ 221 public function loadAjax() { 222 return false; 223 } 224 225 /** {@inheritdoc} */ 226 public function isUserBlock() { 227 return false; 228 } 229 230 /** {@inheritdoc} */ 231 public function isGedcomBlock() { 232 return true; 233 } 234 235 /** {@inheritdoc} */ 236 public function configureBlock($block_id) { 237 if (Filter::postBool('save') && Filter::checkCsrf()) { 238 $this->setBlockSetting($block_id, 'block', Filter::postBool('block')); 239 } 240 241 $block = $this->getBlockSetting($block_id, 'block', '0'); 242 243 echo '<tr><td class="descriptionbox wrap width33">'; 244 echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow'); 245 echo '</td><td class="optionbox">'; 246 echo edit_field_yes_no('block', $block); 247 echo '</td></tr>'; 248 } 249 250 /** 251 * Delete a favorite from the database 252 * 253 * @param int $favorite_id 254 * 255 * @return bool 256 */ 257 public static function deleteFavorite($favorite_id) { 258 return (bool) 259 Database::prepare("DELETE FROM `##favorite` WHERE favorite_id=?") 260 ->execute(array($favorite_id)); 261 } 262 263 /** 264 * Store a new favorite in the database 265 * 266 * @param $favorite 267 * 268 * @return bool 269 */ 270 public static function addFavorite($favorite) { 271 // -- make sure a favorite is added 272 if (empty($favorite['gid']) && empty($favorite['url'])) { 273 return false; 274 } 275 276 //-- make sure this is not a duplicate entry 277 $sql = "SELECT SQL_NO_CACHE 1 FROM `##favorite` WHERE"; 278 if (!empty($favorite['gid'])) { 279 $sql .= " xref=?"; 280 $vars = array($favorite['gid']); 281 } else { 282 $sql .= " url=?"; 283 $vars = array($favorite['url']); 284 } 285 $sql .= " AND gedcom_id=?"; 286 $vars[] = $favorite['gedcom_id']; 287 if ($favorite['user_id']) { 288 $sql .= " AND user_id=?"; 289 $vars[] = $favorite['user_id']; 290 } else { 291 $sql .= " AND user_id IS NULL"; 292 } 293 294 if (Database::prepare($sql)->execute($vars)->fetchOne()) { 295 return false; 296 } 297 298 //-- add the favorite to the database 299 return (bool) 300 Database::prepare("INSERT INTO `##favorite` (user_id, gedcom_id, xref, favorite_type, url, title, note) VALUES (? ,? ,? ,? ,? ,? ,?)") 301 ->execute(array($favorite['user_id'], $favorite['gedcom_id'], $favorite['gid'], $favorite['type'], $favorite['url'], $favorite['title'], $favorite['note'])); 302 } 303 304 /** 305 * Get favorites for a user or family tree 306 * 307 * @param int $gedcom_id 308 * 309 * @return string[][] 310 */ 311 public static function getFavorites($gedcom_id) { 312 return 313 Database::prepare( 314 "SELECT SQL_CACHE favorite_id AS id, user_id, gedcom_id, xref AS gid, favorite_type AS type, title, note, url" . 315 " FROM `##favorite` WHERE gedcom_id=? AND user_id IS NULL") 316 ->execute(array($gedcom_id)) 317 ->fetchAll(PDO::FETCH_ASSOC); 318 } 319 320 /** 321 * Make sure the database structure is up-to-date. 322 */ 323 protected static function updateSchema() { 324 // Create tables, if not already present 325 try { 326 Database::updateSchema(WT_ROOT . WT_MODULES_DIR . 'gedcom_favorites/db_schema/', 'FV_SCHEMA_VERSION', 4); 327 } catch (PDOException $ex) { 328 // The schema update scripts should never fail. If they do, there is no clean recovery. 329 FlashMessages::addMessage($ex->getMessage(), 'danger'); 330 header('Location: ' . WT_BASE_URL . 'site-unavailable.php'); 331 throw $ex; 332 } 333 } 334} 335