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

« back to all changes in this revision

Viewing changes to libraries/PHPExcel/PHPExcel/Writer/Excel2007/StringTable.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_Writer_Excel2007 */
 
38
require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007.php';
 
39
 
 
40
/** PHPExcel_Writer_Excel2007_WriterPart */
 
41
require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007/WriterPart.php';
 
42
 
 
43
/** PHPExcel_Cell_DataType */
 
44
require_once PHPEXCEL_ROOT . 'PHPExcel/Cell/DataType.php';
 
45
 
 
46
/** PHPExcel_Shared_XMLWriter */
 
47
require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/XMLWriter.php';
 
48
 
 
49
/** PHPExcel_Shared_String */
 
50
require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/String.php';
 
51
 
 
52
 
 
53
/**
 
54
 * PHPExcel_Writer_Excel2007_StringTable
 
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_StringTable extends PHPExcel_Writer_Excel2007_WriterPart
 
61
{
 
62
        /**
 
63
         * Create worksheet stringtable
 
64
         *
 
65
         * @param       PHPExcel_Worksheet      $pSheet                         Worksheet
 
66
         * @param       string[]                                $pExistingTable         Existing table to eventually merge with
 
67
         * @return      string[]                                String table for worksheet
 
68
         * @throws      Exception
 
69
         */
 
70
        public function createStringTable($pSheet = null, $pExistingTable = null)
 
71
        {
 
72
                if (!is_null($pSheet)) {
 
73
                        // Create string lookup table
 
74
                        $aStringTable = array();
 
75
                        $cellCollection = null;
 
76
                        $aFlippedStringTable = null;    // For faster lookup
 
77
                
 
78
                        // Is an existing table given?
 
79
                        if (!is_null($pExistingTable) && is_array($pExistingTable)) {
 
80
                                $aStringTable = $pExistingTable;
 
81
                        }
 
82
                        
 
83
                        // Fill index array
 
84
                        $aFlippedStringTable = $this->flipStringTable($aStringTable);
 
85
                        
 
86
                // Loop trough cells
 
87
                $cellCollection = $pSheet->getCellCollection();
 
88
                foreach ($cellCollection as $cell) {
 
89
                        if (!is_object($cell->getValue()) &&
 
90
                                !isset($aFlippedStringTable[$cell->getValue()]) &&
 
91
                                !is_null($cell->getValue()) &&
 
92
                                $cell->getValue() !== '' &&
 
93
                                ($cell->getDataType() == PHPExcel_Cell_DataType::TYPE_STRING || $cell->getDataType() == PHPExcel_Cell_DataType::TYPE_NULL)
 
94
                        ) {
 
95
                                        $aStringTable[] = $cell->getValue();
 
96
                                                $aFlippedStringTable[$cell->getValue()] = 1;
 
97
                                                
 
98
                        } else if ($cell->getValue() instanceof PHPExcel_RichText &&
 
99
                                           !isset($aFlippedStringTable[$cell->getValue()->getHashCode()]) &&
 
100
                                           !is_null($cell->getValue())
 
101
                        ) {
 
102
                                $aStringTable[] = $cell->getValue();
 
103
                                        $aFlippedStringTable[$cell->getValue()->getHashCode()] = 1;
 
104
                        }
 
105
                }
 
106
 
 
107
                // Return
 
108
                return $aStringTable;
 
109
                } else {
 
110
                        throw new Exception("Invalid PHPExcel_Worksheet object passed.");
 
111
                }
 
112
        }
 
113
        
 
114
        /**
 
115
         * Write string table to XML format
 
116
         *
 
117
         * @param       string[]        $pStringTable
 
118
         * @return      string          XML Output
 
119
         * @throws      Exception
 
120
         */
 
121
        public function writeStringTable($pStringTable = null)
 
122
        {
 
123
                if (!is_null($pStringTable)) {                                  
 
124
                        // Create XML writer
 
125
                        $objWriter = null;
 
126
                        if ($this->getParentWriter()->getUseDiskCaching()) {
 
127
                                $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
 
128
                        } else {
 
129
                                $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_MEMORY);
 
130
                        }
 
131
                        
 
132
                        // XML header
 
133
                        $objWriter->startDocument('1.0','UTF-8','yes');
 
134
                        
 
135
                        // String table
 
136
                        $objWriter->startElement('sst');
 
137
                        $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
 
138
                        $objWriter->writeAttribute('uniqueCount', count($pStringTable));
 
139
                        
 
140
                                // Loop trough string table
 
141
                                foreach ($pStringTable as $textElement) {
 
142
                                        $objWriter->startElement('si');
 
143
                                        
 
144
                                                if (! $textElement instanceof PHPExcel_RichText) {
 
145
                                                        $textToWrite = PHPExcel_Shared_String::ControlCharacterPHP2OOXML( $textElement );
 
146
                                                        $objWriter->startElement('t');
 
147
                                                        if ($textToWrite !== trim($textToWrite)) {
 
148
                                                                $objWriter->writeAttribute('xml:space', 'preserve');
 
149
                                                        }
 
150
                                                        $objWriter->writeRaw($textToWrite);
 
151
                                                        $objWriter->endElement();
 
152
                                                } else if ($textElement instanceof PHPExcel_RichText) {
 
153
                                                        $this->writeRichText($objWriter, $textElement);
 
154
                                                }
 
155
                                                
 
156
                    $objWriter->endElement();
 
157
                                }
 
158
                                
 
159
                        $objWriter->endElement();
 
160
 
 
161
                        // Return
 
162
                        return $objWriter->getData();
 
163
                } else {
 
164
                        throw new Exception("Invalid string table array passed.");
 
165
                }
 
