ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
Style.php
Go to the documentation of this file.
1 <?php
37 {
45  public function writeStyles(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 {
53  }
54 
55  // XML header
56  $objWriter->startDocument('1.0','UTF-8','yes');
57 
58  // styleSheet
59  $objWriter->startElement('styleSheet');
60  $objWriter->writeAttribute('xml:space', 'preserve');
61  $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
62 
63  // numFmts
64  $objWriter->startElement('numFmts');
65  $objWriter->writeAttribute('count', $this->getParentWriter()->getNumFmtHashTable()->count());
66 
67  // numFmt
68  for ($i = 0; $i < $this->getParentWriter()->getNumFmtHashTable()->count(); ++$i) {
69  $this->_writeNumFmt($objWriter, $this->getParentWriter()->getNumFmtHashTable()->getByIndex($i), $i);
70  }
71 
72  $objWriter->endElement();
73 
74  // fonts
75  $objWriter->startElement('fonts');
76  $objWriter->writeAttribute('count', $this->getParentWriter()->getFontHashTable()->count());
77 
78  // font
79  for ($i = 0; $i < $this->getParentWriter()->getFontHashTable()->count(); ++$i) {
80  $this->_writeFont($objWriter, $this->getParentWriter()->getFontHashTable()->getByIndex($i));
81  }
82 
83  $objWriter->endElement();
84 
85  // fills
86  $objWriter->startElement('fills');
87  $objWriter->writeAttribute('count', $this->getParentWriter()->getFillHashTable()->count());
88 
89  // fill
90  for ($i = 0; $i < $this->getParentWriter()->getFillHashTable()->count(); ++$i) {
91  $this->_writeFill($objWriter, $this->getParentWriter()->getFillHashTable()->getByIndex($i));
92  }
93 
94  $objWriter->endElement();
95 
96  // borders
97  $objWriter->startElement('borders');
98  $objWriter->writeAttribute('count', $this->getParentWriter()->getBordersHashTable()->count());
99 
100  // border
101  for ($i = 0; $i < $this->getParentWriter()->getBordersHashTable()->count(); ++$i) {
102  $this->_writeBorder($objWriter, $this->getParentWriter()->getBordersHashTable()->getByIndex($i));
103  }
104 
105  $objWriter->endElement();
106 
107  // cellStyleXfs
108  $objWriter->startElement('cellStyleXfs');
109  $objWriter->writeAttribute('count', 1);
110 
111  // xf
112  $objWriter->startElement('xf');
113  $objWriter->writeAttribute('numFmtId', 0);
114  $objWriter->writeAttribute('fontId', 0);
115  $objWriter->writeAttribute('fillId', 0);
116  $objWriter->writeAttribute('borderId', 0);
117  $objWriter->endElement();
118 
119  $objWriter->endElement();
120 
121  // cellXfs
122  $objWriter->startElement('cellXfs');
123  $objWriter->writeAttribute('count', count($pPHPExcel->getCellXfCollection()));
124 
125  // xf
126  foreach ($pPHPExcel->getCellXfCollection() as $cellXf) {
127  $this->_writeCellStyleXf($objWriter, $cellXf, $pPHPExcel);
128  }
129 
130  $objWriter->endElement();
131 
132  // cellStyles
133  $objWriter->startElement('cellStyles');
134  $objWriter->writeAttribute('count', 1);
135 
136  // cellStyle
137  $objWriter->startElement('cellStyle');
138  $objWriter->writeAttribute('name', 'Normal');
139  $objWriter->writeAttribute('xfId', 0);
140  $objWriter->writeAttribute('builtinId', 0);
141  $objWriter->endElement();
142 
143  $objWriter->endElement();
144 
145  // dxfs
146  $objWriter->startElement('dxfs');
147  $objWriter->writeAttribute('count', $this->getParentWriter()->getStylesConditionalHashTable()->count());
148 
149  // dxf
150  for ($i = 0; $i < $this->getParentWriter()->getStylesConditionalHashTable()->count(); ++$i) {
151  $this->_writeCellStyleDxf($objWriter, $this->getParentWriter()->getStylesConditionalHashTable()->getByIndex($i)->getStyle());
152  }
153 
154  $objWriter->endElement();
155 
156  // tableStyles
157  $objWriter->startElement('tableStyles');
158  $objWriter->writeAttribute('defaultTableStyle', 'TableStyleMedium9');
159  $objWriter->writeAttribute('defaultPivotStyle', 'PivotTableStyle1');
160  $objWriter->endElement();
161 
162  $objWriter->endElement();
163 
164  // Return
165  return $objWriter->getData();
166  }
167 
175  private function _writeFill(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style_Fill $pFill = null)
176  {
177  // Check if this is a pattern type or gradient type
178  if ($pFill->getFillType() == PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR
179  || $pFill->getFillType() == PHPExcel_Style_Fill::FILL_GRADIENT_PATH) {
180  // Gradient fill
181  $this->_writeGradientFill($objWriter, $pFill);
182  } else {
183  // Pattern fill
184  $this->_writePatternFill($objWriter, $pFill);
185  }
186  }
187 
195  private function _writeGradientFill(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style_Fill $pFill = null)
196  {
197  // fill
198  $objWriter->startElement('fill');
199 
200  // gradientFill
201  $objWriter->startElement('gradientFill');
202  $objWriter->writeAttribute('type', $pFill->getFillType());
203  $objWriter->writeAttribute('degree', $pFill->getRotation());
204 
205  // stop
206  $objWriter->startElement('stop');
207  $objWriter->writeAttribute('position', '0');
208 
209  // color
210  $objWriter->startElement('color');
211  $objWriter->writeAttribute('rgb', $pFill->getStartColor()->getARGB());
212  $objWriter->endElement();
213 
214  $objWriter->endElement();
215 
216  // stop
217  $objWriter->startElement('stop');
218  $objWriter->writeAttribute('position', '1');
219 
220  // color
221  $objWriter->startElement('color');
222  $objWriter->writeAttribute('rgb', $pFill->getEndColor()->getARGB());
223  $objWriter->endElement();
224 
225  $objWriter->endElement();
226 
227  $objWriter->endElement();
228 
229  $objWriter->endElement();
230  }
231 
239  private function _writePatternFill(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style_Fill $pFill = null)
240  {
241  // fill
242  $objWriter->startElement('fill');
243 
244  // patternFill
245  $objWriter->startElement('patternFill');
246  $objWriter->writeAttribute('patternType', $pFill->getFillType());
247 
248  // fgColor
249  $objWriter->startElement('fgColor');
250  $objWriter->writeAttribute('rgb', $pFill->getStartColor()->getARGB());
251  $objWriter->endElement();
252 
253  // bgColor
254  $objWriter->startElement('bgColor');
255  $objWriter->writeAttribute('rgb', $pFill->getEndColor()->getARGB());
256  $objWriter->endElement();
257 
258  $objWriter->endElement();
259 
260  $objWriter->endElement();
261  }
262 
270  private function _writeFont(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style_Font $pFont = null)
271  {
272  // font
273  $objWriter->startElement('font');
274 
275  // Name
276  $objWriter->startElement('name');
277  $objWriter->writeAttribute('val', $pFont->getName());
278  $objWriter->endElement();
279 
280  // Size
281  $objWriter->startElement('sz');
282  $objWriter->writeAttribute('val', $pFont->getSize());
283  $objWriter->endElement();
284 
285  // Bold. We explicitly write this element also when false (like MS Office Excel 2007 does
286  // for conditional formatting). Otherwise it will apparently not be picked up in conditional
287  // formatting style dialog
288  $objWriter->startElement('b');
289  $objWriter->writeAttribute('val', $pFont->getBold() ? '1' : '0');
290  $objWriter->endElement();
291 
292  // Italic
293  $objWriter->startElement('i');
294  $objWriter->writeAttribute('val', $pFont->getItalic() ? '1' : '0');
295  $objWriter->endElement();
296 
297  // Superscript / subscript
298  if ($pFont->getSuperScript() || $pFont->getSubScript()) {
299  $objWriter->startElement('vertAlign');
300  if ($pFont->getSuperScript()) {
301  $objWriter->writeAttribute('val', 'superscript');
302  } else if ($pFont->getSubScript()) {
303  $objWriter->writeAttribute('val', 'subscript');
304  }
305  $objWriter->endElement();
306  }
307 
308  // Underline
309  $objWriter->startElement('u');
310  $objWriter->writeAttribute('val', $pFont->getUnderline());
311  $objWriter->endElement();
312 
313  // Strikethrough
314  $objWriter->startElement('strike');
315  $objWriter->writeAttribute('val', $pFont->getStrikethrough() ? '1' : '0');
316  $objWriter->endElement();
317 
318  // Foreground color
319  $objWriter->startElement('color');
320  $objWriter->writeAttribute('rgb', $pFont->getColor()->getARGB());
321  $objWriter->endElement();
322 
323  $objWriter->endElement();
324  }
325 
333  private function _writeBorder(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style_Borders $pBorders = null)
334  {
335  // Write border
336  $objWriter->startElement('border');
337  // Diagonal?
338  switch ($pBorders->getDiagonalDirection()) {
340  $objWriter->writeAttribute('diagonalUp', 'true');
341  $objWriter->writeAttribute('diagonalDown', 'false');
342  break;
344  $objWriter->writeAttribute('diagonalUp', 'false');
345  $objWriter->writeAttribute('diagonalDown', 'true');
346  break;
348  $objWriter->writeAttribute('diagonalUp', 'true');
349  $objWriter->writeAttribute('diagonalDown', 'true');
350  break;
351  }
352 
353  // BorderPr
354  $this->_writeBorderPr($objWriter, 'left', $pBorders->getLeft());
355  $this->_writeBorderPr($objWriter, 'right', $pBorders->getRight());
356  $this->_writeBorderPr($objWriter, 'top', $pBorders->getTop());
357  $this->_writeBorderPr($objWriter, 'bottom', $pBorders->getBottom());
358  $this->_writeBorderPr($objWriter, 'diagonal', $pBorders->getDiagonal());
359  $objWriter->endElement();
360  }
361 
370  private function _writeCellStyleXf(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style $pStyle = null, PHPExcel $pPHPExcel = null)
371  {
372  // xf
373  $objWriter->startElement('xf');
374  $objWriter->writeAttribute('xfId', 0);
375  $objWriter->writeAttribute('fontId', (int)$this->getParentWriter()->getFontHashTable()->getIndexForHashCode($pStyle->getFont()->getHashCode()));
376 
377  if ($pStyle->getNumberFormat()->getBuiltInFormatCode() === false) {
378  $objWriter->writeAttribute('numFmtId', (int)($this->getParentWriter()->getNumFmtHashTable()->getIndexForHashCode($pStyle->getNumberFormat()->getHashCode()) + 164) );
379  } else {
380  $objWriter->writeAttribute('numFmtId', (int)$pStyle->getNumberFormat()->getBuiltInFormatCode());
381  }
382 
383  $objWriter->writeAttribute('fillId', (int)$this->getParentWriter()->getFillHashTable()->getIndexForHashCode($pStyle->getFill()->getHashCode()));
384  $objWriter->writeAttribute('borderId', (int)$this->getParentWriter()->getBordersHashTable()->getIndexForHashCode($pStyle->getBorders()->getHashCode()));
385 
386  // Apply styles?
387  $objWriter->writeAttribute('applyFont', ($pPHPExcel->getDefaultStyle()->getFont()->getHashCode() != $pStyle->getFont()->getHashCode()) ? '1' : '0');
388  $objWriter->writeAttribute('applyNumberFormat', ($pPHPExcel->getDefaultStyle()->getNumberFormat()->getHashCode() != $pStyle->getNumberFormat()->getHashCode()) ? '1' : '0');
389  $objWriter->writeAttribute('applyFill', ($pPHPExcel->getDefaultStyle()->getFill()->getHashCode() != $pStyle->getFill()->getHashCode()) ? '1' : '0');
390  $objWriter->writeAttribute('applyBorder', ($pPHPExcel->getDefaultStyle()->getBorders()->getHashCode() != $pStyle->getBorders()->getHashCode()) ? '1' : '0');
391  $objWriter->writeAttribute('applyAlignment', ($pPHPExcel->getDefaultStyle()->getAlignment()->getHashCode() != $pStyle->getAlignment()->getHashCode()) ? '1' : '0');
392  if ($pStyle->getProtection()->getLocked() != PHPExcel_Style_Protection::PROTECTION_INHERIT || $pStyle->getProtection()->getHidden() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
393  $objWriter->writeAttribute('applyProtection', 'true');
394  }
395 
396  // alignment
397  $objWriter->startElement('alignment');
398  $objWriter->writeAttribute('horizontal', $pStyle->getAlignment()->getHorizontal());
399  $objWriter->writeAttribute('vertical', $pStyle->getAlignment()->getVertical());
400 
401  $textRotation = 0;
402  if ($pStyle->getAlignment()->getTextRotation() >= 0) {
403  $textRotation = $pStyle->getAlignment()->getTextRotation();
404  } else if ($pStyle->getAlignment()->getTextRotation() < 0) {
405  $textRotation = 90 - $pStyle->getAlignment()->getTextRotation();
406  }
407 
408  $objWriter->writeAttribute('textRotation', $textRotation);
409  $objWriter->writeAttribute('wrapText', ($pStyle->getAlignment()->getWrapText() ? 'true' : 'false'));
410  $objWriter->writeAttribute('shrinkToFit', ($pStyle->getAlignment()->getShrinkToFit() ? 'true' : 'false'));
411 
412  if ($pStyle->getAlignment()->getIndent() > 0) {
413  $objWriter->writeAttribute('indent', $pStyle->getAlignment()->getIndent());
414  }
415  $objWriter->endElement();
416 
417  // protection
418  if ($pStyle->getProtection()->getLocked() != PHPExcel_Style_Protection::PROTECTION_INHERIT || $pStyle->getProtection()->getHidden() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
419  $objWriter->startElement('protection');
420  if ($pStyle->getProtection()->getLocked() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
421  $objWriter->writeAttribute('locked', ($pStyle->getProtection()->getLocked() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
422  }
423  if ($pStyle->getProtection()->getHidden() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
424  $objWriter->writeAttribute('hidden', ($pStyle->getProtection()->getHidden() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
425  }
426  $objWriter->endElement();
427  }
428 
429  $objWriter->endElement();
430  }
431 
439  private function _writeCellStyleDxf(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style $pStyle = null)
440  {
441  // dxf
442  $objWriter->startElement('dxf');
443 
444  // font
445  $this->_writeFont($objWriter, $pStyle->getFont());
446 
447  // numFmt
448  $this->_writeNumFmt($objWriter, $pStyle->getNumberFormat());
449 
450  // fill
451  $this->_writeFill($objWriter, $pStyle->getFill());
452 
453  // alignment
454  $objWriter->startElement('alignment');
455  $objWriter->writeAttribute('horizontal', $pStyle->getAlignment()->getHorizontal());
456  $objWriter->writeAttribute('vertical', $pStyle->getAlignment()->getVertical());
457 
458  $textRotation = 0;
459  if ($pStyle->getAlignment()->getTextRotation() >= 0) {
460  $textRotation = $pStyle->getAlignment()->getTextRotation();
461  } else if ($pStyle->getAlignment()->getTextRotation() < 0) {
462  $textRotation = 90 - $pStyle->getAlignment()->getTextRotation();
463  }
464 
465  $objWriter->writeAttribute('textRotation', $textRotation);
466  $objWriter->endElement();
467 
468  // border
469  $this->_writeBorder($objWriter, $pStyle->getBorders());
470 
471  // protection
472  if ($pStyle->getProtection()->getLocked() != PHPExcel_Style_Protection::PROTECTION_INHERIT || $pStyle->getProtection()->getHidden() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
473  $objWriter->startElement('protection');
474  if ($pStyle->getProtection()->getLocked() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
475  $objWriter->writeAttribute('locked', ($pStyle->getProtection()->getLocked() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
476  }
477  if ($pStyle->getProtection()->getHidden() != PHPExcel_Style_Protection::PROTECTION_INHERIT) {
478  $objWriter->writeAttribute('hidden', ($pStyle->getProtection()->getHidden() == PHPExcel_Style_Protection::PROTECTION_PROTECTED ? 'true' : 'false'));
479  }
480  $objWriter->endElement();
481  }
482 
483  $objWriter->endElement();
484  }
485 
494  private function _writeBorderPr(PHPExcel_Shared_XMLWriter $objWriter = null, $pName = 'left', PHPExcel_Style_Border $pBorder = null)
495  {
496  // Write BorderPr
497  if ($pBorder->getBorderStyle() != PHPExcel_Style_Border::BORDER_NONE) {
498  $objWriter->startElement($pName);
499  $objWriter->writeAttribute('style', $pBorder->getBorderStyle());
500 
501  // color
502  $objWriter->startElement('color');
503  $objWriter->writeAttribute('rgb', $pBorder->getColor()->getARGB());
504  $objWriter->endElement();
505 
506  $objWriter->endElement();
507  }
508  }
509 
518  private function _writeNumFmt(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Style_NumberFormat $pNumberFormat = null, $pId = 0)
519  {
520  // Translate formatcode
521  $formatCode = $pNumberFormat->getFormatCode();
522 
523  // numFmt
524  $objWriter->startElement('numFmt');
525  $objWriter->writeAttribute('numFmtId', ($pId + 164));
526  $objWriter->writeAttribute('formatCode', $formatCode);
527  $objWriter->endElement();
528  }
529 
537  public function allStyles(PHPExcel $pPHPExcel = null)
538  {
539  $aStyles = $pPHPExcel->getCellXfCollection();
540 
541  return $aStyles;
542  }
543 
551  public function allConditionalStyles(PHPExcel $pPHPExcel = null)
552  {
553  // Get an array of all styles
554  $aStyles = array();
555 
556  $sheetCount = $pPHPExcel->getSheetCount();
557  for ($i = 0; $i < $sheetCount; ++$i) {
558  foreach ($pPHPExcel->getSheet($i)->getConditionalStylesCollection() as $conditionalStyles) {
559  foreach ($conditionalStyles as $conditionalStyle) {
560  $aStyles[] = $conditionalStyle;
561  }
562  }
563  }
564 
565  return $aStyles;
566  }
567 
575  public function allFills(PHPExcel $pPHPExcel = null)
576  {
577  // Get an array of unique fills
578  $aFills = array();
579 
580  // Two first fills are predefined
581  $fill0 = new PHPExcel_Style_Fill();
582  $fill0->setFillType(PHPExcel_Style_Fill::FILL_NONE);
583  $aFills[] = $fill0;
584 
585  $fill1 = new PHPExcel_Style_Fill();
586  $fill1->setFillType(PHPExcel_Style_Fill::FILL_PATTERN_GRAY125);
587  $aFills[] = $fill1;
588 
589  // The remaining fills
590  $aStyles = $this->allStyles($pPHPExcel);
591  foreach ($aStyles as $style) {
592  if (!array_key_exists($style->getFill()->getHashCode(), $aFills)) {
593  $aFills[ $style->getFill()->getHashCode() ] = $style->getFill();
594  }
595  }
596 
597  return $aFills;
598  }
599 
607  public function allFonts(PHPExcel $pPHPExcel = null)
608  {
609  // Get an array of unique fonts
610  $aFonts = array();
611  $aStyles = $this->allStyles($pPHPExcel);
612 
613  foreach ($aStyles as $style) {
614  if (!array_key_exists($style->getFont()->getHashCode(), $aFonts)) {
615  $aFonts[ $style->getFont()->getHashCode() ] = $style->getFont();
616  }
617  }
618 
619  return $aFonts;
620  }
621 
629  public function allBorders(PHPExcel $pPHPExcel = null)
630  {
631  // Get an array of unique borders
632  $aBorders = array();
633  $aStyles = $this->allStyles($pPHPExcel);
634 
635  foreach ($aStyles as $style) {
636  if (!array_key_exists($style->getBorders()->getHashCode(), $aBorders)) {
637  $aBorders[ $style->getBorders()->getHashCode() ] = $style->getBorders();
638  }
639  }
640 
641  return $aBorders;
642  }
643 
651  public function allNumberFormats(PHPExcel $pPHPExcel = null)
652  {
653  // Get an array of unique number formats
654  $aNumFmts = array();
655  $aStyles = $this->allStyles($pPHPExcel);
656 
657  foreach ($aStyles as $style) {
658  if ($style->getNumberFormat()->getBuiltInFormatCode() === false && !array_key_exists($style->getNumberFormat()->getHashCode(), $aNumFmts)) {
659  $aNumFmts[ $style->getNumberFormat()->getHashCode() ] = $style->getNumberFormat();
660  }
661  }
662 
663  return $aNumFmts;
664  }
665 }