xref: /webtrees/app/Module/UserFavoritesModule.php (revision 1062a1429914c995339f502856821457aa975a5a)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
4*1062a142SGreg Roach * Copyright (C) 2018 webtrees development team
58c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
68c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
78c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
88c2e8227SGreg Roach * (at your option) any later version.
98c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
108c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
118c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128c2e8227SGreg Roach * GNU General Public License for more details.
138c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
148c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
158c2e8227SGreg Roach */
1676692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
178c2e8227SGreg Roach
180e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
190e62c4b8SGreg Roachuse Fisharebest\Webtrees\Database;
200e62c4b8SGreg Roachuse Fisharebest\Webtrees\Filter;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
230e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
248c2e8227SGreg Roachuse PDO;
258c2e8227SGreg Roach
268c2e8227SGreg Roach/**
278c2e8227SGreg Roach * Class UserFavoritesModule
288c2e8227SGreg Roach *
298c2e8227SGreg Roach * The "user favorites" module is almost identical to the "family tree favorites" module
308c2e8227SGreg Roach */
318c2e8227SGreg Roachclass UserFavoritesModule extends FamilyTreeFavoritesModule {
328c2e8227SGreg Roach	/** {@inheritdoc} */
338c2e8227SGreg Roach	public function getDescription() {
348c2e8227SGreg Roach		return /* I18N: Description of the “Favorites” module */ I18N::translate('Display and manage a user’s favorite pages.');
358c2e8227SGreg Roach	}
368c2e8227SGreg Roach
3776692c8bSGreg Roach	/**
3876692c8bSGreg Roach	 * Can this block be shown on the user’s home page?
3976692c8bSGreg Roach	 *
4076692c8bSGreg Roach	 * @return bool
4176692c8bSGreg Roach	 */
42a9430be8SGreg Roach	public function isUserBlock(): bool {
438c2e8227SGreg Roach		return true;
448c2e8227SGreg Roach	}
458c2e8227SGreg Roach
4676692c8bSGreg Roach	/**
4776692c8bSGreg Roach	 * Can this block be shown on the tree’s home page?
4876692c8bSGreg Roach	 *
4976692c8bSGreg Roach	 * @return bool
5076692c8bSGreg Roach	 */
51a9430be8SGreg Roach	public function isGedcomBlock(): bool {
528c2e8227SGreg Roach		return false;
538c2e8227SGreg Roach	}
548c2e8227SGreg Roach
558c2e8227SGreg Roach	/**
568c2e8227SGreg Roach	 * Get the favorites for a user (for the current family tree)
578c2e8227SGreg Roach	 *
58cbc1590aSGreg Roach	 * @param int $user_id
598c2e8227SGreg Roach	 *
608c2e8227SGreg Roach	 * @return string[][]
618c2e8227SGreg Roach	 */
628c2e8227SGreg Roach	public static function getFavorites($user_id) {
6324ec66ceSGreg Roach		global $WT_TREE;
6424ec66ceSGreg Roach
658c2e8227SGreg Roach		return
668c2e8227SGreg Roach			Database::prepare(
678c2e8227SGreg Roach				"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" .
688c2e8227SGreg Roach				" FROM `##favorite` WHERE user_id=? AND gedcom_id=?")
6913abd6f3SGreg Roach			->execute([$user_id, $WT_TREE->getTreeId()])
708c2e8227SGreg Roach			->fetchAll(PDO::FETCH_ASSOC);
718c2e8227SGreg Roach	}
728c2e8227SGreg Roach
7376692c8bSGreg Roach	/**
7476692c8bSGreg Roach	 * This is a general purpose hook, allowing modules to respond to routes
7576692c8bSGreg Roach	 * of the form module.php?mod=FOO&mod_action=BAR
7676692c8bSGreg Roach	 *
7776692c8bSGreg Roach	 * @param string $mod_action
7876692c8bSGreg Roach	 */
7976692c8bSGreg Roach	public function modAction($mod_action) {
8024ec66ceSGreg Roach		global $WT_TREE;
8124ec66ceSGreg Roach
8276692c8bSGreg Roach		switch ($mod_action) {
838c2e8227SGreg Roach			case 'menu-add-favorite':
848c2e8227SGreg Roach				// Process the "add to user favorites" menu item on indi/fam/etc. pages
8524ec66ceSGreg Roach				$record = GedcomRecord::getInstance(Filter::post('xref', WT_REGEX_XREF), $WT_TREE);
868c2e8227SGreg Roach				if (Auth::check() && $record->canShowName()) {
8713abd6f3SGreg Roach					self::addFavorite([
888c2e8227SGreg Roach						'user_id'   => Auth::id(),
898c2e8227SGreg Roach						'gedcom_id' => $record->getTree()->getTreeId(),
908c2e8227SGreg Roach						'gid'       => $record->getXref(),
918c2e8227SGreg Roach						'type'      => $record::RECORD_TYPE,
928c2e8227SGreg Roach						'url'       => null,
938c2e8227SGreg Roach						'note'      => null,
948c2e8227SGreg Roach						'title'     => null,
9513abd6f3SGreg Roach					]);
968c2e8227SGreg Roach					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()));
978c2e8227SGreg Roach				}
988c2e8227SGreg Roach				break;
998c2e8227SGreg Roach		}
1008c2e8227SGreg Roach	}
1018c2e8227SGreg Roach}
102