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