xref: /webtrees/app/Module/UserFavoritesModule.php (revision cbc1590a8c715aa2d88bd745610b899587bd9563)
18c2e8227SGreg Roach<?php
28c2e8227SGreg Roachnamespace Fisharebest\Webtrees;
38c2e8227SGreg Roach
48c2e8227SGreg Roach/**
58c2e8227SGreg Roach * webtrees: online genealogy
68c2e8227SGreg Roach * Copyright (C) 2015 webtrees development team
78c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
88c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
98c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
108c2e8227SGreg Roach * (at your option) any later version.
118c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
128c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
138c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
148c2e8227SGreg Roach * GNU General Public License for more details.
158c2e8227SGreg Roach * You should have received a copy of the GNU General Public License
168c2e8227SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
178c2e8227SGreg Roach */
188c2e8227SGreg Roach
198c2e8227SGreg Roachuse PDO;
208c2e8227SGreg Roach
218c2e8227SGreg Roach/**
228c2e8227SGreg Roach * Class UserFavoritesModule
238c2e8227SGreg Roach *
248c2e8227SGreg Roach * The "user favorites" module is almost identical to the "family tree favorites" module
258c2e8227SGreg Roach */
268c2e8227SGreg Roachclass UserFavoritesModule extends FamilyTreeFavoritesModule {
278c2e8227SGreg Roach	/** {@inheritdoc} */
288c2e8227SGreg Roach	public function getDescription() {
298c2e8227SGreg Roach		return /* I18N: Description of the “Favorites” module */ I18N::translate('Display and manage a user’s favorite pages.');
308c2e8227SGreg Roach	}
318c2e8227SGreg Roach
328c2e8227SGreg Roach	/** {@inheritdoc} */
338c2e8227SGreg Roach	public function isUserBlock() {
348c2e8227SGreg Roach		return true;
358c2e8227SGreg Roach	}
368c2e8227SGreg Roach
378c2e8227SGreg Roach	/** {@inheritdoc} */
388c2e8227SGreg Roach	public function isGedcomBlock() {
398c2e8227SGreg Roach		return false;
408c2e8227SGreg Roach	}
418c2e8227SGreg Roach
428c2e8227SGreg Roach	/**
438c2e8227SGreg Roach	 * Get the favorites for a user (for the current family tree)
448c2e8227SGreg Roach	 *
45*cbc1590aSGreg Roach	 * @param int $user_id
468c2e8227SGreg Roach	 *
478c2e8227SGreg Roach	 * @return string[][]
488c2e8227SGreg Roach	 */
498c2e8227SGreg Roach	public static function getFavorites($user_id) {
5024ec66ceSGreg Roach		global $WT_TREE;
5124ec66ceSGreg Roach
528c2e8227SGreg Roach		self::updateSchema(); // make sure the favorites table has been created
538c2e8227SGreg Roach
548c2e8227SGreg Roach		return
558c2e8227SGreg Roach			Database::prepare(
568c2e8227SGreg 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" .
578c2e8227SGreg Roach				" FROM `##favorite` WHERE user_id=? AND gedcom_id=?")
5824ec66ceSGreg Roach			->execute(array($user_id, $WT_TREE->getTreeId()))
598c2e8227SGreg Roach			->fetchAll(PDO::FETCH_ASSOC);
608c2e8227SGreg Roach	}
618c2e8227SGreg Roach
628c2e8227SGreg Roach	/** {@inheritdoc} */
638c2e8227SGreg Roach	public function modAction($modAction) {
6424ec66ceSGreg Roach		global $WT_TREE;
6524ec66ceSGreg Roach
668c2e8227SGreg Roach		switch ($modAction) {
678c2e8227SGreg Roach		case 'menu-add-favorite':
688c2e8227SGreg Roach			// Process the "add to user favorites" menu item on indi/fam/etc. pages
6924ec66ceSGreg Roach			$record = GedcomRecord::getInstance(Filter::post('xref', WT_REGEX_XREF), $WT_TREE);
708c2e8227SGreg Roach			if (Auth::check() && $record->canShowName()) {
718c2e8227SGreg Roach				self::addFavorite(array(
728c2e8227SGreg Roach					'user_id'   => Auth::id(),
738c2e8227SGreg Roach					'gedcom_id' => $record->getTree()->getTreeId(),
748c2e8227SGreg Roach					'gid'       => $record->getXref(),
758c2e8227SGreg Roach					'type'      => $record::RECORD_TYPE,
768c2e8227SGreg Roach					'url'       => null,
778c2e8227SGreg Roach					'note'      => null,
788c2e8227SGreg Roach					'title'     => null,
798c2e8227SGreg Roach				));
808c2e8227SGreg 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()));
818c2e8227SGreg Roach			}
828c2e8227SGreg Roach			break;
838c2e8227SGreg Roach		}
848c2e8227SGreg Roach	}
858c2e8227SGreg Roach}
86