-
Notifications
You must be signed in to change notification settings - Fork 0
/
limeMpdf.php
205 lines (189 loc) · 8.04 KB
/
limeMpdf.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
<?php
/**
* Plugin helper for limesurvey : add mpdf as an helper and some other tool for using it
*
* @author Denis Chenu <denis@sondages.pro>
* @copyright 2019-2023 Denis Chenu <http://www.sondages.pro>
* @license AGPL v3
* @version 2.2.1
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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.
*/
class limeMpdf extends PluginBase {
protected $storage = 'DbStorage';
protected static $description = 'Add mpdf as a plugin helper and some other tool for using it.';
protected static$name = 'limeMpdf';
public function init()
{
if (version_compare(Yii::app()->getConfig('versionnumber'), "3.10.0", "<=")) {
$this->subscribe('beforeActivate');
return;
}
Yii::setPathOfAlias('limeMpdf', dirname(__FILE__));
/* For the demao */
$this->subscribe('newDirectRequest');
/* for twig registering */
$this->subscribe('limeMpdfBeforePdf');
/* for twig edit */
if (intval(App()->getConfig('versionnumber')) <= 3) {
/* see mantis issue https://bugs.limesurvey.org/view.php?id=18589 Unable to use getValidScreenFiles event */
$this->subscribe('getValidScreenFiles');
}
}
public static function getDescription()
{
if (version_compare(Yii::app()->getConfig('versionnumber'), "3.10.0", "<=")) {
return 'This plugin is not compatible with your version. this plugin need LimeSurvey 3.10.0 or up.';
}
return parent::getDescription();
}
/**
* Disable activate in 3.9 and lesser
*/
public function beforeActivate()
{
if (!$this->getEvent()) {
throw new CHttpException(403);
}
// No need to test (registered only for 3.9 and lesser) but more clear
if (version_compare(Yii::app()->getConfig('versionnumber'), "3.10.0", "<=")) {
$event = $this->getEvent();
$event->set('success', false);
// Optionally set a custom error message.
$event->set('message', 'This plugin is not compatible with your version. this plugin need LimeSurvey 3.10.0 or up.');
}
}
/**
* Register to own event to register to getPluginTwigPath event
*/
public function limeMpdfBeforePdf()
{
if (!$this->getEvent()) {
throw new CHttpException(403);
}
$this->subscribe('getPluginTwigPath');
}
/**
* Add some views for this and other plugin
*/
public function getPluginTwigPath()
{
if (!$this->getEvent()) {
throw new CHttpException(403);
}
$viewPath = dirname(__FILE__)."/views";
$this->getEvent()->append('add', array($viewPath));
}
public function getValidScreenFiles()
{
if (!$this->getEvent()) {
throw new CHttpException(403);
}
if(
$this->getEvent()->get("type")!='view' ||
($this->getEvent()->get("screen") && $this->getEvent()->get("screen")!="pdf")
){
return;
}
$this->subscribe('getPluginTwigPath');
$addTwigs = array(
"subviews".DIRECTORY_SEPARATOR."mpdf".DIRECTORY_SEPARATOR."header.twig",
"subviews".DIRECTORY_SEPARATOR."mpdf".DIRECTORY_SEPARATOR."body.twig",
"subviews".DIRECTORY_SEPARATOR."mpdf".DIRECTORY_SEPARATOR."footer.twig",
"subviews".DIRECTORY_SEPARATOR."mpdf".DIRECTORY_SEPARATOR."style-custom.twig",
);
if ($this->get('twigEditDemo',null,null,1) && App()->getRequest()->getQuery('templatename') == App()->getConfig('defaulttheme')) {
$addTwigs[] = "subviews".DIRECTORY_SEPARATOR."mpdf".DIRECTORY_SEPARATOR."demo-html.twig";
}
if ($this->get('twigEditButtonTag',null,null,0)) {
$addTwigs[] = "subviews".DIRECTORY_SEPARATOR."mpdf".DIRECTORY_SEPARATOR."tags".DIRECTORY_SEPARATOR."radio.twig";
$addTwigs[] = "subviews".DIRECTORY_SEPARATOR."mpdf".DIRECTORY_SEPARATOR."tags".DIRECTORY_SEPARATOR."radio-checked.twig";
$addTwigs[] = "subviews".DIRECTORY_SEPARATOR."mpdf".DIRECTORY_SEPARATOR."tags".DIRECTORY_SEPARATOR."checkbox.twig";
$addTwigs[] = "subviews".DIRECTORY_SEPARATOR."mpdf".DIRECTORY_SEPARATOR."tags".DIRECTORY_SEPARATOR."checkbox-checked.twig";
}
$this->getEvent()->append('add', $addTwigs);
}
/**
* @var array[] the settings
*/
protected $settings = array(
'linkDemoView' => array(
'type' => 'info',
'content' => 'See the generated demonstration using default template %s.',
),
'linkDemoDownload' => array(
'type' => 'info',
'content' => 'Download the generated demonstration using default template %s.',
),
'twigEditDemo' => array(
'type' => 'boolean',
'default' => 1,
'label' => 'Allow edit of demo in default theme',
),
'twigEditButtonTag' => array(
'type' => 'boolean',
'default' => 0,
'label' => 'Allow edit of button tags',
),
);
/**
* @see parent:getPluginSettings
*/
public function getPluginSettings($getValues=true)
{
if(!Permission::model()->hasGlobalPermission('settings','read')) {
throw new CHttpException(403);
}
$pluginSettings= parent::getPluginSettings($getValues);
if (version_compare(Yii::app()->getConfig('versionnumber'),"3.10.0","<=")) {
$pluginSettings['linkDemoView']['content'] = "<div class='alert alert-danger error'>This plugin is not compatible with your version. this plugin need LimeSurvey 3.10.0 or up.</div>";
$pluginSettings['linkDemoDownload']['content'] = "";
return;
}
$testLink = Yii::app()->getController()->createUrl('plugins/direct', array('plugin' => get_class($this),'function'=>'view'));
$testLink = CHtml::link($testLink,$testLink);
$pluginSettings['linkDemoView']['content'] = sprintf($pluginSettings['linkDemoView']['content'],$testLink);
$testLink = Yii::app()->getController()->createUrl('plugins/direct', array('plugin' => get_class($this),'function'=>'download'));
$testLink = CHtml::link($testLink,$testLink);
$pluginSettings['linkDemoDownload']['content'] = sprintf($pluginSettings['linkDemoDownload']['content'],$testLink);
return $pluginSettings;
}
public function newDirectRequest()
{
if (!$this->getEvent()) {
throw new CHttpException(403);
}
if($this->getEvent()->get('target') != get_class($this)) {
return;
}
if(App()->getUser()->getIsGuest()) {
/* Making PDF take memory, disable if not loggued */
throw new CHttpException(401);
}
App()->setLanguage(Yii::app()->session['adminlang']);
$function = $this->getEvent()->get('function');
/* event updated here */
$this->subscribe('getPluginTwigPath');
$html = Yii::app()->twigRenderer->renderPartial('./subviews/mpdf/demo-html.twig', array('aSurveyInfo'=>array()));
$pdfHelper = new \limeMpdf\helper\limeMpdfHelper();
$pdfHelper->setOptions(array(
'h2bookmarks' => array('H1'=>0, 'H2'=>1, 'H3'=>2),
'h2toc' => array('H1'=>0, 'H2'=>1, 'H3'=>2),
//~ 'title2annots' => true,
));
$pdfHelper->setTitle("Demo of limeMpdf",Yii::app()->getConfig('sitename'));
$pdfHelper->filterHtml = false;
if($function == 'view') {
$pdfHelper->doPdfContent($html,\Mpdf\Output\Destination::INLINE);
}
$pdfHelper->doPdfContent($html);
}
}