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,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,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,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');
}
}

51
kernel/FileUpload.php Normal file
View File

@ -0,0 +1,51 @@
<?php
namespace kernel;
use app\helpers\Debug;
class FileUpload
{
public string $fileTmpPath;
public string $fileName;
public string $fileSize;
public string $fileType;
public array $fileNameCmps;
public string $fileExtension;
public string $newFileName;
public array $allowedFileExtensions = ['jpg', 'gif', 'png'];
public function __construct(array $file)
{
$this->fileTmpPath = $file['tmp_name'];
$this->fileName = $file['name'];
$this->fileSize = $file['size'];
$this->fileType = $file['type'];
$this->fileNameCmps = explode('.', $this->fileName);
$this->fileExtension = strtolower(end($this->fileNameCmps));
}
public function setNewFileName(): void
{
$this->newFileName = md5(time() . $this->fileName) . '.' . $this->fileExtension;
}
public function checkExtension(): void
{
}
public function upload(): void
{
$this->newFileName = md5(time() . $this->fileName) . '.' . $this->fileExtension;
if (in_array($this->fileExtension, $this->allowedFileExtensions)) {
mkdir('./resources/upload/' . mb_substr($this->newFileName, 0, 2) . '/' . mb_substr($this->newFileName, 2, 2) . '/', 0777, true);
$uploadFileDir = './resources/upload/' . mb_substr($this->newFileName, 0, 2) . '/' . mb_substr($this->newFileName, 2, 2) . '/';
$dest_path = $uploadFileDir . $this->newFileName;
move_uploaded_file($this->fileTmpPath, $dest_path);
} else {
echo "Ниченр не получилочь :(";
}
}
}

View File

@ -1,78 +0,0 @@
<?php
//
//namespace kernel\IGTabel;
//
//use app\helpers\Debug;
//use Exception;
//use Illuminate\Database\Eloquent\Model;
//
//class EloquentDataProvider
//{
// protected int $totalCount;
//
// protected int $perPage = 10;
//
// protected int $currentPage = 1;
//
// protected Model $model;
//
// protected $queryBuilder = false;
//
// protected array $options = [];
// protected array $meta = [];
// protected string $jsonStr = '';
//
// /**
// * @throws Exception
// */
// public function __construct($source, array $options)
// {
// if (is_string($source)) {
// $this->queryBuilder = $source::query();
// $model = new $source();
// } elseif (is_object($source)) {
// $this->queryBuilder = $source;
// $model = $source->getModel();
// } else {
// throw new Exception(message: "source is not valid");
// }
// $this->options = $options;
// $this->currentPage = $this->options['currentPage'] ?? 1;
// $this->perPage = $this->options['perPage'] ?? 10;
// $this->meta['total'] = $model->count();
// $this->meta['totalWithFilters'] = $this->queryBuilder->count();
// $this->meta['columns'] = $options['columns'] ?? $model->labels();
// $this->meta['perPage'] = $options['perPage'] ?? 10;
// $this->meta['currentPage'] = $options['currentPage'] ?? 1;
// $this->meta['baseUrl'] = $options['baseUrl'] ?? $model->table;
// $this->meta['params'] = $options['params'] ?? [];
// $this->meta['actions'] = $options['actions'] ?? [];
// $this->createQuery();
//
// $this->jsonStr = (new JSONCreator($this->meta, $this->getCollection()->toArray()))->getJson();
//
// }
//
// public function createQuery(): void
// {
// if ($this->currentPage > 1) {
// $this->queryBuilder->skip(($this->currentPage - 1) * $this->perPage)->take($this->perPage);
// } else {
// $this->queryBuilder->take($this->perPage);
// }
// }
//
// public function getCollection()
// {
// return $this->queryBuilder->get();
// }
//
// /**
// * @return string|null
// */
// public function getJson(): string|null
// {
// return $this->jsonStr;
// }
//
//}

View File

@ -1,40 +0,0 @@
<?php
//
//namespace kernel\IGTabel;
//
//use app\helpers\Debug;
//
//class JSONCreator
//{
// protected array $informationArray = [];
//
// public function __construct(array $meta, array $data)
// {
// $params = empty($meta['params']) ? ["class" => "table table-bordered", "border" => "1"] : $meta['params'];
// if ($meta) {
// $this->informationArray = [
// "meta" => $meta,
// "data" => $data ?? []
// ];
// }
// }
//
// /**
// * @param array $infArr
// * @return string|null
// */
// protected function toJson(array $infArr): ?string
// {
// if ($infArr)
// return json_encode($infArr, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
// return null;
// }
//
// /**
// * @return string|null
// */
// public function getJson(): ?string
// {
// return $this->toJson($this->informationArray);
// }
//}

