create-booking.php
1 month ago
delete-booking.php
1 month ago
get-booking-details.php
1 month ago
list-bookings.php
1 month ago
update-booking-status.php
1 month ago
list-bookings.php
130 lines
| 1 | <?php |
| 2 | /** |
| 3 | * ListBookings. |
| 4 | * php version 5.6 |
| 5 | * |
| 6 | * @category ListBookings |
| 7 | * @package SureTriggers |
| 8 | * @author BSF <username@example.com> |
| 9 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 | * @link https://www.brainstormforce.com/ |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace SureTriggers\Integrations\JetBooking\Actions; |
| 15 | |
| 16 | use SureTriggers\Integrations\AutomateAction; |
| 17 | use SureTriggers\Traits\SingletonLoader; |
| 18 | use SureTriggers\Integrations\JetBooking\JetBooking; |
| 19 | use JET_ABAF\Plugin; |
| 20 | |
| 21 | /** |
| 22 | * ListBookings |
| 23 | * |
| 24 | * @category ListBookings |
| 25 | * @package SureTriggers |
| 26 | * @author BSF <username@example.com> |
| 27 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 28 | * @link https://www.brainstormforce.com/ |
| 29 | * @since 1.0.0 |
| 30 | */ |
| 31 | class ListBookings extends AutomateAction { |
| 32 | |
| 33 | /** |
| 34 | * Integration type. |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | public $integration = 'JetBooking'; |
| 39 | |
| 40 | /** |
| 41 | * Action name. |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | public $action = 'jet_list_bookings'; |
| 46 | |
| 47 | use SingletonLoader; |
| 48 | |
| 49 | /** |
| 50 | * Register a action. |
| 51 | * |
| 52 | * @param array $actions actions. |
| 53 | * @return array |
| 54 | */ |
| 55 | public function register( $actions ) { |
| 56 | $actions[ $this->integration ][ $this->action ] = [ |
| 57 | 'label' => __( 'List Bookings', 'suretriggers' ), |
| 58 | 'action' => 'jet_list_bookings', |
| 59 | 'function' => [ $this, 'action_listener' ], |
| 60 | ]; |
| 61 | |
| 62 | return $actions; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Action listener. |
| 67 | * |
| 68 | * @param int $user_id user_id. |
| 69 | * @param int $automation_id automation_id. |
| 70 | * @param array $fields fields. |
| 71 | * @param array $selected_options selectedOptions. |
| 72 | * |
| 73 | * @return array |
| 74 | */ |
| 75 | public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) { |
| 76 | |
| 77 | if ( ! class_exists( '\JET_ABAF\Plugin' ) ) { |
| 78 | return [ |
| 79 | 'status' => 'error', |
| 80 | 'message' => __( 'JetBooking plugin is not installed or active.', 'suretriggers' ), |
| 81 | ]; |
| 82 | } |
| 83 | |
| 84 | $apartment_id = isset( $selected_options['apartment_id'] ) ? absint( $selected_options['apartment_id'] ) : 0; |
| 85 | $status = isset( $selected_options['status'] ) ? sanitize_text_field( $selected_options['status'] ) : ''; |
| 86 | |
| 87 | $args = []; |
| 88 | |
| 89 | if ( ! empty( $apartment_id ) ) { |
| 90 | $args['apartment_id'] = $apartment_id; |
| 91 | } |
| 92 | |
| 93 | if ( ! empty( $status ) ) { |
| 94 | $args['status'] = $status; |
| 95 | } |
| 96 | |
| 97 | try { |
| 98 | $bookings = Plugin::instance()->db->query( $args ); |
| 99 | |
| 100 | if ( empty( $bookings ) ) { |
| 101 | return [ |
| 102 | 'success' => true, |
| 103 | 'bookings' => [], |
| 104 | 'total_count' => 0, |
| 105 | 'message' => __( 'No bookings found matching the criteria.', 'suretriggers' ), |
| 106 | ]; |
| 107 | } |
| 108 | |
| 109 | $processed_bookings = []; |
| 110 | |
| 111 | foreach ( $bookings as $booking ) { |
| 112 | $processed_bookings[] = JetBooking::get_booking_context( $booking ); |
| 113 | } |
| 114 | |
| 115 | return [ |
| 116 | 'success' => true, |
| 117 | 'bookings' => $processed_bookings, |
| 118 | 'total_count' => count( $processed_bookings ), |
| 119 | ]; |
| 120 | } catch ( \Exception $e ) { |
| 121 | return [ |
| 122 | 'status' => 'error', |
| 123 | 'message' => $e->getMessage(), |
| 124 | ]; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | ListBookings::get_instance(); |
| 130 |