abstract-activity-ability.php
3 months ago
get-activity.php
3 months ago
list-activities.php
3 months ago
list-activities.php
103 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | class LatePointAbilityListActivities extends LatePointAbstractActivityAbility { |
| 7 | |
| 8 | protected function configure(): void { |
| 9 | $this->id = 'latepoint/list-activities'; |
| 10 | $this->label = __( 'List activities', 'latepoint' ); |
| 11 | $this->description = __( 'Returns a paginated list of activity log entries with optional filters.', 'latepoint' ); |
| 12 | $this->permission = 'activity__view'; |
| 13 | $this->read_only = true; |
| 14 | } |
| 15 | |
| 16 | public function get_input_schema(): array { |
| 17 | return [ |
| 18 | 'type' => 'object', |
| 19 | 'properties' => array_merge( |
| 20 | self::pagination(), |
| 21 | [ |
| 22 | 'entity_type' => [ |
| 23 | 'type' => 'string', |
| 24 | 'description' => __( 'Entity type to filter by (e.g. booking, customer).', 'latepoint' ), |
| 25 | ], |
| 26 | 'entity_id' => [ 'type' => 'integer' ], |
| 27 | 'date_from' => [ |
| 28 | 'type' => 'string', |
| 29 | 'format' => 'date', |
| 30 | ], |
| 31 | 'date_to' => [ |
| 32 | 'type' => 'string', |
| 33 | 'format' => 'date', |
| 34 | ], |
| 35 | ] |
| 36 | ), |
| 37 | ]; |
| 38 | } |
| 39 | |
| 40 | public function get_output_schema(): array { |
| 41 | return [ |
| 42 | 'type' => 'object', |
| 43 | 'properties' => [ |
| 44 | 'activities' => [ |
| 45 | 'type' => 'array', |
| 46 | 'items' => $this->activity_output_schema(), |
| 47 | ], |
| 48 | 'total' => [ 'type' => 'integer' ], |
| 49 | 'page' => [ 'type' => 'integer' ], |
| 50 | 'per_page' => [ 'type' => 'integer' ], |
| 51 | ], |
| 52 | ]; |
| 53 | } |
| 54 | |
| 55 | public function execute( array $args ) { |
| 56 | $page = max( 1, (int) ( $args['page'] ?? 1 ) ); |
| 57 | $per_page = min( 100, max( 1, (int) ( $args['per_page'] ?? 20 ) ) ); |
| 58 | $offset = ( $page - 1 ) * $per_page; |
| 59 | |
| 60 | $query = new OsActivityModel(); |
| 61 | |
| 62 | if ( ! empty( $args['entity_type'] ) && ! empty( $args['entity_id'] ) ) { |
| 63 | $entity_type = sanitize_text_field( $args['entity_type'] ); |
| 64 | $entity_id = (int) $args['entity_id']; |
| 65 | switch ( $entity_type ) { |
| 66 | case 'booking': |
| 67 | $query->where( [ 'booking_id' => $entity_id ] ); |
| 68 | break; |
| 69 | case 'customer': |
| 70 | $query->where( [ 'customer_id' => $entity_id ] ); |
| 71 | break; |
| 72 | case 'agent': |
| 73 | $query->where( [ 'agent_id' => $entity_id ] ); |
| 74 | break; |
| 75 | case 'order': |
| 76 | $query->where( [ 'order_id' => $entity_id ] ); |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if ( ! empty( $args['date_from'] ) ) { |
| 82 | $query->where( [ 'created_at >=' => sanitize_text_field( $args['date_from'] ) . ' 00:00:00' ] ); |
| 83 | } |
| 84 | if ( ! empty( $args['date_to'] ) ) { |
| 85 | $query->where( [ 'created_at <=' => sanitize_text_field( $args['date_to'] ) . ' 23:59:59' ] ); |
| 86 | } |
| 87 | |
| 88 | $activities = ( clone $query ) |
| 89 | ->order_by( 'created_at DESC' ) |
| 90 | ->set_limit( $per_page ) |
| 91 | ->set_offset( $offset ) |
| 92 | ->get_results_as_models(); |
| 93 | $total = $query->count(); |
| 94 | |
| 95 | return [ |
| 96 | 'activities' => array_map( [ $this, 'serialize_activity' ], $activities ), |
| 97 | 'total' => (int) $total, |
| 98 | 'page' => $page, |
| 99 | 'per_page' => $per_page, |
| 100 | ]; |
| 101 | } |
| 102 | } |
| 103 |