source: sourcecode/application/libraries/PHPExcel/Writer/Excel2007/Rels.php @ 1

Last change on this file since 1 was 1, checked in by dungnv, 11 years ago
File size: 13.1 KB
Line 
1<?php
2/**
3 * PHPExcel
4 *
5 * Copyright (c) 2006 - 2014 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 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
24 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
25 * @version    1.8.0, 2014-03-02
26 */
27
28
29/**
30 * PHPExcel_Writer_Excel2007_Rels
31 *
32 * @category   PHPExcel
33 * @package    PHPExcel_Writer_Excel2007
34 * @copyright  Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
35 */
36class PHPExcel_Writer_Excel2007_Rels extends PHPExcel_Writer_Excel2007_WriterPart
37{
38        /**
39         * Write relationships to XML format
40         *
41         * @param       PHPExcel        $pPHPExcel
42         * @return      string          XML Output
43         * @throws      PHPExcel_Writer_Exception
44         */
45        public function writeRelationships(PHPExcel $pPHPExcel = null)
46        {
47                // Create XML writer
48                $objWriter = null;
49                if ($this->getParentWriter()->getUseDiskCaching()) {
50                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
51                } else {
52                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
53                }
54
55                // XML header
56                $objWriter->startDocument('1.0','UTF-8','yes');
57
58                // Relationships
59                $objWriter->startElement('Relationships');
60                $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
61
62                        $customPropertyList = $pPHPExcel->getProperties()->getCustomProperties();
63                        if (!empty($customPropertyList)) {
64                                // Relationship docProps/app.xml
65                                $this->_writeRelationship(
66                                        $objWriter,
67                                        4,
68                                        'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties',
69                                        'docProps/custom.xml'
70                                );
71
72                        }
73
74                        // Relationship docProps/app.xml
75                        $this->_writeRelationship(
76                                $objWriter,
77                                3,
78                                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
79                                'docProps/app.xml'
80                        );
81
82                        // Relationship docProps/core.xml
83                        $this->_writeRelationship(
84                                $objWriter,
85                                2,
86                                'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties',
87                                'docProps/core.xml'
88                        );
89
90                        // Relationship xl/workbook.xml
91                        $this->_writeRelationship(
92                                $objWriter,
93                                1,
94                                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument',
95                                'xl/workbook.xml'
96                        );
97                        // a custom UI in workbook ?
98                        if($pPHPExcel->hasRibbon()){
99                                $this->_writeRelationShip(
100                                        $objWriter,
101                                        5,
102                                        'http://schemas.microsoft.com/office/2006/relationships/ui/extensibility',
103                                        $pPHPExcel->getRibbonXMLData('target')
104                                );
105                        }
106
107                $objWriter->endElement();
108
109                // Return
110                return $objWriter->getData();
111        }
112
113        /**
114         * Write workbook relationships to XML format
115         *
116         * @param       PHPExcel        $pPHPExcel
117         * @return      string          XML Output
118         * @throws      PHPExcel_Writer_Exception
119         */
120        public function writeWorkbookRelationships(PHPExcel $pPHPExcel = null)
121        {
122                // Create XML writer
123                $objWriter = null;
124                if ($this->getParentWriter()->getUseDiskCaching()) {
125                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
126                } else {
127                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
128                }
129
130                // XML header
131                $objWriter->startDocument('1.0','UTF-8','yes');
132
133                // Relationships
134                $objWriter->startElement('Relationships');
135                $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
136
137                        // Relationship styles.xml
138                        $this->_writeRelationship(
139                                $objWriter,
140                                1,
141                                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles',
142                                'styles.xml'
143                        );
144
145                        // Relationship theme/theme1.xml
146                        $this->_writeRelationship(
147                                $objWriter,
148                                2,
149                                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
150                                'theme/theme1.xml'
151                        );
152
153                        // Relationship sharedStrings.xml
154                        $this->_writeRelationship(
155                                $objWriter,
156                                3,
157                                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings',
158                                'sharedStrings.xml'
159                        );
160
161                        // Relationships with sheets
162                        $sheetCount = $pPHPExcel->getSheetCount();
163                        for ($i = 0; $i < $sheetCount; ++$i) {
164                                $this->_writeRelationship(
165                                        $objWriter,
166                                        ($i + 1 + 3),
167                                        'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet',
168                                        'worksheets/sheet' . ($i + 1) . '.xml'
169                                );
170                        }
171                        // Relationships for vbaProject if needed
172                        // id : just after the last sheet
173                        if($pPHPExcel->hasMacros()){
174                                $this->_writeRelationShip(
175                                        $objWriter,
176                                        ($i + 1 + 3),
177                                        'http://schemas.microsoft.com/office/2006/relationships/vbaProject',
178                                        'vbaProject.bin'
179                                );
180                                ++$i;//increment i if needed for an another relation
181                        }
182
183                $objWriter->endElement();
184
185                // Return
186                return $objWriter->getData();
187        }
188
189        /**
190         * Write worksheet relationships to XML format
191         *
192         * Numbering is as follows:
193         *      rId1                            - Drawings
194         *  rId_hyperlink_x     - Hyperlinks
195         *
196         * @param       PHPExcel_Worksheet      $pWorksheet
197         * @param       int                                     $pWorksheetId
198         * @param       boolean                         $includeCharts  Flag indicating if we should write charts
199         * @return      string                          XML Output
200         * @throws      PHPExcel_Writer_Exception
201         */
202        public function writeWorksheetRelationships(PHPExcel_Worksheet $pWorksheet = null, $pWorksheetId = 1, $includeCharts = FALSE)
203        {
204                // Create XML writer
205                $objWriter = null;
206                if ($this->getParentWriter()->getUseDiskCaching()) {
207                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
208                } else {
209                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
210                }
211
212                // XML header
213                $objWriter->startDocument('1.0','UTF-8','yes');
214
215                // Relationships
216                $objWriter->startElement('Relationships');
217                $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
218
219                        // Write drawing relationships?
220                        $d = 0;
221                        if ($includeCharts) {
222                                $charts = $pWorksheet->getChartCollection();
223                        } else {
224                                $charts = array();
225                        }
226                        if (($pWorksheet->getDrawingCollection()->count() > 0) ||
227                                (count($charts) > 0)) {
228                                $this->_writeRelationship(
229                                        $objWriter,
230                                        ++$d,
231                                        'http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing',
232                                        '../drawings/drawing' . $pWorksheetId . '.xml'
233                                );
234                        }
235
236                        // Write chart relationships?
237//                      $chartCount = 0;
238//                      $charts = $pWorksheet->getChartCollection();
239//                      echo 'Chart Rels: ' , count($charts) , '<br />';
240//                      if (count($charts) > 0) {
241//                              foreach($charts as $chart) {
242//                                      $this->_writeRelationship(
243//                                              $objWriter,
244//                                              ++$d,
245//                                              'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart',
246//                                              '../charts/chart' . ++$chartCount . '.xml'
247//                                      );
248//                              }
249//                      }
250//
251                        // Write hyperlink relationships?
252                        $i = 1;
253                        foreach ($pWorksheet->getHyperlinkCollection() as $hyperlink) {
254                                if (!$hyperlink->isInternal()) {
255                                        $this->_writeRelationship(
256                                                $objWriter,
257                                                '_hyperlink_' . $i,
258                                                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink',
259                                                $hyperlink->getUrl(),
260                                                'External'
261                                        );
262
263                                        ++$i;
264                                }
265                        }
266
267                        // Write comments relationship?
268                        $i = 1;
269                        if (count($pWorksheet->getComments()) > 0) {
270                                $this->_writeRelationship(
271                                        $objWriter,
272                                        '_comments_vml' . $i,
273                                        'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
274                                        '../drawings/vmlDrawing' . $pWorksheetId . '.vml'
275                                );
276
277                                $this->_writeRelationship(
278                                        $objWriter,
279                                        '_comments' . $i,
280                                        'http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments',
281                                        '../comments' . $pWorksheetId . '.xml'
282                                );
283                        }
284
285                        // Write header/footer relationship?
286                        $i = 1;
287                        if (count($pWorksheet->getHeaderFooter()->getImages()) > 0) {
288                                $this->_writeRelationship(
289                                        $objWriter,
290                                        '_headerfooter_vml' . $i,
291                                        'http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing',
292                                        '../drawings/vmlDrawingHF' . $pWorksheetId . '.vml'
293                                );
294                        }
295
296                $objWriter->endElement();
297
298                // Return
299                return $objWriter->getData();
300        }
301
302        /**
303         * Write drawing relationships to XML format
304         *
305         * @param       PHPExcel_Worksheet      $pWorksheet
306         * @param       int                                     &$chartRef              Chart ID
307         * @param       boolean                         $includeCharts  Flag indicating if we should write charts
308         * @return      string                          XML Output
309         * @throws      PHPExcel_Writer_Exception
310         */
311        public function writeDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null, &$chartRef, $includeCharts = FALSE)
312        {
313                // Create XML writer
314                $objWriter = null;
315                if ($this->getParentWriter()->getUseDiskCaching()) {
316                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
317                } else {
318                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
319                }
320
321                // XML header
322                $objWriter->startDocument('1.0','UTF-8','yes');
323
324                // Relationships
325                $objWriter->startElement('Relationships');
326                $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
327
328                        // Loop through images and write relationships
329                        $i = 1;
330                        $iterator = $pWorksheet->getDrawingCollection()->getIterator();
331                        while ($iterator->valid()) {
332                                if ($iterator->current() instanceof PHPExcel_Worksheet_Drawing
333                                        || $iterator->current() instanceof PHPExcel_Worksheet_MemoryDrawing) {
334                                        // Write relationship for image drawing
335                                        $this->_writeRelationship(
336                                                $objWriter,
337                                                $i,
338                                                'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
339                                                '../media/' . str_replace(' ', '', $iterator->current()->getIndexedFilename())
340                                        );
341                                }
342
343                                $iterator->next();
344                                ++$i;
345                        }
346
347                        if ($includeCharts) {
348                                // Loop through charts and write relationships
349                                $chartCount = $pWorksheet->getChartCount();
350                                if ($chartCount > 0) {
351                                        for ($c = 0; $c < $chartCount; ++$c) {
352                                                $this->_writeRelationship(
353                                                        $objWriter,
354                                                        $i++,
355                                                        'http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart',
356                                                        '../charts/chart' . ++$chartRef . '.xml'
357                                                );
358                                        }
359                                }
360                        }
361
362                $objWriter->endElement();
363
364                // Return
365                return $objWriter->getData();
366        }
367
368        /**
369         * Write header/footer drawing relationships to XML format
370         *
371         * @param       PHPExcel_Worksheet                      $pWorksheet
372         * @return      string                                          XML Output
373         * @throws      PHPExcel_Writer_Exception
374         */
375        public function writeHeaderFooterDrawingRelationships(PHPExcel_Worksheet $pWorksheet = null)
376        {
377                // Create XML writer
378                $objWriter = null;
379                if ($this->getParentWriter()->getUseDiskCaching()) {
380                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
381                } else {
382                        $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
383                }
384
385                // XML header
386                $objWriter->startDocument('1.0','UTF-8','yes');
387
388                // Relationships
389                $objWriter->startElement('Relationships');
390                $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships');
391
392                        // Loop through images and write relationships
393                        foreach ($pWorksheet->getHeaderFooter()->getImages() as $key => $value) {
394                                // Write relationship for image drawing
395                                $this->_writeRelationship(
396                                        $objWriter,
397                                        $key,
398                                        'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
399                                        '../media/' . $value->getIndexedFilename()
400                                );
401                        }
402
403                $objWriter->endElement();
404
405                // Return
406                return $objWriter->getData();
407        }
408
409        /**
410         * Write Override content type
411         *
412         * @param       PHPExcel_Shared_XMLWriter       $objWriter              XML Writer
413         * @param       int                                                     $pId                    Relationship ID. rId will be prepended!
414         * @param       string                                          $pType                  Relationship type
415         * @param       string                                          $pTarget                Relationship target
416         * @param       string                                          $pTargetMode    Relationship target mode
417         * @throws      PHPExcel_Writer_Exception
418         */
419        private function _writeRelationship(PHPExcel_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '')
420        {
421                if ($pType != '' && $pTarget != '') {
422                        // Write relationship
423                        $objWriter->startElement('Relationship');
424                        $objWriter->writeAttribute('Id',                'rId' . $pId);
425                        $objWriter->writeAttribute('Type',              $pType);
426                        $objWriter->writeAttribute('Target',    $pTarget);
427
428                        if ($pTargetMode != '') {
429                                $objWriter->writeAttribute('TargetMode',        $pTargetMode);
430                        }
431
432                        $objWriter->endElement();
433                } else {
434                        throw new PHPExcel_Writer_Exception("Invalid parameters passed.");
435                }
436        }
437}
Note: See TracBrowser for help on using the repository browser.