snipe-it/app/Models/ReportTemplate.php

148 lines
4.3 KiB
PHP
Raw Normal View History

2023-08-23 16:32:19 -07:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
2023-08-23 16:32:19 -07:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2024-01-17 16:25:28 -08:00
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2023-12-11 16:20:36 -08:00
use Watson\Validating\ValidatingTrait;
2023-08-23 16:32:19 -07:00
2023-12-18 12:55:48 -08:00
class ReportTemplate extends Model
2023-08-23 16:32:19 -07:00
{
use HasFactory;
2023-12-11 16:20:36 -08:00
use ValidatingTrait;
2023-08-24 11:32:37 -07:00
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'options' => 'array',
];
2023-08-28 16:26:31 -07:00
protected $fillable = [
'user_id',
'name',
'options',
];
2023-12-11 16:20:36 -08:00
protected $rules = [
2023-12-18 12:55:48 -08:00
'name' => 'required|unique:report_templates,name',
2023-12-11 16:20:36 -08:00
'options' => 'array',
];
2023-12-11 14:19:03 -08:00
protected static function booted()
{
// Scope to current user
static::addGlobalScope('current_user', function (Builder $builder) {
if (auth()->check()) {
$builder->where('user_id', auth()->id());
}
});
}
2024-01-17 16:25:28 -08:00
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
2024-01-18 11:58:34 -08:00
public function checkmarkValue(string $fieldName): string
{
2024-01-18 11:58:34 -08:00
// Assuming we're using the null object pattern, and an empty model
// was passed to the view when showing the default report page,
// return 1 so that checkboxes are checked by default.
if (is_null($this->id)) {
return '1';
}
2024-01-18 11:58:34 -08:00
// Return the field's value if it exists and return 0
// if not so that checkboxes are unchecked by default.
return $this->options[$fieldName] ?? '0';
}
2024-01-18 11:58:34 -08:00
public function radioValue(string $fieldName, string $value, bool $isDefault = false): bool
{
2024-01-18 11:58:34 -08:00
$fieldExists = array_has($this->options, $fieldName);
2023-12-21 18:07:46 -08:00
2024-01-18 11:58:34 -08:00
// If the field doesn't exist but the radio input
2024-01-17 17:32:35 -08:00
// being checked is the default then return true.
2024-01-18 11:58:34 -08:00
if (!$fieldExists && $isDefault) {
2023-12-21 18:07:46 -08:00
return true;
}
2024-01-18 11:58:34 -08:00
// If the field exists and matches what we're checking then return true.
if ($fieldExists && $this->options[$fieldName] === $value) {
2023-12-21 18:07:46 -08:00
return true;
2023-12-21 12:02:54 -08:00
}
2024-01-17 17:32:35 -08:00
// Otherwise return false.
2023-12-21 18:07:46 -08:00
return false;
}
2024-01-18 11:58:34 -08:00
public function selectValue(string $fieldName, string $model = null)
2023-11-30 16:57:21 -08:00
{
2024-01-18 11:58:34 -08:00
// If the field does not exist then return null.
if (!isset($this->options[$fieldName])) {
return null;
}
2024-01-18 11:58:34 -08:00
$value = $this->options[$fieldName];
2024-01-11 13:34:20 -08:00
2024-01-17 17:32:35 -08:00
// If the value was stored as an array, most likely
// due to a previously being a multi-select,
// then return the first value.
2024-01-11 13:34:20 -08:00
if (is_array($value)) {
$value = $value[0];
}
// If a model is provided then we should ensure we only return
// the value if the model still exists.
2024-01-17 17:32:35 -08:00
// Note: It is possible $value is an id that no longer exists and this will return null.
if ($model) {
2024-01-11 13:34:20 -08:00
$foundModel = $model::find($value);
return $foundModel ? $foundModel->id : null;
}
2024-01-11 13:34:20 -08:00
return $value;
2023-11-30 16:57:21 -08:00
}
2024-01-18 11:58:34 -08:00
public function selectValues(string $fieldName, string $model = null): iterable
2023-11-30 16:57:21 -08:00
{
2024-01-18 11:58:34 -08:00
// If the field does not exist then return an empty array.
if (!isset($this->options[$fieldName])) {
return [];
2023-11-30 16:57:21 -08:00
}
// If a model is provided then we should ensure we only return
// the ids of models that exist and are not deleted.
if ($model) {
2024-01-18 11:58:34 -08:00
return $model::findMany($this->options[$fieldName])->pluck('id');
}
2024-01-17 17:32:35 -08:00
// Wrap the value in an array if needed. This is to ensure
// values previously stored as a single value,
// most likely from a single select, are returned as an array.
2024-01-18 11:58:34 -08:00
if (!is_array($this->options[$fieldName])) {
return [$this->options[$fieldName]];
2023-12-21 17:02:44 -08:00
}
2024-01-18 11:58:34 -08:00
return $this->options[$fieldName];
2023-11-30 16:57:21 -08:00
}
2024-01-18 11:58:34 -08:00
public function textValue(string $fieldName): string
{
// Assuming we're using the null object pattern,
// return the default value if the object is not saved yet.
if (is_null($this->id)) {
return '';
}
2024-01-18 11:58:34 -08:00
// Return the field's value if it exists
// and return the default value if not.
2024-01-18 11:58:34 -08:00
return $this->options[$fieldName] ?? '';
}
2023-08-23 16:32:19 -07:00
}