diff --git a/src/Traits/ActivityModel.php b/src/Traits/ActivityModel.php index c8601a0..002d0f3 100644 --- a/src/Traits/ActivityModel.php +++ b/src/Traits/ActivityModel.php @@ -19,47 +19,46 @@ public static function bootActivityModel() { if (auth()->user()) { try { - $user_id = auth()->user()->id; - static::created(function ($model) use ($user_id) { - if ($model->activityDefault()['created']) { - $activity = $model->activities()->create($model->activityDefault()['created'] + ['user_id' => $user_id]); + static::created(function ($model) { + if (array_key_exists('created', $model->activityDefault())) { + $activity = $model->activities()->create($model->activityDefault()['created'] + ['user_id' => auth()->user()->id]); $model->sendNotification($activity); } }); - static::deleted(function ($model) use ($user_id) { - if ($model->activityDefault()['deleted']) { - $activity = $model->activities()->create($model->activityDefault()['deleted'] + ['user_id' => $user_id]); + static::deleted(function ($model) { + if (array_key_exists('deleted', $model->activityDefault())) { + $activity = $model->activities()->create($model->activityDefault()['deleted'] + ['user_id' => auth()->user()->id]); $model->sendNotification($activity); } }); - static::updated(function ($model) use ($user_id) { + static::updated(function ($model) { $changes = $model->getChanges(); if (count($changes) > 1 && $model->activityChanges()) { foreach ($changes as $key => $value) { if (array_key_exists($key, $model->activityChanges()) && array_key_exists('title_ar', $model->activityChanges()[$key])) { - $activity = $model->activities()->create($model->activityChanges()[$key] + ['user_id' => $user_id]); + $activity = $model->activities()->create($model->activityChanges()[$key] + ['user_id' => auth()->user()->id]); $model->sendNotification($activity); } else { if (array_key_exists($key, $model->activityChanges()) && array_key_exists($value, $model->activityChanges()[$key]) && array_key_exists('title_ar', $model->activityChanges()[$key][$value])) { - $activity = $model->activities()->create($model->activityChanges()[$key][$value] + ['user_id' => $user_id]); + $activity = $model->activities()->create($model->activityChanges()[$key][$value] + ['user_id' => auth()->user()->id]); $model->sendNotification($activity); } } } } else { - if ($model->activityDefault()['updated']) { - $activity = $model->activities()->create($model->activityDefault()['updated'] + ['user_id' => $user_id, 'data' => json_encode($model->getChanges())]); + if (array_key_exists('updated', $model->activityDefault())) { + $activity = $model->activities()->create($model->activityDefault()['updated'] + ['user_id' => auth()->user()->id]); $model->sendNotification($activity); } } }); if (in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses(new self))) { - static::restored(function ($model) use ($user_id) { - if ($model->activityDefault()['restored']) { - $activity = $model->activities()->create($model->activityDefault()['restored'] + ['user_id' => $user_id]); - $model->sendNotification($activity); + static::restored(function ($model) { + if (array_key_exists('restored', $model->activityDefault())) { + $activity = $model->activities()->create($model->activityDefault()['restored'] + ['user_id' => auth()->user()->id]); + $model->sendNotification($activity); } }); } @@ -97,7 +96,7 @@ public function sendNotification($activity) $option = $optionBuilder->build(); $notification = $notificationBuilder->build(); $data = $dataBuilder->build(); - $tokens = $this->ActivityFcmUsers(); + $tokens = $this->activityFcmTokens(); if (count($tokens) > 0) { FCM::sendTo($tokens, $option, $notification, $data);