xref: /webtrees/app/Module/UserFavoritesModule.php (revision a6afca4cf33b4e58e45d8100e7533e97799b5214)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roach/**
38c2e8227SGreg Roach * webtrees: online genealogy
41062a142SGreg 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;
249807cf72SGreg Roachuse Fisharebest\Webtrees\Tree;
25*a6afca4cSGreg Roachuse Fisharebest\Webtrees\User;
268c2e8227SGreg Roachuse PDO;
278c2e8227SGreg Roach
288c2e8227SGreg Roach/**
298c2e8227SGreg Roach * Class UserFavoritesModule
308c2e8227SGreg Roach *
318c2e8227SGreg Roach * The "user favorites" module is almost identical to the "family tree favorites" module
328c2e8227SGreg Roach */
338c2e8227SGreg Roachclass UserFavoritesModule extends FamilyTreeFavoritesModule {
348c2e8227SGreg Roach	/** {@inheritdoc} */
358c2e8227SGreg Roach	public function getDescription() {
368c2e8227SGreg Roach		return /* I18N: Description of the “Favorites” module */ I18N::translate('Display and manage a user’s favorite pages.');
378c2e8227SGreg Roach	}
388c2e8227SGreg Roach
3976692c8bSGreg Roach	/**
4076692c8bSGreg Roach	 * Can this block be shown on the user’s home page?
4176692c8bSGreg Roach	 *
4276692c8bSGreg Roach	 * @return bool
4376692c8bSGreg Roach	 */
44a9430be8SGreg Roach	public function isUserBlock(): bool {
458c2e8227SGreg Roach		return true;
468c2e8227SGreg Roach	}
478c2e8227SGreg Roach
4876692c8bSGreg Roach	/**
4976692c8bSGreg Roach	 * Can this block be shown on the tree’s home page?
5076692c8bSGreg Roach	 *
5176692c8bSGreg Roach	 * @return bool
5276692c8bSGreg Roach	 */
53a9430be8SGreg Roach	public function isGedcomBlock(): bool {
548c2e8227SGreg Roach		return false;
558c2e8227SGreg Roach	}
568c2e8227SGreg Roach
578c2e8227SGreg Roach	/**
588c2e8227SGreg Roach	 * Get the favorites for a user (for the current family tree)
598c2e8227SGreg Roach	 *
609807cf72SGreg Roach	 * @param Tree $tree
61*a6afca4cSGreg Roach	 * @param User $user
628c2e8227SGreg Roach	 *
638c2e8227SGreg Roach	 * @return string[][]
648c2e8227SGreg Roach	 */
65*a6afca4cSGreg Roach	public static function getFavorites(Tree $tree, User $user) {
669807cf72SGreg Roach		$favorites =
678c2e8227SGreg Roach			Database::prepare(
689807cf72SGreg Roach				"SELECT SQL_CACHE favorite_id, user_id, gedcom_id, xref, favorite_type, title, note, url" .
699807cf72SGreg Roach				" FROM `##favorite` WHERE user_id = :user_id AND gedcom_id = :tree_id")
709807cf72SGreg Roach			->execute([
719807cf72SGreg Roach				'tree_id' => $tree->getTreeId(),
72*a6afca4cSGreg Roach				'user_id' => $user->getUserId(),
739807cf72SGreg Roach			])
749807cf72SGreg Roach			->fetchAll();
759807cf72SGreg Roach
769807cf72SGreg Roach		foreach ($favorites as $favorite) {
779807cf72SGreg Roach			$favorite->record = GedcomRecord::getInstance($favorite->xref, $tree);
789807cf72SGreg Roach		}
799807cf72SGreg Roach
809807cf72SGreg Roach		return $favorites;
818c2e8227SGreg Roach	}
828c2e8227SGreg Roach
8376692c8bSGreg Roach	/**
8476692c8bSGreg Roach	 * This is a general purpose hook, allowing modules to respond to routes
8576692c8bSGreg Roach	 * of the form module.php?mod=FOO&mod_action=BAR
8676692c8bSGreg Roach	 *
8776692c8bSGreg Roach	 * @param string $mod_action
8876692c8bSGreg Roach	 */
8976692c8bSGreg Roach	public function modAction($mod_action) {
9024ec66ceSGreg Roach		global $WT_TREE;
9124ec66ceSGreg Roach
9276692c8bSGreg Roach		switch ($mod_action) {
938c2e8227SGreg Roach			case 'menu-add-favorite':
948c2e8227SGreg Roach				// Process the "add to user favorites" menu item on indi/fam/etc. pages
9524ec66ceSGreg Roach				$record = GedcomRecord::getInstance(Filter::post('xref', WT_REGEX_XREF), $WT_TREE);
968c2e8227SGreg Roach				if (Auth::check() && $record->canShowName()) {
9713abd6f3SGreg Roach					self::addFavorite([
988c2e8227SGreg Roach						'user_id'   => Auth::id(),
998c2e8227SGreg Roach						'gedcom_id' => $record->getTree()->getTreeId(),
1008c2e8227SGreg Roach						'gid'       => $record->getXref(),
1018c2e8227SGreg Roach						'type'      => $record::RECORD_TYPE,
1028c2e8227SGreg Roach						'url'       => null,
1038c2e8227SGreg Roach						'note'      => null,
1048c2e8227SGreg Roach						'title'     => null,
10513abd6f3SGreg Roach					]);
1068c2e8227SGreg 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()));
1078c2e8227SGreg Roach				}
1088c2e8227SGreg Roach				break;
1098c2e8227SGreg Roach		}
1108c2e8227SGreg Roach	}
1118c2e8227SGreg Roach}
112