You are here

protected function PHPExcel_Reader_HTML::_processDomElement in Loft Data Grids 7.2

Same name and namespace in other branches
  1. 6.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Reader/HTML.php \PHPExcel_Reader_HTML::_processDomElement()
1 call to PHPExcel_Reader_HTML::_processDomElement()
PHPExcel_Reader_HTML::loadIntoExisting in vendor/phpoffice/phpexcel/Classes/PHPExcel/Reader/HTML.php
Loads PHPExcel from file into PHPExcel instance

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Reader/HTML.php, line 216

Class

PHPExcel_Reader_HTML
PHPExcel_Reader_HTML

Code

protected function _processDomElement(DOMNode $element, $sheet, &$row, &$column, &$cellContent, $format = null) {
  foreach ($element->childNodes as $child) {
    if ($child instanceof DOMText) {
      $domText = preg_replace('/\\s+/u', ' ', trim($child->nodeValue));
      if (is_string($cellContent)) {

        //	simply append the text if the cell content is a plain text string
        $cellContent .= $domText;
      }
      else {

        //	but if we have a rich text run instead, we need to append it correctly
        //	TODO
      }
    }
    elseif ($child instanceof DOMElement) {

      //				echo '<b>DOM ELEMENT: </b>' , strtoupper($child->nodeName) , '<br />';
      $attributeArray = array();
      foreach ($child->attributes as $attribute) {

        //					echo '<b>ATTRIBUTE: </b>' , $attribute->name , ' => ' , $attribute->value , '<br />';
        $attributeArray[$attribute->name] = $attribute->value;
      }
      switch ($child->nodeName) {
        case 'meta':
          foreach ($attributeArray as $attributeName => $attributeValue) {
            switch ($attributeName) {
              case 'content':

                //	TODO
                //	Extract character set, so we can convert to UTF-8 if required
                break;
            }
          }
          $this
            ->_processDomElement($child, $sheet, $row, $column, $cellContent);
          break;
        case 'title':
          $this
            ->_processDomElement($child, $sheet, $row, $column, $cellContent);
          $sheet
            ->setTitle($cellContent);
          $cellContent = '';
          break;
        case 'span':
        case 'div':
        case 'font':
        case 'i':
        case 'em':
        case 'strong':
        case 'b':

          //						echo 'STYLING, SPAN OR DIV<br />';
          if ($cellContent > '') {
            $cellContent .= ' ';
          }
          $this
            ->_processDomElement($child, $sheet, $row, $column, $cellContent);
          if ($cellContent > '') {
            $cellContent .= ' ';
          }

          //						echo 'END OF STYLING, SPAN OR DIV<br />';
          break;
        case 'hr':
          $this
            ->_flushCell($sheet, $column, $row, $cellContent);
          ++$row;
          if (isset($this->_formats[$child->nodeName])) {
            $sheet
              ->getStyle($column . $row)
              ->applyFromArray($this->_formats[$child->nodeName]);
          }
          else {
            $cellContent = '----------';
            $this
              ->_flushCell($sheet, $column, $row, $cellContent);
          }
          ++$row;
        case 'br':
          if ($this->_tableLevel > 0) {

            //	If we're inside a table, replace with a \n
            $cellContent .= "\n";
          }
          else {

            //	Otherwise flush our existing content and move the row cursor on
            $this
              ->_flushCell($sheet, $column, $row, $cellContent);
            ++$row;
          }

          //						echo 'HARD LINE BREAK: ' , '<br />';
          break;
        case 'a':

          //						echo 'START OF HYPERLINK: ' , '<br />';
          foreach ($attributeArray as $attributeName => $attributeValue) {
            switch ($attributeName) {
              case 'href':

                //									echo 'Link to ' , $attributeValue , '<br />';
                $sheet
                  ->getCell($column . $row)
                  ->getHyperlink()
                  ->setUrl($attributeValue);
                if (isset($this->_formats[$child->nodeName])) {
                  $sheet
                    ->getStyle($column . $row)
                    ->applyFromArray($this->_formats[$child->nodeName]);
                }
                break;
            }
          }
          $cellContent .= ' ';
          $this
            ->_processDomElement($child, $sheet, $row, $column, $cellContent);

          //						echo 'END OF HYPERLINK:' , '<br />';
          break;
        case 'h1':
        case 'h2':
        case 'h3':
        case 'h4':
        case 'h5':
        case 'h6':
        case 'ol':
        case 'ul':
        case 'p':
          if ($this->_tableLevel > 0) {

            //	If we're inside a table, replace with a \n
            $cellContent .= "\n";

            //							echo 'LIST ENTRY: ' , '<br />';
            $this
              ->_processDomElement($child, $sheet, $row, $column, $cellContent);

            //							echo 'END OF LIST ENTRY:' , '<br />';
          }
          else {
            if ($cellContent > '') {
              $this
                ->_flushCell($sheet, $column, $row, $cellContent);
              $row++;
            }

            //							echo 'START OF PARAGRAPH: ' , '<br />';
            $this
              ->_processDomElement($child, $sheet, $row, $column, $cellContent);

            //							echo 'END OF PARAGRAPH:' , '<br />';
            $this
              ->_flushCell($sheet, $column, $row, $cellContent);
            if (isset($this->_formats[$child->nodeName])) {
              $sheet
                ->getStyle($column . $row)
                ->applyFromArray($this->_formats[$child->nodeName]);
            }
            $row++;
            $column = 'A';
          }
          break;
        case 'li':
          if ($this->_tableLevel > 0) {

            //	If we're inside a table, replace with a \n
            $cellContent .= "\n";

            //							echo 'LIST ENTRY: ' , '<br />';
            $this
              ->_processDomElement($child, $sheet, $row, $column, $cellContent);

            //							echo 'END OF LIST ENTRY:' , '<br />';
          }
          else {
            if ($cellContent > '') {
              $this
                ->_flushCell($sheet, $column, $row, $cellContent);
            }
            ++$row;

            //							echo 'LIST ENTRY: ' , '<br />';
            $this
              ->_processDomElement($child, $sheet, $row, $column, $cellContent);

            //							echo 'END OF LIST ENTRY:' , '<br />';
            $this
              ->_flushCell($sheet, $column, $row, $cellContent);
            $column = 'A';
          }
          break;
        case 'table':
          $this
            ->_flushCell($sheet, $column, $row, $cellContent);
          $column = $this
            ->_setTableStartColumn($column);

          //						echo 'START OF TABLE LEVEL ' , $this->_tableLevel , '<br />';
          if ($this->_tableLevel > 1) {
            --$row;
          }
          $this
            ->_processDomElement($child, $sheet, $row, $column, $cellContent);

          //						echo 'END OF TABLE LEVEL ' , $this->_tableLevel , '<br />';
          $column = $this
            ->_releaseTableStartColumn();
          if ($this->_tableLevel > 1) {
            ++$column;
          }
          else {
            ++$row;
          }
          break;
        case 'thead':
        case 'tbody':
          $this
            ->_processDomElement($child, $sheet, $row, $column, $cellContent);
          break;
        case 'tr':
          $column = $this
            ->_getTableStartColumn();
          $cellContent = '';

          //						echo 'START OF TABLE ' , $this->_tableLevel , ' ROW<br />';
          $this
            ->_processDomElement($child, $sheet, $row, $column, $cellContent);
          ++$row;

          //						echo 'END OF TABLE ' , $this->_tableLevel , ' ROW<br />';
          break;
        case 'th':
        case 'td':

          //						echo 'START OF TABLE ' , $this->_tableLevel , ' CELL<br />';
          $this
            ->_processDomElement($child, $sheet, $row, $column, $cellContent);

          //						echo 'END OF TABLE ' , $this->_tableLevel , ' CELL<br />';
          while (isset($this->rowspan[$column . $row])) {
            ++$column;
          }
          $this
            ->_flushCell($sheet, $column, $row, $cellContent);

          //                        if (isset($attributeArray['style']) && !empty($attributeArray['style'])) {
          //                            $styleAry = $this->getPhpExcelStyleArray($attributeArray['style']);
          //
          //                            if (!empty($styleAry)) {
          //                                $sheet->getStyle($column . $row)->applyFromArray($styleAry);
          //                            }
          //                        }
          if (isset($attributeArray['rowspan']) && isset($attributeArray['colspan'])) {

            //create merging rowspan and colspan
            $columnTo = $column;
            for ($i = 0; $i < $attributeArray['colspan'] - 1; $i++) {
              ++$columnTo;
            }
            $range = $column . $row . ':' . $columnTo . ($row + $attributeArray['rowspan'] - 1);
            foreach (\PHPExcel_Cell::extractAllCellReferencesInRange($range) as $value) {
              $this->rowspan[$value] = true;
            }
            $sheet
              ->mergeCells($range);
            $column = $columnTo;
          }
          elseif (isset($attributeArray['rowspan'])) {

            //create merging rowspan
            $range = $column . $row . ':' . $column . ($row + $attributeArray['rowspan'] - 1);
            foreach (\PHPExcel_Cell::extractAllCellReferencesInRange($range) as $value) {
              $this->rowspan[$value] = true;
            }
            $sheet
              ->mergeCells($range);
          }
          elseif (isset($attributeArray['colspan'])) {

            //create merging colspan
            $columnTo = $column;
            for ($i = 0; $i < $attributeArray['colspan'] - 1; $i++) {
              ++$columnTo;
            }
            $sheet
              ->mergeCells($column . $row . ':' . $columnTo . $row);
            $column = $columnTo;
          }
          ++$column;
          break;
        case 'body':
          $row = 1;
          $column = 'A';
          $content = '';
          $this->_tableLevel = 0;
          $this
            ->_processDomElement($child, $sheet, $row, $column, $cellContent);
          break;
        default:
          $this
            ->_processDomElement($child, $sheet, $row, $column, $cellContent);
      }
    }
  }
}