xref: /webtrees/app/Module/UserFavoritesModule.php (revision 2836aa05d87d478edc3ef1f1983e9d37862ded10)
1<?php
2namespace Fisharebest\Webtrees\Module;
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 Fisharebest\Webtrees\Auth;
20use Fisharebest\Webtrees\Database;
21use Fisharebest\Webtrees\Filter;
22use Fisharebest\Webtrees\FlashMessages;
23use Fisharebest\Webtrees\GedcomRecord;
24use Fisharebest\Webtrees\I18N;
25use PDO;
26
27/**
28 * Class UserFavoritesModule
29 *
30 * The "user favorites" module is almost identical to the "family tree favorites" module
31 */
32class UserFavoritesModule extends FamilyTreeFavoritesModule {
33	/** {@inheritdoc} */
34	public function getDescription() {
35		return /* I18N: Description of the “Favorites” module */ I18N::translate('Display and manage a user’s favorite pages.');
36	}
37
38	/** {@inheritdoc} */
39	public function isUserBlock() {
40		return true;
41	}
42
43	/** {@inheritdoc} */
44	public function isGedcomBlock() {
45		return false;
46	}
47
48	/**
49	 * Get the favorites for a user (for the current family tree)
50	 *
51	 * @param int $user_id
52	 *
53	 * @return string[][]
54	 */
55	public static function getFavorites($user_id) {
56		global $WT_TREE;
57
58		return
59			Database::prepare(
60				"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" .
61				" FROM `##favorite` WHERE user_id=? AND gedcom_id=?")
62			->execute(array($user_id, $WT_TREE->getTreeId()))
63			->fetchAll(PDO::FETCH_ASSOC);
64	}
65
66	/** {@inheritdoc} */
67	public function modAction($modAction) {
68		global $WT_TREE;
69
70		switch ($modAction) {
71		case 'menu-add-favorite':
72			// Process the "add to user favorites" menu item on indi/fam/etc. pages
73			$record = GedcomRecord::getInstance(Filter::post('xref', WT_REGEX_XREF), $WT_TREE);
74			if (Auth::check() && $record->canShowName()) {
75				self::addFavorite(array(
76					'user_id'   => Auth::id(),
77					'gedcom_id' => $record->getTree()->getTreeId(),
78					'gid'       => $record->getXref(),
79					'type'      => $record::RECORD_TYPE,
80					'url'       => null,
81					'note'      => null,
82					'title'     => null,
83				));
84				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()));
85			}
86			break;
87		}
88	}
89}
90