| 1 |
<?php |
| 2 |
namespace FluentSupport\App\Models\Traits; |
| 3 |
|
| 4 |
use Exception; |
| 5 |
use FluentSupport\App\Services\Helper; |
| 6 |
use FluentSupport\Framework\Support\Arr; |
| 7 |
|
| 8 |
trait ActivityTrait |
| 9 |
{ |
| 10 |
// Get All Activities |
| 11 |
public function getActivities ( $data ) |
| 12 |
{ |
| 13 |
$agentId = intval( Arr::get($data, 'filters.agent_id') ); |
| 14 |
|
| 15 |
$activitiesQuery = static::with([ |
| 16 |
'person' => function ($query) { |
| 17 |
$query->select(['first_name', 'person_type', 'email', 'last_name', 'id', 'avatar']); |
| 18 |
} |
| 19 |
])->latest('id'); |
| 20 |
|
| 21 |
$from = sanitize_text_field( Arr::get( $data, 'from', '' ) ); |
| 22 |
$to = sanitize_text_field( Arr::get( $data, 'to', '') ); |
| 23 |
|
| 24 |
if ( $from != $to ) { |
| 25 |
$from = $from . ' ' . '00:00:00'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 26 |
$to = $to . ' ' . '23:59:59'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 27 |
} |
| 28 |
|
| 29 |
if ( ( !empty($from) && !empty($to) ) && $from == $to ) { |
| 30 |
$activitiesQuery->whereDate('created_at', '=', $from); |
| 31 |
} elseif (!empty($from) && !empty($to)) { |
| 32 |
$activitiesQuery->whereBetween('created_at', [ $from, $to ]); |
| 33 |
} |
| 34 |
|
| 35 |
if ($agentId) { |
| 36 |
$activitiesQuery->where('person_id', $agentId); |
| 37 |
} |
| 38 |
|
| 39 |
$activities = $activitiesQuery->paginate(); |
| 40 |
|
| 41 |
foreach ($activities as $activity) { |
| 42 |
if($activity->person) { |
| 43 |
$activity->person->makeHidden(['email']); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
if (!$activities) { |
| 48 |
throw new \Exception('No activities found'); |
| 49 |
} |
| 50 |
|
| 51 |
$settings = $this->getSettings(); |
| 52 |
|
| 53 |
return [ |
| 54 |
'activities' => $activities, |
| 55 |
'settings' => $settings['activity_settings'] |
| 56 |
]; |
| 57 |
} |
| 58 |
|
| 59 |
// Update Activity Settings |
| 60 |
public function updateSettings ($settings) |
| 61 |
{ |
| 62 |
$defaults = [ |
| 63 |
'delete_days' => 14, |
| 64 |
'disable_logs' => 'no' |
| 65 |
]; |
| 66 |
$settings = wp_parse_args($settings, $defaults); |
| 67 |
$settings['delete_days'] = (int)$settings['delete_days']; |
| 68 |
|
| 69 |
Helper::updateOption('_activity_settings', $settings); |
| 70 |
|
| 71 |
return [ |
| 72 |
'message' => __('Activity settings has been updated', 'fluent-support') |
| 73 |
]; |
| 74 |
} |
| 75 |
|
| 76 |
// Get Activity Settings |
| 77 |
public function getSettings() |
| 78 |
{ |
| 79 |
$settings = Helper::getOption('_activity_settings', []); |
| 80 |
|
| 81 |
$defaults = [ |
| 82 |
'delete_days' => 14, |
| 83 |
'disable_logs' => 'no' |
| 84 |
]; |
| 85 |
|
| 86 |
$settings = wp_parse_args($settings, $defaults); |
| 87 |
|
| 88 |
if (! $settings ) throw new \Exception('No activity settings found'); |
| 89 |
|
| 90 |
return [ |
| 91 |
'activity_settings' => $settings |
| 92 |
]; |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
} |
| 98 |
|