PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / 5.2.2
LatePoint – Calendar Booking Plugin for Appointments and Events v5.2.2
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 / location_helper.php
latepoint / lib / helpers Last commit date
activities_helper.php 1 year ago agent_helper.php 1 year ago auth_helper.php 9 months 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 9 months ago carts_helper.php 1 year ago connector_helper.php 1 year ago csv_helper.php 9 months ago customer_helper.php 9 months ago customer_import_helper.php 9 months ago database_helper.php 9 months ago debug_helper.php 1 year ago defaults_helper.php 1 year ago elementor_helper.php 1 year ago email_helper.php 9 months ago encrypt_helper.php 1 year ago events_helper.php 1 year ago form_helper.php 9 months ago icalendar_helper.php 1 year ago image_helper.php 1 year ago invoices_helper.php 9 months 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 9 months ago meta_helper.php 1 year ago migrations_helper.php 1 year ago money_helper.php 1 year ago notifications_helper.php 9 months ago order_intent_helper.php 9 months ago orders_helper.php 1 year ago otp_helper.php 9 months 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 9 months ago router_helper.php 1 year ago service_helper.php 1 year ago sessions_helper.php 1 year ago settings_helper.php 9 months ago short_links_systems_helper.php 9 months ago shortcodes_helper.php 9 months ago sms_helper.php 1 year ago steps_helper.php 9 months ago stripe_connect_helper.php 9 months ago styles_helper.php 9 months ago support_topics_helper.php 1 year ago time_helper.php 9 months ago timeline_helper.php 9 months ago transaction_helper.php 1 year ago transaction_intent_helper.php 1 year ago util_helper.php 9 months ago version_specific_updates_helper.php 9 months ago whatsapp_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
location_helper.php
366 lines
1 <?php
2
3 class OsLocationHelper {
4
5 static $locations;
6 static $selected_location = false;
7 static $total_locations;
8 static $filtered_total_locations;
9
10 public static function locations_selector_html() {
11 return false;
12 }
13
14 public static function get_location_ids_for_service_and_agent( $service_id = false, $agent_id = false ): array {
15 $all_location_ids = OsConnectorHelper::get_connected_object_ids( 'location_id', [
16 'service_id' => $service_id,
17 'agent_id' => $agent_id
18 ] );
19 $locations = new OsLocationModel();
20 $active_location_ids = $locations->select( 'id' )->should_be_active()->get_results( ARRAY_A );
21 if ( $active_location_ids ) {
22 $active_location_ids = array_column( $active_location_ids, 'id' );
23 $all_location_ids = array_intersect( $active_location_ids, $all_location_ids );
24 } else {
25 $all_location_ids = [];
26 }
27
28 return $all_location_ids;
29 }
30
31
32 public static function generate_summary_for_location( OsBookingModel $booking ): void {
33 if ( $booking->location_id && $booking->location_id != LATEPOINT_ANY_LOCATION ) {
34 $locations = new OsLocationModel();
35 $location_ids = $locations->select( 'id' )->should_be_active()->get_results( ARRAY_A );
36 $location = $booking->get_location();
37 // only show location if there are multiple in database or is location has a full address set
38 if ( ( is_array( $location_ids ) && count( $location_ids ) > 1 ) || ! empty( $location->full_address ) ) { ?>
39 <div class="summary-box summary-box-location-info">
40 <div class="summary-box-heading">
41 <div class="sbh-item"><?php esc_html_e( 'Location', 'latepoint' ); ?></div>
42 <div class="sbh-line"></div>
43 </div>
44 <div class="summary-box-content with-media">
45 <div class="os-location-image"></div>
46 <div class="sbc-content-i">
47 <div class="sbc-main-item">
48 <?php
49 $location = $booking->get_location();
50 echo esc_html( $location->name );
51 if ( ! empty( $location->full_address ) ) {
52 echo ' <a href="' . esc_url( $location->get_google_maps_link() ) . '" target="_blank"><i class="latepoint-icon latepoint-icon-external-link"></i></a>';
53 }
54 ?>
55 </div>
56 <?php if ( $location->full_address ) {
57 echo '<div class="sbc-sub-item">' . esc_html( $location->full_address ) . '</div>';
58 } ?>
59 </div>
60 </div>
61 </div><?php
62 }
63 }
64 }
65
66 public static function generate_locations_list( $locations = false, $preselected_location = false ): void {
67 if ( $locations && is_array( $locations ) && ! empty( $locations ) ) { ?>
68 <div class="os-locations os-animated-parent os-items os-selectable-items os-as-rows">
69 <?php foreach ( $locations as $location ) { ?>
70 <?php if ( $preselected_location && $location->id != $preselected_location->id ) {
71 continue;
72 } ?>
73 <div tabindex="0" class="os-animated-child os-item os-selectable-item <?php echo !empty($location->full_address) ? 'with-description' : ''; ?> <?php echo ($preselected_location && $location->id === $preselected_location->id) ? 'selected is-preselected' : ''; ?>"
74 data-summary-field-name="location"
75 data-summary-value="<?php echo esc_attr($location->name); ?>"
76 data-id-holder=".latepoint_location_id"
77 data-cart-item-item-data-key="location_id"
78 data-item-id="<?php echo esc_attr($location->id); ?>">
79 <div class="os-animated-self os-item-i">
80 <div class="os-item-img-w"
81 style="background-image: url(<?php echo esc_url($location->selection_image_url); ?>);"></div>
82 <div class="os-item-name-w">
83 <div class="os-item-name"><?php echo esc_html($location->name); ?></div>
84 <?php if ($location->full_address) { ?>
85 <div class="os-item-desc"><?php echo wp_kses_post($location->full_address); ?></div>
86 <?php } ?>
87 </div>
88 </div>
89 </div>
90 <?php } ?>
91 </div>
92 <?php }
93 }
94
95 public static function generate_locations_and_categories_list( $parent_id = false, $show_selected_locations = false ) {
96 $location_categories = new OsLocationCategoryModel();
97 $args = array();
98 $args['parent_id'] = $parent_id ? $parent_id : 'IS NULL';
99 $location_categories = $location_categories->where( $args )->order_by( 'order_number asc' )->get_results_as_models();
100
101
102 $main_parent_class = ( $parent_id ) ? 'os-animated-parent' : 'os-item-categories-main-parent os-animated-parent';
103 echo '<div class="os-item-categories-holder ' . esc_attr($main_parent_class) . '">';
104
105 // generate locations that have no category
106 if ( $parent_id == false ) {
107 $locations_without_category = new OsLocationModel();
108 if ( $show_selected_locations ) {
109 $locations_without_category->where_in( 'id', $show_selected_locations );
110 }
111 $locations_without_category = $locations_without_category->where( [ 'category_id' => 0 ] )->should_be_active()->get_results_as_models();
112 if ( $locations_without_category ) {
113 OsLocationHelper::generate_locations_list( $locations_without_category );
114 }
115 }
116
117 if ( is_array( $location_categories ) ) {
118 foreach ( $location_categories as $location_category ) {
119 $locations = [];
120 $category_locations = $location_category->get_active_locations();
121 if ( is_array( $category_locations ) ) {
122 // if show selected locations restriction is set - filter
123 if ( $show_selected_locations ) {
124 foreach ( $category_locations as $category_location ) {
125 if ( in_array( $category_location->id, $show_selected_locations ) ) {
126 $locations[] = $category_location;
127 }
128 }
129 } else {
130 $locations = $category_locations;
131 }
132 }
133 $child_categories = new OsLocationCategoryModel();
134 $count_child_categories = $child_categories->where( [ 'parent_id' => $location_category->id ] )->count();
135 // show only if it has either at least one child category or location
136 if ( $count_child_categories || count( $locations ) ) { ?>
137 <div class="os-item-category-w os-items os-as-rows os-animated-child" data-id="<?php echo esc_attr($location_category->id); ?>">
138 <div class="os-item-category-info-w os-item os-animated-self with-plus">
139 <div class="os-item-category-info os-item-i">
140 <div class="os-item-img-w"
141 style="background-image: url(<?php echo esc_url($location_category->selection_image_url); ?>);"></div>
142 <div class="os-item-name-w">
143 <div class="os-item-name"><?php echo esc_html($location_category->name); ?></div>
144 </div>
145 <?php if (!empty($locations)) { ?>
146 <div class="os-item-child-count">
147 <span><?php echo (int) count($locations); ?></span> <?php esc_html_e('Locations', 'latepoint'); ?>
148 </div>
149 <?php } ?>
150 </div>
151 </div>
152 <?php OsLocationHelper::generate_locations_list( $locations ); ?>
153 <?php OsLocationHelper::generate_locations_and_categories_list( $location_category->id, $show_selected_locations ); ?>
154 </div><?php
155 }
156 }
157 }
158 echo '</div>';
159 }
160
161 public static function get_locations_for_service_and_agent( $service_id = false, $agent_id = false, $active_only = true ) {
162 $all_location_ids = OsConnectorHelper::get_connected_object_ids( 'location_id', [
163 'service_id' => $service_id,
164 'agent_id' => $agent_id
165 ] );
166 if ( $active_only ) {
167 $locations = new OsLocationModel();
168 $active_location_ids = $locations->select( 'id' )->should_be_active()->get_results( ARRAY_A );
169 if ( $active_location_ids ) {
170 $active_location_ids = array_column( $active_location_ids, 'id' );
171 $all_location_ids = array_intersect( $active_location_ids, $all_location_ids );
172 } else {
173 $all_location_ids = [];
174 }
175 }
176
177 return $all_location_ids;
178 }
179
180 /**
181 * @param bool $filter_allowed_records
182 *
183 * @return array
184 */
185 public static function get_locations( bool $filter_allowed_records = false ): array {
186 $locations = new OsLocationModel();
187 if ( $filter_allowed_records ) {
188 $locations->filter_allowed_records();
189 }
190 $locations = $locations->get_results_as_models();
191
192 return $locations;
193 }
194
195 /**
196 * @param bool $filter_allowed_records
197 *
198 * @return array
199 */
200 public static function get_locations_list( bool $filter_allowed_records = false, array $location_ids = [], bool $exclude_disabled = false ): array {
201 $locations = new OsLocationModel();
202 if ( $filter_allowed_records ) {
203 $locations->filter_allowed_records();
204 }
205
206 if (!empty($location_ids)) {
207 $locations->where_in('id', $location_ids);
208 }
209
210 if ($exclude_disabled) {
211 $locations->where(['status' => LATEPOINT_LOCATION_STATUS_ACTIVE]);
212 }
213
214 $locations = $locations->order_by('status asc, name asc')->get_results_as_models();
215 $locations_list = [];
216 if ( $locations ) {
217 foreach ( $locations as $location ) {
218 $label = ($location->status == LATEPOINT_LOCATION_STATUS_DISABLED) ? ($location->name.' ['.esc_html__('Disabled', 'latepoint').']') : $location->name;
219 $locations_list[] = [ 'value' => $location->id, 'label' => $label ];
220 }
221 }
222
223 return $locations_list;
224 }
225
226 /**
227 * @param bool $filter_allowed_records
228 *
229 * @return int
230 */
231 public static function count_locations( bool $filter_allowed_records = false ): int {
232 if ( $filter_allowed_records ) {
233 if ( self::$filtered_total_locations ) {
234 return self::$filtered_total_locations;
235 }
236 } else {
237 if ( self::$total_locations ) {
238 return self::$total_locations;
239 }
240 }
241 $locations = new OsLocationModel();
242 if ( $filter_allowed_records ) {
243 $locations->filter_allowed_records();
244 }
245 $locations = $locations->should_be_active()->get_results_as_models();
246 if ( $filter_allowed_records ) {
247 self::$filtered_total_locations = $locations ? count( $locations ) : 0;
248
249 return self::$filtered_total_locations;
250 } else {
251 self::$total_locations = $locations ? count( $locations ) : 0;
252
253 return self::$total_locations;
254 }
255 }
256
257 public static function get_default_location( bool $filter_allowed_records = false ): OsLocationModel {
258 $location_model = new OsLocationModel();
259 if ( $filter_allowed_records ) {
260 $location_model->filter_allowed_records();
261 }
262 $location = $location_model->should_be_active()->set_limit( 1 )->get_results_as_models();
263 if ( $location && $location->id ) {
264 return $location;
265 } else {
266 // no active locations found, try searching disabled location
267 $disabled_location = $location_model->set_limit( 1 )->get_results_as_models();
268 if ( $disabled_location && $disabled_location->id ) {
269 return $disabled_location;
270 } else {
271 // create location only if we truly haven't found anything unfiltered
272 if ( ! $filter_allowed_records || OsRolesHelper::are_all_records_allowed( 'location' ) ) {
273 return self::create_default_location();
274 } else {
275 return new OsLocationModel();
276 }
277 }
278 }
279 }
280
281 public static function get_default_location_id( bool $filter_allowed_records = false ) {
282 $location = self::get_default_location( $filter_allowed_records );
283
284 return $location->is_new_record() ? 0 : $location->id;
285 }
286
287 public static function create_default_location() {
288 $location_model = new OsLocationModel();
289 $location_model->name = __( 'Main Location', 'latepoint' );
290 if ( $location_model->save() ) {
291 $connector = new OsConnectorModel();
292 $incomplete_connections = $connector->where( [ 'location_id' => 'IS NULL' ] )->get_results_as_models();
293 if ( $incomplete_connections ) {
294 foreach ( $incomplete_connections as $incomplete_connection ) {
295 $incomplete_connection->update_attributes( [ 'location_id' => $location_model->id ] );
296 }
297 }
298 $bookings = new OsBookingModel();
299 $incomplete_bookings = $bookings->where( [ 'location_id' => 'IS NULL' ] )->get_results_as_models();
300 if ( $incomplete_bookings ) {
301 foreach ( $incomplete_bookings as $incomplete_booking ) {
302 $incomplete_booking->update_attributes( [ 'location_id' => $location_model->id ] );
303 }
304 }
305 }
306
307 return $location_model;
308 }
309
310
311 public static function generate_location_categories_list( $parent_id = false ) {
312 $location_categories = new OsLocationCategoryModel();
313 $args = array();
314 $args['parent_id'] = $parent_id ? $parent_id : 'IS NULL';
315 $location_categories = $location_categories->where( $args )->order_by( 'order_number asc' )->get_results_as_models();
316 if ( ! is_array( $location_categories ) ) {
317 return;
318 }
319 if ( $location_categories ) {
320 foreach ( $location_categories as $location_category ) { ?>
321 <div class="os-category-parent-w" data-id="<?php echo esc_attr($location_category->id); ?>">
322 <div class="os-category-w">
323 <div class="os-category-head">
324 <div class="os-category-drag"></div>
325 <div class="os-category-name"><?php echo esc_html($location_category->name); ?></div>
326 <div class="os-category-items-meta"><?php esc_html_e('ID: ', 'latepoint'); ?>
327 <span><?php echo esc_html($location_category->id); ?></span></div>
328 <div class="os-category-items-count">
329 <span><?php echo esc_html($location_category->count_locations()); ?></span> <?php esc_html_e('Locations Linked', 'latepoint'); ?>
330 </div>
331 <button class="os-category-edit-btn"><i class="latepoint-icon latepoint-icon-edit-3"></i>
332 </button>
333 </div>
334 <div class="os-category-body">
335 <?php include( LATEPOINT_ADDON_PRO_VIEWS_ABSPATH . 'location_categories/_form.php' ); ?>
336 </div>
337 </div>
338 <div class="os-category-children">
339 <?php
340 if ( is_array($location_category->locations) ) {
341 foreach ( $location_category->locations as $location ) {
342 echo '<div class="item-in-category-w status-' . esc_attr($location->status) . '" data-id="' . esc_attr($location->id) . '">';
343 echo '<div class="os-category-item-drag"></div>';
344 echo '<div class="os-category-item-name">' . esc_html($location->name) . '</div>';
345 echo '<div class="os-category-item-meta">ID: ' . esc_html($location->id) . '</div>';
346 echo '</div>';
347 }
348 } ?>
349 <?php OsLocationHelper::generate_location_categories_list( $location_category->id ); ?>
350 </div>
351 </div>
352 <?php
353 }
354 }
355 }
356
357 public static function get_location_categories( ): array {
358 $result = [];
359 $location_categories = new OsLocationCategoryModel();
360 $location_categories = $location_categories->order_by('order_number asc')->get_results_as_models();
361 foreach ( $location_categories as $location_category ) {
362 $result[$location_category->id] = $location_category->name;
363 }
364 return $result;
365 }
366 }