snipe-it/app/Models/ReportTemplate.php

242 lines
7.2 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;
2024-11-07 16:40:37 -08:00
use Illuminate\Database\Eloquent\SoftDeletes;
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;
2024-11-07 16:40:37 -08:00
use SoftDeletes;
2023-12-11 16:20:36 -08:00
use ValidatingTrait;
2023-08-24 11:32:37 -07:00
protected $casts = [
'options' => 'array',
];
2023-08-28 16:26:31 -07:00
protected $fillable = [
2024-10-23 16:40:03 -07:00
'created_by',
2023-08-28 16:26:31 -07:00
'name',
'options',
];
2023-12-11 16:20:36 -08:00
protected $rules = [
2024-10-28 14:37:52 -07:00
'name' => [
'required',
2024-11-07 16:40:54 -08:00
'string',
2024-10-28 14:37:52 -07:00
],
'options' => [
2024-10-28 14:48:19 -07:00
'required',
2024-10-28 14:37:52 -07:00
'array',
],
2023-12-11 16:20:36 -08:00
];
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()) {
2024-10-23 16:40:03 -07:00
$builder->where('created_by', auth()->id());
}
});
2024-10-30 12:22:59 -07:00
static::created(function (ReportTemplate $reportTemplate) {
$logAction = new Actionlog([
'item_type' => ReportTemplate::class,
'item_id' => $reportTemplate->id,
'created_by' => auth()->id(),
]);
$logAction->logaction('create');
2024-10-30 12:22:59 -07:00
});
static::updated(function (ReportTemplate $reportTemplate) {
$changed = [];
foreach ($reportTemplate->getDirty() as $key => $value) {
$changed[$key] = [
'old' => $reportTemplate->getOriginal($key),
'new' => $reportTemplate->getAttribute($key),
];
}
$logAction = new Actionlog();
$logAction->item_type = ReportTemplate::class;
$logAction->item_id = $reportTemplate->id;
$logAction->created_by = auth()->id();
$logAction->log_meta = json_encode($changed);
$logAction->logaction('update');
});
2024-10-30 14:05:27 -07:00
static::deleted(function (ReportTemplate $reportTemplate) {
$logAction = new Actionlog([
'item_type' => ReportTemplate::class,
'item_id' => $reportTemplate->id,
'created_by' => auth()->id(),
]);
$logAction->logaction('delete');
});
}
2024-01-18 15:42:52 -08:00
/**
* Establishes the report template -> creator relationship.
*
*/
2024-10-23 16:40:03 -07:00
public function creator(): BelongsTo
2024-01-17 16:25:28 -08:00
{
2024-10-23 16:40:03 -07:00
return $this->belongsTo(User::class, 'created_by');
2024-01-17 16:25:28 -08:00
}
2024-01-18 15:42:52 -08:00
/**
* Get the value of a checkbox field for the given field name.
*
* @param string $fieldName
* @param string $fallbackValue The value to return if the report template is not saved yet.
*
*/
public function checkmarkValue(string $fieldName, string $fallbackValue = '1'): 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 the fallback value so that checkboxes are checked by default.
if (is_null($this->id)) {
return $fallbackValue;
}
2024-11-12 10:01:32 -08:00
// If the model does exist then return the value of the field
// or return 0 so the checkbox is unchecked.
// Falling back to 0 here is because checkboxes are not sent
// in the request when unchecked so they are not
// actually saved in the model's options.
2024-01-18 11:58:34 -08:00
return $this->options[$fieldName] ?? '0';
}
2024-01-18 15:42:52 -08:00
/**
* Get the value of a radio field for the given field name.
*
* @param string $fieldName
* @param string $value The value to check against.
* @param bool $isDefault Whether the radio input being checked is the default.
*
*/
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 15:42:52 -08:00
/**
* Get the value of a select field for the given field name.
*
* @param string $fieldName
* @param string|null $model The Eloquent model to check against.
*
* @return mixed|null
*
*/
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 15:42:52 -08:00
/**
* Get the values of a multi-select field for the given field name.
*
* @param string $fieldName
* @param string|null $model The Eloquent model to check against.
*
* @return iterable
*
*/
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 15:42:52 -08:00
/**
* Get the value of a text field for the given field name.
*
* @param string $fieldName
* @param string|null $fallbackValue
2024-01-18 15:42:52 -08:00
*
* @return string
*/
public function textValue(string $fieldName, string|null $fallbackValue = ''): 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 (string) $fallbackValue;
}
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] ?? '';
}
2024-10-30 14:25:07 -07:00
public function getDisplayNameAttribute()
{
return $this->name;
}
2023-08-23 16:32:19 -07:00
}