Skip to content

Commit

Permalink
Bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
viames committed Apr 18, 2023
1 parent c52d15c commit 7cd29e3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 107 deletions.
109 changes: 6 additions & 103 deletions src/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,111 +77,11 @@ protected function addRow(array $indexedCellsValue): void {
}

/**
* Generic method for creating an Excel document from an array of data.
* @deprecated
*/
protected function createSpreadsheetFromData(string $documentTitle, array $data, array $columnsDef): Spreadsheet {

$spreadsheet = new Spreadsheet();
$spreadsheet->setActiveSheetIndex(0);
$activeSheet = $spreadsheet->getActiveSheet();

foreach ($columnsDef as $col => $def) {
if (!isset($def->title)) {
throw new \Exception('Titolo colonna [' . $col . '] non definito');
}
$activeSheet->getCell([$col+1, 1])->setValue($def->title)->getStyle()->getFont()->setBold(TRUE);
}

$row = 2;

foreach ($data as $o) {

foreach ($columnsDef as $col => $def) {

$propertyName = $def->property ?? $def->title;
$value = $o->$propertyName;

$cell = $activeSheet->getCell([$col+1, $row]);

if (isset($def->format)) {

switch ($def->format) {

case 'numeric':
$cell->setValueExplicit($value, DataType::TYPE_NUMERIC);
break;

case 'currency':
$cell->setValueExplicit($value, DataType::TYPE_NUMERIC);
$cell->getStyle()->getNumberFormat()->setFormatCode('#,##0.00');
break;

case 'stringDate':
$dt = \DateTime::createFromFormat('Y-m-d', $value);
if (is_a($dt,'DateTime')) {
$cell->getStyle()->getNumberFormat()->setFormatCode('DD/MM/YYYY');
$cell->setValue($dt->format('d/m/Y'));
}
break;

case 'stringDateTime':
$dt = \DateTime::createFromFormat('Y-m-d H:i:s', $value);
if (is_a($dt,'DateTime')) {
$cell->getStyle()->getNumberFormat()->setFormatCode('DD/MM/YYYY HH:MM:SS');
$cell->setValue($dt->format('d/m/Y H:i:s'));
}
break;

case 'DateTime':
$cell->getStyle()->getNumberFormat()->setFormatCode('DD/MM/YYYY HH:MM:SS');
$cell->setValue($value->format('d/m/Y H:i:s'));
break;

case 'string':
default:
$cell->setValueExplicit($value, DataType::TYPE_STRING);
break;

}

} else {

$cell->setValue($value);

}

}

$row++;

}

$spreadsheet->getProperties()
->setCreator(PRODUCT_NAME . ' ' . PRODUCT_VERSION)
->setLastModifiedBy(PRODUCT_NAME . ' ' . PRODUCT_VERSION)
->setTitle($documentTitle)
->setSubject($documentTitle);

return $spreadsheet;

}

/**
* Old function to download the past Excel file.
* @deprecated
* Return the number of rows of this Report.
*/
public function downloadLegacy(Spreadsheet $spreadsheet): void {

$filename = Utilities::localCleanFilename($spreadsheet->getProperties()->getTitle() . '.xls');

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $filename);
header('Cache-Control: max-age=0');
protected function countRows(): int {

// output the file to the browser
$xlsWriter = new Xls($spreadsheet);
$xlsWriter->save('php://output');
return count($this->data);

}

Expand Down Expand Up @@ -341,6 +241,9 @@ protected function setColumn(int $index, string $head, ?string $format = NULL):

}

/**
* Set column headers and cell values using an associative array.
*/
protected function setDataAndColumnsFromDictionary(array $dictionary): self {

// empty list
Expand Down
6 changes: 6 additions & 0 deletions src/Upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class Upload {
*/
protected $fileTmpname;

/**
* Array key type as coming by $_FILE variable.
* @var string
*/
protected $fileType;

/**
* MIME data for this file.
* @var string
Expand Down
14 changes: 10 additions & 4 deletions src/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,21 +186,26 @@ public static function varToText($var, bool $showTypes=TRUE, ?int $indent=0): st
switch ($type) {

case 'boolean':
$text .= $var ? 'TRUE' : 'FALSE';
$text .= $var ? 'true' : 'false';
break;

case 'integer':
case 'double':
$text .= $var;
break;

default:
if (self::isJson($var)) {
$text .= nl2br($var);
$text .= '"' . nl2br($var) . '"';
} else {
$text .= $var;
$text .= '"' . $var . '"';
}
break;

case 'array':
$parts = [];
foreach ($var as $k=>$v) {
$parts[] = $getIndent(1) . $k . '=>' . self::varToText($v, $showTypes, $indent+1);
$parts[] = $getIndent(1) . '"' . $k . '"=' . self::varToText($v, $showTypes, $indent+1);
}
$text .= $getIndent(1) . '[<br>' . implode(',<br>', $parts) . '<br>'. $getIndent(-1) . ']<br>';
break;
Expand All @@ -212,6 +217,7 @@ public static function varToText($var, bool $showTypes=TRUE, ?int $indent=0): st
break;

case 'NULL':
$text .= 'null';
break;

}
Expand Down

0 comments on commit 7cd29e3

Please sign in to comment.