Files
safekat/ci4/app/Models/Configuracion/ConfigVariableModel.php
2025-07-25 13:41:40 +02:00

96 lines
2.5 KiB
PHP
Executable File

<?php
namespace App\Models\Configuracion;
use CodeIgniter\Model;
class ConfigVariableModel extends Model
{
protected $table = 'config_variables_app';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = [
"name",
"value",
"description"
];
protected bool $allowEmptyInserts = false;
protected bool $updateOnlyChanged = true;
protected array $casts = [];
protected array $castHandlers = [];
// Dates
protected $useTimestamps = false;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
// Validation
protected $validationRules = [];
protected $validationMessages = [];
protected $skipValidation = false;
protected $cleanValidationRules = true;
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = [];
protected $afterInsert = [];
protected $beforeUpdate = [];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
public function getVariable($name)
{
$builder = $this->db
->table($this->table . " t1")
->where('name', $name);
return $builder->get()->getFirstRow();
}
/**
* Devuelve solo el valor de la variable por nombre
*/
public function getValue(string $name): ?string
{
$row = $this->getVariable($name);
return $row ? $row->value : null;
}
/**
* Devuelve el valor decodificado (JSON) si aplica
*/
public function getDecodedValue(string $name): ?array
{
$value = $this->getValue($name);
$decoded = json_decode($value, true);
return (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) ? $decoded : null;
}
/**
* Devuelve las opciones disponibles de cabezadas (como array clave => langKey)
*/
public function getCabezadasDisponibles(): array
{
return $this->getDecodedValue('cabezadas_disponibles') ?? [];
}
/**
* Devuelve la cabezada por defecto, o 'WHI' si no está definida
*/
public function getCabezadaDefault(): string
{
return $this->getValue('cabezada_default') ?? 'WHI';
}
}