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
agent_helper.php
363 lines
| 1 | <?php |
| 2 | |
| 3 | class OsAgentHelper { |
| 4 | |
| 5 | static $agents; |
| 6 | static $selected_agent = false; |
| 7 | static $total_agents; |
| 8 | static $filtered_total_agents; |
| 9 | |
| 10 | |
| 11 | public static function quick_agent_btn_html( $agent_id = false, $params = array() ) { |
| 12 | $html = ''; |
| 13 | if ( $agent_id ) { |
| 14 | $params['agent_id'] = $agent_id; |
| 15 | } |
| 16 | $route = OsRouterHelper::build_route_name( 'agents', ! empty( $agent_id ) ? 'quick_edit' : 'quick_new' ); |
| 17 | |
| 18 | $params_str = http_build_query( $params ); |
| 19 | $html = 'data-os-params="' . esc_attr( $params_str ) . '" |
| 20 | data-os-action="' . esc_attr( $route ) . '" |
| 21 | data-os-output-target="side-panel" |
| 22 | data-os-after-call="latepoint_init_quick_agent_form"'; |
| 23 | |
| 24 | return $html; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @return OsAgentModel[] |
| 29 | */ |
| 30 | public static function get_allowed_active_agents(): array { |
| 31 | $agents = new OsAgentModel(); |
| 32 | |
| 33 | return $agents->should_be_active()->filter_allowed_records()->get_results_as_models(); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @param bool $filter_allowed_records |
| 38 | * |
| 39 | * @return int |
| 40 | */ |
| 41 | public static function count_agents( bool $filter_allowed_records = false ): int { |
| 42 | if ( $filter_allowed_records ) { |
| 43 | if ( self::$filtered_total_agents ) { |
| 44 | return self::$filtered_total_agents; |
| 45 | } |
| 46 | } else { |
| 47 | if ( self::$total_agents ) { |
| 48 | return self::$total_agents; |
| 49 | } |
| 50 | } |
| 51 | $agents = new OsAgentModel(); |
| 52 | if ( $filter_allowed_records ) { |
| 53 | $agents->filter_allowed_records(); |
| 54 | } |
| 55 | $agents = $agents->should_be_active()->get_results_as_models(); |
| 56 | if ( $filter_allowed_records ) { |
| 57 | self::$filtered_total_agents = $agents ? count( $agents ) : 0; |
| 58 | |
| 59 | return self::$filtered_total_agents; |
| 60 | } else { |
| 61 | self::$total_agents = $agents ? count( $agents ) : 0; |
| 62 | |
| 63 | return self::$total_agents; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | |
| 68 | public static function create_default_agent() { |
| 69 | $agent_model = new OsAgentModel(); |
| 70 | |
| 71 | $current_user = wp_get_current_user(); |
| 72 | |
| 73 | $agent_model->first_name = $current_user->user_firstname ?? ''; |
| 74 | $agent_model->last_name = $current_user->user_lastname ?? ''; |
| 75 | $agent_model->email = get_bloginfo( 'admin_email' ); |
| 76 | if ( $agent_model->save() ) { |
| 77 | $connector = new OsConnectorModel(); |
| 78 | $incomplete_connections = $connector->where( [ 'agent_id' => 'IS NULL' ] )->get_results_as_models(); |
| 79 | if ( $incomplete_connections ) { |
| 80 | foreach ( $incomplete_connections as $incomplete_connection ) { |
| 81 | $incomplete_connection->update_attributes( [ 'agent_id' => $agent_model->id ] ); |
| 82 | } |
| 83 | } |
| 84 | $bookings = new OsBookingModel(); |
| 85 | $incomplete_bookings = $bookings->where( [ 'agent_id' => 'IS NULL' ] )->get_results_as_models(); |
| 86 | if ( $incomplete_bookings ) { |
| 87 | foreach ( $incomplete_bookings as $incomplete_booking ) { |
| 88 | $incomplete_booking->update_attributes( [ 'agent_id' => $agent_model->id ] ); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return $agent_model; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | public static function get_default_agent(): OsAgentModel { |
| 98 | $agent_model = new OsAgentModel(); |
| 99 | $agent = $agent_model->should_be_active()->set_limit( 1 )->get_results_as_models(); |
| 100 | if ( $agent && $agent->id ) { |
| 101 | return $agent; |
| 102 | } else { |
| 103 | // no active agents found, try searching disabled agent |
| 104 | $disabled_agent = $agent_model->set_limit( 1 )->get_results_as_models(); |
| 105 | // create agent only if we truly haven't found anything unfiltered |
| 106 | if ( $disabled_agent && $disabled_agent->id ) { |
| 107 | return $disabled_agent; |
| 108 | } else { |
| 109 | return self::create_default_agent(); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | |
| 115 | public static function get_default_agent_id() { |
| 116 | $agent = self::get_default_agent(); |
| 117 | |
| 118 | return $agent->is_new_record() ? 0 : $agent->id; |
| 119 | } |
| 120 | |
| 121 | public static function generate_summary_for_agent( OsBookingModel $booking ): void { |
| 122 | if ( OsAgentHelper::count_agents() > 1 && OsSettingsHelper::is_off( 'steps_hide_agent_info' ) && $booking->agent_id && $booking->agent_id != LATEPOINT_ANY_AGENT ) { ?> |
| 123 | <div class="summary-box summary-box-agent-info"> |
| 124 | <div class="summary-box-heading"> |
| 125 | <div class="sbh-item"><?php esc_html_e( 'Agent', 'latepoint' ) ?></div> |
| 126 | <div class="sbh-line"></div> |
| 127 | </div> |
| 128 | <div class="summary-box-content with-media"> |
| 129 | <div class="os-avatar-w" |
| 130 | style="background-image: url(<?php echo ( $booking->agent->avatar_image_id ) ? esc_url( $booking->agent->get_avatar_url() ) : ''; ?>)"> |
| 131 | <?php if ( ! $booking->agent->avatar_image_id ) { |
| 132 | echo '<div class="os-avatar"><span>' . esc_html( $booking->agent->get_initials() ) . '</span></div>'; |
| 133 | } ?> |
| 134 | </div> |
| 135 | <div class="sbc-content-i"> |
| 136 | <div class="sbc-main-item"><?php echo esc_html( $booking->agent->full_name ); ?></div> |
| 137 | <?php |
| 138 | if ( OsSettingsHelper::steps_show_agent_bio() ) { |
| 139 | echo '<div class="os-trigger-item-details-popup sbc-link-item" data-item-details-popup-id="osItemDetailsPopupAgent_' . esc_attr( $booking->agent_id ) . '">' . esc_html__( 'Learn More', 'latepoint' ) . '</div>'; |
| 140 | echo OsAgentHelper::generate_bio( $booking->agent ); |
| 141 | } |
| 142 | ?> |
| 143 | </div> |
| 144 | </div> |
| 145 | </div> |
| 146 | <?php |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | public static function generate_agents_list( array $agents ): void { |
| 151 | if ( ! empty( $agents ) ) { ?> |
| 152 | <div class="os-agents os-animated-parent os-items os-selectable-items os-as-grid os-three-columns"> |
| 153 | <?php $show_agent_bio = OsSettingsHelper::steps_show_agent_bio(); ?> |
| 154 | <?php if ( OsSettingsHelper::is_on( 'allow_any_agent' ) ) { ?> |
| 155 | <div class="os-animated-child os-item os-selectable-item" |
| 156 | data-summary-field-name="agent" |
| 157 | data-summary-value="<?php esc_attr_e( 'Any Agent', 'latepoint' ); ?>" |
| 158 | data-id-holder=".latepoint_agent_id" |
| 159 | data-cart-item-item-data-key="agent_id" |
| 160 | data-item-id="<?php echo esc_attr( LATEPOINT_ANY_AGENT ); ?>"> |
| 161 | <div class="os-animated-self os-item-i"> |
| 162 | <div class="os-item-img-w os-with-avatar"> |
| 163 | <div class="os-avatar" |
| 164 | style="background-image: url(<?php echo esc_url( LATEPOINT_IMAGES_URL . 'default-avatar.jpg' ); ?>);"></div> |
| 165 | </div> |
| 166 | <div class="os-item-name-w"> |
| 167 | <div class="os-item-name"><?php esc_html_e( 'Any Agent', 'latepoint' ); ?></div> |
| 168 | </div> |
| 169 | </div> |
| 170 | </div> |
| 171 | <?php } ?> |
| 172 | <?php foreach ( $agents as $agent ) { ?> |
| 173 | <div class="os-animated-child os-item os-selectable-item <?php echo $show_agent_bio ? 'with-details' : ''; ?>" |
| 174 | tabindex="0" |
| 175 | data-summary-field-name="agent" |
| 176 | data-summary-value="<?php echo esc_attr( $agent->name_for_front ); ?>" |
| 177 | data-id-holder=".latepoint_agent_id" |
| 178 | data-cart-item-item-data-key="agent_id" |
| 179 | data-item-id="<?php echo esc_attr( $agent->id ); ?>"> |
| 180 | <div class="os-animated-self os-item-i"> |
| 181 | <div class="os-item-img-w os-with-avatar"> |
| 182 | <div class="os-avatar" |
| 183 | style="background-image: url(<?php echo esc_url( $agent->avatar_url ); ?>);"></div> |
| 184 | </div> |
| 185 | <div class="os-item-name-w"> |
| 186 | <div class="os-item-name"><?php echo esc_html( $agent->name_for_front ); ?></div> |
| 187 | </div> |
| 188 | <?php if ( $show_agent_bio ) { ?> |
| 189 | <div class="os-item-details-popup-btn os-trigger-item-details-popup" |
| 190 | data-item-details-popup-id="osItemDetailsPopupAgent_<?php echo esc_attr( $agent->id ); ?>"> |
| 191 | <span><?php esc_html_e( 'Learn More', 'latepoint' ); ?></span></div> |
| 192 | <?php } ?> |
| 193 | </div> |
| 194 | </div> |
| 195 | <?php } ?> |
| 196 | </div> |
| 197 | <?php |
| 198 | if ( $show_agent_bio ) { |
| 199 | foreach ( $agents as $agent ) { |
| 200 | echo OsAgentHelper::generate_bio( $agent ); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | public static function generate_bio( OsAgentModel $agent ) { |
| 207 | $html = ''; |
| 208 | $agent_features_html = ''; |
| 209 | foreach ( $agent->features_arr as $feature ) { |
| 210 | $agent_features_html .= '<div class="item-details-popup-feature"> |
| 211 | <div class="item-details-popup-feature-value">' . esc_html( $feature['value'] ) . '</div> |
| 212 | <div class="item-details-popup-feature-label">' . esc_html( $feature['label'] ) . '</div> |
| 213 | </div>'; |
| 214 | } |
| 215 | $html .= '<div class="os-item-details-popup" id="osItemDetailsPopupAgent_' . $agent->id . '"> |
| 216 | <a href="#" class="os-item-details-popup-close"><span>' . __( 'Close Details', 'latepoint' ) . '</span><i class="latepoint-icon latepoint-icon-common-01"></i></a> |
| 217 | <div class="os-item-details-popup-inner"> |
| 218 | <div class="item-details-popup-head" style="background-image: url(' . esc_url( $agent->bio_image_url ) . ')"> |
| 219 | <h3>' . esc_html( $agent->name_for_front ) . '</h3> |
| 220 | <div class="item-details-popup-title">' . esc_html( $agent->title ) . '</div> |
| 221 | </div> |
| 222 | <div class="item-details-popup-content"> |
| 223 | <img class="bio-curve" src="' . LATEPOINT_IMAGES_URL . 'white-curve.png" alt=""> |
| 224 | <div class="item-details-popup-features">' . $agent_features_html . '</div> |
| 225 | <div class="item-details-popup-content-i"> |
| 226 | ' . esc_html( $agent->bio ) . ' |
| 227 | </div> |
| 228 | </div> |
| 229 | </div> |
| 230 | </div>'; |
| 231 | |
| 232 | return $html; |
| 233 | } |
| 234 | |
| 235 | public static function generate_day_schedule_info( $filter ) { |
| 236 | $today_date = new OsWpDateTime( 'today' ); |
| 237 | $target_date = new OsWpDateTime( $filter->date_from ); ?> |
| 238 | <div class="agent-schedule-info"> |
| 239 | <div class="agent-today-info"> |
| 240 | <?php echo ( $target_date->format( 'Y-m-d' ) == $today_date->format( 'Y-m-d' ) ) ? esc_html__( 'Today', 'latepoint' ) : esc_html( $target_date->format( OsSettingsHelper::get_readable_date_format() ) ); ?> |
| 241 | <?php |
| 242 | |
| 243 | $booking_request = new \LatePoint\Misc\BookingRequest(); |
| 244 | $booking_request->agent_id = $filter->agent_id; |
| 245 | $booking_request->start_date = $target_date->format( 'Y-m-d' ); |
| 246 | $resources = OsResourceHelper::get_resources_grouped_by_day( $booking_request, $target_date, $target_date ); |
| 247 | |
| 248 | $day_work_periods = []; |
| 249 | |
| 250 | $periods = []; |
| 251 | foreach ( $resources[ $target_date->format( 'Y-m-d' ) ] as $resource ) { |
| 252 | if ( ! empty( $resource->work_time_periods ) ) { |
| 253 | foreach ( $resource->work_time_periods as $work_time_period ) { |
| 254 | if ( $work_time_period->start_time == $work_time_period->end_time ) { |
| 255 | continue; |
| 256 | } |
| 257 | $periods[] = $work_time_period->start_time . ':' . $work_time_period->end_time; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | $periods = array_unique( $periods ); |
| 262 | foreach ( $periods as $work_time_period ) { |
| 263 | $period = explode( ':', $work_time_period ); |
| 264 | $work_time_period = new \LatePoint\Misc\WorkPeriod(); |
| 265 | $work_time_period->start_time = $period[0]; |
| 266 | $work_time_period->end_time = $period[1]; |
| 267 | $day_work_periods[] = $work_time_period; |
| 268 | } |
| 269 | |
| 270 | $is_working_today = ! empty( $day_work_periods ); |
| 271 | ?> |
| 272 | <span class="today-status <?php echo ( $is_working_today ) ? 'is-on-duty' : 'is-off-duty'; ?>"><?php echo ( $is_working_today ) ? esc_html__( 'On Duty', 'latepoint' ) : esc_html__( 'Off Duty', 'latepoint' ); ?></span> |
| 273 | <div class="today-schedule"> |
| 274 | <?php if ( $is_working_today ) { ?> |
| 275 | <?php foreach ( $day_work_periods as $period ) { |
| 276 | echo '<span>' . esc_html( OsTimeHelper::minutes_to_hours_and_minutes( $period->start_time ) . ' - ' . OsTimeHelper::minutes_to_hours_and_minutes( $period->end_time ) ) . '</span>'; |
| 277 | } ?> |
| 278 | <?php } else { |
| 279 | esc_html_e( 'Not Available', 'latepoint' ); |
| 280 | } ?> |
| 281 | </div> |
| 282 | </div> |
| 283 | <div class="today-bookings"> |
| 284 | <?php esc_html_e( 'Bookings', 'latepoint' ); ?> |
| 285 | <div class="today-bookings-count"><?php echo esc_html( OsBookingHelper::count_bookings( $filter ) ); ?></div> |
| 286 | </div> |
| 287 | </div> |
| 288 | <?php |
| 289 | } |
| 290 | |
| 291 | public static function get_full_name( $agent ) { |
| 292 | return join( ' ', array( $agent->first_name, $agent->last_name ) ); |
| 293 | } |
| 294 | |
| 295 | |
| 296 | public static function get_agent_ids_for_service_and_location( $service_id = false, $location_id = false ): array { |
| 297 | $all_agent_ids = OsConnectorHelper::get_connected_object_ids( |
| 298 | 'agent_id', |
| 299 | [ |
| 300 | 'service_id' => $service_id, |
| 301 | 'location_id' => $location_id, |
| 302 | ] |
| 303 | ); |
| 304 | $agents = new OsAgentModel(); |
| 305 | $active_agent_ids = $agents->select( 'id' )->should_be_active()->get_results( ARRAY_A ); |
| 306 | if ( $active_agent_ids ) { |
| 307 | $active_agent_ids = array_column( $active_agent_ids, 'id' ); |
| 308 | $all_agent_ids = array_intersect( $active_agent_ids, $all_agent_ids ); |
| 309 | } else { |
| 310 | $all_agent_ids = []; |
| 311 | } |
| 312 | |
| 313 | return $all_agent_ids; |
| 314 | } |
| 315 | |
| 316 | |
| 317 | /** |
| 318 | * @param bool $filter_allowed_records |
| 319 | * @param array $agent_ids |
| 320 | * |
| 321 | * @return array |
| 322 | */ |
| 323 | public static function get_agents_list( bool $filter_allowed_records = false, array $agent_ids = [], bool $exclude_disabled = false ): array { |
| 324 | $agents = new OsAgentModel(); |
| 325 | if ( $filter_allowed_records ) { |
| 326 | $agents->filter_allowed_records(); |
| 327 | } |
| 328 | |
| 329 | if ( ! empty( $agent_ids ) ) { |
| 330 | $agents->where_in( 'id', $agent_ids ); |
| 331 | } |
| 332 | |
| 333 | if ( $exclude_disabled ) { |
| 334 | $agents->where( [ 'status' => LATEPOINT_AGENT_STATUS_ACTIVE ] ); |
| 335 | } |
| 336 | |
| 337 | $agents = $agents->order_by( 'status asc, first_name asc, last_name asc' )->get_results_as_models(); |
| 338 | $agents_list = []; |
| 339 | if ( $agents ) { |
| 340 | foreach ( $agents as $agent ) { |
| 341 | $label = ( $agent->status == LATEPOINT_LOCATION_STATUS_DISABLED ) ? ( $agent->full_name . ' [' . esc_html__( 'Disabled', 'latepoint' ) . ']' ) : $agent->full_name; |
| 342 | $agents_list[] = [ |
| 343 | 'value' => $agent->id, |
| 344 | 'label' => $label, |
| 345 | ]; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | return $agents_list; |
| 350 | } |
| 351 | |
| 352 | public static function get_avatar_url( $agent ) { |
| 353 | $default_avatar = LATEPOINT_DEFAULT_AVATAR_URL; |
| 354 | |
| 355 | return OsImageHelper::get_image_url_by_id( $agent->avatar_image_id, 'thumbnail', $default_avatar ); |
| 356 | } |
| 357 | |
| 358 | public static function get_bio_image_url( $agent ) { |
| 359 | $default_bio_image = LATEPOINT_DEFAULT_AVATAR_URL; |
| 360 | |
| 361 | return OsImageHelper::get_image_url_by_id( $agent->bio_image_id, 'large', $default_bio_image ); |
| 362 | } |
| 363 | } |