tags
This commit is contained in:
parent
1bc7662be0
commit
745707ee09
@ -2,12 +2,14 @@
|
||||
|
||||
namespace app\modules\tag;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use itguild\forms\builders\TextInputBuilder;
|
||||
use kernel\app_modules\tag\models\Tag;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\helpers\Slug;
|
||||
use kernel\Module;
|
||||
use kernel\modules\menu\service\MenuService;
|
||||
use kernel\modules\option\service\OptionService;
|
||||
use kernel\Request;
|
||||
use kernel\services\MigrationService;
|
||||
|
||||
@ -34,29 +36,50 @@ class TagModule extends Module
|
||||
"url" => "/admin/tag",
|
||||
"slug" => "tag",
|
||||
]);
|
||||
|
||||
$this->menuService->createItem([
|
||||
"label" => "Тэги",
|
||||
"url" => "/admin/settings/tag",
|
||||
"slug" => "tag_settings",
|
||||
"parent_slug" => "settings"
|
||||
]);
|
||||
|
||||
OptionService::createFromParams("entity_tag_list", "{}", "Список тегов");
|
||||
}
|
||||
|
||||
public function deactivate(): void
|
||||
{
|
||||
$this->menuService->removeItemBySlug("tag");
|
||||
OptionService::removeOptionByKey("entity_tag_list");
|
||||
}
|
||||
|
||||
public function formInputs(): void
|
||||
public function formInputs(string $entity, Model $model = null): void
|
||||
{
|
||||
$input = TextInputBuilder::build("tag", ['class' => 'form-control', 'placeholder' => 'Теги']);
|
||||
$tag = Tag::where("entity", $entity)->where("entity_id", $model->id)->first();
|
||||
|
||||
$input = TextInputBuilder::build("tag", [
|
||||
'class' => 'form-control',
|
||||
'placeholder' => 'Теги',
|
||||
'value' => $tag->label ?? ""
|
||||
]);
|
||||
$input->setLabel("Теги");
|
||||
$input->create()->render();
|
||||
}
|
||||
|
||||
public function saveInputs(string $entity, Request $request): void
|
||||
public function saveInputs(string $entity, Model $model, Request $request): void
|
||||
{
|
||||
$model = new Tag();
|
||||
$model->entity = $entity;
|
||||
Debug::dd($request->post());
|
||||
$model->entity_id = 1;
|
||||
$model->label = $request->post('tag');
|
||||
$model->status = Tag::ACTIVE_STATUS;
|
||||
$model->slug = Slug::createSlug($request->post('tag'), $model);
|
||||
$model->save();
|
||||
$tag = new Tag();
|
||||
$tag->entity = $entity;
|
||||
$tag->entity_id = $model->id;
|
||||
$tag->label = $request->post('tag');
|
||||
$tag->status = Tag::ACTIVE_STATUS;
|
||||
$tag->slug = Slug::createSlug($request->post('tag'), $model);
|
||||
$tag->save();
|
||||
}
|
||||
|
||||
public function getInputs(string $entity, Model $model)
|
||||
{
|
||||
|
||||
Debug::dd($tag);
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace kernel;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\models\Option;
|
||||
use kernel\modules\option\service\OptionService;
|
||||
@ -148,38 +149,38 @@ class EntityRelation
|
||||
return false;
|
||||
}
|
||||
|
||||
public function renderFormInputsBySlug(string $slug): void
|
||||
public function renderFormInputsBySlug(string $entity, string $slug, Model $model): void
|
||||
{
|
||||
$moduleClass = $this->getAdditionalPropertyClassBySlug($slug);
|
||||
if ($moduleClass and method_exists($moduleClass, "formInputs")) {
|
||||
$moduleClass->formInputs();
|
||||
$moduleClass->formInputs($entity, $model);
|
||||
}
|
||||
}
|
||||
|
||||
public function renderEntityAdditionalPropertyFormBySlug(string $slug): void
|
||||
{
|
||||
$relations = $this->getEntityRelationsBySlug($slug);
|
||||
if ($relations){
|
||||
foreach ($relations as $relation){
|
||||
$this->renderFormInputsBySlug($relation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function saveEntityRelationBySlug(string $slug, string $entity, Request $request): void
|
||||
{
|
||||
$moduleClass = $this->getAdditionalPropertyClassBySlug($slug);
|
||||
if ($moduleClass and method_exists($moduleClass, "saveInputs")) {
|
||||
$moduleClass->saveInputs($entity, $request);
|
||||
}
|
||||
}
|
||||
|
||||
public function saveEntityRelation(string $entity, Request $request): void
|
||||
public function renderEntityAdditionalPropertyFormBySlug(string $entity, Model $model = null): void
|
||||
{
|
||||
$relations = $this->getEntityRelationsBySlug($entity);
|
||||
if ($relations){
|
||||
foreach ($relations as $relation){
|
||||
$this->saveEntityRelationBySlug($relation, $entity, $request);
|
||||
$this->renderFormInputsBySlug($entity, $relation, $model);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function saveEntityRelationBySlug(string $slug, string $entity, Model $model, Request $request): void
|
||||
{
|
||||
$moduleClass = $this->getAdditionalPropertyClassBySlug($slug);
|
||||
if ($moduleClass and method_exists($moduleClass, "saveInputs")) {
|
||||
$moduleClass->saveInputs($entity, $model, $request);
|
||||
}
|
||||
}
|
||||
|
||||
public function saveEntityRelation(string $entity, Model $model, Request $request): void
|
||||
{
|
||||
$relations = $this->getEntityRelationsBySlug($entity);
|
||||
if ($relations){
|
||||
foreach ($relations as $relation){
|
||||
$this->saveEntityRelationBySlug($relation, $entity, $model, $request);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -98,4 +98,9 @@ class TagController extends AdminController
|
||||
$this->redirect("/admin/tag/");
|
||||
}
|
||||
|
||||
public function actionSettings(): void
|
||||
{
|
||||
echo "tag settings";
|
||||
}
|
||||
|
||||
}
|
@ -11,8 +11,6 @@ use Illuminate\Database\Eloquent\Model;
|
||||
* @property int $entity_id
|
||||
* @property string $slug
|
||||
* @property int $status
|
||||
* @method static where(int[] $array)
|
||||
* @method static find($id)
|
||||
*/
|
||||
class Tag extends Model
|
||||
{
|
||||
@ -21,13 +19,14 @@ class Tag extends Model
|
||||
|
||||
protected $table = 'tag';
|
||||
|
||||
protected $fillable = ['label', 'entity', 'slug', 'status'];
|
||||
protected $fillable = ['label', 'entity', 'entity_id', 'slug', 'status'];
|
||||
|
||||
public static function labels(): array
|
||||
{
|
||||
return [
|
||||
'label' => 'Заголовок',
|
||||
'entity' => 'Сущность',
|
||||
'entity_id' => 'Идентификатор сущность',
|
||||
'slug' => 'Slug',
|
||||
'status' => 'Статус',
|
||||
];
|
||||
|
@ -15,4 +15,7 @@ App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router
|
||||
App::$collector->any("/edit/{id}", [\app\modules\tag\controllers\TagController::class, 'actionEdit']);
|
||||
App::$collector->get('/delete/{id}', [\app\modules\tag\controllers\TagController::class, 'actionDelete']);
|
||||
});
|
||||
App::$collector->group(["prefix" => "settings"], function (CGRouteCollector $router){
|
||||
App::$collector->get('/tag', [\app\modules\tag\controllers\TagController::class, 'actionSettings']);
|
||||
});
|
||||
});
|
@ -35,7 +35,7 @@ class OptionService
|
||||
return false;
|
||||
}
|
||||
|
||||
public function createFromParams(string $key, string $value, string $label): false|Option
|
||||
public static function createFromParams(string $key, string $value, string $label): false|Option
|
||||
{
|
||||
$model = new Option();
|
||||
$model->key = $key;
|
||||
@ -63,6 +63,17 @@ class OptionService
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function removeOptionByKey(string $key): bool
|
||||
{
|
||||
$option = Option::where("key", $key)->first();
|
||||
if (!$option){
|
||||
return false;
|
||||
}
|
||||
|
||||
$option->delete();
|
||||
return true;
|
||||
}
|
||||
|
||||
// public function createOptionArr(): array
|
||||
// {
|
||||
// foreach (Option::all()->toArray() as $option) {
|
||||
|
@ -36,7 +36,7 @@ class PostController extends AdminController
|
||||
$post = $this->postService->create($postForm);
|
||||
|
||||
$entityRelation = new EntityRelation();
|
||||
$entityRelation->saveEntityRelation("post", new Request());
|
||||
$entityRelation->saveEntityRelation(entity: "post", model: $post, request: new Request());
|
||||
|
||||
if ($post) {
|
||||
$this->redirect("/admin/post/view/" . $post->id);
|
||||
|
@ -34,7 +34,7 @@ $form->field(class: \itguild\forms\inputs\Select::class, name: "user_id", params
|
||||
->render();
|
||||
|
||||
$entityRelations = new \kernel\EntityRelation();
|
||||
$entityRelations->renderEntityAdditionalPropertyFormBySlug("post");
|
||||
$entityRelations->renderEntityAdditionalPropertyFormBySlug("post", $model);
|
||||
|
||||
?>
|
||||
<div class="row">
|
||||
|
Loading…
Reference in New Issue
Block a user