first
This commit is contained in:
163
kernel/app_modules/user_custom_fields/UserCustomFieldsModule.php
Normal file
163
kernel/app_modules/user_custom_fields/UserCustomFieldsModule.php
Normal file
@@ -0,0 +1,163 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\user_custom_fields;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use itguild\forms\builders\SelectBuilder;
|
||||
use itguild\forms\builders\TextInputBuilder;
|
||||
use kernel\app_modules\tag\models\Tag;
|
||||
use kernel\app_modules\tag\service\TagEntityService;
|
||||
use kernel\app_modules\user_custom_fields\models\CustomField;
|
||||
use kernel\app_modules\user_custom_fields\models\forms\CreateUserCustomValueForm;
|
||||
use kernel\app_modules\user_custom_fields\models\UserCustomValues;
|
||||
use kernel\app_modules\user_custom_fields\services\CustomFieldService;
|
||||
use kernel\app_modules\user_custom_fields\services\UserCustomValuesService;
|
||||
use kernel\EntityRelation;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\Module;
|
||||
use kernel\modules\menu\service\MenuService;
|
||||
use kernel\Request;
|
||||
use kernel\services\MigrationService;
|
||||
|
||||
class UserCustomFieldsModule 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_APP_MODULES}/user_custom_fields/migrations");
|
||||
|
||||
$this->menuService->createItem([
|
||||
"label" => "Доп. поля пользователей",
|
||||
"url" => "/admin/custom_field",
|
||||
"slug" => "custom_field",
|
||||
]);
|
||||
|
||||
$this->menuService->createItem([
|
||||
"label" => "Список",
|
||||
"url" => "/admin/custom_field",
|
||||
"slug" => "custom_field_list",
|
||||
"parent_slug" => "custom_field"
|
||||
]);
|
||||
|
||||
$this->menuService->createItem([
|
||||
"label" => "Значения",
|
||||
"url" => "/admin/custom_field/user_values",
|
||||
"slug" => "custom_field_user_values",
|
||||
"parent_slug" => "custom_field"
|
||||
]);
|
||||
|
||||
EntityRelation::addEntityRelation('user', 'user_custom_fields');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function deactivate(): void
|
||||
{
|
||||
$this->migrationService->rollbackAtPath("{KERNEL_APP_MODULES}/user_custom_fields/migrations");
|
||||
$this->menuService->removeItemBySlug("custom_field_user_values");
|
||||
$this->menuService->removeItemBySlug("custom_field_list");
|
||||
$this->menuService->removeItemBySlug("custom_field");
|
||||
|
||||
EntityRelation::removePropertyFromEntityRelations('user', 'user_custom_fields');
|
||||
}
|
||||
|
||||
public function formInputs(string $entity, Model $model = null): void
|
||||
{
|
||||
$fields = CustomFieldService::getCustomFields();
|
||||
|
||||
foreach ($fields as $field) {
|
||||
/* @var CustomField $field */
|
||||
if (isset($model->id)) {
|
||||
$value = UserCustomValuesService::getValueByFieldAndUser($field->id, $model->id);
|
||||
}
|
||||
|
||||
if ($field->type === "string"){
|
||||
$input = TextInputBuilder::build($field->slug, [
|
||||
'class' => 'form-control',
|
||||
'placeholder' => $field->label,
|
||||
'value' => $value->value ?? '',
|
||||
]);
|
||||
}
|
||||
else {
|
||||
$options = explode(", ", $field->field_options);
|
||||
$options = array_combine($options, $options);
|
||||
$input = SelectBuilder::build($field->slug, [
|
||||
'class' => 'form-control',
|
||||
'placeholder' => $field->label,
|
||||
'value' => $value->value ?? '',
|
||||
])->setOptions($options);
|
||||
}
|
||||
$input->setLabel($field->label);
|
||||
$input->create()->render();
|
||||
}
|
||||
}
|
||||
|
||||
public function saveInputs(string $entity, Model $model, Request $request): void
|
||||
{
|
||||
$service = new UserCustomValuesService();
|
||||
$form = new CreateUserCustomValueForm();
|
||||
$fields = CustomFieldService::getCustomFields();
|
||||
|
||||
foreach ($fields as $field){
|
||||
/* @var CustomField $field */
|
||||
if (isset($request->data[$field->slug])){
|
||||
UserCustomValuesService::deleteByUserAndField($model->id, $field->id);
|
||||
$form->load([
|
||||
'user_id' => $model->id,
|
||||
'custom_field_id' => $field->id,
|
||||
'value' => $request->data[$field->slug]
|
||||
]);
|
||||
$service->create($form);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getItem(string $entity, string $entity_id): string
|
||||
{
|
||||
$fields = UserCustomValuesService::getValuesByUserId($entity_id);
|
||||
$fieldsArr = [];
|
||||
foreach ($fields as $field){
|
||||
/* @var UserCustomValues $field */
|
||||
$fieldsArr[$field->customField->label] = $field->value;
|
||||
}
|
||||
$string = implode(', ', array_map(
|
||||
function ($key, $value) {
|
||||
return "$key: $value";
|
||||
},
|
||||
array_keys($fieldsArr),
|
||||
$fieldsArr
|
||||
));
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
public function getItems(string $entity, Model $model): string
|
||||
{
|
||||
$fields = UserCustomValuesService::getValuesByUserId($model->id);
|
||||
$fieldsArr = [];
|
||||
foreach ($fields as $field){
|
||||
/* @var UserCustomValues $field */
|
||||
$fieldsArr[$field->customField->label] = $field->value;
|
||||
}
|
||||
$string = implode(', ', array_map(
|
||||
function ($key, $value) {
|
||||
return "$key: $value";
|
||||
},
|
||||
array_keys($fieldsArr),
|
||||
$fieldsArr
|
||||
));
|
||||
|
||||
return $string;
|
||||
}
|
||||
}
|
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\user_custom_fields\controllers;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use kernel\AdminController;
|
||||
use kernel\app_modules\user_custom_fields\models\forms\CreateCustomFieldForm;
|
||||
use kernel\app_modules\user_custom_fields\models\CustomField;
|
||||
use kernel\app_modules\user_custom_fields\models\forms\CreateUserCustomValueForm;
|
||||
use kernel\app_modules\user_custom_fields\models\UserCustomValues;
|
||||
use kernel\app_modules\user_custom_fields\services\CustomFieldService;
|
||||
use kernel\app_modules\user_custom_fields\services\UserCustomValuesService;
|
||||
use kernel\Flash;
|
||||
use kernel\helpers\Debug;
|
||||
|
||||
class UserCustomFieldsController extends AdminController
|
||||
{
|
||||
private CustomFieldService $user_custom_fieldsService;
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/user_custom_fields/views/";
|
||||
$this->user_custom_fieldsService = new CustomFieldService();
|
||||
}
|
||||
|
||||
public function actionCreate(): void
|
||||
{
|
||||
$this->cgView->render("form.php");
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionAdd(): void
|
||||
{
|
||||
$user_custom_fieldsForm = new CreateCustomFieldForm();
|
||||
$user_custom_fieldsForm->load($_REQUEST);
|
||||
if ($user_custom_fieldsForm->validate()){
|
||||
$user_custom_fields = $this->user_custom_fieldsService->create($user_custom_fieldsForm);
|
||||
if ($user_custom_fields){
|
||||
$this->redirect("/admin/custom_field/view/" . $user_custom_fields->id);
|
||||
}
|
||||
}
|
||||
Flash::setMessage("error", $user_custom_fieldsForm->getErrorsStr());
|
||||
$this->redirect("/admin/custom_field/create");
|
||||
}
|
||||
|
||||
public function actionIndex($page_number = 1): void
|
||||
{
|
||||
$this->cgView->render("index.php", ['page_number' => $page_number]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionView($id): void
|
||||
{
|
||||
$user_custom_fields = CustomField::find($id);
|
||||
|
||||
if (!$user_custom_fields){
|
||||
throw new Exception(message: "The user_custom_fields not found");
|
||||
}
|
||||
$this->cgView->render("view.php", ['user_custom_fields' => $user_custom_fields]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionUpdate($id): void
|
||||
{
|
||||
$model = CustomField::find($id);
|
||||
if (!$model){
|
||||
throw new Exception(message: "The user_custom_fields not found");
|
||||
}
|
||||
|
||||
$this->cgView->render("form.php", ['model' => $model]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionEdit($id): void
|
||||
{
|
||||
$user_custom_fields = CustomField::find($id);
|
||||
if (!$user_custom_fields){
|
||||
throw new Exception(message: "The user_custom_fields not found");
|
||||
}
|
||||
$user_custom_fieldsForm = new CreateCustomFieldForm();
|
||||
$user_custom_fieldsService = new CustomFieldService();
|
||||
$user_custom_fieldsForm->load($_REQUEST);
|
||||
if ($user_custom_fieldsForm->validate()) {
|
||||
$user_custom_fields = $user_custom_fieldsService->update($user_custom_fieldsForm, $user_custom_fields);
|
||||
if ($user_custom_fields) {
|
||||
$this->redirect("/admin/custom_field/view/" . $user_custom_fields->id);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/custom_field/update/" . $id);
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionDelete($id): void
|
||||
{
|
||||
$user_custom_fields = CustomField::find($id)->first();
|
||||
$user_custom_fields->delete();
|
||||
$this->redirect("/admin/custom_field/");
|
||||
}
|
||||
|
||||
public function actionUserCustomValuesList($page_number = 1): void
|
||||
{
|
||||
$this->cgView->render("values_index.php", ['page_number' => $page_number]);
|
||||
}
|
||||
|
||||
public function actionCreateUserCustomValues(): void
|
||||
{
|
||||
$this->cgView->render("values_form.php");
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionAddUserCustomValues(): void
|
||||
{
|
||||
$service = new UserCustomValuesService();
|
||||
$form = new CreateUserCustomValueForm();
|
||||
$form->load($_REQUEST);
|
||||
|
||||
UserCustomValuesService::deleteByUserAndField($form->getItem('user_id'), $form->getItem('custom_field_id'));
|
||||
|
||||
if ($form->validate()){
|
||||
$model = $service->create($form);
|
||||
if ($model){
|
||||
$this->redirect("/admin/custom_field/user_values");
|
||||
}
|
||||
}
|
||||
Flash::setMessage("error", $form->getErrorsStr());
|
||||
$this->redirect("/admin/custom_field/user_values");
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionDeleteUserCustomValues($id): void
|
||||
{
|
||||
$user_custom_values = UserCustomValues::find($id)->first();
|
||||
$user_custom_values->delete();
|
||||
Flash::setMessage("success", "Запись удалена");
|
||||
$this->redirect("/admin/custom_field/user_values");
|
||||
}
|
||||
}
|
@@ -0,0 +1,48 @@
|
||||
<?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('custom_field', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('slug')->unique(); // Название поля (например, 'phone')
|
||||
$table->string('type')->default('string'); // Тип поля (string, integer, boolean и т.д.)
|
||||
$table->string('entity')->nullable(true); // Сущность (user, post и т.д.)
|
||||
$table->string('label'); // Человекочитаемое название
|
||||
$table->text('field_options'); // Человекочитаемое название
|
||||
$table->integer('status')->default(1);
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
\kernel\App::$db->schema->create('user_custom_values', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->unsignedBigInteger('user_id');
|
||||
$table->unsignedBigInteger('custom_field_id');
|
||||
$table->text('value')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
// $table->foreign('user_id')->references('id')->on('user')->onDelete('cascade');
|
||||
// $table->foreign('custom_field_id')->references('id')->on('custom_field')->onDelete('cascade');
|
||||
|
||||
// $table->unique(['user_id', 'custom_field_id']); // Уникальная пара пользователь-поле
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
\kernel\App::$db->schema->dropIfExists('user_custom_values');
|
||||
\kernel\App::$db->schema->dropIfExists('custom_field');
|
||||
}
|
||||
};
|
72
kernel/app_modules/user_custom_fields/models/CustomField.php
Normal file
72
kernel/app_modules/user_custom_fields/models/CustomField.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\user_custom_fields\models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
// Добавить @property
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $status
|
||||
* @property string $slug
|
||||
* @property string $label
|
||||
* @property string $type
|
||||
* @property string $entity
|
||||
* @property string $field_options
|
||||
*/
|
||||
class CustomField extends Model
|
||||
{
|
||||
const DISABLE_STATUS = 0;
|
||||
const ACTIVE_STATUS = 1;
|
||||
|
||||
const TYPE_STRING = 'string';
|
||||
|
||||
const TYPE_SELECT = 'select';
|
||||
|
||||
protected $table = 'custom_field';
|
||||
|
||||
protected $fillable = ['slug', 'label', 'type', 'entity', 'field_options', 'status']; // Заполнить массив. Пример: ['label', 'slug', 'status']
|
||||
|
||||
public static function labels(): array
|
||||
{
|
||||
// Заполнить массив
|
||||
// Пример: [
|
||||
// 'label' => 'Заголовок',
|
||||
// 'entity' => 'Сущность',
|
||||
// 'slug' => 'Slug',
|
||||
// 'status' => 'Статус',
|
||||
// ]
|
||||
|
||||
return [
|
||||
'slug' => 'Slug',
|
||||
'label' => 'Название',
|
||||
'type' => 'Тип',
|
||||
'entity' => 'Сущность',
|
||||
'field_options' => 'Опции поля',
|
||||
'status' => 'Статус',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public static function getStatus(): array
|
||||
{
|
||||
return [
|
||||
self::DISABLE_STATUS => "Не активный",
|
||||
self::ACTIVE_STATUS => "Активный",
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public static function getTypes(): array
|
||||
{
|
||||
return [
|
||||
self::TYPE_STRING => 'Текст',
|
||||
self::TYPE_SELECT => 'Список',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\user_custom_fields\models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use kernel\modules\user\models\User;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $user_id
|
||||
* @property int $custom_field_id
|
||||
* @property string $value
|
||||
* @property string $created_at
|
||||
* @property string $updated_at
|
||||
*/
|
||||
class UserCustomValues extends Model
|
||||
{
|
||||
|
||||
const DISABLE_STATUS = 0;
|
||||
const ACTIVE_STATUS = 1;
|
||||
|
||||
protected $table = 'user_custom_values';
|
||||
|
||||
protected $fillable = ['user_id', 'custom_field_id', 'value'];
|
||||
|
||||
public static function labels(): array
|
||||
{
|
||||
return [
|
||||
'user_id' => 'ID пользователя',
|
||||
'custom_field_id' => 'ID кастомного поля',
|
||||
'value' => 'Значение',
|
||||
'created_at' => 'Дата создания',
|
||||
'updated_at' => 'Дата обновления',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public static function getStatus(): array
|
||||
{
|
||||
return [
|
||||
self::DISABLE_STATUS => "Не активный",
|
||||
self::ACTIVE_STATUS => "Активный",
|
||||
];
|
||||
}
|
||||
|
||||
public function customField(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(CustomField::class);
|
||||
}
|
||||
|
||||
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\user_custom_fields\models\forms;
|
||||
|
||||
use kernel\FormModel;
|
||||
|
||||
class CreateCustomFieldForm extends FormModel
|
||||
{
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
// Заполнить массив правил
|
||||
// Пример:
|
||||
// return [
|
||||
// 'label' => 'required|min-str-len:5|max-str-len:30',
|
||||
// 'entity' => 'required',
|
||||
// 'slug' => '',
|
||||
// 'status' => ''
|
||||
// ];
|
||||
return [
|
||||
'slug' => 'required|min-str-len:3|max-str-len:30',
|
||||
'label' => 'required|min-str-len:3|max-str-len:50',
|
||||
'type' => 'required|min-str-len:3|max-str-len:30',
|
||||
'entity' => 'required|min-str-len:3|max-str-len:30',
|
||||
'field_options' => '',
|
||||
'status' => '',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\user_custom_fields\models\forms;
|
||||
|
||||
use kernel\FormModel;
|
||||
|
||||
class CreateUserCustomValueForm extends FormModel
|
||||
{
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
// Заполнить массив правил
|
||||
// Пример:
|
||||
// return [
|
||||
// 'label' => 'required|min-str-len:5|max-str-len:30',
|
||||
// 'entity' => 'required',
|
||||
// 'slug' => '',
|
||||
// 'status' => ''
|
||||
// ];
|
||||
return [
|
||||
'user_id' => 'required|integer|min:1',
|
||||
'custom_field_id' => 'required|integer|min:1',
|
||||
'value' => '',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@@ -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" => "custom_field"], function (CGRouteCollector $router) {
|
||||
App::$collector->get('/', [\app\modules\user_custom_fields\controllers\UserCustomFieldsController::class, 'actionIndex']);
|
||||
App::$collector->get('/page/{page_number}', [\app\modules\user_custom_fields\controllers\UserCustomFieldsController::class, 'actionIndex']);
|
||||
App::$collector->get('/create', [\app\modules\user_custom_fields\controllers\UserCustomFieldsController::class, 'actionCreate']);
|
||||
App::$collector->post("/", [\app\modules\user_custom_fields\controllers\UserCustomFieldsController::class, 'actionAdd']);
|
||||
App::$collector->get('/view/{id}', [\app\modules\user_custom_fields\controllers\UserCustomFieldsController::class, 'actionView']);
|
||||
App::$collector->any('/update/{id}', [\app\modules\user_custom_fields\controllers\UserCustomFieldsController::class, 'actionUpdate']);
|
||||
App::$collector->any("/edit/{id}", [\app\modules\user_custom_fields\controllers\UserCustomFieldsController::class, 'actionEdit']);
|
||||
App::$collector->get('/delete/{id}', [\app\modules\user_custom_fields\controllers\UserCustomFieldsController::class, 'actionDelete']);
|
||||
App::$collector->get('/user_values', [\app\modules\user_custom_fields\controllers\UserCustomFieldsController::class, 'actionUserCustomValuesList']);
|
||||
App::$collector->post('/user_values', [\app\modules\user_custom_fields\controllers\UserCustomFieldsController::class, 'actionAddUserCustomValues']);
|
||||
App::$collector->get('/user_values/create', [\app\modules\user_custom_fields\controllers\UserCustomFieldsController::class, 'actionCreateUserCustomValues']);
|
||||
App::$collector->get('/user_values/delete/{id}', [\app\modules\user_custom_fields\controllers\UserCustomFieldsController::class, 'actionDeleteUserCustomValues']);
|
||||
});
|
||||
});
|
||||
});
|
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\user_custom_fields\services;
|
||||
|
||||
use itguild\forms\builders\SelectBuilder;
|
||||
use itguild\forms\builders\TextInputBuilder;
|
||||
use kernel\app_modules\user_custom_fields\models\CustomField;
|
||||
use kernel\app_modules\user_custom_fields\models\forms\CreateCustomFieldForm;
|
||||
use kernel\FormModel;
|
||||
|
||||
class CustomFieldService
|
||||
{
|
||||
public function create(FormModel $form_model): false|CustomField
|
||||
{
|
||||
$model = new CustomField();
|
||||
// Пример заполнения:
|
||||
$model->slug = $form_model->getItem('slug');
|
||||
$model->label = $form_model->getItem('label');
|
||||
$model->type = $form_model->getItem('type');
|
||||
$model->entity = $form_model->getItem('entity');
|
||||
$model->field_options = $form_model->getItem('field_options');
|
||||
$model->status = $form_model->getItem('status');
|
||||
|
||||
if ($model->save()) {
|
||||
return $model;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function update(FormModel $form_model, CustomField $custom_field): false|CustomField
|
||||
{
|
||||
// Пример обновления:
|
||||
$custom_field->slug = $form_model->getItem('slug');
|
||||
$custom_field->label = $form_model->getItem('label');
|
||||
$custom_field->type = $form_model->getItem('type');
|
||||
$custom_field->entity = $form_model->getItem('entity');
|
||||
$custom_field->field_options = $form_model->getItem('field_options');
|
||||
$custom_field->status = $form_model->getItem('status');
|
||||
|
||||
if ($custom_field->save()) {
|
||||
return $custom_field;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function getCustomFields()
|
||||
{
|
||||
$model = CustomField::where(['entity' => 'user'])->where(['status' => CustomField::ACTIVE_STATUS])->get();
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
public static function getList(): array
|
||||
{
|
||||
return CustomField::select('id', 'label')->get()
|
||||
->pluck('label', 'id')
|
||||
->toArray();
|
||||
|
||||
}
|
||||
|
||||
public static function getCustomFieldHtml(CustomField $field, int $userId)
|
||||
{
|
||||
$value = UserCustomValuesService::getValueByFieldAndUser($field->id, $userId);
|
||||
|
||||
if ($field->type === "string"){
|
||||
$input = TextInputBuilder::build($field->slug, [
|
||||
'class' => 'form-control',
|
||||
'placeholder' => $field->label,
|
||||
'value' => $value->value ?? '',
|
||||
]);
|
||||
}
|
||||
else {
|
||||
$options = explode(", ", $field->field_options);
|
||||
$options = array_combine($options, $options);
|
||||
$input = SelectBuilder::build($field->slug, [
|
||||
'class' => 'form-control',
|
||||
'placeholder' => $field->label,
|
||||
'value' => $value->value ?? '',
|
||||
])->setOptions($options);
|
||||
}
|
||||
$input->setLabel($field->label);
|
||||
|
||||
return $input->create()->fetch();
|
||||
}
|
||||
|
||||
public static function getOrCreateBySlug(string $slug): CustomField
|
||||
{
|
||||
$model = CustomField::where('slug', $slug)->first();
|
||||
if (!$model) {
|
||||
$form = new CreateCustomFieldForm();
|
||||
$service = new self();
|
||||
$form->load([
|
||||
'slug' => $slug,
|
||||
'label' => $slug,
|
||||
'entity' => 'user',
|
||||
'type' => 'string',
|
||||
]);
|
||||
$model = $service->create($form);
|
||||
}
|
||||
|
||||
return $model;
|
||||
}
|
||||
}
|
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\user_custom_fields\services;
|
||||
|
||||
use kernel\app_modules\user_custom_fields\models\forms\CreateUserCustomValueForm;
|
||||
use kernel\app_modules\user_custom_fields\models\UserCustomValues;
|
||||
use kernel\FormModel;
|
||||
|
||||
class UserCustomValuesService
|
||||
{
|
||||
public function create(FormModel $form_model): false|UserCustomValues
|
||||
{
|
||||
$model = new UserCustomValues();
|
||||
$model->user_id = $form_model->getItem('user_id');
|
||||
$model->custom_field_id = $form_model->getItem('custom_field_id');
|
||||
$model->value = $form_model->getItem('value');
|
||||
|
||||
if ($model->save()) {
|
||||
return $model;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function update(FormModel $form_model, UserCustomValues $user_custom_value): false|UserCustomValues
|
||||
{
|
||||
$user_custom_value->user_id = $form_model->getItem('user_id');
|
||||
$user_custom_value->custom_field_id = $form_model->getItem('custom_field_id');
|
||||
$user_custom_value->value = $form_model->getItem('value');
|
||||
|
||||
if ($user_custom_value->save()) {
|
||||
return $user_custom_value;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function getValuesByUserId(int $user_id): \Illuminate\Database\Eloquent\Collection
|
||||
{
|
||||
return UserCustomValues::with(['customField'])->where(['user_id' => $user_id])->get();
|
||||
}
|
||||
|
||||
public static function getValueByFieldAndUser(int $custom_field_id, int $user_id): UserCustomValues|null
|
||||
{
|
||||
return UserCustomValues::where([
|
||||
'custom_field_id' => $custom_field_id,
|
||||
'user_id' => $user_id
|
||||
])->first();
|
||||
}
|
||||
|
||||
public static function deleteByUserAndField(int $user_id, int $custom_field_id): bool
|
||||
{
|
||||
$record = self::getValueByFieldAndUser($custom_field_id, $user_id);
|
||||
|
||||
if ($record) {
|
||||
return $record->delete();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function save(int $userId, string $slug, string $value): UserCustomValues
|
||||
{
|
||||
$customField = CustomFieldService::getOrCreateBySlug($slug);
|
||||
$model = UserCustomValues::where('user_id', $userId)->where('custom_field_id', $customField->id)->first();
|
||||
if (!$model) {
|
||||
$service = new self();
|
||||
$form = new CreateUserCustomValueForm();
|
||||
$form->load([
|
||||
'custom_field_id' => $customField->id,
|
||||
'user_id' => $userId,
|
||||
'value' => $value,
|
||||
]);
|
||||
$model = $service->create($form);
|
||||
}
|
||||
|
||||
return $model;
|
||||
}
|
||||
}
|
96
kernel/app_modules/user_custom_fields/views/form.php
Normal file
96
kernel/app_modules/user_custom_fields/views/form.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
/**
|
||||
* @var CustomField $model
|
||||
*/
|
||||
|
||||
use kernel\app_modules\user_custom_fields\models\CustomField;
|
||||
|
||||
$form = new \itguild\forms\ActiveForm();
|
||||
$form->beginForm(isset($model) ? "/admin/custom_field/edit/" . $model->id : "/admin/custom_field", 'multipart/form-data');
|
||||
|
||||
// Пример формы:
|
||||
|
||||
|
||||
$form->field(\itguild\forms\inputs\TextInput::class, 'slug', [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Slug',
|
||||
'value' => $model->slug ?? ''
|
||||
])
|
||||
->setLabel("Slug")
|
||||
->render();
|
||||
|
||||
$form->field(\itguild\forms\inputs\TextInput::class, 'label', [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Название поля',
|
||||
'value' => $model->label ?? ''
|
||||
])
|
||||
->setLabel("Название поля")
|
||||
->render();
|
||||
|
||||
$form->field(\itguild\forms\inputs\Select::class, 'type', [
|
||||
'class' => "form-control",
|
||||
'value' => $model->type ?? ''
|
||||
])
|
||||
->setLabel("Тип")
|
||||
->setOptions(CustomField::getTypes())
|
||||
->render();
|
||||
|
||||
$form->field(\itguild\forms\inputs\TextInput::class, 'entity', [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Сущность',
|
||||
'value' => $model->entity ?? ''
|
||||
])
|
||||
->setLabel("Сущность")
|
||||
->render();
|
||||
|
||||
$form->field(\itguild\forms\inputs\TextArea::class, 'field_options', [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Вариант 1, Вариант2, Вариант 3',
|
||||
'value' => $model->field_options ?? ''
|
||||
])
|
||||
->setLabel("Опции поля (через запятую)")
|
||||
->render();
|
||||
|
||||
$form->field(\itguild\forms\inputs\Select::class, 'status', [
|
||||
'class' => "form-control",
|
||||
'value' => $model->status ?? ''
|
||||
])
|
||||
->setLabel("Статус")
|
||||
->setOptions(CustomField::getStatus())
|
||||
->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/user_custom_fields/views/index.php
Normal file
78
kernel/app_modules/user_custom_fields/views/index.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Collection $user_custom_fields
|
||||
* @var int $page_number
|
||||
* @var \kernel\CgView $view
|
||||
*/
|
||||
|
||||
use kernel\app_modules\user_custom_fields\models\CustomField;
|
||||
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->setMeta([
|
||||
'description' => 'Список дополнительных полей системы'
|
||||
]);
|
||||
|
||||
//Для использования таблицы с моделью, необходимо создать таблицу в базе данных
|
||||
$table = new ListEloquentTable(new EloquentDataProvider(CustomField::class, [
|
||||
'currentPage' => $page_number,
|
||||
'perPage' => 8,
|
||||
'params' => ["class" => "table table-bordered", "border" => "2"],
|
||||
'baseUrl' => "/admin/custom_field"
|
||||
]));
|
||||
|
||||
|
||||
//$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([
|
||||
'slug' => [
|
||||
'filter' => [
|
||||
'class' => \Itguild\Tables\Filter\InputTextFilter::class,
|
||||
'value' => $get['title'] ?? ''
|
||||
]
|
||||
],
|
||||
]);
|
||||
|
||||
$table->beforePrint(function () {
|
||||
return IconBtnCreateWidget::create(['url' => '/admin/custom_field/create'])->run();
|
||||
});
|
||||
|
||||
$table->addAction(function($row) {
|
||||
return IconBtnViewWidget::create(['url' => '/admin/custom_field/view/' . $row['id']])->run();
|
||||
});
|
||||
$table->addAction(function($row) {
|
||||
return IconBtnEditWidget::create(['url' => '/admin/custom_field/update/' . $row['id']])->run();
|
||||
});
|
||||
$table->addAction(function($row) {
|
||||
return IconBtnDeleteWidget::create(['url' => '/admin/custom_field/delete/' . $row['id']])->run();
|
||||
});
|
||||
$table->create();
|
||||
$table->render();
|
72
kernel/app_modules/user_custom_fields/views/values_form.php
Normal file
72
kernel/app_modules/user_custom_fields/views/values_form.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/**
|
||||
* @var \kernel\app_modules\user_custom_fields\models\UserCustomValues $model
|
||||
*/
|
||||
|
||||
use kernel\app_modules\user_custom_fields\models\CustomField;
|
||||
|
||||
$form = new \itguild\forms\ActiveForm();
|
||||
$form->beginForm(isset($model) ? "/admin/custom_field/user_values/edit/" . $model->id : "/admin/custom_field/user_values", 'multipart/form-data');
|
||||
|
||||
// Пример формы:
|
||||
|
||||
$form->field(\itguild\forms\inputs\Select::class, 'user_id', [
|
||||
'class' => "form-control",
|
||||
'value' => $model->user_id ?? ''
|
||||
])
|
||||
->setLabel("Пользователь")
|
||||
->setOptions(\kernel\modules\user\service\UserService::getList())
|
||||
->render();
|
||||
|
||||
$form->field(\itguild\forms\inputs\Select::class, 'custom_field_id', [
|
||||
'class' => "form-control",
|
||||
'value' => $model->custom_field_id ?? ''
|
||||
])
|
||||
->setLabel("Поле")
|
||||
->setOptions(\kernel\app_modules\user_custom_fields\services\CustomFieldService::getList())
|
||||
->render();
|
||||
|
||||
$form->field(\itguild\forms\inputs\TextInput::class, 'value', [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Значение',
|
||||
'value' => $model->value ?? ''
|
||||
])
|
||||
->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();
|
85
kernel/app_modules/user_custom_fields/views/values_index.php
Normal file
85
kernel/app_modules/user_custom_fields/views/values_index.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Collection $user_custom_fields
|
||||
* @var int $page_number
|
||||
* @var \kernel\CgView $view
|
||||
*/
|
||||
|
||||
use kernel\app_modules\user_custom_fields\models\CustomField;
|
||||
use Itguild\EloquentTable\EloquentDataProvider;
|
||||
use Itguild\EloquentTable\ListEloquentTable;
|
||||
use kernel\modules\user\models\User;
|
||||
use kernel\widgets\IconBtn\IconBtnCreateWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnDeleteWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnEditWidget;
|
||||
use kernel\widgets\IconBtn\IconBtnViewWidget;
|
||||
|
||||
$view->setTitle("Список значений дополнительных полей");
|
||||
$view->setMeta([
|
||||
'description' => 'Список значений дополнительных полей системы'
|
||||
]);
|
||||
|
||||
//Для использования таблицы с моделью, необходимо создать таблицу в базе данных
|
||||
$table = new ListEloquentTable(new EloquentDataProvider(\kernel\app_modules\user_custom_fields\models\UserCustomValues::class, [
|
||||
'currentPage' => $page_number,
|
||||
'perPage' => 8,
|
||||
'params' => ["class" => "table table-bordered", "border" => "2"],
|
||||
'baseUrl' => "/admin/custom_field"
|
||||
]));
|
||||
|
||||
|
||||
//$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([
|
||||
'user_id' => [
|
||||
'value' => function ($data) {
|
||||
return User::find($data)->username;
|
||||
},
|
||||
'filter' => [
|
||||
'class' => \kernel\filters\BootstrapSelectFilter::class,
|
||||
'params' => [
|
||||
'options' => \kernel\modules\user\service\UserService::createUsernameArr(),
|
||||
'prompt' => 'Не выбрано'
|
||||
],
|
||||
'value' => $get['user_id'] ?? '',
|
||||
],
|
||||
],
|
||||
'custom_field_id' => [
|
||||
'value' => function ($data) {
|
||||
return CustomField::find($data)->label ?? '';
|
||||
},
|
||||
],
|
||||
]);
|
||||
|
||||
$table->beforePrint(function () {
|
||||
return IconBtnCreateWidget::create(['url' => '/admin/custom_field/user_values/create'])->run();
|
||||
});
|
||||
|
||||
$table->addAction(function($row) {
|
||||
return IconBtnDeleteWidget::create(['url' => '/admin/custom_field/user_values/delete/' . $row['id']])->run();
|
||||
});
|
||||
$table->create();
|
||||
$table->render();
|
25
kernel/app_modules/user_custom_fields/views/view.php
Normal file
25
kernel/app_modules/user_custom_fields/views/view.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Collection $user_custom_fields
|
||||
*/
|
||||
|
||||
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($user_custom_fields, [
|
||||
'params' => ["class" => "table table-bordered", "border" => "2"],
|
||||
'baseUrl' => "/admin/user_custom_fields",
|
||||
]));
|
||||
$table->beforePrint(function () use ($user_custom_fields) {
|
||||
$btn = IconBtnListWidget::create(['url' => '/admin/custom_field'])->run();
|
||||
$btn .= IconBtnEditWidget::create(['url' => '/admin/custom_field/update/' . $user_custom_fields->id])->run();
|
||||
$btn .= IconBtnDeleteWidget::create(['url' => '/admin/custom_field/delete/' . $user_custom_fields->id])->run();
|
||||
return $btn;
|
||||
});
|
||||
|
||||
$table->create();
|
||||
$table->render();
|
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\user_custom_fields\widgets;
|
||||
|
||||
use kernel\app_modules\user_custom_fields\models\CustomField;
|
||||
use kernel\app_modules\user_custom_fields\services\CustomFieldService;
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\Widget;
|
||||
|
||||
class UserCustomFieldsInputsWidget extends Widget
|
||||
{
|
||||
|
||||
public function run()
|
||||
{
|
||||
$fields = CustomFieldService::getCustomFields();
|
||||
|
||||
Debug::dd($fields);
|
||||
}
|
||||
|
||||
}
|
38
kernel/app_modules/user_stage/UserStageModule.php
Normal file
38
kernel/app_modules/user_stage/UserStageModule.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\user_stage;
|
||||
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\Module;
|
||||
use kernel\modules\menu\service\MenuService;
|
||||
use kernel\services\MigrationService;
|
||||
|
||||
class UserStageModule 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_APP_MODULES}/user_stage/migrations");
|
||||
|
||||
$this->menuService->createItem([
|
||||
"label" => "Этапы пользователя",
|
||||
"url" => "/admin/user_stage",
|
||||
"slug" => "user_stage",
|
||||
]);
|
||||
}
|
||||
|
||||
public function deactivate(): void
|
||||
{
|
||||
$this->migrationService->rollbackAtPath("{KERNEL_APP_MODULES}/user_stage/migrations");
|
||||
$this->menuService->removeItemBySlug("user_stage");
|
||||
}
|
||||
}
|
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\user_stage\controllers;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use kernel\AdminController;
|
||||
use kernel\App;
|
||||
use kernel\app_modules\user_stage\models\forms\CreateUserStageForm;
|
||||
use kernel\app_modules\user_stage\models\User;
|
||||
use kernel\app_modules\user_stage\models\UserStage;
|
||||
use kernel\app_modules\user_stage\notification_messages\UserNewStageNotification;
|
||||
use kernel\app_modules\user_stage\services\UserStageService;
|
||||
use kernel\Flash;
|
||||
|
||||
class UserStageController extends AdminController
|
||||
{
|
||||
private UserStageService $user_stageService;
|
||||
|
||||
protected function init(): void
|
||||
{
|
||||
parent::init();
|
||||
$this->cgView->viewPath = KERNEL_APP_MODULES_DIR . "/user_stage/views/";
|
||||
$this->user_stageService = new UserStageService();
|
||||
}
|
||||
|
||||
public function actionCreate(): void
|
||||
{
|
||||
$this->cgView->render("form.php");
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionAdd(): void
|
||||
{
|
||||
$user_stageForm = new CreateUserStageForm();
|
||||
$user_stageForm->load($_REQUEST);
|
||||
if ($user_stageForm->validate()) {
|
||||
$user_stage = $this->user_stageService->create($user_stageForm);
|
||||
if ($user_stage) {
|
||||
$this->redirect("/admin/user_stage/view/" . $user_stage->id);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/user_stage/create");
|
||||
}
|
||||
|
||||
public function actionIndex($page_number = 1): void
|
||||
{
|
||||
$this->cgView->render("index.php", ['page_number' => $page_number]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionView($id): void
|
||||
{
|
||||
$user_stage = UserStage::find($id);
|
||||
|
||||
if (!$user_stage) {
|
||||
throw new Exception(message: "The user_stage not found");
|
||||
}
|
||||
$this->cgView->render("view.php", ['user_stage' => $user_stage]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionUpdate($id): void
|
||||
{
|
||||
$model = UserStage::find($id);
|
||||
if (!$model) {
|
||||
throw new Exception(message: "The user_stage not found");
|
||||
}
|
||||
|
||||
$this->cgView->render("form.php", ['model' => $model]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function actionEdit($id): void
|
||||
{
|
||||
$user_stage = UserStage::find($id);
|
||||
if (!$user_stage) {
|
||||
throw new Exception(message: "The user_stage not found");
|
||||
}
|
||||
$user_stageForm = new CreateUserStageForm();
|
||||
$user_stageService = new UserStageService();
|
||||
$user_stageForm->load($_REQUEST);
|
||||
if ($user_stageForm->validate()) {
|
||||
$user_stage = $user_stageService->update($user_stageForm, $user_stage);
|
||||
if ($user_stage) {
|
||||
$this->redirect("/admin/user_stage/view/" . $user_stage->id);
|
||||
}
|
||||
}
|
||||
$this->redirect("/admin/user_stage/update/" . $id);
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionDelete($id): void
|
||||
{
|
||||
$user_stage = UserStage::find($id)->first();
|
||||
$user_stage->delete();
|
||||
$this->redirect("/admin/user_stage/");
|
||||
}
|
||||
|
||||
#[NoReturn] public function actionSetStage(int $stage_id, int $stage_status, int $user_id): void
|
||||
{
|
||||
$user = User::find($user_id);
|
||||
$stage = UserStage::find($stage_id);
|
||||
|
||||
if ($stage_status === 2) {
|
||||
$notification = new UserNewStageNotification($stage, $user);
|
||||
App::$notificationDispatcher->dispatch($notification, $user);
|
||||
$user->openStage($stage);
|
||||
}
|
||||
if ($stage_status === 3) {
|
||||
$user->completeStage($stage);
|
||||
}
|
||||
if ($stage_status === 1) {
|
||||
$user->closeStage($stage);
|
||||
}
|
||||
|
||||
Flash::setMessage('success', 'Этап изменен');
|
||||
$this->redirect("/admin/user/view/" . $user_id, 302);
|
||||
}
|
||||
}
|
@@ -0,0 +1,49 @@
|
||||
<?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('user_stage', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('slug')->nullable(false);
|
||||
$table->string('title')->nullable(false);
|
||||
$table->text('description')->nullable(false);
|
||||
$table->dateTime('start_date')->nullable();
|
||||
$table->dateTime('end_date')->nullable();
|
||||
$table->integer('position')->nullable(false)->default(1);
|
||||
$table->integer('status')->default(1);
|
||||
$table->text('user_fields')->nullable(true);
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
\kernel\App::$db->schema->create('user_stage_progress', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->integer('user_id')->nullable(false);
|
||||
$table->integer('user_stage_id')->nullable(false);
|
||||
$table->boolean('is_completed')->default(false);
|
||||
$table->boolean('is_closed')->default(true);
|
||||
$table->integer('status')->default(1);
|
||||
$table->timestamp('completed_at')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
\kernel\App::$db->schema->dropIfExists('user_stage_progress');
|
||||
\kernel\App::$db->schema->dropIfExists('user_stage');
|
||||
}
|
||||
};
|
81
kernel/app_modules/user_stage/models/User.php
Normal file
81
kernel/app_modules/user_stage/models/User.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\user_stage\models;
|
||||
|
||||
class User extends \kernel\modules\user\models\User
|
||||
{
|
||||
|
||||
public function stageProgress(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
{
|
||||
return $this->hasMany(UserStageProgress::class);
|
||||
}
|
||||
|
||||
public function stages()
|
||||
{
|
||||
return $this->belongsToMany(UserStage::class, 'user_stage_progress')
|
||||
->withPivot(['is_completed', 'completed_at', 'is_closed', 'status'])
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
// Метод для отметки этапа как завершенного
|
||||
public function completeStage(UserStage $stage): void
|
||||
{
|
||||
$this->stages()->updateExistingPivot($stage->id, [
|
||||
'is_completed' => true,
|
||||
'status' => 3,
|
||||
'completed_at' => date('Y-m-d H:i')
|
||||
]);
|
||||
}
|
||||
|
||||
public function openStage(UserStage $stage): void
|
||||
{
|
||||
$this->stages()->updateExistingPivot($stage->id, [
|
||||
'status' => 2,
|
||||
'is_closed' => 0,
|
||||
]);
|
||||
}
|
||||
|
||||
public function closeStage(UserStage $stage): void
|
||||
{
|
||||
$this->stages()->updateExistingPivot($stage->id, [
|
||||
'status' => 1,
|
||||
'is_closed' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
// Проверка, завершен ли этап
|
||||
public function hasCompletedStage(UserStage $stage)
|
||||
{
|
||||
return $this->stages()
|
||||
->where('stage_id', $stage->id)
|
||||
->where('is_completed', true)
|
||||
->exists();
|
||||
}
|
||||
|
||||
public function isClosed(UserStage $stage)
|
||||
{
|
||||
return $this->stages()
|
||||
->where('user_stage_id', $stage->id)
|
||||
->where('is_closed', true)
|
||||
->exists();
|
||||
}
|
||||
|
||||
public function hasStages(): bool
|
||||
{
|
||||
if ($this->stages()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Получение текущего этапа (первый незавершенный)
|
||||
public function currentStage()
|
||||
{
|
||||
return $this->stages()
|
||||
->where('is_completed', false)
|
||||
->orderBy('stage_id')
|
||||
->first();
|
||||
}
|
||||
|
||||
}
|
96
kernel/app_modules/user_stage/models/UserStage.php
Normal file
96
kernel/app_modules/user_stage/models/UserStage.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\user_stage\models;
|
||||
|
||||
use DateTime;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
// Добавить @property
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $status
|
||||
* @property int $position
|
||||
* @property string $title
|
||||
* @property string $slug
|
||||
* @property string $description
|
||||
* @property string $start_date
|
||||
* @property string $end_date
|
||||
* @property string $user_fields
|
||||
* @property string $dateStartFormatedToForm
|
||||
* @property string $dateEndFormatedToForm
|
||||
*/
|
||||
class UserStage extends Model
|
||||
{
|
||||
const DISABLE_STATUS = 0;
|
||||
const ACTIVE_STATUS = 1;
|
||||
|
||||
protected $table = 'user_stage';
|
||||
|
||||
protected $fillable = ['title', 'slug', 'description', 'start_date', 'end_date', 'position', 'status', 'user_fields']; // Заполнить массив. Пример: ['label', 'slug', 'status']
|
||||
|
||||
public static function labels(): array
|
||||
{
|
||||
// Заполнить массив
|
||||
// Пример: [
|
||||
// 'label' => 'Заголовок',
|
||||
// 'entity' => 'Сущность',
|
||||
// 'slug' => 'Slug',
|
||||
// 'status' => 'Статус',
|
||||
// ]
|
||||
|
||||
return [
|
||||
'title' => 'Заголовок',
|
||||
'slug' => 'Slug',
|
||||
'description' => 'Описание',
|
||||
'start_date' => 'Начало',
|
||||
'end_date' => 'Конец',
|
||||
'position' => 'Позиция',
|
||||
'status' => 'Статус',
|
||||
'user_fields' => 'Поля пользователя',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public static function getStatus(): array
|
||||
{
|
||||
return [
|
||||
self::DISABLE_STATUS => "Не активный",
|
||||
self::ACTIVE_STATUS => "Активный",
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getDateStartFormatedToFormAttribute(): string
|
||||
{
|
||||
$startDate = new DateTime($this->start_date);
|
||||
|
||||
return $startDate->format("Y-m-d");
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getDateEndFormatedToFormAttribute(): string
|
||||
{
|
||||
$endDate = new DateTime($this->end_date);
|
||||
|
||||
return $endDate->format("Y-m-d");
|
||||
}
|
||||
|
||||
public function userProgress(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
{
|
||||
return $this->hasMany(UserStageProgress::class);
|
||||
}
|
||||
|
||||
public function users()
|
||||
{
|
||||
return $this->belongsToMany(User::class, 'user_stage_progress')
|
||||
->withPivot(['is_completed', 'completed_at', 'status', 'is_closed'])
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
}
|
32
kernel/app_modules/user_stage/models/UserStageProgress.php
Normal file
32
kernel/app_modules/user_stage/models/UserStageProgress.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\user_stage\models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* @property int $user_id
|
||||
* @property int $user_stage_id
|
||||
* @property int $status
|
||||
* @property bool $is_completed
|
||||
* @property bool $is_closed
|
||||
* @property string $completed_at
|
||||
*/
|
||||
class UserStageProgress extends Model
|
||||
{
|
||||
|
||||
protected $table = 'user_stage_progress';
|
||||
|
||||
protected $fillable = ['user_id', 'user_stage_id', 'is_completed', 'is_closed', 'completed_at', 'status'];
|
||||
|
||||
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function stage(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
{
|
||||
return $this->belongsTo(UserStage::class);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\user_stage\models\forms;
|
||||
|
||||
use kernel\FormModel;
|
||||
|
||||
class CreateUserStageForm extends FormModel
|
||||
{
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
// Заполнить массив правил
|
||||
// Пример:
|
||||
// return [
|
||||
// 'label' => 'required|min-str-len:5|max-str-len:30',
|
||||
// 'entity' => 'required',
|
||||
// 'slug' => '',
|
||||
// 'status' => ''
|
||||
// ];
|
||||
return [
|
||||
'title' => 'required|min-str-len:5|max-str-len:30',
|
||||
'slug' => '',
|
||||
'description' => 'required|min-str-len:5',
|
||||
'start_date' => '',
|
||||
'end_date' => '',
|
||||
'position' => 'required|integer',
|
||||
'status' => 'integer',
|
||||
'user_fields' => '',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\user_stage\notification_messages;
|
||||
|
||||
use kernel\app_modules\user_stage\models\User;
|
||||
use kernel\app_modules\user_stage\models\UserStage;
|
||||
use kernel\CgView;
|
||||
use kernel\modules\notification\contracts\NotificationMessage;
|
||||
use kernel\modules\user\service\UserService;
|
||||
|
||||
class UserNewStageNotification extends NotificationMessage
|
||||
{
|
||||
protected UserStage $userStage;
|
||||
protected User $user;
|
||||
protected CgView $cgView;
|
||||
|
||||
public function __construct(UserStage $userStage, User $user)
|
||||
{
|
||||
$this->cgView = new CgView();
|
||||
$this->cgView->viewPath = ROOT_DIR . $_ENV['EMAIL_VIEWS_PATH'];
|
||||
$this->userStage = $userStage;
|
||||
$this->user = $user;
|
||||
$this->channels = ['email'];
|
||||
}
|
||||
|
||||
public function getMessage(): string
|
||||
{
|
||||
return $this->cgView->fetch('user_new_stage.php', [
|
||||
'user' => $this->user->username,
|
||||
'stage' => $this->userStage->title,
|
||||
'url' => $_ENV['APP_URL'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function getSubject(): string
|
||||
{
|
||||
return "Вам открыт новый этап {$this->userStage->title}";
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return array_merge(parent::toArray(), [
|
||||
'stage_id' => $this->userStage->id,
|
||||
'stage_title' => $this->userStage->title
|
||||
]);
|
||||
}
|
||||
}
|
21
kernel/app_modules/user_stage/routs/user_stage.php
Normal file
21
kernel/app_modules/user_stage/routs/user_stage.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?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" => "user_stage"], function (CGRouteCollector $router) {
|
||||
App::$collector->get('/', [\app\modules\user_stage\controllers\UserStageController::class, 'actionIndex']);
|
||||
App::$collector->get('/page/{page_number}', [\app\modules\user_stage\controllers\UserStageController::class, 'actionIndex']);
|
||||
App::$collector->get('/create', [\app\modules\user_stage\controllers\UserStageController::class, 'actionCreate']);
|
||||
App::$collector->post("/", [\app\modules\user_stage\controllers\UserStageController::class, 'actionAdd']);
|
||||
App::$collector->get('/view/{id}', [\app\modules\user_stage\controllers\UserStageController::class, 'actionView']);
|
||||
App::$collector->any('/update/{id}', [\app\modules\user_stage\controllers\UserStageController::class, 'actionUpdate']);
|
||||
App::$collector->any("/edit/{id}", [\app\modules\user_stage\controllers\UserStageController::class, 'actionEdit']);
|
||||
App::$collector->get('/delete/{id}', [\app\modules\user_stage\controllers\UserStageController::class, 'actionDelete']);
|
||||
App::$collector->get('/set_stage/{stage_id}/{stage_status}/{user_id}', [\app\modules\user_stage\controllers\UserStageController::class, 'actionSetStage']);
|
||||
});
|
||||
});
|
||||
});
|
88
kernel/app_modules/user_stage/services/UserStageService.php
Normal file
88
kernel/app_modules/user_stage/services/UserStageService.php
Normal file
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\app_modules\user_stage\services;
|
||||
|
||||
use kernel\helpers\Debug;
|
||||
use kernel\app_modules\user_stage\models\UserStage;
|
||||
use kernel\FormModel;
|
||||
use kernel\helpers\Slug;
|
||||
|
||||
class UserStageService
|
||||
{
|
||||
public function create(FormModel $form_model): false|UserStage
|
||||
{
|
||||
$model = new UserStage();
|
||||
// Пример заполнения:
|
||||
$model->description = $form_model->getItem('description');
|
||||
$model->start_date = $form_model->getItem('start_date');
|
||||
$model->end_date = $form_model->getItem('end_date');
|
||||
$model->title = $form_model->getItem('title');
|
||||
$model->position = $form_model->getItem('position');
|
||||
$model->status = $form_model->getItem('status');
|
||||
$model->user_fields = $form_model->getItem('user_fields');
|
||||
$model->slug = Slug::createSlug($form_model->getItem('title'), UserStage::class);
|
||||
|
||||
if ($model->save()) {
|
||||
return $model;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function update(FormModel $form_model, UserStage $user_stage): false|UserStage
|
||||
{
|
||||
// Пример обновления:
|
||||
$user_stage->description = $form_model->getItem('description');
|
||||
$user_stage->start_date = $form_model->getItem('start_date');
|
||||
$user_stage->end_date = $form_model->getItem('end_date');
|
||||
$user_stage->title = $form_model->getItem('title');
|
||||
$user_stage->position = $form_model->getItem('position');
|
||||
$user_stage->status = $form_model->getItem('status');
|
||||
$user_stage->user_fields = $form_model->getItem('user_fields');
|
||||
|
||||
if ($user_stage->save()) {
|
||||
return $user_stage;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function getStageClass(UserStage $stage): string
|
||||
{
|
||||
if ($stage->pivot->status === 1){
|
||||
return "grey_border";
|
||||
}
|
||||
elseif ($stage->pivot->status === 2){
|
||||
return "blue_border";
|
||||
}
|
||||
else {
|
||||
return "green_border";
|
||||
}
|
||||
}
|
||||
|
||||
public static function getStageStatusText(UserStage $stage): string
|
||||
{
|
||||
if ($stage->pivot->status === 1){
|
||||
return "Не доступен";
|
||||
}
|
||||
elseif ($stage->pivot->status === 2){
|
||||
return "Открыт";
|
||||
}
|
||||
else {
|
||||
return "Завершен";
|
||||
}
|
||||
}
|
||||
|
||||
public static function getStageStyle(UserStage $stage): string
|
||||
{
|
||||
if ($stage->pivot->status === 1){
|
||||
return "style='background-color: rgba(255, 193, 7, 0.1); color: #ffc107;'";
|
||||
}
|
||||
elseif ($stage->pivot->status === 2){
|
||||
return "style='background-color: rgba(13, 202, 240, 0.1); color: #0dcaf0;'";
|
||||
}
|
||||
else {
|
||||
return "style='background-color: rgba(25, 135, 84, 0.1); color: #198754;'";
|
||||
}
|
||||
}
|
||||
}
|
106
kernel/app_modules/user_stage/views/form.php
Normal file
106
kernel/app_modules/user_stage/views/form.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/**
|
||||
* @var UserStage $model
|
||||
*/
|
||||
|
||||
use kernel\app_modules\user_stage\models\UserStage;
|
||||
|
||||
$form = new \itguild\forms\ActiveForm();
|
||||
$form->beginForm(isset($model) ? "/admin/user_stage/edit/" . $model->id : "/admin/user_stage", 'multipart/form-data');
|
||||
|
||||
// Пример формы:
|
||||
|
||||
|
||||
$form->field(\itguild\forms\inputs\TextInput::class, 'title', [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Заголовок',
|
||||
'value' => $model->title ?? ''
|
||||
])
|
||||
->setLabel("Заголовок")
|
||||
->render();
|
||||
|
||||
$form->field(\itguild\forms\inputs\TextArea::class, 'description', [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Описание',
|
||||
'value' => $model->description ?? ''
|
||||
])
|
||||
->setLabel("Описание")
|
||||
->render();
|
||||
|
||||
$form->field(\itguild\forms\inputs\TextInput::class, 'position', [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Позиция',
|
||||
'type' => 'number',
|
||||
'value' => $model->position ?? ''
|
||||
])
|
||||
->setLabel("Позиция")
|
||||
->render();
|
||||
|
||||
$form->field(\itguild\forms\inputs\TextInput::class, 'start_date', [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Начало',
|
||||
'type' => 'date',
|
||||
'value' => $model->dateStartFormatedToForm ?? ''
|
||||
])
|
||||
->setLabel("Начало")
|
||||
->render();
|
||||
|
||||
$form->field(\itguild\forms\inputs\TextInput::class, 'end_date', [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Конец',
|
||||
'type' => 'date',
|
||||
'value' => $model->dateEndFormatedToForm ?? ''
|
||||
])
|
||||
->setLabel("Конец")
|
||||
->render();
|
||||
|
||||
$form->field(\itguild\forms\inputs\TextArea::class, 'user_fields', [
|
||||
'class' => "form-control",
|
||||
'placeholder' => 'Поля пользователя',
|
||||
'value' => $model->user_fields ?? ''
|
||||
])
|
||||
->setLabel("Поля пользователя")
|
||||
->render();
|
||||
|
||||
$form->field(\itguild\forms\inputs\Select::class, 'status', [
|
||||
'class' => "form-control",
|
||||
'value' => $model->status ?? ''
|
||||
])
|
||||
->setLabel("Статус")
|
||||
->setOptions(UserStage::getStatus())
|
||||
->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();
|
83
kernel/app_modules/user_stage/views/index.php
Normal file
83
kernel/app_modules/user_stage/views/index.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Collection $user_stage
|
||||
* @var int $page_number
|
||||
* @var \kernel\CgView $view
|
||||
*/
|
||||
|
||||
use kernel\app_modules\user_stage\models\UserStage;
|
||||
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("Список user_stage");
|
||||
$view->setMeta([
|
||||
'description' => 'Список user_stage системы'
|
||||
]);
|
||||
|
||||
//Для использования таблицы с моделью, необходимо создать таблицу в базе данных
|
||||
$table = new ListEloquentTable(new EloquentDataProvider(UserStage::class, [
|
||||
'currentPage' => $page_number,
|
||||
'perPage' => 8,
|
||||
'params' => ["class" => "table table-bordered", "border" => "2"],
|
||||
'baseUrl' => "/admin/user_stage"
|
||||
]));
|
||||
|
||||
|
||||
//$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'] ?? ''
|
||||
]
|
||||
],
|
||||
"status" => [
|
||||
"value" => function ($cell) {
|
||||
return UserStage::getStatus()[$cell];
|
||||
}
|
||||
]
|
||||
]);
|
||||
|
||||
$table->beforePrint(function () {
|
||||
return IconBtnCreateWidget::create(['url' => '/admin/user_stage/create'])->run();
|
||||
});
|
||||
|
||||
$table->addAction(function ($row) {
|
||||
return IconBtnViewWidget::create(['url' => '/admin/user_stage/view/' . $row['id']])->run();
|
||||
});
|
||||
$table->addAction(function ($row) {
|
||||
return IconBtnEditWidget::create(['url' => '/admin/user_stage/update/' . $row['id']])->run();
|
||||
});
|
||||
$table->addAction(function ($row) {
|
||||
return IconBtnDeleteWidget::create(['url' => '/admin/user_stage/delete/' . $row['id']])->run();
|
||||
});
|
||||
$table->create();
|
||||
$table->render();
|
31
kernel/app_modules/user_stage/views/view.php
Normal file
31
kernel/app_modules/user_stage/views/view.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\Eloquent\Collection $user_stage
|
||||
*/
|
||||
|
||||
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($user_stage, [
|
||||
'params' => ["class" => "table table-bordered", "border" => "2"],
|
||||
'baseUrl' => "/admin/user_stage",
|
||||
]));
|
||||
$table->beforePrint(function () use ($user_stage) {
|
||||
$btn = IconBtnListWidget::create(['url' => '/admin/user_stage'])->run();
|
||||
$btn .= IconBtnEditWidget::create(['url' => '/admin/user_stage/update/' . $user_stage->id])->run();
|
||||
$btn .= IconBtnDeleteWidget::create(['url' => '/admin/user_stage/delete/' . $user_stage->id])->run();
|
||||
return $btn;
|
||||
});
|
||||
|
||||
$table->rows([
|
||||
'status' => (function ($data) {
|
||||
return \kernel\app_modules\user_stage\models\UserStage::getStatus()[$data];
|
||||
})
|
||||
]);
|
||||
|
||||
$table->create();
|
||||
$table->render();
|
Reference in New Issue
Block a user