ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
StringTable.php
Go to the documentation of this file.
1 <?php
30 if (!defined('PHPEXCEL_ROOT')) {
34  define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../../');
35 }
36 
38 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007.php';
39 
41 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/Excel2007/WriterPart.php';
42 
44 require_once PHPEXCEL_ROOT . 'PHPExcel/Cell/DataType.php';
45 
47 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/XMLWriter.php';
48 
50 require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/String.php';
51 
52 
61 {
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 
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 {
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 
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 
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 }