| @@ -3,58 +3,78 @@ | ||
| 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 | + * ActivityLoggerController class for REST API | |
| 11 | + * This class is responsible for getting data for all request related to activity and activity settings | |
| 12 | + * @package FluentSupport\App\Http\Controllers | |
| 13 | + * | |
| 14 | + * @version 1.0.0 | |
| 15 | + */ | |
| 8 | 16 | class ActivityLoggerController extends Controller |
| 9 | 17 | { |
| 10 | - public function getActivities() | |
| 18 | + /** | |
| 19 | + * getActivities method will get information regarding all activity with users(agent/customer) and activity settings | |
| 20 | + * @return \WP_REST_Response | array | |
| 21 | + */ | |
| 22 | + | |
| 23 | + public function getActivities (Request $request, Activity $activity) | |
| 11 | 24 | { |
| 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(); | |
| 25 | + try { | |
| 26 | + $filters = $request->get('filters', null); | |
| 27 | + $filters = is_array($filters) ? map_deep($filters, 'sanitize_text_field') : []; | |
| 17 | 28 | |
| 18 | - $settings = $this->getSettings(); | |
| 19 | - | |
| 20 | - return [ | |
| 21 | - 'activities' => $activities, | |
| 22 | - 'settings' => $settings['activity_settings'] | |
| 23 | - ]; | |
| 29 | + return $activity->getActivities( [ | |
| 30 | + 'page' => $request->getSafe('page', 'intval', 1), | |
| 31 | + 'per_page' => $request->getSafe('per_page', 'intval', 10), | |
| 32 | + 'from' => $request->getSafe('from', 'sanitize_text_field', ''), | |
| 33 | + 'to' => $request->getSafe('to', 'sanitize_text_field', ''), | |
| 34 | + 'filters' => $filters, | |
| 35 | + ] ); | |
| 36 | + } catch (\Exception $e) { | |
| 37 | + return $this->sendError([ | |
| 38 | + 'message' => Helper::getSafeErrorMessage($e) | |
| 39 | + ]); | |
| 40 | + } | |
| 24 | 41 | } |
| 25 | 42 | |
| 26 | - public function getSettings() | |
| 43 | + /** | |
| 44 | + * updateSettings method will update existing activity settings | |
| 45 | + * @return \WP_REST_Response | array | |
| 46 | + */ | |
| 47 | + public function updateSettings (Request $request, Activity $activity) | |
| 27 | 48 | { |
| 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 | - | |
| 49 | + try { | |
| 50 | + // Get raw array - do not use sanitize_text_field on the whole object (it would turn array into empty string) | |
| 51 | + $raw = $request->get('activity_settings', null); | |
| 52 | + $settings = is_array($raw) ? $raw : []; | |
| 53 | + $settings = [ | |
| 54 | + 'delete_days' => isset($settings['delete_days']) ? intval($settings['delete_days']) : 14, | |
| 55 | + 'disable_logs' => isset($settings['disable_logs']) ? sanitize_text_field($settings['disable_logs']) : 'no', | |
| 56 | + 'open_link_in_new_tab' => isset($settings['open_link_in_new_tab']) ? sanitize_text_field($settings['open_link_in_new_tab']) : 'no', | |
| 57 | + ]; | |
| 58 | + return $activity->updateSettings($settings); | |
| 59 | + } catch (\Exception $e) { | |
| 60 | + return $this->sendError([ | |
| 61 | + 'message' => Helper::getSafeErrorMessage($e) | |
| 62 | + ]); | |
| 63 | + } | |
| 41 | 64 | } |
| 42 | 65 | |
| 43 | - public function updateSettings() | |
| 66 | + /** | |
| 67 | + * getSettings method will get the list of activity settings and return | |
| 68 | + * @return \WP_REST_Response | array | |
| 69 | + */ | |
| 70 | + public function getSettings(Activity $activity) | |
| 44 | 71 | { |
| 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 | - | |
| 72 | + try { | |
| 73 | + return $activity->getSettings(); | |
| 74 | + } catch (\Exception $e) { | |
| 75 | + return $this->sendError([ | |
| 76 | + 'message' => Helper::getSafeErrorMessage($e) | |
| 77 | + ]); | |
| 78 | + } | |
| 59 | 79 | } |
| 60 | 80 | } |