166
        }
 
167
 
 
168
        /**
 
169
         * Write Rich Text
 
170
         *
 
171
         * @param       PHPExcel_Shared_XMLWriter               $objWriter              XML Writer
 
172
         * @param       PHPExcel_RichText                               $pRichText              Rich text
 
173
         * @throws      Exception
 
174
         */
 
175
        public function writeRichText(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_RichText $pRichText = null)
 
176
        {
 
177
                // Loop trough rich text elements
 
178
                $elements = $pRichText->getRichTextElements();
 
179
                foreach ($elements as $element) {
 
180
                        // r
 
181
                        $objWriter->startElement('r');
 
182
                                
 
183
                                // rPr
 
184
                                if ($element instanceof PHPExcel_RichText_Run) {
 
185
                                        // rPr
 
186
                                        $objWriter->startElement('rPr');
 
187
 
 
188
                                                // rFont
 
189
                                                $objWriter->startElement('rFont');
 
190
                                                $objWriter->writeAttribute('val', $element->getFont()->getName());
 
191
                                                $objWriter->endElement();
 
192
                                                        
 
193
                                                // Bold
 
194
                                                $objWriter->startElement('b');
 
195
                                                $objWriter->writeAttribute('val', ($element->getFont()->getBold() ? 'true' : 'false'));
 
196
                                                $objWriter->endElement();
 
197
                                                                        
 
198
                                                // Italic
 
199
                                                $objWriter->startElement('i');
 
200
                                                $objWriter->writeAttribute('val', ($element->getFont()->getItalic() ? 'true' : 'false'));
 
201
                                                $objWriter->endElement();
 
202
                                                
 
203
                                                // Superscript / subscript
 
204
                                                if ($element->getFont()->getSuperScript() || $element->getFont()->getSubScript()) {
 
205
                                                        $objWriter->startElement('vertAlign');
 
206
                                                        if ($element->getFont()->getSuperScript()) {
 
207
                                                                $objWriter->writeAttribute('val', 'superscript');
 
208
                                                        } else if ($element->getFont()->getSubScript()) {
 
209
                                                                $objWriter->writeAttribute('val', 'subscript');
 
210
                                                        }
 
211
                                                        $objWriter->endElement();
 
212
                                                }
 
213
                                                        
 
214
                                                // Strikethrough
 
215
                                                $objWriter->startElement('strike');
 
216
                                                $objWriter->writeAttribute('val', ($element->getFont()->getStrikethrough() ? 'true' : 'false'));
 
217
                                                $objWriter->endElement();                       
 
218
                                                        
 
219
                                                // Color
 
220
                                                $objWriter->startElement('color');
 
221
                                                $objWriter->writeAttribute('rgb', $element->getFont()->getColor()->getARGB());
 
222
                                                $objWriter->endElement();       
 
223
                                                        
 
224
                                                // Size
 
225
                                                $objWriter->startElement('sz');
 
226
                                                $objWriter->writeAttribute('val', $element->getFont()->getSize());
 
227
                                                $objWriter->endElement();
 
228
                                                        
 
229
                                                // Underline
 
230
                                                $objWriter->startElement('u');
 
231
                                                $objWriter->writeAttribute('val', $element->getFont()->getUnderline());
 
232
                                                $objWriter->endElement();
 
233
                                
 
234
                                        $objWriter->endElement();
 
235
                                }
 
236
                                        
 
237
                                // t
 
238
                                $objWriter->startElement('t');
 
239
                                $objWriter->writeAttribute('xml:space', 'preserve');
 
240
                                $objWriter->writeRaw(PHPExcel_Shared_String::ControlCharacterPHP2OOXML( htmlspecialchars($element->getText()) ));
 
241
                                $objWriter->endElement();
 
242
                                        
 
243
                        $objWriter->endElement();
 
244
                }       
 
245
        }
 
246
        
 
247
        /**
 
248
         * Flip string table (for index searching)
 
249
         *
 
250
         * @param       array   $stringTable    Stringtable
 
251
         * @return      array
 
252
         */
 
253
        public function flipStringTable($stringTable = array()) {
 
254
                // Return value
 
255
                $returnValue = array();
 
256
                
 
257
                // Loop trough stringtable and add flipped items to $returnValue
 
258
                foreach ($stringTable as $key => $value) {
 
259
                        if (! $value instanceof PHPExcel_RichText) {
 
260
                                $returnValue[$value] = $key;
 
261
                        } else if ($value instanceof PHPExcel_RichText) {
 
262
                                $returnValue[$value->getHashCode()] = $key;
 
263
                        }
 
264
                }
 
265
 
 
266
                // Return
 
267
                return $returnValue;
 
268
        }
 
269
}