|
|
|
@@ -17,6 +17,7 @@ use Lucent\Record\QueryRecord;
|
|
|
|
|
use Lucent\Record\RecordService;
|
|
|
|
|
use Lucent\Record\Status;
|
|
|
|
|
use Lucent\Schema\System;
|
|
|
|
|
use Lucent\Schema\Ui\Reference;
|
|
|
|
|
use Lucent\Schema\Validator\ValidatorException;
|
|
|
|
|
use Lucent\Svelte\Svelte;
|
|
|
|
|
use function Lucent\Response\fail;
|
|
|
|
@@ -134,18 +135,22 @@ class RecordController extends Controller
|
|
|
|
|
->filter($arguments)
|
|
|
|
|
->limit(-1)
|
|
|
|
|
->status(explode(",", $arguments["status_in"]))
|
|
|
|
|
->childrenDepth(1)
|
|
|
|
|
// ->skip($skip)
|
|
|
|
|
->sort($sort)
|
|
|
|
|
->run()
|
|
|
|
|
->records;
|
|
|
|
|
->tree();
|
|
|
|
|
|
|
|
|
|
header('Content-Type: application/csv');
|
|
|
|
|
header('Content-Disposition: attachment; filename="' . $schemaName . '.csv";');
|
|
|
|
|
$handle = fopen('php://output', 'w');
|
|
|
|
|
$csvRow = ["id", ...array_keys($records[0]->data->toArray())];
|
|
|
|
|
$relationColumns = $this->makeCsvRelationColumns($schema);
|
|
|
|
|
$csvRow = ["id", ...array_keys($records[0]->data->toArray()),...$relationColumns];
|
|
|
|
|
fputcsv($handle, $csvRow, ',');
|
|
|
|
|
foreach ($records as $record) {
|
|
|
|
|
|
|
|
|
|
$csvRow = [$record->id, ...$record->data->toArray()];
|
|
|
|
|
$csvRow = array_merge($csvRow,$this->makeCsvRelationColumnValues($schema,$record->_children));
|
|
|
|
|
$csvRow = array_values($csvRow);
|
|
|
|
|
fputcsv($handle, $csvRow, ',');
|
|
|
|
|
}
|
|
|
|
@@ -154,6 +159,32 @@ class RecordController extends Controller
|
|
|
|
|
exit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function makeCsvRelationColumns($schema):array{
|
|
|
|
|
return $schema->fields->filter(fn($f) => get_class($f) === Reference::class)->reduce(function($c,$f){
|
|
|
|
|
$c[] = $f->name." id";
|
|
|
|
|
$c[] = $f->name." name";
|
|
|
|
|
return $c;
|
|
|
|
|
},[]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function makeCsvRelationColumnValues($schema, $children):array{
|
|
|
|
|
return $schema->fields->filter(fn($f) => get_class($f) === Reference::class)->reduce(function($c,$f) use($children){
|
|
|
|
|
|
|
|
|
|
$fieldRecords = data_get($children,$f->name);
|
|
|
|
|
if(empty($fieldRecords)){
|
|
|
|
|
$c[] = "";
|
|
|
|
|
$c[] = "";
|
|
|
|
|
}elseif (count($fieldRecords) === 1){
|
|
|
|
|
$c[] = data_get($fieldRecords,"0.id");
|
|
|
|
|
$c[] = data_get($fieldRecords,"0.data.name");
|
|
|
|
|
}else{
|
|
|
|
|
$c[] = collect($fieldRecords)->pluck("id")->join("::");
|
|
|
|
|
$c[] = collect($fieldRecords)->pluck("data.name")->join("::");
|
|
|
|
|
}
|
|
|
|
|
return $c;
|
|
|
|
|
},[]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function new(Request $request)
|
|
|
|
|
{
|
|
|
|
|
if (!in_array($request->input("schema"), $this->accountService->currentWritableSchemas())) {
|
|
|
|
|