ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
StringTable.php
Go to the documentation of this file.
1 <?php
37 {
46  public function createStringTable($pSheet = null, $pExistingTable = null)
47  {
48  if (!is_null($pSheet)) {
49  // Create string lookup table
50  $aStringTable = array();
51  $cellCollection = null;
52  $aFlippedStringTable = null; // For faster lookup
53 
54  // Is an existing table given?
55  if (!is_null($pExistingTable) && is_array($pExistingTable)) {
56  $aStringTable = $pExistingTable;
57  }
58 
59  // Fill index array
60  $aFlippedStringTable = $this->flipStringTable($aStringTable);
61 
62  // Loop through cells
63  foreach ($pSheet->getCellCollection() as $cellID) {
64  $cell = $pSheet->getCell($cellID);
65  $cellValue = $cell->getValue();
66  if (!is_object($cellValue) &&
67  !is_null($cellValue) &&
68  $cellValue !== '' &&
69  !isset($aFlippedStringTable[$cellValue]) &&
70  ($cell->getDataType() == PHPExcel_Cell_DataType::TYPE_STRING || $cell->getDataType() == PHPExcel_Cell_DataType::TYPE_STRING2 || $cell->getDataType() == PHPExcel_Cell_DataType::TYPE_NULL)) {
71  $aStringTable[] = $cellValue;
72  $aFlippedStringTable[$cellValue] = 1;
73  } elseif ($cellValue instanceof PHPExcel_RichText &&
74  !is_null($cellValue) &&
75  !isset($aFlippedStringTable[$cellValue->getHashCode()])) {
76  $aStringTable[] = $cellValue;
77  $aFlippedStringTable[$cellValue->getHashCode()] = 1;
78  }
79  }
80 
81  // Return
82  return $aStringTable;
83  } else {
84  throw new Exception("Invalid PHPExcel_Worksheet object passed.");
85  }
86  }
87 
95  public function writeStringTable($pStringTable = null)
96  {
97  if (!is_null($pStringTable)) {
98  // Create XML writer
99  $objWriter = null;
100  if ($this->getParentWriter()->getUseDiskCaching()) {
101  $objWriter = new PHPExcel_Shared_XMLWriter(PHPExcel_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
102  } else {
104  }
105 
106  // XML header
107  $objWriter->startDocument('1.0','UTF-8','yes');
108 
109  // String table
110  $objWriter->startElement('sst');
111  $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
112  $objWriter->writeAttribute('uniqueCount', count($pStringTable));
113 
114  // Loop through string table
115  foreach ($pStringTable as $textElement) {
116  $objWriter->startElement('si');
117 
118  if (! $textElement instanceof PHPExcel_RichText) {
119  $textToWrite = PHPExcel_Shared_String::ControlCharacterPHP2OOXML( $textElement );
120  $objWriter->startElement('t');
121  if ($textToWrite !== trim($textToWrite)) {
122  $objWriter->writeAttribute('xml:space', 'preserve');
123  }
124  $objWriter->writeRawData($textToWrite);
125  $objWriter->endElement();
126  } else if ($textElement instanceof PHPExcel_RichText) {
127  $this->writeRichText($objWriter, $textElement);
128  }
129 
130  $objWriter->endElement();
131  }
132 
133  $objWriter->endElement();
134 
135  // Return
136  return $objWriter->getData();
137  } else {
138  throw new Exception("Invalid string table array passed.");
139  }
140  }
141 
149  public function writeRichText(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_RichText $pRichText = null)
150  {
151  // Loop through rich text elements
152  $elements = $pRichText->getRichTextElements();
153  foreach ($elements as $element) {
154  // r
155  $objWriter->startElement('r');
156 
157  // rPr
158  if ($element instanceof PHPExcel_RichText_Run) {
159  // rPr
160  $objWriter->startElement('rPr');
161 
162  // rFont
163  $objWriter->startElement('rFont');
164  $objWriter->writeAttribute('val', $element->getFont()->getName());
165  $objWriter->endElement();
166 
167  // Bold
168  $objWriter->startElement('b');
169  $objWriter->writeAttribute('val', ($element->getFont()->getBold() ? 'true' : 'false'));
170  $objWriter->endElement();
171 
172  // Italic
173  $objWriter->startElement('i');
174  $objWriter->writeAttribute('val', ($element->getFont()->getItalic() ? 'true' : 'false'));
175  $objWriter->endElement();
176 
177  // Superscript / subscript
178  if ($element->getFont()->getSuperScript() || $element->getFont()->getSubScript()) {
179  $objWriter->startElement('vertAlign');
180  if ($element->getFont()->getSuperScript()) {
181  $objWriter->writeAttribute('val', 'superscript');
182  } else if ($element->getFont()->getSubScript()) {
183  $objWriter->writeAttribute('val', 'subscript');
184  }
185  $objWriter->endElement();
186  }
187 
188  // Strikethrough
189  $objWriter->startElement('strike');
190  $objWriter->writeAttribute('val', ($element->getFont()->getStrikethrough() ? 'true' : 'false'));
191  $objWriter->endElement();
192 
193  // Color
194  $objWriter->startElement('color');
195  $objWriter->writeAttribute('rgb', $element->getFont()->getColor()->getARGB());
196  $objWriter->endElement();
197 
198  // Size
199  $objWriter->startElement('sz');
200  $objWriter->writeAttribute('val', $element->getFont()->getSize());
201  $objWriter->endElement();
202 
203  // Underline
204  $objWriter->startElement('u');
205  $objWriter->writeAttribute('val', $element->getFont()->getUnderline());
206  $objWriter->endElement();
207 
208  $objWriter->endElement();
209  }
210 
211  // t
212  $objWriter->startElement('t');
213  $objWriter->writeAttribute('xml:space', 'preserve');
214  $objWriter->writeRawData(PHPExcel_Shared_String::ControlCharacterPHP2OOXML( $element->getText() ));
215  $objWriter->endElement();
216 
217  $objWriter->endElement();
218  }
219  }
220 
227  public function flipStringTable($stringTable = array()) {
228  // Return value
229  $returnValue = array();
230 
231  // Loop through stringtable and add flipped items to $returnValue
232  foreach ($stringTable as $key => $value) {
233  if (! $value instanceof PHPExcel_RichText) {
234  $returnValue[$value] = $key;
235  } else if ($value instanceof PHPExcel_RichText) {
236  $returnValue[$value->getHashCode()] = $key;
237  }
238  }
239 
240  // Return
241  return $returnValue;
242  }
243 }