Permalink
Browse files

Excel, CSV, HTML Export Type updates

  • Loading branch information...
1 parent 2213397 commit eb4ab64b1cc75370cb97e8fc6cca54c16861a3c1 @benkeen committed Jan 28, 2015
View
@@ -25,11 +25,9 @@
} else {
// Export Types may choose to return a "headers" string or array containing headers to output
-
- // TODO think about when not exhausted
-// if ($response["headers"]) {
-// header("Content-Type: {$response["contentTypeHeader"]}");
-// }
+ if ($response["contentTypeHeader"]) {
+ header("Content-Type: {$response["contentTypeHeader"]}");
+ }
echo $response["content"];
}
@@ -58,8 +58,6 @@ public function getDownloadFilename($generator) {
}
public function getAdditionalSettingsHTML() {
- $LANG = Core::$language->getCurrentLanguageStrings();
-
$html =<<< END
<table cellspacing="0" cellpadding="0" width="100%">
<tr>
@@ -43,27 +43,27 @@ function generate($generator) {
}
}
- //We'll need to check if the compression option is turned on. And then execute this code - unullmass
+ // we'll need to check if the compression option is turned on. And then execute this code - unullmass
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
//get the name of the save file
$filepath=$this->getDownloadFilename($generator);
//save the excel data to that file
$objWriter->save($filepath);
if (!($generator->isPromptDownloadZipped())) {
-
// redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
- header('Content-Disposition: attachment;filename="'.$filepath.'"');
+ header('Content-Disposition: attachment;filename="' . $filepath . '"');
header('Cache-Control: max-age=0');
readfile($filepath);
+ @unlink($filepath);
} else {
- //create archive and send back
- $zippath=$filepath.".zip";
+ // create archive and send back
+ $zippath = $filepath . ".zip";
$zip = new ZipArchive();
- $zipfile = $zip->open($zippath,ZipArchive::CREATE);
+ $zipfile = $zip->open($zippath, ZipArchive::CREATE);
if ($zipfile) {
- if ($zip->addFile($filepath,$filepath)) {
+ if ($zip->addFile($filepath, $filepath)) {
//we've got our zip file now we may set the response header
$zip->close();
header("Cache-Control: private, no-cache, must-revalidate");
@@ -72,7 +72,6 @@ function generate($generator) {
readfile($zippath);
unlink($zippath);
unlink($filepath);
- //exit sending the zip back
exit;
}
}
@@ -25,16 +25,20 @@ public function __construct($runtimeContext) {
* @see ExportTypePlugin::generate()
*/
public function generate($generator) {
+ $this->genEnvironment = $generator->genEnvironment; // API / POST
+ $this->userSettings = $generator->getUserSettings();
+
$exportTarget = $generator->getExportTarget();
- $postData = $generator->getUserSettings();
$data = $generator->generateExportData();
$content = "";
- $htmlFormat = (isset($postData["etHTMLExportFormat"])) ? $postData["etHTMLExportFormat"] : "custom";
+ $format = $this->getExportFormat();
+
+ if ($format == "custom") {
+ $template = $this->getCustomTemplate();
+ error_log($template);
- if ($htmlFormat == "custom") {
- $smartyTemplate = (get_magic_quotes_gpc()) ? stripslashes($postData["etHTMLCustomHTMLSource"]) : $postData["etHTMLCustomHTMLSource"];
- $content .= $this->genFormatCustom($data, $smartyTemplate);
+ $content .= $this->genFormatCustom($data, $template);
} else {
// if we're generating the data in the context of a new window/tab, include the additional
@@ -43,7 +47,7 @@ public function generate($generator) {
$content .= $this->generateExportHeader();
}
- switch ($htmlFormat) {
+ switch ($format) {
case "table":
$content .= $this->genFormatTable($data);
break;
@@ -237,6 +241,7 @@ private function genFormatDl($data) {
* Generates the data in whatever Smarty content the user entered.
* @param array $data
* @param string $template
+ * @return string
*/
private function genFormatCustom($data, $template) {
return Templates::evalSmartyString($template, $data);
@@ -266,4 +271,20 @@ private function generateExportHeader() {
private function generateExportFooter() {
return "</body></html>";
}
+
+ private function getExportFormat() {
+ if ($this->genEnvironment == GEN_ENVIRONMENT_API) {
+ return $this->userSettings->export->settings->exportFormat;
+ } else {
+ return (isset($this->userSettings["etHTMLExportFormat"])) ? $this->userSettings["etHTMLExportFormat"] : "custom";
+ }
+ }
+
+ private function getCustomTemplate() {
+ if ($this->genEnvironment == GEN_ENVIRONMENT_API) {
+ return (property_exists($this->userSettings->export->settings, "customTemplate")) ? $this->userSettings->export->settings->customTemplate : "";
+ } else {
+ return (get_magic_quotes_gpc()) ? stripslashes($this->userSettings["etHTMLCustomHTMLSource"]) : $this->userSettings["etHTMLCustomHTMLSource"];
+ }
+ }
}
@@ -0,0 +1,82 @@
+## HTML Export Type
+
+This plugin outputs the data in HTML format. You can either choose to output the data in a `<table>`, a `<ul>` or
+`<dl>`'s, or if you're feeling really fancy, you can control the exact output via a custom Smarty template. The
+latter's intended for advanced users and requires knowledge of the PHP Smarty templating language
+
+### Example API Usage
+
+Try POSTing the following JSON content to the following URL:
+`http://[your site]/[generate data folder]/api/v1/data`
+
+```javascript
+{
+ "numRows": 10,
+ "rows": [
+ {
+ "type": "AutoIncrement",
+ "title": "Row",
+ "settings": {
+ "incrementStart": 1,
+ "incrementValue": 1
+ }
+ },
+ {
+ "type": "Names",
+ "title": "name",
+ "settings": {
+ "placeholder": "Name Initial. Surname"
+ }
+ }
+ ],
+ "export": {
+ "type": "HTML",
+ "settings": {
+ "exportFormat": "table"
+ }
+ }
+}
+```
+
+### Custom template example
+
+Here's a second example that uses a custom Smarty template to customize the generated HTML. Note: the inconvenient
+thing about using the API for this is that you need to serialize the template into a single line. Yeah, that's a pain.
+
+
+```javascript
+{
+ "numRows": 10,
+ "rows": [
+ {
+ "type": "AutoIncrement",
+ "title": "Row",
+ "settings": {
+ "incrementStart": 1,
+ "incrementValue": 1
+ }
+ },
+ {
+ "type": "Names",
+ "title": "name",
+ "settings": {
+ "placeholder": "Name Initial. Surname"
+ }
+ }
+ ],
+ "export": {
+ "type": "HTML",
+ "settings": {
+ "exportFormat": "custom",
+ "customTemplate": "{if $isFirstBatch}<table cellspacing=\"0\" cellpadding=\"1\">\n<tr>\n{foreach $colData as $col} <th>{$col}</th>\n{/foreach}\n</tr>\n{/if}{foreach $rowData as $row}<tr>\n{foreach $row as $r} <td>{$r}</td>\n{/foreach}</tr>\n{/foreach}{if $isLastBatch}</table>{/if}"
+ }
+ }
+}
+```
+
+
+### API help
+
+For more information about the API, check out:
+[http://benkeen.github.io/generatedata/api.html](http://benkeen.github.io/generatedata/api.html)
+
@@ -3,12 +3,12 @@
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
- "delimiter": {
- "type": "string"
+ "exportFormat": {
+ "enum": ["table", "ul", "dl", "custom"]
},
- "eol": { // TODO default?
- "enum": ["Windows", "Unix", "Mac"]
+ "customTemplate": {
+ "type": "string"
}
},
- "required": ["delimiter", "eol"]
-}
+ "required": ["exportFormat"]
+}
@@ -36,7 +36,7 @@ public function data() {
$response = $gen->generate();
if ($response["success"]) {
- echo $response["content"];
+ return $response;
}
}
@@ -145,6 +145,7 @@ private function initAPIGenerator($json) {
$this->createDataSetTemplateAPI($json);
$this->exportType = ExportTypePluginHelper::getExportTypeByFolder($json->export->type);
+ $this->isCompressionRequired = false;
}

0 comments on commit eb4ab64

Please sign in to comment.