PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.4.1
LatePoint – Calendar Booking Plugin for Appointments and Events v5.4.1
5.6.7 5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / helpers / connector_helper.php
latepoint / lib / helpers Last commit date
activities_helper.php 3 months ago agent_helper.php 3 months ago analytics_helper.php 2 months ago auth_helper.php 3 months ago blocks_helper.php 3 months ago booking_helper.php 3 months ago bricks_helper.php 3 months ago bundles_helper.php 3 months ago calendar_helper.php 3 months ago carts_helper.php 3 months ago connector_helper.php 3 months ago csv_helper.php 3 months ago customer_helper.php 3 months ago customer_import_helper.php 3 months ago database_helper.php 3 months 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 months 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 2 months 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 3 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 3 months ago price_breakdown_helper.php 3 months ago process_jobs_helper.php 3 months ago processes_helper.php 3 months ago replacer_helper.php 3 months ago resource_helper.php 3 months ago roles_helper.php 3 months ago router_helper.php 3 months ago service_helper.php 3 months ago sessions_helper.php 3 months ago settings_helper.php 3 months ago short_links_systems_helper.php 3 months ago shortcodes_helper.php 2 months ago sms_helper.php 3 months ago steps_helper.php 3 months ago stripe_connect_helper.php 2 months ago styles_helper.php 3 months ago support_topics_helper.php 3 months ago time_helper.php 2 months ago timeline_helper.php 3 months ago transaction_helper.php 3 months ago transaction_intent_helper.php 3 months ago update_helper.php 2 months ago util_helper.php 3 months ago version_specific_updates_helper.php 3 months ago whatsapp_helper.php 3 months 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