new export options

pull/26/head
Ondřej Hruška 6 years ago
parent 8047a0487f
commit 8f76ce3569
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 13
      app/Http/Controllers/TableController.php
  2. 24
      app/Tables/BaseExporter.php
  3. 10
      app/Tables/CStructArrayExporter.php
  4. 12
      app/Tables/CsvExporter.php
  5. 18
      app/Tables/JsonExporter.php
  6. 64
      app/Tables/PhpExporter.php
  7. 3
      resources/views/table/_panel-export.blade.php

@ -10,6 +10,7 @@ use App\Tables\Column;
use App\Tables\CStructArrayExporter;
use App\Tables\CsvExporter;
use App\Tables\JsonExporter;
use App\Tables\PhpExporter;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use MightyPork\Exceptions\NotApplicableException;
@ -280,10 +281,22 @@ class TableController extends Controller
$exporter = new CsvExporter($tableModel);
break;
case 'csv-tab':
$exporter = (new CsvExporter($tableModel))->withDelimiter("\t");
break;
case 'c':
$exporter = new CStructArrayExporter($tableModel);
break;
case 'php':
$exporter = new PhpExporter($tableModel);
break;
case 'js':
$exporter = (new JsonExporter($tableModel))->withJsWrapper();
break;
default:
abort(400, "Unspecified or unknown format.");
}

@ -37,6 +37,30 @@ abstract class BaseExporter
$this->columns = Column::columnsFromJson($table->revision->columns);
}
protected function getHeaderComment($prefix = " ")
{
$table = $this->table;
$owner = $this->table->owner;
$s =
$prefix."Table \"$table->title\" by $owner->title\n".
$prefix."exported ".date('M n, Y \\a\\t G:i')."\n".
$prefix."\n".
$prefix."License: ". str_replace("\n", "\n$prefix", $table->license ?: 'CC0') ."\n".
$prefix."\n".
$prefix."Upstream: https://datatable.directory/$owner->handle/$table->name\n";
$s .=
$prefix."\n".
$prefix."Columns:\n";
foreach ($this->columns as $c) {
$s .= $prefix." - $c->name ... $c->title\n";
}
return $s;
}
/**
* @return string - mime type for the downloaded file
*/

@ -36,6 +36,8 @@ class CStructArrayExporter extends BaseExporter
*/
protected function writeDocument()
{
$table = $this->table;
$any_bool = false;
$fields = array_map(function (Column $c) use (&$any_bool) {
$type = $c->type;
@ -50,13 +52,15 @@ class CStructArrayExporter extends BaseExporter
return (object)['name' => $c->name, 'type' => $c->type, 'ctype' => $type, 'cname' => $name];
}, $this->columns);
$ctablename = $this->cify($this->table->name);
$ctablename = $this->cify($table->name);
// preamble
if ($any_bool) {
echo "#include <stdbool.h>\n\n";
}
echo "/*\n" . $this->getHeaderComment(' ') . "*/\n\n";
echo "struct " . $ctablename . " {\n";
foreach ($fields as $field) {
echo " " . $field->ctype . " " . $field->cname . ";\n";
@ -74,7 +78,7 @@ class CStructArrayExporter extends BaseExporter
echo ",\n";
}
echo " {";
echo " { ";
$firstf = true;
foreach ($fields as $field) {
@ -115,7 +119,7 @@ class CStructArrayExporter extends BaseExporter
break;
}
}
echo "}";
echo " }";
}
echo "\n};\n";

@ -6,6 +6,14 @@ namespace App\Tables;
class CsvExporter extends BaseExporter
{
private $delimiter = ',';
public function withDelimiter($delimiter)
{
$this->delimiter = $delimiter;
return $this;
}
/**
* @return string - mime type for the downloaded file
*/
@ -33,7 +41,7 @@ class CsvExporter extends BaseExporter
return $c->title;
}, $this->columns);
fputcsv($handle, $columnNames);
fputcsv($handle, $columnNames, $this->delimiter);
foreach ($this->iterateRows() as $row) {
$items = [];
@ -44,7 +52,7 @@ class CsvExporter extends BaseExporter
$items[] = null;
}
}
fputcsv($handle, $items);
fputcsv($handle, $items, $this->delimiter);
}
fclose($handle);

@ -6,12 +6,20 @@ namespace App\Tables;
class JsonExporter extends BaseExporter
{
private $fullJsDoc = false;
public function withJsWrapper()
{
$this->fullJsDoc = true;
return $this;
}
/**
* @return string - mime type for the downloaded file
*/
protected function getMimeType()
{
return 'application/json';
return $this->fullJsDoc ? 'application/javascript' : 'application/json';
}
/**
@ -27,6 +35,12 @@ class JsonExporter extends BaseExporter
*/
protected function writeDocument()
{
if ($this->fullJsDoc) {
echo "/*\n" . $this->getHeaderComment(' ') . "*/\n\n";
echo 'module.exports = ';
}
echo "[\n";
$first = true;
@ -36,7 +50,7 @@ class JsonExporter extends BaseExporter
} else {
echo ",\n";
}
echo json_encode($row, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
echo ' '.json_encode($row, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
}
echo "\n]\n";

@ -0,0 +1,64 @@
<?php
namespace App\Tables;
class PhpExporter extends BaseExporter
{
/**
* @return string - mime type for the downloaded file
*/
protected function getMimeType()
{
return $this->wantDownload ? 'application/x-php' : 'text/plain';
}
/**
* @return string - file extension for the downloaded file
*/
protected function getFileExtension()
{
return 'php';
}
/**
* Write the document to stdout ('php://output')
*/
protected function writeDocument()
{
echo "<?php\n\n";
echo "/*\n" . $this->getHeaderComment(' ') . "*/\n\n";
echo "return [\n";
$first = true;
foreach ($this->iterateRows() as $row) {
if ($first) {
$first = false;
} else {
echo ",\n";
}
echo ' [';
$firstf = true;
foreach ($this->columns as $column) {
if ($firstf) {
$firstf = false;
} else {
echo ", ";
}
var_export($column->name);
echo ' => ';
var_export($row->{$column->name});
}
echo ']';
}
echo "\n];\n";
}
}

@ -3,7 +3,10 @@
<ul class="mt-2">
@foreach([
'json' => 'JSON array of objects',
'js' => 'JavaScript module',
'php' => 'PHP include',
'csv' => 'CSV table',
'csv-tab' => 'CSV table, tabbed',
'c' => 'C structs array',
] as $type => $title)

Loading…
Cancel
Save