xref: /webtrees/app/Module/UserFavoritesModule.php (revision bc241c54273892142cd7cd386c7e97823e1415df)
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\FlashMessages;
21use Fisharebest\Webtrees\GedcomRecord;
22use Fisharebest\Webtrees\I18N;
23use Fisharebest\Webtrees\Tree;
24use Fisharebest\Webtrees\User;
25use Symfony\Component\HttpFoundation\Request;
26use Symfony\Component\HttpFoundation\Response;
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	 * @param Request $request
85	 *
86	 * @return Response
87	 */
88	public function postAddFavoriteAction(Request $request): Response {
89		/** @var Tree $tree */
90		$tree = $request->attributes->get('tree');
91
92		$xref = $request->get('xref');
93
94		$record = GedcomRecord::getInstance($xref, $tree);
95
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
107			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()));
108		}
109
110		return new Response;
111	}
112}
113