server test version

This commit is contained in:
2025-01-03 02:11:09 +03:00
parent 093b04c2c9
commit 74efe9e01e
101 changed files with 2871 additions and 117 deletions

View File

@ -0,0 +1,39 @@
<?php
namespace kernel\app_modules\tgbot;
use kernel\Module;
use kernel\modules\menu\service\MenuService;
use kernel\services\MigrationService;
class TgbotModule 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}/tgbot/migrations");
$this->menuService->createItem([
"label" => "TG bot",
"url" => "/admin/tg-bot",
"slug" => "tg-bot",
]);
}
public function deactivate()
{
$this->menuService->removeItemBySlug("tg-bot");
$this->migrationService->rollbackAtPath("{KERNEL_APP_MODULES}/tgbot/migrations");
}
}

View File

@ -0,0 +1,91 @@
<?php
namespace kernel\app_modules\tgbot\controllers;
use app\modules\tgbot\models\Tgbot;
use JetBrains\PhpStorm\NoReturn;
use kernel\app_modules\card\models\forms\CreateCardForm;
use kernel\app_modules\card\services\CardService;
use kernel\helpers\Debug;
use kernel\modules\user\models\forms\CreateUserForm;
use kernel\modules\user\service\UserService;
use kernel\Request;
use kernel\RestController;
use kernel\services\TokenService;
class TgBotRestController extends RestController
{
public function __construct()
{
$this->model = new Tgbot();
}
#[NoReturn] public function actionStore(): void
{
$request = new Request();
$data = $request->post();
$tgBot = $this->model->where("bot_id", $data['bot_id'])->where("dialog_id", $data['dialog_id'])->first();
if (!$tgBot){
foreach ($this->model->getFillable() as $item){
$this->model->{$item} = $data[$item] ?? null;
}
$userService = new UserService();
$userForm = new CreateUserForm();
$username = $data['username'];
$userForm->load([
'username' => $username,
'password' => TokenService::random_bytes(20),
'email' => $username . "@hookahdnr.ru"
]);
$user = $userService->create($userForm);
if ($user) {
$this->model->user_id = $user->id;
}
$this->model->save();
$this->renderApi($this->model->toArray());
}
$this->renderApi($tgBot->toArray());
}
#[NoReturn] public function actionCreateCard(): void
{
$cardService = new CardService();
$request = new Request();
$data = $request->post();
$form = new CreateCardForm();
$form->load($data);
$form->setItem('payment_type', 2);
$form->setItem('bank_id', 323);
$form->setItem('info', 42);
$form->setItem('program', 74);
$form->setItem('cvc', 101);
$form->setItem('pin', 1111);
$form->setItem('status', 1);
if ($form->validate()){
$model = $cardService->create($form);
$this->renderApi($model->load(['cardFile'])->toArray());
}
$this->renderApi([]);
}
#[NoReturn] public function actionGetByDialog(int $dialog_id, int $bot_id): void
{
$model = \kernel\app_modules\tgbot\models\Tgbot::where(['dialog_id' => $dialog_id, 'bot_id' => $bot_id])->first();
if ($model) {
$this->renderApi($model->toArray());
}
$this->renderApi([]);
}
}

View File

