xref: /webtrees/app/Module/UserFavoritesModule.php (revision 4cb8d1f0eb760216ff1402f3415ef3e289b4d8b3)
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		self::updateSchema(); // make sure the favorites table has been created
51
52		return
53			Database::prepare(
54				"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" .
55				" FROM `##favorite` WHERE user_id=? AND gedcom_id=?")
56			->execute(array($user_id, WT_GED_ID))
57			->fetchAll(PDO::FETCH_ASSOC);
58	}
59
60	/** {@inheritdoc} */
61	public function modAction($modAction) {
62		switch ($modAction) {
63		case 'menu-add-favorite':
64			// Process the "add to user favorites" menu item on indi/fam/etc. pages
65			$record = GedcomRecord::getInstance(Filter::post('xref', WT_REGEX_XREF));
66			if (Auth::check() && $record->canShowName()) {
67				self::addFavorite(array(
68					'user_id'   => Auth::id(),
69					'gedcom_id' => $record->getTree()->getTreeId(),
70					'gid'       => $record->getXref(),
71					'type'      => $record::RECORD_TYPE,
72					'url'       => null,
73					'note'      => null,
74					'title'     => null,
75				));
76				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()));
77			}
78			break;
79		}
80	}
81}
82