first prod
This commit is contained in:
31
kernel/app_modules/view/ViewModule.php
Normal file
31
kernel/app_modules/view/ViewModule.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\view;
|
||||
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\Module;
|
||||
use kernel\modules\menu\service\MenuService;
|
||||
|
||||
class ViewModule extends Module
|
||||
{
|
||||
|
||||
public MenuService $menuService;
|
||||
public function __construct()
|
||||
{
|
||||
$this->menuService = new MenuService();
|
||||
}
|
||||
|
||||
public function init(): void
|
||||
{
|
||||
$this->menuService->createItem([
|
||||
"label" => "Просмотры",
|
||||
"url" => "/admin/view",
|
||||
"slug" => "view",
|
||||
]);
|
||||
}
|
||||
|
||||
public function deactivate(): void
|
||||
{
|
||||
$this->menuService->removeItemBySlug("view");
|
||||
}
|
||||
}
|
98
kernel/app_modules/view/controllers/ViewController.php
Normal file
98
kernel/app_modules/view/controllers/ViewController.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\view\controllers;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use kernel\AdminController;
|
||||
use kernel\app_modules\view\models\forms\CreateViewForm;
|
||||
use kernel\app_modules\view\models\View;
|
||||
use kernel\app_modules\view\services\ViewService;
|
||||
|
||||
class ViewController extends AdminController
|
||||
{
|
||||
private ViewService $viewService;
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/view/views/";
|
||||
$this->viewService = new ViewService();
|
||||
}
|
||||
|
||||
public function actionCreate(): void
|
||||
{
|
||||
$this->cgView->render("form.php");
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionAdd(): void
|
||||
{
|
||||
$viewForm = new CreateViewForm();
|
||||
$viewForm->load($_REQUEST);
|
||||
if ($viewForm->validate()){
|
||||
$view = $this->viewService->create($viewForm);
|
||||
if ($view){
|
||||
$this->redirect("/admin/view/view/" . $view->id);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/view/create");
|
||||
}
|
||||
|
||||
public function actionIndex($page_number = 1): void
|
||||
{
|
||||
$this->cgView->render("index.php", ['page_number' => $page_number]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionView($id): void
|
||||
{
|
||||
$view = View::find($id);
|
||||
|
||||
if (!$view){
|
||||
throw new Exception(message: "The view not found");
|
||||
}
|
||||
$this->cgView->render("view.php", ['view' => $view]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionUpdate($id): void
|
||||
{
|
||||
$model = View::find($id);
|
||||
if (!$model){
|
||||
throw new Exception(message: "The view not found");
|
||||
}
|
||||
|
||||
$this->cgView->render("form.php", ['model' => $model]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionEdit($id): void
|
||||
{
|
||||
$view = View::find($id);
|
||||
if (!$view){
|
||||
throw new Exception(message: "The view not found");
|
||||
}
|
||||
$viewForm = new CreateViewForm();
|
||||
$viewService = new ViewService();
|
||||
$viewForm->load($_REQUEST);
|
||||
if ($viewForm->validate()) {
|
||||
$view = $viewService->update($viewForm, $view);
|
||||
if ($view) {
|
||||
$this->redirect("/admin/view/view/" . $view->id);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/view/update/" . $id);
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionDelete($id): void
|
||||
{
|
||||
$view = View::find($id)->first();
|
||||
$view->delete();
|
||||
$this->redirect("/admin/view/");
|
||||
}
|
||||
}
|
23
kernel/app_modules/view/interfaces/Viewable.php
Normal file
23
kernel/app_modules/view/interfaces/Viewable.php
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\view\interfaces;
|
||||
|
||||
interface Viewable
|
||||
{
|
||||
|
||||
/**
|
||||
* Отношение просмотров
|
||||
*/
|
||||
public function views();
|
||||
|
||||
/**
|
||||
* Получить количество просмотров
|
||||
*/
|
||||
public function getViewsCountAttribute();
|
||||
|
||||
/**
|
||||
* Получить уникальное количество просмотров
|
||||
*/
|
||||
public function getUniqueViewsCountAttribute();
|
||||
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
<?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('view', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('user_id')->nullable(); // кто просмотрел
|
||||
$table->morphs('viewable'); // полиморфное отношение (viewable_id, viewable_type)
|
||||
$table->string('ip_address')->nullable(); // IP адрес
|
||||
$table->string('user_agent')->nullable(); // User agent
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
\kernel\App::$db->schema->dropIfExists('view');
|
||||
}
|
||||
};
|
93
kernel/app_modules/view/models/View.php
Normal file
93
kernel/app_modules/view/models/View.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\view\models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use kernel\app_modules\view\interfaces\Viewable;
|
||||
use kernel\modules\user\models\User;
|
||||
|
||||
// Добавить @property
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $status
|
||||
*/
|
||||
class View extends Model
|
||||
{
|
||||
const DISABLE_STATUS = 0;
|
||||
const ACTIVE_STATUS = 1;
|
||||
|
||||
protected $table = 'view';
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'ip_address',
|
||||
'user_agent',
|
||||
];
|
||||
|
||||
/**
|
||||
* Получить родительскую модель (пост, комментарий, файл и т.д.)
|
||||
*/
|
||||
public function viewable(): \Illuminate\Database\Eloquent\Relations\MorphTo
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
/**
|
||||
* Пользователь, который просмотрел
|
||||
*/
|
||||
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Увеличить счетчик просмотров для модели
|
||||
*/
|
||||
public static function track(Viewable $viewable)
|
||||
{
|
||||
// Проверяем, не просматривал ли уже пользователь этот элемент
|
||||
//$existingView = static::forViewable($viewable)->first();
|
||||
|
||||
// if (!$existingView) {
|
||||
// return static::create([
|
||||
// 'user_id' => null,
|
||||
// 'viewable_id' => $viewable->id,
|
||||
// 'viewable_type' => get_class($viewable),
|
||||
// ]);
|
||||
// }
|
||||
|
||||
return static::create([
|
||||
'user_id' => null,
|
||||
'viewable_id' => $viewable->id,
|
||||
'viewable_type' => get_class($viewable),
|
||||
]);
|
||||
|
||||
//return $existingView;
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope для поиска просмотров определенной модели
|
||||
*/
|
||||
public function scopeForViewable($query, Viewable $viewable)
|
||||
{
|
||||
return $query->where('viewable_id', $viewable->id)
|
||||
->where('viewable_type', get_class($viewable))
|
||||
->when(auth()->check(), function ($q) {
|
||||
$q->where('user_id', auth()->id());
|
||||
}, function ($q) {
|
||||
$q->where('ip_address', request()->ip());
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public static function getStatus(): array
|
||||
{
|
||||
return [
|
||||
self::DISABLE_STATUS => "Не активный",
|
||||
self::ACTIVE_STATUS => "Активный",
|
||||
];
|
||||
}
|
||||
|
||||
}
|
25
kernel/app_modules/view/models/forms/CreateViewForm.php
Normal file
25
kernel/app_modules/view/models/forms/CreateViewForm.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\view\models\forms;
|
||||
|
||||
use kernel\FormModel;
|
||||
|
||||
class CreateViewForm extends FormModel
|
||||
{
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
// Заполнить массив правил
|
||||
// Пример:
|
||||
// return [
|
||||
// 'label' => 'required|min-str-len:5|max-str-len:30',
|
||||
// 'entity' => 'required',
|
||||
// 'slug' => '',
|
||||
// 'status' => ''
|
||||
// ];
|
||||
return [
|
||||
|
||||
];
|
||||
}
|
||||
|
||||
}
|
20
kernel/app_modules/view/routs/view.php
Normal file
20
kernel/app_modules/view/routs/view.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" => "view"], function (CGRouteCollector $router) {
|
||||
App::$collector->get('/', [\app\modules\view\controllers\ViewController::class, 'actionIndex']);
|
||||
App::$collector->get('/page/{page_number}', [\app\modules\view\controllers\ViewController::class, 'actionIndex']);
|
||||
App::$collector->get('/create', [\app\modules\view\controllers\ViewController::class, 'actionCreate']);
|
||||
App::$collector->post("/", [\app\modules\view\controllers\ViewController::class, 'actionAdd']);
|
||||
App::$collector->get('/view/{id}', [\app\modules\view\controllers\ViewController::class, 'actionView']);
|
||||
App::$collector->any('/update/{id}', [\app\modules\view\controllers\ViewController::class, 'actionUpdate']);
|
||||
App::$collector->any("/edit/{id}", [\app\modules\view\controllers\ViewController::class, 'actionEdit']);
|
||||
App::$collector->get('/delete/{id}', [\app\modules\view\controllers\ViewController::class, 'actionDelete']);
|
||||
});
|
||||
});
|
||||
});
|
39
kernel/app_modules/view/services/ViewService.php
Normal file
39
kernel/app_modules/view/services/ViewService.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\view\services;
|
||||
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\app_modules\view\models\View;
|
||||
use kernel\FormModel;
|
||||
|
||||
class ViewService
|
||||
{
|
||||
public function create(FormModel $form_model): false|View
|
||||
{
|
||||
$model = new View();
|
||||
// Пример заполнения:
|
||||
// $model->content = $form_model->getItem('content');
|
||||
// $model->user_id = $form_model->getItem('user_id');
|
||||
// $model->title = $form_model->getItem('title');
|
||||
// $model->slug = Slug::createSlug($form_model->getItem('title'), View::class); // Генерация уникального slug
|
||||
|
||||
if ($model->save()){
|
||||
return $model;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function update(FormModel $form_model, View $view): false|View
|
||||
{
|
||||
// Пример обновления:
|
||||
// $view->content = $form_model->getItem('content');
|
||||
// $view->user_id = $form_model->getItem('user_id');
|
||||
|
||||
if ($view->save()){
|
||||
return $view;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
55
kernel/app_modules/view/views/form.php
Normal file
55
kernel/app_modules/view/views/form.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
/**
|
||||
* @var View $model
|
||||
*/
|
||||
|
||||
use kernel\app_modules\view\models\View;
|
||||
|
||||
$form = new \itguild\forms\ActiveForm();
|
||||
$form->beginForm(isset($model) ? "/admin/view/edit/" . $model->id : "/admin/view", '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\Select::class, name: "user_id", params: [
|
||||
'class' => "form-control",
|
||||
'value' => $model->user_id ?? ''
|
||||
])
|
||||
->setLabel("Пользователи")
|
||||
->setOptions(\kernel\modules\user\service\UserService::createUsernameArr())
|
||||
->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();
|
78
kernel/app_modules/view/views/index.php
Normal file
78
kernel/app_modules/view/views/index.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Collection $view
|
||||
* @var int $page_number
|
||||
* @var \kernel\CgView $view
|
||||
*/
|
||||
|
||||
use kernel\app_modules\view\models\View;
|
||||
use Itguild\EloquentTable\EloquentDataProvider;
|
||||
use Itguild\EloquentTable\ListEloquentTable;
|
||||
use kernel\widgets\IconBtn\IconBtnCreateWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnDeleteWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnEditWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnViewWidget;
|
||||
|
||||
$view->setTitle("Список view");
|
||||
$view->setMeta([
|
||||
'description' => 'Список view системы'
|
||||
]);
|
||||
|
||||
//Для использования таблицы с моделью, необходимо создать таблицу в базе данных
|
||||
//$table = new ListEloquentTable(new EloquentDataProvider(View::class, [
|
||||
// 'currentPage' => $page_number,
|
||||
// 'perPage' => 8,
|
||||
// 'params' => ["class" => "table table-bordered", "border" => "2"],
|
||||
// 'baseUrl' => "/admin/view"
|
||||
//]));
|
||||
|
||||
|
||||
$table = new \Itguild\Tables\ListJsonTable(json_encode(
|
||||
[
|
||||
'meta' => [
|
||||
'total' => 0,
|
||||
'totalWithFilters' => 0,
|
||||
'columns' => [
|
||||
'title',
|
||||
'slug',
|
||||
'status',
|
||||
],
|
||||
'perPage' => 5,
|
||||
'currentPage' => 1,
|
||||
'baseUrl' => '/admin/some',
|
||||
'params' => [
|
||||
'class' => 'table table-bordered',
|
||||
'border' => 2
|
||||
]
|
||||
],
|
||||
'filters' => [],
|
||||
'data' => [],
|
||||
]
|
||||
));
|
||||
|
||||
// Пример фильтра
|
||||
$table->columns([
|
||||
'title' => [
|
||||
'filter' => [
|
||||
'class' => \Itguild\Tables\Filter\InputTextFilter::class,
|
||||
'value' => $get['title'] ?? ''
|
||||
]
|
||||
],
|
||||
]);
|
||||
|
||||
$table->beforePrint(function () {
|
||||
return IconBtnCreateWidget::create(['url' => '/admin/view/create'])->run();
|
||||
});
|
||||
|
||||
$table->addAction(function($row) {
|
||||
return IconBtnViewWidget::create(['url' => '/admin/view/view/' . $row['id']])->run();
|
||||
});
|
||||
$table->addAction(function($row) {
|
||||
return IconBtnEditWidget::create(['url' => '/admin/view/update/' . $row['id']])->run();
|
||||
});
|
||||
$table->addAction(function($row) {
|
||||
return IconBtnDeleteWidget::create(['url' => '/admin/view/delete/' . $row['id']])->run();
|
||||
});
|
||||
$table->create();
|
||||
$table->render();
|
25
kernel/app_modules/view/views/view.php
Normal file
25
kernel/app_modules/view/views/view.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Collection $view
|
||||
*/
|
||||
|
||||
use Itguild\EloquentTable\ViewEloquentTable;
|
||||
use Itguild\EloquentTable\ViewJsonTableEloquentModel;
|
||||
use kernel\widgets\IconBtn\IconBtnDeleteWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnEditWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnListWidget;
|
||||
|
||||
$table = new ViewEloquentTable(new ViewJsonTableEloquentModel($view, [
|
||||
'params' => ["class" => "table table-bordered", "border" => "2"],
|
||||
'baseUrl' => "/admin/view",
|
||||
]));
|
||||
$table->beforePrint(function () use ($view) {
|
||||
$btn = IconBtnListWidget::create(['url' => '/admin/view'])->run();
|
||||
$btn .= IconBtnEditWidget::create(['url' => '/admin/view/update/' . $view->id])->run();
|
||||
$btn .= IconBtnDeleteWidget::create(['url' => '/admin/view/delete/' . $view->id])->run();
|
||||
return $btn;
|
||||
});
|
||||
|
||||
$table->create();
|
||||
$table->render();
|
Reference in New Issue
Block a user