2024-07-03 14:41:15 +03:00
|
|
|
<?php
|
2024-07-05 13:49:04 +03:00
|
|
|
namespace app\controllers;
|
|
|
|
|
|
|
|
|
|
|
|
use app\models\Question;
|
|
|
|
use app\models\User;
|
2024-07-12 11:31:04 +03:00
|
|
|
use kernel\Controller;
|
2024-07-03 14:41:15 +03:00
|
|
|
|
2024-07-11 16:16:36 +03:00
|
|
|
class UserController extends Controller{
|
2024-07-08 16:20:25 +03:00
|
|
|
public function actionCreate(): void
|
2024-07-03 14:41:15 +03:00
|
|
|
{
|
2024-07-11 16:16:36 +03:00
|
|
|
// $loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../views');
|
|
|
|
// $twig = new \Twig\Environment($loader, ['cache' => 'app/views/cache']);
|
|
|
|
echo $this->twig->render('userCreate.html');
|
2024-07-09 16:08:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function actionAdd(): void
|
|
|
|
{
|
2024-07-10 14:39:37 +03:00
|
|
|
$_REQUEST["password_hash"] = password_hash($_REQUEST["password_hash"], PASSWORD_DEFAULT);
|
2024-07-09 16:08:50 +03:00
|
|
|
User::create($_REQUEST);
|
2024-07-03 14:41:15 +03:00
|
|
|
}
|
|
|
|
|
2024-07-03 15:15:59 +03:00
|
|
|
public function actionQuestionCount($user_id)
|
2024-07-03 14:41:15 +03:00
|
|
|
{
|
|
|
|
return Question::where('user_id', $user_id)->count();
|
|
|
|
}
|
2024-07-05 13:49:04 +03:00
|
|
|
|
2024-07-08 16:20:25 +03:00
|
|
|
public function actionIndex(): void
|
2024-07-05 13:49:04 +03:00
|
|
|
{
|
2024-07-11 16:16:36 +03:00
|
|
|
// $loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../views');
|
|
|
|
// $twig = new \Twig\Environment($loader, ['cache' => 'app/views/cache']);
|
|
|
|
|
|
|
|
$i = 0;
|
2024-07-05 13:49:04 +03:00
|
|
|
foreach (User::all() as $user)
|
|
|
|
{
|
2024-07-11 16:16:36 +03:00
|
|
|
$userArr[$i++] = $user;
|
2024-07-05 13:49:04 +03:00
|
|
|
}
|
2024-07-11 16:16:36 +03:00
|
|
|
echo $this->twig->render('userTable.html', ['userArr' => $userArr]);
|
2024-07-05 13:49:04 +03:00
|
|
|
}
|
|
|
|
|
2024-07-08 16:20:25 +03:00
|
|
|
public function actionView($id): void
|
2024-07-05 13:49:04 +03:00
|
|
|
{
|
2024-07-08 16:20:25 +03:00
|
|
|
echo User::where('id', '=', $id)->get();
|
2024-07-09 16:08:50 +03:00
|
|
|
echo User::where('id', '=', $id)->first() . "<br><br>";
|
2024-07-05 13:49:04 +03:00
|
|
|
|
2024-07-09 16:08:50 +03:00
|
|
|
$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>";
|
2024-07-05 13:49:04 +03:00
|
|
|
}
|
2024-07-09 16:08:50 +03:00
|
|
|
|
|
|
|
public function actionUpdate(): void
|
|
|
|
{
|
2024-07-11 16:16:36 +03:00
|
|
|
// $loader = new \Twig\Loader\FilesystemLoader(__DIR__.'/../views');
|
|
|
|
// $twig = new \Twig\Environment($loader, ['cache' => 'app/views/cache']);
|
|
|
|
echo $this->twig->render('userUpdate.html');
|
2024-07-09 16:08:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function actionEdit(): void
|
|
|
|
{
|
2024-07-11 16:16:36 +03:00
|
|
|
$_REQUEST["password_hash"] = password_hash($_REQUEST["password_hash"], PASSWORD_DEFAULT);
|
|
|
|
|
2024-07-09 16:08:50 +03:00
|
|
|
$user = User::find($_REQUEST['id']);
|
|
|
|
$user->username = $_REQUEST['username'];
|
|
|
|
$user->email = $_REQUEST['email'];
|
2024-07-11 16:16:36 +03:00
|
|
|
$user->password_hash = $_REQUEST['password_hash'];
|
2024-07-09 16:08:50 +03:00
|
|
|
$user->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionDelete($id): void
|
|
|
|
{
|
|
|
|
User::find($id)->delete();
|
|
|
|
}
|
|
|
|
|
2024-07-03 14:41:15 +03:00
|
|
|
}
|