Completed
Pull Request — develop (#187)
by Franck
19:04 queued 41s
created

PowerPoint2007::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of PHPPresentation - A pure PHP library for reading and writing
4
 * presentations documents.
5
 *
6
 * PHPPresentation is free software distributed under the terms of the GNU Lesser
7
 * General Public License version 3 as published by the Free Software Foundation.
8
 *
9
 * For the full copyright and license information, please read the LICENSE
10
 * file that was distributed with this source code. For the full list of
11
 * contributors, visit https://github.com/PHPOffice/PHPPresentation/contributors.
12
 *
13
 * @link        https://github.com/PHPOffice/PHPPresentation
14
 * @copyright   2009-2015 PHPPresentation contributors
15
 * @license     http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16
 */
17
18
namespace PhpOffice\PhpPresentation\Writer;
19
20
use DirectoryIterator;
21
use PhpOffice\PhpPresentation\HashTable;
22
use PhpOffice\PhpPresentation\PhpPresentation;
23
use PhpOffice\PhpPresentation\Shape\AbstractDrawing;
24
use PhpOffice\PhpPresentation\Shape\Chart as ChartShape;
25
use PhpOffice\PhpPresentation\Shape\Group;
26
use PhpOffice\PhpPresentation\Shape\Table;
27
use PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack;
28
use PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\PackDefault;
29
30
/**
31
 * \PhpOffice\PhpPresentation\Writer\PowerPoint2007
32
 */
33
class PowerPoint2007 implements WriterInterface
34
{
35
    /**
36
     * Private PhpPresentation
37
     *
38
     * @var \PhpOffice\PhpPresentation\PhpPresentation
39
     */
40
    protected $presentation;
41
42
    /**
43
     * Private unique hash table
44
     *
45
     * @var \PhpOffice\PhpPresentation\HashTable
46
     */
47
    protected $drawingHashTable;
48
49
    /**
50
     * Use disk caching where possible?
51
     *
52
     * @var boolean
53
     */
54
    protected $useDiskCaching = false;
55
56
    /**
57
     * Disk caching directory
58
     *
59
     * @var string
60
     */
61
    protected $diskCachingDir;
62
63
    /**
64
     * Layout pack to use
65
     *
66
     * @var \PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack
67
     */
68
    protected $layoutPack;
69
70
    /**
71
     * Create a new \PhpOffice\PhpPresentation\Writer\PowerPoint2007
72
     *
73
     * @param PhpPresentation $pPhpPresentation
74
     */
75
    public function __construct(PhpPresentation $pPhpPresentation = null)
76
    {
77
        // Assign PhpPresentation
78
        $this->setPhpPresentation($pPhpPresentation);
79
80
        // Set up disk caching location
81
        $this->diskCachingDir = './';
82
83
        // Set layout pack
84
        $this->layoutPack = new PackDefault();
85
86
        // Set HashTable variables
87
        $this->drawingHashTable = new HashTable();
88
    }
89
90
    /**
91
     * Save PhpPresentation to file
92
     *
93
     * @param  string    $pFilename
94
     * @throws \Exception
95
     */
96
    public function save($pFilename)
97 70
    {
98
        if (empty($pFilename)) {
99
            throw new \Exception("Filename is empty");
100 70
        }
101
        if (empty($this->presentation)) {
102
            throw new \Exception("PhpPresentation object unassigned");
103 70
        }
104
        // If $pFilename is php://output or php://stdout, make it a temporary file...
105
        $originalFilename = $pFilename;
106 70
        if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
107
            $pFilename = @tempnam('./', 'phppttmp');
108
            if ($pFilename == '') {
109 70
                $pFilename = $originalFilename;
110 70
            }
111 70
        }
112 70
113 70
        // Create drawing dictionary
114 70
        $this->drawingHashTable->addFromSource($this->allDrawings());
115 70
116 70
        $oZip = new \ZipArchive();
117 70
118
        // Try opening the ZIP file
119
        if ($oZip->open($pFilename, \ZipArchive::OVERWRITE) !== true) {
120 70
            if ($oZip->open($pFilename, \ZipArchive::CREATE) !== true) {
121 70
                throw new \Exception("Could not open " . $pFilename . " for writing.");
122 70
            }
123
        }
124
125 70
        $oDir = new DirectoryIterator(dirname(__FILE__).DIRECTORY_SEPARATOR.'PowerPoint2007');
126 70
        foreach ($oDir as $oFile) {
127
            if (!$oFile->isFile()) {
128
                continue;
129
            }
130
            $class = __NAMESPACE__.'\\PowerPoint2007\\'.$oFile->getBasename('.php');
131
            $o = new \ReflectionClass($class);
132
133
            if ($o->isAbstract() || !$o->isSubclassOf('PhpOffice\PhpPresentation\Writer\PowerPoint2007\AbstractDecoratorWriter')) {
134 62
                continue;
135
            }
136 62
            $oService = $o->newInstance();
137 61
            $oService->setZip($oZip);
138
            $oService->setPresentation($this->presentation);
139 1
            $oService->setDrawingHashTable($this->drawingHashTable);
140
            $oZip = $oService->render();
141
            unset($oService);
142
        }
143
144
        // Close file
145
        if ($oZip->close() === false) {
146
            throw new \Exception("Could not close zip file $pFilename.");
147
        }
148
149 60
        // If a temporary file was used, copy it to the correct file stream
150
        if ($originalFilename != $pFilename) {
151 60
            if (copy($pFilename, $originalFilename) === false) {
152
                throw new \Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
153
            }
154 60
            if (@unlink($pFilename) === false) {
155
                throw new \Exception('The file '.$pFilename.' could not be removed.');
156 60
            }
157 60
        }
158
    }
159
160
    /**
161
     * Get PhpPresentation object
162
     *
163
     * @return PhpPresentation
164 60
     * @throws \Exception
165 60
     */
166
    public function getPhpPresentation()
167
    {
168 60
        if (empty($this->presentation)) {
169 60
            throw new \Exception("No PhpPresentation assigned.");
170
        }
171
        return $this->presentation;
172 60
    }
173 60
174
    /**
175
     * Set PhpPresentation object
176 60
     *
177 60
     * @param  PhpPresentation $pPhpPresentation PhpPresentation object
178
     * @throws \Exception
179
     * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007
180 60
     */
181 60
    public function setPhpPresentation(PhpPresentation $pPhpPresentation = null)
182
    {
183
        $this->presentation = $pPhpPresentation;
184 60
        return $this;
185 60
    }
186
187
    /**
188 60
     * Get use disk caching where possible?
189 60
     *
190
     * @return boolean
191
     */
192 60
    public function hasDiskCaching()
193 60
    {
194
        return $this->useDiskCaching;
195
    }
196 60
197 60
    /**
198
     * Set use disk caching where possible?
199
     *
200
     * @param  boolean $pValue
201
     * @param  string $pDirectory Disk caching directory
202 60
     * @throws \Exception
203
     * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007
204
     */
205 60
    public function setUseDiskCaching($pValue = false, $pDirectory = null)
206
    {
207
        $this->useDiskCaching = $pValue;
208 60
209
        if (!is_null($pDirectory)) {
210
            if (is_dir($pDirectory)) {
211
                $this->diskCachingDir = $pDirectory;
212
            } else {
213
                throw new \Exception("Directory does not exist: $pDirectory");
214
            }
215 60
        }
216
217
        return $this;
218 60
    }
219 60
220 60
    /**
221
     * Get disk caching directory
222
     *
223 60
     * @return string
224 60
     */
225
    public function getDiskCachingDirectory()
226
    {
227 60
        return $this->diskCachingDir;
228 60
    }
229 60
230
    /**
231 60
     * Get layout pack to use
232 60
     *
233
     * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack
234 60
     */
235 60
    public function getLayoutPack()
236
    {
237 60
        return $this->layoutPack;
238 60
    }
239 60
240
    /**
241
     * Set layout pack to use
242 60
     *
243 60
     * @param \PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack $pValue
244 60
     * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007
245 60
     */
246 60
    public function setLayoutPack(AbstractLayoutPack $pValue = null)
247
    {
248
        $this->layoutPack = $pValue;
249 60
250 60
        return $this;
251
    }
252
253
    /**
254 60
     * Get an array of all drawings
255 60
     *
256 60
     * @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing[] All drawings in PhpPresentation
257
     * @throws \Exception
258
     */
259
    protected function allDrawings()
260 60
    {
261 60
        // Get an array of all drawings
262 60
        $aDrawings  = array();
263
264
        // Loop trough PhpPresentation
265
        foreach ($this->getPhpPresentation()->getAllSlides() as $oSlide) {
266 60
            $oCollection = $oSlide->getShapeCollection();
267
            if ($oCollection->count() <= 0) {
268
                continue;
269 60
            }
270
            $oIterator = $oCollection->getIterator();
271
            while ($oIterator->valid()) {
272 60
                if ($oIterator->current() instanceof AbstractDrawing && !($oIterator->current() instanceof Table)) {
273 60
                    $aDrawings[] = $oIterator->current();
274
                } elseif ($oIterator->current() instanceof Group) {
275 60
                    $oSubIterator = $oIterator->current()->getShapeCollection()->getIterator();
276 60
                    while ($oSubIterator->valid()) {
277
                        if ($oSubIterator->current() instanceof AbstractDrawing && !($oSubIterator->current() instanceof Table)) {
278 60
                            $aDrawings[] = $oSubIterator->current();
279 1
                        }
280 1
                        $oSubIterator->next();
281
                    }
282 60
                }
283 60
284
                $oIterator->next();
285
            }
286 60
        }
287
288
        return $aDrawings;
289 60
    }
290
}
291