diff --git a/Classes/PHPWord/DocumentProperties.php b/Classes/PHPWord/DocumentProperties.php index 7c2aec24..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; } + + /** + * 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/Reader/Word2007.php b/Classes/PHPWord/Reader/Word2007.php index ebe5ef9c..c1886e42 100644 --- a/Classes/PHPWord/Reader/Word2007.php +++ b/Classes/PHPWord/Reader/Word2007.php @@ -38,7 +38,6 @@ if (!defined('PHPWORD_BASE_PATH')) { class PHPWord_Reader_Word2007 extends PHPWord_Reader_Abstract implements PHPWord_Reader_IReader { - /** * Create a new PHPWord_Reader_Word2007 instance */ @@ -57,8 +56,7 @@ class PHPWord_Reader_Word2007 extends PHPWord_Reader_Abstract implements // Check if file exists if (!file_exists($pFilename)) { throw new PHPWord_Exception( - "Could not open " . $pFilename . - " for reading! File does not exist." + "Could not open {$pFilename} for reading! File does not exist." ); } @@ -123,16 +121,13 @@ class PHPWord_Reader_Word2007 extends PHPWord_Reader_Abstract implements * Loads PHPWord from file * * @param string $pFilename - * @return PHPWord + * @return PHPWord|null */ public function load($pFilename) { - // Check if file exists - if (!file_exists($pFilename)) { - throw new PHPWord_Exception( - "Could not open " . $pFilename . - " for reading! File does not exist." - ); + // Check if file exists and can be read + if (!$this->canRead($pFilename)) { + return; } // Initialisations @@ -211,7 +206,7 @@ class PHPWord_Reader_Word2007 extends PHPWord_Reader_Abstract implements foreach ($xmlDoc->body->children() as $elm) { $elmName = $elm->getName(); if ($elmName == 'p') { // Paragraph/section - // Create new section if section section found + // Create new section if section setting found if ($elm->pPr->sectPr) { $section->setSettings($this->loadSectionSettings($elm->pPr)); $section = $word->createSection(); @@ -262,8 +257,8 @@ class PHPWord_Reader_Word2007 extends PHPWord_Reader_Abstract implements } unset($pStyle); unset($fStyle); - $hasParagraphStyle = $elm->pPr && ($elm->pPr != ''); - $hasFontStyle = $elm->rPr && ($elm->rPr != ''); + $hasParagraphStyle = isset($elm->pPr); + $hasFontStyle = isset($elm->rPr); $styleName = (string)$elm->name['val']; if ($hasParagraphStyle) { $pStyle = $this->loadParagraphStyle($elm); diff --git a/Classes/PHPWord/Writer/RTF.php b/Classes/PHPWord/Writer/RTF.php index c730c9c3..66ec7383 100755 --- a/Classes/PHPWord/Writer/RTF.php +++ b/Classes/PHPWord/Writer/RTF.php @@ -411,7 +411,7 @@ class PHPWord_Writer_RTF implements PHPWord_Writer_IWriter if ($this->_lastParagraphStyle != '' || $styleFont) { $sRTFText .= ' '; } - $sRTFText .= $text->getDataContentText(); + $sRTFText .= $text->getText(); if ($styleFont) { $sRTFText .= '\cf0'; diff --git a/Tests/PHPWord/Reader/Word2007.php b/Tests/PHPWord/Reader/Word2007.php new file mode 100644 index 00000000..cbd500ff --- /dev/null +++ b/Tests/PHPWord/Reader/Word2007.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/_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 c9dd88f8..48e48ae2 100644 --- a/Tests/_inc/TestHelperDOCX.php +++ b/Tests/_inc/TestHelperDOCX.php @@ -2,7 +2,6 @@ namespace PHPWord\Tests; use PHPWord; -use DOMDocument; class TestHelperDOCX { @@ -10,7 +9,7 @@ class TestHelperDOCX /** * @param \PHPWord $PHPWord - * @return \PHPWord\Tests\Xml_Document + * @return \PHPWord\Tests\XmlDocument */ public static function getDocument(PHPWord $PHPWord) { @@ -29,7 +28,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() @@ -60,65 +59,3 @@ class TestHelperDOCX 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 - */ - public function __construct($path) - { - $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); - } -} diff --git a/Tests/_inc/XmlDocument.php b/Tests/_inc/XmlDocument.php new file mode 100644 index 00000000..2ec27a3b --- /dev/null +++ b/Tests/_inc/XmlDocument.php @@ -0,0 +1,66 @@ +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); + } +} 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';