Merge branch 'develop' of https://github.com/diego-vieira/PHPWord into #191-diego-vieira
This commit is contained in:
commit
61885e0d37
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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``.
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
include_once 'Sample_Header.php';
|
||||
|
||||
// New Word document
|
||||
echo date('H:i:s') , " Create new PhpWord object" , \EOL;
|
||||
$phpWord = new \PhpOffice\PhpWord\PhpWord();
|
||||
|
||||
$document = $phpWord->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';
|
||||
Binary file not shown.
|
|
@ -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.");
|
||||
|
|
|
|||
|
|
@ -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.");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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.");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
||||
|
|
|
|||
|
|
@ -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';
|
||||
|
|
|
|||
|
|
@ -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.*)(<w:p.*>\${' . $blockname . '}<\/w:.*?p>)(.*)(<w:p.*\${\/' . $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.*)(<w:p.*>\${' . $blockname . '}<\/w:.*?p>)(.*)(<w:p.*\${\/' . $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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -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/')) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue