xref: /webtrees/app/Module/FamilyTreeFavoritesModule.php (revision e5588fb099ce26dcdcb7faeb13861818cd9a4306)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
178c2e8227SGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomTag;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\Individual;
250e62c4b8SGreg Roachuse Fisharebest\Webtrees\Theme;
269807cf72SGreg Roachuse Fisharebest\Webtrees\Tree;
27a6afca4cSGreg Roachuse Fisharebest\Webtrees\User;
288c2e8227SGreg Roachuse PDO;
29577e6b29SGreg Roachuse Ramsey\Uuid\Uuid;
309807cf72SGreg Roachuse stdClass;
318c2e8227SGreg Roach
328c2e8227SGreg Roach/**
338c2e8227SGreg Roach * Class FamilyTreeFavoritesModule
348c2e8227SGreg Roach *
358c2e8227SGreg Roach * Note that the user favorites module simply extends this module, so ensure that the
368c2e8227SGreg Roach * logic works for both.
378c2e8227SGreg Roach */
38e2a378d3SGreg Roachclass FamilyTreeFavoritesModule extends AbstractModule implements ModuleBlockInterface {
397fafef87SGreg Roach	// How to update the database schema for this module
407fafef87SGreg Roach	const SCHEMA_TARGET_VERSION   = 4;
417fafef87SGreg Roach	const SCHEMA_SETTING_NAME     = 'FV_SCHEMA_VERSION';
427fafef87SGreg Roach	const SCHEMA_MIGRATION_PREFIX = '\Fisharebest\Webtrees\Module\FamilyTreeFavorites\Schema';
437fafef87SGreg Roach
4476692c8bSGreg Roach	/**
4576692c8bSGreg Roach	 * Create a new module.
4676692c8bSGreg Roach	 *
4776692c8bSGreg Roach	 * @param string $directory Where is this module installed
4876692c8bSGreg Roach	 */
493bfb94b0SGreg Roach	public function __construct($directory) {
503bfb94b0SGreg Roach		parent::__construct($directory);
513bfb94b0SGreg Roach
523bfb94b0SGreg Roach		// Create/update the database tables.
537fafef87SGreg Roach		// NOTE: if we want to set any module-settings, we'll need to move this.
547fafef87SGreg Roach		Database::updateSchema(self::SCHEMA_MIGRATION_PREFIX, self::SCHEMA_SETTING_NAME, self::SCHEMA_TARGET_VERSION);
553bfb94b0SGreg Roach	}
563bfb94b0SGreg Roach
5776692c8bSGreg Roach	/**
5876692c8bSGreg Roach	 * How should this module be labelled on tabs, menus, etc.?
5976692c8bSGreg Roach	 *
6076692c8bSGreg Roach	 * @return string
6176692c8bSGreg Roach	 */
628c2e8227SGreg Roach	public function getTitle() {
638c2e8227SGreg Roach		return /* I18N: Name of a module */ I18N::translate('Favorites');
648c2e8227SGreg Roach	}
658c2e8227SGreg Roach
6676692c8bSGreg Roach	/**
6776692c8bSGreg Roach	 * A sentence describing what this module does.
6876692c8bSGreg Roach	 *
6976692c8bSGreg Roach	 * @return string
7076692c8bSGreg Roach	 */
718c2e8227SGreg Roach	public function getDescription() {
728c2e8227SGreg Roach		return /* I18N: Description of the “Favorites” module */ I18N::translate('Display and manage a family tree’s favorite pages.');
738c2e8227SGreg Roach	}
748c2e8227SGreg Roach
7576692c8bSGreg Roach	/**
7676692c8bSGreg Roach	 * Generate the HTML content of this block.
7776692c8bSGreg Roach	 *
7876692c8bSGreg Roach	 * @param int      $block_id
7976692c8bSGreg Roach	 * @param bool     $template
80727f238cSGreg Roach	 * @param string[] $cfg
8176692c8bSGreg Roach	 *
8276692c8bSGreg Roach	 * @return string
8376692c8bSGreg Roach	 */
84a9430be8SGreg Roach	public function getBlock($block_id, $template = true, $cfg = []): string {
859c6524dcSGreg Roach		global $ctype, $WT_TREE;
868c2e8227SGreg Roach
878c2e8227SGreg Roach		$action = Filter::get('action');
888c2e8227SGreg Roach		switch ($action) {
898c2e8227SGreg Roach			case 'deletefav':
908c2e8227SGreg Roach				$favorite_id = Filter::getInteger('favorite_id');
918c2e8227SGreg Roach				if ($favorite_id) {
928c2e8227SGreg Roach					self::deleteFavorite($favorite_id);
938c2e8227SGreg Roach				}
948c2e8227SGreg Roach				break;
958c2e8227SGreg Roach			case 'addfav':
969807cf72SGreg Roach				$gid      = Filter::get('gid', WT_REGEX_XREF, '');
978c2e8227SGreg Roach				$favnote  = Filter::get('favnote');
988c2e8227SGreg Roach				$url      = Filter::getUrl('url');
998c2e8227SGreg Roach				$favtitle = Filter::get('favtitle');
1008c2e8227SGreg Roach
1019807cf72SGreg Roach				if ($gid !== '') {
10224ec66ceSGreg Roach					$record = GedcomRecord::getInstance($gid, $WT_TREE);
1038c2e8227SGreg Roach					if ($record && $record->canShow()) {
10413abd6f3SGreg Roach						self::addFavorite([
1058c2e8227SGreg Roach							'user_id'   => $ctype === 'user' ? Auth::id() : null,
10624ec66ceSGreg Roach							'gedcom_id' => $WT_TREE->getTreeId(),
1078c2e8227SGreg Roach							'gid'       => $record->getXref(),
1088c2e8227SGreg Roach							'type'      => $record::RECORD_TYPE,
1098c2e8227SGreg Roach							'url'       => null,
1108c2e8227SGreg Roach							'note'      => $favnote,
1118c2e8227SGreg Roach							'title'     => $favtitle,
11213abd6f3SGreg Roach						]);
1138c2e8227SGreg Roach					}
1149807cf72SGreg Roach				} elseif ($url !== null) {
11513abd6f3SGreg Roach					self::addFavorite([
1168c2e8227SGreg Roach						'user_id'   => $ctype === 'user' ? Auth::id() : null,
11724ec66ceSGreg Roach						'gedcom_id' => $WT_TREE->getTreeId(),
1188c2e8227SGreg Roach						'gid'       => null,
1198c2e8227SGreg Roach						'type'      => 'URL',
1208c2e8227SGreg Roach						'url'       => $url,
1218c2e8227SGreg Roach						'note'      => $favnote,
1228c2e8227SGreg Roach						'title'     => $favtitle ? $favtitle : $url,
12313abd6f3SGreg Roach					]);
1248c2e8227SGreg Roach				}
1258c2e8227SGreg Roach				break;
1268c2e8227SGreg Roach		}
1278c2e8227SGreg Roach
128a6afca4cSGreg Roach		$userfavs = $this->getFavorites($WT_TREE, Auth::user());
1298c2e8227SGreg Roach
1308c2e8227SGreg Roach		$content = '';
1318c2e8227SGreg Roach		if ($userfavs) {
1329807cf72SGreg Roach			foreach ($userfavs as $favorite) {
133b11e001bSGreg Roach				$removeFavourite = '<a class="font9" href="index.php?ctype=' . $ctype . '&amp;ged=' . e($WT_TREE->getName()) . '&amp;action=deletefav&amp;favorite_id=' . $favorite->favorite_id . '" data-confirm="' . I18N::translate('Are you sure you want to remove this item from your list of favorites?') . '" onclick="return confirm(this.dataset.confirm);">' . I18N::translate('Remove') . '</a> ';
1349807cf72SGreg Roach				if ($favorite->favorite_type === 'URL') {
1359807cf72SGreg Roach					$content .= '<div id="boxurl' . e($favorite->favorite_id) . '.0" class="person_box">';
1364b9ff166SGreg Roach					if ($ctype == 'user' || Auth::isManager($WT_TREE)) {
1378c2e8227SGreg Roach						$content .= $removeFavourite;
1388c2e8227SGreg Roach					}
1399807cf72SGreg Roach					$content .= '<a href="' . e($favorite->url) . '"><b>' . e($favorite->title) . '</b></a>';
1409807cf72SGreg Roach					$content .= '<br>' . e((string) $favorite->note);
1418c2e8227SGreg Roach					$content .= '</div>';
1428c2e8227SGreg Roach				} else {
1439807cf72SGreg Roach					$record = GedcomRecord::getInstance($favorite->xref, $WT_TREE);
1448c2e8227SGreg Roach					if ($record && $record->canShow()) {
1458c2e8227SGreg Roach						if ($record instanceof Individual) {
1469807cf72SGreg Roach							$content .= '<div id="box' . e($favorite->xref) . '.0" class="person_box action_header';
1477820e4d7SGreg Roach							switch ($record->getSex()) {
1488c2e8227SGreg Roach								case 'M':
1498c2e8227SGreg Roach									break;
1508c2e8227SGreg Roach								case 'F':
1518c2e8227SGreg Roach									$content .= 'F';
1528c2e8227SGreg Roach									break;
1538c2e8227SGreg Roach								default:
1548c2e8227SGreg Roach									$content .= 'NN';
1558c2e8227SGreg Roach									break;
1568c2e8227SGreg Roach							}
1578c2e8227SGreg Roach							$content .= '">';
1587a6ee1acSGreg Roach							if ($ctype == 'user' || Auth::isManager($WT_TREE)) {
1598c2e8227SGreg Roach								$content .= $removeFavourite;
1608c2e8227SGreg Roach							}
1618c2e8227SGreg Roach							$content .= Theme::theme()->individualBoxLarge($record);
1629807cf72SGreg Roach							$content .= e((string) $favorite->note);
1638c2e8227SGreg Roach							$content .= '</div>';
1648c2e8227SGreg Roach						} else {
1659807cf72SGreg Roach							$content .= '<div id="box' . e($favorite->xref) . '.0" class="person_box">';
1664b9ff166SGreg Roach							if ($ctype == 'user' || Auth::isManager($WT_TREE)) {
1678c2e8227SGreg Roach								$content .= $removeFavourite;
1688c2e8227SGreg Roach							}
169b165e17cSGreg Roach							$content .= $record->formatList();
1709807cf72SGreg Roach							$content .= '<br>' . e((string) $favorite->note);
1718c2e8227SGreg Roach							$content .= '</div>';
1728c2e8227SGreg Roach						}
1738c2e8227SGreg Roach					}
1748c2e8227SGreg Roach				}
1758c2e8227SGreg Roach			}
1768c2e8227SGreg Roach		}
1774b9ff166SGreg Roach		if ($ctype == 'user' || Auth::isManager($WT_TREE)) {
1788c2e8227SGreg Roach			$uniqueID = Uuid::uuid4(); // This block can theoretically appear multiple times, so use a unique ID.
1798c2e8227SGreg Roach			$content .= '<div class="add_fav_head">';
18029bb8efbSGreg Roach			$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>';
1818c2e8227SGreg Roach			$content .= '</div>';
1828c2e8227SGreg Roach			$content .= '<div id="add_fav' . $uniqueID . '" style="display: none;">';
183564ae2d7SGreg Roach			$content .= '<form name="addfavform" action="index.php">';
1848c2e8227SGreg Roach			$content .= '<input type="hidden" name="action" value="addfav">';
1858c2e8227SGreg Roach			$content .= '<input type="hidden" name="ctype" value="' . $ctype . '">';
186b11e001bSGreg Roach			$content .= '<input type="hidden" name="ged" value="' . e($WT_TREE->getName()) . '">';
1878c2e8227SGreg Roach			$content .= '<div class="add_fav_ref">';
188564ae2d7SGreg Roach			$content .= '<input type="radio" name="fav_category" value="record" checked onclick="$(\'#gid' . $uniqueID . '\').removeAttr(\'disabled\'); $(\'#url, #favtitle\').attr(\'disabled\',\'disabled\').val(\'\');">';
1898c2e8227SGreg Roach			$content .= '<label for="gid' . $uniqueID . '">' . I18N::translate('Enter an individual, family, or source ID') . '</label>';
1908c2e8227SGreg Roach			$content .= '<input class="pedigree_form" data-autocomplete-type="IFSRO" type="text" name="gid" id="gid' . $uniqueID . '" size="5" value="">';
1918c2e8227SGreg Roach			$content .= '</div>';
1928c2e8227SGreg Roach			$content .= '<div class="add_fav_url">';
193564ae2d7SGreg Roach			$content .= '<input type="radio" name="fav_category" value="url" onclick="$(\'#url, #favtitle\').removeAttr(\'disabled\'); $(\'#gid' . $uniqueID . '\').attr(\'disabled\',\'disabled\').val(\'\');">';
194764a01d9SGreg Roach			$content .= '<input type="text" name="url" id="url" size="20" value="" placeholder="' . GedcomTag::getLabel('URL') . '" disabled> ';
1958c2e8227SGreg Roach			$content .= '<input type="text" name="favtitle" id="favtitle" size="20" value="" placeholder="' . I18N::translate('Title') . '" disabled>';
1968c2e8227SGreg Roach			$content .= '<p>' . I18N::translate('Enter an optional note about this favorite') . '</p>';
1978c2e8227SGreg Roach			$content .= '<textarea name="favnote" rows="6" cols="50"></textarea>';
1988c2e8227SGreg Roach			$content .= '</div>';
199552fa1fbSGreg Roach			$content .= '<input type="submit" value="' . /* I18N: A button label. */ I18N::translate('add') . '">';
2008c2e8227SGreg Roach			$content .= '</form></div>';
2018c2e8227SGreg Roach		}
2028c2e8227SGreg Roach
2039807cf72SGreg Roach		$content = view('blocks/favorites', [
2049807cf72SGreg Roach			'ctype'      => $ctype,
2059807cf72SGreg Roach			'favorites'  => $userfavs,
2069807cf72SGreg Roach			'is_manager' => Auth::isManager($WT_TREE),
2079807cf72SGreg Roach			'tree'       => $WT_TREE,
2089807cf72SGreg Roach		]);
2099807cf72SGreg Roach
2108c2e8227SGreg Roach		if ($template) {
211225e381fSGreg Roach			return view('blocks/template', [
2129c6524dcSGreg Roach				'block'      => str_replace('_', '-', $this->getName()),
2139c6524dcSGreg Roach				'id'         => $block_id,
2149c6524dcSGreg Roach				'config_url' => '',
2159c6524dcSGreg Roach				'title'      => $this->getTitle(),
2169c6524dcSGreg Roach				'content'    => $content,
2179c6524dcSGreg Roach			]);
2188c2e8227SGreg Roach		} else {
2198c2e8227SGreg Roach			return $content;
2208c2e8227SGreg Roach		}
2218c2e8227SGreg Roach	}
2228c2e8227SGreg Roach
22376692c8bSGreg Roach	/**
22476692c8bSGreg Roach	 * Should this block load asynchronously using AJAX?
22576692c8bSGreg Roach	 *
22676692c8bSGreg Roach	 * Simple blocks are faster in-line, more comples ones
22776692c8bSGreg Roach	 * can be loaded later.
22876692c8bSGreg Roach	 *
22976692c8bSGreg Roach	 * @return bool
23076692c8bSGreg Roach	 */
231a9430be8SGreg Roach	public function loadAjax(): bool {
2328c2e8227SGreg Roach		return false;
2338c2e8227SGreg Roach	}
2348c2e8227SGreg Roach
23576692c8bSGreg Roach	/**
23676692c8bSGreg Roach	 * Can this block be shown on the user’s home page?
23776692c8bSGreg Roach	 *
23876692c8bSGreg Roach	 * @return bool
23976692c8bSGreg Roach	 */
240a9430be8SGreg Roach	public function isUserBlock(): bool {
2418c2e8227SGreg Roach		return false;
2428c2e8227SGreg Roach	}
2438c2e8227SGreg Roach
24476692c8bSGreg Roach	/**
24576692c8bSGreg Roach	 * Can this block be shown on the tree’s home page?
24676692c8bSGreg Roach	 *
24776692c8bSGreg Roach	 * @return bool
24876692c8bSGreg Roach	 */
249a9430be8SGreg Roach	public function isGedcomBlock(): bool {
2508c2e8227SGreg Roach		return true;
2518c2e8227SGreg Roach	}
2528c2e8227SGreg Roach
25376692c8bSGreg Roach	/**
25476692c8bSGreg Roach	 * An HTML form to edit block settings
25576692c8bSGreg Roach	 *
25676692c8bSGreg Roach	 * @param int $block_id
257a9430be8SGreg Roach	 *
258a9430be8SGreg Roach	 * @return void
25976692c8bSGreg Roach	 */
260be9a728cSGreg Roach	public function configureBlock($block_id) {
2618d68cabeSGreg Roach	}
2628c2e8227SGreg Roach
2638c2e8227SGreg Roach	/**
2648c2e8227SGreg Roach	 * Delete a favorite from the database
2658c2e8227SGreg Roach	 *
266cbc1590aSGreg Roach	 * @param int $favorite_id
2678c2e8227SGreg Roach	 *
268cbc1590aSGreg Roach	 * @return bool
2698c2e8227SGreg Roach	 */
2708c2e8227SGreg Roach	public static function deleteFavorite($favorite_id) {
2718c2e8227SGreg Roach		return (bool)
2728c2e8227SGreg Roach			Database::prepare("DELETE FROM `##favorite` WHERE favorite_id=?")
27313abd6f3SGreg Roach			->execute([$favorite_id]);
2748c2e8227SGreg Roach	}
2758c2e8227SGreg Roach
2768c2e8227SGreg Roach	/**
2778c2e8227SGreg Roach	 * Store a new favorite in the database
2788c2e8227SGreg Roach	 *
2798c2e8227SGreg Roach	 * @param $favorite
2808c2e8227SGreg Roach	 *
281cbc1590aSGreg Roach	 * @return bool
2828c2e8227SGreg Roach	 */
2838c2e8227SGreg Roach	public static function addFavorite($favorite) {
2848c2e8227SGreg Roach		// -- make sure a favorite is added
2858c2e8227SGreg Roach		if (empty($favorite['gid']) && empty($favorite['url'])) {
2868c2e8227SGreg Roach			return false;
2878c2e8227SGreg Roach		}
2888c2e8227SGreg Roach
2898c2e8227SGreg Roach		//-- make sure this is not a duplicate entry
290*e5588fb0SGreg Roach		$sql = "SELECT 1 FROM `##favorite` WHERE";
2918c2e8227SGreg Roach		if (!empty($favorite['gid'])) {
2928c2e8227SGreg Roach			$sql .= " xref=?";
29313abd6f3SGreg Roach			$vars = [$favorite['gid']];
2948c2e8227SGreg Roach		} else {
2958c2e8227SGreg Roach			$sql .= " url=?";
29613abd6f3SGreg Roach			$vars = [$favorite['url']];
2978c2e8227SGreg Roach		}
2988c2e8227SGreg Roach		$sql .= " AND gedcom_id=?";
2998c2e8227SGreg Roach		$vars[] = $favorite['gedcom_id'];
3008c2e8227SGreg Roach		if ($favorite['user_id']) {
3018c2e8227SGreg Roach			$sql .= " AND user_id=?";
3028c2e8227SGreg Roach			$vars[] = $favorite['user_id'];
3038c2e8227SGreg Roach		} else {
3048c2e8227SGreg Roach			$sql .= " AND user_id IS NULL";
3058c2e8227SGreg Roach		}
3068c2e8227SGreg Roach
3078c2e8227SGreg Roach		if (Database::prepare($sql)->execute($vars)->fetchOne()) {
3088c2e8227SGreg Roach			return false;
3098c2e8227SGreg Roach		}
3108c2e8227SGreg Roach
3118c2e8227SGreg Roach		//-- add the favorite to the database
3128c2e8227SGreg Roach		return (bool)
3138c2e8227SGreg Roach			Database::prepare("INSERT INTO `##favorite` (user_id, gedcom_id, xref, favorite_type, url, title, note) VALUES (? ,? ,? ,? ,? ,? ,?)")
31413abd6f3SGreg Roach				->execute([$favorite['user_id'], $favorite['gedcom_id'], $favorite['gid'], $favorite['type'], $favorite['url'], $favorite['title'], $favorite['note']]);
3158c2e8227SGreg Roach	}
3168c2e8227SGreg Roach
3178c2e8227SGreg Roach	/**
3188c2e8227SGreg Roach	 * Get favorites for a user or family tree
3198c2e8227SGreg Roach	 *
3209807cf72SGreg Roach	 * @param Tree $tree
321a6afca4cSGreg Roach	 * @param User $user
3228c2e8227SGreg Roach	 *
3239807cf72SGreg Roach	 * @return stdClass[]
3248c2e8227SGreg Roach	 */
325a6afca4cSGreg Roach	public static function getFavorites(Tree $tree, User $user) {
3269807cf72SGreg Roach		$favorites =
3278c2e8227SGreg Roach			Database::prepare(
328*e5588fb0SGreg Roach				"SELECT favorite_id, user_id, gedcom_id, xref, favorite_type, title, note, url" .
3299807cf72SGreg Roach				" FROM `##favorite` WHERE gedcom_id = :tree_id AND user_id IS NULL")
3309807cf72SGreg Roach			->execute([
3319807cf72SGreg Roach				'tree_id' => $tree->getTreeId(),
3329807cf72SGreg Roach			])
3339807cf72SGreg Roach			->fetchAll();
3349807cf72SGreg Roach
3359807cf72SGreg Roach		foreach ($favorites as $favorite) {
3369807cf72SGreg Roach			$favorite->record = GedcomRecord::getInstance($favorite->xref, $tree);
3379807cf72SGreg Roach		}
3389807cf72SGreg Roach
3399807cf72SGreg Roach		return $favorites;
3408c2e8227SGreg Roach	}
3418c2e8227SGreg Roach}
342