backend/app/controllers/UserController.php
2024-07-12 13:46:44 +03:00

86 lines
2.4 KiB
PHP

<?php
namespace app\controllers;
use app\foo;
use app\models\Question;
use app\models\User;
use Itguild\Tables\ListJsonTable;
use kernel\Controller;
class UserController extends Controller{
public function actionCreate(): void
{
// $loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../views');
// $twig = new \Twig\Environment($loader, ['cache' => 'app/views/cache']);
echo $this->twig->render('userCreate.html');
}
public function actionAdd(): void
{
$_REQUEST["password_hash"] = password_hash($_REQUEST["password_hash"], PASSWORD_DEFAULT);
User::create($_REQUEST);
}
public function actionQuestionCount($user_id)
{
return Question::where('user_id', $user_id)->count();
}
public function actionIndex(): void
{
// $loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../views');
// $twig = new \Twig\Environment($loader, ['cache' => 'app/views/cache']);
$i = 0;
foreach (User::all() as $user)
{
$userArr[$i++] = $user;
}
echo $this->twig->render('user_table.html.twig', ['userArr' => $userArr]);
$json = new foo();
$userArr = $json->createJsonArray(User::labels(), $userArr, "form1");
$table = new ListJsonTable($userArr);
$table->create();
$table->render();
}
public function actionView($id): void
{
echo User::where('id', '=', $id)->get();
echo User::where('id', '=', $id)->first() . "<br><br>";
$user = User::find($id);
echo $user->id . "<br>";
echo $user->username . "<br>";
echo $user->email . "<br>";
echo $user->created_at . "<br>";
echo $user->updated_at . "<br>";
}
public function actionUpdate(): void
{
// $loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../views');
// $twig = new \Twig\Environment($loader, ['cache' => 'app/views/cache']);
echo $this->twig->render('userUpdate.html');
}
public function actionEdit(): 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();
}
public function actionDelete($id): void
{
User::find($id)->delete();
}
}