Completed
Push — developer ( b33bdc...d235c9 )
by Błażej
26:07 queued 12:56
created

Tcpdf::initializePdf()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 18
cts 18
cp 1
rs 9.6
c 0
b 0
f 0
cc 2
nc 2
nop 11
crap 2

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * Class using TCPDF as a PDF creator.
5
 *
6
 * @package App\Pdf
7
 *
8
 * @copyright YetiForce Sp. z o.o
9
 * @license   YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com)
10
 * @author    Rafal Pospiech <[email protected]>
11
 */
12
13
namespace App\Pdf;
14
15
/**
16
 * Tcpdf class.
17
 */
18
class Tcpdf extends PDF
19
{
20
	const WATERMARK_TYPE_TEXT = 0;
21
	const WATERMARK_TYPE_IMAGE = 1;
22
23
	/**
24
	 * Are we fully configured or default params were given in constructor (not fully configured) ?
25
	 *
26
	 * @var bool
27
	 */
28
	private $isDefault = true;
29
30
	/**
31
	 * HTML content.
32
	 *
33
	 * @var string
34
	 */
35
	public $html = '';
36
37
	/**
38
	 * Page format.
39
	 *
40
	 * @var string
41
	 */
42
	public $format = '';
43
44
	/**
45
	 * Page orientation.
46
	 *
47
	 * @var array
48
	 */
49
	public $pageOrientation = ['PLL_PORTRAIT' => 'P', 'PLL_LANDSCAPE' => 'L'];
50
51
	/**
52
	 * Default margins.
53
	 *
54
	 * @var array
55
	 */
56
	public $defaultMargins = [
57
		'left' => 15,
58
		'right' => 15,
59
		'top' => 16,
60
		'bottom' => 16
61
	];
62
63
	/**
64
	 * Default font.
65
	 *
66
	 * @var string
67
	 */
68
	protected $defaultFontFamily = 'dejavusans';
69
70
	/**
71
	 * Default font size.
72
	 *
73
	 * @var int
74
	 */
75
	protected $defaultFontSize = 10;
76
77
	/**
78
	 * @var \Vtiger_Module_Model
79
	 */
80
	protected $moduleModel;
81
82
	/**
83
	 * Returns pdf library object.
84
	 */
85 1
	public function pdf()
86
	{
87 1
		return $this->pdf;
88
	}
89
90
	/**
91
	 * Constructor.
92
	 */
93 1
	public function __construct($mode = '', $format = 'A4', $defaultFontSize = 10, $defaultFont = 'dejavusans', $orientation = 'P', $leftMargin = 15, $rightMargin = 15, $topMargin = 16, $bottomMargin = 16, $headerMargin = 9, $footerMargin = 9)
94
	{
95 1
		$args = func_get_args();
96
		// this two arguments are kind of signal that we are configured (from template or elsewhere) - not from default argument values (empty = default)
97 1
		if (!empty($args['format']) || !empty($args['orientation'])) {
98
			$this->isDefault = false;
99
		}
100 1
		$this->setLibraryName('tcpdf');
101 1
		$this->defaultFontFamily = $defaultFont;
102 1
		$this->defaultFontSize = $defaultFontSize;
103 1
		$this->format = $format;
104 1
		$this->initializePdf($mode, $format, $defaultFontSize, $defaultFont, $orientation, $leftMargin, $rightMargin, $topMargin, $bottomMargin, $headerMargin, $footerMargin);
105 1
	}
106
107
	/**
108
	 * Initialize pdf file params.
109
	 *
110
	 * @param string $mode
111
	 * @param string $format
112
	 * @param int    $defaultFontSize
113
	 * @param string $defaultFont
114
	 * @param string $orientation
115
	 * @param int    $leftMargin
116
	 * @param int    $rightMargin
117
	 * @param int    $topMargin
118
	 * @param int    $bottomMargin
119
	 * @param int    $headerMargin
120
	 * @param int    $footerMargin
121
	 */
122 1
	public function initializePdf($mode = '', $format = 'A4', $defaultFontSize = 10, $defaultFont = 'dejavusans', $orientation = 'P', $leftMargin = 15, $rightMargin = 15, $topMargin = 16, $bottomMargin = 16, $headerMargin = 9, $footerMargin = 9)
123
	{
124 1
		if (empty($mode)) {
125 1
			$mode = \AppConfig::main('default_charset') ?? 'UTF-8';
126
		}
127 1
		$this->pdf = new \App\Pdf\Libs\Tcpdf($orientation, 'mm', $format, true, $mode);
128 1
		$this->pdf->setFontSubsetting(true);
129 1
		$this->pdf->SetFont($this->defaultFontFamily, '', $this->defaultFontSize);
130 1
		$this->pdf->SetMargins($leftMargin, $topMargin, $rightMargin, true);
131 1
		$this->pdf->SetHeaderMargin($headerMargin);
132 1
		$this->pdf->SetFooterMargin($footerMargin);
133 1
		$this->pdf->SetAutoPageBreak(true, $bottomMargin);
134 1
		$this->pdf->setHeaderFontFamily($defaultFont);
135 1
		$this->pdf->setHeaderFontVariation('');
136 1
		$this->pdf->setHeaderFontSize($defaultFontSize);
137 1
		$this->pdf->setFooterFontFamily($defaultFont);
138 1
		$this->pdf->setFooterFontVariation('');
139 1
		$this->pdf->setFooterFontSize($defaultFontSize);
140 1
		$this->pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
141 1
	}
142
143
	/**
144
	 * Returns bank name.
145
	 */
146
	public function getLibraryName()
147
	{
148
		return $this->library;
149
	}
150
151
	/**
152
	 * Sets library name.
153
	 */
154 1
	public function setLibraryName($name)
155
	{
156 1
		$this->library = $name;
157 1
	}
158
159
	/**
160
	 * Returns template id.
161
	 */
162
	public function getTemplateId()
163
	{
164
		return $this->templateId;
165
	}
166
167
	/**
168
	 * Sets the template id.
169
	 */
170 1
	public function setTemplateId($id)
171
	{
172 1
		$this->templateId = $id;
173 1
	}
174
175
	/**
176
	 * Returns record id.
177
	 */
178
	public function getRecordId()
179
	{
180
		return $this->recordId;
181
	}
182
183
	/**
184
	 * Sets the record id.
185
	 */
186 1
	public function setRecordId($id)
187
	{
188 1
		$this->recordId = $id;
189 1
	}
190
191
	/**
192
	 * Returns module name.
193
	 */
194
	public function getModuleName()
195
	{
196
		return $this->moduleName;
197
	}
198
199
	/**
200
	 * Sets module name.
201
	 */
202 1
	public function setModuleName($name)
203
	{
204 1
		$this->moduleName = $name;
205 1
		$handlerClass = \Vtiger_Loader::getComponentClassName('Model', 'PDF', $name);
206 1
		$this->moduleModel = new $handlerClass();
207 1
	}
208
209
	/**
210
	 * Set top margin.
211
	 */
212 1
	public function setTopMargin($margin)
213
	{
214 1
		$this->pdf->SetTopMargin($margin);
215 1
	}
216
217
	/**
218
	 * Set bottom margin.
219
	 */
220 1
	public function setBottomMargin($margin)
221
	{
222 1
		$this->pdf->SetAutoPageBreak(true, $margin);
223 1
	}
224
225
	/**
226
	 * Set left margin.
227
	 */
228 1
	public function setLeftMargin($margin)
229
	{
230 1
		$this->pdf->SetLeftMargin($margin);
231 1
	}
232
233
	/**
234
	 * Set right margin.
235
	 */
236 1
	public function setRightMargin($margin)
237
	{
238 1
		$this->pdf->SetRightMargin($margin);
239 1
	}
240
241
	/**
242
	 * Set page size and orientation.
243
	 *
244
	 * @param string|null $format      - page format
245
	 * @param string      $orientation - page orientation
246
	 */
247
	public function setPageSize($format, $orientation = null)
248
	{
249
		$this->pdf->setPageSize($format, $orientation);
250
	}
251
252
	/**
253
	 * Set language.
254
	 *
255
	 * @param $language
256
	 */
257 1
	public function setLanguage($language)
258
	{
259 1
		parent::setLanguage($language);
260 1
		$this->pdf->setLanguage($language);
261 1
	}
262
263
	/**
264
	 * Parse variables.
265
	 *
266
	 * @param string $str
267
	 *
268
	 * @return string
269
	 */
270 1
	public function parseVariables(string $str)
271
	{
272 1
		$textParser = \App\TextParser::getInstanceById($this->recordId, $this->moduleName);
273 1
		$textParser->setType('pdf');
274 1
		$textParser->setParams(['pdf' => $this->moduleModel]);
275 1
		if ($this->language) {
276 1
			$textParser->setLanguage($this->language);
277
		}
278 1
		return $textParser->setContent($str)->parse()->getContent();
279
	}
280
281
	/**
282
	 * Parse and set options.
283
	 *
284
	 * @param array $params         - array of parameters
285
	 * @param bool  $defaultMargins - use default margins or custom user specified?
286
	 */
287 1
	public function parseParams(array $params, $defaultMargins = true)
288
	{
289 1
		if ($defaultMargins) {
290 1
			$params = array_diff_key($params, ['margin-top', 'margin-bottom', 'margin-left', 'margin-right', 'header_height', 'footer_height']);
291
		}
292 1
		foreach ($params as $param => $value) {
293 1
			switch ($param) {
294 1
				case 'margin-top':
295 1
					if (is_numeric($value)) {
296 1
						$this->setTopMargin($value);
297
					} else {
298
						$this->setTopMargin($this->defaultMargins['top']);
299
					}
300 1
					break;
301 1
				case 'margin-bottom':
302 1
					if (is_numeric($value)) {
303 1
						$this->setBottomMargin($value);
304
					} else {
305
						$this->setBottomMargin($this->defaultMargins['bottom']);
306
					}
307 1
					break;
308 1
				case 'margin-left':
309 1
					if (is_numeric($value)) {
310 1
						$this->setLeftMargin($value);
311
					} else {
312
						$this->setLeftMargin($this->defaultMargins['left']);
313
					}
314 1
					break;
315 1
				case 'margin-right':
316 1
					if (is_numeric($value)) {
317 1
						$this->setRightMargin($value);
318
					} else {
319
						$this->setRightMargin($this->defaultMargins['right']);
320
					}
321 1
					break;
322 1
				case 'header_height':
323 1
					if (is_numeric($value)) {
324 1
						$this->pdf->setHeaderMargin($value);
325
					}
326 1
					break;
327 1
				case 'footer_height':
328 1
					if (is_numeric($value)) {
329 1
						$this->pdf->setFooterMargin($value);
330
					}
331 1
					break;
332 1
				case 'title':
333 1
					$this->setTitle($value);
334 1
					break;
335 1
				case 'author':
336 1
					$this->setAuthor($value);
337 1
					break;
338 1
				case 'creator':
339 1
					$this->setCreator($value);
340 1
					break;
341 1
				case 'subject':
342 1
					$this->setSubject($value);
343 1
					break;
344 1
				case 'keywords':
345 1
					$this->setKeywords($value);
346 1
					break;
347
				default:
348 1
					break;
349
			}
350
		}
351 1
	}
352
353
	// meta attributes
354
355
	/**
356
	 * Set Title of the document.
357
	 */
358 1
	public function setTitle($title)
359
	{
360 1
		$this->pdf->SetTitle($this->parseVariables($title));
361 1
	}
362
363
	/**
364
	 * Set Title of the document.
365
	 */
366 1
	public function setAuthor($author)
367
	{
368 1
		$this->pdf->SetAuthor($this->parseVariables($author));
369 1
	}
370
371
	/**
372
	 * Set Title of the document.
373
	 */
374 1
	public function setCreator($creator)
375
	{
376 1
		$this->pdf->SetCreator($creator);
377 1
	}
378
379
	/**
380
	 * Set Title of the document.
381
	 */
382 1
	public function setSubject($subject)
383
	{
384 1
		$this->pdf->SetSubject($this->parseVariables($subject));
385 1
	}
386
387
	/**
388
	 * Set Title of the document.
389
	 */
390 1
	public function setKeywords($keywords)
391
	{
392 1
		$this->pdf->SetKeywords($this->parseVariables($keywords));
393 1
	}
394
395
	/**
396
	 * Set header content.
397
	 */
398
	public function setHeader($name, $header)
399
	{
400
		$this->pdf->setHtmlHeader($header);
401
	}
402
403
	/**
404
	 * Set footer content.
405
	 */
406
	public function setFooter($name, $footer)
407
	{
408
		$this->pdf->setHtmlFooter($footer);
409
	}
410
411
	/**
412
	 * Write html.
413
	 */
414 1
	public function writeHTML()
415
	{
416 1
		$this->pdf->writeHTML($this->parseVariables($this->html));
417 1
	}
418
419
	/**
420
	 * Set watermark.
421
	 *
422
	 * @param $templateModel
423
	 */
424 1
	public function setWaterMark($templateModel)
425
	{
426 1
		if ($templateModel->get('watermark_type') === self::WATERMARK_TYPE_IMAGE) {
427
			if ($templateModel->get('watermark_image')) {
428
				$this->pdf->setWatermarkImage($templateModel->get('watermark_image'), 0.15, 'P');
429
			} else {
430
				$this->pdf->clearWatermarkImage();
431
			}
432 1
		} elseif ($templateModel->get('watermark_type') === self::WATERMARK_TYPE_TEXT) {
433 1
			$this->pdf->SetWatermarkText($this->parseVariables($templateModel->get('watermark_text')), 0.15, $templateModel->get('watermark_size'), $templateModel->get('watermark_angle'));
434
		}
435 1
	}
436
437
	/**
438
	 * Load html.
439
	 *
440
	 * @param string $html
441
	 */
442
	public function loadHTML($html)
443
	{
444
		$this->html = $html;
445
	}
446
447
	/**
448
	 * Prepare pdf, generate all content.
449
	 *
450
	 * @param int    $recordId
451
	 * @param string $moduleName
452
	 * @param int    $templateId
453
	 * @param int    $templateMainRecordId - optional if null $recordId is used
454
	 *
455
	 * @return Tcpdf current or new instance if needed
456
	 */
457 1
	public function generateContent($recordId, $moduleName, $templateId, $templateMainRecordId = null)
458
	{
459 1
		$template = \Vtiger_PDF_Model::getInstanceById($templateId, $moduleName);
460 1
		$template->setMainRecordId($templateMainRecordId ? $templateMainRecordId : $recordId);
461 1
		$pageOrientationValue = $template->get('page_orientation') === 'PLL_PORTRAIT' ? 'P' : 'L';
462 1
		if ($this->isDefault) {
463 1
			$charset = \AppConfig::main('default_charset') ?? 'UTF-8';
464 1
			if ($template->get('margin_chkbox') === 1) {
465
				$self = new self($charset, $template->get('page_format'), $this->defaultFontSize, $this->defaultFontFamily, $pageOrientationValue);
466
			} else {
467 1
				$self = new self($charset, $template->get('page_format'), $this->defaultFontSize, $this->defaultFontFamily, $pageOrientationValue, $template->get('margin_left'), $template->get('margin_right'), $template->get('margin_top'), $template->get('margin_bottom'), $template->get('header_height'), $template->get('footer_height'));
468
			}
469 1
			$self->isDefault = false;
470
		} else {
471
			$self = $this;
472
		}
473 1
		$self->setTemplateId($templateId);
474 1
		$self->setRecordId($recordId);
475 1
		$self->setModuleName($moduleName);
476 1
		\App\Language::setTemporaryLanguage($template->get('language'));
477 1
		$self->setWaterMark($template);
478 1
		$self->setLanguage($template->get('language'));
479 1
		$self->setFileName($self->parseVariables($template->get('filename')));
480 1
		$self->pdf->setHeaderFont([$self->defaultFontFamily, '', $self->defaultFontSize]);
481 1
		$self->pdf->setFooterFont([$self->defaultFontFamily, '', $self->defaultFontSize]);
482 1
		$self->parseParams($template->getParameters(), $template->get('margin_chkbox') !== 1);
483 1
		$self->pdf()->setHtmlHeader($self->parseVariables($template->getHeader()));
484 1
		$self->pdf()->AddPage($template->get('page_orientation') === 'PLL_PORTRAIT' ? 'P' : 'L', $template->get('page_format'));
485 1
		$self->pdf()->setHtmlFooter($self->parseVariables($template->getFooter()));
486 1
		$self->pdf()->writeHTML($self->parseVariables($template->getBody()));
487 1
		$self->pdf()->lastPage();
488 1
		\App\Language::clearTemporaryLanguage();
489 1
		return $self;
490
	}
491
492
	/**
493
	 * Output content to PDF.
494
	 *
495
	 * @param string $fileName
496
	 * @param string $dest
497
	 */
498 1
	public function output($fileName = '', $dest = '')
499
	{
500 1
		if (empty($fileName)) {
501
			$fileName = $this->getFileName() . '.pdf';
502
			$dest = 'I';
503
		}
504 1
		$this->writeHTML();
505 1
		$this->pdf->Output($fileName, $dest);
506 1
	}
507
508
	/**
509
	 * Export record to PDF file.
510
	 *
511
	 * @param int    $recordId   - id of a record
512
	 * @param string $moduleName - name of records module
513
	 * @param int    $templateId - id of pdf template
514
	 * @param string $filePath   - path name for saving pdf file
515
	 * @param string $saveFlag   - save option flag
516
	 */
517 1
	public function export($recordId, $moduleName, $templateId, $filePath = '', $saveFlag = '')
518
	{
519 1
		$this->generateContent($recordId, $moduleName, $templateId, $recordId)->output($filePath, $saveFlag);
520 1
	}
521
}
522