PdfJs   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 250
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 194
c 1
b 0
f 0
dl 0
loc 250
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPdfJsModule() 0 11 4
A run() 0 31 2
1
<?php
2
3
namespace diecoding\pdfjs;
4
5
use Yii;
6
use yii\base\InvalidConfigException;
7
use yii\base\Widget;
8
use yii\helpers\ArrayHelper;
9
use yii\helpers\Html;
10
11
/**
12
 * PdfJs is the class for widgets.
13
 * 
14
 * @link [sugeng-sulistiyawan.github.io](sugeng-sulistiyawan.github.io)
15
 * @author Sugeng Sulistiyawan <[email protected]>
16
 * @copyright Copyright (c) 2023
17
 */
18
class PdfJs extends Widget
19
{
20
	/**
21
	 * @var string
22
	 */
23
	public $url;
24
25
	/**
26
	 * @var string
27
	 */
28
	public $title;
29
30
	/**
31
	 * @var array
32
	 */
33
	public $options = [];
34
35
	/**
36
	 * Show or hide section container id
37
	 * Example:
38
	 * 
39
	 * ```php
40
	 * 'sections' => [
41
	 * 	 'presentationMode' => true,
42
	 * 	 'print' => true,
43
	 * 	 'download' => true,
44
	 * 	 'secondaryToolbarToggle' => true,
45
	 * ],
46
	 * ```
47
	 * 
48
	 * This equivalent to:
49
	 * ```$('#{$sectionId}').hide();```;
50
	 * 
51
	 * @var array
52
	 */
53
	public $sections = [
54
		'outerContainer' => true,
55
		'sidebarContainer' => true,
56
		'toolbarSidebar' => true,
57
		'toolbarSidebarLeft' => true,
58
		'sidebarViewButtons' => true,
59
		'viewThumbnail' => true,
60
		'viewOutline' => true,
61
		'viewAttachments' => true,
62
		'viewLayers' => true,
63
		'toolbarSidebarRight' => true,
64
		'outlineOptionsContainer' => true,
65
		'currentOutlineItem' => true,
66
		'sidebarContent' => true,
67
		'thumbnailView' => true,
68
		'outlineView' => true,
69
		'attachmentsView' => true,
70
		'layersView' => true,
71
		'sidebarResizer' => true,
72
		'mainContainer' => true,
73
		'findbar' => true,
74
		'findbarInputContainer' => true,
75
		'findInput' => true,
76
		'findPrevious' => true,
77
		'findNext' => true,
78
		'findbarOptionsOneContainer' => true,
79
		'findHighlightAll' => true,
80
		'findMatchCase' => true,
81
		'findbarOptionsTwoContainer' => true,
82
		'findMatchDiacritics' => true,
83
		'findEntireWord' => true,
84
		'findbarMessageContainer' => true,
85
		'findResultsCount' => true,
86
		'findMsg' => true,
87
		'editorHighlightParamsToolbar' => true,
88
		'highlightParamsToolbarContainer' => true,
89
		'editorHighlightColorPicker' => true,
90
		'highlightColorPickerLabel' => true,
91
		'editorHighlightThickness' => true,
92
		'editorFreeHighlightThickness' => true,
93
		'editorHighlightVisibility' => true,
94
		'editorHighlightShowAll' => true,
95
		'editorFreeTextParamsToolbar' => true,
96
		'editorFreeTextColor' => true,
97
		'editorFreeTextFontSize' => true,
98
		'editorInkParamsToolbar' => true,
99
		'editorInkColor' => true,
100
		'editorInkThickness' => true,
101
		'editorInkOpacity' => true,
102
		'editorStampParamsToolbar' => true,
103
		'editorStampAddImage' => true,
104
		'secondaryToolbar' => true,
105
		'secondaryToolbarButtonContainer' => true,
106
		'secondaryOpenFile' => true,
107
		'secondaryPrint' => true,
108
		'secondaryDownload' => true,
109
		'presentationMode' => true,
110
		'viewBookmark' => true,
111
		'viewBookmarkSeparator' => true,
112
		'firstPage' => true,
113
		'lastPage' => true,
114
		'pageRotateCw' => true,
115
		'pageRotateCcw' => true,
116
		'cursorToolButtons' => true,
117
		'cursorSelectTool' => true,
118
		'cursorHandTool' => true,
119
		'scrollModeButtons' => true,
120
		'scrollPage' => true,
121
		'scrollVertical' => true,
122
		'scrollHorizontal' => true,
123
		'scrollWrapped' => true,
124
		'spreadModeButtons' => true,
125
		'spreadNone' => true,
126
		'spreadOdd' => true,
127
		'spreadEven' => true,
128
		'documentProperties' => true,
129
		'toolbarContainer' => true,
130
		'toolbarViewer' => true,
131
		'toolbarViewerLeft' => true,
132
		'sidebarToggle' => true,
133
		'viewFind' => true,
134
		'previous' => true,
135
		'next' => true,
136
		'pageNumber' => true,
137
		'numPages' => true,
138
		'toolbarViewerRight' => true,
139
		'editorModeButtons' => true,
140
		'editorHighlight' => true,
141
		'editorFreeText' => true,
142
		'editorInk' => true,
143
		'editorStamp' => true,
144
		'editorModeSeparator' => true,
145
		'print' => true,
146
		'download' => true,
147
		'secondaryToolbarToggle' => true,
148
		'toolbarViewerMiddle' => true,
149
		'zoomOut' => true,
150
		'zoomIn' => true,
151
		'scaleSelectContainer' => true,
152
		'scaleSelect' => true,
153
		'pageAutoOption' => true,
154
		'pageActualOption' => true,
155
		'pageFitOption' => true,
156
		'pageWidthOption' => true,
157
		'customScaleOption' => true,
158
		'loadingBar' => true,
159
		'viewerContainer' => true,
160
		'viewer' => true,
161
		'dialogContainer' => true,
162
		'passwordDialog' => true,
163
		'passwordText' => true,
164
		'password' => true,
165
		'passwordCancel' => true,
166
		'passwordSubmit' => true,
167
		'documentPropertiesDialog' => true,
168
		'fileNameLabel' => true,
169
		'fileNameField' => true,
170
		'fileSizeLabel' => true,
171
		'fileSizeField' => true,
172
		'titleLabel' => true,
173
		'titleField' => true,
174
		'authorLabel' => true,
175
		'authorField' => true,
176
		'subjectLabel' => true,
177
		'subjectField' => true,
178
		'keywordsLabel' => true,
179
		'keywordsField' => true,
180
		'creationDateLabel' => true,
181
		'creationDateField' => true,
182
		'modificationDateLabel' => true,
183
		'modificationDateField' => true,
184
		'creatorLabel' => true,
185
		'creatorField' => true,
186
		'producerLabel' => true,
187
		'producerField' => true,
188
		'versionLabel' => true,
189
		'versionField' => true,
190
		'pageCountLabel' => true,
191
		'pageCountField' => true,
192
		'pageSizeLabel' => true,
193
		'pageSizeField' => true,
194
		'linearizedLabel' => true,
195
		'linearizedField' => true,
196
		'documentPropertiesClose' => true,
197
		'altTextDialog' => true,
198
		'altTextContainer' => true,
199
		'overallDescription' => true,
200
		'dialogLabel' => true,
201
		'dialogDescription' => true,
202
		'addDescription' => true,
203
		'descriptionButton' => true,
204
		'descriptionAreaLabel' => true,
205
		'descriptionTextarea' => true,
206
		'markAsDecorative' => true,
207
		'decorativeButton' => true,
208
		'decorativeLabel' => true,
209
		'buttons' => true,
210
		'altTextCancel' => true,
211
		'altTextSave' => true,
212
		'printServiceDialog' => true,
213
		'printCancel' => true,
214
		'printContainer' => true,
215
	];
216
217
	/**
218
	 * @return Module
219
	 * @throws InvalidConfigException
220
	 */
221
	protected function getPdfJsModule()
222
	{
223
		$moduleClass = Module::class;
224
		foreach (Yii::$app->getModules(false) as $id => $module) {
225
			$class = is_array($module) ? $module['class'] : $module::class;
226
			if ($class === $moduleClass) {
227
				return Yii::$app->getModule($id);
228
			}
229
		}
230
231
		throw new InvalidConfigException('PDFJs module must be an application module.');
232
	}
233
234
	/**
235
	 * @inheritdoc
236
	 */
237
	public function run()
238
	{
239
		$module = $this->getPdfJsModule();
240
		$defaultOptions = $module->defaultOptions;
241
		$defaultSections = $module->defaultSections;
242
243
		$defaultOptions = ArrayHelper::merge($defaultOptions, [
244
			'style' => [
245
				'width' => '100%',
246
				'height' => '480px',
247
				'background-color' => 'rgba(212, 212, 215, 1)',
248
				'border' => 'none',
249
			],
250
		]);
251
252
		if (isset($this->options['style'])) {
253
			$style = $this->options['style'];
254
			unset($this->options['style']);
255
256
			Html::addCssStyle($defaultOptions, $style);
257
		}
258
		$this->options = ArrayHelper::merge($defaultOptions, $this->options);
259
		$this->sections = ArrayHelper::merge($defaultSections, $this->sections);
260
261
		return $this->render('viewer', [
262
			'url' => $this->url,
263
			'title' => $this->title,
264
			'widgetId' => $this->getId(),
265
			'module' => $this->getPdfJsModule(),
266
			'options' => $this->options,
267
			'sections' => $this->sections,
268
		]);
269
	}
270
}
271