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
location_helper.php
356 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 { |
| 201 | $locations = new OsLocationModel(); |
| 202 | if ( $filter_allowed_records ) { |
| 203 | $locations->filter_allowed_records(); |
| 204 | } |
| 205 | $locations = $locations->get_results_as_models(); |
| 206 | $locations_list = []; |
| 207 | if ( $locations ) { |
| 208 | foreach ( $locations as $location ) { |
| 209 | $locations_list[] = [ 'value' => $location->id, 'label' => $location->name ]; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return $locations_list; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * @param bool $filter_allowed_records |
| 218 | * |
| 219 | * @return int |
| 220 | */ |
| 221 | public static function count_locations( bool $filter_allowed_records = false ): int { |
| 222 | if ( $filter_allowed_records ) { |
| 223 | if ( self::$filtered_total_locations ) { |
| 224 | return self::$filtered_total_locations; |
| 225 | } |
| 226 | } else { |
| 227 | if ( self::$total_locations ) { |
| 228 | return self::$total_locations; |
| 229 | } |
| 230 | } |
| 231 | $locations = new OsLocationModel(); |
| 232 | if ( $filter_allowed_records ) { |
| 233 | $locations->filter_allowed_records(); |
| 234 | } |
| 235 | $locations = $locations->should_be_active()->get_results_as_models(); |
| 236 | if ( $filter_allowed_records ) { |
| 237 | self::$filtered_total_locations = $locations ? count( $locations ) : 0; |
| 238 | |
| 239 | return self::$filtered_total_locations; |
| 240 | } else { |
| 241 | self::$total_locations = $locations ? count( $locations ) : 0; |
| 242 | |
| 243 | return self::$total_locations; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | public static function get_default_location( bool $filter_allowed_records = false ): OsLocationModel { |
| 248 | $location_model = new OsLocationModel(); |
| 249 | if ( $filter_allowed_records ) { |
| 250 | $location_model->filter_allowed_records(); |
| 251 | } |
| 252 | $location = $location_model->should_be_active()->set_limit( 1 )->get_results_as_models(); |
| 253 | if ( $location && $location->id ) { |
| 254 | return $location; |
| 255 | } else { |
| 256 | // no active locations found, try searching disabled location |
| 257 | $disabled_location = $location_model->set_limit( 1 )->get_results_as_models(); |
| 258 | if ( $disabled_location && $disabled_location->id ) { |
| 259 | return $disabled_location; |
| 260 | } else { |
| 261 | // create location only if we truly haven't found anything unfiltered |
| 262 | if ( ! $filter_allowed_records || OsRolesHelper::are_all_records_allowed( 'location' ) ) { |
| 263 | return self::create_default_location(); |
| 264 | } else { |
| 265 | return new OsLocationModel(); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | public static function get_default_location_id( bool $filter_allowed_records = false ) { |
| 272 | $location = self::get_default_location( $filter_allowed_records ); |
| 273 | |
| 274 | return $location->is_new_record() ? 0 : $location->id; |
| 275 | } |
| 276 | |
| 277 | public static function create_default_location() { |
| 278 | $location_model = new OsLocationModel(); |
| 279 | $location_model->name = __( 'Main Location', 'latepoint' ); |
| 280 | if ( $location_model->save() ) { |
| 281 | $connector = new OsConnectorModel(); |
| 282 | $incomplete_connections = $connector->where( [ 'location_id' => 'IS NULL' ] )->get_results_as_models(); |
| 283 | if ( $incomplete_connections ) { |
| 284 | foreach ( $incomplete_connections as $incomplete_connection ) { |
| 285 | $incomplete_connection->update_attributes( [ 'location_id' => $location_model->id ] ); |
| 286 | } |
| 287 | } |
| 288 | $bookings = new OsBookingModel(); |
| 289 | $incomplete_bookings = $bookings->where( [ 'location_id' => 'IS NULL' ] )->get_results_as_models(); |
| 290 | if ( $incomplete_bookings ) { |
| 291 | foreach ( $incomplete_bookings as $incomplete_booking ) { |
| 292 | $incomplete_booking->update_attributes( [ 'location_id' => $location_model->id ] ); |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | return $location_model; |
| 298 | } |
| 299 | |
| 300 | |
| 301 | public static function generate_location_categories_list( $parent_id = false ) { |
| 302 | $location_categories = new OsLocationCategoryModel(); |
| 303 | $args = array(); |
| 304 | $args['parent_id'] = $parent_id ? $parent_id : 'IS NULL'; |
| 305 | $location_categories = $location_categories->where( $args )->order_by( 'order_number asc' )->get_results_as_models(); |
| 306 | if ( ! is_array( $location_categories ) ) { |
| 307 | return; |
| 308 | } |
| 309 | if ( $location_categories ) { |
| 310 | foreach ( $location_categories as $location_category ) { ?> |
| 311 | <div class="os-category-parent-w" data-id="<?php echo esc_attr($location_category->id); ?>"> |
| 312 | <div class="os-category-w"> |
| 313 | <div class="os-category-head"> |
| 314 | <div class="os-category-drag"></div> |
| 315 | <div class="os-category-name"><?php echo esc_html($location_category->name); ?></div> |
| 316 | <div class="os-category-items-meta"><?php esc_html_e('ID: ', 'latepoint'); ?> |
| 317 | <span><?php echo esc_html($location_category->id); ?></span></div> |
| 318 | <div class="os-category-items-count"> |
| 319 | <span><?php echo esc_html($location_category->count_locations()); ?></span> <?php esc_html_e('Locations Linked', 'latepoint'); ?> |
| 320 | </div> |
| 321 | <button class="os-category-edit-btn"><i class="latepoint-icon latepoint-icon-edit-3"></i> |
| 322 | </button> |
| 323 | </div> |
| 324 | <div class="os-category-body"> |
| 325 | <?php include( LATEPOINT_ADDON_PRO_VIEWS_ABSPATH . 'location_categories/_form.php' ); ?> |
| 326 | </div> |
| 327 | </div> |
| 328 | <div class="os-category-children"> |
| 329 | <?php |
| 330 | if ( is_array($location_category->locations) ) { |
| 331 | foreach ( $location_category->locations as $location ) { |
| 332 | echo '<div class="item-in-category-w status-' . esc_attr($location->status) . '" data-id="' . esc_attr($location->id) . '">'; |
| 333 | echo '<div class="os-category-item-drag"></div>'; |
| 334 | echo '<div class="os-category-item-name">' . esc_html($location->name) . '</div>'; |
| 335 | echo '<div class="os-category-item-meta">ID: ' . esc_html($location->id) . '</div>'; |
| 336 | echo '</div>'; |
| 337 | } |
| 338 | } ?> |
| 339 | <?php OsLocationHelper::generate_location_categories_list( $location_category->id ); ?> |
| 340 | </div> |
| 341 | </div> |
| 342 | <?php |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | public static function get_location_categories( ): array { |
| 348 | $result = []; |
| 349 | $location_categories = new OsLocationCategoryModel(); |
| 350 | $location_categories = $location_categories->order_by('order_number asc')->get_results_as_models(); |
| 351 | foreach ( $location_categories as $location_category ) { |
| 352 | $result[$location_category->id] = $location_category->name; |
| 353 | } |
| 354 | return $result; |
| 355 | } |
| 356 | } |