Completed
Pull Request — develop (#186)
by
unknown
07:55
created

PowerPoint2007::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 1
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 85
    public function __construct(PhpPresentation $pPhpPresentation = null)
76
    {
77
        // Assign PhpPresentation
78 85
        $this->setPhpPresentation($pPhpPresentation);
79
80
        // Set up disk caching location
81 85
        $this->diskCachingDir = './';
82
83
        // Set layout pack
84 85
        $this->layoutPack = new PackDefault();
85
86
        // Set HashTable variables
87 85
        $this->drawingHashTable = new HashTable();
88 85
    }
89
90
    /**
91
     * Save PhpPresentation to file
92
     *
93
     * @param  string    $pFilename
94
     * @throws \Exception
95
     */
96 77
    public function save($pFilename)
97
    {
98 77
        if (empty($pFilename)) {
99 1
            throw new \Exception("Filename is empty");
100
        }
101 76
        if (empty($this->presentation)) {
102 1
            throw new \Exception("PhpPresentation object unassigned");
103
        }
104
        // If $pFilename is php://output or php://stdout, make it a temporary file...
105 75
        $originalFilename = $pFilename;
106 75
        if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
107
            $pFilename = @tempnam('./', 'phppttmp');
108
            if ($pFilename == '') {
109
                $pFilename = $originalFilename;
110
            }
111
        }
112
113
        // Create drawing dictionary
114 75
        $this->drawingHashTable->addFromSource($this->allDrawings());
115
116 75
        $oZip = new \ZipArchive();
117
118
        // Try opening the ZIP file
119 75
        if ($oZip->open($pFilename, \ZipArchive::OVERWRITE) !== true) {
120
            if ($oZip->open($pFilename, \ZipArchive::CREATE) !== true) {
121
                throw new \Exception("Could not open " . $pFilename . " for writing.");
122
            }
123
        }
124
125 75
        $oDir = new DirectoryIterator(dirname(__FILE__).DIRECTORY_SEPARATOR.'PowerPoint2007');
126 75
        foreach ($oDir as $oFile) {
127 75
            if (!$oFile->isFile()) {
128 75
                continue;
129
            }
130 75
            $class = __NAMESPACE__.'\\PowerPoint2007\\'.$oFile->getBasename('.php');
131 75
            $o = new \ReflectionClass($class);
132
133 75
            if ($o->isAbstract() || !$o->isSubclassOf('PhpOffice\PhpPresentation\Writer\PowerPoint2007\AbstractDecoratorWriter')) {
134 75
                continue;
135
            }
136 75
            $oService = $o->newInstance();
137 75
            $oService->setZip($oZip);
138 75
            $oService->setPresentation($this->presentation);
139 75
            $oService->setDrawingHashTable($this->drawingHashTable);
140 75
            $oZip = $oService->render();
141 75
            unset($oService);
142
        }
143
144
        // Close file
145 74
        if ($oZip->close() === false) {
146
            throw new \Exception("Could not close zip file $pFilename.");
147
        }
148
149
        // If a temporary file was used, copy it to the correct file stream
150 74
        if ($originalFilename != $pFilename) {
151
            if (copy($pFilename, $originalFilename) === false) {
152
                throw new \Exception("Could not copy temporary zip file $pFilename to $originalFilename.");
153
            }
154
            if (@unlink($pFilename) === false) {
155
                throw new \Exception('The file '.$pFilename.' could not be removed.');
156
            }
157
        }
158 74
    }
159
160
    /**
161
     * Get PhpPresentation object
162
     *
163
     * @return PhpPresentation
164
     * @throws \Exception
165
     */
166 77
    public function getPhpPresentation()
167
    {
168 77
        if (empty($this->presentation)) {
169 1
            throw new \Exception("No PhpPresentation assigned.");
170
        }
171 76
        return $this->presentation;
172
    }
173
174
    /**
175
     * Set PhpPresentation object
176
     *
177
     * @param  PhpPresentation $pPhpPresentation PhpPresentation object
178
     * @throws \Exception
179
     * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007
180
     */
181 85
    public function setPhpPresentation(PhpPresentation $pPhpPresentation = null)
182
    {
183 85
        $this->presentation = $pPhpPresentation;
184 85
        return $this;
185
    }
186
187
    /**
188
     * Get use disk caching where possible?
189
     *
190
     * @return boolean
191
     */
192 1
    public function hasDiskCaching()
193
    {
194 1
        return $this->useDiskCaching;
195
    }
196
197
    /**
198
     * Set use disk caching where possible?
199
     *
200
     * @param  boolean $pValue
201
     * @param  string $pDirectory Disk caching directory
202
     * @throws \Exception
203
     * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007
204
     */
205 2
    public function setUseDiskCaching($pValue = false, $pDirectory = null)
206
    {
207 2
        $this->useDiskCaching = $pValue;
208
209 2
        if (!is_null($pDirectory)) {
210 2
            if (is_dir($pDirectory)) {
211 1
                $this->diskCachingDir = $pDirectory;
212
            } else {
213 1
                throw new \Exception("Directory does not exist: $pDirectory");
214
            }
215
        }
216
217 1
        return $this;
218
    }
219
220
    /**
221
     * Get disk caching directory
222
     *
223
     * @return string
224
     */
225 2
    public function getDiskCachingDirectory()
226
    {
227 2
        return $this->diskCachingDir;
228
    }
229
230
    /**
231
     * Get layout pack to use
232
     *
233
     * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack
234
     */
235 2
    public function getLayoutPack()
236
    {
237 2
        return $this->layoutPack;
238
    }
239
240
    /**
241
     * Set layout pack to use
242
     *
243
     * @param \PhpOffice\PhpPresentation\Writer\PowerPoint2007\LayoutPack\AbstractLayoutPack $pValue
244
     * @return \PhpOffice\PhpPresentation\Writer\PowerPoint2007
245
     */
246 1
    public function setLayoutPack(AbstractLayoutPack $pValue = null)
247
    {
248 1
        $this->layoutPack = $pValue;
249
250 1
        return $this;
251
    }
252
253
    /**
254
     * Get an array of all drawings
255
     *
256
     * @return \PhpOffice\PhpPresentation\Shape\AbstractDrawing[] All drawings in PhpPresentation
257
     * @throws \Exception
258
     */
259 75
    protected function allDrawings()
260
    {
261
        // Get an array of all drawings
262 75
        $aDrawings  = array();
263
264
        // Loop trough PhpPresentation
265 75
        foreach ($this->getPhpPresentation()->getAllSlides() as $oSlide) {
266 75
            $oCollection = $oSlide->getShapeCollection();
267 75
            if ($oCollection->count() <= 0) {
268 19
                continue;
269
            }
270 56
            $oIterator = $oCollection->getIterator();
271 56
            while ($oIterator->valid()) {
272 56
                if ($oIterator->current() instanceof AbstractDrawing && !($oIterator->current() instanceof Table)) {
273 26
                    $aDrawings[] = $oIterator->current();
274 30
                } elseif ($oIterator->current() instanceof Group) {
275
                    $oSubIterator = $oIterator->current()->getShapeCollection()->getIterator();
276
                    while ($oSubIterator->valid()) {
277
                        if ($oSubIterator->current() instanceof AbstractDrawing && !($oSubIterator->current() instanceof Table)) {
278
                            $aDrawings[] = $oSubIterator->current();
279
                        }
280
                        $oSubIterator->next();
281
                    }
282
                }
283
284 56
                $oIterator->next();
285
            }
286
        }
287
288 75
        return $aDrawings;
289
    }
290
}
291