xref: /webtrees/app/Module/FamilyTreeFavoritesModule.php (revision cbc1590a8c715aa2d88bd745610b899587bd9563)
1<?php
2namespace Fisharebest\Webtrees;
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 PDO;
20use PDOException;
21use Rhumsaa\Uuid\Uuid;
22
23/**
24 * Class FamilyTreeFavoritesModule
25 *
26 * Note that the user favorites module simply extends this module, so ensure that the
27 * logic works for both.
28 */
29class FamilyTreeFavoritesModule extends AbstractModule implements ModuleBlockInterface {
30	/** {@inheritdoc} */
31	public function getTitle() {
32		return /* I18N: Name of a module */ I18N::translate('Favorites');
33	}
34
35	/** {@inheritdoc} */
36	public function getDescription() {
37		return /* I18N: Description of the “Favorites” module */ I18N::translate('Display and manage a family tree’s favorite pages.');
38	}
39
40	/** {@inheritdoc} */
41	public function getBlock($block_id, $template = true, $cfg = null) {
42		global $ctype, $controller, $WT_TREE;
43
44		self::updateSchema(); // make sure the favorites table has been created
45
46		$action = Filter::get('action');
47		switch ($action) {
48		case 'deletefav':
49			$favorite_id = Filter::getInteger('favorite_id');
50			if ($favorite_id) {
51				self::deleteFavorite($favorite_id);
52			}
53			break;
54		case 'addfav':
55			$gid      = Filter::get('gid', WT_REGEX_XREF);
56			$favnote  = Filter::get('favnote');
57			$url      = Filter::getUrl('url');
58			$favtitle = Filter::get('favtitle');
59
60			if ($gid) {
61				$record = GedcomRecord::getInstance($gid, $WT_TREE);
62				if ($record && $record->canShow()) {
63					self::addFavorite(array(
64						'user_id'   => $ctype === 'user' ? Auth::id() : null,
65						'gedcom_id' => $WT_TREE->getTreeId(),
66						'gid'       => $record->getXref(),
67						'type'      => $record::RECORD_TYPE,
68						'url'       => null,
69						'note'      => $favnote,
70						'title'     => $favtitle,
71					));
72				}
73			} elseif ($url) {
74				self::addFavorite(array(
75					'user_id'   => $ctype === 'user' ? Auth::id() : null,
76					'gedcom_id' => $WT_TREE->getTreeId(),
77					'gid'       => null,
78					'type'      => 'URL',
79					'url'       => $url,
80					'note'      => $favnote,
81					'title'     => $favtitle ? $favtitle : $url,
82				));
83			}
84			break;
85		}
86
87		$block = $this->getBlockSetting($block_id, 'block', '0');
88
89		if ($cfg) {
90			foreach (array('block') as $name) {
91				if (array_key_exists($name, $cfg)) {
92					$$name = $cfg[$name];
93				}
94			}
95		}
96
97		$userfavs = $this->getFavorites($ctype === 'user' ? Auth::id() : $WT_TREE->getTreeId());
98		if (!is_array($userfavs)) {
99			$userfavs = array();
100		}
101
102		$id    = $this->getName() . $block_id;
103		$class = $this->getName() . '_block';
104		$title = $this->getTitle();
105
106		if (Auth::check()) {
107			$controller
108				->addExternalJavascript(WT_AUTOCOMPLETE_JS_URL)
109				->addInlineJavascript('autocomplete();');
110		}
111
112		$content = '';
113		if ($userfavs) {
114			foreach ($userfavs as $key => $favorite) {
115				if (isset($favorite['id'])) {
116					$key = $favorite['id'];
117				}
118				$removeFavourite = '<a class="font9" href="index.php?ctype=' . $ctype . '&amp;action=deletefav&amp;favorite_id=' . $key . '" onclick="return confirm(\'' . I18N::translate('Are you sure you want to remove this item from your list of favorites?') . '\');">' . I18N::translate('Remove') . '</a> ';
119				if ($favorite['type'] == 'URL') {
120					$content .= '<div id="boxurl' . $key . '.0" class="person_box">';
121					if ($ctype == 'user' || Auth::isManager($WT_TREE)) {
122						$content .= $removeFavourite;
123					}
124					$content .= '<a href="' . $favorite['url'] . '"><b>' . $favorite['title'] . '</b></a>';
125					$content .= '<br>' . $favorite['note'];
126					$content .= '</div>';
127				} else {
128					$record = GedcomRecord::getInstance($favorite['gid'], $WT_TREE);
129					if ($record && $record->canShow()) {
130						if ($record instanceof Individual) {
131							$content .= '<div id="box' . $favorite["gid"] . '.0" class="person_box action_header';
132							switch ($record->getsex()) {
133							case 'M':
134								break;
135							case 'F':
136								$content .= 'F';
137								break;
138							default:
139								$content .= 'NN';
140								break;
141							}
142							$content .= '">';
143							if ($ctype == "user" || Auth::isManager($WT_TREE)) {
144								$content .= $removeFavourite;
145							}
146							$content .= Theme::theme()->individualBoxLarge($record);
147							$content .= $favorite['note'];
148							$content .= '</div>';
149						} else {
150							$content .= '<div id="box' . $favorite['gid'] . '.0" class="person_box">';
151							if ($ctype == 'user' || Auth::isManager($WT_TREE)) {
152								$content .= $removeFavourite;
153							}
154							$content .= $record->formatList('span');
155							$content .= '<br>' . $favorite['note'];
156							$content .= '</div>';
157						}
158					}
159				}
160			}
161		}
162		if ($ctype == 'user' || Auth::isManager($WT_TREE)) {
163			$uniqueID = Uuid::uuid4(); // This block can theoretically appear multiple times, so use a unique ID.
164			$content .= '<div class="add_fav_head">';
165			$content .= '<a href="#" onclick="return expand_layer(\'add_fav' . $uniqueID . '\');">' . I18N::translate('Add a new favorite') . '<i id="add_fav' . $uniqueID . '_img" class="icon-plus"></i></a>';
166			$content .= '</div>';
167			$content .= '<div id="add_fav' . $uniqueID . '" style="display: none;">';
168			$content .= '<form name="addfavform" method="get" action="index.php">';
169			$content .= '<input type="hidden" name="action" value="addfav">';
170			$content .= '<input type="hidden" name="ctype" value="' . $ctype . '">';
171			$content .= '<input type="hidden" name="ged" value="' . $WT_TREE->getNameHtml() . '">';
172			$content .= '<div class="add_fav_ref">';
173			$content .= '<input type="radio" name="fav_category" value="record" checked onclick="jQuery(\'#gid' . $uniqueID . '\').removeAttr(\'disabled\'); jQuery(\'#url, #favtitle\').attr(\'disabled\',\'disabled\').val(\'\');">';
174			$content .= '<label for="gid' . $uniqueID . '">' . I18N::translate('Enter an individual, family, or source ID') . '</label>';
175			$content .= '<input class="pedigree_form" data-autocomplete-type="IFSRO" type="text" name="gid" id="gid' . $uniqueID . '" size="5" value="">';
176			$content .= ' ' . print_findindi_link('gid' . $uniqueID);
177			$content .= ' ' . print_findfamily_link('gid' . $uniqueID);
178			$content .= ' ' . print_findsource_link('gid' . $uniqueID);
179			$content .= ' ' . print_findrepository_link('gid' . $uniqueID);
180			$content .= ' ' . print_findnote_link('gid' . $uniqueID);
181			$content .= ' ' . print_findmedia_link('gid' . $uniqueID);
182			$content .= '</div>';
183			$content .= '<div class="add_fav_url">';
184			$content .= '<input type="radio" name="fav_category" value="url" onclick="jQuery(\'#url, #favtitle\').removeAttr(\'disabled\'); jQuery(\'#gid' . $uniqueID . '\').attr(\'disabled\',\'disabled\').val(\'\');">';
185			$content .= '<input type="text" name="url" id="url" size="20" value="" placeholder="' . GedcomTag::getLabel('URL') . '" disabled> ';
186			$content .= '<input type="text" name="favtitle" id="favtitle" size="20" value="" placeholder="' . I18N::translate('Title') . '" disabled>';
187			$content .= '<p>' . I18N::translate('Enter an optional note about this favorite') . '</p>';
188			$content .= '<textarea name="favnote" rows="6" cols="50"></textarea>';
189			$content .= '</div>';
190			$content .= '<input type="submit" value="' . I18N::translate('Add') . '">';
191			$content .= '</form></div>';
192		}
193
194		if ($template) {
195			if ($block) {
196				$class .= ' small_inner_block';
197			}
198
199			return Theme::theme()->formatBlock($id, $title, $class, $content);
200		} else {
201			return $content;
202		}
203	}
204
205	/** {@inheritdoc} */
206	public function loadAjax() {
207		return false;
208	}
209
210	/** {@inheritdoc} */
211	public function isUserBlock() {
212		return false;
213	}
214
215	/** {@inheritdoc} */
216	public function isGedcomBlock() {
217		return true;
218	}
219
220	/** {@inheritdoc} */
221	public function configureBlock($block_id) {
222		if (Filter::postBool('save') && Filter::checkCsrf()) {
223			$this->setBlockSetting($block_id, 'block', Filter::postBool('block'));
224		}
225
226		$block = $this->getBlockSetting($block_id, 'block', '0');
227
228		echo '<tr><td class="descriptionbox wrap width33">';
229		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
230		echo '</td><td class="optionbox">';
231		echo edit_field_yes_no('block', $block);
232		echo '</td></tr>';
233	}
234
235	/**
236	 * Delete a favorite from the database
237	 *
238	 * @param int $favorite_id
239	 *
240	 * @return bool
241	 */
242	public static function deleteFavorite($favorite_id) {
243		return (bool)
244			Database::prepare("DELETE FROM `##favorite` WHERE favorite_id=?")
245			->execute(array($favorite_id));
246	}
247
248	/**
249	 * Store a new favorite in the database
250	 *
251	 * @param $favorite
252	 *
253	 * @return bool
254	 */
255	public static function addFavorite($favorite) {
256		// -- make sure a favorite is added
257		if (empty($favorite['gid']) && empty($favorite['url'])) {
258			return false;
259		}
260
261		//-- make sure this is not a duplicate entry
262		$sql = "SELECT SQL_NO_CACHE 1 FROM `##favorite` WHERE";
263		if (!empty($favorite['gid'])) {
264			$sql .= " xref=?";
265			$vars = array($favorite['gid']);
266		} else {
267			$sql .= " url=?";
268			$vars = array($favorite['url']);
269		}
270		$sql .= " AND gedcom_id=?";
271		$vars[] = $favorite['gedcom_id'];
272		if ($favorite['user_id']) {
273			$sql .= " AND user_id=?";
274			$vars[] = $favorite['user_id'];
275		} else {
276			$sql .= " AND user_id IS NULL";
277		}
278
279		if (Database::prepare($sql)->execute($vars)->fetchOne()) {
280			return false;
281		}
282
283		//-- add the favorite to the database
284		return (bool)
285			Database::prepare("INSERT INTO `##favorite` (user_id, gedcom_id, xref, favorite_type, url, title, note) VALUES (? ,? ,? ,? ,? ,? ,?)")
286				->execute(array($favorite['user_id'], $favorite['gedcom_id'], $favorite['gid'], $favorite['type'], $favorite['url'], $favorite['title'], $favorite['note']));
287	}
288
289	/**
290	 * Get favorites for a user or family tree
291	 *
292	 * @param int $gedcom_id
293	 *
294	 * @return string[][]
295	 */
296	public static function getFavorites($gedcom_id) {
297		self::updateSchema(); // make sure the favorites table has been created
298
299		return
300			Database::prepare(
301				"SELECT SQL_CACHE favorite_id AS id, user_id, gedcom_id, xref AS gid, favorite_type AS type, title, note, url" .
302				" FROM `##favorite` WHERE gedcom_id=? AND user_id IS NULL")
303			->execute(array($gedcom_id))
304			->fetchAll(PDO::FETCH_ASSOC);
305	}
306
307	/**
308	 * Make sure the database structure is up-to-date.
309	 */
310	protected static function updateSchema() {
311		// Create tables, if not already present
312		try {
313			Database::updateSchema(WT_ROOT . WT_MODULES_DIR . 'gedcom_favorites/db_schema/', 'FV_SCHEMA_VERSION', 4);
314		} catch (PDOException $ex) {
315			// The schema update scripts should never fail.  If they do, there is no clean recovery.
316			FlashMessages::addMessage($ex->getMessage(), 'danger');
317			header('Location: ' . WT_BASE_URL . 'site-unavailable.php');
318			throw $ex;
319		}
320	}
321}
322