2024-11-20 15:30:29 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace kernel;
|
|
|
|
|
|
|
|
use kernel\helpers\Debug;
|
|
|
|
use kernel\models\Option;
|
2024-11-20 16:48:13 +03:00
|
|
|
use kernel\modules\option\service\OptionService;
|
|
|
|
use kernel\services\ModuleService;
|
2024-11-20 15:30:29 +03:00
|
|
|
|
|
|
|
class EntityRelation
|
|
|
|
{
|
2024-11-20 16:48:13 +03:00
|
|
|
protected ModuleService $moduleService;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->moduleService = new ModuleService();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-11-20 15:30:29 +03:00
|
|
|
public function getEntityList(): array
|
|
|
|
{
|
2024-11-20 16:48:13 +03:00
|
|
|
$list = [];
|
|
|
|
$activeModules = $this->moduleService->getActiveModules();
|
|
|
|
foreach ($activeModules as $module) {
|
|
|
|
if (isset($module['type']) and $module['type'] === "entity") {
|
|
|
|
$list[] = $module['slug'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAdditionalPropertyList(): array
|
|
|
|
{
|
|
|
|
$list = [];
|
|
|
|
$activeModules = $this->moduleService->getActiveModules();
|
|
|
|
foreach ($activeModules as $module) {
|
|
|
|
if (isset($module['type']) and $module['type'] === "additional_property") {
|
|
|
|
$list[] = $module['slug'];
|
2024-11-20 15:30:29 +03:00
|
|
|
}
|
2024-11-20 16:48:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEntitiesRelations(): array|bool
|
|
|
|
{
|
|
|
|
$entity_relations = OptionService::getItem("entity_relations");
|
|
|
|
if ($entity_relations) {
|
|
|
|
return json_decode($entity_relations, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2024-11-20 15:30:29 +03:00
|
|
|
|
2024-11-20 16:48:13 +03:00
|
|
|
public function removeFromEntityRelations(string $entity, string $property)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEntityRelationsBySlug(string $slug)
|
|
|
|
{
|
|
|
|
$entityRelations = $this->getEntitiesRelations();
|
|
|
|
if ($entityRelations) {
|
|
|
|
if (isset($entityRelations[$slug])){
|
|
|
|
return $entityRelations[$slug];
|
|
|
|
}
|
2024-11-20 15:30:29 +03:00
|
|
|
}
|
|
|
|
|
2024-11-20 16:48:13 +03:00
|
|
|
return false;
|
2024-11-20 15:30:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function addEntityRelation(array $data): bool
|
|
|
|
{
|
|
|
|
$entity_relations_info = Option::where("key", "entity_relations")->first();
|
|
|
|
|
|
|
|
if ($entity_relations_info) {
|
|
|
|
$entity_relations = json_decode($entity_relations_info->value, true);
|
|
|
|
foreach ($data as $entity => $relation) {
|
|
|
|
$entity_relations[$entity] = $relation;
|
|
|
|
}
|
|
|
|
$entity_relations_info->value = json_encode($entity_relations, JSON_UNESCAPED_UNICODE);
|
|
|
|
$entity_relations_info->save();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2024-11-20 16:48:13 +03:00
|
|
|
|
|
|
|
public function getAdditionalPropertyClassBySlug(string $slug)
|
|
|
|
{
|
|
|
|
$module = $this->moduleService->getModuleInfoBySlug($slug);
|
|
|
|
if (isset($module['module_class'])) {
|
|
|
|
return new $module['module_class']();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function renderFormInputsBySlug(string $slug): void
|
|
|
|
{
|
|
|
|
$moduleClass = $this->getAdditionalPropertyClassBySlug($slug);
|
|
|
|
if ($moduleClass and method_exists($moduleClass, "formInputs")) {
|
|
|
|
$moduleClass->formInputs();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function renderEntityAdditionalPropertyFormBySlug(string $slug): void
|
|
|
|
{
|
|
|
|
$relations = $this->getEntityRelationsBySlug($slug);
|
|
|
|
if ($relations){
|
|
|
|
foreach ($relations as $relation){
|
|
|
|
$this->renderFormInputsBySlug($relation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-20 15:30:29 +03:00
|
|
|
}
|