| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Models; |
| 4 |
|
| 5 |
use FluentBoards\App\Services\Constant; |
| 6 |
|
| 7 |
class Activity extends Model |
| 8 |
{ |
| 9 |
protected $table = 'fbs_activities'; |
| 10 |
|
| 11 |
protected $guarded = ['id']; |
| 12 |
|
| 13 |
protected $fillable = [ |
| 14 |
'object_id', |
| 15 |
'object_type', |
| 16 |
'action', |
| 17 |
'column', |
| 18 |
'old_value', |
| 19 |
'new_value', |
| 20 |
'description', |
| 21 |
'settings', |
| 22 |
'created_by' |
| 23 |
]; |
| 24 |
|
| 25 |
public static function boot() |
| 26 |
{ |
| 27 |
parent::boot(); |
| 28 |
static::creating(function ($model) { |
| 29 |
$model->created_by = $model->created_by ?: get_current_user_id(); |
| 30 |
$model->created_at = current_time('mysql'); |
| 31 |
$model->updated_at = current_time('mysql'); |
| 32 |
}); |
| 33 |
} |
| 34 |
|
| 35 |
public function setSettingsAttribute($settings) |
| 36 |
{ |
| 37 |
$this->attributes['settings'] = \maybe_serialize($settings); |
| 38 |
} |
| 39 |
|
| 40 |
public function getSettingsAttribute($settings) |
| 41 |
{ |
| 42 |
return \maybe_unserialize($settings); |
| 43 |
} |
| 44 |
|
| 45 |
/** Read full custom-field message attributes from their LONGTEXT payload; preserve legacy rows. */ |
| 46 |
private function customFieldActivityAttribute($key, $fallback) |
| 47 |
{ |
| 48 |
if (empty($this->settings['custom_field_activity_payload'])) { |
| 49 |
return $fallback; |
| 50 |
} |
| 51 |
|
| 52 |
$payload = json_decode($this->attributes['description'] ?? '', true); |
| 53 |
return is_array($payload) && array_key_exists($key, $payload) ? $payload[$key] : $fallback; |
| 54 |
} |
| 55 |
|
| 56 |
/** Resolve the stored label while preserving display translations applied by the activity service. */ |
| 57 |
public function getColumnAttribute($value) |
| 58 |
{ |
| 59 |
// Payload rows store this identifier until translateActivities assigns a display label. |
| 60 |
if ($value !== Constant::ACTIVITY_CUSTOM_FIELD) { |
| 61 |
return $value; |
| 62 |
} |
| 63 |
|
| 64 |
return $this->customFieldActivityAttribute('column', $value); |
| 65 |
} |
| 66 |
|
| 67 |
/** Restore the complete previous value for all activity feeds and serialized responses. */ |
| 68 |
public function getOldValueAttribute($value) |
| 69 |
{ |
| 70 |
return $this->customFieldActivityAttribute('old_value', $value); |
| 71 |
} |
| 72 |
|
| 73 |
/** Restore the complete new value, including null removals and zero values. */ |
| 74 |
public function getNewValueAttribute($value) |
| 75 |
{ |
| 76 |
return $this->customFieldActivityAttribute('new_value', $value); |
| 77 |
} |
| 78 |
|
| 79 |
/** Keep storage details out of the user-facing activity description. */ |
| 80 |
public function getDescriptionAttribute($value) |
| 81 |
{ |
| 82 |
return $this->customFieldActivityAttribute('description', $value); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* One2One: Activity belongs to one Task |
| 87 |
* @return \FluentBoards\Framework\Database\Orm\Relations\BelongsTo |
| 88 |
*/ |
| 89 |
public function board() |
| 90 |
{ |
| 91 |
return $this->belongsTo(Board::class, 'object_id', 'id'); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* One2One: Activity belongs to one Task |
| 96 |
* @return \FluentBoards\Framework\Database\Orm\Relations\BelongsTo |
| 97 |
*/ |
| 98 |
public function task() |
| 99 |
{ |
| 100 |
return $this->belongsTo(Task::class, 'object_id', 'id'); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* One2One: Activity belongs to one User |
| 105 |
* @return \FluentBoards\Framework\Database\Orm\Relations\BelongsTo |
| 106 |
*/ |
| 107 |
public function user() |
| 108 |
{ |
| 109 |
return $this->belongsTo(User::class, 'created_by', 'ID'); |
| 110 |
} |
| 111 |
|
| 112 |
public function scopeType($query, $type) |
| 113 |
{ |
| 114 |
return $query->where('object_type', $type); |
| 115 |
} |
| 116 |
} |
| 117 |
|