first
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user