xref: /webtrees/app/Module/UserFavoritesModule.php (revision d3f5aeb4a663fe2df2598ba53b99f32298f6d354)
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\Filter;
21use Fisharebest\Webtrees\FlashMessages;
22use Fisharebest\Webtrees\GedcomRecord;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Tree;
25use Fisharebest\Webtrees\User;
26use PDO;
27
28/**
29 * Class UserFavoritesModule
30 *
31 * The "user favorites" module is almost identical to the "family tree favorites" module
32 */
33class UserFavoritesModule extends FamilyTreeFavoritesModule {
34	/** {@inheritdoc} */
35	public function getDescription() {
36		return /* I18N: Description of the “Favorites” module */ I18N::translate('Display and manage a user’s favorite pages.');
37	}
38
39	/**
40	 * Can this block be shown on the user’s home page?
41	 *
42	 * @return bool
43	 */
44	public function isUserBlock(): bool {
45		return true;
46	}
47
48	/**
49	 * Can this block be shown on the tree’s home page?
50	 *
51	 * @return bool
52	 */
53	public function isGedcomBlock(): bool {
54		return false;
55	}
56
57	/**
58	 * Get the favorites for a user (for the current family tree)
59	 *
60	 * @param Tree $tree
61	 * @param User $user
62	 *
63	 * @return string[][]
64	 */
65	public static function getFavorites(Tree $tree, User $user) {
66		$favorites =
67			Database::prepare(
68				"SELECT SQL_CACHE favorite_id, user_id, gedcom_id, xref, favorite_type, title, note, url" .
69				" FROM `##favorite` WHERE user_id = :user_id AND gedcom_id = :tree_id")
70			->execute([
71				'tree_id' => $tree->getTreeId(),
72				'user_id' => $user->getUserId(),
73			])
74			->fetchAll();
75
76		foreach ($favorites as $favorite) {
77			$favorite->record = GedcomRecord::getInstance($favorite->xref, $tree);
78		}
79
80		return $favorites;
81	}
82
83	/**
84	 * This is a general purpose hook, allowing modules to respond to routes
85	 * of the form module.php?mod=FOO&mod_action=BAR
86	 *
87	 * @param string $mod_action
88	 */
89	public function modAction($mod_action) {
90		global $WT_TREE;
91
92		switch ($mod_action) {
93			case 'menu-add-favorite':
94				// Process the "add to user favorites" menu item on indi/fam/etc. pages
95				$record = GedcomRecord::getInstance(Filter::post('xref', WT_REGEX_XREF), $WT_TREE);
96				if (Auth::check() && $record->canShowName()) {
97					self::addFavorite([
98						'user_id'   => Auth::id(),
99						'gedcom_id' => $record->getTree()->getTreeId(),
100						'gid'       => $record->getXref(),
101						'type'      => $record::RECORD_TYPE,
102						'url'       => null,
103						'note'      => null,
104						'title'     => null,
105					]);
106					FlashMessages::addMessage(/* I18N: %s is the name of an individual, source or other record */ I18N::translate('“%s” has been added to your favorites.', $record->getFullName()));
107				}
108				break;
109		}
110	}
111}
112