Compare commits

...

2 Commits

Author SHA1 Message Date
lexx 9d5d4dd930 csv relations 2024-12-14 18:56:04 +02:00
lexx c507dc6031 upload fix 2024-10-23 19:34:41 +03:00
2 changed files with 37 additions and 3 deletions
+3
View File
@@ -79,7 +79,10 @@ class FileService
throw new LucentException("File $filename not uploaded");
}
if($this->isImage($mimetype)){
$this->createTemplates($disk, $path, $file);
}
list($width, $height) = $this->isImage($mimetype) ? getimagesize($file) : [0, 0];
$recordFile = new RecordFile(
+33 -2
View File
@@ -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())) {