From 8d76b29d1784a11f84eb22be30c334fb10f5cbbb Mon Sep 17 00:00:00 2001 From: Dave Umrysh Date: Tue, 27 Jul 2021 16:50:25 +0000 Subject: [PATCH 1/2] Allow the changing of a cell's background color --- .../phpword/src/PhpWord/TemplateProcessor.php | 175 ++++++++++++++++++ 1 file changed, 175 insertions(+) diff --git a/vendor/phpoffice/phpword/src/PhpWord/TemplateProcessor.php b/vendor/phpoffice/phpword/src/PhpWord/TemplateProcessor.php index 146c233..8461a52 100644 --- a/vendor/phpoffice/phpword/src/PhpWord/TemplateProcessor.php +++ b/vendor/phpoffice/phpword/src/PhpWord/TemplateProcessor.php @@ -1347,4 +1347,179 @@ class TemplateProcessor $str ); } + + // The below is from here: https://pastebin.com/Hq9qmxih + // So I can do this: https://github.com/PHPOffice/PHPWord/issues/984#issuecomment-334967453 + + /** + * Get a segment. (first segment found) + * + * @param string $needle + * @param string $xmltag + * @param string $docpart + * @param boolean $throwexception + * + * @return string|null + */ + public function getSegment($needle, $xmltag, $docpart = 'MainPart', $throwexception = false) + { + return $this->cloneSegment($needle, $xmltag, $docpart, 1, false, false, $throwexception); + } + + /** + * Clone a segment. + * + * @param string $needle + * @param string $xmltag + * @param string $docpart + * @param integer $clones + * @param boolean $replace + * @param boolean $incrementVariables + * @param boolean $throwexception + * + * @return string|null + */ + public function cloneSegment( + $needle, + $xmltag, + $docpart = 'MainPart', + $clones = 1, + $replace = true, + $incrementVariables = true, + $throwexception = false + ) { + $needlePos = strpos($this->{"tempDocument$docpart"}, $needle); + + if (!$needlePos) { + if ($throwexception) { + throw new Exception( + "Can not find segment '$needle', text not found or text contains markup." + ); + } else { + return null; // Segment not found, return null + } + } + + $startSegmentStart = $this->findTagLeft("<$xmltag>", $needlePos, $throwexception); + $endSegmentEnd = $this->findTagRight("", $needlePos); + + if (!$startSegmentStart || !$endSegmentEnd) { + if ($throwexception) { + throw new Exception( + "Can not find <$xmltag> around segment '$needle'" + ); + } else { + return false; + } + } + + $xmlSegment = $this->getSlice($startSegmentStart, $endSegmentEnd); + + if ($replace) { + $result = $this->getSlice(0, $startSegmentStart); + for ($i = 1; $i <= $clones; $i++) { + if ($incrementVariables) { + $result .= preg_replace('/\$\{(.*?)\}/', '\${\\1#' . $i . '}', $xmlSegment); + } else { + $result .= $xmlSegment; + } + } + $result .= $this->getSlice($endSegmentEnd); + + $this->{"tempDocument$docpart"} = $result; + } + + return $xmlSegment; + } + + /** + * Replace a segment. + * + * @param string $needle + * @param string $xmltag + * @param string $replacement + * @param string $docpart + * @param boolean $throwexception + * + * @return false on no replacement, true on replacement + */ + public function replaceSegment($needle, $xmltag, $replacement = '', $docpart = 'MainPart', $throwexception = false) + { + $TagPos = strpos($this->{"tempDocument$docpart"}, $needle); + + if ($TagPos === false) { + if ($throwexception) { + throw new Exception( + "Can not find segment '$needle', text not found or text contains markup." + ); + } else { + return false; + } + } + + $SegmentStart = $this->findTagLeft("<$xmltag>", $TagPos, $throwexception); + $SegmentEnd = $this->findTagRight("", $TagPos); + + $this->{"tempDocument$docpart"} = + $this->getSlice(0, $SegmentStart) + . $replacement + . $this->getSlice($SegmentEnd); + + return true; + } + + /** + * Find the start position of the nearest $tag before $offset. + * + * @param string $tag + * @param integer $offset + * @param boolean $throwexception + * + * @return integer + * + * @throws \PhpOffice\PhpWord\Exception\Exception + */ + protected function findTagLeft($tag, $offset = 0, $throwexception = false) + { + $tagStart = strrpos( + $this->tempDocumentMainPart, + substr($tag, 0, -1) . ' ', + ((strlen($this->tempDocumentMainPart) - $offset) * -1) + ); + + if (!$tagStart) { + $tagStart = strrpos( + $this->tempDocumentMainPart, + $tag, + ((strlen($this->tempDocumentMainPart) - $offset) * -1) + ); + } + if (!$tagStart) { + if ($throwexception) { + throw new Exception('Can not find the start position of the item to clone.'); + } else { + return 0; + } + } + + return $tagStart; + } + + /** + * Find the end position of the nearest $tag after $offset. + * + * @param string $tag + * @param integer $offset + * + * @return integer + */ + protected function findTagRight($tag, $offset = 0) + { + $pos = strpos($this->tempDocumentMainPart, $tag, $offset); + if ($pos !== false) { + return $pos + strlen($tag); + } else { + return 0; + } + } } From ff95c7db65a15ba0d426c661d793f478d504e16f Mon Sep 17 00:00:00 2001 From: Dave Umrysh Date: Tue, 21 Dec 2021 22:04:22 +0000 Subject: [PATCH 2/2] Fix image border in Word2007 Writer for LibreOffice 7 https://github.com/PHPOffice/PHPWord/pull/2021 --- .../phpword/src/PhpWord/Writer/Word2007/Element/Image.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/vendor/phpoffice/phpword/src/PhpWord/Writer/Word2007/Element/Image.php b/vendor/phpoffice/phpword/src/PhpWord/Writer/Word2007/Element/Image.php index 7f79935..7d38aad 100644 --- a/vendor/phpoffice/phpword/src/PhpWord/Writer/Word2007/Element/Image.php +++ b/vendor/phpoffice/phpword/src/PhpWord/Writer/Word2007/Element/Image.php @@ -83,6 +83,7 @@ class Image extends AbstractElement $xmlWriter->startElement('w:pict'); $xmlWriter->startElement('v:shape'); $xmlWriter->writeAttribute('type', '#_x0000_t75'); + $xmlWriter->writeAttribute('stroked', 'f'); $styleWriter->write(); @@ -115,6 +116,7 @@ class Image extends AbstractElement $xmlWriter->startElement('w:pict'); $xmlWriter->startElement('v:shape'); $xmlWriter->writeAttribute('type', '#_x0000_t75'); + $xmlWriter->writeAttribute('stroked', 'f'); $styleWriter->write();