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