xref: /webtrees/app/Module/FamilyTreeFavoritesModule.php (revision 24ec66ce7e77188cd2495b0f8d4dd0ae6e8c9c52)
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 Module 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 = get_block_setting($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			return Theme::theme()->formatBlock($id, $title, $class, $content);
199		} else {
200			return $content;
201		}
202	}
203
204	/** {@inheritdoc} */
205	public function loadAjax() {
206		return false;
207	}
208
209	/** {@inheritdoc} */
210	public function isUserBlock() {
211		return false;
212	}
213
214	/** {@inheritdoc} */
215	public function isGedcomBlock() {
216		return true;
217	}
218
219	/** {@inheritdoc} */
220	public function configureBlock($block_id) {
221		if (Filter::postBool('save') && Filter::checkCsrf()) {
222			set_block_setting($block_id, 'block', Filter::postBool('block'));
223		}
224
225		$block = get_block_setting($block_id, 'block', '0');
226
227		echo '<tr><td class="descriptionbox wrap width33">';
228		echo /* I18N: label for a yes/no option */ I18N::translate('Add a scrollbar when block contents grow');
229		echo '</td><td class="optionbox">';
230		echo edit_field_yes_no('block', $block);
231		echo '</td></tr>';
232	}
233
234	/**
235	 * Delete a favorite from the database
236	 *
237	 * @param integer $favorite_id
238	 *
239	 * @return boolean
240	 */
241	public static function deleteFavorite($favorite_id) {
242		return (bool)
243			Database::prepare("DELETE FROM `##favorite` WHERE favorite_id=?")
244			->execute(array($favorite_id));
245	}
246
247	/**
248	 * Store a new favorite in the database
249	 *
250	 * @param $favorite
251	 *
252	 * @return boolean
253	 */
254	public static function addFavorite($favorite) {
255		// -- make sure a favorite is added
256		if (empty($favorite['gid']) && empty($favorite['url'])) {
257			return false;
258		}
259
260		//-- make sure this is not a duplicate entry
261		$sql = "SELECT SQL_NO_CACHE 1 FROM `##favorite` WHERE";
262		if (!empty($favorite['gid'])) {
263			$sql .= " xref=?";
264			$vars = array($favorite['gid']);
265		} else {
266			$sql .= " url=?";
267			$vars = array($favorite['url']);
268		}
269		$sql .= " AND gedcom_id=?";
270		$vars[] = $favorite['gedcom_id'];
271		if ($favorite['user_id']) {
272			$sql .= " AND user_id=?";
273			$vars[] = $favorite['user_id'];
274		} else {
275			$sql .= " AND user_id IS NULL";
276		}
277
278		if (Database::prepare($sql)->execute($vars)->fetchOne()) {
279			return false;
280		}
281
282		//-- add the favorite to the database
283		return (bool)
284			Database::prepare("INSERT INTO `##favorite` (user_id, gedcom_id, xref, favorite_type, url, title, note) VALUES (? ,? ,? ,? ,? ,? ,?)")
285				->execute(array($favorite['user_id'], $favorite['gedcom_id'], $favorite['gid'], $favorite['type'], $favorite['url'], $favorite['title'], $favorite['note']));
286	}
287
288	/**
289	 * Get favorites for a user or family tree
290	 *
291	 * @param integer $gedcom_id
292	 *
293	 * @return string[][]
294	 */
295	public static function getFavorites($gedcom_id) {
296		self::updateSchema(); // make sure the favorites table has been created
297
298		return
299			Database::prepare(
300				"SELECT SQL_CACHE favorite_id AS id, user_id, gedcom_id, xref AS gid, favorite_type AS type, title, note, url" .
301				" FROM `##favorite` WHERE gedcom_id=? AND user_id IS NULL")
302			->execute(array($gedcom_id))
303			->fetchAll(PDO::FETCH_ASSOC);
304	}
305
306	/**
307	 * Make sure the database structure is up-to-date.
308	 */
309	protected static function updateSchema() {
310		// Create tables, if not already present
311		try {
312			Database::updateSchema(WT_ROOT . WT_MODULES_DIR . 'gedcom_favorites/db_schema/', 'FV_SCHEMA_VERSION', 4);
313		} catch (PDOException $ex) {
314			// The schema update scripts should never fail.  If they do, there is no clean recovery.
315			FlashMessages::addMessage($ex->getMessage(), 'danger');
316			header('Location: ' . WT_BASE_URL . 'site-unavailable.php');
317			throw $ex;
318		}
319	}
320}
321