You are here

private function PHPExcel_Writer_HTML::_generateRow in Loft Data Grids 7.2

Same name and namespace in other branches
  1. 6.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Writer/HTML.php \PHPExcel_Writer_HTML::_generateRow()

* Generate row * *

Parameters

PHPExcel_Worksheet $pSheet PHPExcel_Worksheet: * @param array $pValues Array containing cells in a row * @param int $pRow Row number (0-based) * @return string * @throws PHPExcel_Writer_Exception

1 call to PHPExcel_Writer_HTML::_generateRow()
PHPExcel_Writer_HTML::generateSheetData in vendor/phpoffice/phpexcel/Classes/PHPExcel/Writer/HTML.php
* Generate sheet data * *

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Writer/HTML.php, line 1084

Class

PHPExcel_Writer_HTML
PHPExcel_Writer_HTML

Code

private function _generateRow(PHPExcel_Worksheet $pSheet, $pValues = null, $pRow = 0, $cellType = 'td') {
  if (is_array($pValues)) {

    // Construct HTML
    $html = '';

    // Sheet index
    $sheetIndex = $pSheet
      ->getParent()
      ->getIndex($pSheet);

    // DomPDF and breaks
    if ($this->_isPdf && count($pSheet
      ->getBreaks()) > 0) {
      $breaks = $pSheet
        ->getBreaks();

      // check if a break is needed before this row
      if (isset($breaks['A' . $pRow])) {

        // close table: </table>
        $html .= $this
          ->_generateTableFooter();

        // insert page break
        $html .= '<div style="page-break-before:always" />';

        // open table again: <table> + <col> etc.
        $html .= $this
          ->_generateTableHeader($pSheet);
      }
    }

    // Write row start
    if (!$this->_useInlineCss) {
      $html .= '		  <tr class="row' . $pRow . '">' . PHP_EOL;
    }
    else {
      $style = isset($this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow]) ? $this
        ->_assembleCSS($this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow]) : '';
      $html .= '		  <tr style="' . $style . '">' . PHP_EOL;
    }

    // Write cells
    $colNum = 0;
    foreach ($pValues as $cellAddress) {
      $cell = $cellAddress > '' ? $pSheet
        ->getCell($cellAddress) : '';
      $coordinate = PHPExcel_Cell::stringFromColumnIndex($colNum) . ($pRow + 1);
      if (!$this->_useInlineCss) {
        $cssClass = '';
        $cssClass = 'column' . $colNum;
      }
      else {
        $cssClass = array();
        if ($cellType == 'th') {
          if (isset($this->_cssStyles['table.sheet' . $sheetIndex . ' th.column' . $colNum])) {
            $this->_cssStyles['table.sheet' . $sheetIndex . ' th.column' . $colNum];
          }
        }
        else {
          if (isset($this->_cssStyles['table.sheet' . $sheetIndex . ' td.column' . $colNum])) {
            $this->_cssStyles['table.sheet' . $sheetIndex . ' td.column' . $colNum];
          }
        }
      }
      $colSpan = 1;
      $rowSpan = 1;

      // initialize
      $cellData = '&nbsp;';

      // PHPExcel_Cell
      if ($cell instanceof PHPExcel_Cell) {
        $cellData = '';
        if (is_null($cell
          ->getParent())) {
          $cell
            ->attach($pSheet);
        }

        // Value
        if ($cell
          ->getValue() instanceof PHPExcel_RichText) {

          // Loop through rich text elements
          $elements = $cell
            ->getValue()
            ->getRichTextElements();
          foreach ($elements as $element) {

            // Rich text start?
            if ($element instanceof PHPExcel_RichText_Run) {
              $cellData .= '<span style="' . $this
                ->_assembleCSS($this
                ->_createCSSStyleFont($element
                ->getFont())) . '">';
              if ($element
                ->getFont()
                ->getSuperScript()) {
                $cellData .= '<sup>';
              }
              else {
                if ($element
                  ->getFont()
                  ->getSubScript()) {
                  $cellData .= '<sub>';
                }
              }
            }

            // Convert UTF8 data to PCDATA
            $cellText = $element
              ->getText();
            $cellData .= htmlspecialchars($cellText);
            if ($element instanceof PHPExcel_RichText_Run) {
              if ($element
                ->getFont()
                ->getSuperScript()) {
                $cellData .= '</sup>';
              }
              else {
                if ($element
                  ->getFont()
                  ->getSubScript()) {
                  $cellData .= '</sub>';
                }
              }
              $cellData .= '</span>';
            }
          }
        }
        else {
          if ($this->_preCalculateFormulas) {
            $cellData = PHPExcel_Style_NumberFormat::toFormattedString($cell
              ->getCalculatedValue(), $pSheet
              ->getParent()
              ->getCellXfByIndex($cell
              ->getXfIndex())
              ->getNumberFormat()
              ->getFormatCode(), array(
              $this,
              'formatColor',
            ));
          }
          else {
            $cellData = PHPExcel_Style_NumberFormat::toFormattedString($cell
              ->getValue(), $pSheet
              ->getParent()
              ->getCellXfByIndex($cell
              ->getXfIndex())
              ->getNumberFormat()
              ->getFormatCode(), array(
              $this,
              'formatColor',
            ));
          }
          $cellData = htmlspecialchars($cellData);
          if ($pSheet
            ->getParent()
            ->getCellXfByIndex($cell
            ->getXfIndex())
            ->getFont()
            ->getSuperScript()) {
            $cellData = '<sup>' . $cellData . '</sup>';
          }
          elseif ($pSheet
            ->getParent()
            ->getCellXfByIndex($cell
            ->getXfIndex())
            ->getFont()
            ->getSubScript()) {
            $cellData = '<sub>' . $cellData . '</sub>';
          }
        }

        // Converts the cell content so that spaces occuring at beginning of each new line are replaced by &nbsp;
        // Example: "  Hello\n to the world" is converted to "&nbsp;&nbsp;Hello\n&nbsp;to the world"
        $cellData = preg_replace("/(?m)(?:^|\\G) /", '&nbsp;', $cellData);

        // convert newline "\n" to '<br>'
        $cellData = nl2br($cellData);

        // Extend CSS class?
        if (!$this->_useInlineCss) {
          $cssClass .= ' style' . $cell
            ->getXfIndex();
          $cssClass .= ' ' . $cell
            ->getDataType();
        }
        else {
          if ($cellType == 'th') {
            if (isset($this->_cssStyles['th.style' . $cell
              ->getXfIndex()])) {
              $cssClass = array_merge($cssClass, $this->_cssStyles['th.style' . $cell
                ->getXfIndex()]);
            }
          }
          else {
            if (isset($this->_cssStyles['td.style' . $cell
              ->getXfIndex()])) {
              $cssClass = array_merge($cssClass, $this->_cssStyles['td.style' . $cell
                ->getXfIndex()]);
            }
          }

          // General horizontal alignment: Actual horizontal alignment depends on dataType
          $sharedStyle = $pSheet
            ->getParent()
            ->getCellXfByIndex($cell
            ->getXfIndex());
          if ($sharedStyle
            ->getAlignment()
            ->getHorizontal() == PHPExcel_Style_Alignment::HORIZONTAL_GENERAL && isset($this->_cssStyles['.' . $cell
            ->getDataType()]['text-align'])) {
            $cssClass['text-align'] = $this->_cssStyles['.' . $cell
              ->getDataType()]['text-align'];
          }
        }
      }

      // Hyperlink?
      if ($pSheet
        ->hyperlinkExists($coordinate) && !$pSheet
        ->getHyperlink($coordinate)
        ->isInternal()) {
        $cellData = '<a href="' . htmlspecialchars($pSheet
          ->getHyperlink($coordinate)
          ->getUrl()) . '" title="' . htmlspecialchars($pSheet
          ->getHyperlink($coordinate)
          ->getTooltip()) . '">' . $cellData . '</a>';
      }

      // Should the cell be written or is it swallowed by a rowspan or colspan?
      $writeCell = !(isset($this->_isSpannedCell[$pSheet
        ->getParent()
        ->getIndex($pSheet)][$pRow + 1][$colNum]) && $this->_isSpannedCell[$pSheet
        ->getParent()
        ->getIndex($pSheet)][$pRow + 1][$colNum]);

      // Colspan and Rowspan
      $colspan = 1;
      $rowspan = 1;
      if (isset($this->_isBaseCell[$pSheet
        ->getParent()
        ->getIndex($pSheet)][$pRow + 1][$colNum])) {
        $spans = $this->_isBaseCell[$pSheet
          ->getParent()
          ->getIndex($pSheet)][$pRow + 1][$colNum];
        $rowSpan = $spans['rowspan'];
        $colSpan = $spans['colspan'];

        //	Also apply style from last cell in merge to fix borders -
        //		relies on !important for non-none border declarations in _createCSSStyleBorder
        $endCellCoord = PHPExcel_Cell::stringFromColumnIndex($colNum + $colSpan - 1) . ($pRow + $rowSpan);
        if (!$this->_useInlineCss) {
          $cssClass .= ' style' . $pSheet
            ->getCell($endCellCoord)
            ->getXfIndex();
        }
      }

      // Write
      if ($writeCell) {

        // Column start
        $html .= '			<' . $cellType;
        if (!$this->_useInlineCss) {
          $html .= ' class="' . $cssClass . '"';
        }
        else {

          //** Necessary redundant code for the sake of PHPExcel_Writer_PDF **

          // We must explicitly write the width of the <td> element because TCPDF
          // does not recognize e.g. <col style="width:42pt">
          $width = 0;
          $i = $colNum - 1;
          $e = $colNum + $colSpan - 1;
          while ($i++ < $e) {
            if (isset($this->_columnWidths[$sheetIndex][$i])) {
              $width += $this->_columnWidths[$sheetIndex][$i];
            }
          }
          $cssClass['width'] = $width . 'pt';

          // We must also explicitly write the height of the <td> element because TCPDF
          // does not recognize e.g. <tr style="height:50pt">
          if (isset($this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow]['height'])) {
            $height = $this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow]['height'];
            $cssClass['height'] = $height;
          }

          //** end of redundant code **
          $html .= ' style="' . $this
            ->_assembleCSS($cssClass) . '"';
        }
        if ($colSpan > 1) {
          $html .= ' colspan="' . $colSpan . '"';
        }
        if ($rowSpan > 1) {
          $html .= ' rowspan="' . $rowSpan . '"';
        }
        $html .= '>';

        // Image?
        $html .= $this
          ->_writeImageInCell($pSheet, $coordinate);

        // Chart?
        if ($this->_includeCharts) {
          $html .= $this
            ->_writeChartInCell($pSheet, $coordinate);
        }

        // Cell data
        $html .= $cellData;

        // Column end
        $html .= '</' . $cellType . '>' . PHP_EOL;
      }

      // Next column
      ++$colNum;
    }

    // Write row end
    $html .= '		  </tr>' . PHP_EOL;

    // Return
    return $html;
  }
  else {
    throw new PHPExcel_Writer_Exception("Invalid parameters passed.");
  }
}