pratiksh404 /
adminetic
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Pratiksh\Adminetic\Models\Admin; |
||||
| 4 | |||||
| 5 | use Illuminate\Database\Eloquent\Model; |
||||
| 6 | use Illuminate\Support\Facades\Cache; |
||||
| 7 | use Spatie\Activitylog\LogOptions; |
||||
| 8 | use Spatie\Activitylog\Traits\LogsActivity; |
||||
| 9 | |||||
| 10 | class Setting extends Model |
||||
| 11 | { |
||||
| 12 | use LogsActivity; |
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 13 | |||||
| 14 | protected $guarded = []; |
||||
| 15 | |||||
| 16 | // Forget cache on updating or saving and deleting |
||||
| 17 | public static function boot() |
||||
| 18 | { |
||||
| 19 | parent::boot(); |
||||
| 20 | |||||
| 21 | static::saving(function () { |
||||
| 22 | self::cacheKey(); |
||||
| 23 | }); |
||||
| 24 | |||||
| 25 | static::deleting(function () { |
||||
| 26 | self::cacheKey(); |
||||
| 27 | }); |
||||
| 28 | } |
||||
| 29 | |||||
| 30 | // Cache Keys |
||||
| 31 | private static function cacheKey() |
||||
| 32 | { |
||||
| 33 | Cache::has('settings') ? Cache::forget('settings') : |
||||
| 34 | Cache::rememberForever('settings', function () { |
||||
| 35 | return Setting::all(); |
||||
| 36 | }); |
||||
| 37 | } |
||||
| 38 | |||||
| 39 | // Logs |
||||
| 40 | protected static $logName = 'setting'; |
||||
| 41 | |||||
| 42 | public function getActivitylogOptions(): LogOptions |
||||
| 43 | { |
||||
| 44 | return LogOptions::defaults(); |
||||
| 45 | } |
||||
| 46 | |||||
| 47 | // Casts |
||||
| 48 | public $casts = [ |
||||
| 49 | 'setting_custom' => 'array', |
||||
| 50 | 'setting_json' => 'array', |
||||
| 51 | ]; |
||||
| 52 | |||||
| 53 | // Appends |
||||
| 54 | public $appends = ['custom', 'value']; |
||||
| 55 | |||||
| 56 | // Mutators |
||||
| 57 | public function setSettingNameAttribute($value) |
||||
| 58 | { |
||||
| 59 | $this->attributes['setting_name'] = strtolower(str_replace(' ', '_', $value)); |
||||
| 60 | } |
||||
| 61 | |||||
| 62 | public function setSettingGroupAttribute($value) |
||||
| 63 | { |
||||
| 64 | $this->attributes['setting_group'] = strtolower(str_replace(' ', '_', $value)); |
||||
| 65 | } |
||||
| 66 | |||||
| 67 | // Accessors |
||||
| 68 | public function getSettingNameAttribute($value) |
||||
| 69 | { |
||||
| 70 | return ucwords(str_replace('_', ' ', $value)); |
||||
| 71 | } |
||||
| 72 | |||||
| 73 | public function getSettingGroupAttribute($value) |
||||
| 74 | { |
||||
| 75 | return ucwords(str_replace('_', ' ', $value)); |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | public function getSettingTypeAttribute($attribute) |
||||
| 79 | { |
||||
| 80 | return [ |
||||
| 81 | 1 => 'string', |
||||
| 82 | 2 => 'integer', |
||||
| 83 | 3 => 'text', |
||||
| 84 | 4 => 'rich text', |
||||
| 85 | 5 => 'switch', |
||||
| 86 | 6 => 'checkbox', |
||||
| 87 | 7 => 'select', |
||||
| 88 | 8 => 'multiple', |
||||
| 89 | 9 => 'tag', |
||||
| 90 | 10 => 'image', |
||||
| 91 | 11 => 'custom', |
||||
| 92 | ][$attribute]; |
||||
| 93 | } |
||||
| 94 | |||||
| 95 | public function getCustomAttribute() |
||||
| 96 | { |
||||
| 97 | return isset($this->setting_custom) ? json_decode($this->setting_custom) : null; |
||||
|
0 ignored issues
–
show
$this->setting_custom of type array is incompatible with the type string expected by parameter $json of json_decode().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 98 | } |
||||
| 99 | |||||
| 100 | public function getValueAttribute() |
||||
| 101 | { |
||||
| 102 | if ($this->getRawOriginal('setting_type') == 1 || $this->getRawOriginal('setting_type') == 10) { |
||||
| 103 | return $this->string_value; |
||||
| 104 | } elseif ($this->getRawOriginal('setting_type') == 2 || $this->getRawOriginal('setting_type') == 6 || $this->getRawOriginal('setting_type') == 7) { |
||||
| 105 | return $this->integer_value; |
||||
| 106 | } elseif ($this->getRawOriginal('setting_type') == 3 || $this->getRawOriginal('setting_type') == 4) { |
||||
| 107 | return $this->text_value; |
||||
| 108 | } elseif ($this->getRawOriginal('setting_type') == 5) { |
||||
| 109 | return $this->boolean_value; |
||||
| 110 | } elseif ($this->getRawOriginal('setting_type') == 8 || $this->getRawOriginal('setting_type') == 9) { |
||||
| 111 | return $this->setting_json; |
||||
| 112 | } else { |
||||
| 113 | return null; |
||||
| 114 | } |
||||
| 115 | } |
||||
| 116 | } |
||||
| 117 |