cgView->viewPath = ROOT_DIR . "/views/admin/"; $this->cgView->layout = "layouts/main.php"; } public function actionCreate(): void { $this->cgView->render("user/form.php"); } #[NoReturn] public function actionAdd(): void { $userForm = new CreateUserForm(); $userService = new UserService(); $userForm->load($_REQUEST); if ($userForm->validate()){ $user = $userService->create($userForm); if ($user){ $this->redirect("/admin/user/" . $user->id); } } $this->redirect("/admin/user/create"); } public function actionQuestionCount($user_id) { return Question::where('user_id', $user_id)->count(); } /** * @throws \Exception */ public function actionIndex(): void { $users = User::where(['role' => 1])->get(); $this->cgView->render("user/index.php", ['users' => $users]); } /** * @throws Exception */ public function actionView($id): void { $user = User::find($id); if (!$user){ throw new Exception(message: "The user not found"); } $this->twig->addFunction(new TwigFunction('table', function () use ($user){ $dataProvider = new ViewJsonTableEloquentModel($user, [ 'params' => ["class" => "table table-bordered", "border" => "2"], 'baseUrl' => "/admin/user", ]); $table = new ViewJsonTable($dataProvider->getJson()); $table->beforeTable(function (){ return PrimaryBtn::create("Список", "/admin/user")->fetch(); }); $table->create(); $table->render(); })); echo $this->twig->render('user_table.html.twig'); } /** * @throws RuntimeError * @throws SyntaxError * @throws LoaderError */ public function actionUpdate($id): void { echo $this->twig->render('user_update.html.twig'); } public function actionEdit($id): void { // $_REQUEST["password_hash"] = password_hash($_REQUEST["password_hash"], PASSWORD_DEFAULT); // // $user = User::find($_REQUEST['id']); // $user->username = $_REQUEST['username']; // $user->email = $_REQUEST['email']; // $user->password_hash = $_REQUEST['password_hash']; // $user->save(); // $user = User::find($id); // if (!$user){ // throw new Exception(message: "The user not found"); // } // $userForm = new CreateUserForm(); // $userService = new UserService(); // $userForm->load($_REQUEST); //// Debug::prn($userForm->validate()); //// Debug::dd($userForm->getErrors()); // if ($userForm->validate()){ //// Debug::prn($userService); // // $userService->create($userForm); // $this->redirect("/admin/user/" . User::find($id)['id']); // } // else // { // $this->redirect("/admin/user/update/" . $id); // } } public function actionDelete($id): void { User::find($id)->delete(); } }