PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.5.4
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.5.4
3.5.4 3.5.3 3.5.2 3.5.1 3.4.9 3.5.0 3.4.8 3.4.7 trunk 2.3.1 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6
supportcandy / includes / admin / tickets / class-wpsc-current-agent-profile.php

class-wpsc-current-agent-profile.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.5.4, at includes/admin/tickets/class-wpsc-current-agent-profile.php

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