xref: /webtrees/app/Module/FamilyTreeFavoritesModule.php (revision 24ec66ce7e77188cd2495b0f8d4dd0ae6e8c9c52)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roachnamespace Fisharebest\Webtrees;
38c2e8227SGreg Roach
48c2e8227SGreg Roach/**
58c2e8227SGreg Roach * webtrees: online genealogy
68c2e8227SGreg Roach * Copyright (C) 2015 webtrees development team
78c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
88c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
98c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
108c2e8227SGreg Roach * (at your option) any later version.
118c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
128c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
138c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
148c2e8227SGreg Roach * GNU General Public License for more details.
158c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
168c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
178c2e8227SGreg Roach */
188c2e8227SGreg Roach
198c2e8227SGreg Roachuse PDO;
208c2e8227SGreg Roachuse PDOException;
218c2e8227SGreg Roachuse Rhumsaa\Uuid\Uuid;
228c2e8227SGreg Roach
238c2e8227SGreg Roach/**
248c2e8227SGreg Roach * Class FamilyTreeFavoritesModule
258c2e8227SGreg Roach *
268c2e8227SGreg Roach * Note that the user favorites module simply extends this module, so ensure that the
278c2e8227SGreg Roach * logic works for both.
288c2e8227SGreg Roach */
298c2e8227SGreg Roachclass FamilyTreeFavoritesModule extends Module implements ModuleBlockInterface {
308c2e8227SGreg Roach	/** {@inheritdoc} */
318c2e8227SGreg Roach	public function getTitle() {
328c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('Favorites');
338c2e8227SGreg Roach	}
348c2e8227SGreg Roach
358c2e8227SGreg Roach	/** {@inheritdoc} */
368c2e8227SGreg Roach	public function getDescription() {
378c2e8227SGreg Roach		return /* I18N: Description of the “Favorites” module */ I18N::translate('Display and manage a family tree’s favorite pages.');
388c2e8227SGreg Roach	}
398c2e8227SGreg Roach
408c2e8227SGreg Roach	/** {@inheritdoc} */
418c2e8227SGreg Roach	public function getBlock($block_id, $template = true, $cfg = null) {
424b9ff166SGreg Roach		global $ctype, $controller, $WT_TREE;
438c2e8227SGreg Roach
448c2e8227SGreg Roach		self::updateSchema(); // make sure the favorites table has been created
458c2e8227SGreg Roach
468c2e8227SGreg Roach		$action = Filter::get('action');
478c2e8227SGreg Roach		switch ($action) {
488c2e8227SGreg Roach		case 'deletefav':
498c2e8227SGreg Roach			$favorite_id = Filter::getInteger('favorite_id');
508c2e8227SGreg Roach			if ($favorite_id) {
518c2e8227SGreg Roach				self::deleteFavorite($favorite_id);
528c2e8227SGreg Roach			}
538c2e8227SGreg Roach			break;
548c2e8227SGreg Roach		case 'addfav':
558c2e8227SGreg Roach			$gid      = Filter::get('gid', WT_REGEX_XREF);
568c2e8227SGreg Roach			$favnote  = Filter::get('favnote');
578c2e8227SGreg Roach			$url      = Filter::getUrl('url');
588c2e8227SGreg Roach			$favtitle = Filter::get('favtitle');
598c2e8227SGreg Roach
608c2e8227SGreg Roach			if ($gid) {
61*24ec66ceSGreg Roach				$record = GedcomRecord::getInstance($gid, $WT_TREE);
628c2e8227SGreg Roach				if ($record && $record->canShow()) {
638c2e8227SGreg Roach					self::addFavorite(array(
648c2e8227SGreg Roach						'user_id'   => $ctype === 'user' ? Auth::id() : null,
65*24ec66ceSGreg Roach						'gedcom_id' => $WT_TREE->getTreeId(),
668c2e8227SGreg Roach						'gid'       => $record->getXref(),
678c2e8227SGreg Roach						'type'      => $record::RECORD_TYPE,
688c2e8227SGreg Roach						'url'       => null,
698c2e8227SGreg Roach						'note'      => $favnote,
708c2e8227SGreg Roach						'title'     => $favtitle,
718c2e8227SGreg Roach					));
728c2e8227SGreg Roach				}
738c2e8227SGreg Roach			} elseif ($url) {
748c2e8227SGreg Roach				self::addFavorite(array(
758c2e8227SGreg Roach					'user_id'   => $ctype === 'user' ? Auth::id() : null,
76*24ec66ceSGreg Roach					'gedcom_id' => $WT_TREE->getTreeId(),
778c2e8227SGreg Roach					'gid'       => null,
788c2e8227SGreg Roach					'type'      => 'URL',
798c2e8227SGreg Roach					'url'       => $url,
808c2e8227SGreg Roach					'note'      => $favnote,
818c2e8227SGreg Roach					'title'     => $favtitle ? $favtitle : $url,
828c2e8227SGreg Roach				));
838c2e8227SGreg Roach			}
848c2e8227SGreg Roach			break;
858c2e8227SGreg Roach		}
868c2e8227SGreg Roach
878c2e8227SGreg Roach		$block = get_block_setting($block_id, 'block', '0');
888c2e8227SGreg Roach
898c2e8227SGreg Roach		if ($cfg) {
908c2e8227SGreg Roach			foreach (array('block') as $name) {
918c2e8227SGreg Roach				if (array_key_exists($name, $cfg)) {
928c2e8227SGreg Roach					$$name = $cfg[$name];
938c2e8227SGreg Roach				}
948c2e8227SGreg Roach			}
958c2e8227SGreg Roach		}
968c2e8227SGreg Roach
97*24ec66ceSGreg Roach		$userfavs = $this->getFavorites($ctype === 'user' ? Auth::id() : $WT_TREE->getTreeId());
988c2e8227SGreg Roach		if (!is_array($userfavs)) {
998c2e8227SGreg Roach			$userfavs = array();
1008c2e8227SGreg Roach		}
1018c2e8227SGreg Roach
1028c2e8227SGreg Roach		$id = $this->getName() . $block_id;
1038c2e8227SGreg Roach		$class = $this->getName() . '_block';
1048c2e8227SGreg Roach		$title = $this->getTitle();
1058c2e8227SGreg Roach
1068c2e8227SGreg Roach		if (Auth::check()) {
1078c2e8227SGreg Roach			$controller
1088c2e8227SGreg Roach				->addExternalJavascript(WT_AUTOCOMPLETE_JS_URL)
1098c2e8227SGreg Roach				->addInlineJavascript('autocomplete();');
1108c2e8227SGreg Roach		}
1118c2e8227SGreg Roach
1128c2e8227SGreg Roach		$content = '';
1138c2e8227SGreg Roach		if ($userfavs) {
1148c2e8227SGreg Roach			foreach ($userfavs as $key=>$favorite) {
1158c2e8227SGreg Roach				if (isset($favorite['id'])) {
1168c2e8227SGreg Roach					$key = $favorite['id'];
1178c2e8227SGreg Roach				}
1188c2e8227SGreg Roach				$removeFavourite = '<a class="font9" href="index.php?ctype=' . $ctype . '&amp;action=deletefav&amp;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> ';
1198c2e8227SGreg Roach				if ($favorite['type'] == 'URL') {
1208c2e8227SGreg Roach					$content .= '<div id="boxurl' . $key . '.0" class="person_box">';
1214b9ff166SGreg Roach					if ($ctype == 'user' || Auth::isManager($WT_TREE)) {
1228c2e8227SGreg Roach						$content .= $removeFavourite;
1238c2e8227SGreg Roach					}
1248c2e8227SGreg Roach					$content .= '<a href="' . $favorite['url'] . '"><b>' . $favorite['title'] . '</b></a>';
1258c2e8227SGreg Roach					$content .= '<br>' . $favorite['note'];
1268c2e8227SGreg Roach					$content .= '</div>';
1278c2e8227SGreg Roach				} else {
128*24ec66ceSGreg Roach					$record = GedcomRecord::getInstance($favorite['gid'], $WT_TREE);
1298c2e8227SGreg Roach					if ($record && $record->canShow()) {
1308c2e8227SGreg Roach						if ($record instanceof Individual) {
1318c2e8227SGreg Roach							$content .= '<div id="box' . $favorite["gid"] . '.0" class="person_box action_header';
1328c2e8227SGreg Roach							switch ($record->getsex()) {
1338c2e8227SGreg Roach							case 'M':
1348c2e8227SGreg Roach								break;
1358c2e8227SGreg Roach							case 'F':
1368c2e8227SGreg Roach								$content .= 'F';
1378c2e8227SGreg Roach								break;
1388c2e8227SGreg Roach							default:
1398c2e8227SGreg Roach								$content .= 'NN';
1408c2e8227SGreg Roach								break;
1418c2e8227SGreg Roach							}
1428c2e8227SGreg Roach							$content .= '">';
1434b9ff166SGreg Roach							if ($ctype == "user" || Auth::isManager($WT_TREE)) {
1448c2e8227SGreg Roach								$content .= $removeFavourite;
1458c2e8227SGreg Roach							}
1468c2e8227SGreg Roach							$content .= Theme::theme()->individualBoxLarge($record);
1478c2e8227SGreg Roach							$content .= $favorite['note'];
1488c2e8227SGreg Roach							$content .= '</div>';
1498c2e8227SGreg Roach						} else {
1508c2e8227SGreg Roach							$content .= '<div id="box' . $favorite['gid'] . '.0" class="person_box">';
1514b9ff166SGreg Roach							if ($ctype == 'user' || Auth::isManager($WT_TREE)) {
1528c2e8227SGreg Roach								$content .= $removeFavourite;
1538c2e8227SGreg Roach							}
154e9165d70SGreg Roach							$content .= $record->formatList('span');
1558c2e8227SGreg Roach							$content .= '<br>' . $favorite['note'];
1568c2e8227SGreg Roach							$content .= '</div>';
1578c2e8227SGreg Roach						}
1588c2e8227SGreg Roach					}
1598c2e8227SGreg Roach				}
1608c2e8227SGreg Roach			}
1618c2e8227SGreg Roach		}
1624b9ff166SGreg Roach		if ($ctype == 'user' || Auth::isManager($WT_TREE)) {
1638c2e8227SGreg Roach			$uniqueID = Uuid::uuid4(); // This block can theoretically appear multiple times, so use a unique ID.
1648c2e8227SGreg Roach			$content .= '<div class="add_fav_head">';
1658c2e8227SGreg Roach			$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>';
1668c2e8227SGreg Roach			$content .= '</div>';
1678c2e8227SGreg Roach			$content .= '<div id="add_fav' . $uniqueID . '" style="display: none;">';
1688c2e8227SGreg Roach			$content .= '<form name="addfavform" method="get" action="index.php">';
1698c2e8227SGreg Roach			$content .= '<input type="hidden" name="action" value="addfav">';
1708c2e8227SGreg Roach			$content .= '<input type="hidden" name="ctype" value="' . $ctype . '">';
171*24ec66ceSGreg Roach			$content .= '<input type="hidden" name="ged" value="' . $WT_TREE->getNameHtml() . '">';
1728c2e8227SGreg Roach			$content .= '<div class="add_fav_ref">';
1738c2e8227SGreg Roach			$content .= '<input type="radio" name="fav_category" value="record" checked onclick="jQuery(\'#gid' . $uniqueID . '\').removeAttr(\'disabled\'); jQuery(\'#url, #favtitle\').attr(\'disabled\',\'disabled\').val(\'\');">';
1748c2e8227SGreg Roach			$content .= '<label for="gid' . $uniqueID . '">' . I18N::translate('Enter an individual, family, or source ID') . '</label>';
1758c2e8227SGreg Roach			$content .= '<input class="pedigree_form" data-autocomplete-type="IFSRO" type="text" name="gid" id="gid' . $uniqueID . '" size="5" value="">';
1768c2e8227SGreg Roach			$content .= ' ' . print_findindi_link('gid' . $uniqueID);
1778c2e8227SGreg Roach			$content .= ' ' . print_findfamily_link('gid' . $uniqueID);
1788c2e8227SGreg Roach			$content .= ' ' . print_findsource_link('gid' . $uniqueID);
1798c2e8227SGreg Roach			$content .= ' ' . print_findrepository_link('gid' . $uniqueID);
1808c2e8227SGreg Roach			$content .= ' ' . print_findnote_link('gid' . $uniqueID);
1818c2e8227SGreg Roach			$content .= ' ' . print_findmedia_link('gid' . $uniqueID);
1828c2e8227SGreg Roach			$content .= '</div>';
1838c2e8227SGreg Roach			$content .= '<div class="add_fav_url">';
1848c2e8227SGreg Roach			$content .= '<input type="radio" name="fav_category" value="url" onclick="jQuery(\'#url, #favtitle\').removeAttr(\'disabled\'); jQuery(\'#gid' . $uniqueID . '\').attr(\'disabled\',\'disabled\').val(\'\');">';
185764a01d9SGreg Roach			$content .= '<input type="text" name="url" id="url" size="20" value="" placeholder="' . GedcomTag::getLabel('URL') . '" disabled> ';
1868c2e8227SGreg Roach			$content .= '<input type="text" name="favtitle" id="favtitle" size="20" value="" placeholder="' . I18N::translate('Title') . '" disabled>';
1878c2e8227SGreg Roach			$content .= '<p>' . I18N::translate('Enter an optional note about this favorite') . '</p>';
1888c2e8227SGreg Roach			$content .= '<textarea name="favnote" rows="6" cols="50"></textarea>';
1898c2e8227SGreg Roach			$content .= '</div>';
1908c2e8227SGreg Roach			$content .= '<input type="submit" value="' . I18N::translate('Add') . '">';
1918c2e8227SGreg Roach			$content .= '</form></div>';
1928c2e8227SGreg Roach		}
1938c2e8227SGreg Roach
1948c2e8227SGreg Roach		if ($template) {
1958c2e8227SGreg Roach			if ($block) {
1968c2e8227SGreg Roach				$class .= ' small_inner_block';
1978c2e8227SGreg Roach			}
1988c2e8227SGreg Roach			return Theme::theme()->formatBlock($id, $title, $class, $content);
1998c2e8227SGreg Roach		} else {
2008c2e8227SGreg Roach			return $content;
2018c2e8227SGreg Roach		}
2028c2e8227SGreg Roach	}
2038c2e8227SGreg Roach
2048c2e8227SGreg Roach	/** {@inheritdoc} */
2058c2e8227SGreg Roach	public function loadAjax() {
2068c2e8227SGreg Roach		return false;
2078c2e8227SGreg Roach	}
2088c2e8227SGreg Roach
2098c2e8227SGreg Roach	/** {@inheritdoc} */
2108c2e8227SGreg Roach	public function isUserBlock() {
2118c2e8227SGreg Roach		return false;
2128c2e8227SGreg Roach	}
2138c2e8227SGreg Roach
2148c2e8227SGreg Roach	/** {@inheritdoc} */
2158c2e8227SGreg Roach	public function isGedcomBlock() {
2168c2e8227SGreg Roach		return true;
2178c2e8227SGreg Roach	}
2188c2e8227SGreg Roach
2198c2e8227SGreg Roach	/** {@inheritdoc} */
2208c2e8227SGreg Roach	public function configureBlock($block_id) {
2218c2e8227SGreg Roach		if (Filter::postBool('save') && Filter::checkCsrf()) {
2228c2e8227SGreg Roach			set_block_setting($block_id, 'block', Filter::postBool('block'));
2238c2e8227SGreg Roach		}
2248c2e8227SGreg Roach
2258c2e8227SGreg Roach		$block = get_block_setting($block_id, 'block', '0');
2268c2e8227SGreg Roach
2278c2e8227SGreg Roach		echo '<tr><td class="descriptionbox wrap width33">';
2288c2e8227SGreg Roach		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
2298c2e8227SGreg Roach		echo '</td><td class="optionbox">';
2308c2e8227SGreg Roach		echo edit_field_yes_no('block', $block);
2318c2e8227SGreg Roach		echo '</td></tr>';
2328c2e8227SGreg Roach	}
2338c2e8227SGreg Roach
2348c2e8227SGreg Roach	/**
2358c2e8227SGreg Roach	 * Delete a favorite from the database
2368c2e8227SGreg Roach	 *
2378c2e8227SGreg Roach	 * @param integer $favorite_id
2388c2e8227SGreg Roach	 *
2398c2e8227SGreg Roach	 * @return boolean
2408c2e8227SGreg Roach	 */
2418c2e8227SGreg Roach	public static function deleteFavorite($favorite_id) {
2428c2e8227SGreg Roach		return (bool)
2438c2e8227SGreg Roach			Database::prepare("DELETE FROM `##favorite` WHERE favorite_id=?")
2448c2e8227SGreg Roach			->execute(array($favorite_id));
2458c2e8227SGreg Roach	}
2468c2e8227SGreg Roach
2478c2e8227SGreg Roach	/**
2488c2e8227SGreg Roach	 * Store a new favorite in the database
2498c2e8227SGreg Roach	 *
2508c2e8227SGreg Roach	 * @param $favorite
2518c2e8227SGreg Roach	 *
2528c2e8227SGreg Roach	 * @return boolean
2538c2e8227SGreg Roach	 */
2548c2e8227SGreg Roach	public static function addFavorite($favorite) {
2558c2e8227SGreg Roach		// -- make sure a favorite is added
2568c2e8227SGreg Roach		if (empty($favorite['gid']) && empty($favorite['url'])) {
2578c2e8227SGreg Roach			return false;
2588c2e8227SGreg Roach		}
2598c2e8227SGreg Roach
2608c2e8227SGreg Roach		//-- make sure this is not a duplicate entry
2618c2e8227SGreg Roach		$sql = "SELECT SQL_NO_CACHE 1 FROM `##favorite` WHERE";
2628c2e8227SGreg Roach		if (!empty($favorite['gid'])) {
2638c2e8227SGreg Roach			$sql .= " xref=?";
2648c2e8227SGreg Roach			$vars = array($favorite['gid']);
2658c2e8227SGreg Roach		} else {
2668c2e8227SGreg Roach			$sql .= " url=?";
2678c2e8227SGreg Roach			$vars = array($favorite['url']);
2688c2e8227SGreg Roach		}
2698c2e8227SGreg Roach		$sql .= " AND gedcom_id=?";
2708c2e8227SGreg Roach		$vars[] = $favorite['gedcom_id'];
2718c2e8227SGreg Roach		if ($favorite['user_id']) {
2728c2e8227SGreg Roach			$sql .= " AND user_id=?";
2738c2e8227SGreg Roach			$vars[] = $favorite['user_id'];
2748c2e8227SGreg Roach		} else {
2758c2e8227SGreg Roach			$sql .= " AND user_id IS NULL";
2768c2e8227SGreg Roach		}
2778c2e8227SGreg Roach
2788c2e8227SGreg Roach		if (Database::prepare($sql)->execute($vars)->fetchOne()) {
2798c2e8227SGreg Roach			return false;
2808c2e8227SGreg Roach		}
2818c2e8227SGreg Roach
2828c2e8227SGreg Roach		//-- add the favorite to the database
2838c2e8227SGreg Roach		return (bool)
2848c2e8227SGreg Roach			Database::prepare("INSERT INTO `##favorite` (user_id, gedcom_id, xref, favorite_type, url, title, note) VALUES (? ,? ,? ,? ,? ,? ,?)")
2858c2e8227SGreg Roach				->execute(array($favorite['user_id'], $favorite['gedcom_id'], $favorite['gid'], $favorite['type'], $favorite['url'], $favorite['title'], $favorite['note']));
2868c2e8227SGreg Roach	}
2878c2e8227SGreg Roach
2888c2e8227SGreg Roach	/**
2898c2e8227SGreg Roach	 * Get favorites for a user or family tree
2908c2e8227SGreg Roach	 *
2918c2e8227SGreg Roach	 * @param integer $gedcom_id
2928c2e8227SGreg Roach	 *
2938c2e8227SGreg Roach	 * @return string[][]
2948c2e8227SGreg Roach	 */
2958c2e8227SGreg Roach	public static function getFavorites($gedcom_id) {
2968c2e8227SGreg Roach		self::updateSchema(); // make sure the favorites table has been created
2978c2e8227SGreg Roach
2988c2e8227SGreg Roach		return
2998c2e8227SGreg Roach			Database::prepare(
3008c2e8227SGreg Roach				"SELECT SQL_CACHE favorite_id AS id, user_id, gedcom_id, xref AS gid, favorite_type AS type, title, note, url" .
3018c2e8227SGreg Roach				" FROM `##favorite` WHERE gedcom_id=? AND user_id IS NULL")
3028c2e8227SGreg Roach			->execute(array($gedcom_id))
3038c2e8227SGreg Roach			->fetchAll(PDO::FETCH_ASSOC);
3048c2e8227SGreg Roach	}
3058c2e8227SGreg Roach
3068c2e8227SGreg Roach	/**
3078c2e8227SGreg Roach	 * Make sure the database structure is up-to-date.
3088c2e8227SGreg Roach	 */
3098c2e8227SGreg Roach	protected static function updateSchema() {
3108c2e8227SGreg Roach		// Create tables, if not already present
3118c2e8227SGreg Roach		try {
3128c2e8227SGreg Roach			Database::updateSchema(WT_ROOT . WT_MODULES_DIR . 'gedcom_favorites/db_schema/', 'FV_SCHEMA_VERSION', 4);
3138c2e8227SGreg Roach		} catch (PDOException $ex) {
3148c2e8227SGreg Roach			// The schema update scripts should never fail.  If they do, there is no clean recovery.
3158c2e8227SGreg Roach			FlashMessages::addMessage($ex->getMessage(), 'danger');
3168c2e8227SGreg Roach			header('Location: ' . WT_BASE_URL . 'site-unavailable.php');
3178c2e8227SGreg Roach			throw $ex;
3188c2e8227SGreg Roach		}
3198c2e8227SGreg Roach	}
3208c2e8227SGreg Roach}
321