Compare commits
16 Commits
e7a20d9b97
...
master
Author | SHA1 | Date | |
---|---|---|---|
499f2a37d2 | |||
c7549c225f | |||
bfeb2d3c56 | |||
0e0bc80260 | |||
ccd46636cf | |||
95a9b47fd5 | |||
6c124e9bb7 | |||
aff394ae72 | |||
c0bedcb5ee | |||
159b3933fb | |||
13978449a2 | |||
a1bed2d9f2 | |||
567ab8544d | |||
b981ff0c44 | |||
0ed97877fd | |||
3ef1e7d7e0 |
21
.env.example
21
.env.example
@ -1,16 +1,23 @@
|
||||
APP_NAME="It Guild Micro Framework"
|
||||
|
||||
DB_HOST=localhost
|
||||
DB_USER=root
|
||||
DB_USER={db_user}
|
||||
DB_DRIVER=mysql
|
||||
DB_PASSWORD=123edsaqw
|
||||
DB_NAME=mfw
|
||||
DB_CHARSET=utf8
|
||||
DB_COLLATION=utf8_unicode_ci
|
||||
DB_PASSWORD={db_password}
|
||||
DB_NAME={db_name}
|
||||
DB_CHARSET=utf8mb4
|
||||
DB_COLLATION=utf8mb4_unicode_ci
|
||||
DB_PREFIX=''
|
||||
|
||||
VIEWS_PATH=/views
|
||||
VIEWS_CACHE_PATH=/views_cache
|
||||
|
||||
MODULE_SHOP_URL='http://localhost:8383/api'
|
||||
MODULE_SHOP_TOKEN='eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.W10.Sisyc1bgy03TI0wT11oKhgh5J6vR9XWDOV56L6BiTJY'
|
||||
MAIL_SMTP_HOST=smtp.mail.ru
|
||||
MAIL_SMTP_PORT=587
|
||||
MAIL_SMTP_USERNAME=username@mail.ru
|
||||
MAIL_SMTP_PASSWORD=somepassword
|
||||
|
||||
MODULE_SHOP_URL='http://igfs.loc'
|
||||
MODULE_SHOP_TOKEN='your token'
|
||||
|
||||
SECRET_KEY=''
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -3,4 +3,5 @@ vendor
|
||||
.env
|
||||
views_cache
|
||||
resources/upload
|
||||
resources/tmp
|
||||
resources/tmp
|
||||
composer.lock
|
115
app/modules/photo/PhotoModule.php
Normal file
115
app/modules/photo/PhotoModule.php
Normal file
@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace app\modules\photo;
|
||||
|
||||
use Cassandra\Decimal;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use itguild\forms\builders\FileBuilder;
|
||||
use kernel\app_modules\photo\models\Photo;
|
||||
use kernel\app_modules\photo\services\PhotoService;
|
||||
use kernel\app_modules\tag\services\TagEntityService;
|
||||
use kernel\FileUpload;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\helpers\Html;
|
||||
use kernel\Module;
|
||||
use kernel\modules\menu\service\MenuService;
|
||||
use kernel\Request;
|
||||
use kernel\services\MigrationService;
|
||||
|
||||
class PhotoModule extends Module
|
||||
{
|
||||
|
||||
public MenuService $menuService;
|
||||
public MigrationService $migrationService;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->menuService = new MenuService();
|
||||
$this->migrationService = new MigrationService();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function init(): void
|
||||
{
|
||||
$this->migrationService->runAtPath("{KERNEL_APP_MODULES}/photo/migrations");
|
||||
|
||||
$this->menuService->createItem([
|
||||
"label" => "Фото",
|
||||
"url" => "/admin/photo",
|
||||
"slug" => "photo",
|
||||
]);
|
||||
|
||||
$this->menuService->createItem([
|
||||
"label" => "Фото",
|
||||
"url" => "/admin/settings/photo",
|
||||
"slug" => "photo_settings",
|
||||
"parent_slug" => "settings"
|
||||
]);
|
||||
}
|
||||
|
||||
public function deactivate(): void
|
||||
{
|
||||
$this->menuService->removeItemBySlug("photo");
|
||||
$this->menuService->removeItemBySlug("photo_settings");
|
||||
}
|
||||
|
||||
public function formInputs(string $entity, Model $model = null): void
|
||||
{
|
||||
if (isset($model->id)) {
|
||||
$value = PhotoService::getByEntity($entity, $model->id);
|
||||
echo Html::img($value, ['width' => '200px']);
|
||||
}
|
||||
$input = FileBuilder::build("image", [
|
||||
'class' => 'form-control',
|
||||
'value' => $value ?? '',
|
||||
]);
|
||||
$input->setLabel("Фото");
|
||||
$input->create()->render();
|
||||
}
|
||||
|
||||
public function saveInputs(string $entity, Model $model, Request $request): void
|
||||
{
|
||||
Photo::where("entity", $entity)->where("entity_id", $model->id)->delete();
|
||||
|
||||
if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
|
||||
$file = new FileUpload($_FILES['image'], ['jpg', 'jpeg', 'png']);
|
||||
$file->upload();
|
||||
$image = $file->getUploadFile();
|
||||
$photo = new Photo();
|
||||
$photo->entity = $entity;
|
||||
$photo->entity_id = $model->id;
|
||||
$photo->image = $image;
|
||||
$photo->save();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function getItems(string $entity, Model $model): array|string
|
||||
{
|
||||
$photos = Photo::where("entity", $entity)->where("entity_id", $model->id)->get();
|
||||
$photoStr = "";
|
||||
foreach ($photos as $photo) {
|
||||
$photoStr .= "<img src='$photo->image' width='150px'>" . " ";
|
||||
}
|
||||
|
||||
return substr($photoStr, 0, -1);
|
||||
}
|
||||
|
||||
public function getItem(string $entity, string $entity_id): string
|
||||
{
|
||||
$photos = Photo::where("entity", $entity)->where("entity_id", $entity_id)->get();
|
||||
$photoStr = "";
|
||||
foreach ($photos as $photo) {
|
||||
$photoStr .= "<img src='$photo->image' width='150px'>" . " ";
|
||||
}
|
||||
|
||||
return substr($photoStr, 0, -1);
|
||||
}
|
||||
|
||||
public function deleteItems(string $entity, Model $model): void
|
||||
{
|
||||
Photo::where("entity", $entity)->where("entity_id", $model->id)->delete();
|
||||
}
|
||||
}
|
8
app/modules/photo/controllers/PhotoController.php
Normal file
8
app/modules/photo/controllers/PhotoController.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace app\modules\photo\controllers;
|
||||
|
||||
class PhotoController extends \kernel\app_modules\photo\controllers\PhotoController
|
||||
{
|
||||
|
||||
}
|
13
app/modules/photo/manifest.json
Normal file
13
app/modules/photo/manifest.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "Photo",
|
||||
"version": "0.1",
|
||||
"author": "ITGuild",
|
||||
"slug": "photo",
|
||||
"type": "additional_property",
|
||||
"description": "Photo module",
|
||||
"app_module_path": "{APP}/modules/{slug}",
|
||||
"module_class": "app\\modules\\photo\\PhotoModule",
|
||||
"module_class_file": "{APP}/modules/photo/PhotoModule.php",
|
||||
"routs": "routs/photo.php",
|
||||
"dependence": "menu"
|
||||
}
|
2
app/modules/photo/routs/photo.php
Normal file
2
app/modules/photo/routs/photo.php
Normal file
@ -0,0 +1,2 @@
|
||||
<?php
|
||||
include KERNEL_APP_MODULES_DIR . "/photo/routs/photo.php";
|
12
app/modules/slider/SliderModule.php
Normal file
12
app/modules/slider/SliderModule.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace app\modules\slider;
|
||||
|
||||
use kernel\Module;
|
||||
use kernel\modules\menu\service\MenuService;
|
||||
use kernel\services\MigrationService;
|
||||
|
||||
class SliderModule extends \kernel\app_modules\slider\SliderModule
|
||||
{
|
||||
|
||||
}
|
8
app/modules/slider/controllers/SliderController.php
Normal file
8
app/modules/slider/controllers/SliderController.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace app\modules\slider\controllers;
|
||||
|
||||
class SliderController extends \kernel\app_modules\slider\controllers\SliderController
|
||||
{
|
||||
|
||||
}
|
13
app/modules/slider/manifest.json
Normal file
13
app/modules/slider/manifest.json
Normal file
@ -0,0 +1,13 @@
|
||||
{
|
||||
"name": "Slider",
|
||||
"version": "0.1",
|
||||
"author": "ITGuild",
|
||||
"slug": "slider",
|
||||
"type": "entity",
|
||||
"description": "Slider module",
|
||||
"app_module_path": "{APP}/modules/{slug}",
|
||||
"module_class": "app\\modules\\slider\\SliderModule",
|
||||
"module_class_file": "{APP}/modules/slider/SliderModule.php",
|
||||
"routs": "routs/slider.php",
|
||||
"dependence": "menu"
|
||||
}
|
2
app/modules/slider/routs/slider.php
Normal file
2
app/modules/slider/routs/slider.php
Normal file
@ -0,0 +1,2 @@
|
||||
<?php
|
||||
include KERNEL_APP_MODULES_DIR . "/slider/routs/slider.php";
|
@ -65,6 +65,7 @@ class TagModule extends Module
|
||||
if (isset($model->id)) {
|
||||
$value = TagEntityService::getTagsByEntity($entity, $model->id);
|
||||
}
|
||||
|
||||
$input = SelectBuilder::build("tag[]", [
|
||||
'class' => 'form-control',
|
||||
'placeholder' => 'Теги',
|
||||
@ -81,12 +82,14 @@ class TagModule extends Module
|
||||
TagEntity::where("entity", $entity)->where("entity_id", $model->id)->delete();
|
||||
|
||||
$tags = $request->post("tag");
|
||||
foreach ($tags as $tag) {
|
||||
$tagEntity = new TagEntity();
|
||||
$tagEntity->entity = $entity;
|
||||
$tagEntity->entity_id = $model->id;
|
||||
$tagEntity->tag_id = $tag;
|
||||
$tagEntity->save();
|
||||
if (is_array($tags)) {
|
||||
foreach ($tags as $tag) {
|
||||
$tagEntity = new TagEntity();
|
||||
$tagEntity->entity = $entity;
|
||||
$tagEntity->entity_id = $model->id;
|
||||
$tagEntity->tag_id = $tag;
|
||||
$tagEntity->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@ const KERNEL_MODULES_DIR = __DIR__ . "/kernel/modules";
|
||||
const KERNEL_ADMIN_THEMES_DIR = __DIR__ . "/kernel/admin_themes";
|
||||
const CONSOLE_DIR = __DIR__ . "/kernel/console";
|
||||
const RESOURCES_DIR = __DIR__ . "/resources";
|
||||
|
||||
const KERNEL_TEMPLATES_DIR = __DIR__ . "/kernel/templates";
|
||||
const KERNEL_APP_MODULES_DIR = KERNEL_DIR . "/app_modules";
|
||||
|
||||
const APP_DIR = ROOT_DIR . "/app";
|
||||
@ -29,6 +29,7 @@ function getConst($text): array|false|string
|
||||
"{KERNEL}" => KERNEL_DIR,
|
||||
"{KERNEL_MODULES}" => KERNEL_MODULES_DIR,
|
||||
"{KERNEL_APP_MODULES}" => KERNEL_APP_MODULES_DIR,
|
||||
"{KERNEL_TEMPLATES}" => KERNEL_TEMPLATES_DIR,
|
||||
"{CONSOLE}" => CONSOLE_DIR,
|
||||
"{APP}" => APP_DIR,
|
||||
];
|
||||
|
@ -1,7 +1,8 @@
|
||||
<?php
|
||||
|
||||
$secure_config = [
|
||||
'token_type' => 'JWT', // random_bytes, md5, crypt, hash, JWT
|
||||
'web_auth_type' => 'email_code', // login_password, email_code
|
||||
'token_type' => 'hash', // random_bytes, md5, crypt, hash, JWT
|
||||
'token_expired_time' => "+30 days", // +1 day
|
||||
];
|
||||
|
||||
|
@ -18,7 +18,10 @@
|
||||
"josantonius/session": "^2.0",
|
||||
"firebase/php-jwt": "^6.10",
|
||||
"k-adam/env-editor": "^2.0",
|
||||
"guzzlehttp/guzzle": "^7.9"
|
||||
"guzzlehttp/guzzle": "^7.9",
|
||||
"phpmailer/phpmailer": "^6.9",
|
||||
"zircote/swagger-php": "^4.11",
|
||||
"doctrine/annotations": "^2.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
|
3209
composer.lock
generated
3209
composer.lock
generated
File diff suppressed because it is too large
Load Diff
@ -46,8 +46,16 @@ class CgRouteCollector extends RouteCollector
|
||||
//TODO
|
||||
}
|
||||
|
||||
public function console($route, $handler, array $filters = []): void
|
||||
/**
|
||||
* @param $route
|
||||
* @param $handler
|
||||
* @param array $filters
|
||||
* @param array $additionalInfo
|
||||
* @return void
|
||||
*/
|
||||
public function console($route, $handler, array $filters = [], array $additionalInfo = []): void
|
||||
{
|
||||
$this->addRoute(Route::GET, $route, $handler, $filters);
|
||||
$additionalInfo['type'] = "console";
|
||||
$this->addRoute(Route::GET, $route, $handler, $filters, $additionalInfo);
|
||||
}
|
||||
}
|
@ -10,6 +10,8 @@ class CgView
|
||||
public array $varToLayout = [];
|
||||
public bool|string $layout = false;
|
||||
|
||||
protected array $metaArr = [];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
@ -34,14 +36,37 @@ class CgView
|
||||
$this->varToLayout[$key] = $value;
|
||||
}
|
||||
|
||||
private function createContent(string $view, array $data = []): false|string
|
||||
public function setTitle(string $title): void
|
||||
{
|
||||
$this->addVarToLayout('title', $title);
|
||||
}
|
||||
|
||||
public function setMeta(array $meta): void
|
||||
{
|
||||
foreach ($meta as $key => $value){
|
||||
$this->metaArr[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
public function getMeta(): string
|
||||
{
|
||||
$meta = "";
|
||||
foreach ($this->metaArr as $key => $value){
|
||||
$meta .= "<meta name='$key' content='$value'>";
|
||||
}
|
||||
|
||||
return $meta;
|
||||
}
|
||||
|
||||
private function createContent(string $viewFile, array $data = []): false|string
|
||||
{
|
||||
ob_start();
|
||||
$view = $this;
|
||||
foreach ($data as $key => $datum) {
|
||||
${"$key"} = $datum;
|
||||
}
|
||||
|
||||
include($this->viewPath . $view);
|
||||
include($this->viewPath . $viewFile);
|
||||
|
||||
$content = ob_get_contents();
|
||||
ob_end_clean();
|
||||
@ -50,6 +75,10 @@ class CgView
|
||||
|
||||
$file_content = $content;
|
||||
|
||||
if (!isset($title)){
|
||||
$title = "No Title";
|
||||
}
|
||||
|
||||
$layoutPath = $this->viewPath;
|
||||
|
||||
if ($this->layout) {
|
||||
|
@ -25,7 +25,7 @@ class EntityRelation
|
||||
$activeModules = $moduleService->getActiveModules();
|
||||
foreach ($activeModules as $module) {
|
||||
if (isset($module['type']) and $module['type'] === "entity") {
|
||||
$list[$module['slug']] = $module['name'];
|
||||
$list[$module['slug']] = $module['slug'];
|
||||
}
|
||||
}
|
||||
|
||||
@ -45,7 +45,7 @@ class EntityRelation
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function getEntitiesRelations(): array|bool
|
||||
public static function getEntitiesRelations(): array|bool
|
||||
{
|
||||
$entity_relations = OptionService::getItem("entity_relations");
|
||||
if ($entity_relations) {
|
||||
@ -61,8 +61,13 @@ class EntityRelation
|
||||
if ($entity_relations_info) {
|
||||
$entity_relations = json_decode($entity_relations_info->value, true);
|
||||
if ($entity_relations[$entity]) {
|
||||
if ($entity_relations[$entity][$property]) {
|
||||
unset($entity_relations[$entity][$property]);
|
||||
$propertyKey = array_search($property, $entity_relations[$entity]);
|
||||
if ($entity_relations[$entity][$propertyKey] === $property) {
|
||||
unset($entity_relations[$entity][$propertyKey]);
|
||||
$entity_relations[$entity] = array_values($entity_relations[$entity]);
|
||||
if (empty($entity_relations[$entity])) {
|
||||
unset($entity_relations[$entity]);
|
||||
}
|
||||
$entity_relations_info->value = json_encode($entity_relations, JSON_UNESCAPED_UNICODE);
|
||||
$entity_relations_info->save();
|
||||
return true;
|
||||
@ -118,10 +123,10 @@ class EntityRelation
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return [];
|
||||
}
|
||||
|
||||
public function addEntityRelation(string $entity, string $property): bool
|
||||
public static function addEntityRelation(string $entity, string $property): bool
|
||||
{
|
||||
$entity_relations_info = Option::where("key", "entity_relations")->first();
|
||||
if ($entity_relations_info) {
|
||||
@ -129,7 +134,7 @@ class EntityRelation
|
||||
if (isset($entity_relations[$entity])) {
|
||||
$entity_relations[$entity][] = $property;
|
||||
} else {
|
||||
$entity_relations[$entity] = $property;
|
||||
$entity_relations[$entity][] = $property;
|
||||
}
|
||||
$entity_relations_info->value = json_encode($entity_relations, JSON_UNESCAPED_UNICODE);
|
||||
$entity_relations_info->save();
|
||||
@ -231,4 +236,44 @@ class EntityRelation
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static function getEntityByProperty(string $data): array
|
||||
{
|
||||
$entityRelations = self::getEntitiesRelations();
|
||||
$entities = [];
|
||||
foreach ($entityRelations as $entity => $property) {
|
||||
if (in_array($data, $property)) {
|
||||
$entities[] = $entity;
|
||||
}
|
||||
}
|
||||
|
||||
return $entities;
|
||||
}
|
||||
|
||||
public static function configurationEntitiesByProperty(array|null $entities, string $property): void
|
||||
{
|
||||
$entityRelations = self::getEntitiesRelations();
|
||||
if (isset($entities)) {
|
||||
foreach ($entities as $entity) {
|
||||
if (!isset($entityRelations[$entity])) {
|
||||
EntityRelation::addEntityRelation($entity, $property);
|
||||
}
|
||||
}
|
||||
foreach ($entityRelations as $entity => $additionalProperty) {
|
||||
if (in_array($entity, $entities)) {
|
||||
if (!in_array($property, $additionalProperty)) {
|
||||
EntityRelation::addEntityRelation($entity, $property);
|
||||
}
|
||||
} else {
|
||||
if (in_array($property, $additionalProperty)) {
|
||||
EntityRelation::removePropertyFromEntityRelations($entity, $property);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
foreach ($entityRelations as $entity => $additionalProperty) {
|
||||
EntityRelation::removePropertyFromEntityRelations($entity, $property);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -59,6 +59,16 @@ class FormModel
|
||||
return false;
|
||||
}
|
||||
|
||||
public function validateForUpdate(): bool
|
||||
{
|
||||
$res = $this->validator->validate($this->data, $this->rulesForUpdate());
|
||||
if (!$res) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getErrors(): array
|
||||
{
|
||||
return $this->validator->getProcessedErrors();
|
||||
|
50
kernel/Mailing.php
Normal file
50
kernel/Mailing.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace kernel;
|
||||
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\helpers\SMTP;
|
||||
use PHPMailer\PHPMailer\Exception;
|
||||
|
||||
class Mailing
|
||||
{
|
||||
protected SMTP $SMTP;
|
||||
|
||||
protected CgView $cgView;
|
||||
protected array $data;
|
||||
|
||||
public function __construct(array $data = [])
|
||||
{
|
||||
$this->cgView = new CgView();
|
||||
$this->cgView->viewPath = KERNEL_DIR . "/views/mailing/";
|
||||
|
||||
$this->data = $data;
|
||||
|
||||
$this->SMTP = new SMTP();
|
||||
|
||||
$this->init();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function send_html(string $tpl, array $tplParams, array $mailParams): ?false
|
||||
{
|
||||
$mailParams['body'] = $this->cgView->fetch($tpl, $tplParams);
|
||||
return $this->SMTP->send_html($mailParams);
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
}
|
||||
|
||||
public static function create(array $data = []): static
|
||||
{
|
||||
return new static($data);
|
||||
}
|
||||
|
||||
protected function init()
|
||||
{
|
||||
}
|
||||
|
||||
}
|
@ -2,15 +2,18 @@
|
||||
/**
|
||||
* @var $content
|
||||
* @var string $resources
|
||||
* @var string $title
|
||||
* @var \kernel\CgView $view
|
||||
*/
|
||||
\Josantonius\Session\Facades\Session::start();
|
||||
?>
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Sidebar 01</title>
|
||||
<title><?= $title ?></title>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<?= $view->getMeta() ?>
|
||||
|
||||
<link href="https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700,800,900" rel="stylesheet">
|
||||
|
||||
|
77
kernel/app_modules/photo/controllers/PhotoController.php
Executable file
77
kernel/app_modules/photo/controllers/PhotoController.php
Executable file
@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\photo\controllers;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use kernel\AdminController;
|
||||
use kernel\app_modules\photo\models\form\CreatePhotoForm;
|
||||
use kernel\app_modules\photo\models\Photo;
|
||||
use kernel\app_modules\photo\services\PhotoService;
|
||||
use kernel\EntityRelation;
|
||||
use kernel\Flash;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\Request;
|
||||
|
||||
class PhotoController extends AdminController
|
||||
{
|
||||
private PhotoService $photoService;
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/photo/views/";
|
||||
$this->photoService = new PhotoService();
|
||||
}
|
||||
|
||||
public function actionCreate(): void
|
||||
{
|
||||
$this->cgView->render("form.php");
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionAdd(): void
|
||||
{
|
||||
$photoForm = new CreatePhotoForm();
|
||||
$photoForm->load($_REQUEST);
|
||||
if ($photoForm->validate()){
|
||||
$photo = $this->photoService->create($photoForm);
|
||||
if ($photo){
|
||||
$this->redirect("/admin/photo/view/" . $photo->id);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/photo/create");
|
||||
}
|
||||
|
||||
public function actionIndex($page_number = 1): void
|
||||
{
|
||||
$this->cgView->render("index.php", ['page_number' => $page_number]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionView($id): void
|
||||
{
|
||||
$photo = Photo::find($id);
|
||||
|
||||
if (!$photo){
|
||||
throw new Exception(message: "The photo not found");
|
||||
}
|
||||
$this->cgView->render("view.php", ['photo' => $photo]);
|
||||
}
|
||||
|
||||
public function actionSettings(): void
|
||||
{
|
||||
$this->cgView->render('settingsForm.php');
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionSaveSettings(): void
|
||||
{
|
||||
$request = new Request();
|
||||
$entities = $request->post('entity');
|
||||
EntityRelation::configurationEntitiesByProperty($entities, 'photo');
|
||||
|
||||
Flash::setMessage("success", "Настройка прошла успешно");
|
||||
$this->redirect("/admin/settings/photo", 302);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
\kernel\App::$db->schema->create('photo', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('image', 255)->nullable(false);
|
||||
$table->string('entity', 255)->nullable(false);
|
||||
$table->integer('entity_id')->default(1);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
\kernel\App::$db->schema->dropIfExists('photo');
|
||||
}
|
||||
};
|
44
kernel/app_modules/photo/models/Photo.php
Executable file
44
kernel/app_modules/photo/models/Photo.php
Executable file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\photo\models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $image
|
||||
* @property string $entity
|
||||
* @property int $entity_id
|
||||
*/
|
||||
class Photo extends Model
|
||||
{
|
||||
protected $table = 'photo';
|
||||
|
||||
protected $fillable = ['image', 'entity', 'entity_id'];
|
||||
|
||||
public static function labels(): array
|
||||
{
|
||||
return [
|
||||
'image' => 'Фото',
|
||||
'entity' => 'Сущность',
|
||||
'entity_id' => 'Идентификатор сущности',
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPhotoListByEntity(string $entity): array
|
||||
{
|
||||
return self::where("entity", $entity)->get()->toArray();
|
||||
}
|
||||
|
||||
public static function getPhotoByEntity(string $entity): array
|
||||
{
|
||||
$result = [];
|
||||
$photos = self::getPhotoListByEntity($entity);
|
||||
foreach ($photos as $photo) {
|
||||
$result[$photo['id']] = $photo['label'];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
19
kernel/app_modules/photo/models/form/CreatePhotoForm.php
Executable file
19
kernel/app_modules/photo/models/form/CreatePhotoForm.php
Executable file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\photo\models\form;
|
||||
|
||||
use kernel\FormModel;
|
||||
|
||||
class CreatePhotoForm extends FormModel
|
||||
{
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'image' => 'required',
|
||||
'entity' => 'required|string',
|
||||
'entity_id' => 'required|integer|min:1',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
24
kernel/app_modules/photo/routs/photo.php
Executable file
24
kernel/app_modules/photo/routs/photo.php
Executable file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
use kernel\App;
|
||||
use kernel\CgRouteCollector;
|
||||
use Phroute\Phroute\RouteCollector;
|
||||
|
||||
App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router) {
|
||||
App::$collector->group(["before" => "auth"], function (RouteCollector $router) {
|
||||
App::$collector->group(["prefix" => "photo"], function (CGRouteCollector $router) {
|
||||
App::$collector->get('/', [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionIndex']);
|
||||
App::$collector->get('/page/{page_number}', [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionIndex']);
|
||||
App::$collector->get('/create', [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionCreate']);
|
||||
App::$collector->post("/", [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionAdd']);
|
||||
App::$collector->get('/view/{id}', [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionView']);
|
||||
App::$collector->any('/update/{id}', [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionUpdate']);
|
||||
App::$collector->any("/edit/{id}", [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionEdit']);
|
||||
App::$collector->get('/delete/{id}', [\kernel\app_modules\photo\controllers\PhotoController::class]);
|
||||
});
|
||||
App::$collector->group(["prefix" => "settings"], function (CGRouteCollector $router) {
|
||||
App::$collector->get('/photo', [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionSettings']);
|
||||
App::$collector->post('/photo/update', [\kernel\app_modules\photo\controllers\PhotoController::class, 'actionSaveSettings']);
|
||||
});
|
||||
});
|
||||
});
|
40
kernel/app_modules/photo/services/PhotoService.php
Executable file
40
kernel/app_modules/photo/services/PhotoService.php
Executable file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\photo\services;
|
||||
|
||||
use kernel\app_modules\photo\models\Photo;
|
||||
use kernel\FormModel;
|
||||
use kernel\helpers\Debug;
|
||||
|
||||
class PhotoService
|
||||
{
|
||||
public function create(FormModel $form_model): false|Photo
|
||||
{
|
||||
$model = new Photo();
|
||||
$model->image = $form_model->getItem('image');
|
||||
$model->entity = $form_model->getItem('entity');
|
||||
$model->entity_id = $form_model->getItem('entity_id');
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function update(FormModel $form_model, Photo $photo): false|Photo
|
||||
{
|
||||
$photo->image = $form_model->getItem('image');
|
||||
$photo->entity = $form_model->getItem('entity');
|
||||
$photo->entity_id = $form_model->getItem('entity_id');
|
||||
|
||||
if ($photo->save()){
|
||||
return $photo;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function getByEntity(string $entity, int $entity_id): string
|
||||
{
|
||||
$photo = Photo::where("entity", $entity)->where("entity_id", $entity_id)->first();
|
||||
return $photo->image ?? "";
|
||||
}
|
||||
|
||||
}
|
41
kernel/app_modules/photo/views/index.php
Executable file
41
kernel/app_modules/photo/views/index.php
Executable file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/**
|
||||
* @var int $page_number
|
||||
*/
|
||||
|
||||
use Itguild\EloquentTable\EloquentDataProvider;
|
||||
use Itguild\EloquentTable\ListEloquentTable;
|
||||
use kernel\app_modules\photo\models\Photo;
|
||||
use kernel\widgets\IconBtn\IconBtnCreateWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnDeleteWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnEditWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnViewWidget;
|
||||
|
||||
$table = new ListEloquentTable(new EloquentDataProvider(Photo::class, [
|
||||
'currentPage' => $page_number,
|
||||
'perPage' => 8,
|
||||
'params' => ["class" => "table table-bordered", "border" => "2"],
|
||||
'baseUrl' => "/admin/photo",
|
||||
]));
|
||||
|
||||
//$table->beforePrint(function () {
|
||||
// return IconBtnCreateWidget::create(['url' => '/admin/photo/create'])->run();
|
||||
//});
|
||||
|
||||
$table->columns([
|
||||
'image' => function ($data) {
|
||||
return $data ? "<img src='$data' width='150px'>" : "";
|
||||
}
|
||||
]);
|
||||
|
||||
$table->addAction(function($row) {
|
||||
return IconBtnViewWidget::create(['url' => '/admin/photo/view/' . $row['id']])->run();
|
||||
});
|
||||
//$table->addAction(function($row) {
|
||||
// return IconBtnEditWidget::create(['url' => '/admin/photo/update/' . $row['id']])->run();
|
||||
//});
|
||||
//$table->addAction(function($row) {
|
||||
// return IconBtnDeleteWidget::create(['url' => '/admin/photo/delete/' . $row['id']])->run();
|
||||
//});
|
||||
$table->create();
|
||||
$table->render();
|
46
kernel/app_modules/photo/views/settingsForm.php
Normal file
46
kernel/app_modules/photo/views/settingsForm.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
$form = new \itguild\forms\ActiveForm();
|
||||
$form->beginForm("/admin/settings/photo/update");
|
||||
|
||||
?>
|
||||
|
||||
<div class="row">
|
||||
<h5>Выберите сущности, к которым хотите прикрепить фото</h5>
|
||||
</div>
|
||||
|
||||
<?php
|
||||
$form->field(\itguild\forms\inputs\Select::class, "entity[]", [
|
||||
'class' => "form-control",
|
||||
'value' => \kernel\EntityRelation::getEntityByProperty('photo') ?? '',
|
||||
'multiple' => "multiple",
|
||||
|
||||
])
|
||||
->setLabel("Сущности")
|
||||
->setOptions(\kernel\EntityRelation::getEntityList())
|
||||
->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();
|
34
kernel/app_modules/photo/views/view.php
Executable file
34
kernel/app_modules/photo/views/view.php
Executable file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Collection $photo
|
||||
*/
|
||||
|
||||
use kernel\modules\user\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\widgets\IconBtn\IconBtnDeleteWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnEditWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnListWidget;
|
||||
|
||||
$table = new ViewEloquentTable(new ViewJsonTableEloquentModel($photo, [
|
||||
'params' => ["class" => "table table-bordered", "border" => "2"],
|
||||
'baseUrl' => "/admin/photo",
|
||||
]));
|
||||
$table->beforePrint(function () use ($photo) {
|
||||
$btn = IconBtnListWidget::create(['url' => '/admin/photo'])->run();
|
||||
// $btn .= IconBtnEditWidget::create(['url' => '/admin/photo/update/' . $photo->id])->run();
|
||||
// $btn .= IconBtnDeleteWidget::create(['url' => '/admin/photo/delete/' . $photo->id])->run();
|
||||
return $btn;
|
||||
});
|
||||
|
||||
$table->rows([
|
||||
'image' => function ($data) {
|
||||
return $data ? "<img src='$data' width='300px'>" : "";
|
||||
}
|
||||
]);
|
||||
$table->create();
|
||||
$table->render();
|
38
kernel/app_modules/slider/SliderModule.php
Normal file
38
kernel/app_modules/slider/SliderModule.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\slider;
|
||||
|
||||
use kernel\Module;
|
||||
use kernel\modules\menu\service\MenuService;
|
||||
use kernel\services\MigrationService;
|
||||
|
||||
class SliderModule extends Module
|
||||
{
|
||||
public MenuService $menuService;
|
||||
public MigrationService $migrationService;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->menuService = new MenuService();
|
||||
$this->migrationService = new MigrationService();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function init(): void
|
||||
{
|
||||
$this->migrationService->runAtPath("{KERNEL_APP_MODULES}/slider/migrations");
|
||||
|
||||
$this->menuService->createItem([
|
||||
"label" => "Слайдер",
|
||||
"url" => "/admin/slider",
|
||||
"slug" => "slider",
|
||||
]);
|
||||
}
|
||||
|
||||
public function deactivate(): void
|
||||
{
|
||||
$this->menuService->removeItemBySlug("slider");
|
||||
}
|
||||
}
|
118
kernel/app_modules/slider/controllers/SliderController.php
Normal file
118
kernel/app_modules/slider/controllers/SliderController.php
Normal file
@ -0,0 +1,118 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\slider\controllers;
|
||||
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\app_modules\slider\models\forms\CreateSliderForm;
|
||||
use kernel\app_modules\slider\models\Slider;
|
||||
use kernel\app_modules\slider\services\SliderService;
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use kernel\AdminController;
|
||||
use kernel\EntityRelation;
|
||||
use kernel\Request;
|
||||
|
||||
class SliderController extends AdminController
|
||||
{
|
||||
private SliderService $sliderService;
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/slider/views/";
|
||||
$this->sliderService = new SliderService();
|
||||
}
|
||||
|
||||
public function actionCreate(): void
|
||||
{
|
||||
$this->cgView->render("form.php");
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionAdd(): void
|
||||
{
|
||||
$sliderForm = new CreateSliderForm();
|
||||
$sliderForm->load($_REQUEST);
|
||||
if ($sliderForm->validate()) {
|
||||
$slider = $this->sliderService->create($sliderForm);
|
||||
|
||||
$entityRelation = new EntityRelation();
|
||||
$entityRelation->saveEntityRelation(entity: "slider", model: $slider, request: new Request());
|
||||
|
||||
if ($slider) {
|
||||
$this->redirect("/admin/slider/view/" . $slider->id);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/slider/create", 302);
|
||||
}
|
||||
|
||||
public function actionIndex($page_number = 1): void
|
||||
{
|
||||
$this->cgView->render("index.php", ['page_number' => $page_number]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionView($id): void
|
||||
{
|
||||
$slide = Slider::find($id);
|
||||
|
||||
if (!$slide){
|
||||
throw new Exception(message: "The slide not found");
|
||||
}
|
||||
$this->cgView->render("view.php", ['slider' => $slide]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionUpdate(int $id): void
|
||||
{
|
||||
$model = Slider::find($id);
|
||||
if (!$model){
|
||||
throw new Exception(message: "The slide not found");
|
||||
}
|
||||
|
||||
$this->cgView->render("form.php", ['model' => $model]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionEdit(int $id): void
|
||||
{
|
||||
$slide = Slider::find($id);
|
||||
if (!$slide){
|
||||
throw new Exception(message: "The slide not found");
|
||||
}
|
||||
$sliderForm = new CreateSliderForm();
|
||||
$sliderForm->load($_REQUEST);
|
||||
if ($sliderForm->validate()) {
|
||||
$slide = $this->sliderService->update($sliderForm, $slide);
|
||||
|
||||
$entityRelation = new EntityRelation();
|
||||
$entityRelation->saveEntityRelation(entity: "slider", model: $slide, request: new Request());
|
||||
|
||||
if ($slide) {
|
||||
$this->redirect("/admin/slider/view/" . $slide->id, 302);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/slider/update/" . $id, 302);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
#[NoReturn] public function actionDelete(int $id): void
|
||||
{
|
||||
$slide = Slider::find($id)->first();
|
||||
if (!$slide){
|
||||
throw new Exception(message: "The slide not found");
|
||||
}
|
||||
|
||||
$entityRelation = new EntityRelation();
|
||||
$entityRelation->deleteEntityRelation(entity: "slider", model: $slide);
|
||||
|
||||
$slide->delete();
|
||||
$this->redirect("/admin/slider/", 302);
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\slider\controllers;
|
||||
|
||||
use kernel\RestController;
|
||||
|
||||
class SliderRestController extends RestController
|
||||
{
|
||||
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
\kernel\App::$db->schema->create('slider', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->string('title', 255)->nullable(false);
|
||||
$table->string('additional_information', 255)->nullable(false);
|
||||
$table->string('content', 255)->nullable(false);
|
||||
$table->string('link', 255)->nullable(false);
|
||||
$table->integer('status')->default(0);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
\kernel\App::$db->schema->dropIfExists('slider');
|
||||
}
|
||||
};
|
42
kernel/app_modules/slider/models/Slider.php
Normal file
42
kernel/app_modules/slider/models/Slider.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\slider\models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $title
|
||||
* @property string $additional_information
|
||||
* @property string $content
|
||||
* @property string $link
|
||||
*/
|
||||
|
||||
class Slider extends Model
|
||||
{
|
||||
const int DISABLE_STATUS = 0;
|
||||
const int ACTIVE_STATUS = 1;
|
||||
|
||||
protected $table = "slider";
|
||||
protected $fillable = ['title', 'additional_information', 'content', 'link', 'status'];
|
||||
|
||||
public static function labels(): array
|
||||
{
|
||||
return [
|
||||
'title' => 'Заголовок',
|
||||
'content' => 'Контент',
|
||||
'link' => 'Ссылка',
|
||||
'additional_information' => 'Дополнительная информация',
|
||||
'status' => 'Статус'
|
||||
];
|
||||
}
|
||||
|
||||
public static function getStatus(): array
|
||||
{
|
||||
return [
|
||||
self::DISABLE_STATUS => "Не активный",
|
||||
self::ACTIVE_STATUS => "Активный",
|
||||
];
|
||||
}
|
||||
|
||||
}
|
19
kernel/app_modules/slider/models/forms/CreateSliderForm.php
Normal file
19
kernel/app_modules/slider/models/forms/CreateSliderForm.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\slider\models\forms;
|
||||
|
||||
use kernel\FormModel;
|
||||
|
||||
class CreateSliderForm extends FormModel
|
||||
{
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'title' => 'required|min-str-len:5',
|
||||
'additional_information' => 'nullable',
|
||||
'content' => 'required|min-str-len:10',
|
||||
'link' => '',
|
||||
'status' => 'required|integer',
|
||||
];
|
||||
}
|
||||
}
|
20
kernel/app_modules/slider/routs/slider.php
Normal file
20
kernel/app_modules/slider/routs/slider.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
use kernel\App;
|
||||
use kernel\CgRouteCollector;
|
||||
use Phroute\Phroute\RouteCollector;
|
||||
|
||||
App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router) {
|
||||
App::$collector->group(["before" => "auth"], function (RouteCollector $router) {
|
||||
App::$collector->group(["prefix" => "slider"], function (CGRouteCollector $router) {
|
||||
App::$collector->get('/', [\kernel\app_modules\slider\controllers\SliderController::class, 'actionIndex']);
|
||||
App::$collector->get('/page/{page_number}', [\kernel\app_modules\slider\controllers\SliderController::class, 'actionIndex']);
|
||||
App::$collector->get('/create', [\kernel\app_modules\slider\controllers\SliderController::class, 'actionCreate']);
|
||||
App::$collector->post("/", [\kernel\app_modules\slider\controllers\SliderController::class, 'actionAdd']);
|
||||
App::$collector->get('/view/{id}', [\kernel\app_modules\slider\controllers\SliderController::class, 'actionView']);
|
||||
App::$collector->any('/update/{id}', [\kernel\app_modules\slider\controllers\SliderController::class, 'actionUpdate']);
|
||||
App::$collector->any("/edit/{id}", [\kernel\app_modules\slider\controllers\SliderController::class, 'actionEdit']);
|
||||
App::$collector->get('/delete/{id}', [\kernel\app_modules\slider\controllers\SliderController::class, 'actionDelete']);
|
||||
});
|
||||
});
|
||||
});
|
37
kernel/app_modules/slider/services/SliderService.php
Normal file
37
kernel/app_modules/slider/services/SliderService.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\slider\services;
|
||||
|
||||
use kernel\app_modules\slider\models\Slider;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\FormModel;
|
||||
|
||||
class SliderService
|
||||
{
|
||||
public function create(FormModel $form_model): false|Slider
|
||||
{
|
||||
$model = new Slider();
|
||||
$model->title = $form_model->getItem('title');
|
||||
$model->content = $form_model->getItem('content');
|
||||
$model->additional_information = $form_model->getItem('additional_information');
|
||||
$model->link = $form_model->getItem('link');
|
||||
if ($model->save()){
|
||||
return $model;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function update(FormModel $form_model, Slider $slider): false|Slider
|
||||
{
|
||||
$slider->title = $form_model->getItem('title');
|
||||
$slider->content = $form_model->getItem('content');
|
||||
$slider->additional_information = $form_model->getItem('additional_information');
|
||||
$slider->link = $form_model->getItem('link');
|
||||
if ($slider->save()){
|
||||
return $slider;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
85
kernel/app_modules/slider/views/form.php
Normal file
85
kernel/app_modules/slider/views/form.php
Normal file
@ -0,0 +1,85 @@
|
||||
<?php
|
||||
/**
|
||||
* @var Slider $model
|
||||
*/
|
||||
|
||||
use kernel\app_modules\slider\models\Slider;
|
||||
|
||||
$form = new \itguild\forms\ActiveForm();
|
||||
$form->beginForm(isset($model) ? "/admin/slider/edit/" . $model->id : "/admin/slider", 'multipart/form-data');
|
||||
|
||||
$form->field(\itguild\forms\inputs\TextInput::class, 'title', [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Заголовок',
|
||||
'value' => $model->title ?? ''
|
||||
])
|
||||
->setLabel("Заголовок")
|
||||
->render();
|
||||
|
||||
|
||||
$form->field(class: \itguild\forms\inputs\TextArea::class, name: "content", params: [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Контент',
|
||||
'rows' => '10',
|
||||
'value' => $model->content ?? ''
|
||||
])
|
||||
->setLabel("Контент")
|
||||
->render();
|
||||
|
||||
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "additional_information", params: [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Дополнительная информация',
|
||||
'rows' => '10',
|
||||
'value' => $model->additional_information ?? ''
|
||||
])
|
||||
->setLabel("Дополнительная информация")
|
||||
->render();
|
||||
|
||||
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "link", params: [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Ссылка',
|
||||
'rows' => '10',
|
||||
'value' => $model->link ?? ''
|
||||
])
|
||||
->setLabel("Ссылка")
|
||||
->render();
|
||||
|
||||
$form->field(class: \itguild\forms\inputs\Select::class, name: "status", params: [
|
||||
'class' => "form-control",
|
||||
'value' => $model->status ?? ''
|
||||
])
|
||||
->setLabel("Статус")
|
||||
->setOptions(Slider::getStatus())
|
||||
->render();
|
||||
|
||||
$entityRelations = new \kernel\EntityRelation();
|
||||
if (!isset($model)) {
|
||||
$model = new Slider();
|
||||
}
|
||||
$entityRelations->renderEntityAdditionalPropertyFormBySlug("slider", $model);
|
||||
|
||||
?>
|
||||
<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();
|
52
kernel/app_modules/slider/views/index.php
Normal file
52
kernel/app_modules/slider/views/index.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* @var int $page_number
|
||||
*/
|
||||
|
||||
use kernel\app_modules\slider\models\Slider;
|
||||
use Itguild\EloquentTable\EloquentDataProvider;
|
||||
use Itguild\EloquentTable\ListEloquentTable;
|
||||
use kernel\app_modules\photo\models\Photo;
|
||||
use kernel\widgets\IconBtn\IconBtnCreateWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnDeleteWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnEditWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnViewWidget;
|
||||
|
||||
$table = new ListEloquentTable(new EloquentDataProvider(Slider::class, [
|
||||
'currentPage' => $page_number,
|
||||
'perPage' => 8,
|
||||
'params' => ["class" => "table table-bordered", "border" => "2"],
|
||||
'baseUrl' => "/admin/slider",
|
||||
]));
|
||||
|
||||
$table->beforePrint(function () {
|
||||
return IconBtnCreateWidget::create(['url' => '/admin/slider/create'])->run();
|
||||
});
|
||||
|
||||
$entityRelation = new \kernel\EntityRelation();
|
||||
$additionals = $entityRelation->getEntityRelationsBySlug("slider");
|
||||
|
||||
foreach ($additionals as $additional) {
|
||||
$table->addColumn($additional, $additional, function ($id) use ($entityRelation, $additional) {
|
||||
return $entityRelation->getAdditionalPropertyByEntityId("slider", $id, $additional);
|
||||
});
|
||||
}
|
||||
|
||||
$table->columns([
|
||||
"status" => [
|
||||
"value" => function ($cell) {
|
||||
return Slider::getStatus()[$cell];
|
||||
}]
|
||||
]);
|
||||
|
||||
$table->addAction(function($row) {
|
||||
return IconBtnViewWidget::create(['url' => '/admin/slider/view/' . $row['id']])->run();
|
||||
});
|
||||
$table->addAction(function($row) {
|
||||
return IconBtnEditWidget::create(['url' => '/admin/slider/update/' . $row['id']])->run();
|
||||
});
|
||||
$table->addAction(function($row) {
|
||||
return IconBtnDeleteWidget::create(['url' => '/admin/slider/delete/' . $row['id']])->run();
|
||||
});
|
||||
$table->create();
|
||||
$table->render();
|
39
kernel/app_modules/slider/views/view.php
Normal file
39
kernel/app_modules/slider/views/view.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Collection $slider
|
||||
*/
|
||||
|
||||
use Itguild\EloquentTable\ViewEloquentTable;
|
||||
use Itguild\EloquentTable\ViewJsonTableEloquentModel;
|
||||
use kernel\IGTabel\btn\DangerBtn;
|
||||
use kernel\IGTabel\btn\PrimaryBtn;
|
||||
use kernel\IGTabel\btn\SuccessBtn;
|
||||
|
||||
$table = new ViewEloquentTable(new ViewJsonTableEloquentModel($slider, [
|
||||
'params' => ["class" => "table table-bordered", "border" => "2"],
|
||||
'baseUrl' => "/admin/slider",
|
||||
]));
|
||||
$table->beforePrint(function () use ($slider) {
|
||||
$btn = \kernel\widgets\IconBtn\IconBtnListWidget::create(['url' => "/admin/slider"])->run();
|
||||
$btn .= \kernel\widgets\IconBtn\IconBtnEditWidget::create(['url' => "/admin/slider/update/" . $slider->id])->run();
|
||||
$btn .= \kernel\widgets\IconBtn\IconBtnDeleteWidget::create(['url' => "/admin/slider/delete/" . $slider->id])->run();
|
||||
return $btn;
|
||||
});
|
||||
|
||||
$entityRelation = new \kernel\EntityRelation();
|
||||
$additionals = $entityRelation->getEntityAdditionalProperty("slider", $slider);
|
||||
|
||||
foreach ($additionals as $key => $additional) {
|
||||
$table->addRow($key, function () use ($additional) {
|
||||
return $additional;
|
||||
}, ['after' => 'status']);
|
||||
}
|
||||
|
||||
$table->rows([
|
||||
'status' => (function ($data) {
|
||||
return \kernel\app_modules\slider\models\Slider::getStatus()[$data];
|
||||
})
|
||||
]);
|
||||
$table->create();
|
||||
$table->render();
|
@ -8,8 +8,12 @@ use kernel\AdminController;
|
||||
use kernel\app_modules\tag\models\forms\CreateTagForm;
|
||||
use kernel\app_modules\tag\models\Tag;
|
||||
use kernel\app_modules\tag\services\TagService;
|
||||
use kernel\EntityRelation;
|
||||
use kernel\Flash;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\models\Option;
|
||||
use kernel\modules\menu\service\MenuService;
|
||||
use kernel\Request;
|
||||
|
||||
class TagController extends AdminController
|
||||
{
|
||||
@ -100,7 +104,17 @@ class TagController extends AdminController
|
||||
|
||||
public function actionSettings(): void
|
||||
{
|
||||
$this->cgView->render('form.php');
|
||||
$this->cgView->render('settingsForm.php');
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionSaveSettings(): void
|
||||
{
|
||||
$request = new Request();
|
||||
$entities = $request->post('entity');
|
||||
EntityRelation::configurationEntitiesByProperty($entities, 'tag');
|
||||
|
||||
Flash::setMessage("success", "Настройка прошла успешно");
|
||||
$this->redirect("/admin/settings/tag", 302);
|
||||
}
|
||||
|
||||
}
|
@ -15,32 +15,19 @@ use kernel\modules\menu\service\MenuService;
|
||||
|
||||
class TagEntityController extends AdminController
|
||||
{
|
||||
private TagEntityService $tagEntityService;
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/tag/views/tag_entity/";
|
||||
$this->tagEntityService = new TagEntityService();
|
||||
}
|
||||
|
||||
// public function actionCreate(): void
|
||||
// {
|
||||
// $this->cgView->render("form.php");
|
||||
// }
|
||||
//
|
||||
// #[NoReturn] public function actionAdd(): void
|
||||
// {
|
||||
// $tagForm = new CreateTagForm();
|
||||
// $tagForm->load($_REQUEST);
|
||||
// if ($tagForm->validate()){
|
||||
// $tag = $this->tagEntityService->create($tagForm);
|
||||
// if ($tag){
|
||||
// $this->redirect("/admin/tag/" . $tag->id);
|
||||
// }
|
||||
// }
|
||||
// $this->redirect("/admin/tag/create");
|
||||
// }
|
||||
|
||||
/**
|
||||
* @param $page_number
|
||||
* @return void
|
||||
*/
|
||||
public function actionIndex($page_number = 1): void
|
||||
{
|
||||
$this->cgView->render("index.php", ['page_number' => $page_number]);
|
||||
@ -59,45 +46,4 @@ class TagEntityController extends AdminController
|
||||
$this->cgView->render("view.php", ['tagEntity' => $tagEntity]);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @throws Exception
|
||||
// */
|
||||
// public function actionUpdate($id): void
|
||||
// {
|
||||
// $model = Tag::find($id);
|
||||
// if (!$model){
|
||||
// throw new Exception(message: "The tag not found");
|
||||
// }
|
||||
//
|
||||
// $this->cgView->render("form.php", ['model' => $model]);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * @throws Exception
|
||||
// */
|
||||
// public function actionEdit($id): void
|
||||
// {
|
||||
// $tag = Tag::find($id);
|
||||
// if (!$tag){
|
||||
// throw new Exception(message: "The tag not found");
|
||||
// }
|
||||
// $tagForm = new CreateTagForm();
|
||||
// $tagService = new TagService();
|
||||
// $tagForm->load($_REQUEST);
|
||||
// if ($tagForm->validate()) {
|
||||
// $tag = $tagService->update($tagForm, $tag);
|
||||
// if ($tag) {
|
||||
// $this->redirect("/admin/tag/" . $tag->id);
|
||||
// }
|
||||
// }
|
||||
// $this->redirect("/admin/tag/update/" . $id);
|
||||
// }
|
||||
//
|
||||
// #[NoReturn] public function actionDelete($id): void
|
||||
// {
|
||||
// $post = Tag::find($id)->first();
|
||||
// $post->delete();
|
||||
// $this->redirect("/admin/tag/");
|
||||
// }
|
||||
|
||||
}
|
@ -28,6 +28,7 @@ App::$collector->group(["prefix" => "admin"], function (CgRouteCollector $router
|
||||
});
|
||||
App::$collector->group(["prefix" => "settings"], function (CGRouteCollector $router) {
|
||||
App::$collector->get('/tag', [\app\modules\tag\controllers\TagController::class, 'actionSettings']);
|
||||
App::$collector->post('/tag/update', [\app\modules\tag\controllers\TagController::class, 'actionSaveSettings']);
|
||||
});
|
||||
});
|
||||
});
|
@ -1,6 +1,5 @@
|
||||
<?php
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Collection $menuItem
|
||||
* @var int $page_number
|
||||
*/
|
||||
|
||||
@ -22,7 +21,6 @@ $table = new ListEloquentTable(new EloquentDataProvider(Tag::class, [
|
||||
|
||||
$table->beforePrint(function () {
|
||||
return PrimaryBtn::create("Создать", "/admin/tag/create")->fetch();
|
||||
//return (new PrimaryBtn("Создать", "/admin/user/create"))->fetch();
|
||||
});
|
||||
|
||||
$table->columns([
|
||||
|
47
kernel/app_modules/tag/views/tag/settingsForm.php
Normal file
47
kernel/app_modules/tag/views/tag/settingsForm.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
$form = new \itguild\forms\ActiveForm();
|
||||
$form->beginForm("/admin/settings/tag/update");
|
||||
|
||||
//\kernel\helpers\Debug::dd($value);
|
||||
?>
|
||||
|
||||
<div class="row">
|
||||
<h5>Выберите сущности, к которым хотите прикрепить теги</h5>
|
||||
</div>
|
||||
|
||||
<?php
|
||||
$form->field(\itguild\forms\inputs\Select::class, "entity[]", [
|
||||
'class' => "form-control",
|
||||
'value' => \kernel\EntityRelation::getEntityByProperty('tag') ?? '',
|
||||
'multiple' => "multiple",
|
||||
|
||||
])
|
||||
->setLabel("Сущности")
|
||||
->setOptions(\kernel\EntityRelation::getEntityList())
|
||||
->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();
|
@ -4,6 +4,8 @@
|
||||
namespace kernel\console;
|
||||
|
||||
|
||||
use kernel\helpers\Debug;
|
||||
|
||||
class Out
|
||||
{
|
||||
private $foreground_colors = array();
|
||||
@ -64,6 +66,11 @@ class Out
|
||||
echo $this->get($string, $foreground_color, $background_color) . "\n";
|
||||
}
|
||||
|
||||
public function inLine($string, $foreground_color = null, $background_color = null): void
|
||||
{
|
||||
echo $this->get($string, $foreground_color, $background_color) ;
|
||||
}
|
||||
|
||||
// Returns all foreground color names
|
||||
public function getForegroundColors()
|
||||
{
|
||||
@ -75,4 +82,16 @@ class Out
|
||||
{
|
||||
return array_keys($this->background_colors);
|
||||
}
|
||||
|
||||
// public function printHeaderTable(): void
|
||||
// {
|
||||
// echo "\n+-----------------------------+-----------------------------+-----------------------------+-----------------------------+\n";
|
||||
// printf("%-30s", "| Routs");
|
||||
// printf("%-30s", "| Description");
|
||||
// printf("%-30s", "| Params");
|
||||
// printf("%-30s", "| Params description");
|
||||
// printf("%-30s", "|");
|
||||
// echo "\n+-----------------------------+-----------------------------+-----------------------------+-----------------------------+\n";
|
||||
// }
|
||||
|
||||
}
|
@ -46,6 +46,9 @@ class AdminConsoleController extends ConsoleController
|
||||
$out = $this->migrationService->runAtPath("kernel/modules/post/migrations");
|
||||
$this->out->r("create post table", "green");
|
||||
|
||||
$out = $this->migrationService->runAtPath("kernel/modules/secure/migrations");
|
||||
$this->out->r("create secret_code table", "green");
|
||||
|
||||
$this->optionService->createFromParams(
|
||||
key: "admin_theme_paths",
|
||||
value: "{\"paths\": [\"{KERNEL_ADMIN_THEMES}\", \"{APP}/admin_themes\"]}",
|
||||
@ -136,7 +139,7 @@ class AdminConsoleController extends ConsoleController
|
||||
"slug" => "menu",
|
||||
"parent_slug" => "settings"
|
||||
]);
|
||||
$this->out->r("create item menu admin-themes", "green");
|
||||
$this->out->r("create item menu menu", "green");
|
||||
|
||||
$this->menuService->createItem([
|
||||
"label" => "Опции",
|
||||
|
@ -27,7 +27,7 @@ class KernelController extends ConsoleController
|
||||
|
||||
if (file_exists(ROOT_DIR . $this->argv['path'])) {
|
||||
$tmpKernelDirFull = RESOURCES_DIR . '/tmp/ad/kernel/kernel';
|
||||
$this->files->copy_folder(KERNEL_DIR, $tmpKernelDirFull);
|
||||
$this->files->copy_folder(ROOT_DIR . $this->argv['path'], $tmpKernelDirFull);
|
||||
$this->out->r("Ядро скопировано во временную папку", 'green');
|
||||
} else {
|
||||
$this->out->r("Ядро не найдено", 'red');
|
||||
@ -65,11 +65,19 @@ class KernelController extends ConsoleController
|
||||
$this->out->r("/composer.json не найден", 'red');
|
||||
}
|
||||
|
||||
if (!is_dir(RESOURCES_DIR . '/tmp/app')) {
|
||||
mkdir(RESOURCES_DIR . '/tmp/app');
|
||||
if (!is_dir(RESOURCES_DIR . '/tmp/kernel')) {
|
||||
mkdir(RESOURCES_DIR . '/tmp/kernel');
|
||||
}
|
||||
|
||||
if (file_exists(KERNEL_DIR . '/manifest.json')) {
|
||||
$manifest = json_decode(file_get_contents(KERNEL_DIR . '/manifest.json'), true);
|
||||
$version = $manifest['version'] ?? '';
|
||||
$this->files->pack(RESOURCES_DIR . '/tmp/ad/kernel/', RESOURCES_DIR . '/tmp/kernel/kernel_v' . $version . '.igk');
|
||||
}
|
||||
else {
|
||||
$this->files->pack(RESOURCES_DIR . '/tmp/ad/kernel/', RESOURCES_DIR . '/tmp/kernel/kernel.igk');
|
||||
}
|
||||
|
||||
$this->files->pack(RESOURCES_DIR . '/tmp/ad/kernel/', RESOURCES_DIR . '/tmp/kernel/kernel.igk');
|
||||
$this->files->recursiveRemoveDir(RESOURCES_DIR . '/tmp/ad/kernel/');
|
||||
}
|
||||
|
||||
|
@ -2,7 +2,9 @@
|
||||
|
||||
namespace kernel\console\controllers;
|
||||
|
||||
use kernel\App;
|
||||
use kernel\console\ConsoleController;
|
||||
use kernel\helpers\Debug;
|
||||
|
||||
class MainController extends ConsoleController
|
||||
{
|
||||
@ -12,4 +14,25 @@ class MainController extends ConsoleController
|
||||
$this->out->r("Привет", "green");
|
||||
}
|
||||
|
||||
public function actionHelp(): void
|
||||
{
|
||||
$routs = App::$collector->getData()->getStaticRoutes();
|
||||
foreach ($routs as $rout => $data){
|
||||
$additionalInfo = $data['GET'][3];
|
||||
if (isset($additionalInfo['description']) and $additionalInfo['type'] === "console"){
|
||||
$this->out->inLine($rout . " - ", "green");
|
||||
$this->out->inLine($additionalInfo['description'], 'yellow');
|
||||
$this->out->r("");
|
||||
if (isset($additionalInfo['params'])){
|
||||
foreach ($additionalInfo['params'] as $key => $param){
|
||||
$this->out->inLine($key . " - ", "green");
|
||||
$this->out->inLine($param, 'yellow');
|
||||
$this->out->r("");
|
||||
}
|
||||
}
|
||||
$this->out->r("");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -90,4 +90,33 @@ class ModuleController extends ConsoleController
|
||||
}
|
||||
}
|
||||
|
||||
public function actionConstructModule(): void
|
||||
{
|
||||
$this->out->r("Введите slug модуля:", 'yellow');
|
||||
$slug = substr(fgets(STDIN), 0, -1);
|
||||
$slug = strtolower($slug);
|
||||
|
||||
$this->out->r("Введите название модуля:", 'yellow');
|
||||
$name = substr(fgets(STDIN), 0, -1);
|
||||
|
||||
$this->out->r("Введите автора модуля:", 'yellow');
|
||||
$author = substr(fgets(STDIN), 0, -1);
|
||||
|
||||
$this->out->r("Введите название пунтка меню для модуля:", 'yellow');
|
||||
$label = substr(fgets(STDIN), 0, -1);
|
||||
|
||||
$moduleService = new ModuleService();
|
||||
$moduleService->createDirs($slug);
|
||||
|
||||
$moduleService->createModuleByParams([
|
||||
'slug' => $slug,
|
||||
'model' => ucfirst($slug),
|
||||
'author' => $author,
|
||||
'name' => $name,
|
||||
'label' => $label,
|
||||
]);
|
||||
|
||||
$this->out->r("Модуль $slug создан", 'green');
|
||||
}
|
||||
|
||||
}
|
@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public string $migration;
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
|
@ -1,41 +1,96 @@
|
||||
<?php
|
||||
|
||||
use kernel\App;
|
||||
use kernel\console\controllers\MigrationController;
|
||||
use Phroute\Phroute\RouteCollector;
|
||||
|
||||
App::$collector->console("hello", [\kernel\console\controllers\MainController::class, "indexAction"]);
|
||||
App::$collector->console("help", [\kernel\console\controllers\MainController::class, "actionHelp"]);
|
||||
|
||||
App::$collector->group(["prefix" => "migration"], callback: function (RouteCollector $router){
|
||||
App::$collector->console('run', [\kernel\console\controllers\MigrationController::class, 'actionRun']);
|
||||
App::$collector->console('init', [\kernel\console\controllers\MigrationController::class, 'actionCreateMigrationTable']);
|
||||
App::$collector->console('create', [\kernel\console\controllers\MigrationController::class, 'actionCreate']);
|
||||
App::$collector->console('rollback', [\kernel\console\controllers\MigrationController::class, 'actionRollback']);
|
||||
App::$collector->console('run',
|
||||
[MigrationController::class, 'actionRun'],
|
||||
additionalInfo: ['description' => 'Запуск существующих миграций']
|
||||
);
|
||||
App::$collector->console('init',
|
||||
[MigrationController::class, 'actionCreateMigrationTable'],
|
||||
additionalInfo: ['description' => 'Инициализация миграций']
|
||||
);
|
||||
App::$collector->console('create',
|
||||
[MigrationController::class, 'actionCreate'],
|
||||
additionalInfo: ['description' => 'Создание миграции', 'params' => ['--name' => 'Название миграции', '--path' => 'Путь по которому будет создана миграция']]
|
||||
);
|
||||
App::$collector->console('rollback',
|
||||
[MigrationController::class, 'actionRollback'],
|
||||
additionalInfo: ['description' => 'Откатить миграции']
|
||||
);
|
||||
});
|
||||
|
||||
App::$collector->group(["prefix" => "admin-theme"], callback: function (RouteCollector $router){
|
||||
App::$collector->console('install', [\kernel\console\controllers\AdminThemeController::class, 'actionInstallTheme']);
|
||||
App::$collector->console('uninstall', [\kernel\console\controllers\AdminThemeController::class, 'actionUninstallTheme']);
|
||||
App::$collector->console('install',
|
||||
[\kernel\console\controllers\AdminThemeController::class, 'actionInstallTheme'],
|
||||
additionalInfo: ['description' => 'Установить тему админ-панели', 'params' => ['--path' => 'Путь к устанавливаемой теме']]
|
||||
);
|
||||
App::$collector->console('uninstall',
|
||||
[\kernel\console\controllers\AdminThemeController::class, 'actionUninstallTheme'],
|
||||
additionalInfo: ['description' => 'Удалить тему админ-панели', 'params' => ['--path' => 'Путь к удаляемой теме']]
|
||||
);
|
||||
});
|
||||
|
||||
App::$collector->group(["prefix" => "secure"], callback: function (RouteCollector $router){
|
||||
App::$collector->console('create-secret-key', [\kernel\console\controllers\SecureController::class, 'actionCreateSecretKey']);
|
||||
App::$collector->console('create-secret-key',
|
||||
[\kernel\console\controllers\SecureController::class, 'actionCreateSecretKey'],
|
||||
additionalInfo: ['description' => 'Генерация секрктного ключа и запись его в .env']
|
||||
);
|
||||
});
|
||||
|
||||
App::$collector->group(["prefix" => "admin"], callback: function (RouteCollector $router){
|
||||
App::$collector->console('init', [\kernel\console\controllers\AdminConsoleController::class, 'actionInit']);
|
||||
App::$collector->console('init',
|
||||
[\kernel\console\controllers\AdminConsoleController::class, 'actionInit'],
|
||||
additionalInfo: ['description' => 'Инициализация админ-панели']
|
||||
);
|
||||
});
|
||||
|
||||
App::$collector->group(["prefix" => "module"], callback: function (RouteCollector $router){
|
||||
App::$collector->console('install', [\kernel\console\controllers\ModuleController::class, 'actionInstallModule']);
|
||||
App::$collector->console('uninstall', [\kernel\console\controllers\ModuleController::class, 'actionUninstallModule']);
|
||||
App::$collector->console('pack', [\kernel\console\controllers\ModuleController::class, 'actionPackModule']);
|
||||
App::$collector->console('update', [\kernel\console\controllers\ModuleController::class, 'actionUpdateModule']);
|
||||
App::$collector->console('install',
|
||||
[\kernel\console\controllers\ModuleController::class, 'actionInstallModule'],
|
||||
additionalInfo: ['description' => 'Установка модуля', 'params' => ['--path' => 'Путь к устанавливаемому модулю']]
|
||||
);
|
||||
App::$collector->console('uninstall',
|
||||
[\kernel\console\controllers\ModuleController::class, 'actionUninstallModule'],
|
||||
additionalInfo: ['description' => 'Удалить модуль', 'params' => ['--path' => 'Путь к удаляемому модулю']]
|
||||
);
|
||||
App::$collector->console('pack',
|
||||
[\kernel\console\controllers\ModuleController::class, 'actionPackModule'],
|
||||
additionalInfo: ['description' => 'Заархивировать модуль', 'params' => ['--path' => 'Путь к модулю, который нужно заархивировать']]
|
||||
);
|
||||
App::$collector->console('update',
|
||||
[\kernel\console\controllers\ModuleController::class, 'actionUpdateModule'],
|
||||
additionalInfo: ['description' => 'Обновить модуль', 'params' => ['--path' => 'Путь к архиву с модулем']]
|
||||
);
|
||||
App::$collector->console('construct',
|
||||
[\kernel\console\controllers\ModuleController::class, 'actionConstructModule'],
|
||||
additionalInfo: ['description' => 'Сгенерировать модуль']
|
||||
);
|
||||
});
|
||||
|
||||
App::$collector->group(["prefix" => "kernel"], callback: function (RouteCollector $router){
|
||||
// App::$collector->console('install', [\kernel\console\controllers\ModuleController::class, 'actionInstallModule']);
|
||||
// App::$collector->console('uninstall', [\kernel\console\controllers\ModuleController::class, 'actionUninstallModule']);
|
||||
App::$collector->console('pack', [\kernel\console\controllers\KernelController::class, 'actionPackKernel']);
|
||||
App::$collector->console('update', [\kernel\console\controllers\KernelController::class, 'actionUpdateKernel']);
|
||||
App::$collector->console('pack',
|
||||
[\kernel\console\controllers\KernelController::class, 'actionPackKernel'],
|
||||
additionalInfo: ['description' => 'Заархивировать ядро', 'params' => ['--path' => 'Путь к ядру']]
|
||||
);
|
||||
App::$collector->console('update',
|
||||
[\kernel\console\controllers\KernelController::class, 'actionUpdateKernel'],
|
||||
additionalInfo: [
|
||||
'description' => 'Обновить модуль',
|
||||
'params' =>
|
||||
[
|
||||
'--path' => 'Путь к архиву ядра',
|
||||
'bootstrap' => 'Обновить bootstrap',
|
||||
'composer' => 'Обновить composer',
|
||||
'env' => 'Обновить .env.example'
|
||||
]
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
|
@ -12,6 +12,7 @@ use kernel\models\Option;
|
||||
use kernel\modules\module_shop_client\services\ModuleShopClientService;
|
||||
use kernel\modules\user\service\UserService;
|
||||
use kernel\Request;
|
||||
use kernel\services\MigrationService;
|
||||
use kernel\services\ModuleService;
|
||||
|
||||
class ModuleController extends AdminController
|
||||
|
42
kernel/helpers/Html.php
Normal file
42
kernel/helpers/Html.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\helpers;
|
||||
|
||||
class Html
|
||||
{
|
||||
|
||||
public static function img(string $src, array $params = [])
|
||||
{
|
||||
$paramsStr = self::createParams($params);
|
||||
return "<img src='$src' $paramsStr>";
|
||||
}
|
||||
|
||||
public static function h(string|int $type = 1, string $title = '', array $params = [])
|
||||
{
|
||||
$paramsStr = self::createParams($params);
|
||||
return "<h$type $paramsStr>$title</h$type>";
|
||||
}
|
||||
|
||||
public static function a(string $link, array $params = []): string
|
||||
{
|
||||
$paramsStr = self::createParams($params);
|
||||
return "<a href='$link' $paramsStr>";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return string
|
||||
*/
|
||||
public static function createParams(array $data = []): string
|
||||
{
|
||||
$paramsString = "";
|
||||
foreach($data as $key => $param){
|
||||
if(is_string($param)){
|
||||
$paramsString .= $key . "='" . $param . "'";
|
||||
}
|
||||
}
|
||||
|
||||
return $paramsString;
|
||||
}
|
||||
|
||||
}
|
@ -2,13 +2,17 @@
|
||||
|
||||
namespace kernel\helpers;
|
||||
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use http\Client;
|
||||
|
||||
class RESTClient
|
||||
{
|
||||
|
||||
|
||||
public static function request(string $url, string $method = 'GET')
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public static function request(string $url, string $method = 'GET'): \Psr\Http\Message\ResponseInterface
|
||||
{
|
||||
$client = new \GuzzleHttp\Client();
|
||||
return $client->request($method, $url, [
|
||||
@ -18,4 +22,31 @@ class RESTClient
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public static function request_without_auth(string $url, string $method = 'GET'): \Psr\Http\Message\ResponseInterface
|
||||
{
|
||||
$client = new \GuzzleHttp\Client();
|
||||
return $client->request($method, $url);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public static function post(string $url, array $data = [], bool $auth = true): \Psr\Http\Message\ResponseInterface
|
||||
{
|
||||
$headers = [];
|
||||
if ($auth){
|
||||
$headers = [
|
||||
'Authorization' => 'Bearer ' . $_ENV['MODULE_SHOP_TOKEN']
|
||||
];
|
||||
}
|
||||
$client = new \GuzzleHttp\Client();
|
||||
return $client->request("POST", $url, [
|
||||
'form_params' => $data,
|
||||
'headers' => $headers,
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
40
kernel/helpers/SMTP.php
Normal file
40
kernel/helpers/SMTP.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\helpers;
|
||||
|
||||
use PHPMailer\PHPMailer\Exception;
|
||||
use PHPMailer\PHPMailer\PHPMailer;
|
||||
|
||||
class SMTP
|
||||
{
|
||||
public PHPMailer $mail;
|
||||
public function __construct()
|
||||
{
|
||||
$this->mail = new PHPMailer(true);
|
||||
$this->mail->CharSet = 'UTF-8';
|
||||
$this->mail->isSMTP();
|
||||
$this->mail->SMTPAuth = true;
|
||||
$this->mail->SMTPDebug = 0;
|
||||
$this->mail->Host = $_ENV['MAIL_SMTP_HOST'];
|
||||
$this->mail->Port = $_ENV['MAIL_SMTP_PORT'];
|
||||
$this->mail->Username = $_ENV['MAIL_SMTP_USERNAME'];
|
||||
$this->mail->Password = $_ENV['MAIL_SMTP_PASSWORD'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function send_html(array $params)
|
||||
{
|
||||
if (!isset($params['address'])){
|
||||
return false;
|
||||
}
|
||||
$this->mail->setFrom($this->mail->Username, $params['from_name'] ?? $this->mail->Host);
|
||||
$this->mail->addAddress($params['address']);
|
||||
$this->mail->Subject = $params['subject'] ?? 'Без темы';
|
||||
$body = $params['body'] ?? 'Нет информации';
|
||||
$this->mail->msgHTML($body);
|
||||
|
||||
$this->mail->send();
|
||||
}
|
||||
}
|
8
kernel/manifest.json
Normal file
8
kernel/manifest.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"name": "Kernel",
|
||||
"version": "0.2",
|
||||
"author": "ITGuild",
|
||||
"slug": "kernel",
|
||||
"type": "kernel",
|
||||
"description": "Kernel"
|
||||
}
|
@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public string $migration;
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
|
@ -10,15 +10,21 @@ use kernel\Flash;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\helpers\Files;
|
||||
use kernel\helpers\RESTClient;
|
||||
use kernel\modules\module_shop_client\services\ModuleShopClientService;
|
||||
use kernel\helpers\SMTP;
|
||||
use kernel\Mailing;
|
||||
use kernel\Request;
|
||||
use kernel\services\KernelService;
|
||||
use kernel\services\ModuleService;
|
||||
use kernel\services\ModuleShopService;
|
||||
use kernel\services\TokenService;
|
||||
use PHPMailer\PHPMailer\Exception;
|
||||
|
||||
class ModuleShopClientController extends AdminController
|
||||
{
|
||||
|
||||
protected Client $client;
|
||||
protected ModuleService $moduleService;
|
||||
protected KernelService $kernelService;
|
||||
|
||||
protected function init(): void
|
||||
{
|
||||
@ -27,6 +33,7 @@ class ModuleShopClientController extends AdminController
|
||||
|
||||
$this->client = new Client();
|
||||
$this->moduleService = new ModuleService();
|
||||
$this->kernelService = new KernelService();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -34,18 +41,31 @@ class ModuleShopClientController extends AdminController
|
||||
*/
|
||||
public function actionIndex(int $page_number = 1): void
|
||||
{
|
||||
$per_page = 8;
|
||||
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
|
||||
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
|
||||
$module_count = count($modules_info);
|
||||
$modules_info = array_slice($modules_info, $per_page*($page_number-1), $per_page);
|
||||
$this->cgView->render("index.php", [
|
||||
'modules_info' => $modules_info,
|
||||
'moduleService' => $this->moduleService,
|
||||
'page_number' => $page_number,
|
||||
'module_count' => $module_count,
|
||||
'per_page' => $per_page,
|
||||
]);
|
||||
|
||||
if ($this->moduleService->issetModuleShopToken()) {
|
||||
if ($this->moduleService->isServerAvailable()) {
|
||||
|
||||
$per_page = 8;
|
||||
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
|
||||
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
|
||||
$module_count = count($modules_info);
|
||||
$modules_info = array_slice($modules_info, $per_page * ($page_number - 1), $per_page);
|
||||
|
||||
$this->cgView->render("index.php", [
|
||||
'modules_info' => $modules_info,
|
||||
'moduleService' => $this->moduleService,
|
||||
'page_number' => $page_number,
|
||||
'module_count' => $module_count,
|
||||
'per_page' => $per_page,
|
||||
'kernelService' => new KernelService(),
|
||||
]);
|
||||
} else {
|
||||
$this->cgView->render("module_shop_error_connection.php");
|
||||
}
|
||||
|
||||
} else {
|
||||
$this->cgView->render("login_at_module_shop.php");
|
||||
}
|
||||
}
|
||||
|
||||
public function actionView(int $id): void
|
||||
@ -95,6 +115,36 @@ class ModuleShopClientController extends AdminController
|
||||
$this->redirect('/admin/module_shop_client', 302);
|
||||
}
|
||||
|
||||
public function actionRenderKernelUpdateForm(): void
|
||||
{
|
||||
$this->cgView->render("kernel_update.php");
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionKernelUpdate(): void
|
||||
{
|
||||
$request = new Request();
|
||||
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
|
||||
|
||||
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
|
||||
foreach ($modules_info as $module) {
|
||||
if ($module['slug'] === 'kernel') {
|
||||
$path = $module['path_to_archive'];
|
||||
}
|
||||
}
|
||||
if (isset($path)) {
|
||||
Files::uploadByUrl($_ENV['MODULE_SHOP_URL'] . $path, RESOURCES_DIR . "/tmp/kernel");
|
||||
if ($this->kernelService->updateKernel('/resources/tmp/kernel/' . basename($path))) {
|
||||
Flash::setMessage("success", "Ядро успешно обновлено.");
|
||||
} else {
|
||||
Flash::setMessage("error", "Ошибка обновления ядра.");
|
||||
}
|
||||
} else {
|
||||
Flash::setMessage("error", "Ошибка обновления ядра.");
|
||||
}
|
||||
|
||||
$this->redirect('/admin/module_shop_client', 302);
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionDelete(): void
|
||||
{
|
||||
$request = new Request();
|
||||
@ -106,4 +156,44 @@ class ModuleShopClientController extends AdminController
|
||||
$this->redirect('/admin/module_shop_client', 302);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionAuth(): void
|
||||
{
|
||||
$request = new Request();
|
||||
$address = $request->post("email");
|
||||
|
||||
$moduleShopService = new ModuleShopService();
|
||||
$result = $moduleShopService->email_auth($address);
|
||||
|
||||
if ($result['status'] == 'success'){
|
||||
$this->cgView->render('enter_code.php', ['email' => $address]);
|
||||
}
|
||||
|
||||
$this->cgView->render('module_shop_error_connection.php', ['email' => $address]);
|
||||
}
|
||||
|
||||
public function actionCodeCheck(): void
|
||||
{
|
||||
$request = new Request();
|
||||
$code = $request->post("code");
|
||||
|
||||
$moduleShopService = new ModuleShopService();
|
||||
$result = $moduleShopService->code_check($code);
|
||||
|
||||
if (isset($result['access_token'])){
|
||||
|
||||
$envFile = \EnvEditor\EnvFile::loadFrom(ROOT_DIR . "/.env");
|
||||
|
||||
$envFile->setValue("MODULE_SHOP_TOKEN", $result['access_token']);
|
||||
|
||||
$envFile->saveTo(ROOT_DIR . "/.env");
|
||||
|
||||
$this->cgView->render('success_login.php');
|
||||
}
|
||||
|
||||
$this->cgView->render('module_shop_error_connection.php');
|
||||
}
|
||||
|
||||
}
|
@ -15,6 +15,12 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){
|
||||
App::$collector->get('/view/{id}', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionView']);
|
||||
App::$collector->get('/delete', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionDelete']);
|
||||
App::$collector->get('/update', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionUpdate']);
|
||||
App::$collector->post('/auth', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionAuth']);
|
||||
App::$collector->post('/code_check', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionCodeCheck']);
|
||||
App::$collector->group(["prefix" => "kernel"], function (RouteCollector $router) {
|
||||
App::$collector->get('/update_form', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionRenderKernelUpdateForm']);
|
||||
App::$collector->post('/update', [\kernel\modules\module_shop_client\controllers\ModuleShopClientController::class, 'actionKernelUpdate']);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
36
kernel/modules/module_shop_client/views/enter_code.php
Normal file
36
kernel/modules/module_shop_client/views/enter_code.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
/**
|
||||
* @var $email
|
||||
*/
|
||||
use itguild\forms\ActiveForm;
|
||||
|
||||
\kernel\widgets\ModuleTabsWidget::create()->run();
|
||||
|
||||
echo \kernel\helpers\Html::h(2, "Введите код подтверждения отправленный на почту \"$email\"");
|
||||
|
||||
$form = new ActiveForm();
|
||||
$form->beginForm("/admin/module_shop_client/code_check/");
|
||||
|
||||
$form->field(\itguild\forms\inputs\TextInput::class, 'code', [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Код',
|
||||
])
|
||||
->setLabel("Код")
|
||||
->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>
|
||||
<?php
|
||||
$form->endForm();
|
@ -5,6 +5,7 @@
|
||||
* @var int $page_number
|
||||
* @var int $per_page
|
||||
* @var \kernel\services\ModuleService $moduleService
|
||||
* @var \kernel\services\KernelService $kernelService
|
||||
*/
|
||||
|
||||
use Itguild\Tables\ListJsonTable;
|
||||
@ -37,21 +38,23 @@ $table->addAction(function ($row, $url) use ($moduleService) {
|
||||
});
|
||||
|
||||
$table->addAction(function ($row, $url) use ($moduleService){
|
||||
if ($moduleService->isInstall($row['slug'])){
|
||||
$url = "$url/delete/?slug=" . $row['slug'];
|
||||
if ($row['slug'] !== 'kernel') {
|
||||
if ($moduleService->isInstall($row['slug'])) {
|
||||
$url = "$url/delete/?slug=" . $row['slug'];
|
||||
|
||||
return \kernel\widgets\IconBtn\IconBtnDeleteWidget::create(['url' => $url])->run();
|
||||
}
|
||||
else {
|
||||
$url = "$url/install/?id=" . $row['id'];
|
||||
return \kernel\widgets\IconBtn\IconBtnDeleteWidget::create(['url' => $url])->run();
|
||||
} else {
|
||||
$url = "$url/install/?id=" . $row['id'];
|
||||
|
||||
return \kernel\widgets\IconBtn\IconBtnInstallWidget::create(['url' => $url])->run();
|
||||
return \kernel\widgets\IconBtn\IconBtnInstallWidget::create(['url' => $url])->run();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
$table->addAction(function ($row, $url) use ($moduleService){
|
||||
$table->addAction(function ($row, $url) use ($moduleService, $kernelService){
|
||||
$slug = $row['slug'];
|
||||
if ($moduleService->isInstall($slug)){
|
||||
if ($moduleService->isInstall($slug)) {
|
||||
if (!$moduleService->isLastVersion($slug)) {
|
||||
$url = "$url/update/?slug=" . $slug;
|
||||
|
||||
@ -59,6 +62,14 @@ $table->addAction(function ($row, $url) use ($moduleService){
|
||||
}
|
||||
}
|
||||
|
||||
if ($slug === 'kernel') {
|
||||
if (!$kernelService->isLastVersion()) {
|
||||
$url = "$url/kernel/update_form/";
|
||||
|
||||
return \kernel\widgets\IconBtn\IconBtnUpdateWidget::create(['url' => $url])->run();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
|
39
kernel/modules/module_shop_client/views/kernel_update.php
Normal file
39
kernel/modules/module_shop_client/views/kernel_update.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
use itguild\forms\ActiveForm;
|
||||
|
||||
\kernel\widgets\ModuleTabsWidget::create()->run();
|
||||
|
||||
echo \kernel\helpers\Html::h(2, "Выберите нужные файлы для обновления");
|
||||
|
||||
$form = new ActiveForm();
|
||||
$form->beginForm("/admin/module_shop_client/kernel/update/", enctype: 'multipart/form-data');
|
||||
|
||||
$form->field(\itguild\forms\inputs\Select::class, "files[]", [
|
||||
'class' => "form-control",
|
||||
'multiple' => "multiple",
|
||||
])
|
||||
->setLabel("Дополнительные файлы")
|
||||
->setOptions([
|
||||
'.env.example' => '.env.example',
|
||||
'bootstrap.php' => 'bootstrap',
|
||||
'composer.json' => 'composer.json',
|
||||
])
|
||||
->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>
|
||||
<?php
|
||||
$form->endForm();
|
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use itguild\forms\ActiveForm;
|
||||
|
||||
\kernel\widgets\ModuleTabsWidget::create()->run();
|
||||
|
||||
echo \kernel\helpers\Html::h(2, "Форма авторизации/регистрации");
|
||||
|
||||
$form = new ActiveForm();
|
||||
$form->beginForm("/admin/module_shop_client/auth/");
|
||||
|
||||
$form->field(\itguild\forms\inputs\EmailInput::class, 'email', [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Email',
|
||||
])
|
||||
->setLabel("Email")
|
||||
->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>
|
||||
<?php
|
||||
$form->endForm();
|
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
\kernel\widgets\ModuleTabsWidget::create()->run();
|
||||
?>
|
||||
|
||||
<h1>Ошибка подключения к сервису</h1>
|
||||
|
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
use itguild\forms\ActiveForm;
|
||||
|
||||
\kernel\widgets\ModuleTabsWidget::create()->run();
|
||||
|
||||
echo \kernel\helpers\Html::h(2, "Авторизация прошла успешно");
|
||||
echo \kernel\helpers\Html::a("/admin", ['class' => 'btm btm-primary']);
|
||||
|
@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public string $migration;
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
|
@ -5,17 +5,22 @@ namespace kernel\modules\post;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\Module;
|
||||
use kernel\modules\menu\service\MenuService;
|
||||
use kernel\services\MigrationService;
|
||||
|
||||
class PostModule extends Module
|
||||
{
|
||||
public MenuService $menuService;
|
||||
public MigrationService $migrationService;
|
||||
public function __construct()
|
||||
{
|
||||
$this->menuService = new MenuService();
|
||||
$this->migrationService = new MigrationService();
|
||||
}
|
||||
|
||||
public function init(): void
|
||||
{
|
||||
$this->migrationService->runAtPath("{KERNEL_MODULES}/post/migrations");
|
||||
|
||||
$this->menuService->createItem([
|
||||
"label" => "Посты",
|
||||
"url" => "/admin/post",
|
||||
@ -26,5 +31,6 @@ class PostModule extends Module
|
||||
public function deactivate(): void
|
||||
{
|
||||
$this->menuService->removeItemBySlug("post");
|
||||
$this->migrationService->rollbackAtPath("{KERNEL_MODULES}/post/migrations");
|
||||
}
|
||||
}
|
@ -6,6 +6,8 @@ use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public string $migration;
|
||||
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
|
@ -6,7 +6,7 @@
|
||||
use kernel\modules\post\models\Post;
|
||||
|
||||
$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", 'multipart/form-data');
|
||||
|
||||
$form->field(\itguild\forms\inputs\TextInput::class, 'title', [
|
||||
'class' => "form-control",
|
||||
|
@ -3,16 +3,13 @@
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Collection $contents
|
||||
* @var int $page_number
|
||||
* @var \kernel\CgView $view
|
||||
*/
|
||||
|
||||
use kernel\IGTabel\action_column\DeleteActionColumn;
|
||||
use kernel\IGTabel\action_column\EditActionColumn;
|
||||
use kernel\IGTabel\action_column\ViewActionColumn;
|
||||
use kernel\modules\post\models\Post;
|
||||
use kernel\modules\user\models\User;
|
||||
use Itguild\EloquentTable\EloquentDataProvider;
|
||||
use Itguild\EloquentTable\ListEloquentTable;
|
||||
use kernel\IGTabel\btn\PrimaryBtn;
|
||||
use kernel\widgets\IconBtn\IconBtnCreateWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnDeleteWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnEditWidget;
|
||||
@ -25,6 +22,11 @@ $table = new ListEloquentTable(new EloquentDataProvider(Post::class, [
|
||||
'baseUrl' => "/admin/post"
|
||||
]));
|
||||
|
||||
$view->setTitle("Список постов");
|
||||
$view->setMeta([
|
||||
'description' => 'Список постов системы'
|
||||
]);
|
||||
|
||||
$entityRelation = new \kernel\EntityRelation();
|
||||
$additionals = $entityRelation->getEntityRelationsBySlug("post");
|
||||
|
||||
|
@ -7,9 +7,6 @@
|
||||
use kernel\modules\user\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\widgets\IconBtn\IconBtnDeleteWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnEditWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnListWidget;
|
||||
|
@ -4,10 +4,19 @@ namespace kernel\modules\secure\controllers;
|
||||
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use kernel\AdminController;
|
||||
use kernel\App;
|
||||
use kernel\Flash;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\Mailing;
|
||||
use kernel\modules\secure\models\forms\LoginEmailForm;
|
||||
use kernel\modules\secure\models\forms\LoginForm;
|
||||
use kernel\modules\secure\models\forms\RegisterForm;
|
||||
use kernel\modules\secure\services\SecureService;
|
||||
use kernel\modules\user\models\User;
|
||||
use kernel\modules\user\service\UserService;
|
||||
use kernel\Request;
|
||||
use PHPMailer\PHPMailer\Exception;
|
||||
use Random\RandomException;
|
||||
|
||||
class SecureController extends AdminController
|
||||
{
|
||||
@ -16,7 +25,6 @@ class SecureController extends AdminController
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
// $this->cgView->viewPath = KERNEL_DIR . "/views/secure/";
|
||||
$this->cgView->viewPath = KERNEL_MODULES_DIR. "/secure/views/";
|
||||
$this->cgView->layout = "/login.php";
|
||||
$this->userService = new UserService();
|
||||
@ -24,7 +32,12 @@ class SecureController extends AdminController
|
||||
|
||||
public function actionLogin(): void
|
||||
{
|
||||
$this->cgView->render('login.php');
|
||||
$this->cgView->render(match (App::$secure['web_auth_type']) {
|
||||
"login_password" => "login.php",
|
||||
"email_code" => "email_login.php",
|
||||
});
|
||||
|
||||
// $this->cgView->render('login.php');
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionAuth(): void
|
||||
@ -54,11 +67,107 @@ class SecureController extends AdminController
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RandomException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionEmailAuth(): void
|
||||
{
|
||||
$mailing = new Mailing();
|
||||
|
||||
$loginForm = new LoginEmailForm();
|
||||
$loginForm->load($_REQUEST);
|
||||
|
||||
$email = $loginForm->getItem("email");
|
||||
$user = $this->userService->getByField('email', $email);
|
||||
|
||||
if (!$user){
|
||||
$password = bin2hex(random_bytes(8));
|
||||
|
||||
UserService::createUserByEmailAndPassword($email, $password);
|
||||
$user = $this->userService->getByField('email', $email);
|
||||
|
||||
SecureService::createSecretCode($user);
|
||||
$secretCode = SecureService::getByField("user_id", $user->id);
|
||||
|
||||
|
||||
$mailing->send_html("register_by_code.php", ['code' => $secretCode->code, 'password' => $password], [
|
||||
'address' => $email,
|
||||
'subject' => "Код регистрации",
|
||||
"from_name" => $_ENV['APP_NAME']
|
||||
]);
|
||||
} else {
|
||||
SecureService::updateSecretCode($user);
|
||||
$secretCode = SecureService::getByField("user_id", $user->id);
|
||||
$mailing->send_html("login_by_code.php", ['code' => $secretCode->code], [
|
||||
'address' => $email,
|
||||
'subject' => "Код авторизации",
|
||||
"from_name" => $_ENV['APP_NAME']
|
||||
]);
|
||||
}
|
||||
|
||||
setcookie('user_email', $email, time()+60*15, '/', $_SERVER['SERVER_NAME'], false);
|
||||
$this->cgView->render("enter_code.php", ['email' => $email]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionCodeCheck(): void
|
||||
{
|
||||
$request = new Request();
|
||||
|
||||
if (isset($_COOKIE['user_email'])) {
|
||||
$user = User::where('email', $_COOKIE["user_email"])->first();
|
||||
if (!$user) {
|
||||
throw new exception("User not found.");
|
||||
}
|
||||
$code = $request->post("code");
|
||||
$secretCode = SecureService::getByField("user_id", $user->id);
|
||||
if ($secretCode->code == $code && time() <= strtotime($secretCode->code_expires_at)) {
|
||||
setcookie('user_id', $user->id, time() + 60 * 60 * 24, '/', $_SERVER['SERVER_NAME'], false);
|
||||
$this->redirect("/admin", code: 302);
|
||||
} else {
|
||||
Flash::setMessage("error", "Wrong code.");
|
||||
$this->cgView->render("enter_code.php", ['email' => $_COOKIE["user_email"]]);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionLogout(): void
|
||||
{
|
||||
unset($_COOKIE['user_id']);
|
||||
setcookie('user_id', "", -1, '/', ".".$_SERVER['SERVER_NAME'], false);
|
||||
setcookie('user_email', "", -1, '/', ".".$_SERVER['SERVER_NAME'], false);
|
||||
$this->redirect("/", code: 302);
|
||||
}
|
||||
|
||||
public function actionRegister(): void
|
||||
{
|
||||
$this->cgView->render('register.php');
|
||||
}
|
||||
|
||||
public function actionRegistration(): void
|
||||
{
|
||||
$regForm = new RegisterForm();
|
||||
$regForm->load($_REQUEST);
|
||||
|
||||
if ($this->userService->getByField('username', $regForm->getItem("username"))) {
|
||||
Flash::setMessage("error", "Username already exists.");
|
||||
$this->redirect("/admin/register", code: 302);
|
||||
}
|
||||
|
||||
if ($this->userService->getByField('email', $regForm->getItem("email"))) {
|
||||
Flash::setMessage("error", "Email already exists.");
|
||||
$this->redirect("/admin/register", code: 302);
|
||||
}
|
||||
|
||||
$user = $this->userService->create($regForm);
|
||||
if ($user){
|
||||
setcookie('user_id', $user->id, time()+60*60*24, '/', $_SERVER['SERVER_NAME'], false);
|
||||
$this->redirect("/admin", code: 302);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -7,10 +7,15 @@ use Firebase\JWT\Key;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use kernel\App;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\Mailing;
|
||||
use kernel\modules\secure\models\SecretCode;
|
||||
use kernel\modules\secure\services\SecureService;
|
||||
use kernel\modules\user\models\User;
|
||||
use kernel\modules\user\service\UserService;
|
||||
use kernel\Request;
|
||||
use kernel\RestController;
|
||||
use kernel\services\TokenService;
|
||||
use PHPMailer\PHPMailer\Exception;
|
||||
use Random\RandomException;
|
||||
|
||||
class SecureRestController extends RestController
|
||||
@ -31,14 +36,16 @@ class SecureRestController extends RestController
|
||||
$res = [];
|
||||
if ($model) {
|
||||
if (password_verify($data["password"], $model->password_hash)) {
|
||||
$model->access_token_expires_at = date("Y-m-d H:i:s", strtotime(App::$secure['token_expired_time']));
|
||||
$model->access_token = match (App::$secure['token_type']) {
|
||||
"JWT" => TokenService::JWT($_ENV['SECRET_KEY'], 'HS256'),
|
||||
"md5" => TokenService::md5(),
|
||||
"crypt" => TokenService::crypt(),
|
||||
"hash" => TokenService::hash('sha256'),
|
||||
default => TokenService::random_bytes(20),
|
||||
};
|
||||
if ($model->access_token_expires_at < date("Y-m-d H:i:s") or $model->access_token === null){
|
||||
$model->access_token_expires_at = date("Y-m-d H:i:s", strtotime(App::$secure['token_expired_time']));
|
||||
$model->access_token = match (App::$secure['token_type']) {
|
||||
"JWT" => TokenService::JWT($_ENV['SECRET_KEY'], 'HS256'),
|
||||
"md5" => TokenService::md5(),
|
||||
"crypt" => TokenService::crypt(),
|
||||
"hash" => TokenService::hash('sha256'),
|
||||
default => TokenService::random_bytes(20),
|
||||
};
|
||||
}
|
||||
|
||||
$res = [
|
||||
"access_token" => $model->access_token,
|
||||
@ -51,4 +58,75 @@ class SecureRestController extends RestController
|
||||
$this->renderApi($res);
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* @throws Exception
|
||||
* @throws RandomException
|
||||
*/
|
||||
#[NoReturn] public function actionEmailAuth(): void
|
||||
{
|
||||
$mailing = new Mailing();
|
||||
$request = new Request();
|
||||
$data = $request->post();
|
||||
$model = $this->model->where('email', $data['email'])->first();
|
||||
|
||||
if (!$model) {
|
||||
$password = bin2hex(random_bytes(8));
|
||||
|
||||
UserService::createUserByEmailAndPassword($data['email'], $password);
|
||||
$model = UserService::getByField('email', $data['email']);
|
||||
|
||||
SecureService::createSecretCode($model);
|
||||
$secretCode = SecureService::getByField("user_id", $model->id);
|
||||
|
||||
|
||||
$mailing->send_html("register_by_code.php", ['code' => $secretCode->code, 'password' => $password], [
|
||||
'address' => $data['email'],
|
||||
'subject' => "Код регистрации",
|
||||
"from_name" => $_ENV['APP_NAME']
|
||||
]);
|
||||
} else {
|
||||
SecureService::updateSecretCode($model);
|
||||
$secretCode = SecureService::getByField("user_id", $model->id);
|
||||
|
||||
$mailing->send_html("login_by_code.php", ['code' => $secretCode->code], [
|
||||
'address' => $data['email'],
|
||||
'subject' => "Код авторизации",
|
||||
"from_name" => $_ENV['APP_NAME']
|
||||
]);
|
||||
}
|
||||
|
||||
$res = [
|
||||
"status" => "success",
|
||||
"code_expires_at" => $secretCode->code_expires_at,
|
||||
];
|
||||
|
||||
setcookie('user_email', $data['email'], time()+60*15, '/', $_SERVER['SERVER_NAME'], false);
|
||||
$this->renderApi($res);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
#[NoReturn] public function actionCodeCheck(): void
|
||||
{
|
||||
$request = new Request();
|
||||
$code = $request->post("code");
|
||||
|
||||
$model = SecretCode::where("code", $code)->first();
|
||||
if (time() <= strtotime($model->code_expires_at)) {
|
||||
$user = $this->model->where("id", $model->user_id)->first();
|
||||
if ($user){
|
||||
$user->access_token_expires_at = date("Y-m-d H:i:s", strtotime(App::$secure['token_expired_time']));
|
||||
$user->access_token = SecureService::generateAccessToken();
|
||||
$user->save();
|
||||
$this->renderApi([
|
||||
"access_token" => $user->access_token,
|
||||
"access_token_expires_at" => $user->access_token_expires_at,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$this->renderApi(['status' => 'error', 'message' => 'incorrect code']);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public string $migration;
|
||||
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
\kernel\App::$db->schema->create('secret_code', function (Blueprint $table) {
|
||||
$table->increments('id');
|
||||
$table->integer('user_id');
|
||||
$table->integer('code');
|
||||
$table->dateTime('code_expires_at')->nullable(true);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
\kernel\App::$db->schema->dropIfExists('secret_code');
|
||||
|
||||
}
|
||||
};
|
25
kernel/modules/secure/models/SecretCode.php
Normal file
25
kernel/modules/secure/models/SecretCode.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
namespace kernel\modules\secure\models;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $user_id
|
||||
* @property int $code
|
||||
* @property string $code_expires_at
|
||||
*/
|
||||
class SecretCode extends Model {
|
||||
|
||||
protected $table = 'secret_code';
|
||||
protected $fillable = ['user_id', 'code', 'code_expires_at'];
|
||||
|
||||
public static function labels(): array
|
||||
{
|
||||
return [
|
||||
'user_id' => 'Пользователь',
|
||||
'code' => 'Код',
|
||||
'code_expires_at' => 'Срок жизни кода',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
17
kernel/modules/secure/models/forms/LoginEmailForm.php
Normal file
17
kernel/modules/secure/models/forms/LoginEmailForm.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\modules\secure\models\forms;
|
||||
|
||||
use kernel\FormModel;
|
||||
|
||||
class LoginEmailForm extends FormModel
|
||||
{
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'email' => 'required|string|email|max255',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
19
kernel/modules/secure/models/forms/RegisterForm.php
Normal file
19
kernel/modules/secure/models/forms/RegisterForm.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\modules\secure\models\forms;
|
||||
|
||||
use kernel\FormModel;
|
||||
|
||||
class RegisterForm extends FormModel
|
||||
{
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'username' => 'required|min-str-len:5|max-str-len:50',
|
||||
'email' => 'required|email|max-str-len:50',
|
||||
'password' => 'required|min-str-len:6|max-str-len:50',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -14,11 +14,17 @@ App::$collector->group(["prefix" => "admin"], function (RouteCollector $router){
|
||||
App::$collector->get('/login', [\kernel\modules\secure\controllers\SecureController::class, 'actionLogin']);
|
||||
App::$collector->get('/logout', [\kernel\modules\secure\controllers\SecureController::class, 'actionLogout']);
|
||||
App::$collector->post('/auth', [\kernel\modules\secure\controllers\SecureController::class, 'actionAuth']);
|
||||
App::$collector->post('/email_auth', [\kernel\modules\secure\controllers\SecureController::class, 'actionEmailAuth']);
|
||||
App::$collector->get('/register', [\kernel\modules\secure\controllers\SecureController::class, 'actionRegister']);
|
||||
App::$collector->post('/registration', [\kernel\modules\secure\controllers\SecureController::class, 'actionRegistration']);
|
||||
App::$collector->post('/code_check', [\kernel\modules\secure\controllers\SecureController::class, 'actionCodeCheck']);
|
||||
});
|
||||
|
||||
App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){
|
||||
App::$collector->group(["prefix" => "secure"], function (CgRouteCollector $router) {
|
||||
App::$collector->post('/auth', [\kernel\modules\secure\controllers\SecureRestController::class, 'actionAuth']);
|
||||
App::$collector->post('/email_auth', [\kernel\modules\secure\controllers\SecureRestController::class, 'actionEmailAuth']);
|
||||
App::$collector->post('/code_check', [\kernel\modules\secure\controllers\SecureRestController::class, 'actionCodeCheck']);
|
||||
});
|
||||
});
|
||||
|
||||
|
54
kernel/modules/secure/services/SecureService.php
Normal file
54
kernel/modules/secure/services/SecureService.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\modules\secure\services;
|
||||
|
||||
use kernel\App;
|
||||
use kernel\FormModel;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\modules\secure\models\SecretCode;
|
||||
use kernel\modules\user\models\User;
|
||||
use kernel\modules\user\service\UserService;
|
||||
use kernel\services\TokenService;
|
||||
|
||||
class SecureService
|
||||
{
|
||||
|
||||
public static function createSecretCode(User $user): void
|
||||
{
|
||||
$secretCode = new SecretCode();
|
||||
$secretCode->user_id = $user->id;
|
||||
$secretCode->code = mt_rand(100000, 999999);
|
||||
$secretCode->code_expires_at = date("Y-m-d H:i:s", strtotime("+5 minutes"));;
|
||||
$secretCode->save();
|
||||
}
|
||||
|
||||
public static function updateSecretCode(User $user): void
|
||||
{
|
||||
$secretCode = SecretCode::where('user_id', $user->id)->first();
|
||||
$secretCode->code = mt_rand(100000, 999999);
|
||||
$secretCode->code_expires_at = date("Y-m-d H:i:s", strtotime("+5 minutes"));;
|
||||
$secretCode->save();
|
||||
}
|
||||
|
||||
public static function getCodeByUserId(int $user_id)
|
||||
{
|
||||
return SecretCode::where('user_id', $user_id)->one()->code;
|
||||
}
|
||||
|
||||
public static function getByField(string $field, mixed $value)
|
||||
{
|
||||
return SecretCode::where($field, $value)->first();
|
||||
}
|
||||
|
||||
public static function generateAccessToken(): string
|
||||
{
|
||||
return match (App::$secure['token_type']) {
|
||||
"JWT" => TokenService::JWT($_ENV['SECRET_KEY'], 'HS256'),
|
||||
"md5" => TokenService::md5(),
|
||||
"crypt" => TokenService::crypt(),
|
||||
"hash" => TokenService::hash('sha256'),
|
||||
default => TokenService::random_bytes(20),
|
||||
};
|
||||
}
|
||||
|
||||
}
|
48
kernel/modules/secure/views/email_login.php
Normal file
48
kernel/modules/secure/views/email_login.php
Normal file
@ -0,0 +1,48 @@
|
||||
<!-- Section: Design Block -->
|
||||
<section class=" text-center text-lg-start">
|
||||
<style>
|
||||
.rounded-t-5 {
|
||||
border-top-left-radius: 0.5rem;
|
||||
border-top-right-radius: 0.5rem;
|
||||
}
|
||||
|
||||
@media (min-width: 992px) {
|
||||
.rounded-tr-lg-0 {
|
||||
border-top-right-radius: 0;
|
||||
}
|
||||
|
||||
.rounded-bl-lg-5 {
|
||||
border-bottom-left-radius: 0.5rem;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
<div class="card mb-3">
|
||||
<div class="row g-0 d-flex align-items-center">
|
||||
<div class="col-lg-4 d-none d-lg-flex">
|
||||
<img src="https://mdbootstrap.com/img/new/ecommerce/vertical/004.jpg" alt="Trendy Pants and Shoes"
|
||||
class="w-100 rounded-t-5 rounded-tr-lg-0 rounded-bl-lg-5" />
|
||||
</div>
|
||||
<div class="col-lg-8">
|
||||
<div class="card-body py-5 px-md-5">
|
||||
<div class="row md-4 text-md-center">
|
||||
<h1>Форма авторизации/регистрации</h1>
|
||||
</div>
|
||||
|
||||
<form action="/admin/email_auth" method="post">
|
||||
<!-- Email input -->
|
||||
<div data-mdb-input-init class="form-outline mb-4">
|
||||
<input type="text" id="form2Example1" class="form-control" name="email" />
|
||||
<label class="form-label" for="form2Example1">Email</label>
|
||||
</div>
|
||||
|
||||
<!-- Submit button -->
|
||||
<button type="submit" data-mdb-button-init data-mdb-ripple-init class="btn btn-primary btn-block mb-4">Отправить</button>
|
||||
|
||||
</form>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
<!-- Section: Design Block -->
|
60
kernel/modules/secure/views/enter_code.php
Normal file
60
kernel/modules/secure/views/enter_code.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
/**
|
||||
* @var string $email
|
||||
*/
|
||||
?>
|
||||
|
||||
<!-- Section: Design Block -->
|
||||
<section class=" text-center text-lg-start">
|
||||
<style>
|
||||
.rounded-t-5 {
|
||||
border-top-left-radius: 0.5rem;
|
||||
border-top-right-radius: 0.5rem;
|
||||
}
|
||||
|
||||
@media (min-width: 992px) {
|
||||
.rounded-tr-lg-0 {
|
||||
border-top-right-radius: 0;
|
||||
}
|
||||
|
||||
.rounded-bl-lg-5 {
|
||||
border-bottom-left-radius: 0.5rem;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
<div class="card mb-3">
|
||||
<div class="row g-0 d-flex align-items-center">
|
||||
<div class="col-lg-4 d-none d-lg-flex">
|
||||
<img src="https://mdbootstrap.com/img/new/ecommerce/vertical/004.jpg" alt="Trendy Pants and Shoes"
|
||||
class="w-100 rounded-t-5 rounded-tr-lg-0 rounded-bl-lg-5" />
|
||||
</div>
|
||||
<div class="col-lg-8">
|
||||
<div class="card-body py-5 px-md-5">
|
||||
<div class="row md-4 text-md-center">
|
||||
<h1>Введите код, отправленный на почту "<?php echo $email ?>"</h1>
|
||||
</div>
|
||||
|
||||
<form action="/admin/code_check" method="post">
|
||||
<!-- Email input -->
|
||||
<div data-mdb-input-init class="form-outline mb-4">
|
||||
<input type="text" id="form2Example1" class="form-control" name="code" />
|
||||
<label class="form-label" for="form2Example1">Код подтверждения</label>
|
||||
</div>
|
||||
|
||||
<div class="row-md-4">
|
||||
<div class="col">
|
||||
<button type="submit" data-mdb-button-init data-mdb-ripple-init class="btn btn-primary btn-block mb-4">Подтвердить</button>
|
||||
</div>
|
||||
<div class="col">
|
||||
<br>
|
||||
<a href="/admin/login/"> <h5>Отправить код еще раз</h5></a>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
<!-- Section: Design Block -->
|
@ -24,6 +24,9 @@
|
||||
</div>
|
||||
<div class="col-lg-8">
|
||||
<div class="card-body py-5 px-md-5">
|
||||
<div class="row md-4 text-md-center">
|
||||
<h1>Авторизация</h1>
|
||||
</div>
|
||||
|
||||
<form action="/admin/auth" method="post">
|
||||
<!-- Email input -->
|
||||
@ -48,10 +51,15 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col">
|
||||
<div class="col-3">
|
||||
<!-- Simple link -->
|
||||
<a href="#!">Забыл пароль?</a>
|
||||
</div>
|
||||
|
||||
<div class="col">
|
||||
<!-- Simple link -->
|
||||
<a href="/admin/register">Регистрация</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Submit button -->
|
||||
|
71
kernel/modules/secure/views/register.php
Normal file
71
kernel/modules/secure/views/register.php
Normal file
@ -0,0 +1,71 @@
|
||||
<!-- Section: Design Block -->
|
||||
<section class=" text-center text-lg-start">
|
||||
<style>
|
||||
.rounded-t-5 {
|
||||
border-top-left-radius: 0.5rem;
|
||||
border-top-right-radius: 0.5rem;
|
||||
}
|
||||
|
||||
@media (min-width: 992px) {
|
||||
.rounded-tr-lg-0 {
|
||||
border-top-right-radius: 0;
|
||||
}
|
||||
|
||||
.rounded-bl-lg-5 {
|
||||
border-bottom-left-radius: 0.5rem;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
<div class="card mb-3">
|
||||
<div class="row g-0 d-flex align-items-center">
|
||||
<div class="col-lg-4 d-none d-lg-flex">
|
||||
<img src="https://mdbootstrap.com/img/new/ecommerce/vertical/004.jpg" alt="Trendy Pants and Shoes"
|
||||
class="w-100 rounded-t-5 rounded-tr-lg-0 rounded-bl-lg-5" />
|
||||
</div>
|
||||
<div class="col-lg-8">
|
||||
<div class="card-body py-5 px-md-5">
|
||||
|
||||
<div class="row md-4 text-md-center">
|
||||
<h1>Регистрация</h1>
|
||||
</div>
|
||||
|
||||
<form action="/admin/registration" method="post">
|
||||
<!--Username input -->
|
||||
<div data-mdb-input-init class="form-outline mb-4">
|
||||
<input type="text" id="form2Example1" class="form-control" name="username" />
|
||||
<label class="form-label" for="form2Example1">Логин</label>
|
||||
</div>
|
||||
|
||||
<!-- Email input -->
|
||||
<div data-mdb-input-init class="form-outline mb-4">
|
||||
<input type="email" id="form2Example1" class="form-control" name="email" />
|
||||
<label class="form-label" for="form2Example1">Email</label>
|
||||
</div>
|
||||
|
||||
<!-- Password input -->
|
||||
<div data-mdb-input-init class="form-outline mb-4">
|
||||
<input type="password" id="form2Example2" class="form-control" name="password" />
|
||||
<label class="form-label" for="form2Example2">Пароль</label>
|
||||
</div>
|
||||
|
||||
<div class="row mb-4">
|
||||
<div class="col-4">
|
||||
<!-- Submit button -->
|
||||
<button type="submit" data-mdb-button-init data-mdb-ripple-init class="btn btn-primary btn-block mb-4">Регистрация</button>
|
||||
</div>
|
||||
<div class="col-4">
|
||||
<!-- Simple link -->
|
||||
<a href="/admin/login">Войти в существующий аккаунт</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</form>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
<!-- Section: Design Block -->
|
@ -5,9 +5,11 @@ namespace kernel\modules\user\controllers;
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use kernel\AdminController;
|
||||
use kernel\EntityRelation;
|
||||
use kernel\modules\user\models\forms\CreateUserForm;
|
||||
use kernel\modules\user\models\User;
|
||||
use kernel\modules\user\service\UserService;
|
||||
use kernel\Request;
|
||||
use Twig\Error\LoaderError;
|
||||
use Twig\Error\RuntimeError;
|
||||
use Twig\Error\SyntaxError;
|
||||
@ -35,6 +37,11 @@ class UserController extends AdminController
|
||||
$userForm->load($_REQUEST);
|
||||
if ($userForm->validate()){
|
||||
$user = $this->userService->create($userForm);
|
||||
|
||||
|
||||
$entityRelation = new EntityRelation();
|
||||
$entityRelation->saveEntityRelation(entity: "user", model: $user, request: new Request());
|
||||
|
||||
if ($user){
|
||||
$this->redirect("/admin/user/view/" . $user->id);
|
||||
}
|
||||
@ -91,8 +98,12 @@ class UserController extends AdminController
|
||||
$userForm = new CreateUserForm();
|
||||
$userService = new UserService();
|
||||
$userForm->load($_REQUEST);
|
||||
if ($userForm->validate()){
|
||||
if ($userForm->validateForUpdate()){
|
||||
$user = $userService->update($userForm, $user);
|
||||
|
||||
$entityRelation = new EntityRelation();
|
||||
$entityRelation->saveEntityRelation(entity: "user", model: $user, request: new Request());
|
||||
|
||||
if ($user){
|
||||
$this->redirect("/admin/user/view/" . $user->id);
|
||||
}
|
||||
@ -100,9 +111,20 @@ class UserController extends AdminController
|
||||
$this->redirect("/admin/user/update/" . $id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
#[NoReturn] public function actionDelete($id): void
|
||||
{
|
||||
User::find($id)->delete();
|
||||
$user = User::find($id)->first();
|
||||
if (!$user){
|
||||
throw new Exception(message: "The user not found");
|
||||
}
|
||||
|
||||
$entityRelation = new EntityRelation();
|
||||
$entityRelation->deleteEntityRelation(entity: "user", model: $user);
|
||||
|
||||
$user->delete();
|
||||
$this->redirect("/admin/user/");
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,8 @@ use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public string $migration;
|
||||
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
|
@ -16,4 +16,13 @@ class CreateUserForm extends FormModel
|
||||
];
|
||||
}
|
||||
|
||||
public function rulesForUpdate(): array
|
||||
{
|
||||
return [
|
||||
'username' => 'required|min-str-len:5|max-str-len:30',
|
||||
'password' => '',
|
||||
'email' => 'required|email'
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
namespace kernel\modules\user\service;
|
||||
|
||||
use kernel\FormModel;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\modules\user\models\User;
|
||||
|
||||
class UserService
|
||||
@ -25,7 +26,9 @@ class UserService
|
||||
{
|
||||
$user->username = $form_model->getItem('username');
|
||||
$user->email = $form_model->getItem('email');
|
||||
$user->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT);
|
||||
if ($form_model->getItem('password')) {
|
||||
$user->password_hash = password_hash($form_model->getItem('password'), PASSWORD_DEFAULT);
|
||||
}
|
||||
if ($user->save()){
|
||||
return $user;
|
||||
}
|
||||
@ -38,7 +41,7 @@ class UserService
|
||||
* @param string $value
|
||||
* @return mixed
|
||||
*/
|
||||
public function getByField(string $field, string $value)
|
||||
public static function getByField(string $field, string $value): mixed
|
||||
{
|
||||
return User::where($field, $value)->first();
|
||||
}
|
||||
@ -82,4 +85,13 @@ class UserService
|
||||
return $this->getByField("access_token", $token);
|
||||
}
|
||||
|
||||
public static function createUserByEmailAndPassword(string $email, string $password): void
|
||||
{
|
||||
$user = new User();
|
||||
$user->email = $email;
|
||||
$user->username = $email;
|
||||
$user->password_hash = password_hash($password, PASSWORD_DEFAULT);
|
||||
$user->save();
|
||||
}
|
||||
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
use kernel\modules\user\models\User;
|
||||
|
||||
$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", enctype: 'multipart/form-data');
|
||||
|
||||
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "username", params: [
|
||||
'class' => "form-control",
|
||||
@ -32,6 +32,11 @@ $form->field(class: \itguild\forms\inputs\TextInput::class, name: "email", param
|
||||
->setLabel("Email")
|
||||
->render();
|
||||
|
||||
$entityRelations = new \kernel\EntityRelation();
|
||||
if (!isset($model)) {
|
||||
$model = new User();
|
||||
}
|
||||
$entityRelations->renderEntityAdditionalPropertyFormBySlug("user", $model);
|
||||
?>
|
||||
<div class="row">
|
||||
<div class="col-sm-2">
|
||||
|
@ -24,6 +24,16 @@ $table = new ListEloquentTable(new EloquentDataProvider(User::class, [
|
||||
'baseUrl' => "/admin/user",
|
||||
'filters' => ['email'],
|
||||
]));
|
||||
|
||||
$entityRelation = new \kernel\EntityRelation();
|
||||
$additionals = $entityRelation->getEntityRelationsBySlug("user");
|
||||
|
||||
foreach ($additionals as $additional) {
|
||||
$table->addColumn($additional, $additional, function ($id) use ($entityRelation, $additional) {
|
||||
return $entityRelation->getAdditionalPropertyByEntityId("user", $id, $additional);
|
||||
});
|
||||
}
|
||||
|
||||
$table->columns([
|
||||
'username' => [
|
||||
"filter" => [
|
||||
|
@ -25,6 +25,15 @@ $table->beforePrint(function () use ($user) {
|
||||
return $btn;
|
||||
});
|
||||
|
||||
$entityRelation = new \kernel\EntityRelation();
|
||||
$additionals = $entityRelation->getEntityAdditionalProperty("user", $user);
|
||||
|
||||
foreach ($additionals as $key => $additional) {
|
||||
$table->addRow($key, function () use ($additional) {
|
||||
return $additional;
|
||||
}, ['after' => 'email']);
|
||||
}
|
||||
|
||||
$table->rows([
|
||||
'created_at' => function ($data) {
|
||||
if (!$data){
|
||||
|
21
kernel/services/ConsoleService.php
Normal file
21
kernel/services/ConsoleService.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\services;
|
||||
|
||||
class ConsoleService
|
||||
{
|
||||
public function runComposerRequire(string $package): void
|
||||
{
|
||||
exec("composer require $package");
|
||||
}
|
||||
|
||||
public function runComposerRemove(string $package): void
|
||||
{
|
||||
exec("composer remove $package");
|
||||
}
|
||||
|
||||
public function runCommand(string $command): void
|
||||
{
|
||||
exec($command);
|
||||
}
|
||||
}
|
88
kernel/services/KernelService.php
Normal file
88
kernel/services/KernelService.php
Normal file
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\services;
|
||||
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\helpers\Files;
|
||||
use kernel\helpers\Manifest;
|
||||
use kernel\helpers\RESTClient;
|
||||
use kernel\Request;
|
||||
use ZipArchive;
|
||||
|
||||
class KernelService
|
||||
{
|
||||
protected null|bool $serverAvailable = null;
|
||||
protected ModuleService $moduleService;
|
||||
protected Files $files;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->moduleService = new ModuleService();
|
||||
$this->files = new Files();
|
||||
}
|
||||
|
||||
public function getKernelInfo(): false|array|string
|
||||
{
|
||||
$info = [];
|
||||
$info['path'] = KERNEL_DIR;
|
||||
if (file_exists(KERNEL_DIR . "/manifest.json")) {
|
||||
$manifest = json_decode(file_get_contents(KERNEL_DIR . "/manifest.json"), true);
|
||||
$manifest = getConst($manifest);
|
||||
$info = array_merge($info, $manifest);
|
||||
}
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
public function isLastVersion(): bool
|
||||
{
|
||||
if ($this->moduleService->isServerAvailable()) {
|
||||
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
|
||||
|
||||
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
|
||||
|
||||
$kernel_info = $this->getKernelInfo();
|
||||
foreach ($modules_info as $mod) {
|
||||
if ($mod['slug'] === $kernel_info['slug'] && $mod['version'] === $kernel_info['version']) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function updateKernel(string $path): bool
|
||||
{
|
||||
$request = new Request();
|
||||
$files = $request->post('files');
|
||||
|
||||
$zip = new ZipArchive;
|
||||
if (file_exists(ROOT_DIR . $path)) {
|
||||
$tmpKernelDir = md5(time());
|
||||
$res = $zip->open(ROOT_DIR . $path);
|
||||
if ($res === TRUE) {
|
||||
$tmpKernelDirFull = RESOURCES_DIR . '/tmp/kernel/' . $tmpKernelDir . "/";
|
||||
$zip->extractTo($tmpKernelDirFull);
|
||||
$zip->close();
|
||||
$this->files->recursiveRemoveKernelDir();
|
||||
$this->files->copy_folder($tmpKernelDirFull . 'kernel' , ROOT_DIR . "/kernel");
|
||||
|
||||
foreach ($files as $file) {
|
||||
if ($file === 'bootstrap') {
|
||||
$this->files->recursiveRemoveDir(ROOT_DIR . '/bootstrap');
|
||||
$this->files->copy_folder($tmpKernelDirFull . 'bootstrap' , ROOT_DIR . '/bootstrap');
|
||||
}
|
||||
copy($tmpKernelDirFull . $file , ROOT_DIR . '/' . $file);
|
||||
}
|
||||
|
||||
$this->files->recursiveRemoveDir($tmpKernelDirFull);
|
||||
unlink(ROOT_DIR . $path);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@ -36,4 +36,25 @@ class MigrationService
|
||||
}
|
||||
}
|
||||
|
||||
public function rollbackAtPath(string $path): void
|
||||
{
|
||||
$path = getConst($path);
|
||||
try {
|
||||
$filesystem = new Filesystem();
|
||||
$dmr = new DatabaseMigrationRepository(App::$db->capsule->getDatabaseManager(), 'migration');
|
||||
|
||||
$m = new Migrator($dmr, App::$db->capsule->getDatabaseManager(), $filesystem);
|
||||
|
||||
$migrationFiles = $m->getMigrationFiles($path);
|
||||
foreach ($migrationFiles as $name => $migrationFile){
|
||||
$migrationInstance = $filesystem->getRequire($migrationFile);
|
||||
$migrationInstance->migration = $name;
|
||||
$migrationInstance->down();
|
||||
$dmr->delete($migrationInstance);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception('Не удалось откатить миграции');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -4,6 +4,7 @@ namespace kernel\services;
|
||||
|
||||
use DirectoryIterator;
|
||||
use kernel\EntityRelation;
|
||||
use kernel\Flash;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\helpers\Files;
|
||||
use kernel\helpers\Manifest;
|
||||
@ -15,6 +16,8 @@ class ModuleService
|
||||
{
|
||||
protected array $errors = [];
|
||||
|
||||
protected null|bool $serverAvailable = null;
|
||||
|
||||
/**
|
||||
* @param string $module
|
||||
* @return false|array|string
|
||||
@ -109,7 +112,7 @@ class ModuleService
|
||||
if (isset($module_info['dependence'])) {
|
||||
$dependence_array = explode(',', $module_info['dependence']);
|
||||
foreach ($dependence_array as $depend) {
|
||||
if (!in_array($depend, $active_modules->modules)) {
|
||||
if (!in_array(trim($depend), $active_modules->modules)) {
|
||||
$this->addError("first activate the $depend module");
|
||||
return false;
|
||||
}
|
||||
@ -359,8 +362,6 @@ class ModuleService
|
||||
mkdir(RESOURCES_DIR . '/tmp/modules', 0777, true);
|
||||
}
|
||||
$fileHelper->pack($tmpModuleDirFull, RESOURCES_DIR . '/tmp/modules/' . $moduleName . '.igm');
|
||||
|
||||
//$fileHelper->recursiveRemoveDir($tmpModuleDirFull);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -442,17 +443,20 @@ class ModuleService
|
||||
|
||||
public function isLastVersion(string $slug): bool
|
||||
{
|
||||
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
|
||||
if ($this->isServerAvailable()) {
|
||||
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
|
||||
|
||||
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
|
||||
$mod_info = $this->getModuleInfoBySlug($slug);
|
||||
foreach ($modules_info as $mod) {
|
||||
if ($mod['slug'] === $mod_info['slug'] && $mod['version'] === $mod_info['version']) {
|
||||
return true;
|
||||
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
|
||||
|
||||
$mod_info = $this->getModuleInfoBySlug($slug);
|
||||
foreach ($modules_info as $mod) {
|
||||
if ($mod['slug'] === $mod_info['slug'] && $mod['version'] === $mod_info['version']) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function isKernelModule(string $slug): bool
|
||||
@ -467,6 +471,29 @@ class ModuleService
|
||||
return false;
|
||||
}
|
||||
|
||||
public function isShopModule(string $slug): bool
|
||||
{
|
||||
if ($this->isServerAvailable()) {
|
||||
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
|
||||
|
||||
if (!$this->issetModuleShopToken()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
|
||||
if (isset($modules_info)) {
|
||||
$mod_info = $this->getModuleInfoBySlug($slug);
|
||||
foreach ($modules_info as $mod) {
|
||||
if ($mod['slug'] === $mod_info['slug']) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getKernelModules(): array
|
||||
{
|
||||
$modules_info = [];
|
||||
@ -478,4 +505,77 @@ class ModuleService
|
||||
return $modules_info;
|
||||
}
|
||||
|
||||
public function isServerAvailable(): bool
|
||||
{
|
||||
if (null !== $this->serverAvailable) {
|
||||
return $this->serverAvailable;
|
||||
}
|
||||
|
||||
try {
|
||||
RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
|
||||
$this->serverAvailable = true;
|
||||
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$this->serverAvailable = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function issetModuleShopToken(): bool
|
||||
{
|
||||
if (!empty($_ENV['MODULE_SHOP_TOKEN'])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function createDirs(string $slug): void
|
||||
{
|
||||
mkdir(KERNEL_APP_MODULES_DIR . "/$slug");
|
||||
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/controllers");
|
||||
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/migrations");
|
||||
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/services");
|
||||
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/models");
|
||||
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/models/forms");
|
||||
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/routs");
|
||||
mkdir(KERNEL_APP_MODULES_DIR . "/$slug/views");
|
||||
|
||||
mkdir(APP_DIR . "/modules/$slug");
|
||||
mkdir(APP_DIR . "/modules/$slug/controllers");
|
||||
mkdir(APP_DIR . "/modules/$slug/routs");
|
||||
}
|
||||
|
||||
public function createModuleByParams(array $params): void
|
||||
{
|
||||
$slug = $params['slug'];
|
||||
$model = $params['model'];
|
||||
|
||||
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/manifests/manifest_template', APP_DIR . "/modules/$slug/manifest.json", $params);
|
||||
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/controllers/kernel_controller_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/controllers/' . $model . 'Controller.php', $params);
|
||||
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/controllers/app_controller_template', APP_DIR . '/modules/' . $slug . '/controllers/' . $model . 'Controller.php', $params);
|
||||
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/routs/kernel_routs_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/routs/' . $slug . '.php', $params);
|
||||
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/routs/app_routs_template', APP_DIR . '/modules/' . $slug . '/routs/' . $slug . '.php', $params);
|
||||
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/module_files/kernel_module_file_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/' . $model . 'Module.php', $params);
|
||||
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/module_files/app_module_file_template', APP_DIR . '/modules/' . $slug . '/' . $model . 'Module.php', $params);
|
||||
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/models/model_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/models/' . $model . '.php', $params);
|
||||
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/models/forms/create_form_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/models/forms/Create' . $model . 'Form.php', $params);
|
||||
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/services/service_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/services/' . $model . 'Service.php', $params);
|
||||
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/views/index_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/index.php', $params);
|
||||
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/views/view_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/view.php', $params);
|
||||
$this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/views/form_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/form.php', $params);
|
||||
}
|
||||
|
||||
public function createModuleFileByTemplate(string $templatePath, string $filePath, array $params): void
|
||||
{
|
||||
$data = file_get_contents($templatePath);
|
||||
|
||||
foreach ($params as $key => $param){
|
||||
$data = str_replace("{" . $key . "}", $param, $data);
|
||||
}
|
||||
|
||||
file_put_contents($filePath, $data);
|
||||
}
|
||||
|
||||
}
|
36
kernel/services/ModuleShopService.php
Normal file
36
kernel/services/ModuleShopService.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\services;
|
||||
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use kernel\helpers\RESTClient;
|
||||
|
||||
class ModuleShopService
|
||||
{
|
||||
protected string $url;
|
||||
protected string $token;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->url = $_ENV['MODULE_SHOP_URL'];
|
||||
$this->token = $_ENV['MODULE_SHOP_TOKEN'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function email_auth(string $email)
|
||||
{
|
||||
$request = RESTClient::post($this->url . "/api/secure/email_auth", ['email' => $email], false);
|
||||
|
||||
return json_decode($request->getBody()->getContents(), true);
|
||||
}
|
||||
|
||||
public function code_check(string $code)
|
||||
{
|
||||
$request = RESTClient::post($this->url . "/api/secure/code_check", ['code' => $code], false);
|
||||
|
||||
return json_decode($request->getBody()->getContents(), true);
|
||||
}
|
||||
|
||||
}
|
@ -32,7 +32,7 @@ class TokenService
|
||||
*/
|
||||
public static function md5(): string
|
||||
{
|
||||
return md5(microtime() . self::getSalt() . time());
|
||||
return md5(microtime() . self::getSalt(10) . time());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -40,7 +40,7 @@ class TokenService
|
||||
*/
|
||||
public static function crypt(): string
|
||||
{
|
||||
return crypt(microtime(), self::getSalt());
|
||||
return crypt(microtime(), self::getSalt(20));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -48,15 +48,15 @@ class TokenService
|
||||
*/
|
||||
public static function hash(string $alg): string
|
||||
{
|
||||
return hash($alg, self::getSalt());
|
||||
return hash($alg, self::getSalt(10));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws RandomException
|
||||
*/
|
||||
public static function getSalt(): string
|
||||
public static function getSalt(int $length): string
|
||||
{
|
||||
return bin2hex(random_bytes(10));
|
||||
return bin2hex(random_bytes($length));
|
||||
}
|
||||
|
||||
}
|
8
kernel/templates/controllers/app_controller_template
Normal file
8
kernel/templates/controllers/app_controller_template
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace app\modules\{slug}\controllers;
|
||||
|
||||
class {model}Controller extends \kernel\app_modules\{slug}\controllers\{model}Controller
|
||||
{
|
||||
|
||||
}
|
98
kernel/templates/controllers/kernel_controller_template
Normal file
98
kernel/templates/controllers/kernel_controller_template
Normal file
@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\{slug}\controllers;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use kernel\AdminController;
|
||||
use kernel\app_modules\{slug}\models\forms\Create{model}Form;
|
||||
use kernel\app_modules\{slug}\models\{model};
|
||||
use kernel\app_modules\{slug}\services\{model}Service;
|
||||
|
||||
class {model}Controller extends AdminController
|
||||
{
|
||||
private {model}Service ${slug}Service;
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/{slug}/views/";
|
||||
$this->{slug}Service = new {model}Service();
|
||||
}
|
||||
|
||||
public function actionCreate(): void
|
||||
{
|
||||
$this->cgView->render("form.php");
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionAdd(): void
|
||||
{
|
||||
${slug}Form = new Create{model}Form();
|
||||
${slug}Form->load($_REQUEST);
|
||||
if (${slug}Form->validate()){
|
||||
${slug} = $this->{slug}Service->create(${slug}Form);
|
||||
if (${slug}){
|
||||
$this->redirect("/admin/{slug}/view/" . ${slug}->id);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/{slug}/create");
|
||||
}
|
||||
|
||||
public function actionIndex($page_number = 1): void
|
||||
{
|
||||
$this->cgView->render("index.php", ['page_number' => $page_number]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionView($id): void
|
||||
{
|
||||
${slug} = {model}::find($id);
|
||||
|
||||
if (!${slug}){
|
||||
throw new Exception(message: "The {slug} not found");
|
||||
}
|
||||
$this->cgView->render("view.php", ['{slug}' => ${slug}]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionUpdate($id): void
|
||||
{
|
||||
$model = {model}::find($id);
|
||||
if (!$model){
|
||||
throw new Exception(message: "The {slug} not found");
|
||||
}
|
||||
|
||||
$this->cgView->render("form.php", ['model' => $model]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionEdit($id): void
|
||||
{
|
||||
${slug} = {model}::find($id);
|
||||
if (!${slug}){
|
||||
throw new Exception(message: "The {slug} not found");
|
||||
}
|
||||
${slug}Form = new Create{model}Form();
|
||||
${slug}Service = new {model}Service();
|
||||
${slug}Form->load($_REQUEST);
|
||||
if (${slug}Form->validate()) {
|
||||
${slug} = ${slug}Service->update(${slug}Form, ${slug});
|
||||
if (${slug}) {
|
||||
$this->redirect("/admin/{slug}/view/" . ${slug}->id);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/{slug}/update/" . $id);
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionDelete($id): void
|
||||
{
|
||||
${slug} = {model}::find($id)->first();
|
||||
${slug}->delete();
|
||||
$this->redirect("/admin/{slug}/");
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user