2024-09-23 15:33:48 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace kernel\modules\option\service;
|
|
|
|
|
|
|
|
use kernel\FormModel;
|
|
|
|
use kernel\modules\option\models\Option;
|
|
|
|
|
|
|
|
class OptionService
|
|
|
|
{
|
|
|
|
|
|
|
|
public function create(FormModel $form_model): false|Option
|
|
|
|
{
|
|
|
|
$model = new Option();
|
|
|
|
$model->key = $form_model->getItem('key');
|
|
|
|
$model->value = $form_model->getItem('value');
|
|
|
|
$model->label = $form_model->getItem('label');
|
|
|
|
$model->status = $form_model->getItem('status');
|
|
|
|
if ($model->save()) {
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update(FormModel $form_model, Option $option): false|Option
|
|
|
|
{
|
|
|
|
$option->key = $form_model->getItem('key');
|
|
|
|
$option->value = $form_model->getItem('value');
|
|
|
|
$option->label = $form_model->getItem('label');
|
|
|
|
$option->status = $form_model->getItem('status');
|
|
|
|
if ($option->save()) {
|
|
|
|
return $option;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-11-25 15:17:41 +03:00
|
|
|
public static function createFromParams(string $key, string $value, string $label): false|Option
|
2024-09-23 17:03:42 +03:00
|
|
|
{
|
|
|
|
$model = new Option();
|
|
|
|
$model->key = $key;
|
|
|
|
$model->value = $value;
|
|
|
|
$model->label = $label;
|
|
|
|
|
|
|
|
if ($model->save()) {
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-09-24 17:22:09 +03:00
|
|
|
/**
|
|
|
|
* @param $key
|
|
|
|
* @return false|array|string
|
|
|
|
*/
|
|
|
|
public static function getItem($key): false|array|string
|
|
|
|
{
|
|
|
|
$item = Option::where("key", $key)->first();
|
|
|
|
if ($item){
|
|
|
|
return getConst($item->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-11-25 15:17:41 +03:00
|
|
|
public static function removeOptionByKey(string $key): bool
|
|
|
|
{
|
|
|
|
$option = Option::where("key", $key)->first();
|
|
|
|
if (!$option){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$option->delete();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-09-23 15:33:48 +03:00
|
|
|
// public function createOptionArr(): array
|
|
|
|
// {
|
|
|
|
// foreach (Option::all()->toArray() as $option) {
|
|
|
|
// $optionArr[$option['id']] = $option['key'];
|
|
|
|
// }
|
|
|
|
// if (!empty($optionArr)) {
|
|
|
|
// return $optionArr;
|
|
|
|
// }
|
|
|
|
// return [];
|
|
|
|
// }
|
|
|
|
|
|
|
|
}
|