xref: /webtrees/app/Module/UserFavoritesModule.php (revision 1e71bdc0ba6fc5add8fed9a3beb51cfca09e47dd)
1<?php
2namespace Fisharebest\Webtrees;
3
4/**
5 * webtrees: online genealogy
6 * Copyright (C) 2015 webtrees development team
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19use PDO;
20
21/**
22 * Class UserFavoritesModule
23 *
24 * The "user favorites" module is almost identical to the "family tree favorites" module
25 */
26class UserFavoritesModule extends FamilyTreeFavoritesModule {
27	/** {@inheritdoc} */
28	public function getDescription() {
29		return /* I18N: Description of the “Favorites” module */ I18N::translate('Display and manage a user’s favorite pages.');
30	}
31
32	/** {@inheritdoc} */
33	public function isUserBlock() {
34		return true;
35	}
36
37	/** {@inheritdoc} */
38	public function isGedcomBlock() {
39		return false;
40	}
41
42	/**
43	 * Get the favorites for a user (for the current family tree)
44	 *
45	 * @param integer $user_id
46	 *
47	 * @return string[][]
48	 */
49	public static function getFavorites($user_id) {
50		global $WT_TREE;
51
52		self::updateSchema(); // make sure the favorites table has been created
53
54		return
55			Database::prepare(
56				"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" .
57				" FROM `##favorite` WHERE user_id=? AND gedcom_id=?")
58			->execute(array($user_id, $WT_TREE->getTreeId()))
59			->fetchAll(PDO::FETCH_ASSOC);
60	}
61
62	/** {@inheritdoc} */
63	public function modAction($modAction) {
64		global $WT_TREE;
65
66		switch ($modAction) {
67		case 'menu-add-favorite':
68			// Process the "add to user favorites" menu item on indi/fam/etc. pages
69			$record = GedcomRecord::getInstance(Filter::post('xref', WT_REGEX_XREF), $WT_TREE);
70			if (Auth::check() && $record->canShowName()) {
71				self::addFavorite(array(
72					'user_id'   => Auth::id(),
73					'gedcom_id' => $record->getTree()->getTreeId(),
74					'gid'       => $record->getXref(),
75					'type'      => $record::RECORD_TYPE,
76					'url'       => null,
77					'note'      => null,
78					'title'     => null,
79				));
80				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()));
81			}
82			break;
83		}
84	}
85}
86