@ -0,0 +1,111 @@
<?php
namespace kernel\app_modules\tgbot\controllers;
use app\modules\tgbot\models\forms\CreateTgBotForm;
use app\modules\tgbot\models\Tgbot;
use app\modules\tgbot\services\TgBotService;
use Exception;
use JetBrains\PhpStorm\NoReturn;
use kernel\AdminController;
use kernel\helpers\Debug;
use kernel\modules\user\models\forms\CreateUserForm;
use kernel\modules\user\service\UserService;
use kernel\services\TokenService;
class TgbotController extends AdminController
{
protected TgBotService $botService;
protected function init(): void
{
parent::init();
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/tgbot/views/tgbot/";
$this->botService = new TgBotService();
}
public function actionCreate(): void
{
$this->cgView->render("form.php");
}
public function actionIndex($page_number = 1): void
{
$this->cgView->render("index.php", ['page_number' => $page_number]);
}
#[NoReturn] public function actionAdd(): void
{
$tgForm = new CreateTgBotForm();
$tgForm->load($_REQUEST);
if ($tgForm->validate()) {
$tg = $this->botService->create($tgForm);
if ($tg) {
$this->redirect("/admin/tg-bot/view/" . $tg->id);
}
}
$this->redirect("/admin/tg-bot/create");
}
/**
* @throws Exception
*/
public function actionUpdate($id): void
{
$model = Tgbot::find($id);
if (!$model) {
throw new Exception(message: "The dialog not found");
}
$this->cgView->render("form.php", ['model' => $model]);
}
/**
* @throws Exception
*/
public function actionEdit($id): void
{
$tg = Tgbot::find($id);
if (!$tg) {
throw new Exception(message: "The tag not found");
}
$tgForm = new CreateTgBotForm();
$tgService = new TgBotService();
$tgForm->load($_REQUEST);
if ($tgForm->validate()) {
$tg = $tgService->update($tgForm, $tg);
if ($tg) {
$this->redirect("/admin/tg-bot/view/" . $tg->id);
}
}
$this->redirect("/admin/tg-bot/update/" . $id);
}
/**
* @throws Exception
*/
public function actionView($id): void
{
$tg = Tgbot::find($id);
if (!$tg) {
throw new Exception(message: "The dialog not found");
}
$this->cgView->render("view.php", ['tg' => $tg]);
}
/**
* @throws Exception
*/
#[NoReturn] public function actionDelete(int $id): void
{
$post = Tgbot::find($id)->first();
if (!$post){
throw new Exception(message: "The tg client not found");
}
$post->delete();
$this->redirect("/admin/tg-bot/");
}
}

View File

@ -0,0 +1,35 @@
<?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('tgbot', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('bot_id')->nullable(false);
$table->bigInteger('dialog_id')->nullable(false);
$table->integer('user_id')->nullable();
$table->string('username', 255)->nullable(false);
$table->string('first_name', 255)->nullable();
$table->string('last_name', 255)->nullable();
$table->integer('status')->nullable()->default(1);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
\kernel\App::$db->schema->dropIfExists('tgbot');
}
};

View File

