diff --git a/Classes/PHPWord.php b/Classes/PHPWord.php index d66fdb33..0614cee0 100755 --- a/Classes/PHPWord.php +++ b/Classes/PHPWord.php @@ -26,12 +26,13 @@ */ /** PHPWORD_BASE_PATH */ +// @codeCoverageIgnoreStart if (!defined('PHPWORD_BASE_PATH')) { define('PHPWORD_BASE_PATH', dirname(__FILE__) . '/'); require PHPWORD_BASE_PATH . 'PHPWord/Autoloader.php'; PHPWord_Autoloader::Register(); } - +// @codeCoverageIgnoreEnd /** * PHPWord @@ -155,7 +156,7 @@ class PHPWord } /** - * Get default Font size + * Get default Font size (in points) * @return string */ public function getDefaultFontSize() @@ -164,15 +165,24 @@ class PHPWord } /** - * Set default Font size + * Set default Font size (in points) * @param int $pValue */ public function setDefaultFontSize($pValue) { - $pValue = $pValue * 2; $this->_defaultFontSize = $pValue; } + /** + * Set default paragraph style definition to styles.xml + * + * @param array $styles Paragraph style definition + */ + public function setDefaultParagraphStyle($styles) + { + PHPWord_Style::setDefaultParagraphStyle($styles); + } + /** * Adds a paragraph style definition to styles.xml * @@ -217,16 +227,6 @@ class PHPWord PHPWord_Style::addTitleStyle($titleCount, $styleFont, $styleParagraph); } - /** - * Set default paragraph style definition to styles.xml - * - * @param array $styles Paragraph style definition - */ - public function setDefaultParagraphStyle($styles) - { - PHPWord_Style::setDefaultParagraphStyle($styles); - } - /** * Adds a hyperlink style to styles.xml * @@ -247,15 +247,6 @@ class PHPWord return $this->_sectionCollection; } - /** - * Get section count - * @return int - */ - private function _countSections() - { - return count($this->_sectionCollection); - } - /** * Load a Template File * @@ -268,7 +259,18 @@ class PHPWord $template = new PHPWord_Template($strFilename); return $template; } else { - trigger_error('Template file ' . $strFilename . ' not found.', E_USER_ERROR); + throw new PHPWord_Exception( + "Template file {$strFilename} not found." + ); } } -} \ No newline at end of file + + /** + * Get section count + * @return int + */ + private function _countSections() + { + return count($this->_sectionCollection); + } +} diff --git a/Classes/PHPWord/Autoloader.php b/Classes/PHPWord/Autoloader.php index cf476646..74d1a500 100755 --- a/Classes/PHPWord/Autoloader.php +++ b/Classes/PHPWord/Autoloader.php @@ -82,4 +82,4 @@ class PHPWord_Autoloader } } } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/DocumentProperties.php b/Classes/PHPWord/DocumentProperties.php index 2bf44043..3fc097b4 100755 --- a/Classes/PHPWord/DocumentProperties.php +++ b/Classes/PHPWord/DocumentProperties.php @@ -30,6 +30,13 @@ */ class PHPWord_DocumentProperties { + /** Constants */ + const PROPERTY_TYPE_BOOLEAN = 'b'; + const PROPERTY_TYPE_INTEGER = 'i'; + const PROPERTY_TYPE_FLOAT = 'f'; + const PROPERTY_TYPE_DATE = 'd'; + const PROPERTY_TYPE_STRING = 's'; + const PROPERTY_TYPE_UNKNOWN = 'u'; /** * Creator @@ -101,21 +108,36 @@ class PHPWord_DocumentProperties */ private $_company; + /** + * Manager + * + * @var string + */ + private $_manager; + + /** + * Custom Properties + * + * @var string + */ + private $_customProperties = array(); + /** * Create new PHPWord_DocumentProperties */ public function __construct() { - $this->_creator = ''; + $this->_creator = ''; $this->_lastModifiedBy = $this->_creator; - $this->_created = time(); - $this->_modified = time(); - $this->_title = ''; - $this->_subject = ''; - $this->_description = ''; - $this->_keywords = ''; - $this->_category = ''; - $this->_company = ''; + $this->_created = time(); + $this->_modified = time(); + $this->_title = ''; + $this->_subject = ''; + $this->_description = ''; + $this->_keywords = ''; + $this->_category = ''; + $this->_company = ''; + $this->_manager = ''; } /** @@ -343,4 +365,243 @@ class PHPWord_DocumentProperties $this->_company = $pValue; return $this; } -} \ No newline at end of file + + /** + * Get Manager + * + * @return string + */ + public function getManager() + { + return $this->_manager; + } + + /** + * Set Manager + * + * @param string $pValue + * @return PHPExcel_DocumentProperties + */ + public function setManager($pValue = '') + { + $this->_manager = $pValue; + return $this; + } + + /** + * Get a List of Custom Property Names + * + * @return array of string + */ + public function getCustomProperties() + { + return array_keys($this->_customProperties); + } + + /** + * Check if a Custom Property is defined + * + * @param string $propertyName + * @return boolean + */ + public function isCustomPropertySet($propertyName) + { + return isset($this->_customProperties[$propertyName]); + } + + /** + * Get a Custom Property Value + * + * @param string $propertyName + * @return string + */ + public function getCustomPropertyValue($propertyName) + { + if (isset($this->_customProperties[$propertyName])) { + return $this->_customProperties[$propertyName]['value']; + } + + } + + /** + * Get a Custom Property Type + * + * @param string $propertyName + * @return string + */ + public function getCustomPropertyType($propertyName) + { + if (isset($this->_customProperties[$propertyName])) { + return $this->_customProperties[$propertyName]['type']; + } + + } + + /** + * Set a Custom Property + * + * @param string $propertyName + * @param mixed $propertyValue + * @param string $propertyType + * 'i': Integer + * 'f': Floating Point + * 's': String + * 'd': Date/Time + * 'b': Boolean + * @return PHPExcel_DocumentProperties + */ + public function setCustomProperty($propertyName, $propertyValue = '', $propertyType = null) + { + if (($propertyType === null) || (!in_array($propertyType, array( + self::PROPERTY_TYPE_INTEGER, + self::PROPERTY_TYPE_FLOAT, + self::PROPERTY_TYPE_STRING, + self::PROPERTY_TYPE_DATE, + self::PROPERTY_TYPE_BOOLEAN + )))) { + if ($propertyValue === null) { + $propertyType = self::PROPERTY_TYPE_STRING; + } elseif (is_float($propertyValue)) { + $propertyType = self::PROPERTY_TYPE_FLOAT; + } elseif (is_int($propertyValue)) { + $propertyType = self::PROPERTY_TYPE_INTEGER; + } elseif (is_bool($propertyValue)) { + $propertyType = self::PROPERTY_TYPE_BOOLEAN; + } else { + $propertyType = self::PROPERTY_TYPE_STRING; + } + } + + $this->_customProperties[$propertyName] = array( + 'value' => $propertyValue, + 'type' => $propertyType + ); + return $this; + } + + /** + * Convert document propery based on type + * + * @param mixed $propertyValue + * @param string $propertyType + * @return mixed + */ + public static function convertProperty($propertyValue, $propertyType) + { + switch ($propertyType) { + case 'empty': // Empty + return ''; + break; + case 'null': // Null + return null; + break; + case 'i1': // 1-Byte Signed Integer + case 'i2': // 2-Byte Signed Integer + case 'i4': // 4-Byte Signed Integer + case 'i8': // 8-Byte Signed Integer + case 'int': // Integer + return (int) $propertyValue; + break; + case 'ui1': // 1-Byte Unsigned Integer + case 'ui2': // 2-Byte Unsigned Integer + case 'ui4': // 4-Byte Unsigned Integer + case 'ui8': // 8-Byte Unsigned Integer + case 'uint': // Unsigned Integer + return abs((int) $propertyValue); + break; + case 'r4': // 4-Byte Real Number + case 'r8': // 8-Byte Real Number + case 'decimal': // Decimal + return (float) $propertyValue; + break; + case 'lpstr': // LPSTR + case 'lpwstr': // LPWSTR + case 'bstr': // Basic String + return $propertyValue; + break; + case 'date': // Date and Time + case 'filetime': // File Time + return strtotime($propertyValue); + break; + case 'bool': // Boolean + return ($propertyValue == 'true') ? true : false; + break; + case 'cy': // Currency + case 'error': // Error Status Code + case 'vector': // Vector + case 'array': // Array + case 'blob': // Binary Blob + case 'oblob': // Binary Blob Object + case 'stream': // Binary Stream + case 'ostream': // Binary Stream Object + case 'storage': // Binary Storage + case 'ostorage': // Binary Storage Object + case 'vstream': // Binary Versioned Stream + case 'clsid': // Class ID + case 'cf': // Clipboard Data + return $propertyValue; + break; + } + + return $propertyValue; + } + + /** + * Convert document property type + * + * @param string $propertyType + * @return mixed + */ + public static function convertPropertyType($propertyType) + { + switch ($propertyType) { + case 'i1': // 1-Byte Signed Integer + case 'i2': // 2-Byte Signed Integer + case 'i4': // 4-Byte Signed Integer + case 'i8': // 8-Byte Signed Integer + case 'int': // Integer + case 'ui1': // 1-Byte Unsigned Integer + case 'ui2': // 2-Byte Unsigned Integer + case 'ui4': // 4-Byte Unsigned Integer + case 'ui8': // 8-Byte Unsigned Integer + case 'uint': // Unsigned Integer + return self::PROPERTY_TYPE_INTEGER; + break; + case 'r4': // 4-Byte Real Number + case 'r8': // 8-Byte Real Number + case 'decimal': // Decimal + return self::PROPERTY_TYPE_FLOAT; + break; + case 'empty': // Empty + case 'null': // Null + case 'lpstr': // LPSTR + case 'lpwstr': // LPWSTR + case 'bstr': // Basic String + return self::PROPERTY_TYPE_STRING; + break; + case 'date': // Date and Time + case 'filetime': // File Time + return self::PROPERTY_TYPE_DATE; + break; + case 'bool': // Boolean + return self::PROPERTY_TYPE_BOOLEAN; + break; + case 'cy': // Currency + case 'error': // Error Status Code + case 'vector': // Vector + case 'array': // Array + case 'blob': // Binary Blob + case 'oblob': // Binary Blob Object + case 'stream': // Binary Stream + case 'ostream': // Binary Stream Object + case 'storage': // Binary Storage + case 'ostorage': // Binary Storage Object + case 'vstream': // Binary Versioned Stream + case 'clsid': // Class ID + case 'cf': // Clipboard Data + return self::PROPERTY_TYPE_UNKNOWN; + break; + } + return self::PROPERTY_TYPE_UNKNOWN; + } +} diff --git a/Classes/PHPWord/Exception.php b/Classes/PHPWord/Exception.php index bd73d52c..91b5d54f 100755 --- a/Classes/PHPWord/Exception.php +++ b/Classes/PHPWord/Exception.php @@ -46,4 +46,4 @@ class PHPWord_Exception extends Exception $e->file = $file; throw $e; } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Exceptions/InvalidImageException.php b/Classes/PHPWord/Exceptions/InvalidImageException.php index 2d5337b4..eda84a8d 100644 --- a/Classes/PHPWord/Exceptions/InvalidImageException.php +++ b/Classes/PHPWord/Exceptions/InvalidImageException.php @@ -12,4 +12,4 @@ use Exception; */ class InvalidImageException extends Exception { -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Exceptions/InvalidStyleException.php b/Classes/PHPWord/Exceptions/InvalidStyleException.php index d624c62c..df436f3a 100644 --- a/Classes/PHPWord/Exceptions/InvalidStyleException.php +++ b/Classes/PHPWord/Exceptions/InvalidStyleException.php @@ -12,4 +12,4 @@ use InvalidArgumentException; */ class InvalidStyleException extends InvalidArgumentException { -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Exceptions/UnsupportedImageTypeException.php b/Classes/PHPWord/Exceptions/UnsupportedImageTypeException.php index 56bb310e..3a4975fe 100644 --- a/Classes/PHPWord/Exceptions/UnsupportedImageTypeException.php +++ b/Classes/PHPWord/Exceptions/UnsupportedImageTypeException.php @@ -12,4 +12,4 @@ use Exception; */ class UnsupportedImageTypeException extends Exception { -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Footnote.php b/Classes/PHPWord/Footnote.php index 81054843..81b64049 100644 --- a/Classes/PHPWord/Footnote.php +++ b/Classes/PHPWord/Footnote.php @@ -2,7 +2,7 @@ /** * PHPWord * - * Copyright (c) 2011 PHPWord + * Copyright (c) 2014 PHPWord * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -20,106 +20,108 @@ * * @category PHPWord * @package PHPWord - * @copyright Copyright (c) 010 PHPWord + * @copyright Copyright (c) 2014 PHPWord * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL - * @version Beta 0.6.3, 08.07.2011 + * @version 0.7.0 */ /** * PHPWord_Footnote - * - * @category PHPWord - * @package PHPWord - * @copyright Copyright (c) 2011 PHPWord */ -class PHPWord_Footnote { +class PHPWord_Footnote +{ - /** - * Footnote Elements - * - * @var array - */ - private static $_footnoteCollection = array(); + /** + * Footnote Elements + * + * @var array + */ + private static $_footnoteCollection = array(); - /** - * Footnote Link Elements - * - * @var array - */ - private static $_footnoteLink = array(); + /** + * Footnote Link Elements + * + * @var array + */ + private static $_footnoteLink = array(); - /** - * Add new Footnote Element - * - * @param string $linkSrc - * @param string $linkName - * - * @return mixed - */ - public static function addFootnoteElement(PHPWord_Section_Footnote $footnote) { - $refID = self::countFootnoteElements() + 2; + /** + * Add new Footnote Element + * + * @param string $linkSrc + * @param string $linkName + * + * @return mixed + */ + public static function addFootnoteElement(PHPWord_Section_Footnote $footnote) + { + $refID = self::countFootnoteElements() + 2; - self::$_footnoteCollection[] = $footnote; + self::$_footnoteCollection[] = $footnote; - return $refID; - } + return $refID; + } - /** - * Get Footnote Elements - * - * @return array - */ - public static function getFootnoteElements() { - return self::$_footnoteCollection; - } + /** + * Get Footnote Elements + * + * @return array + */ + public static function getFootnoteElements() + { + return self::$_footnoteCollection; + } - /** - * Get Footnote Elements Count - * - * @return int - */ - public static function countFootnoteElements() { - return count(self::$_footnoteCollection); - } + /** + * Get Footnote Elements Count + * + * @return int + */ + public static function countFootnoteElements() + { + return count(self::$_footnoteCollection); + } - /** - * Add new Footnote Link Element - * - * @param string $src - * @param string $type - * - * @return mixed - */ - public static function addFootnoteLinkElement($linkSrc) { - $rID = self::countFootnoteLinkElements() + 1; + /** + * Add new Footnote Link Element + * + * @param string $src + * @param string $type + * + * @return mixed + */ + public static function addFootnoteLinkElement($linkSrc) + { + $rID = self::countFootnoteLinkElements() + 1; - $link = array(); - $link['target'] = $linkSrc; - $link['rID'] = $rID; - $link['type'] = 'hyperlink'; + $link = array(); + $link['target'] = $linkSrc; + $link['rID'] = $rID; + $link['type'] = 'hyperlink'; - self::$_footnoteLink[] = $link; + self::$_footnoteLink[] = $link; - return $rID; - } + return $rID; + } - /** - * Get Footnote Link Elements - * - * @return array - */ - public static function getFootnoteLinkElements() { - return self::$_footnoteLink; - } + /** + * Get Footnote Link Elements + * + * @return array + */ + public static function getFootnoteLinkElements() + { + return self::$_footnoteLink; + } - /** - * Get Footnote Link Elements Count - * - * @return int - */ - public static function countFootnoteLinkElements() { - return count(self::$_footnoteLink); - } - -} \ No newline at end of file + /** + * Get Footnote Link Elements Count + * + * @return int + */ + public static function countFootnoteLinkElements() + { + return count(self::$_footnoteLink); + } +} diff --git a/Classes/PHPWord/HashTable.php b/Classes/PHPWord/HashTable.php index 5b6fbd04..f2ef3148 100755 --- a/Classes/PHPWord/HashTable.php +++ b/Classes/PHPWord/HashTable.php @@ -69,7 +69,7 @@ class PHPWord_HashTable // Check if an array was passed if ($pSource == null) { return; - } else if (!is_array($pSource)) { + } elseif (!is_array($pSource)) { throw new Exception('Invalid array parameter passed.'); } @@ -91,7 +91,7 @@ class PHPWord_HashTable $hashIndex = $pSource->getHashIndex(); if (is_null($hashIndex)) { $hashCode = $pSource->getHashCode(); - } else if (isset ($this->_keyMap[$hashIndex])) { + } elseif (isset ($this->_keyMap[$hashIndex])) { $hashCode = $this->_keyMap[$hashIndex]; } else { $hashCode = $pSource->getHashCode(); diff --git a/Classes/PHPWord/IOFactory.php b/Classes/PHPWord/IOFactory.php index a9eae570..aac0be87 100755 --- a/Classes/PHPWord/IOFactory.php +++ b/Classes/PHPWord/IOFactory.php @@ -37,7 +37,8 @@ class PHPWord_IOFactory * @var array */ private static $_searchLocations = array( - array('type' => 'IWriter', 'path' => 'PHPWord/Writer/{0}.php', 'class' => 'PHPWord_Writer_{0}') + array('type' => 'IWriter', 'path' => 'PHPWord/Writer/{0}.php', 'class' => 'PHPWord_Writer_{0}'), + array('type' => 'IReader', 'path' => 'PHPWord/Reader/{0}.php', 'class' => 'PHPWord_Reader_{0}' ), ); /** @@ -118,4 +119,40 @@ class PHPWord_IOFactory throw new Exception("No $searchType found for type $writerType"); } + + /** + * Create PHPWord_Reader_IReader + * + * @param string $readerType Example: Word2007 + * @return PHPWord_Reader_IReader + */ + public static function createReader($readerType = '') + { + $searchType = 'IReader'; + + foreach (self::$_searchLocations as $searchLocation) { + if ($searchLocation['type'] == $searchType) { + $className = str_replace('{0}', $readerType, $searchLocation['class']); + + $instance = new $className(); + if ($instance !== null) { + return $instance; + } + } + } + + throw new PHPWord_Exception("No $searchType found for type $readerType"); + } + + /** + * Loads PHPWord from file + * + * @param string $pFilename The name of the file + * @return PHPWord + */ + public static function load($pFilename, $readerType = 'Word2007') + { + $reader = self::createReader($readerType); + return $reader->load($pFilename); + } } diff --git a/Classes/PHPWord/Media.php b/Classes/PHPWord/Media.php index de913719..28d3d033 100755 --- a/Classes/PHPWord/Media.php +++ b/Classes/PHPWord/Media.php @@ -30,15 +30,16 @@ */ class PHPWord_Media { - /** * Section Media Elements * * @var array */ - private static $_sectionMedia = array('images' => array(), + private static $_sectionMedia = array( + 'images' => array(), 'embeddings' => array(), - 'links' => array()); + 'links' => array() + ); /** * Header Media Elements @@ -61,18 +62,18 @@ class PHPWord_Media */ private static $_objectId = 1325353440; - /** * Add new Section Media Element * * @param string $src * @param string $type + * @param PHPWord_Section_MemoryImage|null $memoryImage * @return mixed */ public static function addSectionMediaElement($src, $type, PHPWord_Section_MemoryImage $memoryImage = null) { $mediaId = md5($src); - $key = ($type == 'image') ? 'images' : 'embeddings'; + $key = ($type === 'image') ? 'images' : 'embeddings'; if (!array_key_exists($mediaId, self::$_sectionMedia[$key])) { $cImg = self::countSectionMediaElements('images'); @@ -81,26 +82,39 @@ class PHPWord_Media $media = array(); - if ($type == 'image') { + $folder = null; + $file = null; + if ($type === 'image') { $cImg++; - $inf = pathinfo($src); - $isMemImage = (substr(strtolower($inf['extension']), 0, 3) == 'php' && $type == 'image') ? true : false; + $isMemImage = false; + if (stripos(strrev($src), strrev('.php')) === 0) { + $isMemImage = true; + } + $extension = ''; if ($isMemImage) { - $ext = $memoryImage->getImageExtension(); + $extension = $memoryImage->getImageExtension(); $media['isMemImage'] = true; $media['createfunction'] = $memoryImage->getImageCreateFunction(); $media['imagefunction'] = $memoryImage->getImageFunction(); } else { - $ext = $inf['extension']; - if ($ext == 'jpeg') { // Office crashes when adding a jpEg Image, so rename to jpg - $ext = 'jpg'; + $imageType = exif_imagetype($src); + if ($imageType === IMAGETYPE_JPEG) { + $extension = 'jpg'; + } elseif ($imageType === IMAGETYPE_GIF) { + $extension = 'gif'; + } elseif ($imageType === IMAGETYPE_PNG) { + $extension = 'png'; + } elseif ($imageType === IMAGETYPE_BMP) { + $extension = 'bmp'; + } elseif ($imageType === IMAGETYPE_TIFF_II || $imageType === IMAGETYPE_TIFF_MM) { + $extension = 'tif'; } } $folder = 'media'; - $file = $type . $cImg . '.' . strtolower($ext); - } elseif ($type == 'oleObject') { + $file = $type . $cImg . '.' . strtolower($extension); + } elseif ($type === 'oleObject') { $cObj++; $folder = 'embedding'; $file = $type . $cObj . '.bin'; @@ -113,27 +127,24 @@ class PHPWord_Media self::$_sectionMedia[$key][$mediaId] = $media; - if ($type == 'oleObject') { + if ($type === 'oleObject') { return array($rID, ++self::$_objectId); - } else { - return $rID; - } - } else { - if ($type == 'oleObject') { - $rID = self::$_sectionMedia[$key][$mediaId]['rID']; - return array($rID, ++self::$_objectId); - } else { - return self::$_sectionMedia[$key][$mediaId]['rID']; } + + return $rID; } + + if ($type === 'oleObject') { + $rID = self::$_sectionMedia[$key][$mediaId]['rID']; + return array($rID, ++self::$_objectId); + } + return self::$_sectionMedia[$key][$mediaId]['rID']; } /** * Add new Section Link Element * * @param string $linkSrc - * @param string $linkName - * * @return mixed */ public static function addSectionLinkElement($linkSrc) @@ -160,12 +171,12 @@ class PHPWord_Media { if (!is_null($key)) { return self::$_sectionMedia[$key]; - } else { - $arrImages = self::$_sectionMedia['images']; - $arrObjects = self::$_sectionMedia['embeddings']; - $arrLinks = self::$_sectionMedia['links']; - return array_merge($arrImages, $arrObjects, $arrLinks); } + + $arrImages = self::$_sectionMedia['images']; + $arrObjects = self::$_sectionMedia['embeddings']; + $arrLinks = self::$_sectionMedia['links']; + return array_merge($arrImages, $arrObjects, $arrLinks); } /** @@ -178,12 +189,12 @@ class PHPWord_Media { if (!is_null($key)) { return count(self::$_sectionMedia[$key]); - } else { - $cImages = count(self::$_sectionMedia['images']); - $cObjects = count(self::$_sectionMedia['embeddings']); - $cLinks = count(self::$_sectionMedia['links']); - return ($cImages + $cObjects + $cLinks); } + + $cImages = count(self::$_sectionMedia['images']); + $cObjects = count(self::$_sectionMedia['embeddings']); + $cLinks = count(self::$_sectionMedia['links']); + return ($cImages + $cObjects + $cLinks); } /** @@ -191,6 +202,7 @@ class PHPWord_Media * * @param int $headerCount * @param string $src + * @param PHPWord_Section_MemoryImage|null $memoryImage * @return int */ public static function addHeaderMediaElement($headerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null) @@ -232,9 +244,8 @@ class PHPWord_Media self::$_headerMedia[$key][$mediaId] = $media; return $rID; - } else { - return self::$_headerMedia[$key][$mediaId]['rID']; } + return self::$_headerMedia[$key][$mediaId]['rID']; } /** @@ -263,6 +274,7 @@ class PHPWord_Media * * @param int $footerCount * @param string $src + * @param PHPWord_Section_MemoryImage|null $memoryImage * @return int */ public static function addFooterMediaElement($footerCount, $src, PHPWord_Section_MemoryImage $memoryImage = null) @@ -304,9 +316,8 @@ class PHPWord_Media self::$_footerMedia[$key][$mediaId] = $media; return $rID; - } else { - return self::$_footerMedia[$key][$mediaId]['rID']; } + return self::$_footerMedia[$key][$mediaId]['rID']; } /** @@ -330,4 +341,3 @@ class PHPWord_Media return self::$_footerMedia; } } - diff --git a/Classes/PHPWord/Reader/Abstract.php b/Classes/PHPWord/Reader/Abstract.php new file mode 100644 index 00000000..1e7acb51 --- /dev/null +++ b/Classes/PHPWord/Reader/Abstract.php @@ -0,0 +1,105 @@ +readDataOnly; + return true; + } + + /** + * Set read data only + * + * @param boolean $pValue + * @return PHPWord_Reader_IReader + */ + public function setReadDataOnly($pValue = true) + { + $this->readDataOnly = $pValue; + return $this; + } + + /** + * Open file for reading + * + * @param string $pFilename + * @throws PHPWord_Exception + * @return resource + */ + protected function openFile($pFilename) + { + // Check if file exists + if (!file_exists($pFilename) || !is_readable($pFilename)) { + throw new PHPWord_Exception("Could not open " . $pFilename . " for reading! File does not exist."); + } + + // Open file + $this->fileHandle = fopen($pFilename, 'r'); + if ($this->fileHandle === false) { + throw new PHPWord_Exception("Could not open file " . $pFilename . " for reading."); + } + } + + /** + * Can the current PHPWord_Reader_IReader read the file? + * + * @param string $pFilename + * @return boolean + * @throws PHPWord_Exception + */ + public function canRead($pFilename) + { + // Check if file exists + try { + $this->openFile($pFilename); + } catch (Exception $e) { + return false; + } + fclose($this->fileHandle); + return $readable; + } +} diff --git a/Classes/PHPWord/Reader/IReader.php b/Classes/PHPWord/Reader/IReader.php new file mode 100644 index 00000000..f51eed77 --- /dev/null +++ b/Classes/PHPWord/Reader/IReader.php @@ -0,0 +1,47 @@ +open($pFilename) === true) { + // check if it is an OOXML archive + $rels = simplexml_load_string($this->getFromZipArchive($zip, "_rels/.rels")); + if ($rels !== false) { + foreach ($rels->Relationship as $rel) { + switch ($rel["Type"]) { + case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument": + if (basename($rel["Target"]) == 'document.xml') { + $return = true; + } + break; + + } + } + } + $zip->close(); + } + + return $return; + } + + /** + * Get from zip archive + * + * @param ZipArchive $archive + * @param string $fileName + * @param bool $removeNamespace + */ + public function getFromZipArchive( + $archive, + $fileName = '', + $removeNamespace = false + ) { + // Root-relative paths + if (strpos($fileName, '//') !== false) { + $fileName = substr($fileName, strpos($fileName, '//') + 1); + } + $fileName = PHPWord_Shared_File::realpath($fileName); + + // Apache POI fixes + $contents = $archive->getFromName($fileName); + if ($contents === false) { + $contents = $archive->getFromName(substr($fileName, 1)); + } + + // Remove namespaces from elements and attributes name + if ($removeNamespace) { + $contents = preg_replace('~(canRead($pFilename)) { + return; + } + + // Initialisations + $word = new PHPWord; + $zip = new ZipArchive; + $zip->open($pFilename); + + // Read properties and documents + $rels = simplexml_load_string($this->getFromZipArchive($zip, "_rels/.rels")); + foreach ($rels->Relationship as $rel) { + switch ($rel["Type"]) { + // Core properties + case "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties": + $xmlCore = simplexml_load_string($this->getFromZipArchive($zip, "{$rel['Target']}")); + if (is_object($xmlCore)) { + $xmlCore->registerXPathNamespace("dc", "http://purl.org/dc/elements/1.1/"); + $xmlCore->registerXPathNamespace("dcterms", "http://purl.org/dc/terms/"); + $xmlCore->registerXPathNamespace("cp", "http://schemas.openxmlformats.org/package/2006/metadata/core-properties"); + $docProps = $word->getProperties(); + $docProps->setCreator((string) self::arrayItem($xmlCore->xpath("dc:creator"))); + $docProps->setLastModifiedBy((string) self::arrayItem($xmlCore->xpath("cp:lastModifiedBy"))); + $docProps->setCreated(strtotime(self::arrayItem($xmlCore->xpath("dcterms:created")))); + $docProps->setModified(strtotime(self::arrayItem($xmlCore->xpath("dcterms:modified")))); + $docProps->setTitle((string) self::arrayItem($xmlCore->xpath("dc:title"))); + $docProps->setDescription((string) self::arrayItem($xmlCore->xpath("dc:description"))); + $docProps->setSubject((string) self::arrayItem($xmlCore->xpath("dc:subject"))); + $docProps->setKeywords((string) self::arrayItem($xmlCore->xpath("cp:keywords"))); + $docProps->setCategory((string) self::arrayItem($xmlCore->xpath("cp:category"))); + } + break; + // Extended properties + case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties": + $xmlCore = simplexml_load_string($this->getFromZipArchive($zip, "{$rel['Target']}")); + if (is_object($xmlCore)) { + $docProps = $word->getProperties(); + if (isset($xmlCore->Company)) { + $docProps->setCompany((string) $xmlCore->Company); + } + if (isset($xmlCore->Manager)) { + $docProps->setManager((string) $xmlCore->Manager); + } + } + break; + // Custom properties + case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties": + $xmlCore = simplexml_load_string($this->getFromZipArchive($zip, "{$rel['Target']}")); + if (is_object($xmlCore)) { + $docProps = $word->getProperties(); + foreach ($xmlCore as $xmlProperty) { + $cellDataOfficeAttributes = $xmlProperty->attributes(); + if (isset($cellDataOfficeAttributes['name'])) { + $propertyName = (string) $cellDataOfficeAttributes['name']; + $cellDataOfficeChildren = $xmlProperty->children("http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"); + $attributeType = $cellDataOfficeChildren->getName(); + $attributeValue = (string) $cellDataOfficeChildren->{$attributeType}; + $attributeValue = PHPWord_DocumentProperties::convertProperty($attributeValue, $attributeType); + $attributeType = PHPWord_DocumentProperties::convertPropertyType($attributeType); + $docProps->setCustomProperty($propertyName, $attributeValue, $attributeType); + } + } + } + break; + // Document + case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument": + $dir = dirname($rel["Target"]); + $archive = "$dir/_rels/" . basename($rel["Target"]) . ".rels"; + $relsDoc = simplexml_load_string($this->getFromZipArchive($zip, $archive)); + $relsDoc->registerXPathNamespace("rel", "http://schemas.openxmlformats.org/package/2006/relationships"); + $xpath = self::arrayItem( + $relsDoc->xpath("rel:Relationship[@Type='http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles']") + ); + $xmlDoc = simplexml_load_string($this->getFromZipArchive($zip, "{$rel['Target']}", true)); + if (is_object($xmlDoc)) { + $section = $word->createSection(); + + foreach ($xmlDoc->body->children() as $elm) { + $elmName = $elm->getName(); + if ($elmName == 'p') { // Paragraph/section + // Create new section if section setting found + if ($elm->pPr->sectPr) { + $section->setSettings($this->loadSectionSettings($elm->pPr)); + $section = $word->createSection(); + continue; + } + // Has w:r? It's either text or textrun + if ($elm->r) { + // w:r = 1? It's a plain paragraph + if (count($elm->r) == 1) { + $section->addText( + $elm->r->t, + $this->loadFontStyle($elm->r) + ); + // w:r more than 1? It's a textrun + } else { + $textRun = $section->createTextRun(); + foreach ($elm->r as $r) { + $textRun->addText( + $r->t, + $this->loadFontStyle($r) + ); + } + } + // No, it's a textbreak + } else { + $section->addTextBreak(); + } + } elseif ($elmName == 'sectPr') { + // Last section setting + $section->setSettings($this->loadSectionSettings($xmlDoc->body)); + } + } + } + break; + } + } + + // Read styles + $docRels = simplexml_load_string($this->getFromZipArchive($zip, "word/_rels/document.xml.rels")); + foreach ($docRels->Relationship as $rel) { + switch ($rel["Type"]) { + case "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles": + $xmlStyle = simplexml_load_string($this->getFromZipArchive($zip, "word/{$rel['Target']}", true)); + if (is_object($xmlStyle)) { + foreach ($xmlStyle->children() as $elm) { + if ($elm->getName() != 'style') { + continue; + } + unset($pStyle); + unset($fStyle); + $hasParagraphStyle = isset($elm->pPr); + $hasFontStyle = isset($elm->rPr); + $styleName = (string)$elm->name['val']; + if ($hasParagraphStyle) { + $pStyle = $this->loadParagraphStyle($elm); + if (!$hasFontStyle) { + $word->addParagraphStyle($styleName, $pStyle); + } + } + if ($hasFontStyle) { + $fStyle = $this->loadFontStyle($elm); + $word->addFontStyle($styleName, $fStyle, $pStyle); + } + } + } + break; + } + } + $zip->close(); + + return $word; + } + + /** + * Load section settings from SimpleXMLElement + * + * @param SimpleXMLElement $elm + * @return array|string|null + * + * @todo Implement gutter + */ + private function loadSectionSettings($elm) + { + if ($xml = $elm->sectPr) { + $setting = array(); + if ($xml->type) { + $setting['breakType'] = (string)$xml->type['val']; + } + if ($xml->pgSz) { + if (isset($xml->pgSz['w'])) { + $setting['pageSizeW'] = (int)$xml->pgSz['w']; + } + if (isset($xml->pgSz['h'])) { + $setting['pageSizeH'] = (int)$xml->pgSz['h']; + } + if (isset($xml->pgSz['orient'])) { + $setting['orientation'] = (string)$xml->pgSz['orient']; + } + } + if ($xml->pgMar) { + if (isset($xml->pgMar['top'])) { + $setting['topMargin'] = (int)$xml->pgMar['top']; + } + if (isset($xml->pgMar['left'])) { + $setting['leftMargin'] = (int)$xml->pgMar['left']; + } + if (isset($xml->pgMar['bottom'])) { + $setting['bottomMargin'] = (int)$xml->pgMar['bottom']; + } + if (isset($xml->pgMar['right'])) { + $setting['rightMargin'] = (int)$xml->pgMar['right']; + } + if (isset($xml->pgMar['header'])) { + $setting['headerHeight'] = (int)$xml->pgMar['header']; + } + if (isset($xml->pgMar['footer'])) { + $setting['footerHeight'] = (int)$xml->pgMar['footer']; + } + if (isset($xml->pgMar['gutter'])) { + // $setting['gutter'] = (int)$xml->pgMar['gutter']; + } + } + if ($xml->cols) { + if (isset($xml->cols['num'])) { + $setting['colsNum'] = (int)$xml->cols['num']; + } + if (isset($xml->cols['space'])) { + $setting['colsSpace'] = (int)$xml->cols['space']; + } + } + return $setting; + } else { + return null; + } + } + + /** + * Load paragraph style from SimpleXMLElement + * + * @param SimpleXMLElement $elm + * @return array|string|null + */ + private function loadParagraphStyle($elm) + { + if ($xml = $elm->pPr) { + if ($xml->pStyle) { + return (string)$xml->pStyle['val']; + } + $style = array(); + if ($xml->jc) { + $style['align'] = (string)$xml->jc['val']; + } + if ($xml->ind) { + if (isset($xml->ind->left)) { + $style['indent'] = (int)$xml->ind->left; + } + if (isset($xml->ind->hanging)) { + $style['hanging'] = (int)$xml->ind->hanging; + } + if (isset($xml->ind->line)) { + $style['spacing'] = (int)$xml->ind->line; + } + } + if ($xml->spacing) { + if (isset($xml->spacing['after'])) { + $style['spaceAfter'] = (int)$xml->spacing['after']; + } + if (isset($xml->spacing['before'])) { + $style['spaceBefore'] = (int)$xml->spacing['before']; + } + if (isset($xml->spacing['line'])) { + $style['spacing'] = (int)$xml->spacing['line']; + } + } + if ($xml->basedOn) { + $style['basedOn'] = (string)$xml->basedOn['val']; + } + if ($xml->next) { + $style['next'] = (string)$xml->next['val']; + } + if ($xml->widowControl) { + $style['widowControl'] = false; + } + if ($xml->keepNext) { + $style['keepNext'] = true; + } + if ($xml->keepLines) { + $style['keepLines'] = true; + } + if ($xml->pageBreakBefore) { + $style['pageBreakBefore'] = true; + } + return $style; + } else { + return null; + } + } + + /** + * Load font style from SimpleXMLElement + * + * @param SimpleXMLElement $elm + * @return array|string|null + */ + private function loadFontStyle($elm) + { + if ($xml = $elm->rPr) { + if ($xml->rStyle) { + return (string)$xml->rStyle['val']; + } + $style = array(); + if ($xml->rFonts) { + $style['name'] = (string)$xml->rFonts['ascii']; + } + if ($xml->sz) { + $style['size'] = (int)$xml->sz['val'] / 2; + } + if ($xml->color) { + $style['color'] = (string)$xml->color['val']; + } + if ($xml->b) { + $style['bold'] = true; + } + if ($xml->i) { + $style['italic'] = true; + } + if ($xml->u) { + $style['underline'] = (string)$xml->u['val']; + } + if ($xml->strike) { + $style['strikethrough'] = true; + } + if ($xml->highlight) { + $style['fgColor'] = (string)$xml->highlight['val']; + } + if ($xml->vertAlign) { + if ($xml->vertAlign['val'] == 'superscript') { + $style['superScript'] = true; + } else { + $style['subScript'] = true; + } + } + return $style; + } else { + return null; + } + } + + /** + * Get array item + * + * @param array $array + * @param mixed $key + * @return mixed|null + */ + private static function arrayItem($array, $key = 0) + { + return (isset($array[$key]) ? $array[$key] : null); + } +} diff --git a/Classes/PHPWord/Section.php b/Classes/PHPWord/Section.php index c50e2d0c..70dceba2 100755 --- a/Classes/PHPWord/Section.php +++ b/Classes/PHPWord/Section.php @@ -77,7 +77,16 @@ class PHPWord_Section { $this->_sectionCount = $sectionCount; $this->_settings = new PHPWord_Section_Settings(); + $this->setSettings($settings); + } + /** + * Set Section Settings + * + * @param array $settings + */ + public function setSettings($settings = null) + { if (!is_null($settings) && is_array($settings)) { foreach ($settings as $key => $value) { if (substr($key, 0, 1) != '_') { @@ -416,11 +425,12 @@ class PHPWord_Section * @param string $text * @return PHPWord_Section_Footnote */ - public function createFootnote($styleParagraph = null) { - $footnote = new PHPWord_Section_Footnote($styleParagraph); - $refID = PHPWord_Footnote::addFootnoteElement($footnote); - $footnote->setReferenceId($refID); - $this->_elementCollection[] = $footnote; - return $footnote; + public function createFootnote($styleParagraph = null) + { + $footnote = new PHPWord_Section_Footnote($styleParagraph); + $refID = PHPWord_Footnote::addFootnoteElement($footnote); + $footnote->setReferenceId($refID); + $this->_elementCollection[] = $footnote; + return $footnote; } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Section/Footer.php b/Classes/PHPWord/Section/Footer.php index 56e3e95a..04d40f33 100755 --- a/Classes/PHPWord/Section/Footer.php +++ b/Classes/PHPWord/Section/Footer.php @@ -210,4 +210,4 @@ class PHPWord_Section_Footer { return $this->_footerCount; } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Section/Footnote.php b/Classes/PHPWord/Section/Footnote.php index b02ee4ef..7b2159e4 100644 --- a/Classes/PHPWord/Section/Footnote.php +++ b/Classes/PHPWord/Section/Footnote.php @@ -2,7 +2,7 @@ /** * PHPWord * - * Copyright (c) 2011 PHPWord + * Copyright (c) 2014 PHPWord * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -11,138 +11,141 @@ * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - * @category PHPWord - * @package PHPWord - * @copyright Copyright (c) 010 PHPWord - * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL - * @version Beta 0.6.3, 08.07.2011 + * @category PHPWord + * @package PHPWord + * @copyright Copyright (c) 2014 PHPWord + * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL + * @version 0.7.0 */ - /** * PHPWord_Section_Footnote - * - * @category PHPWord - * @package PHPWord_Section - * @copyright Copyright (c) 2011 PHPWord */ -class PHPWord_Section_Footnote { +class PHPWord_Section_Footnote +{ - /** - * Paragraph style - * - * @var PHPWord_Style_Font - */ - private $_styleParagraph; + /** + * Paragraph style + * + * @var PHPWord_Style_Font + */ + private $_styleParagraph; - /** - * Footnote Reference ID - * - * @var string - */ - private $_refId; + /** + * Footnote Reference ID + * + * @var string + */ + private $_refId; - /** - * Text collection - * - * @var array - */ - private $_elementCollection; + /** + * Text collection + * + * @var array + */ + private $_elementCollection; - /** - * Create a new Footnote Element - */ - public function __construct($styleParagraph = null) { - $this->_elementCollection = array(); + /** + * Create a new Footnote Element + */ + public function __construct($styleParagraph = null) + { + $this->_elementCollection = array(); - // Set paragraph style - if(is_array($styleParagraph)) { - $this->_styleParagraph = new PHPWord_Style_Paragraph(); + // Set paragraph style + if (is_array($styleParagraph)) { + $this->_styleParagraph = new PHPWord_Style_Paragraph(); - foreach($styleParagraph as $key => $value) { - if(substr($key, 0, 1) != '_') { - $key = '_'.$key; + foreach ($styleParagraph as $key => $value) { + if (substr($key, 0, 1) != '_') { + $key = '_' . $key; + } + $this->_styleParagraph->setStyleValue($key, $value); + } + } else { + $this->_styleParagraph = $styleParagraph; } - $this->_styleParagraph->setStyleValue($key, $value); - } - } else { - $this->_styleParagraph = $styleParagraph; } - } - /** - * Add a Text Element - * - * @var string $text - * @var mixed $styleFont - * @return PHPWord_Section_Text - */ - public function addText($text = null, $styleFont = null) { - $givenText = $text; - $text = new PHPWord_Section_Text($givenText, $styleFont); - $this->_elementCollection[] = $text; - return $text; - } + /** + * Add a Text Element + * + * @var string $text + * @var mixed $styleFont + * @return PHPWord_Section_Text + */ + public function addText($text = null, $styleFont = null) + { + $givenText = $text; + $text = new PHPWord_Section_Text($givenText, $styleFont); + $this->_elementCollection[] = $text; + return $text; + } - /** - * Add a Link Element - * - * @param string $linkSrc - * @param string $linkName - * @param mixed $styleFont - * @return PHPWord_Section_Link - */ - public function addLink($linkSrc, $linkName = null, $styleFont = null) { + /** + * Add a Link Element + * + * @param string $linkSrc + * @param string $linkName + * @param mixed $styleFont + * @return PHPWord_Section_Link + */ + public function addLink($linkSrc, $linkName = null, $styleFont = null) + { - $link = new PHPWord_Section_Link($linkSrc, $linkName, $styleFont); - $rID = PHPWord_Footnote::addFootnoteLinkElement($linkSrc); - $link->setRelationId($rID); + $link = new PHPWord_Section_Link($linkSrc, $linkName, $styleFont); + $rID = PHPWord_Footnote::addFootnoteLinkElement($linkSrc); + $link->setRelationId($rID); - $this->_elementCollection[] = $link; - return $link; - } + $this->_elementCollection[] = $link; + return $link; + } - /** - * Get Footnote content - * - * @return array - */ - public function getElements() { - return $this->_elementCollection; - } + /** + * Get Footnote content + * + * @return array + */ + public function getElements() + { + return $this->_elementCollection; + } - /** - * Get Paragraph style - * - * @return PHPWord_Style_Paragraph - */ - public function getParagraphStyle() { - return $this->_styleParagraph; - } + /** + * Get Paragraph style + * + * @return PHPWord_Style_Paragraph + */ + public function getParagraphStyle() + { + return $this->_styleParagraph; + } - /** - * Get Footnote Reference ID - * - * @return int - */ - public function getReferenceId() { - return $this->_refId; - } + /** + * Get Footnote Reference ID + * + * @return int + */ + public function getReferenceId() + { + return $this->_refId; + } - /** - * Set Footnote Reference ID - * - * @param int $refId - */ - public function setReferenceId($refId) { - $this->_refId = $refId; - } -} \ No newline at end of file + /** + * Set Footnote Reference ID + * + * @param int $refId + */ + public function setReferenceId($refId) + { + $this->_refId = $refId; + } +} diff --git a/Classes/PHPWord/Section/Header.php b/Classes/PHPWord/Section/Header.php index cb91e085..6c624406 100755 --- a/Classes/PHPWord/Section/Header.php +++ b/Classes/PHPWord/Section/Header.php @@ -292,5 +292,4 @@ class PHPWord_Section_Header { return $this->_type = PHPWord_Section_Header::EVEN; } - -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Section/Image.php b/Classes/PHPWord/Section/Image.php index 6c722a98..a8fcde46 100755 --- a/Classes/PHPWord/Section/Image.php +++ b/Classes/PHPWord/Section/Image.php @@ -175,4 +175,4 @@ class PHPWord_Section_Image { $this->_isWatermark = $pValue; } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Section/MemoryImage.php b/Classes/PHPWord/Section/MemoryImage.php index 9652955c..8486a2a3 100755 --- a/Classes/PHPWord/Section/MemoryImage.php +++ b/Classes/PHPWord/Section/MemoryImage.php @@ -30,7 +30,6 @@ */ class PHPWord_Section_MemoryImage { - /** * Image Src * @@ -85,7 +84,7 @@ class PHPWord_Section_MemoryImage * Create a new Image * * @param string $src - * @param mixed style + * @param mixed $style */ public function __construct($src, $style = null) { @@ -113,10 +112,6 @@ class PHPWord_Section_MemoryImage } $this->_setFunctions(); - - return $this; - } else { - return false; } } @@ -145,7 +140,6 @@ class PHPWord_Section_MemoryImage } } - /** * Get Image style * @@ -235,4 +229,4 @@ class PHPWord_Section_MemoryImage { return $this->_imageExtension; } -} +} \ No newline at end of file diff --git a/Classes/PHPWord/Section/Settings.php b/Classes/PHPWord/Section/Settings.php index 67d01de4..38af6410 100755 --- a/Classes/PHPWord/Section/Settings.php +++ b/Classes/PHPWord/Section/Settings.php @@ -616,7 +616,8 @@ class PHPWord_Section_Settings * * @return int */ - public function getHeaderHeight() { + public function getHeaderHeight() + { return $this->headerHeight; } @@ -625,7 +626,8 @@ class PHPWord_Section_Settings * * @param int $pValue */ - public function setHeaderHeight($pValue = '') { + public function setHeaderHeight($pValue = '') + { if (!is_numeric($pValue)) { $pValue = 720; } @@ -638,7 +640,8 @@ class PHPWord_Section_Settings * * @return int */ - public function getFooterHeight() { + public function getFooterHeight() + { return $this->footerHeight; } @@ -647,7 +650,8 @@ class PHPWord_Section_Settings * * @param int $pValue */ - public function setFooterHeight($pValue = '') { + public function setFooterHeight($pValue = '') + { if (!is_numeric($pValue)) { $pValue = 720; } @@ -660,7 +664,8 @@ class PHPWord_Section_Settings * * @param int $pValue */ - public function setColsNum($pValue = '') { + public function setColsNum($pValue = '') + { if (!is_numeric($pValue)) { $pValue = 1; } @@ -673,7 +678,8 @@ class PHPWord_Section_Settings * * @return int */ - public function getColsNum() { + public function getColsNum() + { return $this->_colsNum; } @@ -682,7 +688,8 @@ class PHPWord_Section_Settings * * @param int $pValue */ - public function setColsSpace($pValue = '') { + public function setColsSpace($pValue = '') + { if (!is_numeric($pValue)) { $pValue = 720; } @@ -695,7 +702,8 @@ class PHPWord_Section_Settings * * @return int */ - public function getColsSpace() { + public function getColsSpace() + { return $this->_colsSpace; } @@ -704,7 +712,8 @@ class PHPWord_Section_Settings * * @param string $pValue */ - public function setBreakType($pValue = null) { + public function setBreakType($pValue = null) + { $this->_breakType = $pValue; return $this; } @@ -714,8 +723,8 @@ class PHPWord_Section_Settings * * @return string */ - public function getBreakType() { + public function getBreakType() + { return $this->_breakType; } - } diff --git a/Classes/PHPWord/Section/Table.php b/Classes/PHPWord/Section/Table.php index 081b6484..dd93c95e 100755 --- a/Classes/PHPWord/Section/Table.php +++ b/Classes/PHPWord/Section/Table.php @@ -160,5 +160,4 @@ class PHPWord_Section_Table { return $this->_width; } - } diff --git a/Classes/PHPWord/Section/Table/Cell.php b/Classes/PHPWord/Section/Table/Cell.php index b4bc8242..4f8f78fe 100755 --- a/Classes/PHPWord/Section/Table/Cell.php +++ b/Classes/PHPWord/Section/Table/Cell.php @@ -333,4 +333,4 @@ class PHPWord_Section_Table_Cell { return $this->_width; } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Section/Table/Row.php b/Classes/PHPWord/Section/Table/Row.php index c2db614b..d174ef8f 100644 --- a/Classes/PHPWord/Section/Table/Row.php +++ b/Classes/PHPWord/Section/Table/Row.php @@ -138,4 +138,4 @@ class PHPWord_Section_Table_Row { return $this->_height; } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Section/Text.php b/Classes/PHPWord/Section/Text.php index 5d628d2c..296084e6 100755 --- a/Classes/PHPWord/Section/Text.php +++ b/Classes/PHPWord/Section/Text.php @@ -149,4 +149,4 @@ class PHPWord_Section_Text { return $this->text; } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Section/TextRun.php b/Classes/PHPWord/Section/TextRun.php index 7c03138b..c26b2ef6 100755 --- a/Classes/PHPWord/Section/TextRun.php +++ b/Classes/PHPWord/Section/TextRun.php @@ -116,7 +116,8 @@ class PHPWord_Section_TextRun * @param mixed $styleFont * @return PHPWord_Section_Image */ - public function addImage($imageSrc, $style = null) { + public function addImage($imageSrc, $style = null) + { $image = new PHPWord_Section_Image($imageSrc, $style); if (!is_null($image->getSource())) { @@ -130,13 +131,26 @@ class PHPWord_Section_TextRun } } + /** + * Add a Text Break + * + * @param int $count + */ + public function addTextBreak($count = 1) + { + for ($i=1; $i<=$count; $i++) { + $this->_elementCollection[] = new PHPWord_Section_TextBreak(); + } + } + /** * Create a new Footnote Element * * @param string $text * @return PHPWord_Section_Footnote */ - public function createFootnote($styleParagraph = null) { + public function createFootnote($styleParagraph = null) + { $footnote = new PHPWord_Section_Footnote($styleParagraph); $refID = PHPWord_Footnote::addFootnoteElement($footnote); $footnote->setReferenceId($refID); @@ -163,4 +177,4 @@ class PHPWord_Section_TextRun { return $this->_styleParagraph; } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Shared/File.php b/Classes/PHPWord/Shared/File.php index 7c1470fe..84cd2e84 100755 --- a/Classes/PHPWord/Shared/File.php +++ b/Classes/PHPWord/Shared/File.php @@ -76,7 +76,7 @@ class PHPWord_Shared_File // Found something? if ($returnValue == '' || is_null($returnValue)) { - $pathArray = split('/', $pFilename); + $pathArray = explode('/', $pFilename); while (in_array('..', $pathArray) && $pathArray[0] != '..') { for ($i = 0; $i < count($pathArray); ++$i) { if ($pathArray[$i] == '..' && $i > 0) { diff --git a/Classes/PHPWord/Shared/Font.php b/Classes/PHPWord/Shared/Font.php index ccb19bc7..9e6dc44f 100755 --- a/Classes/PHPWord/Shared/Font.php +++ b/Classes/PHPWord/Shared/Font.php @@ -88,5 +88,4 @@ class PHPWord_Shared_Font { return ($sizeInPoint * 20); } - } diff --git a/Classes/PHPWord/Shared/String.php b/Classes/PHPWord/Shared/String.php index f570e6b7..2aa68d3f 100755 --- a/Classes/PHPWord/Shared/String.php +++ b/Classes/PHPWord/Shared/String.php @@ -266,5 +266,4 @@ class PHPWord_Shared_String $count = strlen($value); return $count; } - } diff --git a/Classes/PHPWord/Shared/XMLWriter.php b/Classes/PHPWord/Shared/XMLWriter.php index ae4fa160..1563ab46 100755 --- a/Classes/PHPWord/Shared/XMLWriter.php +++ b/Classes/PHPWord/Shared/XMLWriter.php @@ -150,4 +150,4 @@ class PHPWord_Shared_XMLWriter return $this->text($text); } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Shared/ZipStreamWrapper.php b/Classes/PHPWord/Shared/ZipStreamWrapper.php index 0dc42875..ee346921 100755 --- a/Classes/PHPWord/Shared/ZipStreamWrapper.php +++ b/Classes/PHPWord/Shared/ZipStreamWrapper.php @@ -121,7 +121,7 @@ class PHPWord_Shared_ZipStreamWrapper /** * Read stream */ - function stream_read($count) + public function stream_read($count) { $ret = substr($this->_data, $this->_position, $count); $this->_position += strlen($ret); diff --git a/Classes/PHPWord/Style.php b/Classes/PHPWord/Style.php index daabd46b..060ce1be 100755 --- a/Classes/PHPWord/Style.php +++ b/Classes/PHPWord/Style.php @@ -175,4 +175,3 @@ class PHPWord_Style } } } - diff --git a/Classes/PHPWord/Style/Cell.php b/Classes/PHPWord/Style/Cell.php index 8783675e..10d4a9be 100755 --- a/Classes/PHPWord/Style/Cell.php +++ b/Classes/PHPWord/Style/Cell.php @@ -123,7 +123,7 @@ class PHPWord_Style_Cell * * @var integer */ - private $_gridSpan = NULL; + private $_gridSpan = null; /** * rowspan (restart, continue) @@ -133,7 +133,7 @@ class PHPWord_Style_Cell * * @var string */ - private $_vMerge = NULL; + private $_vMerge = null; /** * Create a new Cell Style @@ -342,4 +342,4 @@ class PHPWord_Style_Cell { return $this->_vMerge; } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Style/Image.php b/Classes/PHPWord/Style/Image.php index 47a71cba..4453463a 100755 --- a/Classes/PHPWord/Style/Image.php +++ b/Classes/PHPWord/Style/Image.php @@ -173,4 +173,4 @@ class PHPWord_Style_Image { return $this->wrappingStyle; } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Style/Paragraph.php b/Classes/PHPWord/Style/Paragraph.php index bf0ae182..754589cb 100755 --- a/Classes/PHPWord/Style/Paragraph.php +++ b/Classes/PHPWord/Style/Paragraph.php @@ -506,4 +506,4 @@ class PHPWord_Style_Paragraph { return $this->lineHeight; } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Style/Row.php b/Classes/PHPWord/Style/Row.php index a0e79b3a..66d5505f 100644 --- a/Classes/PHPWord/Style/Row.php +++ b/Classes/PHPWord/Style/Row.php @@ -81,5 +81,4 @@ class PHPWord_Style_Row { return $this->_cantSplit ? 1 : 0; } - -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Style/Tab.php b/Classes/PHPWord/Style/Tab.php index a280eebb..f8ee4b9b 100755 --- a/Classes/PHPWord/Style/Tab.php +++ b/Classes/PHPWord/Style/Tab.php @@ -92,7 +92,7 @@ class PHPWord_Style_Tab * @param int $position Must be an integer; otherwise defaults to 0. * @param string $leader Defaults to NULL if value is not possible. */ - public function __construct($val = NULL, $position = 0, $leader = NULL) + public function __construct($val = null, $position = 0, $leader = null) { // Default to clear if the stop type is not matched $this->_val = (self::isStopType($val)) ? $val : 'clear'; @@ -101,7 +101,7 @@ class PHPWord_Style_Tab $this->_position = (is_numeric($position)) ? intval($position) : 0; // Default to NULL if no tab leader - $this->_leader = (self::isLeaderType($leader)) ? $leader : NULL; + $this->_leader = (self::isLeaderType($leader)) ? $leader : null; } /** @@ -109,7 +109,7 @@ class PHPWord_Style_Tab * * @param PHPWord_Shared_XMLWriter $objWriter */ - public function toXml(PHPWord_Shared_XMLWriter &$objWriter = NULL) + public function toXml(PHPWord_Shared_XMLWriter &$objWriter = null) { if (isset($objWriter)) { $objWriter->startElement("w:tab"); @@ -143,4 +143,4 @@ class PHPWord_Style_Tab { return in_array($attribute, self::$_possibleLeaders); } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Style/Tabs.php b/Classes/PHPWord/Style/Tabs.php index ebad8fbb..144d2a5d 100755 --- a/Classes/PHPWord/Style/Tabs.php +++ b/Classes/PHPWord/Style/Tabs.php @@ -51,7 +51,7 @@ class PHPWord_Style_Tabs * * @param PHPWord_Shared_XMLWriter $objWriter */ - public function toXml(PHPWord_Shared_XMLWriter &$objWriter = NULL) + public function toXml(PHPWord_Shared_XMLWriter &$objWriter = null) { if (isset($objWriter)) { $objWriter->startElement("w:tabs"); @@ -61,4 +61,4 @@ class PHPWord_Style_Tabs $objWriter->endElement(); } } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/TOC.php b/Classes/PHPWord/TOC.php index 1366f0c3..ae514a54 100755 --- a/Classes/PHPWord/TOC.php +++ b/Classes/PHPWord/TOC.php @@ -152,4 +152,4 @@ class PHPWord_TOC { return self::$_styleFont; } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Template.php b/Classes/PHPWord/Template.php index edf0de75..4d356dd3 100755 --- a/Classes/PHPWord/Template.php +++ b/Classes/PHPWord/Template.php @@ -75,7 +75,7 @@ class PHPWord_Template throw new PHPWord_Exception('Could not create temporary file with unique name in the default temporary directory.'); } } - + /** * Applies XSL style sheet to template's parts * @@ -133,10 +133,10 @@ class PHPWord_Template } $replace = htmlspecialchars($replace); } else { - foreach($replace as $key=>$value) { + foreach ($replace as $key => $value) { $replace[$key] = htmlspecialchars($value); } - } + } $regExpDelim = '/'; $escapedSearch = preg_quote($search, $regExpDelim); @@ -154,37 +154,40 @@ class PHPWord_Template /** * Find the start position of the nearest table row before $offset - * + * * @param mixed $offset */ - private function _findRowStart($offset) { - $rowStart = strrpos($this->_documentXML, "_documentXML) - $offset) * -1)); - if (!$rowStart) { - $rowStart = strrpos($this->_documentXML, "", ((strlen($this->_documentXML) - $offset) * -1)); - } - if (!$rowStart) { - trigger_error("Can not find the start position of the row to clone."); - return false; - } + private function _findRowStart($offset) + { + $rowStart = strrpos($this->_documentXML, "_documentXML) - $offset) * -1)); + if (!$rowStart) { + $rowStart = strrpos($this->_documentXML, "", ((strlen($this->_documentXML) - $offset) * -1)); + } + if (!$rowStart) { + trigger_error("Can not find the start position of the row to clone."); + return false; + } return $rowStart; } /** * Find the end position of the nearest table row after $offset - * + * * @param mixed $offset */ - private function _findRowEnd($offset) { - $rowEnd = strpos($this->_documentXML, "", $offset) + 7; + private function _findRowEnd($offset) + { + $rowEnd = strpos($this->_documentXML, "", $offset) + 7; return $rowEnd; } /** * Get a slice of a string - * + * * @param mixed $offset */ - private function _getSlice($startPosition, $endPosition = 0) { + private function _getSlice($startPosition, $endPosition = 0) + { if (!$endPosition) { $endPosition = strlen($this->_documentXML); } @@ -193,38 +196,39 @@ class PHPWord_Template /** * Clone a table row in a template document - * + * * @param mixed $search * @param mixed $numberOfClones */ - public function cloneRow($search, $numberOfClones) { - if(substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') { + public function cloneRow($search, $numberOfClones) + { + if (substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') { $search = '${'.$search.'}'; } - + $tagPos = strpos($this->_documentXML, $search); - if (!$tagPos) { - trigger_error("Can not clone row, template variable not found or variable contains markup."); - return false; - } - + if (!$tagPos) { + trigger_error("Can not clone row, template variable not found or variable contains markup."); + return false; + } + $rowStart = $this->_findRowStart($tagPos); $rowEnd = $this->_findRowEnd($tagPos); $xmlRow = $this->_getSlice($rowStart, $rowEnd); - + // Check if there's a cell spanning multiple rows. if (preg_match('##', $xmlRow)) { $extraRowStart = $rowEnd; $extraRowEnd = $rowEnd; - while(true) { + while (true) { $extraRowStart = $this->_findRowStart($extraRowEnd + 1); $extraRowEnd = $this->_findRowEnd($extraRowEnd + 1); - + // If extraRowEnd is lower then 7, there was no next row found. if ($extraRowEnd < 7) { break; } - + // If tmpXmlRow doesn't contain continue, this row is no longer part of the spanned row. $tmpXmlRow = $this->_getSlice($extraRowStart, $extraRowEnd); if (!preg_match('##', $tmpXmlRow) && !preg_match('##', $tmpXmlRow)) { @@ -232,17 +236,17 @@ class PHPWord_Template } // This row was a spanned row, update $rowEnd and search for the next row. $rowEnd = $extraRowEnd; - } + } $xmlRow = $this->_getSlice($rowStart, $rowEnd); } - + $result = $this->_getSlice(0, $rowStart); - for ($i = 1; $i <= $numberOfClones; $i++) { - $result .= preg_replace('/\$\{(.*?)\}/','\${\\1#'.$i.'}', $xmlRow); - } - $result .= $this->_getSlice($rowEnd); - - $this->_documentXML = $result; + for ($i = 1; $i <= $numberOfClones; $i++) { + $result .= preg_replace('/\$\{(.*?)\}/', '\${\\1#'.$i.'}', $xmlRow); + } + $result .= $this->_getSlice($rowEnd); + + $this->_documentXML = $result; } /** @@ -250,7 +254,8 @@ class PHPWord_Template * * @return string */ - public function save() { + public function save() + { $this->_objZip->addFromString('word/document.xml', $this->_documentXML); // Close zip file @@ -266,7 +271,8 @@ class PHPWord_Template * * @param string $strFilename */ - public function saveAs($strFilename) { + public function saveAs($strFilename) + { $tempFilename = $this->save(); if (file_exists($strFilename)) { diff --git a/Classes/PHPWord/Writer/ODText.php b/Classes/PHPWord/Writer/ODText.php index 3421da8f..71775630 100755 --- a/Classes/PHPWord/Writer/ODText.php +++ b/Classes/PHPWord/Writer/ODText.php @@ -161,7 +161,7 @@ class PHPWord_Writer_ODText implements PHPWord_Writer_IWriter } $objZip->addFromString('Pictures/' . str_replace(' ', '_', $this->getDrawingHashTable()->getByIndex($i)->getIndexedFilename()), $imageContents); - } else if ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_MemoryDrawing) { + } elseif ($this->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_MemoryDrawing) { ob_start(); call_user_func( $this->getDrawingHashTable()->getByIndex($i)->getRenderingFunction(), @@ -236,7 +236,7 @@ class PHPWord_Writer_ODText implements PHPWord_Writer_IWriter * @param string $pPartName Writer part name * @return PHPWord_Writer_ODText_WriterPart */ - function getWriterPart($pPartName = '') + public function getWriterPart($pPartName = '') { if ($pPartName != '' && isset($this->_writerParts[strtolower($pPartName)])) { return $this->_writerParts[strtolower($pPartName)]; @@ -287,4 +287,4 @@ class PHPWord_Writer_ODText implements PHPWord_Writer_IWriter { return $this->_diskCachingDirectory; } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Writer/ODText/Content.php b/Classes/PHPWord/Writer/ODText/Content.php index f79eb4b4..7ee571a3 100755 --- a/Classes/PHPWord/Writer/ODText/Content.php +++ b/Classes/PHPWord/Writer/ODText/Content.php @@ -249,27 +249,27 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart foreach ($_elements as $element) { if ($element instanceof PHPWord_Section_Text) { $this->_writeText($objWriter, $element); - } elseif($element instanceof PHPWord_Section_TextRun) { + } elseif ($element instanceof PHPWord_Section_TextRun) { $this->_writeTextRun($objWriter, $element); } elseif ($element instanceof PHPWord_Section_TextBreak) { $this->_writeTextBreak($objWriter); /* - } elseif($element instanceof PHPWord_Section_Link) { + } elseif ($element instanceof PHPWord_Section_Link) { $this->_writeLink($objWriter, $element); - } elseif($element instanceof PHPWord_Section_Title) { + } elseif ($element instanceof PHPWord_Section_Title) { $this->_writeTitle($objWriter, $element); - } elseif($element instanceof PHPWord_Section_PageBreak) { + } elseif ($element instanceof PHPWord_Section_PageBreak) { $this->_writePageBreak($objWriter); - } elseif($element instanceof PHPWord_Section_Table) { + } elseif ($element instanceof PHPWord_Section_Table) { $this->_writeTable($objWriter, $element); - } elseif($element instanceof PHPWord_Section_ListItem) { + } elseif ($element instanceof PHPWord_Section_ListItem) { $this->_writeListItem($objWriter, $element); - } elseif($element instanceof PHPWord_Section_Image || + } elseif ($element instanceof PHPWord_Section_Image || $element instanceof PHPWord_Section_MemoryImage) { $this->_writeImage($objWriter, $element); - } elseif($element instanceof PHPWord_Section_Object) { + } elseif ($element instanceof PHPWord_Section_Object) { $this->_writeObject($objWriter, $element); - } elseif($element instanceof PHPWord_TOC) { + } elseif ($element instanceof PHPWord_TOC) { $this->_writeTOC($objWriter); */ } else { @@ -303,8 +303,8 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart protected function _writeText( PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Text $text, - $withoutP = false) - { + $withoutP = false + ) { $styleFont = $text->getFontStyle(); $styleParagraph = $text->getParagraphStyle(); @@ -351,9 +351,7 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart * @param PHPWord_Section_TextRun $textrun * @todo Enable all other section types */ - protected function _writeTextRun( - PHPWord_Shared_XMLWriter $objWriter = null, - PHPWord_Section_TextRun $textrun) + protected function _writeTextRun(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_TextRun $textrun) { $elements = $textrun->getElements(); $objWriter->startElement('text:p'); @@ -377,7 +375,7 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart $objWriter->endElement(); } - private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section) + private function _writeEndSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section = null) { } @@ -386,9 +384,7 @@ class PHPWord_Writer_ODText_Content extends PHPWord_Writer_ODText_WriterPart * * @todo Create the real function */ - private function _writeSection( - PHPWord_Shared_XMLWriter $objWriter = null, - PHPWord_Section $section) + private function _writeSection(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section $section = null) { } } diff --git a/Classes/PHPWord/Writer/ODText/Manifest.php b/Classes/PHPWord/Writer/ODText/Manifest.php index f9815941..4b1c6b26 100755 --- a/Classes/PHPWord/Writer/ODText/Manifest.php +++ b/Classes/PHPWord/Writer/ODText/Manifest.php @@ -86,7 +86,7 @@ class PHPWord_Writer_ODText_Manifest extends PHPWord_Writer_ODText_WriterPart $objWriter->writeAttribute('manifest:media-type', $mimeType); $objWriter->writeAttribute('manifest:full-path', 'Pictures/' . str_replace(' ', '_', $this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getIndexedFilename())); $objWriter->endElement(); - } else if ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_MemoryDrawing) { + } elseif ($this->getParentWriter()->getDrawingHashTable()->getByIndex($i) instanceof PHPWord_Shape_MemoryDrawing) { $extension = strtolower($this->getParentWriter()->getDrawingHashTable()->getByIndex($i)->getMimeType()); $extension = explode('/', $extension); $extension = $extension[1]; diff --git a/Classes/PHPWord/Writer/ODText/Mimetype.php b/Classes/PHPWord/Writer/ODText/Mimetype.php index 439ada1a..def40db4 100755 --- a/Classes/PHPWord/Writer/ODText/Mimetype.php +++ b/Classes/PHPWord/Writer/ODText/Mimetype.php @@ -42,5 +42,4 @@ class PHPWord_Writer_ODText_Mimetype extends PHPWord_Writer_ODText_WriterPart return 'application/vnd.oasis.opendocument.text'; } - } diff --git a/Classes/PHPWord/Writer/ODText/Styles.php b/Classes/PHPWord/Writer/ODText/Styles.php index 745da08e..7f388809 100755 --- a/Classes/PHPWord/Writer/ODText/Styles.php +++ b/Classes/PHPWord/Writer/ODText/Styles.php @@ -182,8 +182,8 @@ class PHPWord_Writer_ODText_Styles extends PHPWord_Writer_ODText_WriterPart } $objWriter->endElement(); $objWriter->endElement(); - } // PHPWord_Style_Paragraph - elseif ($style instanceof PHPWord_Style_Paragraph) { + } elseif ($style instanceof PHPWord_Style_Paragraph) { + // PHPWord_Style_Paragraph // style:style $objWriter->startElement('style:style'); $objWriter->writeAttribute('style:name', $styleName); @@ -197,9 +197,8 @@ class PHPWord_Writer_ODText_Styles extends PHPWord_Writer_ODText_WriterPart $objWriter->endElement(); $objWriter->endElement(); - - } // PHPWord_Style_TableFull - elseif ($style instanceof PHPWord_Style_TableFull) { + } elseif ($style instanceof PHPWord_Style_TableFull) { + // PHPWord_Style_TableFull } } } diff --git a/Classes/PHPWord/Writer/RTF.php b/Classes/PHPWord/Writer/RTF.php index e22fe99c..66ec7383 100755 --- a/Classes/PHPWord/Writer/RTF.php +++ b/Classes/PHPWord/Writer/RTF.php @@ -81,7 +81,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter } $hFile = fopen($pFilename, 'w') or die("can't open file"); - fwrite($hFile, $this->_getData()); + fwrite($hFile, $this->getData()); fclose($hFile); // If a temporary file was used, copy it to the correct file stream @@ -135,11 +135,11 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter return $this->_drawingHashTable; } - private function _getData() + private function getData() { // PHPWord object : $this->_document - $this->_fontTable = $this->_getDataFont(); - $this->_colorTable = $this->_getDataColor(); + $this->_fontTable = $this->getDataFont(); + $this->_colorTable = $this->getDataColor(); $sRTFContent = '{\rtf1'; // Set the default character set @@ -180,7 +180,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter $sRTFContent .= '\fs' . (PHPWord::DEFAULT_FONT_SIZE * 2); $sRTFContent .= PHP_EOL; // Body - $sRTFContent .= $this->_getDataContent(); + $sRTFContent .= $this->getDataContent(); $sRTFContent .= '}'; @@ -188,7 +188,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter return $sRTFContent; } - private function _getDataFont() + private function getDataFont() { $pPHPWord = $this->_document; @@ -204,7 +204,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter foreach ($styles as $styleName => $style) { // PHPWord_Style_Font if ($style instanceof PHPWord_Style_Font) { - if (in_array($style->getName(), $arrFonts) == FALSE) { + if (in_array($style->getName(), $arrFonts) == false) { $arrFonts[] = $style->getName(); } } @@ -226,7 +226,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter $fStyle = $element->getFontStyle(); if ($fStyle instanceof PHPWord_Style_Font) { - if (in_array($fStyle->getName(), $arrFonts) == FALSE) { + if (in_array($fStyle->getName(), $arrFonts) == false) { $arrFonts[] = $fStyle->getName(); } } @@ -238,7 +238,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter return $arrFonts; } - private function _getDataColor() + private function getDataColor() { $pPHPWord = $this->_document; @@ -254,10 +254,10 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter if ($style instanceof PHPWord_Style_Font) { $color = $style->getColor(); $fgcolor = $style->getFgColor(); - if (in_array($color, $arrColors) == FALSE && $color != PHPWord::DEFAULT_FONT_COLOR && !empty($color)) { + if (in_array($color, $arrColors) == false && $color != PHPWord::DEFAULT_FONT_COLOR && !empty($color)) { $arrColors[] = $color; } - if (in_array($fgcolor, $arrColors) == FALSE && $fgcolor != PHPWord::DEFAULT_FONT_COLOR && !empty($fgcolor)) { + if (in_array($fgcolor, $arrColors) == false && $fgcolor != PHPWord::DEFAULT_FONT_COLOR && !empty($fgcolor)) { $arrColors[] = $fgcolor; } } @@ -279,10 +279,10 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter $fStyle = $element->getFontStyle(); if ($fStyle instanceof PHPWord_Style_Font) { - if (in_array($fStyle->getColor(), $arrColors) == FALSE) { + if (in_array($fStyle->getColor(), $arrColors) == false) { $arrColors[] = $fStyle->getColor(); } - if (in_array($fStyle->getFgColor(), $arrColors) == FALSE) { + if (in_array($fStyle->getFgColor(), $arrColors) == false) { $arrColors[] = $fStyle->getFgColor(); } } @@ -294,7 +294,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter return $arrColors; } - private function _getDataContent() + private function getDataContent() { $pPHPWord = $this->_document; $sRTFBody = ''; @@ -309,11 +309,11 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter $_elements = $section->getElements(); foreach ($_elements as $element) { if ($element instanceof PHPWord_Section_Text) { - $sRTFBody .= $this->_getDataContent_writeText($element); + $sRTFBody .= $this->getDataContentText($element); } elseif ($element instanceof PHPWord_Section_TextBreak) { - $sRTFBody .= $this->_getDataContent_writeTextBreak(); + $sRTFBody .= $this->getDataContentTextBreak(); } elseif ($element instanceof PHPWord_Section_TextRun) { - $sRTFBody .= $this->_getDataContent_writeTextRun($element); + $sRTFBody .= $this->getDataContentTextRun($element); /* } elseif($element instanceof PHPWord_Section_Link) { $this->_writeLink($objWriter, $element); @@ -346,7 +346,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter /** * Get text */ - private function _getDataContent_writeText(PHPWord_Section_Text $text, $withoutP = false) + private function getDataContentText(PHPWord_Section_Text $text, $withoutP = false) { $sRTFText = ''; @@ -384,7 +384,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter if ($styleFont) { if ($styleFont->getColor() != null) { $idxColor = array_search($styleFont->getColor(), $this->_colorTable); - if ($idxColor !== FALSE) { + if ($idxColor !== false) { $sRTFText .= '\cf' . ($idxColor + 1); } } else { @@ -392,7 +392,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter } if ($styleFont->getName() != null) { $idxFont = array_search($styleFont->getName(), $this->_fontTable); - if ($idxFont !== FALSE) { + if ($idxFont !== false) { $sRTFText .= '\f' . $idxFont; } } else { @@ -437,7 +437,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter /** * Get text run content */ - private function _getDataContent_writeTextRun(PHPWord_Section_TextRun $textrun) + private function getDataContentTextRun(PHPWord_Section_TextRun $textrun) { $sRTFText = ''; $elements = $textrun->getElements(); @@ -446,7 +446,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter foreach ($elements as $element) { if ($element instanceof PHPWord_Section_Text) { $sRTFText .= '{'; - $sRTFText .= $this->_getDataContent_writeText($element, true); + $sRTFText .= $this->getDataContentText($element, true); $sRTFText .= '}' . PHP_EOL; } } @@ -455,12 +455,10 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter return $sRTFText; } - private function _getDataContent_writeTextBreak() + private function getDataContentTextBreak() { $this->_lastParagraphStyle = ''; return '\par' . PHP_EOL; } - - -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Writer/Word2007.php b/Classes/PHPWord/Writer/Word2007.php index b55fb556..8d112d47 100755 --- a/Classes/PHPWord/Writer/Word2007.php +++ b/Classes/PHPWord/Writer/Word2007.php @@ -25,6 +25,9 @@ * @version 0.7.0 */ +use PhpOffice\PhpWord\Exceptions\InvalidImageException; +use PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException; + /** * Class PHPWord_Writer_Word2007 */ @@ -115,7 +118,8 @@ class PHPWord_Writer_Word2007 implements PHPWord_Writer_IWriter $footnoteLinks = array(); $_footnoteElements = PHPWord_Footnote::getFootnoteLinkElements(); - foreach($_footnoteElements as $element) { // loop through footnote link elements + // loop through footnote link elements + foreach ($_footnoteElements as $element) { $footnoteLinks[] = $element; } @@ -190,24 +194,38 @@ class PHPWord_Writer_Word2007 implements PHPWord_Writer_IWriter } } - private function _chkContentTypes($src) + /** + * @param string $src + */ + private function checkContentTypes($src) { - $srcInfo = pathinfo($src); - $extension = strtolower($srcInfo['extension']); - if (substr($extension, 0, 3) == 'php') { + $extension = null; + if (stripos(strrev($src), strrev('.php')) === 0) { $extension = 'php'; + } else { + $imageType = exif_imagetype($src); + if ($imageType === IMAGETYPE_JPEG) { + $extension = 'jpg'; + } elseif ($imageType === IMAGETYPE_GIF) { + $extension = 'gif'; + } elseif ($imageType === IMAGETYPE_PNG) { + $extension = 'png'; + } elseif ($imageType === IMAGETYPE_BMP) { + $extension = 'bmp'; + } elseif ($imageType === IMAGETYPE_TIFF_II || $imageType === IMAGETYPE_TIFF_MM) { + $extension = 'tif'; + } } - $_supportedImageTypes = array('jpg', 'jpeg', 'gif', 'png', 'bmp', 'tif', 'tiff', 'php'); - if (in_array($extension, $_supportedImageTypes)) { - $imagedata = getimagesize($src); - $imagetype = image_type_to_mime_type($imagedata[2]); - $imageext = image_type_to_extension($imagedata[2]); - $imageext = str_replace('.', '', $imageext); - if ($imageext == 'jpeg') $imageext = 'jpg'; - - if (!in_array($imagetype, $this->_imageTypes)) { - $this->_imageTypes[$imageext] = $imagetype; + if (isset($extension)) { + $imageData = getimagesize($src); + $imageType = image_type_to_mime_type($imageData[2]); + $imageExtension = str_replace('.', '', image_type_to_extension($imageData[2])); + if ($imageExtension === 'jpeg') { + $imageExtension = 'jpg'; + } + if (!in_array($imageType, $this->_imageTypes)) { + $this->_imageTypes[$imageExtension] = $imageType; } } else { if (!in_array($extension, $this->_objectTypes)) { @@ -256,10 +274,10 @@ class PHPWord_Writer_Word2007 implements PHPWord_Writer_IWriter $objZip->addFromString('word/' . $element['target'], $imageContents); imagedestroy($image); - $this->_chkContentTypes($element['source']); + $this->checkContentTypes($element['source']); } else { $objZip->addFile($element['source'], 'word/' . $element['target']); - $this->_chkContentTypes($element['source']); + $this->checkContentTypes($element['source']); } } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php old mode 100755 new mode 100644 index 64b5c3cf..77cc83fa --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -108,6 +108,8 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { $this->_writeImage($objWriter, $element, true); } elseif ($element instanceof PHPWord_Section_Footnote) { $this->_writeFootnoteReference($objWriter, $element, true); + } elseif ($element instanceof PHPWord_Section_TextBreak) { + $objWriter->writeElement('w:br'); } } } @@ -124,7 +126,14 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { * @return void */ protected function _writeParagraphStyle( +<<<<<<< HEAD PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Style_Paragraph $style, $withoutPPR = false) { +======= + PHPWord_Shared_XMLWriter $objWriter = null, + PHPWord_Style_Paragraph $style, + $withoutPPR = false + ) { +>>>>>>> 5e0fc7a2d815c96de6f6cdd081404df61e8ed886 $align = $style->getAlign(); $spacing = $style->getSpacing(); $spaceBefore = $style->getSpaceBefore(); @@ -870,7 +879,8 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { $objWriter->endElement(); } - protected function _writeFootnote(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footnote $footnote) { + protected function _writeFootnote(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footnote $footnote) + { $objWriter->startElement('w:footnote'); $objWriter->writeAttribute('w:id', $footnote->getReferenceId()); @@ -904,7 +914,8 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { $objWriter->endElement(); // w:footnote } - protected function _writeFootnoteReference(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footnote $footnote, $withoutP = false) { + protected function _writeFootnoteReference(PHPWord_Shared_XMLWriter $objWriter = null, PHPWord_Section_Footnote $footnote, $withoutP = false) + { if (!$withoutP) { $objWriter->startElement('w:p'); } @@ -921,5 +932,8 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart { $objWriter->endElement(); // w:p } } +<<<<<<< HEAD +======= +>>>>>>> 5e0fc7a2d815c96de6f6cdd081404df61e8ed886 } diff --git a/Classes/PHPWord/Writer/Word2007/ContentTypes.php b/Classes/PHPWord/Writer/Word2007/ContentTypes.php index b17bdae3..56a33d69 100755 --- a/Classes/PHPWord/Writer/Word2007/ContentTypes.php +++ b/Classes/PHPWord/Writer/Word2007/ContentTypes.php @@ -50,12 +50,16 @@ class PHPWord_Writer_Word2007_ContentTypes extends PHPWord_Writer_Word2007_Write // Rels $this->_writeDefaultContentType( - $objWriter, 'rels', 'application/vnd.openxmlformats-package.relationships+xml' + $objWriter, + 'rels', + 'application/vnd.openxmlformats-package.relationships+xml' ); // XML $this->_writeDefaultContentType( - $objWriter, 'xml', 'application/xml' + $objWriter, + 'xml', + 'application/xml' ); // Add media content-types @@ -65,62 +69,88 @@ class PHPWord_Writer_Word2007_ContentTypes extends PHPWord_Writer_Word2007_Write // Add embedding content-types if (count($_objectTypes) > 0) { - $this->_writeDefaultContentType($objWriter, 'bin', 'application/vnd.openxmlformats-officedocument.oleObject'); + $this->_writeDefaultContentType( + $objWriter, + 'bin', + 'application/vnd.openxmlformats-officedocument.oleObject' + ); } // DocProps $this->_writeOverrideContentType( - $objWriter, '/docProps/app.xml', 'application/vnd.openxmlformats-officedocument.extended-properties+xml' + $objWriter, + '/docProps/app.xml', + 'application/vnd.openxmlformats-officedocument.extended-properties+xml' ); $this->_writeOverrideContentType( - $objWriter, '/docProps/core.xml', 'application/vnd.openxmlformats-package.core-properties+xml' + $objWriter, + '/docProps/core.xml', + 'application/vnd.openxmlformats-package.core-properties+xml' ); // Document $this->_writeOverrideContentType( - $objWriter, '/word/document.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml' + $objWriter, + '/word/document.xml', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml' ); // Styles $this->_writeOverrideContentType( - $objWriter, '/word/styles.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml' + $objWriter, + '/word/styles.xml', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml' ); // Numbering $this->_writeOverrideContentType( - $objWriter, '/word/numbering.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml' + $objWriter, + '/word/numbering.xml', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml' ); // Settings $this->_writeOverrideContentType( - $objWriter, '/word/settings.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml' + $objWriter, + '/word/settings.xml', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml' ); // Theme1 $this->_writeOverrideContentType( - $objWriter, '/word/theme/theme1.xml', 'application/vnd.openxmlformats-officedocument.theme+xml' + $objWriter, + '/word/theme/theme1.xml', + 'application/vnd.openxmlformats-officedocument.theme+xml' ); // WebSettings $this->_writeOverrideContentType( - $objWriter, '/word/webSettings.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml' + $objWriter, + '/word/webSettings.xml', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml' ); // Font Table $this->_writeOverrideContentType( - $objWriter, '/word/fontTable.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml' + $objWriter, + '/word/fontTable.xml', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml' ); for ($i = 1; $i <= $_cHdrs; $i++) { $this->_writeOverrideContentType( - $objWriter, '/word/header' . $i . '.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml' + $objWriter, + '/word/header' . $i . '.xml', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml' ); } for ($i = 1; $i <= $_cFtrs; $i++) { $this->_writeOverrideContentType( - $objWriter, '/word/footer' . $i . '.xml', 'application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml' + $objWriter, + '/word/footer' . $i . '.xml', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml' ); } diff --git a/Classes/PHPWord/Writer/Word2007/Document.php b/Classes/PHPWord/Writer/Word2007/Document.php index 044dbcec..558fa72f 100755 --- a/Classes/PHPWord/Writer/Word2007/Document.php +++ b/Classes/PHPWord/Writer/Word2007/Document.php @@ -94,7 +94,7 @@ class PHPWord_Writer_Word2007_Document extends PHPWord_Writer_Word2007_Base $this->_writeObject($objWriter, $element); } elseif ($element instanceof PHPWord_TOC) { $this->_writeTOC($objWriter); - } elseif($element instanceof PHPWord_Section_Footnote) { + } elseif ($element instanceof PHPWord_Section_Footnote) { $this->_writeFootnoteReference($objWriter, $element); } } diff --git a/Classes/PHPWord/Writer/Word2007/Footnotes.php b/Classes/PHPWord/Writer/Word2007/Footnotes.php index 1bf27ee4..fe7ced0e 100644 --- a/Classes/PHPWord/Writer/Word2007/Footnotes.php +++ b/Classes/PHPWord/Writer/Word2007/Footnotes.php @@ -26,8 +26,10 @@ */ -class PHPWord_Writer_Word2007_Footnotes extends PHPWord_Writer_Word2007_Base { - public function writeFootnotes($allFootnotesCollection) { +class PHPWord_Writer_Word2007_Footnotes extends PHPWord_Writer_Word2007_Base +{ + public function writeFootnotes($allFootnotesCollection) + { // Create XML writer $objWriter = null; if ($this->getParentWriter()->getUseDiskCaching()) { @@ -40,8 +42,8 @@ class PHPWord_Writer_Word2007_Footnotes extends PHPWord_Writer_Word2007_Base { $objWriter->startDocument('1.0', 'UTF-8', 'yes'); $objWriter->startElement('w:footnotes'); - $objWriter->writeAttribute('xmlns:r','http://schemas.openxmlformats.org/officeDocument/2006/relationships'); - $objWriter->writeAttribute('xmlns:w','http://schemas.openxmlformats.org/wordprocessingml/2006/main'); + $objWriter->writeAttribute('xmlns:r', 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'); + $objWriter->writeAttribute('xmlns:w', 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'); // write separator and continuation separator $objWriter->startElement('w:footnote'); @@ -67,8 +69,8 @@ class PHPWord_Writer_Word2007_Footnotes extends PHPWord_Writer_Word2007_Base { $objWriter->endElement(); // w:footnote - foreach($allFootnotesCollection as $footnote) { - if($footnote instanceof PHPWord_Section_Footnote) { + foreach ($allFootnotesCollection as $footnote) { + if ($footnote instanceof PHPWord_Section_Footnote) { $this->_writeFootnote($objWriter, $footnote); } } @@ -78,4 +80,4 @@ class PHPWord_Writer_Word2007_Footnotes extends PHPWord_Writer_Word2007_Base { // Return return $objWriter->getData(); } -} \ No newline at end of file +} diff --git a/Classes/PHPWord/Writer/Word2007/FootnotesRels.php b/Classes/PHPWord/Writer/Word2007/FootnotesRels.php index 22533815..6c81b3c9 100644 --- a/Classes/PHPWord/Writer/Word2007/FootnotesRels.php +++ b/Classes/PHPWord/Writer/Word2007/FootnotesRels.php @@ -26,64 +26,61 @@ */ -class PHPWord_Writer_Word2007_FootnotesRels extends PHPWord_Writer_Word2007_WriterPart { - public function writeFootnotesRels($_relsCollection) { - // Create XML writer - $objWriter = null; - if ($this->getParentWriter()->getUseDiskCaching()) { - $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); - } else { - $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY); +class PHPWord_Writer_Word2007_FootnotesRels extends PHPWord_Writer_Word2007_WriterPart +{ + public function writeFootnotesRels($_relsCollection) + { + // Create XML writer + $objWriter = null; + if ($this->getParentWriter()->getUseDiskCaching()) { + $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory()); + } else { + $objWriter = new PHPWord_Shared_XMLWriter(PHPWord_Shared_XMLWriter::STORAGE_MEMORY); + } + + // XML header + $objWriter->startDocument('1.0', 'UTF-8', 'yes'); + + // Relationships + $objWriter->startElement('Relationships'); + $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); + + // Relationships to Links + foreach ($_relsCollection as $relation) { + $relationType = $relation['type']; + $relationName = $relation['target']; + $relationId = $relation['rID']; + $targetMode = ($relationType == 'hyperlink') ? 'External' : ''; + + $this->_writeRelationship($objWriter, $relationId, 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/' . $relationType, $relationName, $targetMode); + } + + $objWriter->endElement(); + + // Return + return $objWriter->getData(); } - // XML header - $objWriter->startDocument('1.0','UTF-8','yes'); + private function _writeRelationship(PHPWord_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '') + { + if ($pType != '' && $pTarget != '') { + if (strpos($pId, 'rId') === false) { + $pId = 'rId' . $pId; + } - // Relationships - $objWriter->startElement('Relationships'); - $objWriter->writeAttribute('xmlns', 'http://schemas.openxmlformats.org/package/2006/relationships'); + // Write relationship + $objWriter->startElement('Relationship'); + $objWriter->writeAttribute('Id', $pId); + $objWriter->writeAttribute('Type', $pType); + $objWriter->writeAttribute('Target', $pTarget); - // Relationships to Links - foreach($_relsCollection as $relation) { - $relationType = $relation['type']; - $relationName = $relation['target']; - $relationId = $relation['rID']; - $targetMode = ($relationType == 'hyperlink') ? 'External' : ''; + if ($pTargetMode != '') { + $objWriter->writeAttribute('TargetMode', $pTargetMode); + } - $this->_writeRelationship( - $objWriter, - $relationId, - 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/'.$relationType, - $relationName, - $targetMode - ); + $objWriter->endElement(); + } else { + throw new Exception("Invalid parameters passed."); + } } - - $objWriter->endElement(); - - // Return - return $objWriter->getData(); - } - - private function _writeRelationship(PHPWord_Shared_XMLWriter $objWriter = null, $pId = 1, $pType = '', $pTarget = '', $pTargetMode = '') { - if($pType != '' && $pTarget != '') { - if(strpos($pId, 'rId') === false) { - $pId = 'rId' . $pId; - } - - // Write relationship - $objWriter->startElement('Relationship'); - $objWriter->writeAttribute('Id', $pId); - $objWriter->writeAttribute('Type', $pType); - $objWriter->writeAttribute('Target', $pTarget); - - if($pTargetMode != '') { - $objWriter->writeAttribute('TargetMode', $pTargetMode); - } - - $objWriter->endElement(); - } else { - throw new Exception("Invalid parameters passed."); - } - } -} \ No newline at end of file +} diff --git a/README.md b/README.md index fc9a59d0..1003a29a 100755 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ __Want to contribute?__ Fork us! ## Requirements * PHP version 5.3.0 or higher +* PHP extension [php_zip](http://php.net/manual/en/book.zip.php) enabled ## Installation diff --git a/Tests/PHPWord/AutoloaderTest.php b/Tests/PHPWord/AutoloaderTest.php index 2ad280f0..d8882f9f 100644 --- a/Tests/PHPWord/AutoloaderTest.php +++ b/Tests/PHPWord/AutoloaderTest.php @@ -16,8 +16,14 @@ class AutoloaderTest extends PHPUnit_Framework_TestCase public function testAutoloadLegacy() { - $this->assertNull(PHPWord_Autoloader::load('Foo'), 'PHPWord_Autoloader::load() is trying to load classes outside of the PHPWord namespace'); - $this->assertTrue(PHPWord_Autoloader::load('PHPWord'), 'PHPWord_Autoloader::load() failed to autoload the PHPWord class'); + $this->assertNull( + PHPWord_Autoloader::load('Foo'), + 'PHPWord_Autoloader::load() is trying to load classes outside of the PHPWord namespace' + ); + $this->assertTrue( + PHPWord_Autoloader::load('PHPWord'), + 'PHPWord_Autoloader::load() failed to autoload the PHPWord class' + ); } public function testAutoload() @@ -25,8 +31,20 @@ class AutoloaderTest extends PHPUnit_Framework_TestCase $declared = get_declared_classes(); $declaredCount = count($declared); Autoloader::autoload('Foo'); - $this->assertEquals($declaredCount, count(get_declared_classes()), 'PhpOffice\\PhpWord\\Autoloader::autoload() is trying to load classes outside of the PhpOffice\\PhpWord namespace'); - Autoloader::autoload('PhpOffice\\PhpWord\\Exceptions\\InvalidStyleException'); // TODO change this class to the main PHPWord class when it is namespaced - $this->assertTrue(in_array('PhpOffice\\PhpWord\\Exceptions\\InvalidStyleException', get_declared_classes()), 'PhpOffice\\PhpWord\\Autoloader::autoload() failed to autoload the PhpOffice\\PhpWord\\Exceptions\\InvalidStyleException class'); + $this->assertEquals( + $declaredCount, + count(get_declared_classes()), + 'PhpOffice\\PhpWord\\Autoloader::autoload() is trying to load classes ' . + 'outside of the PhpOffice\\PhpWord namespace' + ); + // TODO change this class to the main PHPWord class when it is namespaced + Autoloader::autoload( + 'PhpOffice\\PhpWord\\Exceptions\\InvalidStyleException' + ); + $this->assertTrue( + in_array('PhpOffice\\PhpWord\\Exceptions\\InvalidStyleException', get_declared_classes()), + 'PhpOffice\\PhpWord\\Autoloader::autoload() failed to autoload the ' . + 'PhpOffice\\PhpWord\\Exceptions\\InvalidStyleException class' + ); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/IOFactoryTest.php b/Tests/PHPWord/IOFactoryTest.php index a7c05bcf..c0dfab30 100644 --- a/Tests/PHPWord/IOFactoryTest.php +++ b/Tests/PHPWord/IOFactoryTest.php @@ -16,7 +16,11 @@ class IOFactoryTest extends \PHPUnit_Framework_TestCase { public function testGetSearchLocations() { - $this->assertAttributeEquals(PHPWord_IOFactory::getSearchLocations(), '_searchLocations', 'PHPWord_IOFactory'); + $this->assertAttributeEquals( + PHPWord_IOFactory::getSearchLocations(), + '_searchLocations', + 'PHPWord_IOFactory' + ); } public function testSetSearchLocationsWithArray() @@ -38,7 +42,11 @@ class IOFactoryTest extends \PHPUnit_Framework_TestCase { PHPWord_IOFactory::setSearchLocations(array()); PHPWord_IOFactory::addSearchLocation('type', 'location', 'classname'); - $this->assertAttributeEquals(array(array('type' => 'type', 'path' => 'location', 'class' => 'classname')), '_searchLocations', 'PHPWord_IOFactory'); + $this->assertAttributeEquals( + array(array('type' => 'type', 'path' => 'location', 'class' => 'classname')), + '_searchLocations', + 'PHPWord_IOFactory' + ); } /** @@ -57,6 +65,9 @@ class IOFactoryTest extends \PHPUnit_Framework_TestCase { $oPHPWord = new PHPWord(); - $this->assertEquals(PHPWord_IOFactory::createWriter($oPHPWord, 'Word2007'), new PHPWord_Writer_Word2007($oPHPWord)); + $this->assertEquals( + PHPWord_IOFactory::createWriter($oPHPWord, 'Word2007'), + new PHPWord_Writer_Word2007($oPHPWord) + ); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/MediaTest.php b/Tests/PHPWord/MediaTest.php index 25cdab0a..e5722724 100644 --- a/Tests/PHPWord/MediaTest.php +++ b/Tests/PHPWord/MediaTest.php @@ -3,6 +3,7 @@ namespace PHPWord\Tests; use PHPUnit_Framework_TestCase; use PHPWord_Media; +use PHPWord_Section; class MediaTest extends \PHPUnit_Framework_TestCase { @@ -25,4 +26,26 @@ class MediaTest extends \PHPUnit_Framework_TestCase { $this->assertAttributeEquals(PHPWord_Media::getFooterMediaElements(), '_footerMedia', 'PHPWord_Media'); } + + /** + * Todo: add memory image to this test + * + * @covers PHPWord_Media::addSectionMediaElement + */ + public function testAddSectionMediaElement() + { + $section = new PHPWord_Section(0); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif"); + + $elements = $section->getElements(); + $this->assertEquals(6, count($elements)); + foreach ($elements as $element) { + $this->assertInstanceOf('PHPWord_Section_Image', $element); + } + } } \ No newline at end of file diff --git a/Tests/PHPWord/Reader/Word2007Test.php b/Tests/PHPWord/Reader/Word2007Test.php new file mode 100644 index 00000000..cbd500ff --- /dev/null +++ b/Tests/PHPWord/Reader/Word2007Test.php @@ -0,0 +1,69 @@ +assertTrue($object->canRead($file)); + } + + /** + * Test canRead() failure + * + * @expectedException Exception + */ + public function testCanReadFailed() + { + $dir = join( + DIRECTORY_SEPARATOR, + array(PHPWORD_TESTS_DIR_ROOT, '_files', 'documents') + ); + $object = new PHPWord_Reader_Word2007; + $file = $dir . DIRECTORY_SEPARATOR . 'foo.docx'; + $this->assertFalse($object->canRead($file)); + $object = PHPWord_IOFactory::load($file); + } + + /** + * Test load document + */ + public function testLoad() + { + $dir = join( + DIRECTORY_SEPARATOR, + array(PHPWORD_TESTS_DIR_ROOT, '_files', 'documents') + ); + $file = $dir . DIRECTORY_SEPARATOR . 'reader.docx'; + $object = PHPWord_IOFactory::load($file); + $this->assertInstanceOf('PHPWord', $object); + } +} diff --git a/Tests/PHPWord/Section/Footer/PreserveTextTest.php b/Tests/PHPWord/Section/Footer/PreserveTextTest.php index ad6b5c24..02e82fa1 100644 --- a/Tests/PHPWord/Section/Footer/PreserveTextTest.php +++ b/Tests/PHPWord/Section/Footer/PreserveTextTest.php @@ -26,8 +26,12 @@ class PreserveTextTest extends \PHPUnit_Framework_TestCase public function testConstructWithArray() { - $oPreserveText = new PHPWord_Section_Footer_PreserveText('text', array('align' => 'center'), array('marginLeft' => 600, 'marginRight' => 600, 'marginTop' => 600, 'marginBottom' => 600)); + $oPreserveText = new PHPWord_Section_Footer_PreserveText( + 'text', + array('align' => 'center'), + array('marginLeft' => 600, 'marginRight' => 600, 'marginTop' => 600, 'marginBottom' => 600) + ); $this->assertInstanceOf('PHPWord_Style_Font', $oPreserveText->getFontStyle()); $this->assertInstanceOf('PHPWord_Style_Paragraph', $oPreserveText->getParagraphStyle()); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Section/FooterTest.php b/Tests/PHPWord/Section/FooterTest.php index 46999b59..2942f2fb 100644 --- a/Tests/PHPWord/Section/FooterTest.php +++ b/Tests/PHPWord/Section/FooterTest.php @@ -87,7 +87,9 @@ class FooterTest extends \PHPUnit_Framework_TestCase public function testAddMemoryImage() { $oFooter = new PHPWord_Section_Footer(1); - $element = $oFooter->addMemoryImage('https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png'); + $element = $oFooter->addMemoryImage( + 'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png' + ); $this->assertCount(1, $oFooter->getElements()); $this->assertInstanceOf('PHPWord_Section_MemoryImage', $element); @@ -118,4 +120,4 @@ class FooterTest extends \PHPUnit_Framework_TestCase $this->assertInternalType('array', $oFooter->getElements()); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Section/FootnoteTest.php b/Tests/PHPWord/Section/FootnoteTest.php index a1f26494..1c798f4a 100644 --- a/Tests/PHPWord/Section/FootnoteTest.php +++ b/Tests/PHPWord/Section/FootnoteTest.php @@ -61,4 +61,4 @@ class FootnoteTest extends \PHPUnit_Framework_TestCase $oFootnote = new PHPWord_Section_Footnote(); $this->assertInternalType('array', $oFootnote->getElements()); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Section/HeaderTest.php b/Tests/PHPWord/Section/HeaderTest.php index d7dc3ace..f71f9d81 100644 --- a/Tests/PHPWord/Section/HeaderTest.php +++ b/Tests/PHPWord/Section/HeaderTest.php @@ -83,7 +83,9 @@ class HeaderTest extends \PHPUnit_Framework_TestCase public function testAddMemoryImage() { $oHeader = new PHPWord_Section_Header(1); - $element = $oHeader->addMemoryImage('https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png'); + $element = $oHeader->addMemoryImage( + 'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png' + ); $this->assertCount(1, $oHeader->getElements()); $this->assertInstanceOf('PHPWord_Section_MemoryImage', $element); @@ -161,4 +163,4 @@ class HeaderTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oHeader->getType(), PHPWord_Section_Header::EVEN); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Section/ImageTest.php b/Tests/PHPWord/Section/ImageTest.php index f11b1293..9362be19 100644 --- a/Tests/PHPWord/Section/ImageTest.php +++ b/Tests/PHPWord/Section/ImageTest.php @@ -28,11 +28,18 @@ class ImageTest extends \PHPUnit_Framework_TestCase \DIRECTORY_SEPARATOR, array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'images', 'firefox.png') ); - $oImage = new PHPWord_Section_Image($src, array('width' => 210, 'height' => 210, 'align' => 'center', 'wrappingStyle' => \PHPWord_Style_Image::WRAPPING_STYLE_BEHIND)); + $oImage = new PHPWord_Section_Image( + $src, + array('width' => 210, 'height' => 210, 'align' => 'center', + 'wrappingStyle' => \PHPWord_Style_Image::WRAPPING_STYLE_BEHIND) + ); $this->assertInstanceOf('PHPWord_Style_Image', $oImage->getStyle()); } + /** + * @covers PHPWord_Section_Image::__construct + */ public function testValidImageTypes() { new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg"); @@ -45,6 +52,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase /** * @expectedException \PhpOffice\PhpWord\Exceptions\InvalidImageException + * @covers PHPWord_Section_Image::__construct */ public function testImageNotFound() { @@ -53,6 +61,7 @@ class ImageTest extends \PHPUnit_Framework_TestCase /** * @expectedException \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException + * @covers PHPWord_Section_Image::__construct */ public function testInvalidImageTypes() { @@ -91,4 +100,4 @@ class ImageTest extends \PHPUnit_Framework_TestCase $oImage->setIsWatermark(true); $this->assertEquals($oImage->getIsWatermark(), true); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Section/LinkTest.php b/Tests/PHPWord/Section/LinkTest.php index c9f1bdca..340ddc41 100644 --- a/Tests/PHPWord/Section/LinkTest.php +++ b/Tests/PHPWord/Section/LinkTest.php @@ -20,7 +20,12 @@ class LinkTest extends \PHPUnit_Framework_TestCase public function testConstructWithParamsArray() { - $oLink = new PHPWord_Section_Link('http://www.google.com', 'Search Engine', array('color' => '0000FF', 'underline' => PHPWord_Style_Font::UNDERLINE_SINGLE), array('marginLeft' => 600, 'marginRight' => 600, 'marginTop' => 600, 'marginBottom' => 600)); + $oLink = new PHPWord_Section_Link( + 'http://www.google.com', + 'Search Engine', + array('color' => '0000FF', 'underline' => PHPWord_Style_Font::UNDERLINE_SINGLE), + array('marginLeft' => 600, 'marginRight' => 600, 'marginTop' => 600, 'marginBottom' => 600) + ); $this->assertInstanceOf('PHPWord_Section_Link', $oLink); $this->assertEquals($oLink->getLinkSrc(), 'http://www.google.com'); @@ -45,4 +50,4 @@ class LinkTest extends \PHPUnit_Framework_TestCase $oLink->setRelationId($iVal); $this->assertEquals($oLink->getRelationId(), $iVal); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Section/ListItemTest.php b/Tests/PHPWord/Section/ListItemTest.php index 0b656eed..28323695 100644 --- a/Tests/PHPWord/Section/ListItemTest.php +++ b/Tests/PHPWord/Section/ListItemTest.php @@ -16,7 +16,12 @@ class ListItemTest extends \PHPUnit_Framework_TestCase public function testStyle() { - $oListItem = new PHPWord_Section_ListItem('text', 1, null, array('listType' => PHPWord_Style_ListItem::TYPE_NUMBER)); + $oListItem = new PHPWord_Section_ListItem( + 'text', + 1, + null, + array('listType' => PHPWord_Style_ListItem::TYPE_NUMBER) + ); $this->assertInstanceOf('PHPWord_Style_ListItem', $oListItem->getStyle()); $this->assertEquals($oListItem->getStyle()->getListType(), PHPWord_Style_ListItem::TYPE_NUMBER); @@ -29,4 +34,4 @@ class ListItemTest extends \PHPUnit_Framework_TestCase $this->assertEquals($oListItem->getDepth(), $iVal); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Section/MemoryImageTest.php b/Tests/PHPWord/Section/MemoryImageTest.php index 9b9694d9..be53f119 100644 --- a/Tests/PHPWord/Section/MemoryImageTest.php +++ b/Tests/PHPWord/Section/MemoryImageTest.php @@ -92,4 +92,4 @@ class MemoryImageTest extends \PHPUnit_Framework_TestCase $oMemoryImage->setRelationId($iVal); $this->assertEquals($oMemoryImage->getRelationId(), $iVal); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Section/ObjectTest.php b/Tests/PHPWord/Section/ObjectTest.php index fa5fa24b..53d26314 100644 --- a/Tests/PHPWord/Section/ObjectTest.php +++ b/Tests/PHPWord/Section/ObjectTest.php @@ -83,4 +83,4 @@ class ObjectTest extends \PHPUnit_Framework_TestCase $oObject->setObjectId($iVal); $this->assertEquals($oObject->getObjectId(), $iVal); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Section/PageBreakTest.php b/Tests/PHPWord/Section/PageBreakTest.php index 01944d06..497a7c1a 100644 --- a/Tests/PHPWord/Section/PageBreakTest.php +++ b/Tests/PHPWord/Section/PageBreakTest.php @@ -16,4 +16,4 @@ class PageBreakTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PHPWord_Section_PageBreak', $oPageBreak); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Section/SettingsTest.php b/Tests/PHPWord/Section/SettingsTest.php index 505347e6..69d7fe2c 100644 --- a/Tests/PHPWord/Section/SettingsTest.php +++ b/Tests/PHPWord/Section/SettingsTest.php @@ -233,4 +233,4 @@ class SettingsTest extends \PHPUnit_Framework_TestCase $oSettings->setBreakType(); $this->assertNull($oSettings->getBreakType()); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Section/Table/CellTest.php b/Tests/PHPWord/Section/Table/CellTest.php index bfd7e0d0..8d5b99bd 100644 --- a/Tests/PHPWord/Section/Table/CellTest.php +++ b/Tests/PHPWord/Section/Table/CellTest.php @@ -130,7 +130,9 @@ class CellTest extends \PHPUnit_Framework_TestCase public function testAddMemoryImageSection() { $oCell = new PHPWord_Section_Table_Cell('section', 1); - $element = $oCell->addMemoryImage('https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png'); + $element = $oCell->addMemoryImage( + 'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png' + ); $this->assertCount(1, $oCell->getElements()); $this->assertInstanceOf('PHPWord_Section_MemoryImage', $element); @@ -139,7 +141,9 @@ class CellTest extends \PHPUnit_Framework_TestCase public function testAddMemoryImageHeader() { $oCell = new PHPWord_Section_Table_Cell('header', 1); - $element = $oCell->addMemoryImage('https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png'); + $element = $oCell->addMemoryImage( + 'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png' + ); $this->assertCount(1, $oCell->getElements()); $this->assertInstanceOf('PHPWord_Section_MemoryImage', $element); @@ -148,7 +152,9 @@ class CellTest extends \PHPUnit_Framework_TestCase public function testAddMemoryImageFooter() { $oCell = new PHPWord_Section_Table_Cell('footer', 1); - $element = $oCell->addMemoryImage('https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png'); + $element = $oCell->addMemoryImage( + 'https://assets.mozillalabs.com/Brands-Logos/Thunderbird/logo-only/thunderbird_logo-only_RGB.png' + ); $this->assertCount(1, $oCell->getElements()); $this->assertInstanceOf('PHPWord_Section_MemoryImage', $element); @@ -201,4 +207,4 @@ class CellTest extends \PHPUnit_Framework_TestCase $this->assertInternalType('array', $oCell->getElements()); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Section/Table/RowTest.php b/Tests/PHPWord/Section/Table/RowTest.php index 849abbac..115eb1c7 100644 --- a/Tests/PHPWord/Section/Table/RowTest.php +++ b/Tests/PHPWord/Section/Table/RowTest.php @@ -22,7 +22,12 @@ class RowTest extends \PHPUnit_Framework_TestCase { $iVal = rand(1, 1000); $iVal2 = rand(1, 1000); - $oRow = new PHPWord_Section_Table_Row('section', $iVal, $iVal2, array('borderBottomSize' => 18, 'borderBottomColor' => '0000FF', 'bgColor' => '66BBFF')); + $oRow = new PHPWord_Section_Table_Row( + 'section', + $iVal, + $iVal2, + array('borderBottomSize' => 18, 'borderBottomColor' => '0000FF', 'bgColor' => '66BBFF') + ); $this->assertEquals($oRow->getHeight(), $iVal2); $this->assertInstanceOf('PHPWord_Style_Row', $oRow->getStyle()); @@ -36,4 +41,4 @@ class RowTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PHPWord_Section_Table_Cell', $element); $this->assertCount(1, $oRow->getCells()); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Section/TableTest.php b/Tests/PHPWord/Section/TableTest.php index f207d426..80cd4296 100644 --- a/Tests/PHPWord/Section/TableTest.php +++ b/Tests/PHPWord/Section/TableTest.php @@ -26,7 +26,11 @@ class TableTest extends \PHPUnit_Framework_TestCase public function testStyleArray() { - $oTable = new PHPWord_Section_Table('section', 1, array('borderSize' => 6, 'borderColor' => '006699', 'cellMargin' => 80)); + $oTable = new PHPWord_Section_Table( + 'section', + 1, + array('borderSize' => 6, 'borderColor' => '006699', 'cellMargin' => 80) + ); $this->assertInstanceOf('PHPWord_Style_Table', $oTable->getStyle()); } @@ -54,4 +58,4 @@ class TableTest extends \PHPUnit_Framework_TestCase $element = $oTable->addCell(); $this->assertInstanceOf('PHPWord_Section_Table_Cell', $element); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Section/TextBreakTest.php b/Tests/PHPWord/Section/TextBreakTest.php index 86e9ac48..a75b97c4 100644 --- a/Tests/PHPWord/Section/TextBreakTest.php +++ b/Tests/PHPWord/Section/TextBreakTest.php @@ -16,4 +16,4 @@ class TextBreakTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PHPWord_Section_TextBreak', $oTextBreak); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Section/TextRunTest.php b/Tests/PHPWord/Section/TextRunTest.php index 0ea9d701..aa352fa5 100644 --- a/Tests/PHPWord/Section/TextRunTest.php +++ b/Tests/PHPWord/Section/TextRunTest.php @@ -95,4 +95,4 @@ class TextRunTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PHPWord_Section_Footnote', $element); $this->assertCount(1, $oTextRun->getElements()); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Section/TextTest.php b/Tests/PHPWord/Section/TextTest.php index 8a2bf321..b85b2028 100644 --- a/Tests/PHPWord/Section/TextTest.php +++ b/Tests/PHPWord/Section/TextTest.php @@ -40,4 +40,4 @@ class TextTest extends \PHPUnit_Framework_TestCase $oText->setParagraphStyle(array('align' => 'center', 'spaceAfter' => 100)); $this->assertInstanceOf('PHPWord_Style_Paragraph', $oText->getParagraphStyle()); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Section/TitleTest.php b/Tests/PHPWord/Section/TitleTest.php index c595ab97..c0a6185d 100644 --- a/Tests/PHPWord/Section/TitleTest.php +++ b/Tests/PHPWord/Section/TitleTest.php @@ -45,4 +45,4 @@ class TitleTest extends \PHPUnit_Framework_TestCase $oTitle->setBookmarkId($iVal); $this->assertEquals($oTitle->getBookmarkId(), $iVal); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/SectionTest.php b/Tests/PHPWord/SectionTest.php index 378fd642..3af02eca 100644 --- a/Tests/PHPWord/SectionTest.php +++ b/Tests/PHPWord/SectionTest.php @@ -35,4 +35,4 @@ class SectionTest extends \PHPUnit_Framework_TestCase $oSection = new PHPWord_Section(0); $this->assertAttributeEquals($oSection->getElements(), '_elementCollection', new PHPWord_Section(0)); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Shared/DrawingTest.php b/Tests/PHPWord/Shared/DrawingTest.php index cfebad34..c939b9c0 100644 --- a/Tests/PHPWord/Shared/DrawingTest.php +++ b/Tests/PHPWord/Shared/DrawingTest.php @@ -12,7 +12,6 @@ use PHPWord_Shared_Drawing; */ class DrawingTest extends \PHPUnit_Framework_TestCase { - /** * Test unit conversion functions with various numbers */ @@ -65,5 +64,4 @@ class DrawingTest extends \PHPUnit_Framework_TestCase $this->assertEquals($value[1], $result); } } - -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Shared/FileTest.php b/Tests/PHPWord/Shared/FileTest.php index fb7a8bd6..618d5801 100644 --- a/Tests/PHPWord/Shared/FileTest.php +++ b/Tests/PHPWord/Shared/FileTest.php @@ -12,13 +12,13 @@ use PHPWord_Shared_File; */ class FileTest extends \PHPUnit_Framework_TestCase { - /** * Test file_exists() */ - public function testFile_exists() + public function testFileExists() { - $dir = join(DIRECTORY_SEPARATOR, + $dir = join( + DIRECTORY_SEPARATOR, array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates') ); chdir($dir); @@ -30,12 +30,13 @@ class FileTest extends \PHPUnit_Framework_TestCase */ public function testRealpath() { - $dir = join(DIRECTORY_SEPARATOR, - array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates')); + $dir = join( + DIRECTORY_SEPARATOR, + array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates') + ); chdir($dir); $file = 'blank.docx'; $expected = $dir . DIRECTORY_SEPARATOR . $file; $this->assertEquals($expected, PHPWord_Shared_File::realpath($file)); } - } diff --git a/Tests/PHPWord/Shared/FontTest.php b/Tests/PHPWord/Shared/FontTest.php index f2950abb..74ece26a 100644 --- a/Tests/PHPWord/Shared/FontTest.php +++ b/Tests/PHPWord/Shared/FontTest.php @@ -43,4 +43,4 @@ class FontTest extends \PHPUnit_Framework_TestCase $result = PHPWord_Shared_Font::pointSizeToTwips($original); $this->assertEquals($original * 20, $result); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Shared/StringTest.php b/Tests/PHPWord/Shared/StringTest.php index 9bb0d63b..a0ab7ad0 100644 --- a/Tests/PHPWord/Shared/StringTest.php +++ b/Tests/PHPWord/Shared/StringTest.php @@ -12,7 +12,6 @@ use PHPWord_Shared_String; */ class StringTest extends \PHPUnit_Framework_TestCase { - /** * Test getIsMbstringEnabled() and getIsIconvEnabled() */ @@ -41,5 +40,4 @@ class StringTest extends \PHPUnit_Framework_TestCase $returned = PHPWord_Shared_String::FormatNumber('1022.1234'); $this->assertEquals($expected, $returned); } - } diff --git a/Tests/PHPWord/Style/CellTest.php b/Tests/PHPWord/Style/CellTest.php index 759f6e0e..61ef93f4 100644 --- a/Tests/PHPWord/Style/CellTest.php +++ b/Tests/PHPWord/Style/CellTest.php @@ -12,7 +12,6 @@ use PHPWord_Style_Cell; */ class CellTest extends \PHPUnit_Framework_TestCase { - /** * Test setting style with normal value */ @@ -75,4 +74,4 @@ class CellTest extends \PHPUnit_Framework_TestCase $object->setStyleValue('_borderSize', $value); $this->assertEquals($expected, $object->getBorderSize()); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Style/FontTest.php b/Tests/PHPWord/Style/FontTest.php index eb29cd5c..375ebe5b 100644 --- a/Tests/PHPWord/Style/FontTest.php +++ b/Tests/PHPWord/Style/FontTest.php @@ -114,4 +114,4 @@ class FontTest extends \PHPUnit_Framework_TestCase $this->assertEquals(720, $lineHeight); $this->assertEquals('auto', $lineRule); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Style/ImageTest.php b/Tests/PHPWord/Style/ImageTest.php index 52d0b4c2..60af0b6e 100644 --- a/Tests/PHPWord/Style/ImageTest.php +++ b/Tests/PHPWord/Style/ImageTest.php @@ -12,7 +12,6 @@ use PHPWord_Style_Image; */ class ImageTest extends \PHPUnit_Framework_TestCase { - /** * Test setting style with normal value */ @@ -67,5 +66,4 @@ class ImageTest extends \PHPUnit_Framework_TestCase $object = new PHPWord_Style_Image(); $object->setWrappingStyle('foo'); } - -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Style/ListItemTest.php b/Tests/PHPWord/Style/ListItemTest.php index 9b9f6ec9..7185e25e 100644 --- a/Tests/PHPWord/Style/ListItemTest.php +++ b/Tests/PHPWord/Style/ListItemTest.php @@ -12,7 +12,6 @@ use PHPWord_Style_ListItem; */ class ListItemTest extends \PHPUnit_Framework_TestCase { - /** * Test construct */ @@ -47,5 +46,4 @@ class ListItemTest extends \PHPUnit_Framework_TestCase $object->setListType($value); $this->assertEquals($value, $object->getListType()); } - -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Style/ParagraphTest.php b/Tests/PHPWord/Style/ParagraphTest.php index e61ff8fd..94550da1 100644 --- a/Tests/PHPWord/Style/ParagraphTest.php +++ b/Tests/PHPWord/Style/ParagraphTest.php @@ -133,5 +133,4 @@ class ParagraphTest extends \PHPUnit_Framework_TestCase $object->setLineHeight('12.5pt'); $this->assertEquals(12.5, $object->getLineHeight()); } - -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Style/RowTest.php b/Tests/PHPWord/Style/RowTest.php index 63c05d61..3d2f9b2f 100644 --- a/Tests/PHPWord/Style/RowTest.php +++ b/Tests/PHPWord/Style/RowTest.php @@ -12,7 +12,6 @@ use PHPWord_Style_Row; */ class RowTest extends \PHPUnit_Framework_TestCase { - /** * Test properties with normal value */ @@ -39,5 +38,4 @@ class RowTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, $object->$get()); } } - } diff --git a/Tests/PHPWord/Style/TOCTest.php b/Tests/PHPWord/Style/TOCTest.php index 78ac6506..80e34d74 100644 --- a/Tests/PHPWord/Style/TOCTest.php +++ b/Tests/PHPWord/Style/TOCTest.php @@ -12,7 +12,6 @@ use PHPWord_Style_TOC; */ class TOCTest extends \PHPUnit_Framework_TestCase { - /** * Test properties with normal value */ @@ -37,5 +36,4 @@ class TOCTest extends \PHPUnit_Framework_TestCase $this->assertEquals(null, $object->$get()); } } - } diff --git a/Tests/PHPWord/Style/TableFullTest.php b/Tests/PHPWord/Style/TableFullTest.php index 10855d55..6261404d 100644 --- a/Tests/PHPWord/Style/TableFullTest.php +++ b/Tests/PHPWord/Style/TableFullTest.php @@ -12,7 +12,6 @@ use PHPWord_Style_TableFull; */ class TableFullTest extends \PHPUnit_Framework_TestCase { - /** * Test class construction * @@ -132,5 +131,4 @@ class TableFullTest extends \PHPUnit_Framework_TestCase } $this->assertEquals($values, $object->getCellMargin()); } - } diff --git a/Tests/PHPWord/Style/TableTest.php b/Tests/PHPWord/Style/TableTest.php index 93f4ebfc..61459aad 100644 --- a/Tests/PHPWord/Style/TableTest.php +++ b/Tests/PHPWord/Style/TableTest.php @@ -12,7 +12,6 @@ use PHPWord_Style_Table; */ class TableTest extends \PHPUnit_Framework_TestCase { - /** * Test set style value */ @@ -50,5 +49,4 @@ class TableTest extends \PHPUnit_Framework_TestCase } $this->assertEquals($values, $object->getCellMargin()); } - } diff --git a/Tests/PHPWord/Style/TabsTest.php b/Tests/PHPWord/Style/TabsTest.php index 5d318610..3a3d69df 100644 --- a/Tests/PHPWord/Style/TabsTest.php +++ b/Tests/PHPWord/Style/TabsTest.php @@ -16,8 +16,8 @@ use PHPWord\Tests\TestHelperDOCX; class TabsTest extends \PHPUnit_Framework_TestCase { /** - * Executed before each method of the class - */ + * Executed before each method of the class + */ public function tearDown() { TestHelperDOCX::clear(); @@ -42,5 +42,4 @@ class TabsTest extends \PHPUnit_Framework_TestCase $this->assertEquals(1440, $element->getAttribute('w:pos')); $this->assertEquals('dot', $element->getAttribute('w:leader')); } - } diff --git a/Tests/PHPWord/TOCTest.php b/Tests/PHPWord/TOCTest.php new file mode 100644 index 00000000..74038ee3 --- /dev/null +++ b/Tests/PHPWord/TOCTest.php @@ -0,0 +1,72 @@ + 9062, + 'tabLeader' => PHPWord_Style_TOC::TABLEADER_DOT, + 'indent' => 200, + ); + $object = new PHPWord_TOC( + array('size' => 11), + array('tabPos' => $expected['tabPos']) + ); + $tocStyle = $object->getStyleTOC(); + + $this->assertInstanceOf('PHPWord_Style_TOC', $tocStyle); + $this->assertInstanceOf('PHPWord_Style_Font', $object->getStyleFont()); + + foreach ($expected as $key => $value) { + $method = "get{$key}"; + $this->assertEquals($value, $tocStyle->$method()); + } + } + + /** + * @covers PHPWord_TOC::addTitle + * @covers PHPWord_TOC::getTitles + */ + public function testAddAndGetTitle() + { + // Prepare variables + $titleCount = 3; + $anchor = '_Toc' . (252634154 + $titleCount); + $bookmark = $titleCount - 1; // zero based + $titles = array( + 'Heading 1' => 1, + 'Heading 2' => 2, + 'Heading 3' => 3, + ); + + // @covers PHPWord_TOC::addTitle + foreach ($titles as $text => $depth) { + $response = PHPWord_TOC::addTitle($text, $depth); + } + $this->assertEquals($anchor, $response[0]); + $this->assertEquals($bookmark, $response[1]); + + // @covers PHPWord_TOC::getTitles + $i = 0; + $savedTitles = PHPWord_TOC::getTitles(); + foreach ($titles as $text => $depth) { + $this->assertEquals($text, $savedTitles[$i]['text']); + $this->assertEquals($depth, $savedTitles[$i]['depth']); + $i++; + } + } +} diff --git a/Tests/PHPWord/TemplateTest.php b/Tests/PHPWord/TemplateTest.php index 8c17d2e3..65db9850 100644 --- a/Tests/PHPWord/TemplateTest.php +++ b/Tests/PHPWord/TemplateTest.php @@ -115,4 +115,4 @@ class TemplateTest extends \PHPUnit_Framework_TestCase */ @$template->applyXslStyleSheet($xslDOMDocument); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Writer/ODTextTest.php b/Tests/PHPWord/Writer/ODTextTest.php new file mode 100644 index 00000000..d2ac2fc2 --- /dev/null +++ b/Tests/PHPWord/Writer/ODTextTest.php @@ -0,0 +1,88 @@ +assertInstanceOf('PHPWord', $object->getPHPWord()); + $this->assertInstanceOf("PHPWord_HashTable", $object->getDrawingHashTable()); + + $this->assertEquals('./', $object->getDiskCachingDirectory()); + $writerParts = array('Content', 'Manifest', 'Meta', 'Mimetype', 'Styles'); + foreach ($writerParts as $part) { + $this->assertInstanceOf( + "PHPWord_Writer_ODText_{$part}", + $object->getWriterPart($part) + ); + $this->assertInstanceOf( + "PHPWord_Writer_ODText", + $object->getWriterPart($part)->getParentWriter() + ); + } + } + + /** + * Test construct with null value/without PHPWord + * + * @expectedException Exception + * @expectedExceptionMessage No PHPWord assigned. + */ + public function testConstructWithNull() + { + $object = new PHPWord_Writer_ODText(); + $object->getPHPWord(); + } + + /** + * Test save() + */ + public function testSave() + { + $phpWord = new PHPWord(); + $phpWord->addFontStyle('Font', array('size' => 11)); + $phpWord->addParagraphStyle('Paragraph', array('align' => 'center')); + $section = $phpWord->createSection(); + $section->addText('Test 1', 'Font', 'Paragraph'); + $section->addTextBreak(); + $section->addText('Test 2'); + $section = $phpWord->createSection(); + $textrun = $section->createTextRun(); + $textrun->addText('Test 3'); + + $writer = new PHPWord_Writer_ODText($phpWord); + $file = join( + DIRECTORY_SEPARATOR, + array(PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.odt') + ); + $writer->save($file); + $this->assertTrue(file_exists($file)); + unlink($file); + } + + /** + * Test disk caching parameters + */ + public function testSetDiskCaching() + { + $object = new PHPWord_Writer_ODText(); + $object->setUseDiskCaching(true, PHPWORD_TESTS_DIR_ROOT); + $this->assertTrue($object->getUseDiskCaching()); + $this->assertEquals(PHPWORD_TESTS_DIR_ROOT, $object->getDiskCachingDirectory()); + } +} diff --git a/Tests/PHPWord/Writer/RTFTest.php b/Tests/PHPWord/Writer/RTFTest.php new file mode 100644 index 00000000..fc713786 --- /dev/null +++ b/Tests/PHPWord/Writer/RTFTest.php @@ -0,0 +1,63 @@ +assertInstanceOf('PHPWord', $object->getPHPWord()); + $this->assertInstanceOf("PHPWord_HashTable", $object->getDrawingHashTable()); + } + + /** + * Test construct with null value/without PHPWord + * + * @expectedException Exception + * @expectedExceptionMessage No PHPWord assigned. + */ + public function testConstructWithNull() + { + $object = new PHPWord_Writer_RTF(); + $object->getPHPWord(); + } + + /** + * Test save() + */ + public function testSave() + { + $phpWord = new PHPWord(); + $phpWord->addFontStyle('Font', array('size' => 11)); + $phpWord->addParagraphStyle('Paragraph', array('align' => 'center')); + $section = $phpWord->createSection(); + $section->addText('Test 1', 'Font', 'Paragraph'); + $section->addTextBreak(); + $section->addText('Test 2'); + $section = $phpWord->createSection(); + $textrun = $section->createTextRun(); + $textrun->addText('Test 3'); + + $writer = new PHPWord_Writer_RTF($phpWord); + $file = join( + DIRECTORY_SEPARATOR, + array(PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.rtf') + ); + $writer->save($file); + $this->assertTrue(file_exists($file)); + unlink($file); + } +} diff --git a/Tests/PHPWord/Writer/Word2007/BaseTest.php b/Tests/PHPWord/Writer/Word2007/BaseTest.php index cb8fa578..cd1f6dbd 100644 --- a/Tests/PHPWord/Writer/Word2007/BaseTest.php +++ b/Tests/PHPWord/Writer/Word2007/BaseTest.php @@ -20,29 +20,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase TestHelperDOCX::clear(); } - public function testWriteImage_Position() - { - $PHPWord = new PHPWord(); - $section = $PHPWord->createSection(); - $section->addImage( - PHPWORD_TESTS_DIR_ROOT . '/_files/images/earth.jpg', - array( - 'marginTop' => -1, - 'marginLeft' => -1, - 'wrappingStyle' => 'behind' - ) - ); - - $doc = TestHelperDOCX::getDocument($PHPWord); - $element = $doc->getElement('/w:document/w:body/w:p/w:r/w:pict/v:shape'); - - $style = $element->getAttribute('style'); - - $this->assertRegExp('/z\-index:\-[0-9]*/', $style); - $this->assertRegExp('/position:absolute;/', $style); - } - - public function testWriteParagraphStyle_Align() + public function testWriteParagraphStyleAlign() { $PHPWord = new PHPWord(); $section = $PHPWord->createSection(); @@ -55,34 +33,10 @@ class BaseTest extends \PHPUnit_Framework_TestCase $this->assertEquals('right', $element->getAttribute('w:val')); } - public function testWriteCellStyle_CellGridSpan() - { - $PHPWord = new PHPWord(); - $section = $PHPWord->createSection(); - - $table = $section->addTable(); - - $table->addRow(); - $cell = $table->addCell(200); - $cell->getStyle()->setGridSpan(5); - - $table->addRow(); - $table->addCell(40); - $table->addCell(40); - $table->addCell(40); - $table->addCell(40); - $table->addCell(40); - - $doc = TestHelperDOCX::getDocument($PHPWord); - $element = $doc->getElement('/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:gridSpan'); - - $this->assertEquals(5, $element->getAttribute('w:val')); - } - /** * Test write paragraph pagination */ - public function testWriteParagraphStyle_Pagination() + public function testWriteParagraphStylePagination() { // Create the doc $PHPWord = new PHPWord(); @@ -109,6 +63,52 @@ class BaseTest extends \PHPUnit_Framework_TestCase } } + public function testWriteCellStyleCellGridSpan() + { + $PHPWord = new PHPWord(); + $section = $PHPWord->createSection(); + + $table = $section->addTable(); + + $table->addRow(); + $cell = $table->addCell(200); + $cell->getStyle()->setGridSpan(5); + + $table->addRow(); + $table->addCell(40); + $table->addCell(40); + $table->addCell(40); + $table->addCell(40); + $table->addCell(40); + + $doc = TestHelperDOCX::getDocument($PHPWord); + $element = $doc->getElement('/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:gridSpan'); + + $this->assertEquals(5, $element->getAttribute('w:val')); + } + + public function testWriteImagePosition() + { + $PHPWord = new PHPWord(); + $section = $PHPWord->createSection(); + $section->addImage( + PHPWORD_TESTS_DIR_ROOT . '/_files/images/earth.jpg', + array( + 'marginTop' => -1, + 'marginLeft' => -1, + 'wrappingStyle' => 'behind' + ) + ); + + $doc = TestHelperDOCX::getDocument($PHPWord); + $element = $doc->getElement('/w:document/w:body/w:p/w:r/w:pict/v:shape'); + + $style = $element->getAttribute('style'); + + $this->assertRegExp('/z\-index:\-[0-9]*/', $style); + $this->assertRegExp('/position:absolute;/', $style); + } + public function testWritePreserveText() { $PHPWord = new PHPWord(); @@ -123,4 +123,4 @@ class BaseTest extends \PHPUnit_Framework_TestCase $this->assertEquals('PAGE', $preserve->nodeValue); $this->assertEquals('preserve', $preserve->getAttribute('xml:space')); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Writer/Word2007/DocumentTest.php b/Tests/PHPWord/Writer/Word2007/DocumentTest.php index 5d91ebab..22efe7d0 100644 --- a/Tests/PHPWord/Writer/Word2007/DocumentTest.php +++ b/Tests/PHPWord/Writer/Word2007/DocumentTest.php @@ -22,7 +22,7 @@ class DocumentTest extends \PHPUnit_Framework_TestCase TestHelperDOCX::clear(); } - public function testWriteEndSection_PageNumbering() + public function testWriteEndSectionPageNumbering() { $PHPWord = new PHPWord(); $section = $PHPWord->createSection(); @@ -33,4 +33,4 @@ class DocumentTest extends \PHPUnit_Framework_TestCase $this->assertEquals(2, $element->getAttribute('w:start')); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Writer/Word2007/StylesTest.php b/Tests/PHPWord/Writer/Word2007/StylesTest.php index 9fd19335..39162eae 100644 --- a/Tests/PHPWord/Writer/Word2007/StylesTest.php +++ b/Tests/PHPWord/Writer/Word2007/StylesTest.php @@ -51,4 +51,4 @@ class StylesTest extends \PHPUnit_Framework_TestCase $element = $doc->getElement($path, $file); $this->assertEquals('Normal', $element->getAttribute('w:val')); } -} \ No newline at end of file +} diff --git a/Tests/PHPWord/Writer/Word2007Test.php b/Tests/PHPWord/Writer/Word2007Test.php new file mode 100644 index 00000000..59bc6387 --- /dev/null +++ b/Tests/PHPWord/Writer/Word2007Test.php @@ -0,0 +1,88 @@ +assertInstanceOf( + "PHPWord_Writer_Word2007_{$part}", + $object->getWriterPart($part) + ); + $this->assertInstanceOf( + "PHPWord_Writer_Word2007", + $object->getWriterPart($part)->getParentWriter() + ); + } + } + + public function testSave() + { + $phpWord = new PHPWord(); + $phpWord->addFontStyle('Font', array('size' => 11)); + $phpWord->addParagraphStyle('Paragraph', array('align' => 'center')); + $section = $phpWord->createSection(); + $section->addText('Test 1', 'Font', 'Paragraph'); + $section->addTextBreak(); + $section->addText('Test 2'); + $section = $phpWord->createSection(); + $textrun = $section->createTextRun(); + $textrun->addText('Test 3'); + + $writer = new PHPWord_Writer_Word2007($phpWord); + $file = join( + DIRECTORY_SEPARATOR, + array(PHPWORD_TESTS_DIR_ROOT, '_files', 'temp.docx') + ); + $writer->save($file); + $this->assertTrue(file_exists($file)); + unlink($file); + } + + /** + * @covers PHPWord_Writer_Word2007::checkContentTypes + */ + public function testCheckContentTypes() + { + $phpWord = new PHPWord(); + $section = $phpWord->createSection(); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp"); + $section->addImage(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif"); + + $doc = TestHelperDOCX::getDocument($phpWord); + $mediaPath = $doc->getPath() . "/word/media"; + + $this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg", $mediaPath . "/section_image1.jpg"); + $this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg", $mediaPath . "/section_image2.jpg"); + $this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif", $mediaPath . "/section_image3.gif"); + $this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png", $mediaPath . "/section_image4.png"); + $this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp", $mediaPath . "/section_image5.bmp"); + $this->assertFileEquals(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif", $mediaPath . "/section_image6.tif"); + } +} \ No newline at end of file diff --git a/Tests/PHPWordTest.php b/Tests/PHPWordTest.php new file mode 100644 index 00000000..54ab4ae0 --- /dev/null +++ b/Tests/PHPWordTest.php @@ -0,0 +1,183 @@ +assertEquals( + new PHPWord_DocumentProperties(), + $object->getProperties() + ); + $this->assertEquals( + PHPWord::DEFAULT_FONT_NAME, + $object->getDefaultFontName() + ); + $this->assertEquals( + PHPWord::DEFAULT_FONT_SIZE, + $object->getDefaultFontSize() + ); + } + + /** + * @covers PHPWord::setProperties + * @covers PHPWord::getProperties + */ + public function testSetGetProperties() + { + $object = new PHPWord(); + $creator = 'PHPWord'; + $properties = $object->getProperties(); + $properties->setCreator($creator); + $object->setProperties($properties); + $this->assertEquals($creator, $object->getProperties()->getCreator()); + } + + /** + * @covers PHPWord::createSection + * @covers PHPWord::getSections + */ + public function testCreateGetSections() + { + $object = new PHPWord(); + $this->assertEquals(new PHPWord_Section(1), $object->createSection()); + $object->createSection(); + $this->assertEquals(2, count($object->getSections())); + } + + /** + * @covers PHPWord::setDefaultFontName + * @covers PHPWord::getDefaultFontName + */ + public function testSetGetDefaultFontName() + { + $object = new PHPWord(); + $fontName = 'Times New Roman'; + $this->assertEquals( + PHPWord::DEFAULT_FONT_NAME, + $object->getDefaultFontName() + ); + $object->setDefaultFontName($fontName); + $this->assertEquals($fontName, $object->getDefaultFontName()); + } + + /** + * @covers PHPWord::setDefaultFontSize + * @covers PHPWord::getDefaultFontSize + */ + public function testSetGetDefaultFontSize() + { + $object = new PHPWord(); + $fontSize = 16; + $this->assertEquals( + PHPWord::DEFAULT_FONT_SIZE, + $object->getDefaultFontSize() + ); + $object->setDefaultFontSize($fontSize); + $this->assertEquals($fontSize, $object->getDefaultFontSize()); + } + + /** + * @covers PHPWord::setDefaultParagraphStyle + * @covers PHPWord::loadTemplate + */ + public function testSetDefaultParagraphStyle() + { + $object = new PHPWord(); + $object->setDefaultParagraphStyle(array()); + $this->assertInstanceOf( + 'PHPWord_Style_Paragraph', + PHPWord_Style::getStyle('Normal') + ); + } + + /** + * @covers PHPWord::addParagraphStyle + * @covers PHPWord::addFontStyle + * @covers PHPWord::addTableStyle + * @covers PHPWord::addLinkStyle + */ + public function testAddStyles() + { + $object = new PHPWord(); + $styles = array('Paragraph' => 'Paragraph', 'Font' => 'Font', + 'Table' => 'TableFull', 'Link' => 'Font'); + foreach ($styles as $key => $value) { + $method = "add{$key}Style"; + $styleId = "{$key} Style"; + $styleType = "PHPWord_Style_{$value}"; + $object->$method($styleId, array()); + $this->assertInstanceOf( + $styleType, + PHPWord_Style::getStyle($styleId) + ); + } + + } + + /** + * @covers PHPWord::addTitleStyle + */ + public function testAddTitleStyle() + { + $object = new PHPWord(); + $titleLevel = 1; + $titleName = "Heading_{$titleLevel}"; + $object->addTitleStyle($titleLevel, array()); + $this->assertInstanceOf( + 'PHPWord_Style_Font', + PHPWord_Style::getStyle($titleName) + ); + } + + /** + * @covers PHPWord::loadTemplate + */ + public function testLoadTemplate() + { + $file = join( + DIRECTORY_SEPARATOR, + array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'blank.docx') + ); + $object = new PHPWord(); + $this->assertInstanceOf( + 'PHPWord_Template', + $object->loadTemplate($file) + ); + } + + /** + * @covers PHPWord::loadTemplate + * @expectedException PHPWord_Exception + */ + public function testLoadTemplateException() + { + $file = join( + DIRECTORY_SEPARATOR, + array(PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'blanks.docx') + ); + $object = new PHPWord(); + $object->loadTemplate($file); + } +} diff --git a/Tests/_files/documents/reader.docx b/Tests/_files/documents/reader.docx new file mode 100644 index 00000000..e2ceeb64 Binary files /dev/null and b/Tests/_files/documents/reader.docx differ diff --git a/Tests/_inc/TestHelperDOCX.php b/Tests/_inc/TestHelperDOCX.php index 965835d0..2d59dcb6 100644 --- a/Tests/_inc/TestHelperDOCX.php +++ b/Tests/_inc/TestHelperDOCX.php @@ -2,15 +2,15 @@ namespace PHPWord\Tests; use PHPWord; -use DOMDocument; class TestHelperDOCX { + /** @var string $file */ static protected $file; /** * @param \PHPWord $PHPWord - * @return \PHPWord\Tests\Xml_Document + * @return \PHPWord\Tests\XmlDocument */ public static function getDocument(PHPWord $PHPWord) { @@ -29,7 +29,7 @@ class TestHelperDOCX $zip->close(); } - return new Xml_Document(sys_get_temp_dir() . '/PHPWord_Unit_Test/'); + return new XmlDocument(sys_get_temp_dir() . '/PHPWord_Unit_Test/'); } public static function clear() @@ -50,75 +50,21 @@ class TestHelperDOCX foreach (scandir($dir) as $file) { if ($file === '.' || $file === '..') { continue; - } else if (is_file($dir . "/" . $file)) { + } elseif (is_file($dir . "/" . $file)) { unlink($dir . "/" . $file); - } else if (is_dir($dir . "/" . $file)) { + } elseif (is_dir($dir . "/" . $file)) { self::deleteDir($dir . "/" . $file); } } rmdir($dir); } -} - -class Xml_Document -{ - /** @var string $path */ - private $path; - - /** @var \DOMDocument $dom */ - private $dom; - - /** @var \DOMXpath $xpath */ - private $xpath; - - /** @var string $file */ - private $file; /** - * @param string $path + * @return string */ - public function __construct($path) + public static function getFile() { - $this->path = realpath($path); - } - - /** - * @param string $file - * @return \DOMDocument - */ - public function getFileDom($file = 'word/document.xml') - { - if (null !== $this->dom && $file === $this->file) { - return $this->dom; - } - - $this->xpath = null; - $this->file = $file; - - $file = $this->path . '/' . $file; - $this->dom = new DOMDocument(); - $this->dom->load($file); - return $this->dom; - } - - /** - * @param string $path - * @param string $file - * @return \DOMElement - */ - public function getElement($path, $file = 'word/document.xml') - { - if ($this->dom === null || $file !== $this->file) { - $this->getFileDom($file); - } - - if (null === $this->xpath) { - $this->xpath = new \DOMXpath($this->dom); - - } - - $elements = $this->xpath->query($path); - return $elements->item(0); + return self::$file; } } \ No newline at end of file diff --git a/Tests/_inc/XmlDocument.php b/Tests/_inc/XmlDocument.php new file mode 100644 index 00000000..f16bd6d8 --- /dev/null +++ b/Tests/_inc/XmlDocument.php @@ -0,0 +1,82 @@ +path = realpath($path); + } + + /** + * @param string $file + * @return \DOMDocument + */ + public function getFileDom($file = 'word/document.xml') + { + if (null !== $this->dom && $file === $this->file) { + return $this->dom; + } + + $this->xpath = null; + $this->file = $file; + + $file = $this->path . '/' . $file; + $this->dom = new DOMDocument(); + $this->dom->load($file); + return $this->dom; + } + + /** + * @param string $path + * @param string $file + * @return \DOMElement + */ + public function getElement($path, $file = 'word/document.xml') + { + if ($this->dom === null || $file !== $this->file) { + $this->getFileDom($file); + } + + if (null === $this->xpath) { + $this->xpath = new \DOMXpath($this->dom); + + } + + $elements = $this->xpath->query($path); + return $elements->item(0); + } + + /** + * @return string + */ + public function getFile() + { + return $this->file; + } + + /** + * @return string + */ + public function getPath() + { + return $this->path; + } +} diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php index 916d3503..1066a0d4 100755 --- a/Tests/bootstrap.php +++ b/Tests/bootstrap.php @@ -12,3 +12,4 @@ require_once __DIR__ . '/../Classes/PHPWord/Autoloader.php'; PHPWord_Autoloader::Register(); require_once __DIR__ . '/_inc/TestHelperDOCX.php'; +require_once __DIR__ . '/_inc/XmlDocument.php'; diff --git a/changelog.txt b/changelog.txt index 2230f687..a76efb00 100755 --- a/changelog.txt +++ b/changelog.txt @@ -50,6 +50,7 @@ Changes in branch for release 0.7.1 : - Bugfix: (ivanlanin) GH-94 - General: PHPWord_Shared_Drawing::centimetersToPixels() conversion - Feature: (ivanlanin) - Paragraph: setTabs() function - Feature: (ivanlanin) GH-99 - General: Basic support for TextRun on ODT and RTF +- Feature: (ivanlanin) - Reader: Initial effort for Word2007 - QA: (Progi1984) - UnitTests Changes in branch for release 0.7.0 : diff --git a/samples/Sample_10_ReadWord2007.php b/samples/Sample_10_ReadWord2007.php new file mode 100644 index 00000000..a837a574 --- /dev/null +++ b/samples/Sample_10_ReadWord2007.php @@ -0,0 +1,33 @@ +'); + +require_once '../Classes/PHPWord.php'; + +// Read contents +$sample = 'Sample_10_ReadWord2007'; +$source = "resources/{$sample}.docx"; +$target = "results/{$sample}"; +echo '

', date('H:i:s'), " Reading contents from `{$source}`

"; +$PHPWord = PHPWord_IOFactory::load($source); + +// Rewrite contents +echo date('H:i:s') , " Write to Word2007 format" , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007'); +$objWriter->save("{$sample}.docx"); +rename("{$sample}.docx", "{$target}.docx"); + +echo date('H:i:s') , ' Write to OpenDocumentText format' , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'ODText'); +$objWriter->save("{$sample}.odt"); +rename("{$sample}.odt", "{$target}.odt"); + +echo date('H:i:s') , ' Write to RTF format' , EOL; +$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'RTF'); +$objWriter->save("{$sample}.rtf"); +rename("{$sample}.rtf", "{$target}.rtf"); + +// Echo memory peak usage +echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL; +echo date('H:i:s') , " Done writing file" , EOL; diff --git a/samples/resources/Sample_10_ReadWord2007.docx b/samples/resources/Sample_10_ReadWord2007.docx new file mode 100644 index 00000000..fe8ec7ac Binary files /dev/null and b/samples/resources/Sample_10_ReadWord2007.docx differ diff --git a/samples/results/.gitkeep b/samples/results/.gitkeep new file mode 100644 index 00000000..e69de29b