100 lines
3.4 KiB
PHP
100 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace KupShop\LLMBundle\Admin;
|
|
|
|
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
|
|
use KupShop\LLMBundle\Util\TextObjectUtil;
|
|
use Query\Operator;
|
|
|
|
class LlmPrompt extends \Window
|
|
{
|
|
protected $tableName = 'llm_prompts';
|
|
protected $nameField = 'title';
|
|
protected $template = 'window/llmPrompt.tpl';
|
|
protected $required = ['title' => true];
|
|
protected TextObjectUtil $textObjectUtil;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->textObjectUtil = ServiceContainer::getService(TextObjectUtil::class);
|
|
}
|
|
|
|
public function get_vars()
|
|
{
|
|
$vars = parent::get_vars();
|
|
|
|
$vars['body']['textObjects'] = $this->textObjectUtil->getLabelsMap();
|
|
$selectedObjects = array_map(fn () => true, sqlQueryBuilder()->select('object_type')
|
|
->from('llm_prompts_objects')
|
|
->where(Operator::equals(['id_prompt' => $this->getID()]))
|
|
->execute()->fetchAllAssociativeIndexed());
|
|
|
|
$vars['body']['disableEdit'] = !empty($vars['body']['data']['label']) && !$this->isDuplicate();
|
|
$vars['body']['textObjectsSel'] = $selectedObjects;
|
|
$vars['body']['providedPlaceholders'] = $this->getProvidedPlaceholders($selectedObjects);
|
|
|
|
return $vars;
|
|
}
|
|
|
|
public function processFormData()
|
|
{
|
|
$data = $this->getData();
|
|
|
|
if ($this->getPromptLabel() && !$this->isDuplicate()) {
|
|
$data = array_intersect_key($data, array_flip(['figure']));
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
protected function getProvidedPlaceholders(array $objectTypes): array
|
|
{
|
|
$allPlaceholders = [];
|
|
$commonPlaceholders = [];
|
|
|
|
foreach ($objectTypes as $objectType => $_) {
|
|
$objectPlaceholders = $this->textObjectUtil->getByLabel($objectType)->providesPlaceholders();
|
|
$objectPlaceholders = array_combine(array_map(fn ($obj) => $obj->name, $objectPlaceholders), $objectPlaceholders);
|
|
$commonPlaceholders = $commonPlaceholders ? array_intersect_key($commonPlaceholders, $objectPlaceholders) : $objectPlaceholders;
|
|
$allPlaceholders = array_merge($allPlaceholders, $objectPlaceholders);
|
|
}
|
|
|
|
// common -> all selected object types provide the placeholder
|
|
return array_map(fn ($val) => ['name' => $val->name, 'descr' => $val->description(), 'common' => isset($commonPlaceholders[$val->name])], $allPlaceholders);
|
|
}
|
|
|
|
protected function getPromptLabel(): ?string
|
|
{
|
|
return sqlQueryBuilder()->select('label')->from('llm_prompts')->where(Operator::equals(['id' => $this->getID()]))->execute()->fetchOne();
|
|
}
|
|
|
|
public function handleUpdate()
|
|
{
|
|
parent::handleUpdate();
|
|
$data = $this->getData();
|
|
|
|
if (!$this->getPromptLabel()) {
|
|
sqlQueryBuilder()->delete('llm_prompts_objects')->where(Operator::equals(['id_prompt' => $this->getID()]))->execute();
|
|
foreach ($data['textObjects'] as $objectType) {
|
|
sqlQueryBuilder()->insert('llm_prompts_objects')->directValues([
|
|
'id_prompt' => $this->getID(),
|
|
'object_type' => $objectType,
|
|
])->execute();
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function hasRights($name = null)
|
|
{
|
|
if (!in_array($name, [self::RIGHT_DUPLICATE, self::RIGHT_SAVE]) && !empty($this->getObjectData()['label'])) {
|
|
return false;
|
|
}
|
|
|
|
return parent::hasRights($name);
|
|
}
|
|
}
|
|
|
|
return LlmPrompt::class;
|