add modules and upload file

This commit is contained in:
2024-09-06 16:53:20 +03:00
parent 4e031c7c8b
commit da77807b81
38 changed files with 180 additions and 782 deletions

View File

@ -1,32 +0,0 @@
<?php
namespace app\controllers;
use app\models\Answer;
use app\models\Upvote;
class AnswerController {
public function actionAddAnswer($answer,$question_id,$user_id)
{
return Answer::create(['answer'=>$answer,'question_id'=>$question_id,'user_id'=>$user_id]);
}
public static function actionUpvoteAnswer($answer_id,$user_id)
{
return Upvote::create(['answer_id'=>$answer_id,'user_id'=>$user_id]);
}
public function actionUpdateAnswer($answer_id,$new_answer)
{
$answer = Answer::find($answer_id);
$answer->answer = $new_answer;
return $answer->save();
}
public function actionViewAllAnswers(): void
{
foreach (Answer::all() as $answer)
{
echo $answer->answer . "<br>";
}
}
}

View File

@ -1,106 +0,0 @@
<?php
namespace app\controllers;
use app\helpers\Debug;
use app\models\forms\CreateMenuForm;
use app\services\MenuService;
use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\Controller;
use kernel\models\Menu;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
class MenuController extends Controller
{
protected function init(): void
{
$this->cgView->viewPath = ROOT_DIR . "/views/admin/";
$this->cgView->layout = "layouts/main.php";
}
public function actionCreate(): void
{
$this->cgView->render("menu/form.php");
}
#[NoReturn] public function actionAdd(): void
{
$menuForm = new CreateMenuForm();
$menuService = new MenuService();
$menuForm->load($_REQUEST);
if ($menuForm->validate()){
$menuItem = $menuService->create($menuForm);
if ($menuItem){
$this->redirect("/admin/menu/" . $menuItem->id);
}
}
$this->redirect("/admin/menu/create");
}
/**
* @throws Exception
*/
public function actionIndex($page_number = 1): void
{
$this->cgView->render("menu/index.php", ['page_number' => $page_number]);
}
/**
* @throws Exception
*/
public function actionView($id): void
{
$menuItem = Menu::find($id);
if (!$menuItem){
throw new Exception(message: "The menu item not found");
}
$this->cgView->render("menu/view.php", ['menu' => $menuItem]);
}
/**
* @throws RuntimeError
* @throws SyntaxError
* @throws LoaderError|Exception
*/
public function actionUpdate($id): void
{
$model = Menu::find($id);
if (!$model){
throw new Exception(message: "The menu item not found");
}
$this->cgView->render("menu/form.php", ['model' => $model]);
}
/**
* @throws Exception
*/
public function actionEdit($id): void
{
$menuItem = Menu::find($id);
if (!$menuItem){
throw new Exception(message: "The menu item not found");
}
$menuForm = new CreateMenuForm();
$menuService = new MenuService();
$menuForm->load($_REQUEST);
if ($menuForm->validate()){
$menuItem = $menuService->update($menuForm, $menuItem);
if ($menuItem){
$this->redirect("/admin/menu/" . $menuItem->id);
}
}
$this->redirect("/admin/menu/update/" . $id);
}
#[NoReturn] public function actionDelete($id): void
{
Menu::find($id)->delete();
$this->redirect("/admin/menu/");
}
}

View File

