content tables
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
<?php namespace Lucent\Core\Query;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Lucent\Core\Data\RecordPreview;
|
||||
use Lucent\Core\Data\QueryRecord;
|
||||
use Lucent\Core\Data\RecordMode;
|
||||
use Lucent\Core\Record\RecordModule;
|
||||
use Lucent\Core\Record\RecordFieldModule;
|
||||
use stdClass;
|
||||
|
||||
class QueryModule
|
||||
{
|
||||
public static function withSchema($query, string $schemaId)
|
||||
{
|
||||
return $query->where("records.schema_id", "=", $schemaId);
|
||||
}
|
||||
|
||||
public static function withSort($query, string $fieldId, string $sortDir)
|
||||
{
|
||||
return $query
|
||||
->join("records_data as sortData", function ($join) use (
|
||||
$fieldId,
|
||||
$sortDir,
|
||||
) {
|
||||
$join
|
||||
->on("records.id", "=", "sortData.record_id")
|
||||
->where("sortData.mode", "=", RecordMode::DRAFT)
|
||||
->where("sortData.locale", "=", "main")
|
||||
->where("sortData.field_id", "=", $fieldId);
|
||||
})
|
||||
->orderBy("sortData.value", $sortDir)
|
||||
->distinct();
|
||||
}
|
||||
|
||||
// public static function withColumns($query, array $columns)
|
||||
// {
|
||||
// return $query
|
||||
// ->join("records_data as includeData", function ($join) use (
|
||||
// $columns,
|
||||
// ) {
|
||||
// $join
|
||||
// ->on("records.id", "=", "includeData.record_id")
|
||||
// ->where("includeData.mode", "=", RecordMode::DRAFT)
|
||||
// ->where("includeData.locale", "=", "main")
|
||||
// ->whereIn("includeData.field_id", $columns);
|
||||
// })
|
||||
// ->distinct();
|
||||
// }
|
||||
|
||||
public static function run($query, array $schemas, array $columns)
|
||||
{
|
||||
$records = $query->get();
|
||||
|
||||
$recordIds = $records->pluck("id")->toArray();
|
||||
$recordData = DB::table("records_data")
|
||||
->whereIn("record_id", $recordIds)
|
||||
->whereIn("field_id", $columns)
|
||||
->where("mode", "=", RecordMode::DRAFT)
|
||||
->where("locale", "=", "main")
|
||||
->get()
|
||||
->map(RecordFieldModule::fromDb(...))
|
||||
->toArray();
|
||||
|
||||
return $records->map(
|
||||
fn($row) => static::fromDB($row, $schemas, $recordData),
|
||||
);
|
||||
}
|
||||
|
||||
/** @return QueryRecord[] */
|
||||
public static function make()
|
||||
{
|
||||
return DB::table("records")
|
||||
->select("records.*", "records_data.value as record_title")
|
||||
->join("records_data", function ($join) {
|
||||
$join
|
||||
->on("records.id", "=", "records_data.record_id")
|
||||
->on(
|
||||
"records.title_field_id",
|
||||
"=",
|
||||
"records_data.field_id",
|
||||
);
|
||||
})
|
||||
->where("records_data.mode", "=", RecordMode::DRAFT)
|
||||
->where("records_data.locale", "=", "main");
|
||||
// ->leftJoin(
|
||||
// "records_data",
|
||||
// "records.id",
|
||||
// "=",
|
||||
// "records_data.record_id",
|
||||
// )
|
||||
}
|
||||
|
||||
/** @param array $data */
|
||||
private static function fromDB(
|
||||
stdClass $row,
|
||||
$schemas,
|
||||
array $recordData,
|
||||
): QueryRecord {
|
||||
return new QueryRecord(
|
||||
null,
|
||||
RecordModule::fromDb($row),
|
||||
new RecordPreview(
|
||||
id: $row->id,
|
||||
schemaId: $row->schema_id,
|
||||
schemaName: collect($schemas)
|
||||
->where("id", data_get($row, "schema_id"))
|
||||
->first()->name,
|
||||
title: $row->record_title,
|
||||
),
|
||||
collect($recordData)
|
||||
->where("recordId", $row->id)
|
||||
->values()
|
||||
->toArray(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user