47 lines
1.3 KiB
PHP
47 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace Lucent\ViewModel;
|
|
|
|
use Lucent\Channel\ChannelService;
|
|
use Lucent\Record\QueryRecord;
|
|
use Lucent\Schema\CollectionSchema;
|
|
use Lucent\Schema\FieldInterface;
|
|
use Lucent\Schema\FilesSchema;
|
|
use Mustache_Engine;
|
|
|
|
class ViewModel
|
|
{
|
|
public function __construct(
|
|
public ChannelService $channelService
|
|
)
|
|
{
|
|
}
|
|
|
|
|
|
public function getRecordName(QueryRecord $record): string
|
|
{
|
|
$schema = $this->channelService->getSchema($record->schema)->get();
|
|
if (empty($schema->titleTemplate)) {
|
|
$title = match (get_class($schema)) {
|
|
CollectionSchema::class => $record->data[$schema->fields->filter(fn(FieldInterface $f) => $f->info->name === "text")->first()->name],
|
|
FilesSchema::class => $record->_file->path,
|
|
};
|
|
|
|
if (empty(trim($title))) {
|
|
return "~Untitled~";
|
|
}
|
|
|
|
return $title;
|
|
}
|
|
|
|
$m = new Mustache_Engine(array('entity_flags' => ENT_QUOTES));
|
|
return $m->render($schema->cardTitle, $record->data);
|
|
}
|
|
|
|
public function renderRow(QueryRecord $record, FieldInterface $field): string
|
|
{
|
|
$renderers = config("lucent.renderers.row");
|
|
return (new $renderers[$field->info->name])($record, $field);
|
|
}
|
|
|
|
} |