~ubuntu-branches/ubuntu/lucid/phpmyadmin/lucid

« back to all changes in this revision

Viewing changes to libraries/PHPExcel/PHPExcel/Writer/Excel2007/Rels.php

  • Committer: Bazaar Package Importer
  • Author(s): Michal Čihař
  • Date: 2010-03-08 15:25:00 UTC
  • mfrom: (1.2.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20100308152500-6e8hmuqc5co39de5
Tags: 4:3.3.0-1
* New upstream version.
* Rediff debian/patches.
* Fix permissions on mediawiki export extension.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * PHPExcel
 
4
 *
 
5
 * Copyright (c) 2006 - 2009 PHPExcel
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or
 
8
 * modify it under the terms of the GNU Lesser General Public
 
9
 * License as published by the Free Software Foundation; either
 
10
 * version 2.1 of the License, or (at your option) any later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
 * Lesser General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public
 
18
 * License along with this library; if not, write to the Free Software
 
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
20
 *
 
21
 * @category   PHPExcel
 
22
 * @package    PHPExcel_Writer_Excel2007
 
23
 * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
 
24
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
 
25
 * @version    1.7.0, 2009-08-10
 
26
 */
 
27
 
 
28
 
 
29
/** PHPExcel root directory */
 
30
if (!defined('PHPEXCEL_ROOT')) {
 
31
        /**
 
32
         * @ignore
 
33
         */
 
34
        define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../../');
 
35
}
 
36
 
 
37
/** PHPExcel */
 
38
require_once PHPEXCEL_ROOT . 'PHPExcel.php';
 
39
 
 
40
/** PHPExcel_Worksheet */
 
41
require_once PHPEXCEL_ROOT . 'PHPExcel/Worksheet.php';
 
42
 
 
43
/** PHPExcel_Writer_Excel2007 */
 
44
require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007.php';
 
45
 
 
46
/** PHPExcel_Writer_Excel2007_WriterPart */
 
47
require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007/WriterPart.php';
 
48
 
 
49
/** PHPExcel_Shared_XMLWriter */
 
50
require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/XMLWriter.php';
 
51
 
 
52
 
 
53
/**
 
54
 * PHPExcel_Writer_Excel2007_Rels
 
55
 *
 
56
 * @category   PHPExcel
 
57
 * @package    PHPExcel_Writer_Excel2007
 
58
 * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
 
59
 */
 
60
class PHPExcel_Writer_Excel2007_Rels extends PHPExcel_Writer_Excel2007_WriterPart
 
61
{
 
62
        /**
 
63
         * Write relationships to XML format
 
64
         *
 
65
         * @param       PHPExcel        $pPHPExcel
 
66
         * @return      string          XML Output
 
67
         * @throws      Exception
 
68
         */
 
69
        public function writeRelationships(PHPExcel $pPHPExcel = null)
 
70
        {
 
71
                // Create XML writer
 
72
                $objWriter = null;
 
73
                if ($this->getParentWriter()->getUseDiskCaching()) {
 
74
                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
 
75
                } else {
 
76
                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
 
77
                }
 
78
 
 
79
                // XML header
 
80
                $objWriter->startDocument('1.0','UTF-8','yes');
 
81
 
 
82
                // Relationships
 
83
                $objWriter->startElement('Relationships');
 
84
                $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
 
85
 
 
86
                        // Relationship docProps/app.xml
 
87
                        $this->_writeRelationship(
 
88
                                $objWriter,
 
89
                                3,
 
90
                                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
 
91
                                'docProps/app.xml'
 
92
                        );
 
93
 
 
94
                        // Relationship docProps/core.xml
 
95
                        $this->_writeRelationship(
 
96
                                $objWriter,
 
97
                                2,
 
98
                                'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
 
99
                                'docProps/core.xml'
 
100
                        );
 
101
 
 
102
                        // Relationship xl/workbook.xml
 
103
                        $this->_writeRelationship(
 
104
                                $objWriter,
 
105
                                1,
 
106
                                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
 
107
                                'xl/workbook.xml'
 
108
                        );
 
109
 
 
110
                $objWriter->endElement();
 
111
 
 
112
                // Return
 
113
                return $objWriter->getData();
 
114
        }
 
115
 
 
116
        /**
 
117
         * Write workbook relationships to XML format
 
118
         *
 
119
         * @param       PHPExcel        $pPHPExcel
 
120
         * @return      string          XML Output
 
121
         * @throws      Exception
 
122
         */
 
123
        public function writeWorkbookRelationships(PHPExcel $pPHPExcel = null)
 
124
        {
 
125
                // Create XML writer
 
126
                $objWriter = null;
 
127
                if ($this->getParentWriter()->getUseDiskCaching()) {
 
128
                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
 
129
                } else {
 
130
                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
 
131
                }
 
132
 
 
133
                // XML header
 
134
                $objWriter->startDocument('1.0','UTF-8','yes');
 
135
 
 
136
                // Relationships
 
137
                $objWriter->startElement('Relationships');
 
138
                $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
 
139
 
 
140
                        // Relationship styles.xml
 
141
                        $this->_writeRelationship(
 
142
                                $objWriter,
 
143
                                1,
 
144
                                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
 
145
                                'styles.xml'
 
146
                        );
 
147
 
 
148
                        // Relationship theme/theme1.xml
 
149
                        $this->_writeRelationship(
 
150
                                $objWriter,
 
151
                                2,
 
152
                                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
 
153
                                'theme/theme1.xml'
 
154
                        );
 
155
 
 
156
                        // Relationship sharedStrings.xml
 
157
                        $this->_writeRelationship(
 
158
                                $objWriter,
 
159
                                3,
 
160
                                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings',
 
161
                                'sharedStrings.xml'
 
162
                        );
 
163
 
 
164
                        // Relationships with sheets
 
165
                        $sheetCount = $pPHPExcel->getSheetCount();
 
166
                        for ($i = 0; $i < $sheetCount; ++$i) {
 
167
                                $this->_writeRelationship(
 
168
                                        $objWriter,
 
169
                                        ($i + 1 + 3),
 
170
                                        'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet',
 
171
                                        'worksheets/sheet' . ($i + 1) . '.xml'
 
172
                                );
 
173
                        }
 
174
 
 
175
                $objWriter->endElement();
 
176
 
 
177
                // Return
 
178
                return $objWriter->getData();
 
179
        }
 
180
 
 
181
        /**
 
182
         * Write worksheet relationships to XML format
 
183
         *
 
184
         * Numbering is as follows:
 
185
         *      rId1                            - Drawings
 
186
         *  rId_hyperlink_x     - Hyperlinks
 
187
         *
 
188
         * @param       PHPExcel_Worksheet              $pWorksheet
 
189
         * @param       int                                             $pWorksheetId
 
190
         * @return      string                                  XML Output
 
191
         * @throws      Exception
 
192
         */
 
193
        public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet = null, $pWorksheetId = 1)
 
194
        {
 
195
                // Create XML writer
 
196
                $objWriter = null;
 
197
                if ($this->getParentWriter()->getUseDiskCaching()) {
 
198
                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
 
199
                } else {
 
200
                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
 
201
                }
 
202
 
 
203
                // XML header
 
204
                $objWriter->startDocument('1.0','UTF-8','yes');
 
205
 
 
206
                // Relationships
 
207
                $objWriter->startElement('Relationships');
 
208
                $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
 
209
 
 
210
                        // Write drawing relationships?
 
211
                        if ($pWorksheet->getDrawingCollection()->count() > 0) {
 
212
                                $this->_writeRelationship(
 
213
                                        $objWriter,
 
214
                                        1,
 
215
                                        'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
 
216
                                        '../drawings/drawing' . $pWorksheetId . '.xml'
 
217
                                );
 
218
                        }
 
219
 
 
220
                        // Write hyperlink relationships?
 
221
                        $i = 1;
 
222
                        foreach ($pWorksheet->getCellCollection() as $cell) {
 
223
                                if ($cell->hasHyperlink() && !$cell->getHyperlink()->isInternal()) {
 
224
                                        $this->_writeRelationship(
 
225
                                                $objWriter,
 
226
                                                '_hyperlink_' . $i,
 
227
                                                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
 
228
                                                $cell->getHyperlink()->getUrl(),
 
229
                                                'External'
 
230
                                        );
 
231
 
 
232
                                        ++$i;
 
233
                                }
 
234
                        }
 
235
 
 
236
                        // Write comments relationship?
 
237
                        $i = 1;
 
238
                        if (count($pWorksheet->getComments()) > 0) {
 
239
                                $this->_writeRelationship(
 
240
                                        $objWriter,
 
241
                                        '_comments_vml' . $i,
 
242
                                        'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
 
243
                                        '../drawings/vmlDrawing' . $pWorksheetId . '.vml'
 
244
                                );
 
245
 
 
246
                                $this->_writeRelationship(
 
247
                                        $objWriter,
 
248
                                        '_comments' . $i,
 
249
                                        'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
 
250
                                        '../comments' . $pWorksheetId . '.xml'
 
251
                                );
 
252
                        }
 
253
 
 
254
                        // Write header/footer relationship?
 
255
                        $i = 1;
 
256
                        if (count($pWorksheet->getHeaderFooter()->getImages()) > 0) {
 
257
                                $this->_writeRelationship(
 
258
                                        $objWriter,
 
259
                                        '_headerfooter_vml' . $i,
 
260
                                        'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
 
261
                                        '../drawings/vmlDrawingHF' . $pWorksheetId . '.vml'
 
262
                                );
 
263
                        }
 
264
 
 
265
                $objWriter->endElement();
 
266
 
 
267
                // Return
 
268
                return $objWriter->getData();
 
269
        }
 
270
 
 
271
        /**
 
272
         * Write drawing relationships to XML format
 
273
         *
 
274
         * @param       PHPExcel_Worksheet                      $pWorksheet
 
275
         * @return      string                                          XML Output
 
276
         * @throws      Exception
 
277
         */
 
278
        public function writeDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
 
279
        {
 
280
                // Create XML writer
 
281
                $objWriter = null;
 
282
                if ($this->getParentWriter()->getUseDiskCaching()) {
 
283
                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
 
284
                } else {
 
285
                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
 
286
                }
 
287
 
 
288
                // XML header
 
289
                $objWriter->startDocument('1.0','UTF-8','yes');
 
290
 
 
291
                // Relationships
 
292
                $objWriter->startElement('Relationships');
 
293
                $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
 
294
 
 
295
                        // Loop trough images and write relationships
 
296
                        $i = 1;
 
297
                        $iterator = $pWorksheet->getDrawingCollection()->getIterator();
 
298
                        while ($iterator->valid()) {
 
299
                                if ($iterator->current() instanceof PHPExcel_Worksheet_Drawing
 
300
                                        || $iterator->current() instanceof PHPExcel_Worksheet_MemoryDrawing) {
 
301
                                        // Write relationship for image drawing
 
302
                                        $this->_writeRelationship(
 
303
                                                $objWriter,
 
304
                                                $i,
 
305
                                                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
 
306
                                                '../media/' . str_replace(' ', '', $iterator->current()->getIndexedFilename())
 
307
                                        );
 
308
                                }
 
309
 
 
310
                                $iterator->next();
 
311
                                ++$i;
 
312
                        }
 
313
 
 
314
                $objWriter->endElement();
 
315
 
 
316
                // Return
 
317
                return $objWriter->getData();
 
318
        }
 
319
 
 
320
        /**
 
321
         * Write header/footer drawing relationships to XML format
 
322
         *
 
323
         * @param       PHPExcel_Worksheet                      $pWorksheet
 
324
         * @return      string                                          XML Output
 
325
         * @throws      Exception
 
326
         */
 
327
        public function writeHeaderFooterDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
 
328
        {
 
329
                // Create XML writer
 
330
                $objWriter = null;
 
331
                if ($this->getParentWriter()->getUseDiskCaching()) {
 
332
                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
 
333
                } else {
 
334
                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
 
335
                }
 
336
 
 
337
                // XML header
 
338
                $objWriter->startDocument('1.0','UTF-8','yes');
 
339
 
 
340
                // Relationships
 
341
                $objWriter->startElement('Relationships');
 
342
                $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
 
343
 
 
344
                        // Loop trough images and write relationships
 
345
                        foreach ($pWorksheet->getHeaderFooter()->getImages() as $key => $value) {
 
346
                                // Write relationship for image drawing
 
347
                                $this->_writeRelationship(
 
348
                                        $objWriter,
 
349
                                        $key,
 
350
                                        'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
 
351
                                        '../media/' . $value->getIndexedFilename()
 
352
                                );
 
353
                        }
 
354
 
 
355
                $objWriter->endElement();
 
356
 
 
357
                // Return
 
358
                return $objWriter->getData();
 
359
        }
 
360
 
 
361
        /**
 
362
         * Write Override content type
 
363
         *
 
364
         * @param       PHPExcel_Shared_XMLWriter       $objWriter              XML Writer
 
365
         * @param       int                                                     $pId                    Relationship ID. rId will be prepended!
 
366
         * @param       string                                          $pType                  Relationship type
 
367
         * @param       string                                          $pTarget                Relationship target
 
368
         * @param       string                                          $pTargetMode    Relationship target mode
 
369
         * @throws      Exception
 
370
         */
 
371
        private function _writeRelationship(PHPExcel_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '')
 
372
        {
 
373
                if ($pType != '' && $pTarget != '') {
 
374
                        // Write relationship
 
375
                        $objWriter->startElement('Relationship');
 
376
                        $objWriter->writeAttribute('Id',                'rId' . $pId);
 
377
                        $objWriter->writeAttribute('Type',              $pType);
 
378
                        $objWriter->writeAttribute('Target',    $pTarget);
 
379
 
 
380
                        if ($pTargetMode != '') {
 
381
                                $objWriter->writeAttribute('TargetMode',        $pTargetMode);
 
382
                        }
 
383
 
 
384
                        $objWriter->endElement();
 
385
                } else {
 
386
                        throw new Exception("Invalid parameters passed.");
 
387
                }
 
388
        }
 
389
}