forked from tillsc/GVExport
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodule.php
452 lines (407 loc) · 15.6 KB
/
module.php
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
/**
* GraphViz module for Webtrees
*
* Ported to Webtrees by Iain MacDonald <[email protected]>
*/
// Classes and libraries for module system
//
// webtrees: Web based Family History software
// Copyright (C) 2012 webtrees development team.
//
// Derived from PhpGedView
// Copyright (C) 2010 John Finlay
//
// 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 2 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, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
namespace vendor\WebtreesModules\gvexport;
require_once dirname(__FILE__) . "/config.php";
require_once dirname(__FILE__) . "/app/functionsClippingsCart.php";
// Auto-load class files
spl_autoload_register(function ($class) {
if (strpos($class, "\gvexport\\")) {
$name = basename(dirname(__FILE__) . "/app/" . str_replace('\\', '/',$class . '.php'));
include dirname(__FILE__) . "/app/" . $name;
}
});
use Exception;
use Fig\Http\Message\StatusCodeInterface;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Contracts\UserInterface;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Localization\Translation;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Module\AbstractModule;
use Fisharebest\Webtrees\Module\ModuleCustomInterface;
use Fisharebest\Webtrees\Module\ModuleChartInterface;
use Fisharebest\Webtrees\Module\ModuleCustomTrait;
use Fisharebest\Webtrees\Module\ModuleChartTrait;
use Fisharebest\Webtrees\Module\ModuleConfigInterface;
use Fisharebest\Webtrees\Module\ModuleConfigTrait;
use Fisharebest\Webtrees\FlashMessages;
use Fisharebest\Webtrees\Menu;
use Fisharebest\Webtrees\Services\ModuleService;
use Fisharebest\Webtrees\Validator;
use Fisharebest\Webtrees\View;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\Webtrees;
use JsonException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
/**
* Main class for GVExport module
*/
class GVExport extends AbstractModule implements ModuleCustomInterface, ModuleChartInterface, ModuleConfigInterface
{
use ModuleCustomTrait;
use ModuleChartTrait;
use ModuleConfigTrait;
public const CUSTOM_VERSION = '2.2.2';
public const CUSTOM_MODULE = "GVExport";
public const CUSTOM_LATEST = 'https://raw.githubusercontent.com/Neriderc/' . self::CUSTOM_MODULE. '/main/latest-version.txt';
public const SUPPORT_URL = 'https://github.com/Neriderc/GVExport';
public string $base_url;
public ModuleService $module_service;
/**
*
* @param ModuleService $module_service
*/
public function __construct(ModuleService $module_service)
{
$this->module_service = $module_service;
}
public function boot(): void
{
// Register a namespace for our views.
View::registerNamespace($this->name(), $this->resourcesFolder() . 'views/');
}
public function resourcesFolder(): string
{
return __DIR__ . '/resources/';
}
public function title(): string
{
return 'GVExport';
}
public function description(): string
{
return 'This is the "GVExport" module';
}
public function chartMenuClass(): string
{
return 'menu-chart-familybook';
}
public function chartBoxMenu(Individual $individual): ?Menu
{
return $this->chartMenu($individual);
}
public function chartUrl(Individual $individual, array $parameters = []): string
{
return route('module', array_merge($parameters, [
'module' => $this->name(),
'action' => 'Chart',
'xref' => $individual->xref(),
'tree' => $individual->tree()->name(),
]));
}
/**
* The version of this module.
*
* @return string
*/
public function customModuleVersion(): string
{
return self::CUSTOM_VERSION;
}
/**
* A URL that will provide the latest version of this module.
*
* @return string
*/
public function customModuleLatestVersionUrl(): string
{
return self::CUSTOM_LATEST;
}
/**
* Where to get support for this module.
*
* @return string
*/
public function customModuleSupportUrl(): string
{
return self::SUPPORT_URL;
}
public function getIndividual($tree, $xref): Individual
{
$individual = Registry::individualFactory()->make($xref, $tree);
return Auth::checkIndividualAccess($individual, false, true);
}
/**
* @throws Exception
*/
public function getChartAction(ServerRequestInterface $request): ResponseInterface
{
$tree = $request->getAttribute('tree');
Auth::checkComponentAccess($this, ModuleChartInterface::class, $tree, Auth::user());
assert($tree instanceof Tree);
if (isset($request->getQueryParams()['xref'])) {
$xref = $request->getQueryParams()['xref'];
} else {
$xref = $tree->getUserPreference(Auth::user(), UserInterface::PREF_TREE_ACCOUNT_XREF);
}
$individual = $this->getIndividual($tree, $tree->significantIndividual(Auth::user(), $xref)->xref());
$userDefaultVars = (new Settings($tree))->getAdminSettings($this);
$settings = new Settings($tree);
$userDefaultVars['first_render'] = true;
if (isset($_REQUEST['reset'])){
if (!$userDefaultVars['enable_graphviz'] && $userDefaultVars['graphviz_bin'] != "") {
$userDefaultVars['graphviz_bin'] = "";
}
$userDefaultVars['first_render'] = false; // Allow settings to be overwritten by defaults
} else if (isset($_REQUEST['t'])) {
try {
if (ctype_alnum($_REQUEST['t'])) {
$this->base_url = $this->strip_param_from_url($this->chartUrl($individual), 'xref');
$tokenSettings = $settings->loadSettingsToken($this, $tree, $_REQUEST['t']);
foreach ($tokenSettings as $key => $value) {
$userDefaultVars[$key] = $value;
}
} else {
throw new Exception("Invalid token");
}
} catch (Exception $e) {
$userDefaultVars = $settings->loadUserSettings($this, $tree);
}
} else {
// Load settings from webtrees
$userDefaultVars = $settings->loadUserSettings($this, $tree);
}
$otypes = $this->getOTypes($userDefaultVars);
if (!isset($userDefaultVars['first_render'])) {
$userDefaultVars['first_render'] = true;
}
return $this->viewResponse($this->name() . '::page', [
'tree' => $tree,
'individual' => $individual,
'title' => 'GVExport',
'vars' => $userDefaultVars,
'otypes' => $otypes,
'cartempty' => !functionsClippingsCart::isIndividualInCart($tree),
'module' => $this
]);
}
/**
* Where are the Javascript functions for this module stored?
*
* @return ResponseInterface
*
* @throws JsonException
*/
public function getJSAction() : ResponseInterface
{
return response(
file_get_contents($this->resourcesFolder() . 'javascript' . DIRECTORY_SEPARATOR . 'gvexport.js'),
200,
['content-type' => 'text/javascript']
);
}
public function postChartAction(ServerRequestInterface $request): ResponseInterface
{
$tree = $request->getAttribute('tree');
if (isset($_POST['json_data'])) {
$individual = $this->getIndividual($tree, $tree->significantIndividual(Auth::user(), '')->xref());
$this->base_url = $this->strip_param_from_url($this->chartUrl($individual), 'xref');
$api = new ApiHandler($request, $this, $tree);
return $api->handle();
} else {
$vars_data = Validator::parsedBody($request)->array('vars');
try {
$temp_dir = $this->saveDOTFile($tree, $vars_data);
} catch (Exception $e) {
// Remove comments around $e below to get a proper error message. Ensure full error is not showing for prod.
return Registry::responseFactory()->response('Failed to generate file' /* . $e */, StatusCodeInterface::STATUS_NOT_ACCEPTABLE);
}
// If browser mode, output dot instead of selected file
$file_type = isset($_POST["browser"]) && $_POST["browser"] == "true" ? "dot" : $vars_data["output_type"];
$outputFile = new OutputFile($temp_dir, $file_type, $this);
return $outputFile->downloadFile();
}
}
/**
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
public function getAdminAction(ServerRequestInterface $request): ResponseInterface
{
$this->layout = 'layouts/administration';
$otypes = $this->getOTypes((new Settings())->getAdminSettings($this));
$response['module'] = $this;
$response['otypes'] = $otypes;
if (isset($_REQUEST['reset']) && $_REQUEST['reset'] === "1") {
$response['vars'] = (new Settings())->getDefaultSettings();
} else {
$response['vars'] = (new Settings())->getAdminSettings($this);
}
$response['title'] = $this->title();
return $this->viewResponse($this->name() . '::' . 'settings', $response);
}
/**
* save the user preferences in the database
*
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
public function postAdminAction(ServerRequestInterface $request): ResponseInterface
{
$params = (array) $request->getParsedBody();
$formSubmission = new FormSubmission();
$vars_data = Validator::parsedBody($request)->array('vars');
$vars = $formSubmission->load($vars_data, $this);
if ($params['save'] === '1') {
(new Settings())->saveAdminSettings($this, $vars);
FlashMessages::addMessage(I18N::translate('The preferences for the module “%s” have been updated.',
$this->title()), 'success');
}
return redirect($this->getConfigLink());
}
/**
* Creates and saves a DOT file
*
* @return string Directory where the file is saved
* @throws Exception
*/
function saveDOTFile($tree, $vars_data): string
{
// Make a unique directory to the tmp dir
$temp_dir = (new File())->sys_get_temp_dir_my() . "/" . md5(Auth::id());
if (!is_dir("$temp_dir")) {
mkdir("$temp_dir");
}
// Create the dump
$contents = $this->createGraphVizDump($tree, $vars_data, $temp_dir);
// Put the contents into the file
$settings = (new Settings())->getAdminSettings($this);
$fid = fopen($temp_dir . "/" . $settings['filename'] . ".dot", "w");
fwrite($fid, $contents);
fclose($fid);
return $temp_dir;
}
/**
* @throws Exception
*/
function createGraphVizDump($tree, $vars_data, $temp_dir): string
{
$out = "";
$dot = new Dot($tree, $this);
$formSubmission = new FormSubmission();
$vars = $formSubmission->load($vars_data, $this);
if (isset($temp_dir)) {
$vars['temp_dir'] = $temp_dir;
}
$dot->setSettings($vars);
$dot->settings['ancestor_levels'] = min($vars['ancestor_levels'], $dot->settings['limit_levels']);
$dot->settings['descendant_levels'] = min($vars['descendant_levels'], $dot->settings['limit_levels']);
$settings = new Settings();
$settings->saveUserSettings($this, $tree,$dot->settings);
// Get out DOT file
$out .= $dot->createDOTDump();
if (isset($_POST["browser"]) && $_POST["browser"] == "true") {
$dot->messages[] = I18N::translate('Generated %s individuals and %s family records', sizeof($dot->individuals), sizeof($dot->families));
$response['messages'] = $dot->messages;
$response['enable_debug_mode'] = $dot->debug_string;
$response['dot'] = $out;
try {
$response['settings'] = $settings->getSettingsJson($this, $tree, Settings::ID_MAIN_SETTINGS);
} catch (Exception $e) {
$dot->messages[] = 'Failed to retrieve settings JSON';
}
$r = json_encode($response);
} else {
$r = $out;
}
$settings->updateRecordCount($dot->settings['time_token'], sizeof($dot->individuals), sizeof($dot->families));
return $r;
}
/**
* Additional translations for module.
*
* @param string $language
*
* @return string[]
*/
public function customTranslations(string $language): array
{
$lang_dir = $this->resourcesFolder() . 'lang/';
$file = $lang_dir . $language . '.mo';
if (file_exists($file)) {
return (new Translation($file))->asArray();
} else {
return [];
}
}
/** Return list of available output types
*
* @param $vars
* @return array
*/
private function getOTypes($vars): array
{
$otypes = array();
foreach ($vars['graphviz_config']["output"] as $fmt => $val) {
if (isset($vars['graphviz_config']["output"][$fmt]["label"]) and isset($vars['graphviz_config']["output"][$fmt]["extension"])) {
$lbl = $vars['graphviz_config']["output"][$fmt]["label"];
$ext = $vars['graphviz_config']["output"][$fmt]["extension"];
$otypes[$ext] = $lbl;
}
}
return $otypes;
}
/**
* From https://stackoverflow.com/questions/4937478/strip-off-specific-parameter-from-urls-querystring
*
* @param $url
* @param $param
* @return string
*/
private function strip_param_from_url($url, $param): string
{
$base_url = strtok($url, '?'); // Get the base URL
$parsed_url = parse_url($url); // Parse it
if(array_key_exists('query',$parsed_url)) { // Only execute if there are parameters
$query = $parsed_url['query']; // Get the query string
parse_str($query, $parameters); // Convert Parameters into array
unset($parameters[$param]); // Delete the one you want
$new_query = http_build_query($parameters); // Rebuilt query string
$url =$base_url.'?'.$new_query; // Finally URL is ready
}
return $url;
}
/**
* A breaking change in webtrees 2.2.0 changes how the classes are retrieved.
* This function allows support for both 2.1.X and 2.2.X versions
* @param $class
* @return mixed
*/
static function getClass($class)
{
if (version_compare(Webtrees::VERSION, '2.2.0', '>=')) {
return Registry::container()->get($class);
} else {
return app($class);
}
}
}
$moduleService = GVExport::getClass(ModuleService::class);
return new GVExport($moduleService);