This repository has been archived by the owner on Jan 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.php
189 lines (163 loc) · 6.63 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
<?php
/**
* webtrees: online genealogy
* Copyright (C) 2018 JustCarmen (http://justcarmen.nl)
* 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/>.
*/
namespace JustCarmen\WebtreesAddOns\FancyTreeviewPdf;
use Composer\Autoload\ClassLoader;
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Database;
use Fisharebest\Webtrees\File;
use Fisharebest\Webtrees\Filter;
use Fisharebest\Webtrees\FlashMessages;
use Fisharebest\Webtrees\Html;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Log;
use Fisharebest\Webtrees\Tree;
use JustCarmen\WebtreesAddOns\FancyTreeview\FancyTreeviewModule;
use JustCarmen\WebtreesAddOns\FancyTreeviewPdf\Template\AdminTemplate;
use JustCarmen\WebtreesAddOns\FancyTreeviewPdf\Template\PdfTemplate;
/**
* This module works with Fancy Treeview version 1.7.9 or higher.
*
*/
const FTV_COMPATIBLE_VERSION = '2.0.0';
/**
* PDF extension for the Fancy Treeview module
*
* First check if the correct version of the Fancy Treeview module is installed and enabled (Class won't exist if the module hasn't been installed or has been disabled.
*/
$message1 = I18N::translate('You have installed the Fancy Treeview PDF module. This module won’t work without the correct version of the Fancy Treeview module installed and enabled. Please install and enable Fancy Treeview version %s or higher to use this module. <a href="http://www.justcarmen.nl/fancy-modules/fancy-treeview/">Click here to download the latest release</a>.', FTV_COMPATIBLE_VERSION);
$message2 = I18N::translate('You have installed the Fancy Treeview PDF module. This module won’t work without the Fancy Treeview module enabled. Please enable Fancy Treeview to use this module.');
if (method_exists('JustCarmen\WebtreesAddOns\FancyTreeview\FancyTreeviewModule', '__construct')) {
$ftv_version = str_replace("-dev", "", FancyTreeviewModule::CUSTOM_VERSION);
$ftv_minor = (int)substr(strrchr($ftv_version, "."), 1); // retrieve the digits after the last dot to compare
$compatible_minor = (int)substr(strrchr(FTV_COMPATIBLE_VERSION, "."), 1);
if (!file_exists(WT_MODULES_DIR . 'fancy_treeview') || (file_exists(WT_MODULES_DIR . 'fancy_treeview') && $compatible_minor > $ftv_minor)) {
if (Auth::isAdmin() && WT_SCRIPT_NAME === 'admin_modules.php') {
FlashMessages::addMessage($message1);
}
return;
}
} else {
if (Auth::isAdmin() && WT_SCRIPT_NAME === 'admin_modules.php') {
FlashMessages::addMessage($message1);
}
}
$ftv_module_status = Database::prepare("SELECT status FROM `##module` WHERE module_name = 'fancy_treeview'")->fetchOne();
if (file_exists(WT_MODULES_DIR . 'fancy_treeview') && $ftv_module_status === 'disabled') {
if (Auth::isAdmin() && WT_SCRIPT_NAME === 'admin_modules.php') {
FlashMessages::addMessage($message2);
}
return;
}
class FancyTreeviewPdfModule extends FancyTreeviewModule {
const CUSTOM_VERSION = '2.0.0-dev';
const CUSTOM_WEBSITE = 'http://www.justcarmen.nl/fancy-modules/fancy-treeview-pdf/';
/** {@inheritdoc} */
public function __construct() {
parent::__construct();
$this->directory = WT_MODULES_DIR . $this->getName();
// register the namespaces
$loader = new ClassLoader();
$loader->addPsr4('JustCarmen\\WebtreesAddOns\\FancyTreeviewPdf\\', $this->directory . '/app');
$loader->register();
}
public function getName(): string {
return 'fancy_treeview_pdf';
}
/** {@inheritdoc} */
public function getTitle(): string {
return /* I18N: Name of the Fancy Treeview PDF-module */ I18N::translate('Fancy Treeview PDF');
}
/** {@inheritdoc} */
public function getDescription(): string {
return /* I18N: Description of the module */ I18N::translate('Fancy Treeview module extension: offer your users to download a Fancy Treeview page as PDF.');
}
/** {@inheritdoc} */
public function getConfigLink(): string {
return Html::url('module.php', [
'mod' => $this->getName(),
'mod_action' => 'admin_config',
]);
}
/** {@inheritdoc} */
public function modAction($mod_action) {
switch ($mod_action) {
case 'admin_config':
if (Filter::postBool('save') && Filter::checkCsrf()) {
$this->setPreference('FTV_PDF_ACCESS_LEVEL', Filter::postInteger('NEW_FTV_PDF_ACCESS_LEVEL'));
$this->setPreference('FTV_PDF_TAB', Filter::postBool('NEW_FTV_PDF_TAB'));
Log::addConfigurationLog($this->getTitle() . ' config updated');
FlashMessages::addMessage(I18N::translate('The settings for this module are saved'), 'success');
}
$template = new AdminTemplate;
return $template->pageContent();
case 'full_pdf':
echo $this->module()->printPage();
break;
case 'write_pdf':
$tmp_dir = WT_DATA_DIR . 'ftv_pdf_tmp/';
if (file_exists($tmp_dir)) {
File::delete($tmp_dir);
}
File::mkdir($tmp_dir);
$template = new PdfTemplate();
return $template->pageBody();
case 'output_pdf':
$file = WT_DATA_DIR . 'ftv_pdf_tmp/' . Filter::get('title') . '.pdf';
if (file_exists($file)) {
ob_start();
header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
ob_clean();
ob_end_flush();
readfile($file);
File::delete(dirname($file));
} else {
FlashMessages::addMessage(I18N::translate('The file %s could not be created.', basename($file)), 'danger');
Header('Location:' . WT_BASE_URL . 'module.php?mod=fancy_treeview&mod_action=page&rootid=' . Filter::get('rootid') . '&ged=' . Filter::get('ged'));
}
break;
default:
http_response_code(404);
break;
}
}
/** {@inheritdoc} */
public function hasTabContent(Individual $individual): bool {
return false;
}
/** {@inheritdoc} */
public function getMenu(Tree $tree) {
return null;
}
protected function access() {
global $WT_TREE;
if ($this->getPreference('FTV_PDF_ACCESS_LEVEL', '2') >= Auth::accessLevel($WT_TREE)) {
return true;
}
}
protected function tab() {
if ($this->getPreference('FTV_PDF_TAB')) {
return true;
}
}
}
return new FancyTreeviewPdfModule;