2024-11-20 15:30:29 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace kernel;
|
|
|
|
|
2024-11-25 15:17:41 +03:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-11-20 15:30:29 +03:00
|
|
|
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-27 16:19:19 +03:00
|
|
|
public static function getEntityList(): array
|
2024-11-20 15:30:29 +03:00
|
|
|
{
|
2024-11-20 16:48:13 +03:00
|
|
|
$list = [];
|
2024-11-27 16:19:19 +03:00
|
|
|
$moduleService = new ModuleService();
|
|
|
|
$activeModules = $moduleService->getActiveModules();
|
2024-11-20 16:48:13 +03:00
|
|
|
foreach ($activeModules as $module) {
|
|
|
|
if (isset($module['type']) and $module['type'] === "entity") {
|
2024-11-27 16:19:19 +03:00
|
|
|
$list[$module['slug']] = $module['name'];
|
2024-11-20 16:48:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-21 13:00:21 +03:00
|
|
|
public static function removePropertyFromEntityRelations(string $entity, string $property): bool
|
2024-11-20 16:48:13 +03:00
|
|
|
{
|
2024-11-21 11:47:13 +03:00
|
|
|
$entity_relations_info = Option::where("key", "entity_relations")->first();
|
|
|
|
if ($entity_relations_info) {
|
|
|
|
$entity_relations = json_decode($entity_relations_info->value, true);
|
|
|
|
if ($entity_relations[$entity]) {
|
|
|
|
if ($entity_relations[$entity][$property]) {
|
|
|
|
unset($entity_relations[$entity][$property]);
|
|
|
|
$entity_relations_info->value = json_encode($entity_relations, JSON_UNESCAPED_UNICODE);
|
|
|
|
$entity_relations_info->save();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-20 16:48:13 +03:00
|
|
|
|
2024-11-21 11:47:13 +03:00
|
|
|
return false;
|
2024-11-20 16:48:13 +03:00
|
|
|
}
|
|
|
|
|
2024-11-21 13:00:21 +03:00
|
|
|
public static function removePropertyRelation(string $property): bool
|
|
|
|
{
|
|
|
|
$entity_relations_info = Option::where("key", "entity_relations")->first();
|
|
|
|
if ($entity_relations_info) {
|
|
|
|
$entity_relations = json_decode($entity_relations_info->value, true);
|
|
|
|
foreach ($entity_relations as $entity => $entity_relation){
|
|
|
|
if (in_array($property, $entity_relation)) {
|
|
|
|
$index = array_search($property, $entity_relation);
|
|
|
|
unset($entity_relations[$entity][$index]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$entity_relations_info->value = json_encode($entity_relations, JSON_UNESCAPED_UNICODE);
|
|
|
|
$entity_relations_info->save();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function removeEntityRelation(string $entity): bool
|
|
|
|
{
|
|
|
|
$entity_relations_info = Option::where("key", "entity_relations")->first();
|
|
|
|
if ($entity_relations_info) {
|
|
|
|
$entity_relations = json_decode($entity_relations_info->value, true);
|
|
|
|
if (isset($entity_relations[$entity])) {
|
|
|
|
unset($entity_relations[$entity]);
|
|
|
|
$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 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
|
|
|
}
|
|
|
|
|
2024-11-21 11:47:13 +03:00
|
|
|
public function addEntityRelation(string $entity, string $property): bool
|
2024-11-20 15:30:29 +03:00
|
|
|
{
|
|
|
|
$entity_relations_info = Option::where("key", "entity_relations")->first();
|
|
|
|
if ($entity_relations_info) {
|
|
|
|
$entity_relations = json_decode($entity_relations_info->value, true);
|
2024-11-21 11:47:13 +03:00
|
|
|
if (isset($entity_relations[$entity])) {
|
|
|
|
$entity_relations[$entity][] = $property;
|
|
|
|
} else {
|
|
|
|
$entity_relations[$entity] = $property;
|
2024-11-20 15:30:29 +03:00
|
|
|
}
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2024-11-25 15:17:41 +03:00
|
|
|
public function renderFormInputsBySlug(string $entity, string $slug, Model $model): void
|
2024-11-20 16:48:13 +03:00
|
|
|
{
|
|
|
|
$moduleClass = $this->getAdditionalPropertyClassBySlug($slug);
|
|
|
|
if ($moduleClass and method_exists($moduleClass, "formInputs")) {
|
2024-11-25 15:17:41 +03:00
|
|
|
$moduleClass->formInputs($entity, $model);
|
2024-11-20 16:48:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-25 15:17:41 +03:00
|
|
|
public function renderEntityAdditionalPropertyFormBySlug(string $entity, Model $model = null): void
|
2024-11-20 16:48:13 +03:00
|
|
|
{
|
2024-11-25 15:17:41 +03:00
|
|
|
$relations = $this->getEntityRelationsBySlug($entity);
|
2024-11-20 16:48:13 +03:00
|
|
|
if ($relations){
|
|
|
|
foreach ($relations as $relation){
|
2024-11-25 15:17:41 +03:00
|
|
|
$this->renderFormInputsBySlug($entity, $relation, $model);
|
2024-11-20 16:48:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-21 13:00:21 +03:00
|
|
|
|
2024-11-25 15:17:41 +03:00
|
|
|
public function saveEntityRelationBySlug(string $slug, string $entity, Model $model, Request $request): void
|
2024-11-21 13:00:21 +03:00
|
|
|
{
|
|
|
|
$moduleClass = $this->getAdditionalPropertyClassBySlug($slug);
|
|
|
|
if ($moduleClass and method_exists($moduleClass, "saveInputs")) {
|
2024-11-27 14:15:55 +03:00
|
|
|
$moduleClass->saveInputs($entity, $model, $request);
|
2024-11-21 13:00:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-25 15:17:41 +03:00
|
|
|
public function saveEntityRelation(string $entity, Model $model, Request $request): void
|
2024-11-21 13:00:21 +03:00
|
|
|
{
|
|
|
|
$relations = $this->getEntityRelationsBySlug($entity);
|
|
|
|
if ($relations){
|
|
|
|
foreach ($relations as $relation){
|
2024-11-25 15:17:41 +03:00
|
|
|
$this->saveEntityRelationBySlug($relation, $entity, $model, $request);
|
2024-11-21 13:00:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-27 17:11:06 +03:00
|
|
|
|
|
|
|
public function getEntityAdditionalProperty(string $entity, Model $model): array
|
|
|
|
{
|
|
|
|
$relations = $this->getEntityRelationsBySlug($entity);
|
|
|
|
if ($relations){
|
2024-11-28 12:15:30 +03:00
|
|
|
$relationsArr = [];
|
2024-11-27 17:11:06 +03:00
|
|
|
foreach ($relations as $relation){
|
|
|
|
$moduleClass = $this->getAdditionalPropertyClassBySlug($relation);
|
|
|
|
if ($moduleClass and method_exists($moduleClass, "getItems")) {
|
2024-11-28 12:15:30 +03:00
|
|
|
$relationsArr[$relation] = $moduleClass->getItems($entity, $model);
|
2024-11-27 17:11:06 +03:00
|
|
|
}
|
|
|
|
}
|
2024-11-28 12:15:30 +03:00
|
|
|
|
|
|
|
return $relationsArr;
|
2024-11-27 17:11:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
2024-11-28 15:34:57 +03:00
|
|
|
|
|
|
|
public function deleteEntityRelationBySlug(string $slug, string $entity, Model $model): void
|
|
|
|
{
|
|
|
|
$moduleClass = $this->getAdditionalPropertyClassBySlug($slug);
|
|
|
|
if ($moduleClass and method_exists($moduleClass, "deleteItems")) {
|
|
|
|
$moduleClass->deleteItems($entity, $model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function deleteEntityRelation(string $entity, Model $model): void
|
|
|
|
{
|
|
|
|
$relations = $this->getEntityRelationsBySlug($entity);
|
|
|
|
if ($relations){
|
|
|
|
foreach ($relations as $relation){
|
|
|
|
$this->deleteEntityRelationBySlug($relation, $entity, $model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-20 15:30:29 +03:00
|
|
|
}
|