View File

@ -1,42 +0,0 @@
<?php
//
//namespace kernel\IGTabel;
//
//use app\helpers\Debug;
//use Illuminate\Database\Eloquent\Collection;
//
//class ListJsonTableEloquentCollection
//{
// private string|null $jsonStr;
// private array $meta = [];
//
// /**
// * @param Collection $collection
// * @param array $params
// * @throws \Exception
// */
// public function __construct(Collection $collection, array $params = [])
// {
// if (!isset($params['model'])) {
// throw new \Exception(message: "The parameter model must not be empty");
// }
// $model = new $params['model']();
// $this->meta['columns'] = $params['columns'] ?? $model->labels();
// $this->meta['perPage'] = $params['perPage'] ?? 10;
// $this->meta['currentPage'] = $params['currentPage'] ?? 1;
// $this->meta['baseUrl'] = $params['baseUrl'] ?? $model->table;
// $this->meta['params'] = $params['params'] ?? [];
// $this->meta['actions'] = $params['actions'] ?? [];
//
// $this->jsonStr = (new JSONCreator($this->meta, $collection->toArray()))->getJson();
// }
//
// /**
// * @return string|null
// */
// public function getJson(): string|null
// {
// return $this->jsonStr;
// }
//
//}

View File

@ -1,37 +0,0 @@
<?php
//
//namespace kernel\IGTabel;
//
//use app\helpers\Debug;
//use app\models\User;
//use Illuminate\Database\Eloquent\Model;
//
//class ViewJsonTableEloquentModel
//{
// private string|null $jsonStr;
//
// private array $meta = [];
//
// public function __construct(Model $model, array $params = [])
// {
// $this->meta['rows'] = $params['rows'] ?? $model->labels();
// $this->meta['baseUrl'] = $params['baseUrl'] ?? $model->table;
// $this->meta['params'] = $params['params'] ?? [];
// $this->meta['actions'] = $params['actions'] ?? [];
//
//// $this->jsonStr = (new JSONCreator($this->meta, $model->toArray()))->getJson();
// $model = $model->toArray();
// if(isset($model['user_id']))
// {
// $model['user_id'] = User::find($model['user_id'])->username;
// }
//
// $this->jsonStr = (new JSONCreator($this->meta, $model))->getJson();
// }
//
// public function getJson(): ?string
// {
// return $this->jsonStr;
// }
//
//}

View File

