This repository has been archived by the owner on Dec 27, 2023. It is now read-only.
/
Xls.php
709 lines (600 loc) · 24 KB
/
Xls.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
<?php
/**
* Tinebase Xls/Xlsx generation class
*
* @package Tinebase
* @subpackage Export
* @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
* @author Paul Mehrer <p.mehrer@metaways.de>
* @copyright Copyright (c) 2017-2019 Metaways Infosystems GmbH (http://www.metaways.de)
*/
use PhpOffice\PhpSpreadsheet\Cell;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Worksheet\Row;
/**
* Tinebase Xls/Xlsx generation class
*
* @package Tinebase
* @subpackage Export
*/
class Tinebase_Export_Xls extends Tinebase_Export_Abstract implements Tinebase_Record_IteratableInterface, Tinebase_Export_Convertible {
use Tinebase_Export_Convertible_PreviewServicePdf;
/**
* the document
*
* @var PhpOffice\PhpSpreadsheet\Spreadsheet
*/
protected $_spreadsheet;
/**
* format strings
*
* @var string
*/
protected $_format = 'xls';
protected $_rowOffset = 0;
protected $_rowCount = 0;
protected $_columnCount = 1;
protected $_cloneRow;
protected $_cloneRowStyles = array();
protected $_cloneGroupStartRowStyles = null;
protected $_cloneGroupStartRow = null;
protected $_cloneGroupEndRowStyles = null;
protected $_cloneGroupEndRow = null;
protected $_excelVersion;
/**
* the constructor
*
* @param Tinebase_Model_Filter_FilterGroup $_filter
* @param Tinebase_Controller_Record_Interface $_controller (optional)
* @param array $_additionalOptions (optional) additional options
* @throws Tinebase_Exception_AccessDenied
* @throws Tinebase_Exception_InvalidArgument
* @throws Tinebase_Exception_NotFound
*/
public function __construct(Tinebase_Model_Filter_FilterGroup $_filter = null, Tinebase_Controller_Record_Interface $_controller = NULL, $_additionalOptions = array())
{
parent::__construct($_filter, $_controller, $_additionalOptions);
if (empty($this->_config->writer)) {
$this->_excelVersion = 'Xlsx';
} else {
$this->_excelVersion = $this->_config->writer;
}
}
public static function getDefaultFormat()
{
return 'xlsx';
}
/**
* get excel object
*
* @return PhpOffice\PhpSpreadsheet\Spreadsheet
*/
public function getDocument()
{
return $this->_spreadsheet;
}
/**
* get export content type
*
* @return string
*/
public function getDownloadContentType()
{
$contentType = ('Xlsx' === $this->_excelVersion)
// Excel 2007 content type
? 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
// Excel 5 content type or other
: 'application/vnd.ms-excel';
return $contentType;
}
/**
* return download filename
* @param string $_appName
* @param string $_format
* @return string
*/
public function getDownloadFilename($_appName = null, $_format = null)
{
$result = parent::getDownloadFilename($_appName, $_format);
if ('Xlsx' === $this->_excelVersion && $_format !== 'xlsx') {
// excel2007 extension is .xlsx
$result .= 'x';
}
return $result;
}
/**
* get export format string (csv, ...)
*
* @return string
* @throws Tinebase_Exception_NotFound
*/
public function getFormat()
{
if ('xls' === $this->_format && 'Xlsx' === $this->_excelVersion) {
// excel2007 extension is .xlsx
return 'xlsx';
}
if ($this->_format === null) {
throw new Tinebase_Exception_NotFound('Format string not found.');
}
return $this->_format;
}
/**
* output result
*
* @param string $_target
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
public function write($_target = 'php://output')
{
Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Creating and sending xls to client (Format: ' . $this->_excelVersion . ').');
$xlswriter = IOFactory::createWriter($this->_spreadsheet, $this->_excelVersion);
// precalculating formula values costs tons of time, because sum formulas are like SUM C1:C65000
/** @noinspection PhpUndefinedMethodInspection */
$xlswriter->setPreCalculateFormulas(FALSE);
$xlswriter->save($_target);
}
/**
* @param string $_target
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
public function save($_target)
{
$this->write($_target);
}
/**
* generate export
* @throws Tinebase_Exception
*/
public function generate()
{
$this->_rowCount = 0;
$this->_columnCount = 1;
$this->_createDocument();
$this->_exportRecords();
$this->_replaceTine20ImagePaths();
}
protected function _startRow()
{
$this->_rowCount += 1;
$this->_columnCount = 1;
//insert cloned row
if ($this->_rowOffset > 0) {
$newRowOffset = $this->_rowOffset + $this->_rowCount - 1;
$sheet = $this->_spreadsheet->getActiveSheet();
if ($this->_rowCount > 1) {
$sheet->insertNewRowBefore($newRowOffset);
}
foreach($this->_cloneRow as $newRow) {
$cell = $sheet->getCell($newRow['column'] . $newRowOffset);
$cell->setValue(preg_replace('/\$\{twig[^}]+\}/', '$0#' . $this->_rowCount, $newRow['value']));
$cell->setXfIndex($newRow['XFIndex']);
}
$rowDimension = $sheet->getRowDimension($newRowOffset);
foreach($this->_cloneRowStyles as $func => $value) {
call_user_func(array($rowDimension, $func), $value);
}
}
}
protected function _startGroup()
{
if (null === $this->_cloneGroupStartRow) {
return;
}
$this->_rowCount += 1;
//insert cloned row
if ($this->_rowOffset > 0) {
$newRowOffset = $this->_rowOffset + $this->_rowCount - 1;
$sheet = $this->_spreadsheet->getActiveSheet();
if ($this->_rowCount > 1) {
$sheet->insertNewRowBefore($newRowOffset);
}
foreach($this->_cloneGroupStartRow as $newRow) {
$cell = $sheet->getCell($newRow['column'] . $newRowOffset);
$cell->setValue(preg_replace('/\$\{twig[^}]+\}/', '$0#' . $this->_rowCount, $newRow['value']));
$cell->setXfIndex($newRow['XFIndex']);
}
$rowDimension = $sheet->getRowDimension($newRowOffset);
foreach($this->_cloneGroupStartRowStyles as $func => $value) {
call_user_func(array($rowDimension, $func), $value);
}
}
$this->_renderTwigTemplate();
}
protected function _endGroup()
{
if (null === $this->_cloneGroupEndRow) {
return;
}
$this->_rowCount += 1;
//insert cloned row
if ($this->_rowOffset > 0) {
$newRowOffset = $this->_rowOffset + $this->_rowCount - 1;
$sheet = $this->_spreadsheet->getActiveSheet();
if ($this->_rowCount > 1) {
$sheet->insertNewRowBefore($newRowOffset);
}
foreach($this->_cloneGroupEndRow as $newRow) {
$cell = $sheet->getCell($newRow['column'] . $newRowOffset);
$cell->setValue(preg_replace('/\$\{twig[^}]+\}/', '$0#' . $this->_rowCount, $newRow['value']));
$cell->setXfIndex($newRow['XFIndex']);
}
$rowDimension = $sheet->getRowDimension($newRowOffset);
foreach($this->_cloneGroupEndRowStyles as $func => $value) {
call_user_func(array($rowDimension, $func), $value);
}
}
$this->_renderTwigTemplate();
}
protected function _createDocument()
{
Tinebase_Export_Spreadsheet_NumberFormat::fillBuiltInFormatCodes();
$templateFile = $this->_getTemplateFilename();
if ($templateFile !== NULL) {
// autodetection works much better with file ending, thanks to phpspreadsheet we can simply use the reader
// version! (at least until ms will change the file endings) :-)
$tmpFile = Tinebase_TempFile::getTempPath() . '.' . strtolower($this->_excelVersion);
Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Copy template file to temp path: '
. $templateFile . ' -> ' . $tmpFile);
if (false === copy($templateFile, $tmpFile)) {
Tinebase_Core::getLogger()->err(__METHOD__ . '::' . __LINE__ . ' could not copy template file to temp path');
throw new Tinebase_Exception('could not copy template file to temp path');
}
if (! $this->_config->reader || 'autodetection' === $this->_config->reader) {
$this->_spreadsheet = IOFactory::load($tmpFile);
} else {
$reader = IOFactory::createReader($this->_config->reader);
$this->_spreadsheet = $reader->load($tmpFile);
}
$activeSheet = isset($this->_config->sheet) ? $this->_config->sheet : 0;
$this->_spreadsheet->setActiveSheetIndex($activeSheet);
$this->_hasTemplate = true;
$this->_dumpRecords = true;
$this->_writeGenericHeader = true;
} else {
Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Creating new PhpSpreadsheet object.');
$this->_spreadsheet = new PhpOffice\PhpSpreadsheet\Spreadsheet();
}
}
/**
* TODO performance?
* TODO performance: cache cells with replacement tokens, update cache when cloning a row
* TODO not just replace first, replace all!
*
* @param string $_key
* @param string $_value
*/
protected function _setValue($_key, $_value)
{
if (true !== $this->_iterationDone) {
$_key = $_key . '#' . $this->_rowCount;
}
if (null !== ($cell = $this->_findCell($_key))) {
$cell->setValue(str_replace($_key, $_value, $cell->getValue()));
}
foreach($this->_spreadsheet->getActiveSheet()->getDrawingCollection() as $drawing) {
$desc = $drawing->getDescription();
if (\strpos($desc, $_key) !== false) {
$drawing->setDescription(str_replace($_key, $_value, $desc));
}
}
}
/**
* @param string $_search
* @return Cell
*/
protected function _findCell($_search)
{
$sheet = $this->_spreadsheet->getActiveSheet();
$rowIter = $sheet->getRowIterator();
/** @var Row $row */
foreach($rowIter as $row) {
$cellIter = $row->getCellIterator();
try {
$cellIter->setIterateOnlyExistingCells(true);
} catch (\PhpOffice\PhpSpreadsheet\Exception $pe) {
continue;
}
/** @var PhpOffice\PhpSpreadsheet\Cell $cell */
foreach($cellIter as $cell) {
if (false !== strpos($cell->getValue(), $_search)) {
return $cell;
}
}
}
return null;
}
/**
* TODO pass value type? for dates etc.?
*
* @param string $_value
* @throws Tinebase_Exception_NotImplemented
*/
protected function _writeValue($_value)
{
$sheet = $this->_spreadsheet->getActiveSheet();
$cell = $sheet->getCellByColumnAndRow($this->_columnCount++, $this->_rowCount);
$cell->setValue($_value);
}
/**
* TODO build up cache of replacement tokens so that _setValue can be implemented faster!
*
* @return string
*/
public function _getTwigSource()
{
$i = 0;
$source = '[';
$sheet = $this->_spreadsheet->getActiveSheet();
$rowIter = $sheet->getRowIterator();
/** @var Row $row */
foreach($rowIter as $row) {
$cellIter = $row->getCellIterator();
try {
$cellIter->setIterateOnlyExistingCells(true);
} catch (\PhpOffice\PhpSpreadsheet\Exception $pe) {
continue;
}
/** @var Cell $cell */
foreach($cellIter as $cell) {
if (false !== strpos($cell->getValue(), '${twig:') &&
preg_match_all('/(\${twig:(.+?[^}%])})([^}]|$)/s', $cell->getValue(), $matches, PREG_SET_ORDER)) {
foreach($matches as $match) {
$this->_twigMapping[$i] = $match[1];
$source .= ($i === 0 ? '' : ',') .
(strpos($match[2], '{{') !== false || strpos($match[2], '{%') !== false ? $match[2]
: '{{' . $match[2] . '}}');
++$i;
}
}
}
}
foreach($this->_spreadsheet->getActiveSheet()->getDrawingCollection() as $drawing) {
$desc = $drawing->getDescription();
if (false !== strpos($desc, '${twig:') &&
preg_match_all('/(\${twig:(.+?[^}%])})([^}]|$)/s', $desc, $matches, PREG_SET_ORDER)
) {
foreach ($matches as $match) {
$this->_twigMapping[$i] = $match[1];
$source .= ($i === 0 ? '' : ',') .
(strpos($match[2], '{{') !== false || strpos($match[2], '{%') !== false ? $match[2]
: '{{' . $match[2] . '}}');
++$i;
}
}
}
$source .= ']';
if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
. ' returning twig template source: ' . $source);
return $source;
}
protected function _findFirstFreeRow()
{
$sheet = $this->_spreadsheet->getActiveSheet();
$rowIter = $sheet->getRowIterator();
/** @var Row $row */
/** @noinspection PhpUnusedLocalVariableInspection */
foreach($rowIter as $row) {
++$this->_rowCount;
}
}
protected function _onBeforeExportRecords()
{
// TODO header row?
if (null === ($block = $this->_findCell('${ROW}'))) {
$this->_findFirstFreeRow();
return;
}
$startColumn = $block->getColumn();
$this->_rowOffset = $block->getRow();
if (null === ($block = $this->_findCell('${/ROW}'))) {
$this->_findFirstFreeRow();
return;
}
$this->_dumpRecords = false;
$this->_writeGenericHeader = false;
$endColumn = $block->getColumn();
if ($block->getRow() !== $this->_rowOffset) {
if (Tinebase_Core::isLogLevel(Zend_Log::WARN))
Tinebase_Core::getLogger()->warn(__METHOD__ . ' ' . __LINE__ . ' block tags need to be in the same row');
throw new Tinebase_Exception_UnexpectedValue('block tags need to be in the same row');
}
if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG))
Tinebase_Core::getLogger()->debug(__METHOD__ . ' ' . __LINE__ . ' found block...');
$sheet = $this->_spreadsheet->getActiveSheet();
/** @var $rowIterator */
$rowIterator = $sheet->getRowIterator($this->_rowOffset);
$row = $rowIterator->current();
$rowDimension = $sheet->getRowDimension($row->getRowIndex());
$this->_cloneRowStyles = array(
'setCollapsed' => $rowDimension->getCollapsed(),
'setOutlineLevel' => $rowDimension->getOutlineLevel(),
'setRowHeight' => $rowDimension->getRowHeight(),
'setVisible' => $rowDimension->getVisible(),
'setXfIndex' => $rowDimension->getXfIndex(),
'setZeroHeight' => $rowDimension->getZeroHeight()
);
$cellIterator = $row->getCellIterator($startColumn, $endColumn);
$replace = array('${ROW}', '${/ROW}');
/** @var Cell $cell */
foreach($cellIterator as $cell) {
$this->_cloneRow[] = array(
'column' => $cell->getColumn(),
'value' => str_replace($replace, '', $cell->getValue()),
'XFIndex' => $cell->getXfIndex()
);
$cell->setValue(null);
$cell->setXfIndex(0);
}
if ($this->_config->group) {
$this->_findGroupStart();
$this->_findGroupEnd();
}
}
protected function _findGroupStart()
{
if (null === ($block = $this->_findCell('${GROUP_START}'))) {
return;
}
$startColumn = $block->getColumn();
$rowOffset = $block->getRow();
if (null === ($block = $this->_findCell('${/GROUP_START}'))) {
return;
}
$endColumn = $block->getColumn();
if ($block->getRow() !== $rowOffset) {
if (Tinebase_Core::isLogLevel(Zend_Log::WARN))
Tinebase_Core::getLogger()->warn(__METHOD__ . ' ' . __LINE__ . ' block tags need to be in the same row');
throw new Tinebase_Exception_UnexpectedValue('block tags need to be in the same row');
}
if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG))
Tinebase_Core::getLogger()->debug(__METHOD__ . ' ' . __LINE__ . ' found group start...');
$sheet = $this->_spreadsheet->getActiveSheet();
/** @var $rowIterator */
$rowIterator = $sheet->getRowIterator($rowOffset);
$row = $rowIterator->current();
$rowDimension = $sheet->getRowDimension($row->getRowIndex());
$this->_cloneGroupStartRowStyles = array(
'setCollapsed' => $rowDimension->getCollapsed(),
'setOutlineLevel' => $rowDimension->getOutlineLevel(),
'setRowHeight' => $rowDimension->getRowHeight(),
'setVisible' => $rowDimension->getVisible(),
'setXfIndex' => $rowDimension->getXfIndex(),
'setZeroHeight' => $rowDimension->getZeroHeight()
);
$cellIterator = $row->getCellIterator($startColumn, $endColumn);
$replace = array('${GROUP_START}', '${/GROUP_START}');
/** @var Cell $cell */
foreach($cellIterator as $cell) {
$this->_cloneGroupStartRow[] = array(
'column' => $cell->getColumn(),
'value' => str_replace($replace, '', $cell->getValue()),
'XFIndex' => $cell->getXfIndex()
);
$cell->setValue(null);
$cell->setXfIndex(0);
}
}
protected function _findGroupEnd()
{
if (null === ($block = $this->_findCell('${GROUP_END}'))) {
return;
}
$startColumn = $block->getColumn();
$rowOffset = $block->getRow();
if (null === ($block = $this->_findCell('${/GROUP_END}'))) {
return;
}
$endColumn = $block->getColumn();
if ($block->getRow() !== $rowOffset) {
if (Tinebase_Core::isLogLevel(Zend_Log::WARN))
Tinebase_Core::getLogger()->warn(__METHOD__ . ' ' . __LINE__ . ' block tags need to be in the same row');
throw new Tinebase_Exception_UnexpectedValue('block tags need to be in the same row');
}
if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG))
Tinebase_Core::getLogger()->debug(__METHOD__ . ' ' . __LINE__ . ' found group end...');
$sheet = $this->_spreadsheet->getActiveSheet();
/** @var $rowIterator */
$rowIterator = $sheet->getRowIterator($rowOffset);
$row = $rowIterator->current();
$rowDimension = $sheet->getRowDimension($row->getRowIndex());
$this->_cloneGroupEndRowStyles = array(
'setCollapsed' => $rowDimension->getCollapsed(),
'setOutlineLevel' => $rowDimension->getOutlineLevel(),
'setRowHeight' => $rowDimension->getRowHeight(),
'setVisible' => $rowDimension->getVisible(),
'setXfIndex' => $rowDimension->getXfIndex(),
'setZeroHeight' => $rowDimension->getZeroHeight()
);
$cellIterator = $row->getCellIterator($startColumn, $endColumn);
$replace = array('${GROUP_END}', '${/GROUP_END}');
/** @var Cell $cell */
foreach($cellIterator as $cell) {
$this->_cloneGroupEndRow[] = array(
'column' => $cell->getColumn(),
'value' => str_replace($replace, '', $cell->getValue()),
'XFIndex' => $cell->getXfIndex()
);
$cell->setValue(null);
$cell->setXfIndex(0);
}
}
/**
* @throws Tinebase_Exception_UnexpectedValue
*/
protected function _replaceTine20ImagePaths()
{
/** @var Drawing $drawing */
foreach($this->_spreadsheet->getActiveSheet()->getDrawingCollection() as $drawing) {
$desc = $drawing->getDescription();
if (strpos($desc, '://') !== false) {
$desc = trim($desc);
if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG))
Tinebase_Core::getLogger()->debug(__METHOD__ . ' ' . __LINE__ . ' found url: ' . $desc);
if (!in_array(mb_strtolower(pathinfo($desc, PATHINFO_EXTENSION)), array('jpg', 'jpeg', 'png', 'gif'))) {
if (Tinebase_Core::isLogLevel(Zend_Log::INFO))
Tinebase_Core::getLogger()->info(__METHOD__ . ' ' . __LINE__ . ' unsupported file extension: ' . $desc);
continue;
}
$fileContent = file_get_contents($desc);
if (!empty($fileContent)) {
$tempFile = Tinebase_TempFile::getTempPath();
if (strlen($fileContent) !== file_put_contents($tempFile, $fileContent)) {
throw new Tinebase_Exception('could not store filecontents in a temp file');
}
$this->_replaceDrawing($tempFile, $drawing);
} else {
if (Tinebase_Core::isLogLevel(Zend_Log::WARN))
Tinebase_Core::getLogger()->warn(__METHOD__ . ' ' . __LINE__ . ' could not get file content: ' . $desc);
throw new Tinebase_Exception_UnexpectedValue('could not get file content: ' . $desc);
}
}
}
}
protected function _replaceDrawing($filePath, Drawing $drawing)
{
list($newWidth, $newHeight) = getimagesize($filePath);
$oldWidth = $drawing->getWidth();
$oldHeight = $drawing->getHeight();
$drawing->setResizeProportional(false);
if ($newWidth <= $oldWidth && $newHeight <= $oldHeight) {
$drawing->setWidth($newWidth);
$drawing->setHeight($newHeight);
} else {
// 0,25 1 4
// 4 4 1
$oldRatio = $oldWidth / $oldHeight;
// 0,75 3 4
$newRatio = $newWidth / $newHeight;
if ($newRatio >= $oldRatio) {
$drawing->setWidth($oldWidth);
$drawing->setHeight((int)($newHeight * $oldWidth / $newWidth));
} else {
$drawing->setHeight($oldHeight);
$drawing->setWidth((int)($newWidth * $oldHeight / $newHeight));
}
}
$drawing->setPath($filePath);
}
/**
* @param $to
* @param $from
* @return null|string
* @throws Tinebase_Exception_NotFound
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
function convert($to, $from = null)
{
if (!$from) {
$from = Tinebase_TempFile::getTempPath();
$this->save($from);
}
switch($to) {
case Tinebase_Export_Convertible::PDF:
return $this->convertToPdf($from);
default:
return null;
}
}
}