| @@ -3,58 +3,71 @@ | ||
| 3 | 3 | namespace FluentSupport\App\Http\Controllers; |
| 4 | 4 | |
| 5 | 5 | use FluentSupport\App\Models\Activity; |
| 6 | 6 | use FluentSupport\App\Services\Helper; |
| 7 | +use FluentSupport\Framework\Http\Request\Request; | |
| 7 | 8 | |
| 9 | +/** | |
| 10 | + * Activity log endpoints and settings. | |
| 11 | + */ | |
| 8 | 12 | class ActivityLoggerController extends Controller |
| 9 | 13 | { |
| 10 | - public function getActivities() | |
| 14 | + /** | |
| 15 | + * @return \WP_REST_Response | array | |
| 16 | + */ | |
| 17 | + | |
| 18 | + public function getActivities (Request $request, Activity $activity) | |
| 11 | 19 | { |
| 12 | - $activities = Activity::with([ | |
| 13 | - 'person' => function ($query) { | |
| 14 | - $query->select(['first_name', 'person_type', 'last_name', 'id', 'avatar']); | |
| 15 | - } | |
| 16 | - ])->orderBy('id', 'DESC')->paginate(); | |
| 20 | + try { | |
| 21 | + $filters = $request->get('filters', null); | |
| 22 | + $filters = is_array($filters) ? map_deep($filters, 'sanitize_text_field') : []; | |
| 17 | 23 | |
| 18 | - $settings = $this->getSettings(); | |
| 19 | - | |
| 20 | - return [ | |
| 21 | - 'activities' => $activities, | |
| 22 | - 'settings' => $settings['activity_settings'] | |
| 23 | - ]; | |
| 24 | + return $activity->getActivities( [ | |
| 25 | + 'page' => $request->getSafe('page', 'intval', 1), | |
| 26 | + 'per_page' => $request->getSafe('per_page', 'intval', 10), | |
| 27 | + 'from' => $request->getSafe('from', 'sanitize_text_field', ''), | |
| 28 | + 'to' => $request->getSafe('to', 'sanitize_text_field', ''), | |
| 29 | + 'filters' => $filters, | |
| 30 | + ] ); | |
| 31 | + } catch (\Exception $e) { | |
| 32 | + return $this->sendError([ | |
| 33 | + 'message' => Helper::getSafeErrorMessage($e) | |
| 34 | + ]); | |
| 35 | + } | |
| 24 | 36 | } |
| 25 | 37 | |
| 26 | - public function getSettings() | |
| 38 | + /** | |
| 39 | + * @return \WP_REST_Response | array | |
| 40 | + */ | |
| 41 | + public function updateSettings (Request $request, Activity $activity) | |
| 27 | 42 | { |
| 28 | - $settings = Helper::getOption('_activity_settings', []); | |
| 29 | - | |
| 30 | - $defaults = [ | |
| 31 | - 'delete_days' => 14, | |
| 32 | - 'disable_logs' => 'no' | |
| 33 | - ]; | |
| 34 | - | |
| 35 | - $settings = wp_parse_args($settings, $defaults); | |
| 36 | - | |
| 37 | - return [ | |
| 38 | - 'activity_settings' => $settings | |
| 39 | - ]; | |
| 40 | - | |
| 43 | + try { | |
| 44 | + // Read raw: sanitize_text_field on the array would turn it into an empty string. | |
| 45 | + $raw = $request->get('activity_settings', null); | |
| 46 | + $settings = is_array($raw) ? $raw : []; | |
| 47 | + $settings = [ | |
| 48 | + 'delete_days' => isset($settings['delete_days']) ? intval($settings['delete_days']) : 14, | |
| 49 | + 'disable_logs' => isset($settings['disable_logs']) ? sanitize_text_field($settings['disable_logs']) : 'no', | |
| 50 | + 'open_link_in_new_tab' => isset($settings['open_link_in_new_tab']) ? sanitize_text_field($settings['open_link_in_new_tab']) : 'no', | |
| 51 | + ]; | |
| 52 | + return $activity->updateSettings($settings); | |
| 53 | + } catch (\Exception $e) { | |
| 54 | + return $this->sendError([ | |
| 55 | + 'message' => Helper::getSafeErrorMessage($e) | |
| 56 | + ]); | |
| 57 | + } | |
| 41 | 58 | } |
| 42 | 59 | |
| 43 | - public function updateSettings() | |
| 60 | + /** | |
| 61 | + * @return \WP_REST_Response | array | |
| 62 | + */ | |
| 63 | + public function getSettings(Activity $activity) | |
| 44 | 64 | { |
| 45 | - $settings = $this->request->get('activity_settings', []); | |
| 46 | - $defaults = [ | |
| 47 | - 'delete_days' => 14, | |
| 48 | - 'disable_logs' => 'no' | |
| 49 | - ]; | |
| 50 | - $settings = wp_parse_args($settings, $defaults); | |
| 51 | - $settings['delete_days'] = intval($settings['delete_days']); | |
| 52 | - | |
| 53 | - Helper::updateOption('_activity_settings', $settings); | |
| 54 | - | |
| 55 | - return [ | |
| 56 | - 'message' => __('Activity settings has been updated', 'fluent-support') | |
| 57 | - ]; | |
| 58 | - | |
| 65 | + try { | |
| 66 | + return $activity->getSettings(); | |
| 67 | + } catch (\Exception $e) { | |
| 68 | + return $this->sendError([ | |
| 69 | + 'message' => Helper::getSafeErrorMessage($e) | |
| 70 | + ]); | |
| 71 | + } | |
| 59 | 72 | } |
| 60 | 73 | } |