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