2023-08-23 16:32:19 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
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-11-02 17:10:50 -07:00
|
|
|
|
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
|
|
|
|
2023-12-06 15:05:58 -08:00
|
|
|
//we will need a bit to catch and store the name of the report.
|
|
|
|
//for now the blip above is creating the name, but can be confusing if multiple are made at once
|
|
|
|
|
2023-12-11 11:27:56 -08:00
|
|
|
public function checkmarkValue(string $property): string
|
2023-11-02 17:10:50 -07:00
|
|
|
{
|
|
|
|
// 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 '1';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the property's value if it exists
|
|
|
|
// and return the default value if not.
|
|
|
|
return $this->options[$property] ?? '0';
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:27:56 -08:00
|
|
|
public function radioValue(string $property, $value, $return)
|
2023-11-30 18:15:04 -08:00
|
|
|
{
|
|
|
|
// @todo: this method feels more like "radioShouldBeChecked" or something...
|
2023-12-11 11:27:56 -08:00
|
|
|
// @todo: improve the variable names...
|
2023-11-30 18:15:04 -08:00
|
|
|
|
2023-12-21 12:02:54 -08:00
|
|
|
if (array_has($this->options, $property) && $this->options[$property] === $value) {
|
|
|
|
return $return;
|
|
|
|
}
|
2023-12-06 15:05:58 -08:00
|
|
|
// this is currently throwing an error. $property is coming through as a string and it needs to be an array
|
2023-11-30 18:15:04 -08:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-12-21 13:03:43 -08:00
|
|
|
public function selectValue(string $property, string $model = null)
|
2023-11-30 16:57:21 -08:00
|
|
|
{
|
2023-12-21 13:11:44 -08:00
|
|
|
if (!isset($this->options[$property])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-12-21 13:03:43 -08:00
|
|
|
// If a model is provided then we should ensure we only return
|
|
|
|
// the value if the model still exists.
|
|
|
|
if ($model) {
|
|
|
|
$foundModel = $model::find($this->options[$property]);
|
|
|
|
|
|
|
|
return $foundModel ? $foundModel->id : null;
|
|
|
|
}
|
2023-11-30 16:57:21 -08:00
|
|
|
return $this->options[$property] ?? null;
|
|
|
|
}
|
|
|
|
|
2023-12-21 16:38:51 -08:00
|
|
|
public function selectValues(string $property, string $model = null): iterable
|
2023-11-30 16:57:21 -08:00
|
|
|
{
|
|
|
|
if (!isset($this->options[$property])) {
|
2023-12-21 16:38:51 -08:00
|
|
|
return [];
|
2023-11-30 16:57:21 -08:00
|
|
|
}
|
|
|
|
|
2023-12-21 14:27:31 -08:00
|
|
|
// @todo: I think this was added to support the null object pattern
|
2023-12-21 16:38:51 -08:00
|
|
|
// @todo: Check if this is still needed and if so, add a test for it (testParsingSelectValues()).
|
|
|
|
// if ($this->options[$property] === [null]) {
|
|
|
|
// return null;
|
|
|
|
// }
|
2023-11-30 16:57:21 -08:00
|
|
|
|
2023-12-21 12:02:48 -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) {
|
|
|
|
return $model::findMany($this->options[$property])->pluck('id');
|
|
|
|
}
|
|
|
|
|
2023-11-30 16:57:21 -08:00
|
|
|
return $this->options[$property];
|
|
|
|
}
|
|
|
|
|
2023-12-11 11:27:56 -08:00
|
|
|
public function textValue(string $property): string
|
2023-11-02 17:10:50 -07:00
|
|
|
{
|
|
|
|
// 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 '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the property's value if it exists
|
|
|
|
// and return the default value if not.
|
|
|
|
return $this->options[$property] ?? '';
|
|
|
|
}
|
2023-08-23 16:32:19 -07:00
|
|
|
}
|