| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_Current_Agent_Profile' ) ) : |
| 7 |
|
| 8 |
final class WPSC_Current_Agent_Profile { |
| 9 |
|
| 10 |
/** |
| 11 |
* Initialize this class |
| 12 |
*/ |
| 13 |
public static function init() { |
| 14 |
|
| 15 |
// Get aget profile layout. |
| 16 |
add_action( 'wp_ajax_wpsc_get_agent_profile', array( __CLASS__, 'get_agent_profile' ) ); |
| 17 |
add_action( 'wp_ajax_nopriv_wpsc_get_agent_profile', array( __CLASS__, 'get_agent_profile' ) ); |
| 18 |
|
| 19 |
// Set agent general settings. |
| 20 |
add_action( 'wp_ajax_wpsc_set_agent_settings', array( __CLASS__, 'set_agent_settings' ) ); |
| 21 |
add_action( 'wp_ajax_nopriv_wpsc_set_agent_settings', array( __CLASS__, 'set_agent_settings' ) ); |
| 22 |
|
| 23 |
// Get agent leaves. |
| 24 |
add_action( 'wp_ajax_wpsc_get_ap_leaves_actions', array( __CLASS__, 'get_leaves_actions' ) ); |
| 25 |
add_action( 'wp_ajax_nopriv_wpsc_get_ap_leaves_actions', array( __CLASS__, 'get_leaves_actions' ) ); |
| 26 |
|
| 27 |
// Set agent leaves. |
| 28 |
add_action( 'wp_ajax_wpsc_set_ap_leaves_actions', array( __CLASS__, 'set_leaves_actions' ) ); |
| 29 |
add_action( 'wp_ajax_nopriv_wpsc_set_ap_leaves_actions', array( __CLASS__, 'set_leaves_actions' ) ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get current agent profile layout |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public static function get_agent_profile() { |
| 38 |
|
| 39 |
$current_user = WPSC_Current_User::$current_user; |
| 40 |
if ( ! $current_user->is_agent ) { |
| 41 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 42 |
} |
| 43 |
|
| 44 |
$menu_tabs = array( |
| 45 |
'general' => array( |
| 46 |
'slug' => 'general', |
| 47 |
'icon' => 'control', |
| 48 |
'label' => esc_attr__( 'General Settings', 'supportcandy' ), |
| 49 |
'callback' => 'get_general_settings', |
| 50 |
), |
| 51 |
); |
| 52 |
|
| 53 |
$wh_settings = get_option( 'wpsc-wh-settings' ); |
| 54 |
|
| 55 |
if ( $wh_settings['allow-agent-modify-wh'] ) { |
| 56 |
$menu_tabs['working-hrs'] = array( |
| 57 |
'slug' => 'working-hrs', |
| 58 |
'icon' => 'clock', |
| 59 |
'label' => esc_attr__( 'Working hours', 'supportcandy' ), |
| 60 |
'callback' => 'get_working_hrs', |
| 61 |
); |
| 62 |
|
| 63 |
$menu_tabs['exceptions'] = array( |
| 64 |
'slug' => 'exceptions', |
| 65 |
'icon' => 'clock', |
| 66 |
'label' => esc_attr__( 'Exceptions', 'supportcandy' ), |
| 67 |
'callback' => 'get_exceptions', |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
if ( $wh_settings['allow-agent-modify-leaves'] ) { |
| 72 |
$menu_tabs['leaves'] = array( |
| 73 |
'slug' => 'leaves', |
| 74 |
'icon' => 'calendar-times', |
| 75 |
'label' => esc_attr__( 'Leaves', 'supportcandy' ), |
| 76 |
'callback' => 'get_leaves', |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
$menu_tabs = apply_filters( 'wpsc_ap_menu_items', $menu_tabs ); |
| 81 |
?> |
| 82 |
<div class="wpsc-tff wpsc-xs-12 wpsc-sm-12 wpsc-md-12 wpsc-lg-12"> |
| 83 |
<div class="wpsc-up-container"> |
| 84 |
<div class="wpsc-up-picture img"> |
| 85 |
<?php echo get_avatar( $current_user->customer->email, 50 ); ?> |
| 86 |
</div> |
| 87 |
<div class="wpsc-up-info"> |
| 88 |
<div class="wpsc-up-name"><?php echo esc_attr( $current_user->customer->name ); ?></div> |
| 89 |
<div class="wpsc-up-email"><?php echo esc_attr( $current_user->customer->email ); ?></div> |
| 90 |
</div> |
| 91 |
</div> |
| 92 |
</div> |
| 93 |
<div class="wpsc-up-section" style="margin: 0px 20px;"> |
| 94 |
<div class="wpsc-up-tab"> |
| 95 |
<?php |
| 96 |
foreach ( $menu_tabs as $tab_key => $tab_menu ) { |
| 97 |
?> |
| 98 |
<label class="wpsc-profile-tab <?php echo ( $tab_key === 'general' ) ? 'active' : ''; ?>" data-toggle-target="wpsc-up-<?php echo esc_attr( $tab_key ); ?>"><?php echo esc_attr( $tab_menu['label'] ); ?></label> |
| 99 |
<?php |
| 100 |
} |
| 101 |
?> |
| 102 |
</div> |
| 103 |
<hr> |
| 104 |
<section> |
| 105 |
<?php |
| 106 |
foreach ( $menu_tabs as $section_key => $section_menu ) { |
| 107 |
?> |
| 108 |
<div class="wpsc-up-content wpsc-up-<?php echo esc_attr( $section_key ); ?> <?php echo ( $section_key === 'general' ) ? 'active' : ''; ?>"> |
| 109 |
<?php echo esc_attr( self::{$section_menu['callback']}() ); ?> |
| 110 |
</div> |
| 111 |
<?php |
| 112 |
} |
| 113 |
?> |
| 114 |
</section> |
| 115 |
</div> |
| 116 |
<script> |
| 117 |
jQuery('.wpsc-profile-tab').on('click', function(event) { |
| 118 |
|
| 119 |
event.preventDefault(); |
| 120 |
if ( jQuery(this).hasClass('active') ) return; |
| 121 |
|
| 122 |
jQuery(this).toggleClass('active'); |
| 123 |
var temp = jQuery(this).data('toggle-target'); |
| 124 |
jQuery('.wpsc-up-content').removeClass('active').filter('.'+temp).addClass('active'); |
| 125 |
jQuery('.wpsc-profile-tab').not(this).removeClass('active'); |
| 126 |
if( temp == 'wpsc-up-leaves' ) { |
| 127 |
wpsc_render_leaves_calender(); |
| 128 |
} |
| 129 |
}); |
| 130 |
</script> |
| 131 |
<?php |
| 132 |
wp_die(); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Get general settings |
| 137 |
* |
| 138 |
* @return void |
| 139 |
*/ |
| 140 |
public static function get_general_settings() { |
| 141 |
|
| 142 |
$current_user = WPSC_Current_User::$current_user; |
| 143 |
$editor = $current_user->agent->get_signature_editor(); |
| 144 |
$editor = $editor ? $editor : 'html'; |
| 145 |
$rich_editing = get_user_meta( $current_user->user->ID, 'rich_editing', true ); |
| 146 |
$rich_editing = filter_var( $rich_editing, FILTER_VALIDATE_BOOLEAN ); |
| 147 |
|
| 148 |
if ( ! $current_user->is_agent ) { |
| 149 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 150 |
} |
| 151 |
|
| 152 |
$default_filters = get_option( 'wpsc-atl-default-filters' ); |
| 153 |
$saved_filters = $current_user->get_saved_filters(); |
| 154 |
$default_filter = $current_user->agent->get_default_filter(); |
| 155 |
$tab = $current_user->agent->get_default_tab(); |
| 156 |
?> |
| 157 |
<form class="wpsc-agent-settings" onsubmit="return false;" action="#"> |
| 158 |
<div class="wpsc-tff wpsc-xs-12 wpsc-sm-12 wpsc-md-12 wpsc-lg-12"> |
| 159 |
<div class="wpsc-tff-label"> |
| 160 |
<span class="name"><?php esc_attr_e( 'Signature', 'supportcandy' ); ?></span> |
| 161 |
</div> |
| 162 |
<span class="extra-info"><?php esc_attr_e( 'Signature used for emails', 'supportcandy' ); ?></span> |
| 163 |
<div class = "textarea-container"> |
| 164 |
<?php |
| 165 |
if ( $rich_editing ) { |
| 166 |
?> |
| 167 |
<div class = "wpsc_tinymce_editor_btns"> |
| 168 |
<div class="inner-container"> |
| 169 |
<button class="visual wpsc-switch-editor <?php echo esc_attr( $editor ) == 'html' ? 'active' : ''; ?>" type="button" onclick="wpsc_get_tinymce(this, 'signature-html', 'signature_body');"><?php esc_attr_e( 'Visual', 'supportcandy' ); ?></button> |
| 170 |
<button class="text wpsc-switch-editor <?php echo esc_attr( $editor ) == 'text' ? 'active' : ''; ?>" type="button" onclick="wpsc_get_textarea(this, 'signature-html')"><?php esc_attr_e( 'Text', 'supportcandy' ); ?></button> |
| 171 |
</div> |
| 172 |
</div> |
| 173 |
<?php |
| 174 |
} |
| 175 |
?> |
| 176 |
<textarea name="signature-html" id="signature-html" class="wpsc_textarea"><?php echo esc_attr( stripslashes( $current_user->agent->get_signature() ) ); ?></textarea> |
| 177 |
</div> |
| 178 |
</div> |
| 179 |
<div class="wpsc-tff wpsc-xs-12 wpsc-sm-12 wpsc-md-12 wpsc-lg-12"> |
| 180 |
<div class="wpsc-tff-label"> |
| 181 |
<span class="name"><?php esc_attr_e( 'Default filter', 'supportcandy' ); ?></span> |
| 182 |
</div> |
| 183 |
<span class="extra-info"><?php esc_attr_e( 'Default filter for ticket list', 'supportcandy' ); ?></span> |
| 184 |
<select name="default-filter"> |
| 185 |
<optgroup label="<?php esc_attr_e( 'Default filters', 'supportcandy' ); ?>"> |
| 186 |
<?php |
| 187 |
foreach ( $default_filters as $index => $filter ) : |
| 188 |
if ( ! $filter['is_enable'] ) { |
| 189 |
continue; |
| 190 |
} |
| 191 |
$selected = $default_filter == $index || $default_filter == 'default-' . $index ? 'selected' : ''; |
| 192 |
?> |
| 193 |
<option <?php echo esc_attr( $selected ); ?> value="<?php echo is_numeric( $index ) ? 'default-' . esc_attr( $index ) : esc_attr( $index ); ?>"> |
| 194 |
<?php |
| 195 |
$filter_label = $filter['label'] ? WPSC_Translations::get( 'wpsc-atl-' . $index, stripslashes( $filter['label'] ) ) : stripslashes( $filter['label'] ); |
| 196 |
echo esc_attr( $filter_label ); |
| 197 |
?> |
| 198 |
</option> |
| 199 |
<?php |
| 200 |
endforeach; |
| 201 |
?> |
| 202 |
</optgroup> |
| 203 |
<optgroup label="<?php esc_attr_e( 'Saved filters', 'supportcandy' ); ?>"> |
| 204 |
<?php |
| 205 |
foreach ( $saved_filters as $index => $filter ) : |
| 206 |
?> |
| 207 |
<option <?php selected( $default_filter, 'saved-' . $index ); ?> value="saved-<?php echo esc_attr( $index ); ?>"><?php echo esc_attr( $filter['label'] ); ?></option> |
| 208 |
<?php |
| 209 |
endforeach; |
| 210 |
?> |
| 211 |
</optgroup> |
| 212 |
</select> |
| 213 |
</div> |
| 214 |
<div class="wpsc-tff wpsc-xs-12 wpsc-sm-12 wpsc-md-12 wpsc-lg-12"> |
| 215 |
<div class="wpsc-tff-label"> |
| 216 |
<span class="name"><?php esc_attr_e( 'Default tab', 'supportcandy' ); ?></span> |
| 217 |
</div> |
| 218 |
<span class="extra-info"><?php esc_attr_e( 'Select default section', 'supportcandy' ); ?></span> |
| 219 |
<select id="wpsc-dt" name="default-tab"> |
| 220 |
<option <?php selected( $tab, 'dashboard' ); ?> value="dashboard"><?php esc_attr_e( 'Dashboard', 'supportcandy' ); ?></option> |
| 221 |
<option <?php selected( $tab, 'ticket-list' ); ?> value="ticket-list"><?php esc_attr_e( 'Ticket list', 'supportcandy' ); ?></option> |
| 222 |
<option <?php selected( $tab, 'new-ticket' ); ?> value="new-ticket"><?php esc_attr_e( 'New ticket', 'supportcandy' ); ?></option> |
| 223 |
</select> |
| 224 |
</div> |
| 225 |
<div class="wpsc-tff wpsc-xs-12 wpsc-sm-12 wpsc-md-12 wpsc-lg-12"> |
| 226 |
<div class="submit-container"> |
| 227 |
<button class="wpsc-button normal primary" onclick="wpsc_set_agent_settings(this, '<?php echo esc_attr( wp_create_nonce( 'wpsc_set_agent_settings' ) ); ?>');"><?php esc_attr_e( 'Save Changes', 'supportcandy' ); ?></button> |
| 228 |
</div> |
| 229 |
</div> |
| 230 |
<input type="hidden" name="action" value="wpsc_set_agent_settings"> |
| 231 |
<input type="hidden" id="editor" name="editor" value="<?php echo esc_attr( $editor ); ?>"> |
| 232 |
<input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_agent_settings' ) ); ?>"> |
| 233 |
|
| 234 |
<script> |
| 235 |
<?php |
| 236 |
if ( $editor == 'html' && $rich_editing ) : |
| 237 |
?> |
| 238 |
jQuery('.wpsc-switch-editor.visual').trigger('click'); |
| 239 |
<?php |
| 240 |
endif; |
| 241 |
?> |
| 242 |
|
| 243 |
function wpsc_get_tinymce(el, selector, body_id){ |
| 244 |
jQuery(el).parent().find('.text').removeClass('active'); |
| 245 |
jQuery(el).addClass('active'); |
| 246 |
tinymce.remove('#'+selector); |
| 247 |
tinymce.init({ |
| 248 |
selector:'#'+selector, |
| 249 |
body_id: body_id, |
| 250 |
menubar: false, |
| 251 |
statusbar: false, |
| 252 |
height : '200', |
| 253 |
plugins: [ |
| 254 |
'lists link image directionality' |
| 255 |
], |
| 256 |
image_advtab: true, |
| 257 |
toolbar: 'bold italic underline blockquote | alignleft aligncenter alignright | bullist numlist | rtl | link image', |
| 258 |
directionality: '<?php echo is_rtl() ? 'rtl' : 'ltr'; ?>', |
| 259 |
branding: false, |
| 260 |
autoresize_bottom_margin: 20, |
| 261 |
browser_spellcheck : true, |
| 262 |
relative_urls : false, |
| 263 |
remove_script_host : false, |
| 264 |
convert_urls : true, |
| 265 |
setup: function (editor) { |
| 266 |
} |
| 267 |
}); |
| 268 |
jQuery('#editor').val('html'); |
| 269 |
} |
| 270 |
function wpsc_get_textarea(el, selector){ |
| 271 |
jQuery(el).parent().find('.visual').removeClass('active'); |
| 272 |
jQuery(el).addClass('active'); |
| 273 |
tinymce.remove('#'+selector); |
| 274 |
jQuery('#editor').val('text'); |
| 275 |
} |
| 276 |
function wpsc_set_agent_settings(el, nonce) { |
| 277 |
var dataform = new FormData(jQuery('form.wpsc-agent-settings')[0]); |
| 278 |
var is_tinymce = (typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden(); |
| 279 |
var signature = is_tinymce ? tinyMCE.activeEditor.getContent().trim() : jQuery('#signature-html').val(); |
| 280 |
dataform.append('signature', signature); |
| 281 |
dataform.append('_ajax_nonce', nonce); |
| 282 |
jQuery(el).text(supportcandy.translations.please_wait); |
| 283 |
jQuery.ajax({ |
| 284 |
url: supportcandy.ajax_url, |
| 285 |
type: 'POST', |
| 286 |
data: dataform, |
| 287 |
processData: false, |
| 288 |
contentType: false, |
| 289 |
error: function (res) { |
| 290 |
window.location.reload(); |
| 291 |
}, |
| 292 |
success: function (res, textStatus, xhr) { |
| 293 |
window.location.reload(); |
| 294 |
} |
| 295 |
}); |
| 296 |
} |
| 297 |
</script> |
| 298 |
</form> |
| 299 |
<?php |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Set agent general settings |
| 304 |
* |
| 305 |
* @return void |
| 306 |
*/ |
| 307 |
public static function set_agent_settings() { |
| 308 |
|
| 309 |
if ( check_ajax_referer( 'wpsc_set_agent_settings', '_ajax_nonce', false ) != 1 ) { |
| 310 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 311 |
} |
| 312 |
|
| 313 |
$current_user = WPSC_Current_User::$current_user; |
| 314 |
if ( ! $current_user->is_agent ) { |
| 315 |
wp_send_json_error( new WP_Error( '001', 'Unauthorized' ), 401 ); |
| 316 |
} |
| 317 |
|
| 318 |
$signature = isset( $_POST['signature'] ) ? wp_kses_post( wp_unslash( $_POST['signature'] ) ) : ''; |
| 319 |
|
| 320 |
// replace new line with br if no text editor. |
| 321 |
$editor = isset( $_POST['editor'] ) ? sanitize_text_field( wp_unslash( $_POST['editor'] ) ) : 'html'; |
| 322 |
$current_user->agent->set_signature_editor( $editor ); |
| 323 |
$current_user->agent->set_signature( $signature ); |
| 324 |
|
| 325 |
$default_filter = isset( $_POST['default-filter'] ) ? sanitize_text_field( wp_unslash( $_POST['default-filter'] ) ) : 'all'; |
| 326 |
$current_user->agent->set_default_filter( $default_filter ); |
| 327 |
|
| 328 |
$default_tab = isset( $_POST['default-tab'] ) ? sanitize_text_field( wp_unslash( $_POST['default-tab'] ) ) : 'dashboard'; |
| 329 |
$current_user->agent->set_default_tab( $default_tab ); |
| 330 |
wp_die(); |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Get working hours |
| 335 |
* |
| 336 |
* @return void |
| 337 |
*/ |
| 338 |
public static function get_working_hrs() { |
| 339 |
|
| 340 |
$settings = get_option( 'wpsc-wh-settings', array() ); |
| 341 |
|
| 342 |
$current_user = WPSC_Current_User::$current_user; |
| 343 |
if ( ! ( |
| 344 |
WPSC_Functions::is_site_admin() || |
| 345 |
( $current_user->is_agent && $settings['allow-agent-modify-wh'] ) |
| 346 |
) ) { |
| 347 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 348 |
} |
| 349 |
|
| 350 |
$working_hrs = $current_user->agent->get_working_hrs(); |
| 351 |
?> |
| 352 |
|
| 353 |
<form action="#" onsubmit="return false;" class="wpsc-frm-agent-wh"> |
| 354 |
<table class="wpsc-working-hrs"> |
| 355 |
<?php |
| 356 |
for ( $i = 1; $i <= 7; $i++ ) : |
| 357 |
$start_time = $working_hrs[ $i ]->start_time; |
| 358 |
$end_time = $working_hrs[ $i ]->end_time; |
| 359 |
$style = $start_time == 'off' ? 'display: none;' : ''; |
| 360 |
?> |
| 361 |
<tr> |
| 362 |
<td class="dayName"><?php echo esc_attr( WPSC_Functions::get_day_name( $i ) ); ?>:</td> |
| 363 |
<td> |
| 364 |
<select class="wpsc-wh-start-time" name="wh[<?php echo esc_attr( $i ); ?>][start_time]"> |
| 365 |
<?php WPSC_Working_Hour::get_start_time_slots( $start_time ); ?> |
| 366 |
</select> |
| 367 |
</td> |
| 368 |
<td style="<?php echo esc_attr( $style ); ?>">-</td> |
| 369 |
<td style="<?php echo esc_attr( $style ); ?>"> |
| 370 |
<select class="wpsc-wh-end-time" name="wh[<?php echo esc_attr( $i ); ?>][end_time]"> |
| 371 |
<?php WPSC_Working_Hour::get_end_time_slots( $start_time, $end_time ); ?> |
| 372 |
</select> |
| 373 |
</td> |
| 374 |
</tr> |
| 375 |
<?php |
| 376 |
endfor; |
| 377 |
?> |
| 378 |
</table> |
| 379 |
<input type="hidden" name="action" value="wpsc_set_agent_wh_hrs"> |
| 380 |
<input type="hidden" name="agent_id" value="<?php echo esc_attr( $current_user->agent->id ); ?>"> |
| 381 |
<input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_agent_wh_hrs' ) ); ?>"> |
| 382 |
</form> |
| 383 |
<div class="setting-footer-actions"> |
| 384 |
<button |
| 385 |
class="wpsc-button normal primary margin-right" |
| 386 |
onclick="wpsc_set_agent_wh_hrs(this);"> |
| 387 |
<?php esc_attr_e( 'Submit', 'supportcandy' ); ?> |
| 388 |
</button> |
| 389 |
</div> |
| 390 |
<script> |
| 391 |
var end_times = []; |
| 392 |
<?php |
| 393 |
$current_slot = new DateTime( '2020-01-01 00:15:00' ); |
| 394 |
$second_last_slot = new DateTime( '2020-01-01 23:45:00' ); |
| 395 |
$last_slot = new DateTime( '2020-01-01 23:59:59' ); |
| 396 |
|
| 397 |
do { |
| 398 |
$time = $current_slot->format( 'H:i:s' ) |
| 399 |
?> |
| 400 |
end_times.push({ |
| 401 |
val: '<?php echo esc_attr( $time ); ?>', |
| 402 |
display_val: '<?php echo esc_attr( $current_slot->format( 'H:i' ) ); ?>', |
| 403 |
}); |
| 404 |
<?php |
| 405 |
if ( $current_slot == $second_last_slot ) { |
| 406 |
$current_slot->add( new DateInterval( 'PT14M59S' ) ); |
| 407 |
} else { |
| 408 |
$current_slot->add( new DateInterval( 'PT15M' ) ); |
| 409 |
} |
| 410 |
} while ( $current_slot <= $last_slot ); |
| 411 |
?> |
| 412 |
supportcandy.temp.end_times = end_times; |
| 413 |
|
| 414 |
// Change event |
| 415 |
jQuery('.wpsc-wh-start-time').change(function(){ |
| 416 |
var start_time = jQuery(this).val(); |
| 417 |
var td1 = jQuery(this).parent().next(); |
| 418 |
var td2 = td1.next(); |
| 419 |
if (start_time === 'off') { |
| 420 |
td1.hide(); |
| 421 |
td2.hide(); |
| 422 |
return; |
| 423 |
} else { |
| 424 |
td1.show(); |
| 425 |
td2.show(); |
| 426 |
} |
| 427 |
var tempArr = start_time.split(":"); |
| 428 |
var startDate = new Date(2020, 0, 1, tempArr[0], tempArr[1], tempArr[2]); |
| 429 |
var cmbEndTime = jQuery(this).closest('tr').find('.wpsc-wh-end-time'); |
| 430 |
cmbEndTime.find('option').remove(); |
| 431 |
jQuery.each(supportcandy.temp.end_times, function(index, end_time){ |
| 432 |
var tempArr = end_time.val.split(":"); |
| 433 |
var endDate = new Date(2020, 0, 1, tempArr[0], tempArr[1], tempArr[2]); |
| 434 |
if (startDate < endDate) { |
| 435 |
var obj = document.createElement('OPTION'); |
| 436 |
var displayVal = document.createTextNode(end_time.display_val); |
| 437 |
obj.setAttribute("value", end_time.val); |
| 438 |
obj.appendChild(displayVal); |
| 439 |
cmbEndTime.append(obj); |
| 440 |
} |
| 441 |
}); |
| 442 |
}); |
| 443 |
</script> |
| 444 |
<?php |
| 445 |
} |
| 446 |
|
| 447 |
/** |
| 448 |
* Get exceptions |
| 449 |
* |
| 450 |
* @return void |
| 451 |
*/ |
| 452 |
public static function get_exceptions() { |
| 453 |
|
| 454 |
$settings = get_option( 'wpsc-wh-settings', array() ); |
| 455 |
$current_user = WPSC_Current_User::$current_user; |
| 456 |
if ( ! ( |
| 457 |
WPSC_Functions::is_site_admin() || |
| 458 |
( $current_user->is_agent && $settings['allow-agent-modify-wh'] ) |
| 459 |
) ) { |
| 460 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 461 |
} |
| 462 |
|
| 463 |
$exceptions = $current_user->agent->get_wh_exceptions(); |
| 464 |
$unique_id = uniqid( 'wpsc_' ); |
| 465 |
?> |
| 466 |
<div class="wpsc-exceptions-container" style="padding: 15px !important;"> |
| 467 |
<table class="wpsc-setting-tbl <?php echo esc_attr( $unique_id ); ?>"> |
| 468 |
<thead> |
| 469 |
<tr> |
| 470 |
<th><?php esc_attr_e( 'Title', 'supportcandy' ); ?></th> |
| 471 |
<th><?php esc_attr_e( 'Date', 'supportcandy' ); ?></th> |
| 472 |
<th><?php esc_attr_e( 'Schedule', 'supportcandy' ); ?></th> |
| 473 |
<th><?php esc_attr_e( 'Actions', 'supportcandy' ); ?></th> |
| 474 |
</tr> |
| 475 |
</thead> |
| 476 |
<tbody> |
| 477 |
<?php |
| 478 |
foreach ( $exceptions as $exception ) { |
| 479 |
?> |
| 480 |
<tr data-id="<?php echo esc_attr( $exception->id ); ?>"> |
| 481 |
<td><?php echo esc_attr( $exception->title ); ?></td> |
| 482 |
<td><?php echo esc_attr( $exception->exception_date->format( 'F d, Y' ) ); ?></td> |
| 483 |
<td> |
| 484 |
<?php |
| 485 |
$start_time = explode( ':', $exception->start_time ); |
| 486 |
$start_time = $start_time[0] . ':' . $start_time[1]; |
| 487 |
$end_time = explode( ':', $exception->end_time ); |
| 488 |
$end_time = $end_time[0] . ':' . $end_time[1]; |
| 489 |
/* translators: %1$s: start time, %2$s: end time e.g. 04:00 - 05:00 */ |
| 490 |
printf( esc_attr__( '%1$s - %2$s', 'supportcandy' ), esc_attr( $start_time ), esc_attr( $end_time ) ); |
| 491 |
?> |
| 492 |
</td> |
| 493 |
<td> |
| 494 |
<a class="wpsc-link"><span class="edit <?php echo esc_attr( $unique_id ); ?>"><?php esc_attr_e( 'Edit', 'supportcandy' ); ?></span></a> | |
| 495 |
<a class="wpsc-link"><span class="delete <?php echo esc_attr( $unique_id ); ?>"><?php esc_attr_e( 'Delete', 'supportcandy' ); ?></span></a> |
| 496 |
</td> |
| 497 |
</tr> |
| 498 |
<?php |
| 499 |
} |
| 500 |
?> |
| 501 |
</tbody> |
| 502 |
</table> |
| 503 |
<script> |
| 504 |
// Add datatable |
| 505 |
jQuery('table.<?php echo esc_attr( $unique_id ); ?>').DataTable({ |
| 506 |
ordering: false, |
| 507 |
pageLength: 20, |
| 508 |
bLengthChange: false, |
| 509 |
columnDefs: [ |
| 510 |
{ targets: -1, searchable: false }, |
| 511 |
{ targets: '_all', className: 'dt-left' } |
| 512 |
], |
| 513 |
layout: { |
| 514 |
topStart: { |
| 515 |
buttons: [ |
| 516 |
{ |
| 517 |
text: '<?php echo esc_attr( wpsc__( 'Add new', 'supportcandy' ) ); ?>', |
| 518 |
className: 'wpsc-button small primary', |
| 519 |
action: function ( e, dt, node, config ) { |
| 520 |
jQuery('.wpsc-exceptions-container').html(supportcandy.loader_html); |
| 521 |
var data = { |
| 522 |
action: 'wpsc_get_add_agent_wh_exception', |
| 523 |
agent_id: <?php echo esc_attr( $current_user->agent->id ); ?>, |
| 524 |
_ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_add_agent_wh_exception' ) ); ?>' |
| 525 |
}; |
| 526 |
jQuery.post(supportcandy.ajax_url, data, function (response) { |
| 527 |
jQuery('.wpsc-exceptions-container').html(response); |
| 528 |
}); |
| 529 |
} |
| 530 |
} |
| 531 |
], |
| 532 |
}, |
| 533 |
}, |
| 534 |
language: supportcandy.translations.datatables |
| 535 |
}); |
| 536 |
// Edit. |
| 537 |
jQuery('span.edit.<?php echo esc_attr( $unique_id ); ?>').click(function(){ |
| 538 |
|
| 539 |
var exception_id = jQuery(this).closest('tr').data('id'); |
| 540 |
jQuery('.wpsc-exceptions-container').html(supportcandy.loader_html); |
| 541 |
var data = { |
| 542 |
action: 'wpsc_get_edit_agent_wh_exception', |
| 543 |
agent_id: <?php echo esc_attr( $current_user->agent->id ); ?>, |
| 544 |
exception_id, |
| 545 |
_ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_edit_agent_wh_exception' ) ); ?>' |
| 546 |
}; |
| 547 |
jQuery.post(supportcandy.ajax_url, data, function (res) { |
| 548 |
jQuery('.wpsc-exceptions-container').html(res); |
| 549 |
}); |
| 550 |
}); |
| 551 |
// Delete. |
| 552 |
jQuery('span.delete.<?php echo esc_attr( $unique_id ); ?>').click(function(){ |
| 553 |
|
| 554 |
var flag = confirm(supportcandy.translations.confirm); |
| 555 |
if (!flag) return; |
| 556 |
var exception_id = jQuery(this).closest('tr').data('id'); |
| 557 |
jQuery('.wpsc-exceptions-container').html(supportcandy.loader_html); |
| 558 |
var data = { |
| 559 |
action: 'wpsc_delete_agent_wh_exception', |
| 560 |
agent_id: <?php echo esc_attr( $current_user->agent->id ); ?>, |
| 561 |
exception_id, |
| 562 |
_ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_delete_agent_wh_exception' ) ); ?>' |
| 563 |
}; |
| 564 |
jQuery.post(supportcandy.ajax_url, data, function (res) { |
| 565 |
window.location.reload(); |
| 566 |
}); |
| 567 |
}); |
| 568 |
supportcandy.temp.uniqueId = '<?php echo esc_attr( $unique_id ); ?>'; |
| 569 |
</script> |
| 570 |
</div> |
| 571 |
<?php |
| 572 |
} |
| 573 |
|
| 574 |
/** |
| 575 |
* Get get leaves |
| 576 |
* |
| 577 |
* @return void |
| 578 |
*/ |
| 579 |
public static function get_leaves() { |
| 580 |
|
| 581 |
$current_user = WPSC_Current_User::$current_user; |
| 582 |
if ( ! $current_user->is_agent ) { |
| 583 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 584 |
} |
| 585 |
|
| 586 |
$wh_settings = get_option( 'wpsc-wh-settings' ); |
| 587 |
if ( ! $wh_settings['allow-agent-modify-leaves'] ) { |
| 588 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 589 |
} |
| 590 |
|
| 591 |
// get non-recurring holidays. |
| 592 |
$non_recurring_holidays = array(); |
| 593 |
$holidays = WPSC_Holiday::find( |
| 594 |
array( |
| 595 |
'meta_query' => array( |
| 596 |
'relation' => 'AND', |
| 597 |
array( |
| 598 |
'slug' => 'agent', |
| 599 |
'compare' => '=', |
| 600 |
'val' => $current_user->agent->id, |
| 601 |
), |
| 602 |
array( |
| 603 |
'slug' => 'is_recurring', |
| 604 |
'compare' => '=', |
| 605 |
'val' => 0, |
| 606 |
), |
| 607 |
), |
| 608 |
) |
| 609 |
)['results']; |
| 610 |
foreach ( $holidays as $holiday ) { |
| 611 |
$non_recurring_holidays[] = $holiday->holiday->format( 'Y-m-d' ); |
| 612 |
} |
| 613 |
|
| 614 |
// get recurring holidays. |
| 615 |
$recurring_holidays = array(); |
| 616 |
$holidays = WPSC_Holiday::find( |
| 617 |
array( |
| 618 |
'meta_query' => array( |
| 619 |
'relation' => 'AND', |
| 620 |
array( |
| 621 |
'slug' => 'agent', |
| 622 |
'compare' => '=', |
| 623 |
'val' => $current_user->agent->id, |
| 624 |
), |
| 625 |
array( |
| 626 |
'slug' => 'is_recurring', |
| 627 |
'compare' => '=', |
| 628 |
'val' => 1, |
| 629 |
), |
| 630 |
), |
| 631 |
) |
| 632 |
)['results']; |
| 633 |
foreach ( $holidays as $holiday ) { |
| 634 |
$recurring_holidays[] = $holiday->holiday->format( 'm-d' ); |
| 635 |
} |
| 636 |
|
| 637 |
$locale = explode( '_', get_locale() ); |
| 638 |
?> |
| 639 |
<div class="wpsc-leaves-container" style="padding: 15px !important;"> |
| 640 |
<div id="wpsc-calendar"></div> |
| 641 |
<script> |
| 642 |
supportcandy.temp.holidayList = { |
| 643 |
'nonRecurring': <?php echo wp_json_encode( $non_recurring_holidays ); ?>, |
| 644 |
'recurring': <?php echo wp_json_encode( $recurring_holidays ); ?> |
| 645 |
}; |
| 646 |
function wpsc_render_leaves_calender() { |
| 647 |
var calendarEl = document.getElementById('wpsc-calendar'); |
| 648 |
var calendar = new FullCalendar.Calendar(calendarEl, { |
| 649 |
initialView: 'dayGridMonth', |
| 650 |
selectable: true, |
| 651 |
locale: '<?php echo esc_attr( $locale[0] ); ?>', |
| 652 |
dayCellDidMount: function(args) { |
| 653 |
|
| 654 |
// non-recurring. |
| 655 |
var dateToCompare = args.date.toLocaleDateString('en-CA'); |
| 656 |
if (jQuery.inArray(dateToCompare, supportcandy.temp.holidayList.nonRecurring) != -1) { |
| 657 |
jQuery(args.el).css('background-color', '#f0932b'); |
| 658 |
} |
| 659 |
|
| 660 |
// recurring. |
| 661 |
var strArr = dateToCompare.split('-'); |
| 662 |
if (jQuery.inArray(strArr[1] + '-' + strArr[2], supportcandy.temp.holidayList.recurring) != -1) { |
| 663 |
jQuery(args.el).css('background-color', '#eb4d4b'); |
| 664 |
} |
| 665 |
|
| 666 |
}, |
| 667 |
select: function(info) { |
| 668 |
|
| 669 |
var start = info.start; |
| 670 |
var end = info.end; |
| 671 |
end.setDate(end.getDate()-1); |
| 672 |
|
| 673 |
var dateSelected = []; |
| 674 |
do { |
| 675 |
var d = start.toLocaleDateString('en-CA'); |
| 676 |
dateSelected.push(d); |
| 677 |
start.setDate(parseInt(start.getDate())+1); |
| 678 |
} while (start <= end); |
| 679 |
|
| 680 |
wpsc_get_ap_leaves_actions(dateSelected, '<?php echo esc_attr( wp_create_nonce( 'wpsc_get_ap_leaves_actions' ) ); ?>'); |
| 681 |
} |
| 682 |
}).render(); |
| 683 |
} |
| 684 |
</script> |
| 685 |
</div> |
| 686 |
<?php |
| 687 |
} |
| 688 |
|
| 689 |
/** |
| 690 |
* Load leaves actions for selected dates |
| 691 |
* |
| 692 |
* @return void |
| 693 |
*/ |
| 694 |
public static function get_leaves_actions() { |
| 695 |
|
| 696 |
if ( check_ajax_referer( 'wpsc_get_ap_leaves_actions', '_ajax_nonce', false ) != 1 ) { |
| 697 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 698 |
} |
| 699 |
|
| 700 |
$current_user = WPSC_Current_User::$current_user; |
| 701 |
if ( ! $current_user->is_agent ) { |
| 702 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 703 |
} |
| 704 |
|
| 705 |
$wh_settings = get_option( 'wpsc-wh-settings' ); |
| 706 |
if ( ! $wh_settings['allow-agent-modify-leaves'] ) { |
| 707 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 708 |
} |
| 709 |
|
| 710 |
$date_selected = isset( $_POST['dateSelected'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash( $_POST['dateSelected'] ) ) ) : array(); |
| 711 |
if ( ! $date_selected ) { |
| 712 |
wp_send_json_error( 'Bad Request', 401 ); |
| 713 |
} |
| 714 |
|
| 715 |
$title = esc_attr__( 'Add/Delete Holidays', 'supportcandy' ); |
| 716 |
$unique_id = uniqid( 'wpsc_' ); |
| 717 |
|
| 718 |
ob_start(); |
| 719 |
?> |
| 720 |
<form action="#" onsubmit="return false;" class="wpsc-frm-ap-holiday-actions"> |
| 721 |
|
| 722 |
<div class="wpsc-input-group"> |
| 723 |
<div class="label-container"> |
| 724 |
<label for=""><?php esc_attr_e( 'Action', 'supportcandy' ); ?></label> |
| 725 |
</div> |
| 726 |
<select class="<?php echo esc_attr( $unique_id ); ?>" name="holiday-action"> |
| 727 |
<option value="add"><?php esc_attr_e( 'Add new holidays', 'supportcandy' ); ?></option> |
| 728 |
<option value="delete"><?php esc_attr_e( 'Delete existing holidays', 'supportcandy' ); ?></option> |
| 729 |
</select> |
| 730 |
</div> |
| 731 |
|
| 732 |
<div class="wpsc-input-group is-recurring"> |
| 733 |
<div class="label-container"> |
| 734 |
<label for=""><?php esc_attr_e( 'Repeate every year', 'supportcandy' ); ?></label> |
| 735 |
</div> |
| 736 |
<select name="is-recurring"> |
| 737 |
<option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option> |
| 738 |
<option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option> |
| 739 |
</select> |
| 740 |
</div> |
| 741 |
|
| 742 |
<input type="hidden" name="action" value="wpsc_set_ap_leaves_actions"> |
| 743 |
<input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_ap_leaves_actions' ) ); ?>"> |
| 744 |
|
| 745 |
</form> |
| 746 |
<script> |
| 747 |
jQuery('.<?php echo esc_attr( $unique_id ); ?>').change(function(){ |
| 748 |
if (jQuery(this).val() == 'add') { |
| 749 |
jQuery('.wpsc-input-group.is-recurring').show(); |
| 750 |
} else { |
| 751 |
jQuery('.wpsc-input-group.is-recurring').hide(); |
| 752 |
} |
| 753 |
}); |
| 754 |
</script> |
| 755 |
<?php |
| 756 |
$body = ob_get_clean(); |
| 757 |
|
| 758 |
ob_start(); |
| 759 |
?> |
| 760 |
<button class="wpsc-button small primary" onclick="wpsc_set_ap_leaves_actions(this);"> |
| 761 |
<?php esc_attr_e( 'Submit', 'supportcandy' ); ?> |
| 762 |
</button> |
| 763 |
<button class="wpsc-button small secondary" onclick="wpsc_close_modal();"> |
| 764 |
<?php esc_attr_e( 'Cancel', 'supportcandy' ); ?> |
| 765 |
</button> |
| 766 |
<?php |
| 767 |
$footer = ob_get_clean(); |
| 768 |
|
| 769 |
$response = array( |
| 770 |
'title' => $title, |
| 771 |
'body' => $body, |
| 772 |
'footer' => $footer, |
| 773 |
); |
| 774 |
|
| 775 |
wp_send_json( $response ); |
| 776 |
} |
| 777 |
|
| 778 |
/** |
| 779 |
* Set leaves actions |
| 780 |
* |
| 781 |
* @return void |
| 782 |
*/ |
| 783 |
public static function set_leaves_actions() { |
| 784 |
|
| 785 |
if ( check_ajax_referer( 'wpsc_set_ap_leaves_actions', '_ajax_nonce', false ) != 1 ) { |
| 786 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 787 |
} |
| 788 |
|
| 789 |
global $wpdb; |
| 790 |
|
| 791 |
$current_user = WPSC_Current_User::$current_user; |
| 792 |
if ( ! $current_user->is_agent ) { |
| 793 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 794 |
} |
| 795 |
|
| 796 |
$wh_settings = get_option( 'wpsc-wh-settings' ); |
| 797 |
if ( ! $wh_settings['allow-agent-modify-leaves'] ) { |
| 798 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 799 |
} |
| 800 |
|
| 801 |
$date_selected = isset( $_POST['dateSelected'] ) ? sanitize_text_field( wp_unslash( $_POST['dateSelected'] ) ) : ''; |
| 802 |
$date_selected = $date_selected ? array_filter( array_map( 'sanitize_text_field', explode( ',', $date_selected ) ) ) : array(); |
| 803 |
if ( ! $date_selected ) { |
| 804 |
wp_send_json_error( 'Bad Request', 401 ); |
| 805 |
} |
| 806 |
|
| 807 |
$action = isset( $_POST['holiday-action'] ) ? sanitize_text_field( wp_unslash( $_POST['holiday-action'] ) ) : ''; |
| 808 |
if ( ! $action ) { |
| 809 |
wp_send_json_error( 'Bad Request', 401 ); |
| 810 |
} |
| 811 |
|
| 812 |
$is_recurring = isset( $_POST['is-recurring'] ) ? intval( $_POST['is-recurring'] ) : ''; |
| 813 |
if ( ! is_numeric( $is_recurring ) ) { |
| 814 |
wp_send_json_error( 'Bad Request', 401 ); |
| 815 |
} |
| 816 |
|
| 817 |
foreach ( $date_selected as $date ) { |
| 818 |
|
| 819 |
$date = new DateTime( $date . ' 00:00:00' ); |
| 820 |
|
| 821 |
// delete non-recurring record if exists. |
| 822 |
$wpdb->delete( |
| 823 |
$wpdb->prefix . 'psmsc_holidays', |
| 824 |
array( |
| 825 |
'holiday' => $date->format( 'Y-m-d H:i:s' ), |
| 826 |
'agent' => $current_user->agent->id, |
| 827 |
) |
| 828 |
); |
| 829 |
|
| 830 |
// delete recurring record if exists. |
| 831 |
$wpdb->query( "DELETE FROM {$wpdb->prefix}psmsc_holidays WHERE agent=" . $current_user->agent->id . ' AND DAYOFMONTH(holiday)=' . $date->format( 'd' ) . ' AND MONTH(holiday)=' . $date->format( 'm' ) . ' AND is_recurring=1' ); |
| 832 |
|
| 833 |
// add record. |
| 834 |
if ( $action == 'add' ) { |
| 835 |
WPSC_Holiday::insert( |
| 836 |
array( |
| 837 |
'agent' => $current_user->agent->id, |
| 838 |
'holiday' => $date->format( 'Y-m-d H:i:s' ), |
| 839 |
'is_recurring' => $is_recurring, |
| 840 |
) |
| 841 |
); |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
// get non-recurring holidays. |
| 846 |
$non_recurring_holidays = array(); |
| 847 |
$holidays = WPSC_Holiday::find( |
| 848 |
array( |
| 849 |
'meta_query' => array( |
| 850 |
'relation' => 'AND', |
| 851 |
array( |
| 852 |
'slug' => 'agent', |
| 853 |
'compare' => '=', |
| 854 |
'val' => $current_user->agent->id, |
| 855 |
), |
| 856 |
array( |
| 857 |
'slug' => 'is_recurring', |
| 858 |
'compare' => '=', |
| 859 |
'val' => 0, |
| 860 |
), |
| 861 |
), |
| 862 |
) |
| 863 |
)['results']; |
| 864 |
foreach ( $holidays as $holiday ) { |
| 865 |
$non_recurring_holidays[] = $holiday->holiday->format( 'Y-m-d' ); |
| 866 |
} |
| 867 |
|
| 868 |
// get recurring holidays. |
| 869 |
$recurring_holidays = array(); |
| 870 |
$holidays = WPSC_Holiday::find( |
| 871 |
array( |
| 872 |
'meta_query' => array( |
| 873 |
'relation' => 'AND', |
| 874 |
array( |
| 875 |
'slug' => 'agent', |
| 876 |
'compare' => '=', |
| 877 |
'val' => $current_user->agent->id, |
| 878 |
), |
| 879 |
array( |
| 880 |
'slug' => 'is_recurring', |
| 881 |
'compare' => '=', |
| 882 |
'val' => 1, |
| 883 |
), |
| 884 |
), |
| 885 |
) |
| 886 |
)['results']; |
| 887 |
foreach ( $holidays as $holiday ) { |
| 888 |
$recurring_holidays[] = $holiday->holiday->format( 'm-d' ); |
| 889 |
} |
| 890 |
|
| 891 |
$response = array( |
| 892 |
'action' => $action, |
| 893 |
'is_recurring' => $is_recurring, |
| 894 |
'holidayList' => array( |
| 895 |
'nonRecurring' => $non_recurring_holidays, |
| 896 |
'recurring' => $recurring_holidays, |
| 897 |
), |
| 898 |
); |
| 899 |
|
| 900 |
wp_send_json( $response, 200 ); |
| 901 |
} |
| 902 |
} |
| 903 |
endif; |
| 904 |
|
| 905 |
WPSC_Current_Agent_Profile::init(); |
| 906 |
|