ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Style.php
Go to the documentation of this file.
1 <?php
37 {
43  protected $_font;
44 
50  protected $_fill;
51 
57  protected $_borders;
58 
64  protected $_alignment;
65 
71  protected $_numberFormat;
72 
79 
85  protected $_protection;
86 
92  protected $_index;
93 
99  protected $_quotePrefix = false;
100 
111  public function __construct($isSupervisor = false, $isConditional = false)
112  {
113  // Supervisor?
114  $this->_isSupervisor = $isSupervisor;
115 
116  // Initialise values
117  $this->_conditionalStyles = array();
118  $this->_font = new PHPExcel_Style_Font($isSupervisor, $isConditional);
119  $this->_fill = new PHPExcel_Style_Fill($isSupervisor, $isConditional);
120  $this->_borders = new PHPExcel_Style_Borders($isSupervisor, $isConditional);
121  $this->_alignment = new PHPExcel_Style_Alignment($isSupervisor, $isConditional);
122  $this->_numberFormat = new PHPExcel_Style_NumberFormat($isSupervisor, $isConditional);
123  $this->_protection = new PHPExcel_Style_Protection($isSupervisor, $isConditional);
124 
125  // bind parent if we are a supervisor
126  if ($isSupervisor) {
127  $this->_font->bindParent($this);
128  $this->_fill->bindParent($this);
129  $this->_borders->bindParent($this);
130  $this->_alignment->bindParent($this);
131  $this->_numberFormat->bindParent($this);
132  $this->_protection->bindParent($this);
133  }
134  }
135 
142  public function getSharedComponent()
143  {
144  $activeSheet = $this->getActiveSheet();
145  $selectedCell = $this->getActiveCell(); // e.g. 'A1'
146 
147  if ($activeSheet->cellExists($selectedCell)) {
148  $xfIndex = $activeSheet->getCell($selectedCell)->getXfIndex();
149  } else {
150  $xfIndex = 0;
151  }
152 
153  return $this->_parent->getCellXfByIndex($xfIndex);
154  }
155 
161  public function getParent()
162  {
163  return $this->_parent;
164  }
165 
172  public function getStyleArray($array)
173  {
174  return array('quotePrefix' => $array);
175  }
176 
217  public function applyFromArray($pStyles = null, $pAdvanced = true)
218  {
219  if (is_array($pStyles)) {
220  if ($this->_isSupervisor) {
221 
222  $pRange = $this->getSelectedCells();
223 
224  // Uppercase coordinate
225  $pRange = strtoupper($pRange);
226 
227  // Is it a cell range or a single cell?
228  if (strpos($pRange, ':') === false) {
229  $rangeA = $pRange;
230  $rangeB = $pRange;
231  } else {
232  list($rangeA, $rangeB) = explode(':', $pRange);
233  }
234 
235  // Calculate range outer borders
236  $rangeStart = PHPExcel_Cell::coordinateFromString($rangeA);
237  $rangeEnd = PHPExcel_Cell::coordinateFromString($rangeB);
238 
239  // Translate column into index
240  $rangeStart[0] = PHPExcel_Cell::columnIndexFromString($rangeStart[0]) - 1;
241  $rangeEnd[0] = PHPExcel_Cell::columnIndexFromString($rangeEnd[0]) - 1;
242 
243  // Make sure we can loop upwards on rows and columns
244  if ($rangeStart[0] > $rangeEnd[0] && $rangeStart[1] > $rangeEnd[1]) {
245  $tmp = $rangeStart;
246  $rangeStart = $rangeEnd;
247  $rangeEnd = $tmp;
248  }
249 
250  // ADVANCED MODE:
251 
252  if ($pAdvanced && isset($pStyles['borders'])) {
253 
254  // 'allborders' is a shorthand property for 'outline' and 'inside' and
255  // it applies to components that have not been set explicitly
256  if (isset($pStyles['borders']['allborders'])) {
257  foreach (array('outline', 'inside') as $component) {
258  if (!isset($pStyles['borders'][$component])) {
259  $pStyles['borders'][$component] = $pStyles['borders']['allborders'];
260  }
261  }
262  unset($pStyles['borders']['allborders']); // not needed any more
263  }
264 
265  // 'outline' is a shorthand property for 'top', 'right', 'bottom', 'left'
266  // it applies to components that have not been set explicitly
267  if (isset($pStyles['borders']['outline'])) {
268  foreach (array('top', 'right', 'bottom', 'left') as $component) {
269  if (!isset($pStyles['borders'][$component])) {
270  $pStyles['borders'][$component] = $pStyles['borders']['outline'];
271  }
272  }
273  unset($pStyles['borders']['outline']); // not needed any more
274  }
275 
276  // 'inside' is a shorthand property for 'vertical' and 'horizontal'
277  // it applies to components that have not been set explicitly
278  if (isset($pStyles['borders']['inside'])) {
279  foreach (array('vertical', 'horizontal') as $component) {
280  if (!isset($pStyles['borders'][$component])) {
281  $pStyles['borders'][$component] = $pStyles['borders']['inside'];
282  }
283  }
284  unset($pStyles['borders']['inside']); // not needed any more
285  }
286 
287  // width and height characteristics of selection, 1, 2, or 3 (for 3 or more)
288  $xMax = min($rangeEnd[0] - $rangeStart[0] + 1, 3);
289  $yMax = min($rangeEnd[1] - $rangeStart[1] + 1, 3);
290 
291  // loop through up to 3 x 3 = 9 regions
292  for ($x = 1; $x <= $xMax; ++$x) {
293  // start column index for region
294  $colStart = ($x == 3) ?
296  : PHPExcel_Cell::stringFromColumnIndex($rangeStart[0] + $x - 1);
297 
298  // end column index for region
299  $colEnd = ($x == 1) ?
301  : PHPExcel_Cell::stringFromColumnIndex($rangeEnd[0] - $xMax + $x);
302 
303  for ($y = 1; $y <= $yMax; ++$y) {
304 
305  // which edges are touching the region
306  $edges = array();
307 
308  // are we at left edge
309  if ($x == 1) {
310  $edges[] = 'left';
311  }
312 
313  // are we at right edge
314  if ($x == $xMax) {
315  $edges[] = 'right';
316  }
317 
318  // are we at top edge?
319  if ($y == 1) {
320  $edges[] = 'top';
321  }
322 
323  // are we at bottom edge?
324  if ($y == $yMax) {
325  $edges[] = 'bottom';
326  }
327 
328  // start row index for region
329  $rowStart = ($y == 3) ?
330  $rangeEnd[1] : $rangeStart[1] + $y - 1;
331 
332  // end row index for region
333  $rowEnd = ($y == 1) ?
334  $rangeStart[1] : $rangeEnd[1] - $yMax + $y;
335 
336  // build range for region
337  $range = $colStart . $rowStart . ':' . $colEnd . $rowEnd;
338 
339  // retrieve relevant style array for region
340  $regionStyles = $pStyles;
341  unset($regionStyles['borders']['inside']);
342 
343  // what are the inner edges of the region when looking at the selection
344  $innerEdges = array_diff( array('top', 'right', 'bottom', 'left'), $edges );
345 
346  // inner edges that are not touching the region should take the 'inside' border properties if they have been set
347  foreach ($innerEdges as $innerEdge) {
348  switch ($innerEdge) {
349  case 'top':
350  case 'bottom':
351  // should pick up 'horizontal' border property if set
352  if (isset($pStyles['borders']['horizontal'])) {
353  $regionStyles['borders'][$innerEdge] = $pStyles['borders']['horizontal'];
354  } else {
355  unset($regionStyles['borders'][$innerEdge]);
356  }
357  break;
358  case 'left':
359  case 'right':
360  // should pick up 'vertical' border property if set
361  if (isset($pStyles['borders']['vertical'])) {
362  $regionStyles['borders'][$innerEdge] = $pStyles['borders']['vertical'];
363  } else {
364  unset($regionStyles['borders'][$innerEdge]);
365  }
366  break;
367  }
368  }
369 
370  // apply region style to region by calling applyFromArray() in simple mode
371  $this->getActiveSheet()->getStyle($range)->applyFromArray($regionStyles, false);
372  }
373  }
374  return $this;
375  }
376 
377  // SIMPLE MODE:
378 
379  // Selection type, inspect
380  if (preg_match('/^[A-Z]+1:[A-Z]+1048576$/', $pRange)) {
381  $selectionType = 'COLUMN';
382  } else if (preg_match('/^A[0-9]+:XFD[0-9]+$/', $pRange)) {
383  $selectionType = 'ROW';
384  } else {
385  $selectionType = 'CELL';
386  }
387 
388  // First loop through columns, rows, or cells to find out which styles are affected by this operation
389  switch ($selectionType) {
390  case 'COLUMN':
391  $oldXfIndexes = array();
392  for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
393  $oldXfIndexes[$this->getActiveSheet()->getColumnDimensionByColumn($col)->getXfIndex()] = true;
394  }
395  break;
396 
397  case 'ROW':
398  $oldXfIndexes = array();
399  for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
400  if ($this->getActiveSheet()->getRowDimension($row)->getXfIndex() == null) {
401  $oldXfIndexes[0] = true; // row without explicit style should be formatted based on default style
402  } else {
403  $oldXfIndexes[$this->getActiveSheet()->getRowDimension($row)->getXfIndex()] = true;
404  }
405  }
406  break;
407 
408  case 'CELL':
409  $oldXfIndexes = array();
410  for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
411  for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
412  $oldXfIndexes[$this->getActiveSheet()->getCellByColumnAndRow($col, $row)->getXfIndex()] = true;
413  }
414  }
415  break;
416  }
417 
418  // clone each of the affected styles, apply the style array, and add the new styles to the workbook
419  $workbook = $this->getActiveSheet()->getParent();
420  foreach ($oldXfIndexes as $oldXfIndex => $dummy) {
421  $style = $workbook->getCellXfByIndex($oldXfIndex);
422  $newStyle = clone $style;
423  $newStyle->applyFromArray($pStyles);
424 
425  if ($existingStyle = $workbook->getCellXfByHashCode($newStyle->getHashCode())) {
426  // there is already such cell Xf in our collection
427  $newXfIndexes[$oldXfIndex] = $existingStyle->getIndex();
428  } else {
429  // we don't have such a cell Xf, need to add
430  $workbook->addCellXf($newStyle);
431  $newXfIndexes[$oldXfIndex] = $newStyle->getIndex();
432  }
433  }
434 
435  // Loop through columns, rows, or cells again and update the XF index
436  switch ($selectionType) {
437  case 'COLUMN':
438  for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
439  $columnDimension = $this->getActiveSheet()->getColumnDimensionByColumn($col);
440  $oldXfIndex = $columnDimension->getXfIndex();
441  $columnDimension->setXfIndex($newXfIndexes[$oldXfIndex]);
442  }
443  break;
444 
445  case 'ROW':
446  for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
447  $rowDimension = $this->getActiveSheet()->getRowDimension($row);
448  $oldXfIndex = $rowDimension->getXfIndex() === null ?
449  0 : $rowDimension->getXfIndex(); // row without explicit style should be formatted based on default style
450  $rowDimension->setXfIndex($newXfIndexes[$oldXfIndex]);
451  }
452  break;
453 
454  case 'CELL':
455  for ($col = $rangeStart[0]; $col <= $rangeEnd[0]; ++$col) {
456  for ($row = $rangeStart[1]; $row <= $rangeEnd[1]; ++$row) {
457  $cell = $this->getActiveSheet()->getCellByColumnAndRow($col, $row);
458  $oldXfIndex = $cell->getXfIndex();
459  $cell->setXfIndex($newXfIndexes[$oldXfIndex]);
460  }
461  }
462  break;
463  }
464 
465  } else {
466  // not a supervisor, just apply the style array directly on style object
467  if (array_key_exists('fill', $pStyles)) {
468  $this->getFill()->applyFromArray($pStyles['fill']);
469  }
470  if (array_key_exists('font', $pStyles)) {
471  $this->getFont()->applyFromArray($pStyles['font']);
472  }
473  if (array_key_exists('borders', $pStyles)) {
474  $this->getBorders()->applyFromArray($pStyles['borders']);
475  }
476  if (array_key_exists('alignment', $pStyles)) {
477  $this->getAlignment()->applyFromArray($pStyles['alignment']);
478  }
479  if (array_key_exists('numberformat', $pStyles)) {
480  $this->getNumberFormat()->applyFromArray($pStyles['numberformat']);
481  }
482  if (array_key_exists('protection', $pStyles)) {
483  $this->getProtection()->applyFromArray($pStyles['protection']);
484  }
485  if (array_key_exists('quotePrefix', $pStyles)) {
486  $this->_quotePrefix = $pStyles['quotePrefix'];
487  }
488  }
489  } else {
490  throw new PHPExcel_Exception("Invalid style array passed.");
491  }
492  return $this;
493  }
494 
500  public function getFill()
501  {
502  return $this->_fill;
503  }
504 
510  public function getFont()
511  {
512  return $this->_font;
513  }
514 
521  public function setFont(PHPExcel_Style_Font $font)
522  {
523  $this->_font = $font;
524  return $this;
525  }
526 
532  public function getBorders()
533  {
534  return $this->_borders;
535  }
536 
542  public function getAlignment()
543  {
544  return $this->_alignment;
545  }
546 
552  public function getNumberFormat()
553  {
554  return $this->_numberFormat;
555  }
556 
562  public function getConditionalStyles()
563  {
564  return $this->getActiveSheet()->getConditionalStyles($this->getActiveCell());
565  }
566 
573  public function setConditionalStyles($pValue = null)
574  {
575  if (is_array($pValue)) {
576  $this->getActiveSheet()->setConditionalStyles($this->getSelectedCells(), $pValue);
577  }
578  return $this;
579  }
580 
586  public function getProtection()
587  {
588  return $this->_protection;
589  }
590 
596  public function getQuotePrefix()
597  {
598  if ($this->_isSupervisor) {
599  return $this->getSharedComponent()->getQuotePrefix();
600  }
601  return $this->_quotePrefix;
602  }
603 
609  public function setQuotePrefix($pValue)
610  {
611  if ($pValue == '') {
612  $pValue = false;
613  }
614  if ($this->_isSupervisor) {
615  $styleArray = array('quotePrefix' => $pValue);
616  $this->getActiveSheet()->getStyle($this->getSelectedCells())->applyFromArray($styleArray);
617  } else {
618  $this->_quotePrefix = (boolean) $pValue;
619  }
620  return $this;
621  }
622 
628  public function getHashCode()
629  {
630  $hashConditionals = '';
631  foreach ($this->_conditionalStyles as $conditional) {
632  $hashConditionals .= $conditional->getHashCode();
633  }
634 
635  return md5(
636  $this->_fill->getHashCode()
637  . $this->_font->getHashCode()
638  . $this->_borders->getHashCode()
639  . $this->_alignment->getHashCode()
640  . $this->_numberFormat->getHashCode()
641  . $hashConditionals
642  . $this->_protection->getHashCode()
643  . ($this->_quotePrefix ? 't' : 'f')
644  . __CLASS__
645  );
646  }
647 
653  public function getIndex()
654  {
655  return $this->_index;
656  }
657 
663  public function setIndex($pValue)
664  {
665  $this->_index = $pValue;
666  }
667 
668 }
setIndex($pValue)
Set own index in style collection.
Definition: Style.php:663
$style
Definition: example_012.php:70
getHashCode()
Get hash code.
Definition: Style.php:628
static coordinateFromString($pCoordinateString='A1')
Coordinate from string.
Definition: Cell.php:580
$x
Definition: example_009.php:98
setConditionalStyles($pValue=null)
Set Conditional Styles.
Definition: Style.php:573
getSelectedCells()
Get the currently active cell coordinate in currently active sheet.
Definition: Supervisor.php:103
getProtection()
Get Protection.
Definition: Style.php:586
setFont(PHPExcel_Style_Font $font)
Set font.
Definition: Style.php:521
getBorders()
Get Borders.
Definition: Style.php:532
applyFromArray($pStyles=null, $pAdvanced=true)
Apply styles from array.
Definition: Style.php:217
$y
Definition: example_007.php:83
getConditionalStyles()
Get Conditional Styles.
Definition: Style.php:562
getQuotePrefix()
Get quote prefix.
Definition: Style.php:596
Create styles array
The data for the language used.
getAlignment()
Get Alignment.
Definition: Style.php:542
getFill()
Get Fill.
Definition: Style.php:500
getSharedComponent()
Get the shared style component for the currently active cell in currently active sheet.
Definition: Style.php:142
static columnIndexFromString($pString='A')
Column index from string.
Definition: Cell.php:782
getActiveCell()
Get the currently active cell coordinate in currently active sheet.
Definition: Supervisor.php:114
static stringFromColumnIndex($pColumnIndex=0)
String from columnindex.
Definition: Cell.php:825
getStyleArray($array)
Build style array from subcomponents.
Definition: Style.php:172
getIndex()
Get own index in style collection.
Definition: Style.php:653
getNumberFormat()
Get Number Format.
Definition: Style.php:552
__construct($isSupervisor=false, $isConditional=false)
Create a new PHPExcel_Style.
Definition: Style.php:111
$_conditionalStyles
Definition: Style.php:78
getActiveSheet()
Get the currently active sheet.
Definition: Supervisor.php:92
getParent()
Get parent.
Definition: Style.php:161
setQuotePrefix($pValue)
Set quote prefix.
Definition: Style.php:609
getFont()
Get Font.
Definition: Style.php:510