From 53eb536213ac34c5763b49e1e62cdadd3d09689e Mon Sep 17 00:00:00 2001 From: stasbilay02 Date: Fri, 26 Jul 2024 16:09:06 +0300 Subject: [PATCH] add select --- app/controllers/PostController.php | 7 ++++++- app/models/forms/CreatePostForm.php | 2 +- app/services/UserService.php | 12 ++++++++++++ app/tables/columns/PostDeleteActionColumn.php | 16 ++++++++++++++++ app/tables/columns/PostViewActionColumn.php | 16 ++++++++++++++++ app/tables/columns/UserDeleteActionColumn.php | 4 ++-- app/tables/columns/UserEditActionColumn.php | 2 +- m.php | 2 +- migrations/PostMigration.php | 2 +- views/admin/post/form.php | 14 ++++++++++---- views/admin/post/index.php | 12 ++++++------ 11 files changed, 72 insertions(+), 17 deletions(-) create mode 100644 app/tables/columns/PostDeleteActionColumn.php create mode 100644 app/tables/columns/PostViewActionColumn.php diff --git a/app/controllers/PostController.php b/app/controllers/PostController.php index 5837fdc..3357781 100644 --- a/app/controllers/PostController.php +++ b/app/controllers/PostController.php @@ -30,8 +30,12 @@ class PostController extends Controller $postForm = new CreatePostForm(); $postService = new PostService(); $postForm->load($_REQUEST); +// Debug::dd($_REQUEST); if(UserService::check($_REQUEST['user_id'])) { +// Debug::dd(123); if ($postForm->validate()) { + +// Debug::dd($postForm); $post = $postService->create($postForm); if ($post) { $this->redirect("/admin/post/" . $post->id); @@ -103,7 +107,8 @@ class PostController extends Controller #[NoReturn] public function actionDelete($id): void { - Post::find($id)->delete(); + $post = Post::find($id)->first(); + $post->delete(); $this->redirect("/admin/post/"); } } \ No newline at end of file diff --git a/app/models/forms/CreatePostForm.php b/app/models/forms/CreatePostForm.php index 8eb195f..c29d673 100644 --- a/app/models/forms/CreatePostForm.php +++ b/app/models/forms/CreatePostForm.php @@ -10,7 +10,7 @@ class CreatePostForm extends FormModel { return [ 'content' => 'required|min-str-len:1', - 'user_id' => 'required|min-str-len:1', + 'user_id' => 'required', ]; } } \ No newline at end of file diff --git a/app/services/UserService.php b/app/services/UserService.php index 0f0142e..e26842b 100644 --- a/app/services/UserService.php +++ b/app/services/UserService.php @@ -44,4 +44,16 @@ class UserService return false; } + public static function createUsernameArr(): array + { + foreach (User::all()->toArray() as $user) { + + $userArr[$user['id']] = $user['username'];; + } + if (!empty($userArr)) { + return $userArr; + } + return []; + } + } \ No newline at end of file diff --git a/app/tables/columns/PostDeleteActionColumn.php b/app/tables/columns/PostDeleteActionColumn.php new file mode 100644 index 0000000..2db370f --- /dev/null +++ b/app/tables/columns/PostDeleteActionColumn.php @@ -0,0 +1,16 @@ +baseUrl . $this->prefix . $this->id; + return " Удалить "; + } +} \ No newline at end of file diff --git a/app/tables/columns/PostViewActionColumn.php b/app/tables/columns/PostViewActionColumn.php new file mode 100644 index 0000000..a09d210 --- /dev/null +++ b/app/tables/columns/PostViewActionColumn.php @@ -0,0 +1,16 @@ +baseUrl . $this->prefix . $this->id; + return " Просмотр "; + } +} \ No newline at end of file diff --git a/app/tables/columns/UserDeleteActionColumn.php b/app/tables/columns/UserDeleteActionColumn.php index 969ec40..c4687ef 100644 --- a/app/tables/columns/UserDeleteActionColumn.php +++ b/app/tables/columns/UserDeleteActionColumn.php @@ -7,11 +7,11 @@ use Itguild\Tables\ActionColumn\ActionColumn; class UserDeleteActionColumn extends ActionColumn { - protected string $prefix = "/"; + protected string $prefix = "/delete/"; public function fetch(): string { - $link = $this->baseUrl . $this->prefix . "delete" . $this->prefix . $this->id; + $link = $this->baseUrl . $this->prefix . $this->id; return " Удалить "; } } \ No newline at end of file diff --git a/app/tables/columns/UserEditActionColumn.php b/app/tables/columns/UserEditActionColumn.php index 84ce168..3f961ce 100644 --- a/app/tables/columns/UserEditActionColumn.php +++ b/app/tables/columns/UserEditActionColumn.php @@ -6,7 +6,7 @@ use Itguild\Tables\ActionColumn\ActionColumn; class UserEditActionColumn extends ActionColumn { - protected string $prefix = "/"; + protected string $prefix = "/update/"; public function fetch(): string { diff --git a/m.php b/m.php index d687cdb..e3bca0e 100644 --- a/m.php +++ b/m.php @@ -9,6 +9,6 @@ require_once __DIR__ . "/vendor/autoload.php"; require_once __DIR__ . "/bootstrap.php"; //UserMigration::up(); -PostMigration::up(); +//PostMigration::up(); //AnswerMigration::up(); //QuestionMigration::up(); \ No newline at end of file diff --git a/migrations/PostMigration.php b/migrations/PostMigration.php index a8e5df9..a4fd8f6 100644 --- a/migrations/PostMigration.php +++ b/migrations/PostMigration.php @@ -18,7 +18,7 @@ class PostMigration extends Migration { Manager::schema()->create('post', function (Blueprint $table) { $table->increments('id'); - $table->string('content', 3000)->nullable(false); + $table->text('content')->nullable(false); $table->integer('user_id'); $table->timestamps(); }); diff --git a/views/admin/post/form.php b/views/admin/post/form.php index 0eb591d..70e9a96 100644 --- a/views/admin/post/form.php +++ b/views/admin/post/form.php @@ -4,6 +4,7 @@ */ use app\models\Post; +use app\models\User; $form = new \itguild\forms\ActiveForm(); $form->beginForm(isset($model) ? "/admin/post/edit/" . $model->id : "/admin/post"); @@ -16,14 +17,19 @@ $form->field(class: \itguild\forms\inputs\TextArea::class, name: "content", para ]) ->setLabel("Пост") ->render(); - -$form->field(class: \itguild\forms\inputs\TextInput::class, name: "user_id", params: [ +$form->field(class: \itguild\forms\inputs\Select::class, name: "user_id", params: [ +//$form->field(class: \itguild\forms\inputs\Select::class, name: "username", params: [ 'class' => "form-control", - 'placeholder' => 'id', 'value' => $model->user_id ?? '' ]) - ->setLabel("id пользователя") + ->setLabel("Пользователи") + ->setOptions(\app\services\UserService::createUsernameArr()) +// ->setOptions([\app\services\UserService::createIdArr()[74] => \app\services\UserService::createUsernameArr()[1], +// \app\services\UserService::createUsernameArr()[74], +// \app\services\UserService::createUsernameArr()[82],]) ->render(); +\app\helpers\Debug::prn(\app\services\UserService::createUsernameArr()); + ?>
diff --git a/views/admin/post/index.php b/views/admin/post/index.php index 58409e2..60b92d0 100644 --- a/views/admin/post/index.php +++ b/views/admin/post/index.php @@ -5,9 +5,9 @@ */ use app\models\Post; -use app\tables\columns\UserDeleteActionColumn; -use app\tables\columns\UserEditActionColumn; -use app\tables\columns\UserViewActionColumn; +use app\tables\columns\PostDeleteActionColumn; +use app\tables\columns\PostEditActionColumn; +use app\tables\columns\PostViewActionColumn; use Itguild\Tables\ListJsonTable; use kernel\IGTabel\btn\PrimaryBtn; use kernel\IGTabel\ListJsonTableEloquentCollection; @@ -24,8 +24,8 @@ $table->beforePrint(function (){ return PrimaryBtn::create("Создать", "/admin/post/create")->fetch(); //return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch(); }); -$table->addAction(UserViewActionColumn::class); -$table->addAction(\app\tables\columns\PostEditActionColumn::class); -$table->addAction(UserDeleteActionColumn::class); +$table->addAction(PostViewActionColumn::class); +$table->addAction(PostEditActionColumn::class); +$table->addAction(PostDeleteActionColumn::class); $table->create(); $table->render(); \ No newline at end of file