post crud
This commit is contained in:
@ -4,21 +4,103 @@ namespace app\controllers;
|
||||
|
||||
|
||||
use app\helpers\Debug;
|
||||
use app\models\forms\CreatePostForm;
|
||||
use app\models\Post;
|
||||
use app\models\User;
|
||||
use app\services\PostService;
|
||||
use app\services\UserService;
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use kernel\Controller;
|
||||
|
||||
class PostController
|
||||
class PostController extends Controller
|
||||
{
|
||||
public function actionCreatePost($post, $user_id)
|
||||
protected function init(): void
|
||||
{
|
||||
return Post::create(['post'=>$post, 'user_id'=>$user_id]);
|
||||
$this->cgView->viewPath = ROOT_DIR . "/views/admin/";
|
||||
$this->cgView->layout = "layouts/main.php";
|
||||
}
|
||||
public function actionCreate(): void
|
||||
{
|
||||
$this->cgView->render("post/form.php");
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionAdd(): void
|
||||
{
|
||||
$postForm = new CreatePostForm();
|
||||
$postService = new PostService();
|
||||
$postForm->load($_REQUEST);
|
||||
if((new UserService)->check($_REQUEST['username'])) {
|
||||
if ($postForm->validate()) {
|
||||
$post = $postService->create($postForm);
|
||||
if ($post) {
|
||||
$this->redirect("/admin/post/" . $post->id);
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/post/create");
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
|
||||
public function actionIndex(): void
|
||||
{
|
||||
Debug::dd("Post list");
|
||||
foreach (Post::all() as $post)
|
||||
{
|
||||
echo $post->post . "<br>";
|
||||
$posts = Post::all();
|
||||
|
||||
$this->cgView->render("post/index.php", ['posts' => $posts]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionView($id): void
|
||||
{
|
||||
$post = Post::find($id);
|
||||
|
||||
if (!$post){
|
||||
throw new Exception(message: "The post not found");
|
||||
}
|
||||
$this->cgView->render("post/view.php", ['post' => $post]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionUpdate($id): void
|
||||
{
|
||||
$model = Post::find($id);
|
||||
if (!$model){
|
||||
throw new Exception(message: "The post not found");
|
||||
}
|
||||
|
||||
$this->cgView->render("post/form.php", ['model' => $model]);
|
||||
}
|
||||
|
||||
public function actionEdit($id): void
|
||||
{
|
||||
$post = Post::find($id);
|
||||
if (!$post){
|
||||
throw new Exception(message: "The user not found");
|
||||
}
|
||||
$postForm = new CreatePostForm();
|
||||
$postService = new PostService();
|
||||
$postForm->load($_REQUEST);
|
||||
if((new UserService)->check($_REQUEST['username'])) {
|
||||
if ($postForm->validate()) {
|
||||
$post = $postService->update($postForm, $post);
|
||||
if ($post) {
|
||||
$this->redirect("/admin/post/" . $post->id);
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/post/update/" . $id);
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionDelete($id): void
|
||||
{
|
||||
Post::find($id)->delete();
|
||||
$this->redirect("/admin/post/");
|
||||
}
|
||||
}
|
@ -50,8 +50,6 @@ class UserController extends Controller{
|
||||
$this->redirect("/admin/user/create");
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function actionQuestionCount($user_id)
|
||||
{
|
||||
return Question::where('user_id', $user_id)->count();
|
||||
@ -62,7 +60,8 @@ class UserController extends Controller{
|
||||
*/
|
||||
public function actionIndex(): void
|
||||
{
|
||||
$users = User::where(['role' => 1])->get();
|
||||
// $users = User::where(['role' => 1])->get();
|
||||
$users = User::all();
|
||||
|
||||
$this->cgView->render("user/index.php", ['users' => $users]);
|
||||
}
|
||||
@ -77,20 +76,6 @@ class UserController extends Controller{
|
||||
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');
|
||||
$this->cgView->render("user/view.php", ['user' => $user]);
|
||||
}
|
||||
|
||||
@ -134,7 +119,6 @@ class UserController extends Controller{
|
||||
{
|
||||
User::find($id)->delete();
|
||||
$this->redirect("/admin/user/");
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user