|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Models; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\Builder; |
| 6 | +use Illuminate\Database\Eloquent\Factories\HasFactory; |
| 7 | +use Illuminate\Database\Eloquent\Model; |
| 8 | +use Illuminate\Database\Eloquent\Relations\BelongsTo; |
| 9 | +use Illuminate\Database\Eloquent\SoftDeletes; |
| 10 | +use Watson\Validating\ValidatingTrait; |
| 11 | + |
| 12 | +class ReportTemplate extends Model |
| 13 | +{ |
| 14 | + use HasFactory; |
| 15 | + use SoftDeletes; |
| 16 | + use ValidatingTrait; |
| 17 | + |
| 18 | + protected $casts = [ |
| 19 | + 'options' => 'array', |
| 20 | + ]; |
| 21 | + |
| 22 | + protected $fillable = [ |
| 23 | + 'created_by', |
| 24 | + 'name', |
| 25 | + 'options', |
| 26 | + ]; |
| 27 | + |
| 28 | + protected $rules = [ |
| 29 | + 'name' => [ |
| 30 | + 'required', |
| 31 | + 'string', |
| 32 | + ], |
| 33 | + 'options' => [ |
| 34 | + 'required', |
| 35 | + 'array', |
| 36 | + ], |
| 37 | + ]; |
| 38 | + |
| 39 | + protected static function booted() |
| 40 | + { |
| 41 | + // Scope to current user |
| 42 | + static::addGlobalScope('current_user', function (Builder $builder) { |
| 43 | + if (auth()->check()) { |
| 44 | + $builder->where('created_by', auth()->id()); |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + static::created(function (ReportTemplate $reportTemplate) { |
| 49 | + $logAction = new Actionlog([ |
| 50 | + 'item_type' => ReportTemplate::class, |
| 51 | + 'item_id' => $reportTemplate->id, |
| 52 | + 'created_by' => auth()->id(), |
| 53 | + ]); |
| 54 | + |
| 55 | + $logAction->logaction('create'); |
| 56 | + }); |
| 57 | + |
| 58 | + static::updated(function (ReportTemplate $reportTemplate) { |
| 59 | + $changed = []; |
| 60 | + |
| 61 | + foreach ($reportTemplate->getDirty() as $key => $value) { |
| 62 | + $changed[$key] = [ |
| 63 | + 'old' => $reportTemplate->getOriginal($key), |
| 64 | + 'new' => $reportTemplate->getAttribute($key), |
| 65 | + ]; |
| 66 | + } |
| 67 | + |
| 68 | + $logAction = new Actionlog(); |
| 69 | + $logAction->item_type = ReportTemplate::class; |
| 70 | + $logAction->item_id = $reportTemplate->id; |
| 71 | + $logAction->created_by = auth()->id(); |
| 72 | + $logAction->log_meta = json_encode($changed); |
| 73 | + $logAction->logaction('update'); |
| 74 | + }); |
| 75 | + |
| 76 | + static::deleted(function (ReportTemplate $reportTemplate) { |
| 77 | + $logAction = new Actionlog([ |
| 78 | + 'item_type' => ReportTemplate::class, |
| 79 | + 'item_id' => $reportTemplate->id, |
| 80 | + 'created_by' => auth()->id(), |
| 81 | + ]); |
| 82 | + |
| 83 | + $logAction->logaction('delete'); |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Establishes the report template -> creator relationship. |
| 89 | + * |
| 90 | + */ |
| 91 | + public function creator(): BelongsTo |
| 92 | + { |
| 93 | + return $this->belongsTo(User::class, 'created_by'); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Get the value of a checkbox field for the given field name. |
| 98 | + * |
| 99 | + * @param string $fieldName |
| 100 | + * @param string $fallbackValue The value to return if the report template is not saved yet. |
| 101 | + * |
| 102 | + */ |
| 103 | + public function checkmarkValue(string $fieldName, string $fallbackValue = '1'): string |
| 104 | + { |
| 105 | + // Assuming we're using the null object pattern, and an empty model |
| 106 | + // was passed to the view when showing the default report page, |
| 107 | + // return the fallback value so that checkboxes are checked by default. |
| 108 | + if (is_null($this->id)) { |
| 109 | + return $fallbackValue; |
| 110 | + } |
| 111 | + |
| 112 | + // If the model does exist then return the value of the field |
| 113 | + // or return 0 so the checkbox is unchecked. |
| 114 | + // Falling back to 0 here is because checkboxes are not sent |
| 115 | + // in the request when unchecked so they are not |
| 116 | + // actually saved in the model's options. |
| 117 | + return $this->options[$fieldName] ?? '0'; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Get the value of a radio field for the given field name. |
| 122 | + * |
| 123 | + * @param string $fieldName |
| 124 | + * @param string $value The value to check against. |
| 125 | + * @param bool $isDefault Whether the radio input being checked is the default. |
| 126 | + * |
| 127 | + */ |
| 128 | + public function radioValue(string $fieldName, string $value, bool $isDefault = false): bool |
| 129 | + { |
| 130 | + $fieldExists = array_has($this->options, $fieldName); |
| 131 | + |
| 132 | + // If the field doesn't exist but the radio input |
| 133 | + // being checked is the default then return true. |
| 134 | + if (!$fieldExists && $isDefault) { |
| 135 | + return true; |
| 136 | + } |
| 137 | + |
| 138 | + // If the field exists and matches what we're checking then return true. |
| 139 | + if ($fieldExists && $this->options[$fieldName] === $value) { |
| 140 | + return true; |
| 141 | + } |
| 142 | + |
| 143 | + // Otherwise return false. |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Get the value of a select field for the given field name. |
| 149 | + * |
| 150 | + * @param string $fieldName |
| 151 | + * @param string|null $model The Eloquent model to check against. |
| 152 | + * |
| 153 | + * @return mixed|null |
| 154 | + * |
| 155 | + */ |
| 156 | + public function selectValue(string $fieldName, string $model = null) |
| 157 | + { |
| 158 | + // If the field does not exist then return null. |
| 159 | + if (!isset($this->options[$fieldName])) { |
| 160 | + return null; |
| 161 | + } |
| 162 | + |
| 163 | + $value = $this->options[$fieldName]; |
| 164 | + |
| 165 | + // If the value was stored as an array, most likely |
| 166 | + // due to a previously being a multi-select, |
| 167 | + // then return the first value. |
| 168 | + if (is_array($value)) { |
| 169 | + $value = $value[0]; |
| 170 | + } |
| 171 | + |
| 172 | + // If a model is provided then we should ensure we only return |
| 173 | + // the value if the model still exists. |
| 174 | + // Note: It is possible $value is an id that no longer exists and this will return null. |
| 175 | + if ($model) { |
| 176 | + $foundModel = $model::find($value); |
| 177 | + |
| 178 | + return $foundModel ? $foundModel->id : null; |
| 179 | + } |
| 180 | + |
| 181 | + return $value; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Get the values of a multi-select field for the given field name. |
| 186 | + * |
| 187 | + * @param string $fieldName |
| 188 | + * @param string|null $model The Eloquent model to check against. |
| 189 | + * |
| 190 | + * @return iterable |
| 191 | + * |
| 192 | + */ |
| 193 | + public function selectValues(string $fieldName, string $model = null): iterable |
| 194 | + { |
| 195 | + // If the field does not exist then return an empty array. |
| 196 | + if (!isset($this->options[$fieldName])) { |
| 197 | + return []; |
| 198 | + } |
| 199 | + |
| 200 | + // If a model is provided then we should ensure we only return |
| 201 | + // the ids of models that exist and are not deleted. |
| 202 | + if ($model) { |
| 203 | + return $model::findMany($this->options[$fieldName])->pluck('id'); |
| 204 | + } |
| 205 | + |
| 206 | + // Wrap the value in an array if needed. This is to ensure |
| 207 | + // values previously stored as a single value, |
| 208 | + // most likely from a single select, are returned as an array. |
| 209 | + if (!is_array($this->options[$fieldName])) { |
| 210 | + return [$this->options[$fieldName]]; |
| 211 | + } |
| 212 | + |
| 213 | + return $this->options[$fieldName]; |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * Get the value of a text field for the given field name. |
| 218 | + * |
| 219 | + * @param string $fieldName |
| 220 | + * @param string|null $fallbackValue |
| 221 | + * |
| 222 | + * @return string |
| 223 | + */ |
| 224 | + public function textValue(string $fieldName, string|null $fallbackValue = ''): string |
| 225 | + { |
| 226 | + // Assuming we're using the null object pattern, |
| 227 | + // return the default value if the object is not saved yet. |
| 228 | + if (is_null($this->id)) { |
| 229 | + return (string) $fallbackValue; |
| 230 | + } |
| 231 | + |
| 232 | + // Return the field's value if it exists |
| 233 | + // and return the default value if not. |
| 234 | + return $this->options[$fieldName] ?? ''; |
| 235 | + } |
| 236 | + |
| 237 | + public function getDisplayNameAttribute() |
| 238 | + { |
| 239 | + return $this->name; |
| 240 | + } |
| 241 | +} |
0 commit comments