Completed
Pull Request — develop (#188)
by Franck
06:55
created

PowerPoint2007::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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