1 | <?php |
||
30 | class ODPresentation extends AbstractWriter implements WriterInterface |
||
31 | { |
||
32 | /** |
||
33 | * @var \PhpOffice\PhpPresentation\Shape\Chart[] |
||
34 | */ |
||
35 | public $chartArray = array(); |
||
36 | |||
37 | /** |
||
38 | * Use disk caching where possible? |
||
39 | * |
||
40 | * @var boolean |
||
41 | */ |
||
42 | private $useDiskCaching = false; |
||
43 | |||
44 | /** |
||
45 | * Disk caching directory |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | private $diskCachingDirectory; |
||
50 | |||
51 | /** |
||
52 | * Create a new \PhpOffice\PhpPresentation\Writer\ODPresentation |
||
53 | * |
||
54 | * @param PhpPresentation $pPhpPresentation |
||
55 | */ |
||
56 | 63 | public function __construct(PhpPresentation $pPhpPresentation = null) |
|
69 | |||
70 | /** |
||
71 | * Save PhpPresentation to file |
||
72 | * |
||
73 | * @param string $pFilename |
||
74 | * @throws \Exception |
||
75 | */ |
||
76 | 59 | public function save($pFilename) |
|
77 | { |
||
78 | 59 | if (empty($pFilename)) { |
|
79 | 1 | throw new \Exception("Filename is empty"); |
|
80 | } |
||
81 | // If $pFilename is php://output or php://stdout, make it a temporary file... |
||
82 | 58 | $originalFilename = $pFilename; |
|
83 | 58 | if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') { |
|
84 | $pFilename = @tempnam('./', 'phppttmp'); |
||
85 | if ($pFilename == '') { |
||
86 | $pFilename = $originalFilename; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | // Initialize HashTable |
||
91 | 58 | $this->getDrawingHashTable()->addFromSource($this->allDrawings()); |
|
92 | |||
93 | // Initialize Zip |
||
94 | 58 | $oZip = $this->getZipAdapter(); |
|
95 | 58 | $oZip->open($pFilename); |
|
96 | |||
97 | // Variables |
||
98 | 58 | $oPresentation = $this->getPhpPresentation(); |
|
99 | 58 | $arrayChart = array(); |
|
100 | |||
101 | 58 | $arrayFiles = array(); |
|
102 | 58 | $oDir = new DirectoryIterator(dirname(__FILE__).DIRECTORY_SEPARATOR.'ODPresentation'); |
|
103 | 58 | foreach ($oDir as $oFile) { |
|
104 | 58 | if (!$oFile->isFile()) { |
|
105 | 58 | continue; |
|
106 | } |
||
107 | |||
108 | 58 | $class = __NAMESPACE__ . '\\ODPresentation\\' . $oFile->getBasename('.php'); |
|
109 | 58 | $o = new \ReflectionClass($class); |
|
110 | |||
111 | 58 | if ($o->isAbstract() || !$o->isSubclassOf('PhpOffice\PhpPresentation\Writer\ODPresentation\AbstractDecoratorWriter')) { |
|
112 | 58 | continue; |
|
113 | } |
||
114 | 58 | $arrayFiles[$oFile->getBasename('.php')] = $o; |
|
115 | 58 | } |
|
116 | |||
117 | 58 | ksort($arrayFiles); |
|
118 | |||
119 | 58 | foreach ($arrayFiles as $o) { |
|
120 | 58 | $oService = $o->newInstance(); |
|
121 | 58 | $oService->setZip($oZip); |
|
122 | 58 | $oService->setPresentation($oPresentation); |
|
123 | 58 | $oService->setDrawingHashTable($this->getDrawingHashTable()); |
|
124 | 58 | $oService->setArrayChart($arrayChart); |
|
125 | 58 | $oZip = $oService->render(); |
|
126 | 58 | $arrayChart = $oService->getArrayChart(); |
|
127 | 58 | unset($oService); |
|
128 | 58 | } |
|
129 | |||
130 | // Close file |
||
131 | 57 | $oZip->close(); |
|
132 | |||
133 | // If a temporary file was used, copy it to the correct file stream |
||
134 | 57 | if ($originalFilename != $pFilename) { |
|
135 | if (copy($pFilename, $originalFilename) === false) { |
||
136 | throw new \Exception("Could not copy temporary zip file $pFilename to $originalFilename."); |
||
137 | } |
||
138 | if (@unlink($pFilename) === false) { |
||
139 | throw new \Exception('The file ' . $pFilename . ' could not be removed.'); |
||
140 | } |
||
141 | } |
||
142 | 57 | } |
|
143 | |||
144 | /** |
||
145 | * Get use disk caching where possible? |
||
146 | * |
||
147 | * @return boolean |
||
148 | */ |
||
149 | 1 | public function hasDiskCaching() |
|
153 | |||
154 | /** |
||
155 | * Set use disk caching where possible? |
||
156 | * |
||
157 | * @param boolean $pValue |
||
158 | * @param string $pDirectory Disk caching directory |
||
159 | * @throws \Exception |
||
160 | * @return \PhpOffice\PhpPresentation\Writer\ODPresentation |
||
161 | */ |
||
162 | 2 | public function setUseDiskCaching($pValue = false, $pDirectory = null) |
|
163 | { |
||
164 | 2 | $this->useDiskCaching = $pValue; |
|
165 | |||
166 | 2 | if (!is_null($pDirectory)) { |
|
167 | 2 | if (!is_dir($pDirectory)) { |
|
168 | 1 | throw new \Exception("Directory does not exist: $pDirectory"); |
|
169 | } |
||
170 | 1 | $this->diskCachingDirectory = $pDirectory; |
|
171 | 1 | } |
|
172 | |||
173 | 1 | return $this; |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * Get disk caching directory |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | 2 | public function getDiskCachingDirectory() |
|
185 | } |
||
186 |