activities_helper.php
3 months ago
agent_helper.php
3 months ago
analytics_helper.php
1 week ago
auth_helper.php
6 days ago
blocks_helper.php
3 weeks ago
booking_helper.php
4 days ago
bricks_helper.php
3 months ago
bundles_helper.php
4 days ago
calendar_helper.php
2 days ago
carts_helper.php
3 months ago
connector_helper.php
3 months ago
csv_helper.php
3 months ago
customer_helper.php
1 month ago
customer_import_helper.php
1 month ago
database_helper.php
1 week ago
debug_helper.php
3 months ago
defaults_helper.php
3 months ago
elementor_helper.php
3 months ago
email_helper.php
3 months ago
encrypt_helper.php
3 months ago
events_helper.php
3 months ago
form_helper.php
3 months ago
icalendar_helper.php
3 months ago
image_helper.php
3 months ago
invoices_helper.php
3 weeks ago
license_helper.php
3 months ago
location_helper.php
3 months ago
marketing_systems_helper.php
3 months ago
meeting_systems_helper.php
3 months ago
menu_helper.php
3 weeks ago
meta_helper.php
3 months ago
migrations_helper.php
3 months ago
money_helper.php
3 months ago
notifications_helper.php
3 months ago
nps_survey_helper.php
3 months ago
order_intent_helper.php
2 months ago
orders_helper.php
3 months ago
otp_helper.php
3 months ago
pages_helper.php
3 months ago
params_helper.php
3 months ago
payments_helper.php
2 months ago
plugin_version_update_helper.php
2 months ago
price_breakdown_helper.php
3 months ago
process_jobs_helper.php
3 months ago
processes_helper.php
3 months ago
razorpay_connect_helper.php
1 week ago
replacer_helper.php
3 months ago
resource_helper.php
4 days ago
roles_helper.php
3 weeks ago
router_helper.php
3 months ago
service_helper.php
3 months ago
sessions_helper.php
3 months ago
settings_helper.php
1 week ago
short_links_systems_helper.php
3 months ago
shortcodes_helper.php
3 weeks ago
sms_helper.php
3 months ago
steps_helper.php
1 week ago
stripe_connect_helper.php
1 week ago
styles_helper.php
3 months ago
support_topics_helper.php
3 months ago
time_helper.php
2 months ago
timeline_helper.php
2 months ago
transaction_helper.php
1 month ago
transaction_intent_helper.php
3 months ago
util_helper.php
1 month ago
version_specific_updates_helper.php
3 months ago
whatsapp_helper.php
1 month ago
work_periods_helper.php
3 months ago
wp_datetime.php
3 months ago
wp_user_helper.php
3 months ago
connector_helper.php
238 lines
| 1 | <?php |
| 2 | |
| 3 | class OsConnectorHelper { |
| 4 | |
| 5 | |
| 6 | /** |
| 7 | * |
| 8 | * Returns an array of all the possible connections/resources combinations(between agent/service/location) available to satisfy a booking request |
| 9 | * |
| 10 | * @param \LatePoint\Misc\BookingRequest $booking_request |
| 11 | * @return OsConnectorModel[] |
| 12 | */ |
| 13 | public static function get_connections_that_satisfy_booking_request( \LatePoint\Misc\BookingRequest $booking_request, $filter_based_on_access = false ): array { |
| 14 | $connection_model = new OsConnectorModel(); |
| 15 | |
| 16 | // agent |
| 17 | if ( $filter_based_on_access && ! OsRolesHelper::are_all_records_allowed( 'agent' ) ) { |
| 18 | $allowed_ids = OsRolesHelper::get_allowed_records( 'agent' ); |
| 19 | // if nothing is allowed, or the requested ID is not part of allowed - return blank |
| 20 | if ( empty( $allowed_ids ) || ( $allowed_ids != LATEPOINT_ALL && ( is_array( $booking_request->agent_id ) && ! empty( reset( $booking_request->agent_id ) ) && ! array_intersect( $booking_request->agent_id, $allowed_ids ) ) ) ) { |
| 21 | return []; |
| 22 | } |
| 23 | if ( $booking_request->agent_id ) { |
| 24 | $connection_model->where( [ 'agent_id' => $booking_request->agent_id ] ); |
| 25 | } else { |
| 26 | // agent_id is 0, it means ANY is requested, filter based on what's allowed |
| 27 | $connection_model->where( [ 'agent_id' => $allowed_ids ] ); |
| 28 | } |
| 29 | } else { |
| 30 | $active_agents = new OsAgentModel(); |
| 31 | $active_agent_ids = array_column( $active_agents->select( 'id' )->should_be_active()->get_results( ARRAY_A ), 'id' ); |
| 32 | |
| 33 | if ( empty( $active_agent_ids ) ) { |
| 34 | return []; // no active agents |
| 35 | } |
| 36 | |
| 37 | if ( empty( $booking_request->agent_id ) ) { |
| 38 | // any agent requested, search in a list of active |
| 39 | $connection_model->where( [ 'agent_id' => $active_agent_ids ] ); |
| 40 | } else { |
| 41 | // specific agent/or agents requested, check if it's in a list of active agents |
| 42 | if ( is_array( $booking_request->agent_id ) ) { |
| 43 | // array of agents requested, intersect |
| 44 | if ( array_intersect( $booking_request->agent_id, $active_agent_ids ) ) { |
| 45 | $connection_model->where( [ 'agent_id' => array_intersect( $booking_request->agent_id, $active_agent_ids ) ] ); |
| 46 | } else { |
| 47 | return []; |
| 48 | } |
| 49 | } else { |
| 50 | if ( in_array( $booking_request->agent_id, $active_agent_ids ) ) { |
| 51 | $connection_model->where( [ 'agent_id' => $booking_request->agent_id ] ); |
| 52 | } else { |
| 53 | // requested agent not in a list of active agents |
| 54 | return []; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // location |
| 61 | if ( $filter_based_on_access && ! OsRolesHelper::are_all_records_allowed( 'location' ) ) { |
| 62 | $allowed_ids = OsRolesHelper::get_allowed_records( 'location' ); |
| 63 | // if nothing is allowed, or the requested ID is not part of allowed - return blank |
| 64 | if ( empty( $allowed_ids ) || ( $allowed_ids != LATEPOINT_ALL && ( is_array( $booking_request->location_id ) && ! empty( reset( $booking_request->location_id ) ) && ! array_intersect( $booking_request->location_id, $allowed_ids ) ) ) ) { |
| 65 | return []; |
| 66 | } |
| 67 | if ( $booking_request->location_id ) { |
| 68 | $connection_model->where( [ 'location_id' => $booking_request->location_id ] ); |
| 69 | } else { |
| 70 | // location_id is 0, it means ANY is requested, filter based on what's allowed |
| 71 | $connection_model->where( [ 'location_id' => $allowed_ids ] ); |
| 72 | } |
| 73 | } else { |
| 74 | $active_locations = new OsLocationModel(); |
| 75 | $active_location_ids = array_column( $active_locations->select( 'id' )->should_be_active()->get_results( ARRAY_A ), 'id' ); |
| 76 | |
| 77 | |
| 78 | if ( empty( $active_location_ids ) ) { |
| 79 | return []; // no active locations |
| 80 | } |
| 81 | |
| 82 | if ( empty( $booking_request->location_id ) ) { |
| 83 | // any location requested, search in a list of active |
| 84 | $connection_model->where( [ 'location_id' => $active_location_ids ] ); |
| 85 | } else { |
| 86 | // specific location/or locations requested, check if it's in a list of active locations |
| 87 | if ( is_array( $booking_request->location_id ) ) { |
| 88 | // array of locations requested, intersect |
| 89 | if ( array_intersect( $booking_request->location_id, $active_location_ids ) ) { |
| 90 | $connection_model->where( [ 'location_id' => array_intersect( $booking_request->location_id, $active_location_ids ) ] ); |
| 91 | } else { |
| 92 | return []; |
| 93 | } |
| 94 | } else { |
| 95 | if ( in_array( $booking_request->location_id, $active_location_ids ) ) { |
| 96 | $connection_model->where( [ 'location_id' => $booking_request->location_id ] ); |
| 97 | } else { |
| 98 | // requested location not in a list of active locations |
| 99 | return []; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // service |
| 106 | if ( $filter_based_on_access && ! OsRolesHelper::are_all_records_allowed( 'service' ) ) { |
| 107 | $allowed_ids = OsRolesHelper::get_allowed_records( 'service' ); |
| 108 | // if nothing is allowed, or the requested ID is not part of allowed - return blank |
| 109 | if ( empty( $allowed_ids ) || ( $allowed_ids != LATEPOINT_ALL && ( is_array( $booking_request->service_id ) && ! empty( reset( $booking_request->service_id ) ) && ! array_intersect( $booking_request->service_id, $allowed_ids ) ) ) ) { |
| 110 | return []; |
| 111 | } |
| 112 | if ( $booking_request->service_id ) { |
| 113 | $connection_model->where( [ 'service_id' => $booking_request->service_id ] ); |
| 114 | } else { |
| 115 | // service_id is 0, it means ANY is requested, filter based on what's allowed |
| 116 | $connection_model->where( [ 'service_id' => $allowed_ids ] ); |
| 117 | } |
| 118 | } else { |
| 119 | $active_services = new OsServiceModel(); |
| 120 | $active_service_ids = array_column( $active_services->select( 'id' )->should_be_active()->get_results( ARRAY_A ), 'id' ); |
| 121 | |
| 122 | if ( empty( $active_service_ids ) ) { |
| 123 | return []; // no active services |
| 124 | } |
| 125 | |
| 126 | if ( empty( $booking_request->service_id ) ) { |
| 127 | // any service requested, search in a list of active |
| 128 | $connection_model->where( [ 'service_id' => $active_service_ids ] ); |
| 129 | } else { |
| 130 | // specific service/or services requested, check if it's in a list of active services |
| 131 | if ( is_array( $booking_request->service_id ) ) { |
| 132 | // array of services requested, intersect |
| 133 | if ( array_intersect( $booking_request->service_id, $active_service_ids ) ) { |
| 134 | $connection_model->where( [ 'service_id' => array_intersect( $booking_request->service_id, $active_service_ids ) ] ); |
| 135 | } else { |
| 136 | return []; |
| 137 | } |
| 138 | } else { |
| 139 | if ( in_array( $booking_request->service_id, $active_service_ids ) ) { |
| 140 | $connection_model->where( [ 'service_id' => $booking_request->service_id ] ); |
| 141 | } else { |
| 142 | // requested service not in a list of active services |
| 143 | return []; |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return $connection_model->get_results_as_models(); |
| 150 | } |
| 151 | |
| 152 | // expects [agent_id, 'service_id', 'location_id'] |
| 153 | public static function count_connections( $connection_query_arr, $group_by = false ) { |
| 154 | $connection_model = new OsConnectorModel(); |
| 155 | $connection_model->where( $connection_query_arr ); |
| 156 | if ( $group_by ) { |
| 157 | $results = $connection_model->select( $group_by )->group_by( $group_by )->get_results(); |
| 158 | $total = count( $results ); |
| 159 | } else { |
| 160 | $total = $connection_model->count(); |
| 161 | } |
| 162 | return $total; |
| 163 | } |
| 164 | |
| 165 | public static function can_satisfy_booking_request( \LatePoint\Misc\BookingRequest $booking_request ) { |
| 166 | $connection_model = new OsConnectorModel(); |
| 167 | return $connection_model->where( |
| 168 | [ |
| 169 | 'agent_id' => $booking_request->agent_id, |
| 170 | 'location_id' => $booking_request->location_id, |
| 171 | 'service_id' => $booking_request->service_id, |
| 172 | ] |
| 173 | )->set_limit( 1 )->get_results_as_models(); |
| 174 | } |
| 175 | |
| 176 | public static function has_connection( $connection_arr ) { |
| 177 | $connection_model = new OsConnectorModel(); |
| 178 | return $connection_model->where( $connection_arr )->set_limit( 1 )->get_results_as_models(); |
| 179 | } |
| 180 | |
| 181 | // expects [agent_id, 'service_id', 'location_id'] |
| 182 | public static function save_connection( $connection_arr ) { |
| 183 | $connection_model = new OsConnectorModel(); |
| 184 | $existing_connection = $connection_model->where( $connection_arr )->set_limit( 1 )->get_results_as_models(); |
| 185 | if ( $existing_connection ) { |
| 186 | // Update |
| 187 | } else { |
| 188 | // Insert |
| 189 | $connection_model->set_data( $connection_arr ); |
| 190 | return $connection_model->save(); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | |
| 195 | // object type: agent_id, service_id, location_id |
| 196 | public static function get_connected_object_ids( $object_type = 'agent_id', $connections = [] ) { |
| 197 | if ( ! in_array( $object_type, [ 'agent_id', 'service_id', 'location_id' ] ) ) { |
| 198 | return false; |
| 199 | } |
| 200 | $clean_connections = []; |
| 201 | if ( isset( $connections['agent_id'] ) && ! empty( $connections['agent_id'] ) && $connections['agent_id'] != LATEPOINT_ANY_AGENT ) { |
| 202 | $clean_connections['agent_id'] = $connections['agent_id']; |
| 203 | } |
| 204 | if ( isset( $connections['service_id'] ) && ! empty( $connections['service_id'] ) ) { |
| 205 | $clean_connections['service_id'] = $connections['service_id']; |
| 206 | } |
| 207 | if ( isset( $connections['location_id'] ) && ! empty( $connections['location_id'] ) && $connections['location_id'] != LATEPOINT_ANY_LOCATION ) { |
| 208 | $clean_connections['location_id'] = $connections['location_id']; |
| 209 | } |
| 210 | $connection_model = new OsConnectorModel(); |
| 211 | if ( ! empty( $clean_connections ) ) { |
| 212 | $connection_model->where( $clean_connections ); |
| 213 | } |
| 214 | $objects = $connection_model->select( $object_type )->group_by( $object_type )->get_results(); |
| 215 | $ids = []; |
| 216 | if ( $objects ) { |
| 217 | foreach ( $objects as $object ) { |
| 218 | if ( isset( $object->$object_type ) ) { |
| 219 | $ids[] = $object->$object_type; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | return $ids; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | // expects [agent_id, 'service_id', 'location_id'] |
| 228 | public static function remove_connection( $connection_arr ) { |
| 229 | $connection_model = new OsConnectorModel(); |
| 230 | if ( isset( $connection_arr['agent_id'] ) && isset( $connection_arr['service_id'] ) && isset( $connection_arr['location_id'] ) ) { |
| 231 | $existing_connection = $connection_model->where( $connection_arr )->set_limit( 1 )->get_results_as_models(); |
| 232 | if ( $existing_connection ) { |
| 233 | $existing_connection->delete(); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 |