Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
<?php

namespace Box\Spout\Writer\XLSX\Manager\Style;

use Box\Spout\Common\Entity\Style\Color;
use Box\Spout\Common\Entity\Style\Style;
use Box\Spout\Writer\XLSX\Helper\BorderHelper;

/**
 * Class StyleManager
 * Manages styles to be applied to a cell
 */
class StyleManager extends \Box\Spout\Writer\Common\Manager\Style\StyleManager
{
    /** @var StyleRegistry */
    protected $styleRegistry;

    /**
     * For empty cells, we can specify a style or not. If no style are specified,
     * then the software default will be applied. But sometimes, it may be useful
     * to override this default style, for instance if the cell should have a
     * background color different than the default one or some borders
     * (fonts property don't really matter here).
     *
     * @param int $styleId
     * @return bool Whether the cell should define a custom style
     */
    public function shouldApplyStyleOnEmptyCell($styleId)
    {
        $associatedFillId = $this->styleRegistry->getFillIdForStyleId($styleId);
        $hasStyleCustomFill = ($associatedFillId !== null && $associatedFillId !== 0);

        $associatedBorderId = $this->styleRegistry->getBorderIdForStyleId($styleId);
        $hasStyleCustomBorders = ($associatedBorderId !== null && $associatedBorderId !== 0);

< return ($hasStyleCustomFill || $hasStyleCustomBorders);
> $associatedFormatId = $this->styleRegistry->getFormatIdForStyleId($styleId); > $hasStyleCustomFormats = ($associatedFormatId !== null && $associatedFormatId !== 0); > > return ($hasStyleCustomFill || $hasStyleCustomBorders || $hasStyleCustomFormats);
} /** * Returns the content of the "styles.xml" file, given a list of styles. * * @return string */ public function getStylesXMLFileContent() { $content = <<<'EOD' <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"> EOD;
> $content .= $this->getFormatsSectionContent();
$content .= $this->getFontsSectionContent(); $content .= $this->getFillsSectionContent(); $content .= $this->getBordersSectionContent(); $content .= $this->getCellStyleXfsSectionContent(); $content .= $this->getCellXfsSectionContent(); $content .= $this->getCellStylesSectionContent(); $content .= <<<'EOD' </styleSheet> EOD; return $content; } /**
> * Returns the content of the "<numFmts>" section. * Returns the content of the "<fonts>" section. > * * > * @return string * @return string > */ */ > protected function getFormatsSectionContent() protected function getFontsSectionContent() > { { > $tags = []; $registeredStyles = $this->styleRegistry->getRegisteredStyles(); > $registeredFormats = $this->styleRegistry->getRegisteredFormats(); > foreach ($registeredFormats as $styleId) { $content = '<fonts count="' . count($registeredStyles) . '">'; > $numFmtId = $this->styleRegistry->getFormatIdForStyleId($styleId); > /** @var Style $style */ > //Built-in formats do not need to be declared, skip them foreach ($registeredStyles as $style) { > if ($numFmtId < 164) { $content .= '<font>'; > continue; > } $content .= '<sz val="' . $style->getFontSize() . '"/>'; > $content .= '<color rgb="' . Color::toARGB($style->getFontColor()) . '"/>'; > /** @var Style $style */ $content .= '<name val="' . $style->getFontName() . '"/>'; > $style = $this->styleRegistry->getStyleFromStyleId($styleId); > $format = $style->getFormat(); if ($style->isFontBold()) { > $tags[] = '<numFmt numFmtId="' . $numFmtId . '" formatCode="' . $format . '"/>'; $content .= '<b/>'; > } } > $content = '<numFmts count="' . \count($tags) . '">'; if ($style->isFontItalic()) { > $content .= \implode('', $tags); $content .= '<i/>'; > $content .= '</numFmts>'; } > if ($style->isFontUnderline()) { > return $content; $content .= '<u/>'; > } } > if ($style->isFontStrikethrough()) { > /**
< $content = '<fonts count="' . count($registeredStyles) . '">';
> $content = '<fonts count="' . \count($registeredStyles) . '">';
} $content .= '</font>'; } $content .= '</fonts>'; return $content; } /** * Returns the content of the "<fills>" section. * * @return string */ protected function getFillsSectionContent() { $registeredFills = $this->styleRegistry->getRegisteredFills(); // Excel reserves two default fills
< $fillsCount = count($registeredFills) + 2; < $content = sprintf('<fills count="%d">', $fillsCount);
> $fillsCount = \count($registeredFills) + 2; > $content = \sprintf('<fills count="%d">', $fillsCount);
$content .= '<fill><patternFill patternType="none"/></fill>'; $content .= '<fill><patternFill patternType="gray125"/></fill>'; // The other fills are actually registered by setting a background color foreach ($registeredFills as $styleId) { /** @var Style $style */ $style = $this->styleRegistry->getStyleFromStyleId($styleId); $backgroundColor = $style->getBackgroundColor();
< $content .= sprintf(
> $content .= \sprintf(
'<fill><patternFill patternType="solid"><fgColor rgb="%s"/></patternFill></fill>', $backgroundColor ); } $content .= '</fills>'; return $content; } /** * Returns the content of the "<borders>" section. * * @return string */ protected function getBordersSectionContent() { $registeredBorders = $this->styleRegistry->getRegisteredBorders(); // There is one default border with index 0
< $borderCount = count($registeredBorders) + 1;
> $borderCount = \count($registeredBorders) + 1;
$content = '<borders count="' . $borderCount . '">'; // Default border starting at index 0 $content .= '<border><left/><right/><top/><bottom/></border>'; foreach ($registeredBorders as $styleId) { /** @var \Box\Spout\Common\Entity\Style\Style $style */ $style = $this->styleRegistry->getStyleFromStyleId($styleId); $border = $style->getBorder(); $content .= '<border>'; // @link https://github.com/box/spout/issues/271 $sortOrder = ['left', 'right', 'top', 'bottom']; foreach ($sortOrder as $partName) { if ($border->hasPart($partName)) { /** @var $part \Box\Spout\Common\Entity\Style\BorderPart */ $part = $border->getPart($partName); $content .= BorderHelper::serializeBorderPart($part); } } $content .= '</border>'; } $content .= '</borders>'; return $content; } /** * Returns the content of the "<cellStyleXfs>" section. * * @return string */ protected function getCellStyleXfsSectionContent() { return <<<'EOD' <cellStyleXfs count="1"> <xf borderId="0" fillId="0" fontId="0" numFmtId="0"/> </cellStyleXfs> EOD; } /** * Returns the content of the "<cellXfs>" section. * * @return string */ protected function getCellXfsSectionContent() { $registeredStyles = $this->styleRegistry->getRegisteredStyles();
< $content = '<cellXfs count="' . count($registeredStyles) . '">';
> $content = '<cellXfs count="' . \count($registeredStyles) . '">';
foreach ($registeredStyles as $style) { $styleId = $style->getId(); $fillId = $this->getFillIdForStyleId($styleId); $borderId = $this->getBorderIdForStyleId($styleId);
> $numFmtId = $this->getFormatIdForStyleId($styleId);
< $content .= '<xf numFmtId="0" fontId="' . $styleId . '" fillId="' . $fillId . '" borderId="' . $borderId . '" xfId="0"';
> $content .= '<xf numFmtId="' . $numFmtId . '" fontId="' . $styleId . '" fillId="' . $fillId . '" borderId="' . $borderId . '" xfId="0"';
if ($style->shouldApplyFont()) { $content .= ' applyFont="1"'; }
< $content .= sprintf(' applyBorder="%d"', $style->shouldApplyBorder() ? 1 : 0);
> $content .= \sprintf(' applyBorder="%d"', $style->shouldApplyBorder() ? 1 : 0);
< if ($style->shouldWrapText()) {
> if ($style->shouldApplyCellAlignment() || $style->shouldWrapText()) {
$content .= ' applyAlignment="1">';
< $content .= '<alignment wrapText="1"/>';
> $content .= '<alignment'; > if ($style->shouldApplyCellAlignment()) { > $content .= \sprintf(' horizontal="%s"', $style->getCellAlignment()); > } > if ($style->shouldWrapText()) { > $content .= ' wrapText="1"'; > } > $content .= '/>';
$content .= '</xf>'; } else { $content .= '/>'; } } $content .= '</cellXfs>'; return $content; } /** * Returns the fill ID associated to the given style ID. * For the default style, we don't a fill. * * @param int $styleId * @return int */ private function getFillIdForStyleId($styleId) { // For the default style (ID = 0), we don't want to override the fill. // Otherwise all cells of the spreadsheet will have a background color. $isDefaultStyle = ($styleId === 0); return $isDefaultStyle ? 0 : ($this->styleRegistry->getFillIdForStyleId($styleId) ?: 0); } /** * Returns the fill ID associated to the given style ID. * For the default style, we don't a border. * * @param int $styleId * @return int */ private function getBorderIdForStyleId($styleId) { // For the default style (ID = 0), we don't want to override the border. // Otherwise all cells of the spreadsheet will have a border. $isDefaultStyle = ($styleId === 0); return $isDefaultStyle ? 0 : ($this->styleRegistry->getBorderIdForStyleId($styleId) ?: 0);
> } } > > /** /** > * Returns the format ID associated to the given style ID. * Returns the content of the "<cellStyles>" section. > * For the default style use general format. * > * * @return string > * @param int $styleId */ > * @return int protected function getCellStylesSectionContent() > */ { > private function getFormatIdForStyleId($styleId) return <<<'EOD' > { <cellStyles count="1"> > // For the default style (ID = 0), we don't want to override the format. <cellStyle builtinId="0" name="Normal" xfId="0"/> > // Otherwise all cells of the spreadsheet will have a format. </cellStyles> > $isDefaultStyle = ($styleId === 0); EOD; > } > return $isDefaultStyle ? 0 : ($this->styleRegistry->getFormatIdForStyleId($styleId) ?: 0);
}