@ -0,0 +1,51 @@
<?php
namespace kernel\app_modules\tgbot\models;
use Illuminate\Database\Eloquent\Model;
/**
* @property integer $id
* @property integer $bot_id
* @property integer $dialog_id
* @property integer $user_id
* @property string $username
* @property string $first_name
* @property string $last_name
* @property integer $status
*/
class Tgbot extends Model
{
const DISABLE_STATUS = 0;
const ACTIVE_STATUS = 1;
protected $table = 'tgbot';
protected $fillable = ['bot_id', 'dialog_id', 'user_id', 'username', 'first_name', 'last_name', 'status'];
public static function labels(): array
{
return [
'bot_id' => 'Bot ID',
'dialog_id' => 'Dialog ID',
'user_id' => 'User ID',
'username' => 'Username',
'first_name' => 'First name',
'last_name' => 'Last name',
'status' => 'Статус',
];
}
/**
* @return string[]
*/
public static function getStatus(): array
{
return [
self::DISABLE_STATUS => "Не активный",
self::ACTIVE_STATUS => "Активный",
];
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace kernel\app_modules\tgbot\models\forms;
use kernel\FormModel;
/**
* @property integer $bot_id
* @property integer $dialog_id
* @property string $username
* @property string $first_name
* @property string $last_name
* @property integer $status
*/
class CreateTgBotForm extends FormModel
{
public function rules(): array
{
return [
'bot_id' => 'required',
'dialog_id' => 'required',
'username' => 'required',
'first_name' => '',
'last_name' => '',
'status' => ''
];
}
}

View File

@ -0,0 +1,33 @@
<?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" => "tg-bot"], function (CGRouteCollector $router) {
App::$collector->get('/', [\app\modules\tgbot\controllers\TgbotController::class, 'actionIndex']);
App::$collector->get('/page/{page_number}', [\app\modules\tag\controllers\TagController::class, 'actionIndex']);
App::$collector->get('/create', [\app\modules\tgbot\controllers\TgbotController::class, 'actionCreate']);
App::$collector->post("/", [\app\modules\tgbot\controllers\TgbotController::class, 'actionAdd']);
App::$collector->get('/view/{id}', [\app\modules\tgbot\controllers\TgbotController::class, 'actionView']);
App::$collector->any('/update/{id}', [\app\modules\tgbot\controllers\TgbotController::class, 'actionUpdate']);
App::$collector->any("/edit/{id}", [\app\modules\tgbot\controllers\TgbotController::class, 'actionEdit']);
App::$collector->get('/delete/{id}', [\app\modules\tgbot\controllers\TgbotController::class, 'actionDelete']);
});
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']);
});
});
});
App::$collector->group(["prefix" => "api"], function (CgRouteCollector $router){
App::$collector->group(['before' => 'bearer'], function (CgRouteCollector $router){
$router->rest("tg-bot", [\app\modules\tgbot\controllers\TgBotRestController::class]);
$router->post('/tg-bot/create-card', [\app\modules\tgbot\controllers\TgBotRestController::class, 'actionCreateCard']);
$router->get('/tg-bot/get-by-dialog/{dialog_id}/{bot_id}', [\app\modules\tgbot\controllers\TgBotRestController::class, 'actionGetByDialog']);
});
});

View File

@ -0,0 +1,48 @@
<?php
namespace kernel\app_modules\tgbot\services;
use app\modules\tgbot\models\Tgbot;
use kernel\app_modules\tag\models\Tag;
use kernel\FormModel;
use kernel\helpers\Debug;
use kernel\helpers\Slug;
use kernel\services\ModuleService;
class TgBotService
{
public function create(FormModel $form_model): false|Tgbot
{
$model = new Tgbot();
$model->bot_id = $form_model->getItem('bot_id');
$model->dialog_id = $form_model->getItem('dialog_id');
$model->user_id = $form_model->getItem('user_id');
$model->username = $form_model->getItem('username');
$model->first_name = $form_model->getItem('first_name');
$model->last_name = $form_model->getItem('last_name');
$model->status = $form_model->getItem('status');
if ($model->save()){
return $model;
}
return false;
}
public function update(FormModel $form_model, Tgbot $model): false|Tgbot
{
$model->bot_id = $form_model->getItem('bot_id');
$model->dialog_id = $form_model->getItem('dialog_id');
$model->user_id = $form_model->getItem('user_id');
$model->username = $form_model->getItem('username');
$model->first_name = $form_model->getItem('first_name');
$model->last_name = $form_model->getItem('last_name');
$model->status = $form_model->getItem('status');
if ($model->save()){
return $model;
}
return false;
}
}

View File

