-
-
Save anonymous/490418104fdf5cd1a791 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function deleteTemplateBlock($blockName) | |
{ | |
$aType = array('BLOCK_'/* , 'TAB_' */); //deletables types | |
foreach ($aType as $type) { | |
$variableName = $type . $blockName; | |
$loadContent = $this->_documentXMLElement . '<w:body>' . | |
$this->_wordDocumentC . '</w:body></w:document>'; | |
if (!self::$_preprocessed) { | |
$loadContent = $this->repairVariables(array($variableName => ''), $loadContent); | |
} | |
$loadContent = preg_replace('/\\' . self::$_templateSymbol . $type . $blockName . '([|]|\\' . self::$_templateSymbol . ').*?\\' . self::$_templateSymbol . $type . $blockName . '.*?\\' . self::$_templateSymbol . '/', self::$_templateSymbol . $variableName . self::$_templateSymbol, $loadContent); | |
//Use XPath to find all paragraphs that include the variable name | |
$name = self::$_templateSymbol . $variableName . self::$_templateSymbol; | |
$domDocument = new DOMDocument(); | |
$domDocument->loadXML($loadContent); | |
$xpath = new DOMXPath($domDocument); | |
$xpath->registerNamespace('w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'); | |
$query = '//w:p[w:r/w:t[text()[contains(.,"' . $variableName . '")]]]'; | |
$affectedNodes = $xpath->query($query); | |
/** @var DOMNode $node */ | |
foreach ($affectedNodes as $node) { | |
$paragraphContents = $node->ownerDocument->saveXML($node); | |
$paragraphText = strip_tags($paragraphContents); | |
if (($pos = strpos($paragraphText, $name, 0)) !== false) { | |
//If we remove a paragraph inside a table cell we need to take special care | |
if ($node->parentNode->nodeName == 'w:tc') { | |
$tcChilds = $node->parentNode->getElementsByTagNameNS('http://schemas.openxmlformats.org/wordprocessingml/2006/main', 'p'); | |
if ($tcChilds->length > 1) { | |
$node->parentNode->removeChild($node); | |
} else { | |
$emptyP = $domDocument->createElement("w:p"); | |
$node->parentNode->appendChild($emptyP); | |
$node->parentNode->removeChild($node); | |
} | |
} else { | |
$node->parentNode->removeChild($node); | |
} | |
} | |
} | |
$bodyTag = explode('<w:body>', $domDocument->saveXML()); | |
$this->_wordDocumentC = str_replace('</w:body></w:document>', '', $bodyTag[1]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment