diff --git a/README.md b/README.md index 77f4a98..cef58f9 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ EEDataExporter ------------- [![Build Status](https://api.travis-ci.org/EE/DataExporter.png?branch=master)](http://travis-ci.org/EE/DataExporter) -Easy export data to CSV, XML, JSON, Excel, HTML file or into memory. +Easy export data to CSV, XML, JSON, Excel, HTML, DOCX(with image) file or into memory. [Read the Documentation and see examples](https://github.com/EE/DataExporter/blob/master/Resources/doc/index.md) diff --git a/Resources/config/services.yml b/Resources/config/services.yml index bfa3e7f..729ed4e 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -4,5 +4,4 @@ parameters: services: ee.dataexporter: class: %ee.dataexporter.class% - public: true - scope: prototype + public: true \ No newline at end of file diff --git a/Service/DataExporter.php b/Service/DataExporter.php index 7a100fd..6ee5622 100644 --- a/Service/DataExporter.php +++ b/Service/DataExporter.php @@ -50,11 +50,17 @@ class DataExporter /** * @var array */ - protected $supportedFormat = array('csv', 'xls', 'html', 'xml', 'json'); + protected $supportedFormat = array('csv', 'xls', 'html', 'xml', 'json', 'docx'); + /** * @var array */ protected $hooks = array(); + /** + * + * @var type + */ + protected $images = array('.jpeg', '.png', '.jpg'); /** * @param $format @@ -83,6 +89,9 @@ public function setOptions($format, $options = array()) } elseif ('html' === $this->format) { //options for html $this->openHTML(); + } elseif ('docx' === $this->format) { + //options for html + $this->openDOCX(); } elseif ('xml' === $this->format) { //options for xml $this->openXML(); @@ -177,6 +186,27 @@ public function closeHTML() return $this; } + + /** + * @return $this + */ + public function openDOCX() + { + $this->data = ""; + $this->data .= ""; + $this->data .= ""; + return $this; + + } + /** + * @return $this + */ + public function closeDOCX() + { + $this->data .= "
"; + + return $this; + } /** * @param $data @@ -298,6 +328,11 @@ public function setData($rows) break; case 'xls': case 'html': + case 'docx': + if(in_array('skip', $this->columns)){ + $key = array_search('skip', $this->columns); + unset($this->columns[$key]); + } case 'xml': $tempRow = ''; break; @@ -329,6 +364,17 @@ function ($column) use ($row, $accessor, $separator, $escape, $hooks, $format) { $this->data .= ''; foreach ($tempRow as $val) { $this->data .= '' . $val . ''; + } + $this->data .= ''; + break; + case 'docx': + $this->data .= ''; + foreach ($tempRow as $val) { + if (is_int($this->strposArray($val, $this->images))) { + $this->data .= ''; + } else { + $this->data .= '' . $val . ''; + } } $this->data .= ''; break; @@ -381,6 +427,21 @@ public function setColumns(Array $columns) } else { $this->data[] = $column . $this->separator; } + } elseif ('docx' === $this->format) { + if (!in_array('skip', $columns)) { + //first item + reset($columns); + if ($key === key($columns)) { + $this->data .= ''; + } + + $this->data .= sprintf('%s', $column); + //last item + end($columns); + if ($key === key($columns)) { + $this->data .= ''; + } + } } elseif ('xls' === $this->format || 'html' === $this->format) { //first item reset($columns); @@ -450,6 +511,12 @@ public function render() $response->headers->set('Content-Type', 'application/xml'); $response->setContent($this->data); break; + case 'docx': + //close tags + $this->closeDOCX(); + $response->headers->set('Content-Type', 'application/vnd.ms-word'); + $response->setContent($this->data); + break; } if ($this->memory) { @@ -460,6 +527,24 @@ public function render() $response->headers->set('Content-Disposition', 'attachment; filename="' . $this->fileName . '"'); return $response; - } + + private function strposArray($haystack, $needles) + { + if ( is_array($needles) ) { + foreach ($needles as $str) { + if ( is_array($str) ) { + $pos = $this->strposArray($haystack, $str); + } else { + $pos = strpos($haystack, $str); + } + if (false !== $pos) { + return $pos; + } + } + } else { + return strpos($haystack, $needles); + } + } + } diff --git a/Tests/DataExporterTest.php b/Tests/DataExporterTest.php index 165e1b2..384d50b 100644 --- a/Tests/DataExporterTest.php +++ b/Tests/DataExporterTest.php @@ -85,7 +85,25 @@ public function testHTMLExport() $this->assertEquals($result, $exporter->render()->getContent()); } + public function testDOCXExport() + { + $exporter = new DataExporter(); + $exporter->setOptions('docx', array('fileName' => 'file')); + $exporter->setColumns(array('[col1]' => 'Column 1', '[col2]' => 'Column 2', '[col3]' => 'Column 3')); + $exporter->setData( + array( + array('col1' => '1a', 'col2' => '1b', 'col3' => '1c'), + array('col1' => '2a', 'col2' => '2b'), + ) + ); + $result = '' + . '' + . '' + . '
Column 1Column 2Column 3
1a1b1c
2a2b
'; + + $this->assertEquals($result, $exporter->render()->getContent()); + } public function testXMLExport() { $exporter = new DataExporter(); diff --git a/composer.json b/composer.json index d006412..6ab4ba4 100644 --- a/composer.json +++ b/composer.json @@ -1,8 +1,8 @@ { "name": "ee/dataexporter-bundle", "type": "symfony-bundle", - "description": "Easy export data to CSV, XML, HTML, JSON or XLS", - "keywords": ["exporter","csv","xls", "xml", "json"], + "description": "Easy export data to CSV, XML, HTML, JSON, DOCX or XLS", + "keywords": ["exporter","csv","xls", "xml", "json", "docx"], "homepage": "http://laboratorium.ee", "license": "MIT", "authors": [