@ -0,0 +1,83 @@
<?php
/**
* @var Tgbot $model
*/
use app\modules\tgbot\models\Tgbot;
$form = new \itguild\forms\ActiveForm();
$form->beginForm(isset($model) ? "/admin/tg-bot/edit/" . $model->id : "/admin/tg-bot");
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "bot_id", params: [
'class' => "form-control",
'placeholder' => 'Bot ID',
'value' => $model->bot_id ?? ''
])
->setLabel("Bot ID")
->render();
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "dialog_id", params: [
'class' => "form-control",
'placeholder' => 'Dialog ID',
'value' => $model->dialog_id ?? ''
])
->setLabel("Dialog ID")
->render();
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "username", params: [
'class' => "form-control",
'placeholder' => 'Username',
'value' => $model->username ?? ''
])
->setLabel("Username")
->render();
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "first_name", params: [
'class' => "form-control",
'placeholder' => 'First name',
'value' => $model->first_name ?? ''
])
->setLabel("First name")
->render();
$form->field(class: \itguild\forms\inputs\TextInput::class, name: "last_name", params: [
'class' => "form-control",
'placeholder' => 'Last name',
'value' => $model->last_name ?? ''
])
->setLabel("Last name")
->render();
$form->field(\itguild\forms\inputs\Select::class, 'status', [
'class' => "form-control",
'value' => $model->status ?? ''
])
->setLabel("Статус")
->setOptions(Tgbot::getStatus())
->render();
?>
<div class="row">
<div class="col-sm-2">
<?php
$form->field(\itguild\forms\inputs\Button::class, name: "btn-submit", params: [
'class' => "btn btn-primary ",
'value' => 'Отправить',
'typeInput' => 'submit'
])
->render();
?>
</div>
<div class="col-sm-2">
<?php
$form->field(\itguild\forms\inputs\Button::class, name: "btn-reset", params: [
'class' => "btn btn-warning",
'value' => 'Сбросить',
'typeInput' => 'reset'
])
->render();
?>
</div>
</div>
<?php
$form->endForm();

View File

@ -0,0 +1,59 @@
<?php
/**
* @var int $page_number
* @var \kernel\CgView $view
*/
use Itguild\EloquentTable\EloquentDataProvider;
use Itguild\EloquentTable\ListEloquentTable;
use kernel\app_modules\tag\models\Tag;
use kernel\IGTabel\btn\PrimaryBtn;
use kernel\models\Menu;
use kernel\modules\menu\table\columns\MenuDeleteActionColumn;
use kernel\modules\menu\table\columns\MenuEditActionColumn;
use kernel\modules\menu\table\columns\MenuViewActionColumn;
$view->setTitle("Список существующих диалогов");
$table = new ListEloquentTable(new EloquentDataProvider(\app\modules\tgbot\models\Tgbot::class, [
'currentPage' => $page_number,
'perPage' => 8,
'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/tg-bot",
'searchPrefix' => "",
'searchParams' => (new \kernel\Request())->get(),
]));
$table->beforePrint(function () {
return PrimaryBtn::create("Создать", "/admin/tg-bot/create")->fetch();
});
$table->columns([
"status" => [
"value" => function ($cell) {
return \app\modules\tgbot\models\Tgbot::getStatus()[$cell];
}
],
"username" => [
"filter" => [
"class" => \Itguild\Tables\Filter\InputTextFilter::class
]
],
"bot_id" => [
"filter" => [
"class" => \Itguild\Tables\Filter\InputTextFilter::class
]
],
"dialog_id" => [
"filter" => [
"class" => \Itguild\Tables\Filter\InputTextFilter::class
]
]
]);
$table->addAction(\kernel\IGTabel\action_column\ViewActionColumn::class);
$table->addAction(\kernel\IGTabel\action_column\DeleteActionColumn::class);
$table->addAction(\kernel\IGTabel\action_column\EditActionColumn::class);
$table->create();
$table->render();

View File

@ -0,0 +1,30 @@
<?php
/**
* @var \Illuminate\Database\Eloquent\Collection $tg
*/
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;
$table = new ViewEloquentTable(new ViewJsonTableEloquentModel($tg, [
'params' => ["class" => "table table-bordered", "border" => "2"],
'baseUrl' => "/admin/tg-bot",
]));
$table->beforePrint(function () use ($tg) {
$btn = PrimaryBtn::create("Список", "/admin/tg-bot")->fetch();
$btn .= SuccessBtn::create("Редактировать", "/admin/tg-bot/update/" . $tg->id)->fetch();
$btn .= DangerBtn::create("Удалить", "/admin/tg-bot/delete/" . $tg->id)->fetch();
return $btn;
});
$table->rows([
'status' => (function ($data) {
return \app\modules\tgbot\models\Tgbot::getStatus()[$data];
})
]);
$table->create();
$table->render();