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