xref: /webtrees/app/Module/UserFavoritesModule.php (revision e5588fb099ce26dcdcb7faeb13861818cd9a4306)
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\FlashMessages;
210e62c4b8SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
239807cf72SGreg Roachuse Fisharebest\Webtrees\Tree;
24a6afca4cSGreg Roachuse Fisharebest\Webtrees\User;
258840c547SGreg Roachuse Symfony\Component\HttpFoundation\Request;
268840c547SGreg Roachuse Symfony\Component\HttpFoundation\Response;
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
61a6afca4cSGreg Roach	 * @param User $user
628c2e8227SGreg Roach	 *
638c2e8227SGreg Roach	 * @return string[][]
648c2e8227SGreg Roach	 */
65a6afca4cSGreg Roach	public static function getFavorites(Tree $tree, User $user) {
669807cf72SGreg Roach		$favorites =
678c2e8227SGreg Roach			Database::prepare(
68*e5588fb0SGreg Roach				"SELECT 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(),
72a6afca4cSGreg 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	/**
848840c547SGreg Roach	 * @param Request $request
8576692c8bSGreg Roach	 *
868840c547SGreg Roach	 * @return Response
8776692c8bSGreg Roach	 */
888840c547SGreg Roach	public function postAddFavoriteAction(Request $request): Response {
898840c547SGreg Roach		/** @var Tree $tree */
908840c547SGreg Roach		$tree = $request->attributes->get('tree');
9124ec66ceSGreg Roach
928840c547SGreg Roach		$xref = $request->get('xref');
938840c547SGreg Roach
948840c547SGreg Roach		$record = GedcomRecord::getInstance($xref, $tree);
958840c547SGreg Roach
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			]);
1068840c547SGreg Roach
1078c2e8227SGreg 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()));
1088c2e8227SGreg Roach		}
1098840c547SGreg Roach
1108840c547SGreg Roach		return new Response;
1118c2e8227SGreg Roach	}
1128c2e8227SGreg Roach}
113