diff --git a/CHANGELOG.md b/CHANGELOG.md index 08ef04fb..76dfcbd9 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ This is the changelog between releases of PHPWord. Releases are listed in revers - Element: New `CheckBox` element for sections and table cells - @ozilion GH-156 - Settings: Ability to use PCLZip as alternative to ZipArchive - @bskrtich @ivanlanin GH-106 GH-140 GH-185 - Template: Ability to find & replace variables in headers & footers - @dgudgeon GH-190 +- Template: Ability to clone & delete block of text using `cloneBlock` and `deleteBlock` - @diego-vieira GH-191 ### Bugfixes diff --git a/docs/templates.rst b/docs/templates.rst index 6b627b06..ec9c7126 100644 --- a/docs/templates.rst +++ b/docs/templates.rst @@ -19,6 +19,8 @@ Example: $template->setValue('Name', 'Somebody someone'); $template->setValue('Street', 'Coming-Undone-Street 32'); -See ``Sample_07_TemplateCloneRow.php`` for more code sample, including -how to create multirow from a single row in a template by using -``cloneRow``. +See ``Sample_07_TemplateCloneRow.php`` for example on how to create multirow +from a single row in a template by using ``cloneRow``. + +See ``Sample_23_TemplateBlock.php`` for example on how to clone a block of +text using ``cloneBlock`` and delete a block of text using ``deleteBlock``. diff --git a/samples/Sample_23_TemplateBlock.php b/samples/Sample_23_TemplateBlock.php new file mode 100644 index 00000000..168070e0 --- /dev/null +++ b/samples/Sample_23_TemplateBlock.php @@ -0,0 +1,21 @@ +loadTemplate('resources/Sample_23_TemplateBlock.docx'); + +// Will clone everything between ${tag} and ${/tag}, the number of times. By default, 1. +$document->cloneBlock('CLONEME', 3); + +// Everything between ${tag} and ${/tag}, will be deleted/erased. +$document->deleteBlock('DELETEME'); + +$name = 'Sample_23_TemplateBlock.docx'; +echo date('H:i:s'), " Write to Word2007 format", EOL; +$document->saveAs($name); +rename($name, "results/{$name}"); + +include_once 'Sample_Footer.php'; diff --git a/samples/resources/Sample_23_TemplateBlock.docx b/samples/resources/Sample_23_TemplateBlock.docx new file mode 100644 index 00000000..049d5ca4 Binary files /dev/null and b/samples/resources/Sample_23_TemplateBlock.docx differ diff --git a/src/PhpWord/PhpWord.php b/src/PhpWord/PhpWord.php index e0506803..f15c8f20 100644 --- a/src/PhpWord/PhpWord.php +++ b/src/PhpWord/PhpWord.php @@ -232,7 +232,7 @@ class PhpWord */ public function loadTemplate($filename) { - if (\file_exists($filename)) { + if (file_exists($filename)) { return new Template($filename); } else { throw new Exception("Template file {$filename} not found."); diff --git a/src/PhpWord/Reader/Reader.php b/src/PhpWord/Reader/Reader.php index 153a4013..15453c6b 100644 --- a/src/PhpWord/Reader/Reader.php +++ b/src/PhpWord/Reader/Reader.php @@ -65,7 +65,7 @@ abstract class Reader implements IReader protected function openFile($pFilename) { // Check if file exists - if (!\file_exists($pFilename) || !is_readable($pFilename)) { + if (!file_exists($pFilename) || !is_readable($pFilename)) { throw new Exception("Could not open " . $pFilename . " for reading! File does not exist."); } diff --git a/src/PhpWord/Reader/Word2007.php b/src/PhpWord/Reader/Word2007.php index 9161d162..82de0bed 100644 --- a/src/PhpWord/Reader/Word2007.php +++ b/src/PhpWord/Reader/Word2007.php @@ -29,7 +29,7 @@ class Word2007 extends Reader implements IReader public function canRead($pFilename) { // Check if file exists - if (!\file_exists($pFilename)) { + if (!file_exists($pFilename)) { throw new Exception("Could not open {$pFilename} for reading! File does not exist."); } diff --git a/src/PhpWord/Section/Image.php b/src/PhpWord/Section/Image.php index 9f5a7838..8f53fac2 100755 --- a/src/PhpWord/Section/Image.php +++ b/src/PhpWord/Section/Image.php @@ -112,7 +112,7 @@ class Image \IMAGETYPE_PNG, \IMAGETYPE_BMP, \IMAGETYPE_TIFF_II, \IMAGETYPE_TIFF_MM ); - if (!\file_exists($source)) { + if (!file_exists($source)) { throw new InvalidImageException; } $imgData = getimagesize($source); diff --git a/src/PhpWord/Section/Object.php b/src/PhpWord/Section/Object.php index 9b6cc328..56870b4d 100644 --- a/src/PhpWord/Section/Object.php +++ b/src/PhpWord/Section/Object.php @@ -61,7 +61,7 @@ class Object $_supportedObjectTypes = array('xls', 'doc', 'ppt', 'xlsx', 'docx', 'pptx'); $inf = pathinfo($src); - if (\file_exists($src) && in_array($inf['extension'], $_supportedObjectTypes)) { + if (file_exists($src) && in_array($inf['extension'], $_supportedObjectTypes)) { $this->_src = $src; $this->_style = new \PhpOffice\PhpWord\Style\Image(); diff --git a/src/PhpWord/Section/Table/Cell.php b/src/PhpWord/Section/Table/Cell.php index e0004350..43822d82 100755 --- a/src/PhpWord/Section/Table/Cell.php +++ b/src/PhpWord/Section/Table/Cell.php @@ -234,7 +234,7 @@ class Cell } $iconSrc = __DIR__ . '/../../_staticDocParts/'; - if (!\file_exists($iconSrc . '_' . $ext . '.png')) { + if (!file_exists($iconSrc . '_' . $ext . '.png')) { $iconSrc = $iconSrc . '_default.png'; } else { $iconSrc .= '_' . $ext . '.png'; diff --git a/src/PhpWord/Template.php b/src/PhpWord/Template.php index f953c5f1..edc1541f 100644 --- a/src/PhpWord/Template.php +++ b/src/PhpWord/Template.php @@ -165,7 +165,7 @@ class Template * Clone a table row in a template document * * @param string $search - * @param int $numberOfClones + * @param integer $numberOfClones * @throws Exception */ public function cloneRow($search, $numberOfClones) @@ -216,6 +216,60 @@ class Template $this->documentXML = $result; } + /** + * Clone a block + * + * @param string $blockname + * @param integer $clones + * @param boolean $replace + * @return null + */ + public function cloneBlock($blockname, $clones = 1, $replace = true) + { + $xmlBlock = null; + preg_match('/(<\?xml.*)(\${' . $blockname . '}<\/w:.*?p>)(.*)()/is', $this->documentXML, $matches); + + if (isset($matches[3])) { + $xmlBlock = $matches[3]; + $cloned = array(); + for ($i = 1; $i <= $clones; $i++) { + $cloned[] = $xmlBlock; + } + + if ($replace) { + $this->documentXML = str_replace($matches[2] . $matches[3] . $matches[4], implode('', $cloned), $this->documentXML); + } + } + + return $xmlBlock; + } + + /** + * Replace a block + * + * @param string $blockname + * @param string $replacement + */ + public function replaceBlock($blockname, $replacement) + { + preg_match('/(<\?xml.*)(\${' . $blockname . '}<\/w:.*?p>)(.*)()/is', $this->documentXML, $matches); + + if (isset($matches[3])) { + $this->documentXML = str_replace($matches[2] . $matches[3] . $matches[4], $replacement, $this->documentXML); + } + } + + /** + * Delete a block of text + * + * @param string $blockname + * @param string $replacement + */ + public function deleteBlock($blockname) + { + $this->replaceBlock($blockname, ''); + } + /** * Save XML to temporary file * @@ -251,7 +305,7 @@ class Template { $tempFilename = $this->save(); - if (\file_exists($strFilename)) { + if (file_exists($strFilename)) { unlink($strFilename); } @@ -332,8 +386,8 @@ class Template /** * Find the start position of the nearest table row before $offset * - * @param int $offset - * @return int + * @param integer $offset + * @return integer * @throws Exception */ private function findRowStart($offset) @@ -351,8 +405,8 @@ class Template /** * Find the end position of the nearest table row after $offset * - * @param int $offset - * @return int + * @param integer $offset + * @return integer */ private function findRowEnd($offset) { @@ -363,8 +417,8 @@ class Template /** * Get a slice of a string * - * @param int $startPosition - * @param int $endPosition + * @param integer $startPosition + * @param integer $endPosition * @return string */ private function getSlice($startPosition, $endPosition = 0) @@ -374,4 +428,17 @@ class Template } return substr($this->documentXML, $startPosition, ($endPosition - $startPosition)); } + + /** + * Delete a block of text + * + * @param string $blockname + * @param string $replacement + * @deprecated + * @codeCoverageIgnore + */ + public function deleteTemplateBlock($blockname, $replacement = '') + { + $this->deleteBlock($blockname, $replacement); + } } diff --git a/tests/PhpWord/Tests/TemplateTest.php b/tests/PhpWord/Tests/TemplateTest.php index 13ed7646..7050bf8e 100644 --- a/tests/PhpWord/Tests/TemplateTest.php +++ b/tests/PhpWord/Tests/TemplateTest.php @@ -179,4 +179,28 @@ final class TemplateTest extends \PHPUnit_Framework_TestCase $this->assertTrue($docFound); } + + /** + * Clone and delete block + */ + public function testCloneDeleteBlock() + { + $template = __DIR__ . "/_files/templates/clone-delete-block.docx"; + $expectedVar = array('DELETEME', '/DELETEME', 'CLONEME', '/CLONEME'); + $docName = 'clone-delete-block-result.docx'; + + $document = new Template($template); + $actualVar = $document->getVariables(); + + $document->cloneBlock('CLONEME', 3); + $document->deleteBlock('DELETEME'); + + $document->saveAs($docName); + $docFound = file_exists($docName); + unlink($docName); + + $this->assertEquals($expectedVar, $actualVar); + $this->assertTrue($docFound); + + } } diff --git a/tests/PhpWord/Tests/Writer/ODTextTest.php b/tests/PhpWord/Tests/Writer/ODTextTest.php index 8c345f55..6bf9848f 100644 --- a/tests/PhpWord/Tests/Writer/ODTextTest.php +++ b/tests/PhpWord/Tests/Writer/ODTextTest.php @@ -83,7 +83,7 @@ class ODTextTest extends \PHPUnit_Framework_TestCase $writer = new ODText($phpWord); $writer->save($file); - $this->assertTrue(\file_exists($file)); + $this->assertTrue(file_exists($file)); unlink($file); } diff --git a/tests/PhpWord/Tests/Writer/RTFTest.php b/tests/PhpWord/Tests/Writer/RTFTest.php index 7b546438..ba110fc1 100644 --- a/tests/PhpWord/Tests/Writer/RTFTest.php +++ b/tests/PhpWord/Tests/Writer/RTFTest.php @@ -72,7 +72,7 @@ class RTFTest extends \PHPUnit_Framework_TestCase $writer = new RTF($phpWord); $writer->save($file); - $this->assertTrue(\file_exists($file)); + $this->assertTrue(file_exists($file)); unlink($file); } diff --git a/tests/PhpWord/Tests/_files/templates/clone-delete-block.docx b/tests/PhpWord/Tests/_files/templates/clone-delete-block.docx new file mode 100644 index 00000000..049d5ca4 Binary files /dev/null and b/tests/PhpWord/Tests/_files/templates/clone-delete-block.docx differ diff --git a/tests/PhpWord/Tests/_includes/TestHelperDOCX.php b/tests/PhpWord/Tests/_includes/TestHelperDOCX.php index 3e24c4cc..bea0d037 100644 --- a/tests/PhpWord/Tests/_includes/TestHelperDOCX.php +++ b/tests/PhpWord/Tests/_includes/TestHelperDOCX.php @@ -56,7 +56,7 @@ class TestHelperDOCX */ public static function clear() { - if (\file_exists(self::$file)) { + if (file_exists(self::$file)) { unlink(self::$file); } if (is_dir(sys_get_temp_dir() . '/PhpWord_Unit_Test/')) {