| 1 |
<?php |
| 2 |
|
| 3 |
namespace Box\Spout\Writer\ODS\Internal; |
| 4 |
|
| 5 |
use Box\Spout\Writer\Common\Internal\AbstractWorkbook; |
| 6 |
use Box\Spout\Writer\ODS\Helper\FileSystemHelper; |
| 7 |
use Box\Spout\Writer\ODS\Helper\StyleHelper; |
| 8 |
use Box\Spout\Writer\Common\Sheet; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class Workbook |
| 12 |
* Represents a workbook within a ODS file. |
| 13 |
* It provides the functions to work with worksheets. |
| 14 |
* |
| 15 |
* @package Box\Spout\Writer\ODS\Internal |
| 16 |
*/ |
| 17 |
class Workbook extends AbstractWorkbook |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Maximum number of rows a ODS sheet can contain |
| 21 |
* @see https://ask.libreoffice.org/en/question/8631/upper-limit-to-number-of-rows-in-calc/ |
| 22 |
*/ |
| 23 |
protected static $maxRowsPerWorksheet = 1048576; |
| 24 |
|
| 25 |
/** @var \Box\Spout\Writer\ODS\Helper\FileSystemHelper Helper to perform file system operations */ |
| 26 |
protected $fileSystemHelper; |
| 27 |
|
| 28 |
/** @var \Box\Spout\Writer\ODS\Helper\StyleHelper Helper to apply styles */ |
| 29 |
protected $styleHelper; |
| 30 |
|
| 31 |
/** |
| 32 |
* @param string $tempFolder |
| 33 |
* @param bool $shouldCreateNewSheetsAutomatically |
| 34 |
* @param \Box\Spout\Writer\Style\Style $defaultRowStyle |
| 35 |
* @throws \Box\Spout\Common\Exception\IOException If unable to create at least one of the base folders |
| 36 |
*/ |
| 37 |
public function __construct($tempFolder, $shouldCreateNewSheetsAutomatically, $defaultRowStyle) |
| 38 |
{ |
| 39 |
parent::__construct($shouldCreateNewSheetsAutomatically, $defaultRowStyle); |
| 40 |
|
| 41 |
$this->fileSystemHelper = new FileSystemHelper($tempFolder); |
| 42 |
$this->fileSystemHelper->createBaseFilesAndFolders(); |
| 43 |
|
| 44 |
$this->styleHelper = new StyleHelper($defaultRowStyle); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @return \Box\Spout\Writer\ODS\Helper\StyleHelper Helper to apply styles to ODS files |
| 49 |
*/ |
| 50 |
protected function getStyleHelper() |
| 51 |
{ |
| 52 |
return $this->styleHelper; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* @return int Maximum number of rows/columns a sheet can contain |
| 57 |
*/ |
| 58 |
protected function getMaxRowsPerWorksheet() |
| 59 |
{ |
| 60 |
return self::$maxRowsPerWorksheet; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Creates a new sheet in the workbook. The current sheet remains unchanged. |
| 65 |
* |
| 66 |
* @return Worksheet The created sheet |
| 67 |
* @throws \Box\Spout\Common\Exception\IOException If unable to open the sheet for writing |
| 68 |
*/ |
| 69 |
public function addNewSheet() |
| 70 |
{ |
| 71 |
$newSheetIndex = count($this->worksheets); |
| 72 |
$sheet = new Sheet($newSheetIndex, $this->internalId); |
| 73 |
|
| 74 |
$sheetsContentTempFolder = $this->fileSystemHelper->getSheetsContentTempFolder(); |
| 75 |
$worksheet = new Worksheet($sheet, $sheetsContentTempFolder); |
| 76 |
$this->worksheets[] = $worksheet; |
| 77 |
|
| 78 |
return $worksheet; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Closes the workbook and all its associated sheets. |
| 83 |
* All the necessary files are written to disk and zipped together to create the ODS file. |
| 84 |
* All the temporary files are then deleted. |
| 85 |
* |
| 86 |
* @param resource $finalFilePointer Pointer to the ODS that will be created |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public function close($finalFilePointer) |
| 90 |
{ |
| 91 |
/** @var Worksheet[] $worksheets */ |
| 92 |
$worksheets = $this->worksheets; |
| 93 |
$numWorksheets = count($worksheets); |
| 94 |
|
| 95 |
foreach ($worksheets as $worksheet) { |
| 96 |
$worksheet->close(); |
| 97 |
} |
| 98 |
|
| 99 |
// Finish creating all the necessary files before zipping everything together |
| 100 |
$this->fileSystemHelper |
| 101 |
->createContentFile($worksheets, $this->styleHelper) |
| 102 |
->deleteWorksheetTempFolder() |
| 103 |
->createStylesFile($this->styleHelper, $numWorksheets) |
| 104 |
->zipRootFolderAndCopyToStream($finalFilePointer); |
| 105 |
|
| 106 |
$this->cleanupTempFolder(); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Deletes the root folder created in the temp folder and all its contents. |
| 111 |
* |
| 112 |
* @return void |
| 113 |
*/ |
| 114 |
protected function cleanupTempFolder() |
| 115 |
{ |
| 116 |
$xlsxRootFolder = $this->fileSystemHelper->getRootFolder(); |
| 117 |
$this->fileSystemHelper->deleteFolderRecursively($xlsxRootFolder); |
| 118 |
} |
| 119 |
} |
| 120 |
|