@ -1,106 +0,0 @@
<?php
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 extends Controller
{
protected function init(): void
{
$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 ($postForm->validate()) {
$post = $postService->create($postForm);
if ($post) {
$this->redirect("/admin/post/" . $post->id);
}
}
$this->redirect("/admin/post/create");
}
/**
* @throws Exception
*/
public function actionIndex($page_number = 1): void
{
$this->cgView->render("post/index.php", ['page_number' => $page_number]);
}
/**
* @throws Exception
*/
public function actionView($id): void
{
$content = Post::find($id);
if (!$content){
throw new Exception(message: "The post not found");
}
$this->cgView->render("post/view.php", ['content' => $content]);
}
/**
* @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]);
}
/**
* @throws Exception
*/
public function actionEdit($id): void
{
$post = Post::find($id);
if (!$post){
throw new Exception(message: "The post not found");
}
$postForm = new CreatePostForm();
$postService = new PostService();
$postForm->load($_REQUEST);
if((new UserService)->check($_REQUEST['user_id'])) {
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 = Post::find($id)->first();
$post->delete();
$this->redirect("/admin/post/");
}
}

View File

@ -1,36 +0,0 @@
<?php
namespace app\controllers;
use app\models\Question;
use kernel\Controller;
class QuestionController extends Controller{
public function actionCreate()
{
echo $this->twig->render('question_create.html.twig');
}
public function actionGetQuestionsWithAnswers(): array
{
return Question::with('AnswerController')->get()->toArray();
}
public function actionGetQuestionsWithUsers(): array
{
return Question::with('user')->get()->toArray();
}
public function actionGetQuestionAnswersUpvotes($question_id)
{
return Question::find($question_id)->answers()->with('upvotes')->get()->toArray();
}
public function actionViewAllQuestions()
{
foreach (Question::all() as $question)
{
echo $question->question. "<br>";
}
}
}

View File

@ -1,110 +0,0 @@
<?php
namespace app\controllers;
use app\models\forms\CreateUserForm;
use app\models\Question;
use app\models\User;
use app\services\UserService;
use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\Controller;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
class UserController extends Controller{
protected function init(): void
{
$this->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($page_number = 1): void
{
$this->cgView->render("user/index.php", ['page_number' => $page_number]);
}
/**
* @throws Exception
*/
public function actionView($id): void
{
$user = User::find($id);
if (!$user){
throw new Exception(message: "The user not found");
}
$this->cgView->render("user/view.php", ['user' => $user]);
}
/**
* @throws RuntimeError
* @throws SyntaxError
* @throws LoaderError
*/
public function actionUpdate($id): void
{
$model = User::find($id);
if (!$model){
throw new Exception(message: "The user not found");
}
$this->cgView->render("user/form.php", ['model' => $model]);
}
/**
* @throws Exception
*/
public function actionEdit($id): void
{
$user = User::find($id);
if (!$user){
throw new Exception(message: "The user not found");
}
$userForm = new CreateUserForm();
$userService = new UserService();
$userForm->load($_REQUEST);
if ($userForm->validate()){
$user = $userService->update($userForm, $user);
if ($user){
$this->redirect("/admin/user/" . $user->id);
}
}
$this->redirect("/admin/user/update/" . $id);
}
#[NoReturn] public function actionDelete($id): void
{
User::find($id)->delete();
$this->redirect("/admin/user/");
}
}

View File

@ -1,14 +0,0 @@
<?php
namespace app\models;
use \Illuminate\Database\Eloquent\Model;
class Answer extends Model {
protected $table = 'answer';
protected $fillable = ['answer','user_id','question_id'];
public function upvotes()
{
return $this->hasMany('\Models\Upvote');
}
}

View File

@ -1,28 +0,0 @@
<?php
namespace app\models;
use \Illuminate\Database\Eloquent\Model;
/**
* @property int $id
* @property string $content
// * @property string $username
* @property int $user_id
* @method static where(int[] $array)
* @method static find($id)
*/
class Post extends Model
{
protected $table = 'post';
protected $fillable = ['content', 'user_id'];
public static function labels(): array
{
return [
'content' => 'Контент',
'user_id' => 'Id пользователя',
'created_at' => 'Создан',
'updated_at' => 'Обновлен'
];
}
}

View File

@ -1,22 +0,0 @@
<?php
namespace app\models;
use \Illuminate\Database\Eloquent\Model;
/**
* @method static where(string $string, $user_id)
*/
class Question extends Model {
protected $table = 'question';
protected $fillable = ['question','user_id'];
public function answers()
{
return $this->hasMany('\Models\Answer');
}
public function user()
{
return $this->belongsTo('\Models\User');
}
}

View File

@ -1,27 +0,0 @@
<?php
namespace app\models;
use Illuminate\Database\Eloquent\Model;
/**
* @property int $id
* @property string $username
* @property string $email
* @property string $password_hash
* @method static where(int[] $array)
* @method static find($id)
*/
class User extends Model {
protected $table = 'user';
protected $fillable = ['username', 'email', 'password_hash', 'role'];
protected array $dates = ['deleted at'];
public static function labels(): array
{
return [
'username' => 'Логин',
'email' => 'Email',
'created_at' => 'Создан',
'updated_at' => 'Обновлен'
];
}
}

View File

@ -1,55 +0,0 @@
<?php
namespace app\services;
use app\helpers\Debug;
use kernel\FormModel;
use kernel\models\Menu;
class MenuService
{
public function create(FormModel $form_model): false|Menu
{
$model = new Menu();
$model->parent_id = $form_model->getItem('parent_id');
$model->icon_file = $form_model->getItem('icon_file');
$model->icon_font = $form_model->getItem('icon_font');
$model->label = $form_model->getItem('label');
$model->url = $form_model->getItem('url');
$model->status = $form_model->getItem('status');
if ($model->save()){
return $model;
}
return false;
}
public function update(FormModel $form_model, Menu $menuItem): false|Menu
{
$menuItem->parent_id = $form_model->getItem('parent_id');
$menuItem->icon_file = $form_model->getItem('icon_file');
$menuItem->icon_font = $form_model->getItem('icon_font');
$menuItem->label = $form_model->getItem('label');
$menuItem->url = $form_model->getItem('url');
$menuItem->status = $form_model->getItem('status');
if ($menuItem->save()){
return $menuItem;
}
return false;
}
public static function createLabelArr(): array
{
$labelArr[0] = "Корневой пункт меню";
foreach (Menu::where("parent_id", 0)->get()->toArray() as $menuItem) {
$labelArr[$menuItem['id']] = $menuItem['label'];
}
if (!empty($labelArr)) {
return $labelArr;
}
return [];
}
}

View File

@ -1,32 +0,0 @@
<?php
namespace app\services;
use app\models\Post;
use kernel\FormModel;
class PostService
{
public function create(FormModel $form_model): false|Post
{
$model = new Post();
$model->content = $form_model->getItem('content');
$model->user_id = $form_model->getItem('user_id');
if ($model->save()){
return $model;
}
return false;
}
public function update(FormModel $form_model, Post $post): false|Post
{
$post->content = $form_model->getItem('content');
$post->user_id = $form_model->getItem('user_id');
if ($post->save()){
return $post;
}
return false;
}
}

View File

@ -1,49 +0,0 @@
<?php
namespace app\services;
use app\helpers\Debug;
use app\models\User;
use kernel\FormModel;
class UserService
{
public function create(FormModel $form_model): false|User
{
$model = new User();
$model->username = $form_model->getItem('username');
$model->email = $form_model->getItem('email');
$model->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT);
if ($model->save()){
return $model;
}
return false;
}
public function update(FormModel $form_model, User $user): false|User
{
$user->username = $form_model->getItem('username');
$user->email = $form_model->getItem('email');
$user->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT);
if ($user->save()){
return $user;
}
return false;
}
public static function createUsernameArr(): array
{
foreach (User::all()->toArray() as $user) {
$userArr[$user['id']] = $user['username'];
}
if (!empty($userArr)) {
return $userArr;
}
return [];
}
}