@ -4,22 +4,26 @@ namespace kernel\modules\menu\controllers;
use app\helpers\Debug; use app\helpers\Debug;
use app\models\forms\CreateMenuForm; use app\models\forms\CreateMenuForm;
use app\services\MenuService; use kernel\FileUpload;
use kernel\modules\menu\service\MenuService;
use Exception; use Exception;
use JetBrains\PhpStorm\NoReturn; use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\Controller; use kernel\Controller;
use kernel\models\Menu; use kernel\models\Menu;
use Twig\Error\LoaderError; use Twig\Error\LoaderError;
use Twig\Error\RuntimeError; use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError; use Twig\Error\SyntaxError;
class MenuController extends Controller class MenuController extends AdminController
{ {
private MenuService $menuService;
protected function init(): void protected function init(): void
{ {
parent::init();
$this->cgView->viewPath = KERNEL_MODULES_DIR . "/menu/views/"; $this->cgView->viewPath = KERNEL_MODULES_DIR . "/menu/views/";
$this->cgView->layoutPath = ROOT_DIR . "/views/admin/"; $this->menuService = new MenuService();
$this->cgView->layout = "layouts/main.php";
} }
public function actionCreate(): void public function actionCreate(): void
@ -29,11 +33,15 @@ class MenuController extends Controller
#[NoReturn] public function actionAdd(): void #[NoReturn] public function actionAdd(): void
{ {
if (isset($_FILES['icon_file']) && $_FILES['icon_file']['error'] === UPLOAD_ERR_OK) {
$file = new FileUpload($_FILES['icon_file']);
$file->upload();
}
$menuForm = new CreateMenuForm(); $menuForm = new CreateMenuForm();
$menuService = new MenuService();
$menuForm->load($_REQUEST); $menuForm->load($_REQUEST);
if ($menuForm->validate()){ if ($menuForm->validate()){
$menuItem = $menuService->create($menuForm); $menuItem = $this->menuService->create($menuForm);
if ($menuItem){ if ($menuItem){
$this->redirect("/admin/settings/menu/" . $menuItem->id); $this->redirect("/admin/settings/menu/" . $menuItem->id);
} }
@ -82,15 +90,20 @@ class MenuController extends Controller
*/ */
public function actionEdit($id): void public function actionEdit($id): void
{ {
// Debug::prn($_REQUEST);
// Debug::prn($_FILES);
$menuItem = Menu::find($id); $menuItem = Menu::find($id);
if (!$menuItem){ if (!$menuItem){
throw new Exception(message: "The menu item not found"); throw new Exception(message: "The menu item not found");
} }
if (isset($_FILES['icon_file']) && $_FILES['icon_file']['error'] === UPLOAD_ERR_OK) {
$file = new FileUpload($_FILES['icon_file']);
$file->upload();
}
$menuForm = new CreateMenuForm(); $menuForm = new CreateMenuForm();
$menuService = new MenuService();
$menuForm->load($_REQUEST); $menuForm->load($_REQUEST);
if ($menuForm->validate()){ if ($menuForm->validate()){
$menuItem = $menuService->update($menuForm, $menuItem); $menuItem = $this->menuService->update($menuForm, $menuItem);
if ($menuItem){ if ($menuItem){
$this->redirect("/admin/settings/menu/" . $menuItem->id); $this->redirect("/admin/settings/menu/" . $menuItem->id);
} }

View File

@ -1,14 +1,13 @@
<?php <?php
namespace app\services; namespace kernel\modules\menu\service;
use app\helpers\Debug;
use kernel\FormModel; use kernel\FormModel;
use kernel\models\Menu; use kernel\models\Menu;
use kernel\Request;
class MenuService class MenuService
{ {
public function create(FormModel $form_model): false|Menu public function create(FormModel $form_model): false|Menu
{ {
$model = new Menu(); $model = new Menu();
@ -51,5 +50,42 @@ class MenuService
} }
return []; return [];
} }
public static function getChild(int $id)
{
$collection = Menu::where("parent_id", $id)->get();
if (!$collection->isEmpty()){
return $collection;
}
return false;
}
public static function hasActiveChild(int $id): bool
{
$child = self::getChild($id);
if (!$child->isEmpty()){
foreach ($child as $item){
// if ($item->url === \kernel\Request::getUrlPath()){
// return true;
// }
if (strripos(Request::getUrlPath(), $item->url) !== false) {
return true;
}
}
}
return false;
}
public static function isActive($url): bool
{
if ($url === Request::getUrlPath()){
return true;
} else {
if (strripos(\kernel\Request::getUrlPath(), ($url . "/page")) !== false) {
return true;
}
}
return false;
}
} }

View File

@ -13,7 +13,7 @@ $form->field(class: \itguild\forms\inputs\Select::class, name: "parent_id", para
'value' => $model->parent_id ?? '' 'value' => $model->parent_id ?? ''
]) ])
->setLabel("Родительский пункт меню") ->setLabel("Родительский пункт меню")
->setOptions(\app\services\MenuService::createLabelArr()) ->setOptions(\kernel\modules\menu\service\MenuService::createLabelArr())
->render(); ->render();
$form->field(class: \itguild\forms\inputs\File::class, name: "icon_file", params: [ $form->field(class: \itguild\forms\inputs\File::class, name: "icon_file", params: [

View File

@ -1,37 +1,36 @@
<?php <?php
namespace app\controllers; namespace kernel\modules\post\controllers;
use app\helpers\Debug; use app\helpers\Debug;
use app\models\forms\CreatePostForm; use app\models\forms\CreatePostForm;
use app\models\Post; use kernel\AdminController;
use app\models\User; use kernel\modules\post\models\Post;
use app\services\PostService; use kernel\modules\post\service\PostService;
use app\services\UserService;
use Exception; use Exception;
use JetBrains\PhpStorm\NoReturn; use JetBrains\PhpStorm\NoReturn;
use kernel\Controller;
class PostController extends Controller class PostController extends AdminController
{ {
private PostService $postService;
protected function init(): void protected function init(): void
{ {
$this->cgView->viewPath = ROOT_DIR . "/views/admin/"; $this->cgView->viewPath = KERNEL_MODULES_DIR . "/post/views/";
$this->cgView->layout = "layouts/main.php"; $this->postService = new PostService();
} }
public function actionCreate(): void public function actionCreate(): void
{ {
$this->cgView->render("post/form.php"); $this->cgView->render("form.php");
} }
#[NoReturn] public function actionAdd(): void #[NoReturn] public function actionAdd(): void
{ {
$postForm = new CreatePostForm(); $postForm = new CreatePostForm();
$postService = new PostService();
$postForm->load($_REQUEST); $postForm->load($_REQUEST);
if ($postForm->validate()) { if ($postForm->validate()) {
$post = $postService->create($postForm); $post = $this->postService->create($postForm);
if ($post) { if ($post) {
$this->redirect("/admin/post/" . $post->id); $this->redirect("/admin/post/" . $post->id);
} }
@ -45,7 +44,7 @@ class PostController extends Controller
public function actionIndex($page_number = 1): void public function actionIndex($page_number = 1): void
{ {
$this->cgView->render("post/index.php", ['page_number' => $page_number]); $this->cgView->render("index.php", ['page_number' => $page_number]);
} }
/** /**
@ -58,7 +57,7 @@ class PostController extends Controller
if (!$content){ if (!$content){
throw new Exception(message: "The post not found"); throw new Exception(message: "The post not found");
} }
$this->cgView->render("post/view.php", ['content' => $content]); $this->cgView->render("view.php", ['content' => $content]);
} }
/** /**
@ -71,7 +70,7 @@ class PostController extends Controller
throw new Exception(message: "The post not found"); throw new Exception(message: "The post not found");
} }
$this->cgView->render("post/form.php", ['model' => $model]); $this->cgView->render("form.php", ['model' => $model]);
} }
/** /**
@ -86,12 +85,10 @@ class PostController extends Controller
$postForm = new CreatePostForm(); $postForm = new CreatePostForm();
$postService = new PostService(); $postService = new PostService();
$postForm->load($_REQUEST); $postForm->load($_REQUEST);
if((new UserService)->check($_REQUEST['user_id'])) { if ($postForm->validate()) {
if ($postForm->validate()) { $post = $postService->update($postForm, $post);
$post = $postService->update($postForm, $post); if ($post) {
if ($post) { $this->redirect("/admin/post/" . $post->id);
$this->redirect("/admin/post/" . $post->id);
}
} }
} }
$this->redirect("/admin/post/update/" . $id); $this->redirect("/admin/post/update/" . $id);

View File

@ -1,6 +1,6 @@
<?php <?php
namespace app\models; namespace kernel\modules\post\models;
use \Illuminate\Database\Eloquent\Model; use \Illuminate\Database\Eloquent\Model;
/** /**

View File

@ -1,8 +1,8 @@
<?php <?php
namespace app\services; namespace kernel\modules\post\service;
use app\models\Post; use kernel\modules\post\models\Post;
use kernel\FormModel; use kernel\FormModel;
class PostService class PostService

View File

@ -3,7 +3,7 @@
* @var Post $model * @var Post $model
*/ */
use app\models\Post; use kernel\modules\post\models\Post;
$form = new \itguild\forms\ActiveForm(); $form = new \itguild\forms\ActiveForm();
$form->beginForm(isset($model) ? "/admin/post/edit/" . $model->id : "/admin/post"); $form->beginForm(isset($model) ? "/admin/post/edit/" . $model->id : "/admin/post");
@ -21,7 +21,7 @@ $form->field(class: \itguild\forms\inputs\Select::class, name: "user_id", params
'value' => $model->user_id ?? '' 'value' => $model->user_id ?? ''
]) ])
->setLabel("Пользователи") ->setLabel("Пользователи")
->setOptions(\app\services\UserService::createUsernameArr()) ->setOptions(\kernel\modules\user\service\UserService::createUsernameArr())
->render(); ->render();
?> ?>

View File

@ -5,8 +5,8 @@
* @var int $page_number * @var int $page_number
*/ */
use app\models\Post; use kernel\modules\post\models\Post;
use app\models\User; use kernel\modules\user\models\User;
use app\tables\columns\post\PostDeleteActionColumn; use app\tables\columns\post\PostDeleteActionColumn;
use app\tables\columns\post\PostEditActionColumn; use app\tables\columns\post\PostEditActionColumn;
use app\tables\columns\post\PostViewActionColumn; use app\tables\columns\post\PostViewActionColumn;

View File

@ -4,7 +4,7 @@
* @var \Illuminate\Database\Eloquent\Collection $content * @var \Illuminate\Database\Eloquent\Collection $content
*/ */
use app\models\User; use kernel\modules\user\models\User;
use Itguild\EloquentTable\ViewEloquentTable; use Itguild\EloquentTable\ViewEloquentTable;
use Itguild\EloquentTable\ViewJsonTableEloquentModel; use Itguild\EloquentTable\ViewJsonTableEloquentModel;
use kernel\IGTabel\btn\DangerBtn; use kernel\IGTabel\btn\DangerBtn;

View File

@ -1,11 +1,11 @@
<?php <?php
namespace app\controllers; namespace kernel\modules\user\controllers;
use app\models\forms\CreateUserForm; use app\models\forms\CreateUserForm;
use app\models\Question; use kernel\modules\user\models\User;
use app\models\User; use kernel\AdminController;
use app\services\UserService; use kernel\modules\user\service\UserService;
use Exception; use Exception;
use JetBrains\PhpStorm\NoReturn; use JetBrains\PhpStorm\NoReturn;
use kernel\Controller; use kernel\Controller;
@ -13,25 +13,29 @@ use Twig\Error\LoaderError;
use Twig\Error\RuntimeError; use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError; use Twig\Error\SyntaxError;
class UserController extends Controller{ class UserController extends AdminController
{
private UserService $userService;
protected function init(): void protected function init(): void
{ {
$this->cgView->viewPath = ROOT_DIR . "/views/admin/"; parent::init();
$this->cgView->layout = "layouts/main.php"; $this->cgView->viewPath = KERNEL_MODULES_DIR . "/user/views/";
$this->userService = new UserService();
} }
public function actionCreate(): void public function actionCreate(): void
{ {
$this->cgView->render("user/form.php"); $this->cgView->render("form.php");
} }
#[NoReturn] public function actionAdd(): void #[NoReturn] public function actionAdd(): void
{ {
$userForm = new CreateUserForm(); $userForm = new CreateUserForm();
$userService = new UserService();
$userForm->load($_REQUEST); $userForm->load($_REQUEST);
if ($userForm->validate()){ if ($userForm->validate()){
$user = $userService->create($userForm); $user = $this->userService->create($userForm);
if ($user){ if ($user){
$this->redirect("/admin/user/" . $user->id); $this->redirect("/admin/user/" . $user->id);
} }
@ -39,17 +43,12 @@ class UserController extends Controller{
$this->redirect("/admin/user/create"); $this->redirect("/admin/user/create");
} }
public function actionQuestionCount($user_id)
{
return Question::where('user_id', $user_id)->count();
}
/** /**
* @throws \Exception * @throws \Exception
*/ */
public function actionIndex($page_number = 1): void public function actionIndex($page_number = 1): void
{ {
$this->cgView->render("user/index.php", ['page_number' => $page_number]); $this->cgView->render("index.php", ['page_number' => $page_number]);
} }
/** /**
@ -62,7 +61,7 @@ class UserController extends Controller{
if (!$user){ if (!$user){
throw new Exception(message: "The user not found"); throw new Exception(message: "The user not found");
} }
$this->cgView->render("user/view.php", ['user' => $user]); $this->cgView->render("view.php", ['user' => $user]);
} }
/** /**
@ -77,7 +76,7 @@ class UserController extends Controller{
throw new Exception(message: "The user not found"); throw new Exception(message: "The user not found");
} }
$this->cgView->render("user/form.php", ['model' => $model]); $this->cgView->render("form.php", ['model' => $model]);
} }
/** /**

View File

@ -1,5 +1,5 @@
<?php <?php
namespace app\models; namespace kernel\modules\user\models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
/** /**

View File

@ -1,9 +1,9 @@
<?php <?php
namespace app\services; namespace kernel\modules\user\service;
use app\helpers\Debug; use app\helpers\Debug;
use app\models\User; use kernel\modules\user\models\User;
use kernel\FormModel; use kernel\FormModel;
class UserService class UserService

View File

@ -3,7 +3,7 @@
* @var User $model * @var User $model
*/ */
use app\models\User; use kernel\modules\user\models\User;
$form = new \itguild\forms\ActiveForm(); $form = new \itguild\forms\ActiveForm();
$form->beginForm(isset($model) ? "/admin/user/edit/" . $model->id : "/admin/user"); $form->beginForm(isset($model) ? "/admin/user/edit/" . $model->id : "/admin/user");

View File

@ -4,13 +4,13 @@
* @var int $page_number * @var int $page_number
*/ */
use app\models\User;
use app\tables\columns\user\UserDeleteActionColumn; use app\tables\columns\user\UserDeleteActionColumn;
use app\tables\columns\user\UserEditActionColumn; use app\tables\columns\user\UserEditActionColumn;
use app\tables\columns\user\UserViewActionColumn; use app\tables\columns\user\UserViewActionColumn;
use Itguild\EloquentTable\EloquentDataProvider; use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\EloquentTable\ListEloquentTable; use Itguild\EloquentTable\ListEloquentTable;
use kernel\IGTabel\btn\PrimaryBtn; use kernel\IGTabel\btn\PrimaryBtn;
use kernel\modules\user\models\User;
$table = new ListEloquentTable(new EloquentDataProvider(User::class, [ $table = new ListEloquentTable(new EloquentDataProvider(User::class, [
'currentPage' => $page_number, 'currentPage' => $page_number,

View File

@ -4,7 +4,7 @@
* @var \Illuminate\Database\Eloquent\Collection $user * @var \Illuminate\Database\Eloquent\Collection $user
*/ */
use app\models\User; use kernel\modules\user\models\User;
use Itguild\EloquentTable\ViewEloquentTable; use Itguild\EloquentTable\ViewEloquentTable;
use Itguild\EloquentTable\ViewJsonTableEloquentModel; use Itguild\EloquentTable\ViewJsonTableEloquentModel;
use kernel\IGTabel\btn\DangerBtn; use kernel\IGTabel\btn\DangerBtn;

View File

@ -8,24 +8,24 @@ use Phroute\Phroute\RouteCollector;
App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){
App::$collector->group(["prefix" => "user"], callback: function (RouteCollector $router){ App::$collector->group(["prefix" => "user"], callback: function (RouteCollector $router){
App::$collector->get('/', [\app\controllers\UserController::class, 'actionIndex']); App::$collector->get('/', [\kernel\modules\user\controllers\UserController::class, 'actionIndex']);
App::$collector->get('/page/{page_number}', [\app\controllers\UserController::class, 'actionIndex']); App::$collector->get('/page/{page_number}', [\kernel\modules\user\controllers\UserController::class, 'actionIndex']);
App::$collector->get('/create', [\app\controllers\UserController::class, 'actionCreate']); App::$collector->get('/create', [\kernel\modules\user\controllers\UserController::class, 'actionCreate']);
App::$collector->post("/", [\app\controllers\UserController::class, 'actionAdd']); App::$collector->post("/", [\kernel\modules\user\controllers\UserController::class, 'actionAdd']);
App::$collector->get('/{id}', [\app\controllers\UserController::class, 'actionView']); App::$collector->get('/{id}', [\kernel\modules\user\controllers\UserController::class, 'actionView']);
App::$collector->any('/update/{id}', [\app\controllers\UserController::class, 'actionUpdate']); App::$collector->any('/update/{id}', [\kernel\modules\user\controllers\UserController::class, 'actionUpdate']);
App::$collector->any("/edit/{id}", [\app\controllers\UserController::class, 'actionEdit']); App::$collector->any("/edit/{id}", [\kernel\modules\user\controllers\UserController::class, 'actionEdit']);
App::$collector->get('/delete/{id}', [\app\controllers\UserController::class, 'actionDelete']); App::$collector->get('/delete/{id}', [\kernel\modules\user\controllers\UserController::class, 'actionDelete']);
}); });
App::$collector->group(["prefix" => "post"], function (RouteCollector $router){ App::$collector->group(["prefix" => "post"], function (RouteCollector $router){
App::$collector->get('/', [\app\controllers\PostController::class, 'actionIndex']); App::$collector->get('/', [\kernel\modules\post\controllers\PostController::class, 'actionIndex']);
App::$collector->get('/page/{page_number}', [\app\controllers\PostController::class, 'actionIndex']); App::$collector->get('/page/{page_number}', [\kernel\modules\post\controllers\PostController::class, 'actionIndex']);
App::$collector->get('/create', [\app\controllers\PostController::class, 'actionCreate']); App::$collector->get('/create', [\kernel\modules\post\controllers\PostController::class, 'actionCreate']);
App::$collector->post("/", [\app\controllers\PostController::class, 'actionAdd']); App::$collector->post("/", [\kernel\modules\post\controllers\PostController::class, 'actionAdd']);
App::$collector->get('/{id}', [\app\controllers\PostController::class, 'actionView']); App::$collector->get('/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionView']);
App::$collector->any('/update/{id}', [\app\controllers\PostController::class, 'actionUpdate']); App::$collector->any('/update/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionUpdate']);
App::$collector->any("/edit/{id}", [\app\controllers\PostController::class, 'actionEdit']); App::$collector->any("/edit/{id}", [\kernel\modules\post\controllers\PostController::class, 'actionEdit']);
App::$collector->get('/delete/{id}', [\app\controllers\PostController::class, 'actionDelete']); App::$collector->get('/delete/{id}', [\kernel\modules\post\controllers\PostController::class, 'actionDelete']);
}); });
App::$collector->group(["prefix" => "settings"], function (RouteCollector $router){ App::$collector->group(["prefix" => "settings"], function (RouteCollector $router){
App::$collector->group(["prefix" => "menu"], function (RouteCollector $router){ App::$collector->group(["prefix" => "menu"], function (RouteCollector $router){

View File

@ -1,49 +0,0 @@
<?php
namespace kernel\services;
use app\helpers\Debug;
use kernel\models\Menu;
use kernel\Request;
class MenuService
{
public static function getChild(int $id)
{
$collection = Menu::where("parent_id", $id)->get();
if (!$collection->isEmpty()){
return $collection;
}
return false;
}
public static function hasActiveChild(int $id): bool
{
$child = self::getChild($id);
if (!$child->isEmpty()){
foreach ($child as $item){
// if ($item->url === \kernel\Request::getUrlPath()){
// return true;
// }
if (strripos(Request::getUrlPath(), $item->url) !== false) {
return true;
}
}
}
return false;
}
public static function isActive($url): bool
{
if ($url === Request::getUrlPath()){
return true;
} else {
if (strripos(\kernel\Request::getUrlPath(), ($url . "/page")) !== false) {
return true;
}
}
return false;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@ -1,78 +0,0 @@
<?php
/**
* @var Menu $model
*/
use kernel\models\Menu;
$form = new \itguild\forms\ActiveForm();
$form->beginForm(isset($model) ? "/admin/menu/edit/" . $model->id : "/admin/menu");
$form->field(class: \itguild\forms\inputs\Select::class, name: "parent_id", params: [
'class' => "form-control",
'value' => $model->parent_id ?? ''
])
->setLabel("Родительский пункт меню")
->setOptions(\app\services\MenuService::createLabelArr())
->render();
$form->field(class: \itguild\forms\inputs\File::class, name: "icon_file", params: [
'class' => "form-control",
'value' => $model->icon_file ?? ''
])
->setLabel("Путь к иконке")
->render();
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "icon_font", params: [
'class' => "form-control",
'value' => $model->icon_font ?? ''
])
->setLabel("Иконка")
->render();
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "label", params: [
'class' => "form-control",
'value' => $model->label ?? ''
])
->setLabel("Заголовок")
->render();
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "url", params: [
'class' => "form-control",
'value' => $model->url ?? ''
])
->setLabel("URL")
->render();
$form->field(class: \itguild\forms\inputs\Select::class, name: "status", params: [
'class' => "form-control",
'value' => $model->status ?? '1'
])
->setLabel("Статус")
->setOptions(Menu::getStatus())
->render();
?>
<div class="row">
<div class="col-sm-2">
<?php
$form->field(\itguild\forms\inputs\Button::class, name: "btn-submit", params: [
'class' => "btn btn-primary ",
'value' => 'Отправить',
'typeInput' => 'submit'
])
->render();
?>
</div>
<div class="col-sm-2">
<?php
$form->field(\itguild\forms\inputs\Button::class, name: "btn-reset", params: [
'class' => "btn btn-warning",
'value' => 'Сбросить',
'typeInput' => 'reset'
])
->render();
?>
</div>
</div>
<?php
$form->endForm();

View File

@ -1,35 +0,0 @@
<?php
/**
* @var \Illuminate\Database\Eloquent\Collection $menuItem
* @var int $page_number
*/
use app\tables\columns\menu\MenuDeleteActionColumn;
use app\tables\columns\menu\MenuEditActionColumn;
use app\tables\columns\menu\MenuViewActionColumn;
use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\EloquentTable\ListEloquentTable;
use kernel\IGTabel\btn\PrimaryBtn;
use kernel\models\Menu;
$table = new ListEloquentTable(new EloquentDataProvider(Menu::class, [
'currentPage' => $page_number,
'perPage' => 8,
'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/menu",
]));
$table->columns([
'parent_id' => (function ($data) {
if ($data == 0) return null;
return Menu::find($data)->label;
})
]);
$table->beforePrint(function () {
return PrimaryBtn::create("Создать", "/admin/menu/create")->fetch();
//return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch();
});
$table->addAction(MenuViewActionColumn::class);
$table->addAction(MenuEditActionColumn::class);
$table->addAction(MenuDeleteActionColumn::class);
$table->create();
$table->render();

View File

@ -1,46 +0,0 @@
<?php
/**
* @var \Illuminate\Database\Eloquent\Collection $menu
*/
use app\models\User;
use Itguild\EloquentTable\ViewEloquentTable;
use Itguild\EloquentTable\ViewJsonTableEloquentModel;
use kernel\IGTabel\btn\DangerBtn;
use kernel\IGTabel\btn\PrimaryBtn;
use kernel\IGTabel\btn\SuccessBtn;
use kernel\models\Menu;
$table = new ViewEloquentTable(new ViewJsonTableEloquentModel($menu, [
'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/menu",
]));
$table->beforePrint(function () use ($menu) {
$btn = PrimaryBtn::create("Список", "/admin/menu")->fetch();
$btn .= SuccessBtn::create("Редактировать", "/admin/menu/update/" . $menu->id)->fetch();
$btn .= DangerBtn::create("Удалить", "/admin/menu/delete/" . $menu->id)->fetch();
return $btn;
});
$table->rows([
'parent_id' => (function ($data) {
if ($data == 0) return null;
return Menu::find($data)->label;
}),
'created_at' => function ($data) {
if (!$data){
return null;
}
return (new DateTimeImmutable($data))->format("d-m-Y");
},
'updated_at' => function ($data) {
if (!$data){
return null;
}
return (new DateTimeImmutable($data))->format("d-m-Y");
}
]);
$table->create();
$table->render();

View File

@ -1,19 +0,0 @@
<!DOCTYPE html>
<html lang="ru-RU">
<head>
<meta charset="UTF-8">
<title>Примеры шаблонизатора Twig</title>
<link rel="stylesheet" href="/resources/css/bootstrap.css">
</head>
<body>
<div class="container">
<div class="row">
<h1>HEADER</h1>
{% block content %}
{% endblock %}
<h1>FOOTER</h1>
</div>
</div>
</body>
</html>

View File

@ -1,13 +0,0 @@
{% extends "main_layout.html.twig" %}
{% block content %}
<form action="/admin/question" method="post">
Вопрос: <br>
<label>
<textarea name = "question" rows="10" cols="50" placeholder="Введите здесь ваш вопрос"></textarea>
</label> <br> <br>
<input type = "submit" value="Подтвердить">
<input type="reset">
</form>
{% endblock %}

View File

@ -1,23 +0,0 @@
{% extends "main_layout.html.twig" %}
{% block content %}
<form action="/admin/user/" method="post">
Логин:<br>
<label>
<input type = "text" name = "username" required size="50" autofocus placeholder="Логин">
</label> <br> <br>
Пароль:<br>
<label>
<input type = "text" name = "password" placeholder="Пароль">
</label> <br> <br>
Email адрес: <br>
<label>
<input type="Email" name="email" required placeholder="Email">
</label> <br><br>
<input type = "submit" value="Подтвердить">
<input type="reset">
</form>
{% endblock %}

View File

@ -1,5 +0,0 @@
{% extends "main_layout.html.twig" %}
{% block content %}
{{ table() }}
{% endblock %}

View File

@ -1,23 +0,0 @@
{% extends "main_layout.html.twig" %}
{% block content %}
<form action="/admin/user/edit" method="post">
Логин:<br>
<label>
<input type = "text" name = "username" required size="50" autofocus placeholder="Логин">
</label> <br> <br>
Пароль:<br>
<label>
<input type = "text" name = "password" placeholder="Пароль">
</label> <br> <br>
Email адрес: <br>
<label>
<input type="Email" name="email" required placeholder="Email">
</label> <br><br>
<input type = "submit" value="Подтвердить">
<input type="reset">
</form>
{% endblock %}

View File

@ -5,13 +5,13 @@
?> ?>
<ul class="list-unstyled components mb-5"> <ul class="list-unstyled components mb-5">
<?php foreach ($menu as $item): <?php foreach ($menu as $item):
$child = \kernel\services\MenuService::getChild($item->id); $child = \kernel\modules\menu\service\MenuService::getChild($item->id);
if ($child): ?> if ($child): ?>
<li> <li>
<a href="#item<?=$item->id?>" data-toggle="collapse" aria-expanded="false" class="dropdown-toggle"><?= $item->label ?></a> <a href="#item<?=$item->id?>" data-toggle="collapse" aria-expanded="false" class="dropdown-toggle"><?= $item->label ?></a>
<ul class="collapse list-unstyled <?= \kernel\services\MenuService::hasActiveChild($item->id) ? "show" : "" ?>" id="item<?=$item->id?>"> <ul class="collapse list-unstyled <?= \kernel\modules\menu\service\MenuService::hasActiveChild($item->id) ? "show" : "" ?>" id="item<?=$item->id?>">
<?php foreach ($child as $subitem): ?> <?php foreach ($child as $subitem): ?>
<li class="<?= \kernel\services\MenuService::isActive($subitem->url) ? "active" : "" ?>"> <li class="<?= \kernel\modules\menu\service\MenuService::isActive($subitem->url) ? "active" : "" ?>">
<a href="<?= $subitem->url ?>"><?= $subitem->label ?></a> <a href="<?= $subitem->url ?>"><?= $subitem->label ?></a>
</li> </li>
<?php endforeach; ?> <?php endforeach; ?>