PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.5
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.5
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 / framework / scripts.js

scripts.js in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.4.5, at framework/scripts.js

3,448 lines 89.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 jQuery(document).ready(function () {
2 wpsc_reset_responsive_style();
3 jQuery(window).resize(function () {
4 wpsc_reset_responsive_style(true);
5 });
6 wpsc_document_ready();
7
8 // for check there is value in reply box.
9 jQuery(window).on("popstate", function () {
10 if (wpsc_is_description_text()) {
11 if (!confirm(supportcandy.translations.warning_message)) {
12 return;
13 }
14 }
15 });
16
17 // Change the element from h1 to h2
18 if (jQuery('#link-modal-title').length > 0) {
19 jQuery('#link-modal-title').replaceWith('<h2 id="link-modal-title">' + jQuery('#link-modal-title').html() + '</h2>');
20 }
21 });
22
23 /**
24 * Apply responsive styles based on container size
25 */
26 function wpsc_reset_responsive_style(isWindowResize = false) {
27 var lastContainerWidth = supportcandy.lastContainerWidth
28 ? supportcandy.lastContainerWidth
29 : 0;
30 var containerWidth = jQuery("#wpsc-container").outerWidth();
31
32 if (isWindowResize && lastContainerWidth === containerWidth) {
33 return;
34 }
35
36 supportcandy.lastContainerWidth = containerWidth;
37
38 jQuery("#wpsc-container").hide();
39 var style = "xs";
40 if (containerWidth <= 768) {
41 style = "xs";
42 } else if (containerWidth > 768 && containerWidth <= 992) {
43 style = "sm";
44 } else if (containerWidth > 992 && containerWidth <= 1200) {
45 style = "md";
46 } else {
47 style = "lg";
48 }
49 supportcandy.responsiveStyle = style;
50 var styleURL =
51 supportcandy.plugin_url +
52 "framework/responsive/" +
53 style +
54 ".css?version=" +
55 supportcandy.version;
56 var styleEl =
57 '<link id="wpsc-responsive-css" rel="stylesheet" type="text/css" href="' +
58 styleURL +
59 '" />';
60 jQuery("#wpsc-responsive-css").remove();
61 jQuery("head").append(styleEl);
62 wpsc_apply_responsive_styles();
63 jQuery("#wpsc-container").show();
64 }
65
66 /**
67 * Apply visible elements on screen resize
68 */
69 function wpsc_el_reset_visible() {
70 jQuery(
71 ".wpsc-visible-xs,.wpsc-visible-sm,.wpsc-visible-md,.wpsc-visible-lg"
72 ).hide();
73 jQuery(".wpsc-visible-" + supportcandy.responsiveStyle).show();
74 }
75
76 /**
77 * Apply hidden elements on screen resize
78 */
79 function wpsc_el_reset_hidden() {
80 jQuery(
81 ".wpsc-hidden-xs,.wpsc-hidden-sm,.wpsc-hidden-md,.wpsc-hidden-lg"
82 ).show();
83 jQuery(".wpsc-hidden-" + supportcandy.responsiveStyle).hide();
84 }
85
86 /**
87 * Toggle humbargar menu
88 */
89 function wpsc_toggle_humbargar() {
90 var flag = supportcandy.humbargar ? true : false;
91 if (flag) {
92 jQuery(".wpsc-humbargar-menu").hide(
93 "slide",
94 { direction: "right" },
95 200,
96 function () {
97 jQuery(".wpsc-humbargar-overlay").hide();
98 }
99 );
100 supportcandy.humbargar = false;
101 } else {
102 jQuery(".wpsc-humbargar-overlay").show();
103 jQuery(".wpsc-humbargar-menu").show("slide", { direction: "right" }, 200);
104 supportcandy.humbargar = true;
105 }
106 }
107
108 /**
109 * Close humbargar menu if open
110 */
111 function wpsc_close_humbargar() {
112 var flag = supportcandy.humbargar ? true : false;
113 if (flag) {
114 jQuery(".wpsc-humbargar-menu").hide();
115 jQuery(".wpsc-humbargar-overlay").hide();
116 supportcandy.humbargar = false;
117 }
118 }
119
120 /**
121 * Bulk selector change
122 */
123 function wpsc_bulk_select_change() {
124 var checked = jQuery(".wpsc-bulk-selector").is(":checked");
125 jQuery(".wpsc-bulk-select").prop("checked", checked);
126 }
127
128 /**
129 * Bulk item selector change
130 */
131 function wpsc_bulk_item_select_change() {
132 var items = jQuery(".wpsc-bulk-select:checked");
133 var checked = items.length === 0 ? false : true;
134 jQuery(".wpsc-bulk-selector").prop("checked", checked);
135 }
136
137 /**
138 * Show modal with loading screen
139 */
140 function wpsc_show_modal() {
141 jQuery(".wpsc-modal .inner-container").hide();
142 jQuery(".wpsc-modal .loader").show();
143 jQuery(".wpsc-modal").show();
144 }
145
146 /**
147 * Close modal
148 */
149 function wpsc_show_modal_inner_container() {
150 jQuery(".wpsc-modal .loader").hide();
151 jQuery(".wpsc-modal .inner-container").show(
152 "slide",
153 { direction: "up" },
154 200
155 );
156 }
157
158 /**
159 * Close modal
160 */
161 function wpsc_close_modal() {
162 jQuery(".wpsc-modal .inner-container").hide(
163 "slide",
164 { direction: "up" },
165 200,
166 function () {
167 jQuery(".wpsc-modal").hide();
168 }
169 );
170 }
171
172 /**
173 * OFF toggle button click
174 */
175 function wpsc_toggle_off(el) {
176 var off = jQuery(el);
177 var on = jQuery(el).next();
178 var input = on.next();
179
180 if (input.val() === 0) {
181 return;
182 }
183
184 input.val(0);
185 on.removeClass("active");
186 off.addClass("active");
187 }
188
189 /**
190 * ON toggle button click
191 */
192 function wpsc_toggle_on(el) {
193 var on = jQuery(el);
194 var off = jQuery(el).prev();
195 var input = on.next();
196
197 if (input.val() === 1) {
198 return;
199 }
200
201 input.val(1);
202 off.removeClass("active");
203 on.addClass("active");
204 }
205
206 /**
207 * Scroll to top of WPSC component
208 */
209 function wpsc_scroll_top() {
210 jQuery("html, body").animate(
211 {
212 scrollTop: jQuery("#wpsc-container").offset().top - 42,
213 },
214 500
215 );
216 }
217
218 /**
219 * Toggle individual widgets
220 */
221 function wpsc_toggle_mob_it_widgets() {
222 var status = parseInt(
223 jQuery(".wpsc-it-mob-widgets-inner-container").data("status")
224 );
225
226 if (!status) {
227 jQuery(".wpsc-it-mob-widgets-inner-container").slideDown();
228 jQuery(".wpsc-it-mob-widget-trigger-btn .down").hide();
229 jQuery(".wpsc-it-mob-widget-trigger-btn .up").show();
230 jQuery(".wpsc-it-mob-widgets-inner-container").data("status", 1);
231 } else {
232 jQuery(".wpsc-it-mob-widgets-inner-container").slideUp();
233 jQuery(".wpsc-it-mob-widget-trigger-btn .up").hide();
234 jQuery(".wpsc-it-mob-widget-trigger-btn .down").show();
235 jQuery(".wpsc-it-mob-widgets-inner-container").data("status", 0);
236 }
237 }
238
239 /**
240 * Get refresh current ticket
241 */
242 function wpsc_it_ab_refresh(ticket_id) {
243 if (wpsc_is_description_text()) {
244 if (confirm(supportcandy.translations.warning_message)) {
245 wpsc_clear_saved_draft_reply(ticket_id);
246 } else {
247 return;
248 }
249 }
250
251 wpsc_get_individual_ticket(ticket_id);
252 }
253
254 /**
255 * Get close current ticket modal UI
256 */
257 function wpsc_it_close_ticket(ticket_id, nonce) {
258 if (wpsc_is_description_text()) {
259 if (!confirm(supportcandy.translations.warning_message)) return;
260 }
261
262 var flag = confirm(supportcandy.translations.confirm);
263 if (flag) {
264 if (wpsc_is_description_text()) {
265 wpsc_clear_saved_draft_reply(ticket_id);
266 }
267 } else {
268 return;
269 }
270
271 var data = { action: "wpsc_it_close_ticket", ticket_id, _ajax_nonce: nonce };
272 jQuery.post(supportcandy.ajax_url, data, function (response) {
273 wpsc_run_ajax_background_process();
274 wpsc_after_close_ticket(ticket_id);
275 });
276 }
277
278 /**
279 * Get duplicate ticket
280 */
281 function wpsc_it_get_duplicate_ticket(ticket_id, nonce) {
282 wpsc_show_modal();
283 var data = {
284 action: "wpsc_it_get_duplicate_ticket",
285 ticket_id,
286 _ajax_nonce: nonce,
287 };
288 jQuery.post(supportcandy.ajax_url, data, function (response) {
289 jQuery(".wpsc-modal-header").text(response.title);
290 jQuery(".wpsc-modal-body").html(response.body);
291 jQuery(".wpsc-modal-footer").html(response.footer);
292 wpsc_show_modal_inner_container();
293 });
294 }
295
296 /**
297 * Set duplicate ticket
298 */
299 function wpsc_it_set_duplicate_ticket(el, ticket_id) {
300 if (wpsc_is_description_text()) {
301 if (confirm(supportcandy.translations.warning_message)) {
302 wpsc_clear_saved_draft_reply(ticket_id);
303 } else {
304 return;
305 }
306 }
307
308 var form = jQuery("form.duplicate")[0];
309 var dataform = new FormData(form);
310 jQuery(".wpsc-modal-footer button").attr("disabled", true);
311 jQuery(el).text(supportcandy.translations.please_wait);
312
313 jQuery
314 .ajax({
315 url: supportcandy.ajax_url,
316 type: "POST",
317 data: dataform,
318 processData: false,
319 contentType: false,
320 })
321 .done(function (response) {
322 wpsc_close_modal();
323 wpsc_get_individual_ticket(response.ticket_id);
324 });
325 }
326
327 /**
328 * Get delete ticket
329 */
330 function wpsc_it_delete_ticket(ticket_id, nonce) {
331 if (wpsc_is_description_text()) {
332 if (!confirm(supportcandy.translations.warning_message)) {
333 return;
334 } else {
335 var is_tinymce =
336 typeof tinyMCE != "undefined" &&
337 tinyMCE.activeEditor &&
338 !tinyMCE.activeEditor.isHidden();
339 if (is_tinymce && tinymce.get("description")) {
340 tinyMCE.get("description").setContent("");
341 } else {
342 jQuery("#description").val("");
343 }
344 wpsc_clear_saved_draft_reply(ticket_id);
345 }
346 }
347
348 var flag = confirm(supportcandy.translations.confirm);
349 if (!flag) {
350 return;
351 }
352
353 var data = { action: "wpsc_it_delete_ticket", ticket_id, _ajax_nonce: nonce };
354 jQuery.post(supportcandy.ajax_url, data, function (response) {
355 wpsc_get_ticket_list();
356 wpsc_run_ajax_background_process();
357 });
358 }
359
360 /**
361 * Restore ticket
362 */
363 function wpsc_it_ticket_restore(ticket_id, nonce) {
364 var flag = confirm(supportcandy.translations.confirm);
365 if (!flag) {
366 return;
367 }
368
369 var data = {
370 action: "wpsc_it_ticket_restore",
371 ticket_id,
372 _ajax_nonce: nonce,
373 };
374 jQuery.post(supportcandy.ajax_url, data, function (response) {
375 wpsc_get_individual_ticket(ticket_id);
376 });
377 }
378
379 /**
380 * Get delete ticket
381 */
382 function wpsc_it_archive_ticket(ticket_id, nonce) {
383 if (wpsc_is_description_text()) {
384 if (!confirm(supportcandy.translations.warning_message)) {
385 return;
386 } else {
387 var is_tinymce =
388 typeof tinyMCE != "undefined" &&
389 tinyMCE.activeEditor &&
390 !tinyMCE.activeEditor.isHidden();
391 if (is_tinymce && tinymce.get("description")) {
392 var description = tinyMCE.get("description").setContent("");
393 } else {
394 var description = jQuery("#description").val("");
395 }
396 wpsc_clear_saved_draft_reply(ticket_id);
397 }
398 }
399
400 var flag = confirm(supportcandy.translations.confirm);
401 if (!flag) {
402 return;
403 }
404
405 var data = { action: "wpsc_it_archive_ticket", ticket_id, _ajax_nonce: nonce };
406 jQuery.post(supportcandy.ajax_url, data, function (response) {
407 wpsc_get_ticket_list();
408 wpsc_run_ajax_background_process();
409 });
410 }
411
412 /**
413 * Delete permanently
414 */
415 function wpsc_it_delete_permanently(ticket_id, nonce) {
416 var flag = confirm(supportcandy.translations.delete_permanently);
417 if (!flag) {
418 return;
419 }
420
421 var data = {
422 action: "wpsc_it_delete_permanently",
423 ticket_id,
424 _ajax_nonce: nonce,
425 };
426 jQuery.post(supportcandy.ajax_url, data, function (response) {
427 wpsc_get_ticket_list();
428 });
429 }
430
431 /**
432 * Get edit ticket subject modal UI
433 */
434 function wpsc_it_get_edit_subject(ticket_id) {
435 wpsc_show_modal();
436 var data = {
437 action: "wpsc_it_get_edit_subject",
438 ticket_id,
439 };
440 jQuery.post(supportcandy.ajax_url, data, function (response) {
441 jQuery(".wpsc-modal-header").text(response.title);
442 jQuery(".wpsc-modal-body").html(response.body);
443 jQuery(".wpsc-modal-footer").html(response.footer);
444 wpsc_show_modal_inner_container();
445 });
446 }
447
448 /**
449 * Set edit ticket subject
450 */
451 function wpsc_it_set_edit_subject(el, ticket_id) {
452 if (wpsc_is_description_text()) {
453 if (!confirm(supportcandy.translations.warning_message)) {
454 wpsc_clear_saved_draft_reply(ticket_id);
455 } else {
456 return;
457 }
458 }
459
460 var form = jQuery("form.edit-subject")[0];
461 var dataform = new FormData(form);
462 jQuery(".wpsc-modal-footer button").attr("disabled", true);
463 jQuery(el).text(supportcandy.translations.please_wait);
464 jQuery
465 .ajax({
466 url: supportcandy.ajax_url,
467 type: "POST",
468 data: dataform,
469 processData: false,
470 contentType: false,
471 })
472 .done(function (res) {
473 wpsc_close_modal();
474 wpsc_get_individual_ticket(ticket_id);
475 });
476 }
477
478 /**
479 * Get change status modal UI
480 */
481 function wpsc_it_get_edit_ticket_status(ticket_id, nonce) {
482 wpsc_show_modal();
483 var data = {
484 action: "wpsc_it_get_edit_ticket_status",
485 ticket_id,
486 _ajax_nonce: nonce,
487 };
488 jQuery.post(supportcandy.ajax_url, data, function (response) {
489 // Set to modal.
490 jQuery(".wpsc-modal-header").text(response.title);
491 jQuery(".wpsc-modal-body").html(response.body);
492 jQuery(".wpsc-modal-footer").html(response.footer);
493 // Display modal.
494 wpsc_show_modal_inner_container();
495 });
496 }
497
498 /**
499 * Update ticket status
500 *
501 * @param {*} el
502 */
503 function wpsc_it_set_edit_ticket_status(el, ticket_id) {
504 if (wpsc_is_description_text()) {
505 if (confirm(supportcandy.translations.warning_message)) {
506 wpsc_clear_saved_draft_reply(ticket_id);
507 } else {
508 return;
509 }
510 }
511
512 var form = jQuery("form.frm-edit-ticket-status")[0];
513 var dataform = new FormData(form);
514 jQuery(".wpsc-modal-footer button").attr("disabled", true);
515 jQuery(el).text(supportcandy.translations.please_wait);
516 jQuery
517 .ajax({
518 url: supportcandy.ajax_url,
519 type: "POST",
520 data: dataform,
521 processData: false,
522 contentType: false,
523 })
524 .done(function (res) {
525 wpsc_close_modal();
526 wpsc_get_individual_ticket(ticket_id);
527 wpsc_run_ajax_background_process();
528 });
529 }
530
531 /**
532 * Get additional recipients modal UI
533 */
534 function wpsc_it_get_add_ar(ticket_id, nonce) {
535 wpsc_show_modal();
536 var data = {
537 action: "wpsc_it_get_add_ar",
538 ticket_id,
539 _ajax_nonce: nonce,
540 };
541 jQuery.post(supportcandy.ajax_url, data, function (response) {
542 jQuery(".wpsc-modal-header").text(response.title);
543 jQuery(".wpsc-modal-body").html(response.body);
544 jQuery(".wpsc-modal-footer").html(response.footer);
545 wpsc_show_modal_inner_container();
546 });
547 }
548
549 /**
550 * Update additional recipients
551 *
552 * @param {*} el
553 */
554 function wpsc_it_set_add_ar(el, ticket_id) {
555 if (wpsc_is_description_text()) {
556 if (confirm(supportcandy.translations.warning_message)) {
557 wpsc_clear_saved_draft_reply(ticket_id);
558 } else {
559 return;
560 }
561 }
562
563 var form = jQuery("form.additional-recipients")[0];
564 var dataform = new FormData(form);
565 jQuery(".wpsc-modal-footer button").attr("disabled", true);
566 jQuery(el).text(supportcandy.translations.please_wait);
567 jQuery
568 .ajax({
569 url: supportcandy.ajax_url,
570 type: "POST",
571 data: dataform,
572 processData: false,
573 contentType: false,
574 })
575 .done(function (res) {
576 wpsc_close_modal();
577 wpsc_get_individual_ticket(ticket_id);
578 });
579 }
580
581 /**
582 * Get assigned agents modal UI
583 */
584 function wpsc_it_get_edit_assigned_agents(ticket_id, nonce) {
585 wpsc_show_modal();
586 var data = {
587 action: "wpsc_it_get_edit_assigned_agents",
588 ticket_id,
589 _ajax_nonce: nonce,
590 };
591 jQuery.post(supportcandy.ajax_url, data, function (response) {
592 jQuery(".wpsc-modal-header").text(response.title);
593 jQuery(".wpsc-modal-body").html(response.body);
594 jQuery(".wpsc-modal-footer").html(response.footer);
595 wpsc_show_modal_inner_container();
596 });
597 }
598
599 /**
600 * Update assigned agents
601 *
602 * @param {*} el
603 */
604 function wpsc_it_set_edit_assigned_agents(el, ticket_id, uniqueId) {
605 if (wpsc_is_description_text()) {
606 if (confirm(supportcandy.translations.warning_message)) {
607 wpsc_clear_saved_draft_reply(ticket_id);
608 } else {
609 return;
610 }
611 }
612
613 var form = jQuery("form.change-assignee." + uniqueId)[0];
614 var dataform = new FormData(form);
615 jQuery(".wpsc-modal-footer button").attr("disabled", true);
616 jQuery(el).text(supportcandy.translations.please_wait);
617 jQuery
618 .ajax({
619 url: supportcandy.ajax_url,
620 type: "POST",
621 data: dataform,
622 processData: false,
623 contentType: false,
624 })
625 .done(function (res) {
626 wpsc_close_modal();
627 if (res.viewPermission) {
628 wpsc_get_individual_ticket(ticket_id);
629 } else {
630 wpsc_get_ticket_list();
631 }
632 wpsc_run_ajax_background_process();
633 });
634 }
635
636 /**
637 * Get raised by modal UI
638 */
639 function wpsc_it_get_edit_raised_by(ticket_id, nonce) {
640 wpsc_show_modal();
641 var data = {
642 action: "wpsc_it_get_edit_raised_by",
643 ticket_id,
644 _ajax_nonce: nonce,
645 };
646 jQuery.post(supportcandy.ajax_url, data, function (response) {
647 // Set to modal.
648 jQuery(".wpsc-modal-header").text(response.title);
649 jQuery(".wpsc-modal-body").html(response.body);
650 jQuery(".wpsc-modal-footer").html(response.footer);
651 // Display modal.
652 wpsc_show_modal_inner_container();
653 });
654 }
655
656 /**
657 * Update raised by
658 *
659 * @param {*} el
660 */
661 function wpsc_it_set_edit_raised_by(el, ticket_id, uniqueId) {
662 if (wpsc_is_description_text()) {
663 if (confirm(supportcandy.translations.warning_message)) {
664 wpsc_clear_saved_draft_reply(ticket_id);
665 } else {
666 return;
667 }
668 }
669
670 var type = jQuery(".type." + uniqueId).val();
671 if (type == "new") {
672 var name = jQuery(".name." + uniqueId)
673 .val()
674 .trim();
675 var email = jQuery(".email." + uniqueId)
676 .val()
677 .trim();
678 if (!name || !email) {
679 return;
680 }
681
682 if (!validateEmail(email)) {
683 alert(supportcandy.translations.raisedByEditWidget.invalidEmail);
684 return;
685 }
686 }
687
688 var form = jQuery("form.change-raised-by." + uniqueId)[0];
689 var dataform = new FormData(form);
690 jQuery(".wpsc-modal-footer button").attr("disabled", true);
691 jQuery(el).text(supportcandy.translations.please_wait);
692 jQuery
693 .ajax({
694 url: supportcandy.ajax_url,
695 type: "POST",
696 data: dataform,
697 processData: false,
698 contentType: false,
699 })
700 .done(function (res) {
701 wpsc_close_modal();
702 wpsc_get_individual_ticket(ticket_id);
703 });
704 }
705
706 /**
707 * Get ticket fields modal UI
708 */
709 function wpsc_it_get_edit_ticket_fields(ticket_id, nonce) {
710 wpsc_show_modal();
711 var data = {
712 action: "wpsc_it_get_edit_ticket_fields",
713 ticket_id,
714 _ajax_nonce: nonce,
715 };
716 jQuery.post(supportcandy.ajax_url, data, function (response) {
717 jQuery(".wpsc-modal-header").text(response.title);
718 jQuery(".wpsc-modal-body").html(response.body);
719 jQuery(".wpsc-modal-footer").html(response.footer);
720 wpsc_show_modal_inner_container();
721 });
722 }
723
724 /**
725 * Update ticket fields
726 *
727 * @param {*} el
728 */
729 function wpsc_it_set_edit_ticket_fields(el, ticket_id) {
730 if (wpsc_is_description_text()) {
731 if (confirm(supportcandy.translations.warning_message)) {
732 wpsc_clear_saved_draft_reply(ticket_id);
733 } else {
734 return;
735 }
736 }
737
738 var form = jQuery("form.change-ticket-fields")[0];
739 var dataform = new FormData(form);
740 jQuery(".wpsc-modal-footer button").attr("disabled", true);
741 jQuery(el).text(supportcandy.translations.please_wait);
742 jQuery
743 .ajax({
744 url: supportcandy.ajax_url,
745 type: "POST",
746 data: dataform,
747 processData: false,
748 contentType: false,
749 })
750 .done(function (res) {
751 wpsc_close_modal();
752 wpsc_get_individual_ticket(ticket_id);
753 });
754 }
755
756 /**
757 * Get agent only filds modal UI
758 */
759 function wpsc_it_get_edit_agentonly_fields(ticket_id, nonce) {
760 wpsc_show_modal();
761 var data = {
762 action: "wpsc_it_get_edit_agentonly_fields",
763 ticket_id,
764 _ajax_nonce: nonce,
765 };
766 jQuery.post(supportcandy.ajax_url, data, function (response) {
767 jQuery(".wpsc-modal-header").text(response.title);
768 jQuery(".wpsc-modal-body").html(response.body);
769 jQuery(".wpsc-modal-footer").html(response.footer);
770 wpsc_show_modal_inner_container();
771 });
772 }
773
774 /**
775 * Update agent only fields
776 *
777 * @param {*} el
778 */
779 function wpsc_it_set_edit_agentonly_fields(el, ticket_id) {
780 if (wpsc_is_description_text()) {
781 if (confirm(supportcandy.translations.warning_message)) {
782 wpsc_clear_saved_draft_reply(ticket_id);
783 } else {
784 return;
785 }
786 }
787
788 var form = jQuery("form.change-agentonly-fields")[0];
789 var dataform = new FormData(form);
790 jQuery(".wpsc-modal-footer button").attr("disabled", true);
791 jQuery(el).text(supportcandy.translations.please_wait);
792 jQuery
793 .ajax({
794 url: supportcandy.ajax_url,
795 type: "POST",
796 data: dataform,
797 processData: false,
798 contentType: false,
799 })
800 .done(function (res) {
801 wpsc_close_modal();
802 wpsc_get_individual_ticket(ticket_id);
803 });
804 }
805
806 /**
807 * Get edit thread
808 */
809 function wpsc_it_get_edit_thread(el, ticket_id, thread_id, nonce) {
810 var thread = jQuery(el).closest(".wpsc-thread");
811 thread.html(supportcandy.loader_html);
812
813 var data = {
814 action: "wpsc_it_get_edit_thread",
815 ticket_id,
816 thread_id,
817 _ajax_nonce: nonce,
818 };
819 jQuery.post(supportcandy.ajax_url, data, function (res) {
820 thread.html(res);
821 });
822 }
823
824 /**
825 * Set edit thread
826 */
827 function wpsc_it_set_edit_thread(el, uniqueId) {
828 var form = jQuery("form.edit-thread")[0];
829 var dataform = new FormData(form);
830
831 var is_tinymce =
832 typeof tinyMCE != "undefined" &&
833 tinyMCE.activeEditor &&
834 !tinyMCE.activeEditor.isHidden();
835 var description =
836 is_tinymce && tinymce.get(uniqueId)
837 ? tinyMCE.get(uniqueId).getContent()
838 : jQuery("#" + uniqueId)
839 .val()
840 .trim();
841 if (!description) {
842 return;
843 }
844
845 dataform.append("thread_content", description);
846
847 var thread = jQuery(el).closest(".wpsc-thread");
848 thread.html(supportcandy.loader_html);
849
850 jQuery
851 .ajax({
852 url: supportcandy.ajax_url,
853 type: "POST",
854 data: dataform,
855 processData: false,
856 contentType: false,
857 })
858 .done(function (res) {
859 if (thread.next().length > 0) {
860 thread.next().before(res);
861 } else {
862 thread.parent().append(res);
863 }
864 thread.remove();
865 });
866 }
867
868 /**
869 * Delete thread
870 */
871 function wpsc_it_thread_delete(el, ticket_id, thread_id, nonce) {
872 var flag = confirm(supportcandy.translations.confirm);
873 if (!flag) {
874 return;
875 }
876
877 var thread = jQuery(el).closest(".wpsc-thread");
878 thread.html(supportcandy.loader_html);
879
880 var data = {
881 action: "wpsc_it_thread_delete",
882 ticket_id,
883 thread_id,
884 _ajax_nonce: nonce,
885 };
886 jQuery.post(supportcandy.ajax_url, data, function (res) {
887 if (res.trim()) {
888 if (thread.next().length > 0) {
889 thread.next().before(res);
890 } else {
891 thread.parent().append(res);
892 }
893 }
894 thread.remove();
895 });
896 }
897
898 /**
899 * Get thread html
900 */
901 function wpsc_it_get_thread(el, ticket_id, thread_id, _ajax_nonce) {
902 var thread = jQuery(el).closest(".wpsc-thread");
903 thread.html(supportcandy.loader_html);
904
905 var data = {
906 action: "wpsc_it_get_thread",
907 ticket_id,
908 thread_id,
909 _ajax_nonce,
910 };
911 jQuery.post(supportcandy.ajax_url, data, function (res) {
912 if (thread.next().length > 0) {
913 thread.next().before(res);
914 } else {
915 thread.parent().append(res);
916 }
917 thread.remove();
918 });
919 }
920
921 /**
922 * View thread log
923 */
924 function wpsc_it_view_thread_log(ticket_id, thread_id, log_id, _ajax_nonce) {
925 wpsc_show_modal();
926 var data = {
927 action: "wpsc_it_view_thread_log",
928 ticket_id,
929 thread_id,
930 log_id,
931 _ajax_nonce,
932 };
933 jQuery.post(supportcandy.ajax_url, data, function (response) {
934 jQuery(".wpsc-modal-header").text(response.title);
935 jQuery(".wpsc-modal-body").html(response.body);
936 jQuery(".wpsc-modal-footer").html(response.footer);
937 wpsc_show_modal_inner_container();
938 });
939 }
940
941 /**
942 * View thread
943 */
944 function wpsc_it_view_deleted_thread(ticket_id, thread_id, _ajax_nonce) {
945 wpsc_show_modal();
946 var data = {
947 action: "wpsc_it_view_deleted_thread",
948 ticket_id,
949 thread_id,
950 _ajax_nonce,
951 };
952 jQuery.post(supportcandy.ajax_url, data, function (response) {
953 jQuery(".wpsc-modal-header").text(response.title);
954 jQuery(".wpsc-modal-body").html(response.body);
955 jQuery(".wpsc-modal-footer").html(response.footer);
956 wpsc_show_modal_inner_container();
957 });
958 }
959
960 /**
961 * Restore thread
962 */
963 function wpsc_it_restore_thread(ticket_id, thread_id, _ajax_nonce) {
964 var flag = confirm(supportcandy.translations.confirm);
965 if (!flag) {
966 return;
967 }
968
969 wpsc_close_modal();
970 var thread = jQuery(".wpsc-thread." + thread_id);
971 thread.html(supportcandy.loader_html);
972
973 var data = {
974 action: "wpsc_it_restore_thread",
975 ticket_id,
976 thread_id,
977 _ajax_nonce,
978 };
979 jQuery.post(supportcandy.ajax_url, data, function (res) {
980 if (thread.next().length > 0) {
981 thread.next().before(res);
982 } else {
983 thread.parent().append(res);
984 }
985 thread.remove();
986 });
987 }
988
989 /**
990 * Restore thread
991 */
992 function wpsc_it_thread_delete_permanently(ticket_id, thread_id, _ajax_nonce) {
993 var flag = confirm(supportcandy.translations.confirm);
994 if (!flag) {
995 return;
996 }
997
998 wpsc_close_modal();
999 var thread = jQuery(".wpsc-thread." + thread_id);
1000 thread.html(supportcandy.loader_html);
1001
1002 var data = {
1003 action: "wpsc_it_thread_delete_permanently",
1004 ticket_id,
1005 thread_id,
1006 _ajax_nonce,
1007 };
1008 jQuery.post(supportcandy.ajax_url, data, function (res) {
1009 thread.remove();
1010 });
1011 }
1012
1013 /**
1014 * Get macros modal UI
1015 */
1016 function wpsc_get_macros() {
1017 wpsc_show_modal();
1018 var data = {
1019 action: "wpsc_get_macros",
1020 };
1021 jQuery.post(supportcandy.ajax_url, data, function (response) {
1022 // Set to modal.
1023 jQuery(".wpsc-modal-header").text(response.title);
1024 jQuery(".wpsc-modal-body").html(response.body);
1025 jQuery(".wpsc-modal-footer").html(response.footer);
1026 // Display modal.
1027 wpsc_show_modal_inner_container();
1028 jQuery("input[type=text]").focus();
1029 });
1030 }
1031
1032 /**
1033 * Add and condition item
1034 * @param {*} el
1035 * @param {*} uniqueId
1036 */
1037 function wpsc_add_and_condition(el, uniqueId) {
1038 var container = jQuery(el)
1039 .closest(".wpsc-form-filter-container")
1040 .find(".and-container");
1041 container.append(jQuery(".and-template." + uniqueId).html());
1042 container = container.find(".and-item:last-child .or-container");
1043 container.append(jQuery(".or-template." + uniqueId).html());
1044 container.find("select").selectWoo();
1045 }
1046
1047 /**
1048 * Add and condition item
1049 * @param {*} el
1050 * @param {*} uniqueId
1051 */
1052 function wpsc_add_or_condition(el, uniqueId) {
1053 var container = jQuery(el).closest(".and-item").find(".or-container");
1054 container.append(jQuery(".or-template." + uniqueId).html());
1055 container.find(":last-child").find("select").selectWoo();
1056 }
1057
1058 /**
1059 * Remove condition item
1060 * @param {*} el
1061 */
1062 function wpsc_remove_condition_item(el) {
1063 var andItem = jQuery(el).closest(".and-item");
1064 jQuery(el).closest(".wpsc-form-filter-item").remove();
1065 if (andItem.find(".or-container").children().length === 0) {
1066 jQuery(andItem).remove();
1067 }
1068 }
1069
1070 /**
1071 * Get JSON string for the condition input of given name
1072 * @param {*} name
1073 */
1074 function wpsc_get_condition_json(name) {
1075 var conditions = [];
1076 jQuery(".wpsc-form-filter-container." + name + " .and-item").each(
1077 function () {
1078 var andCondition = [];
1079 jQuery(this)
1080 .find(".wpsc-form-filter-item")
1081 .each(function () {
1082 var slug = jQuery(this).find("select.filter").first().val();
1083 if (!slug) {
1084 return;
1085 }
1086 var operator = jQuery(this).find("select.operator").first().val();
1087 if (!operator) {
1088 return;
1089 }
1090 var operand_val_1 = jQuery(this).find(".operand_val_1").first().val();
1091 if (!operand_val_1) {
1092 return;
1093 }
1094 var filter = { slug, operator, operand_val_1 };
1095 if (operator === "BETWEEN") {
1096 var operand_val_2 = jQuery(this)
1097 .find(".operand_val_2")
1098 .first()
1099 .val();
1100 if (!operand_val_2) {
1101 return;
1102 }
1103 filter.operand_val_2 = operand_val_2;
1104 }
1105 andCondition.push(filter);
1106 });
1107 conditions.push(andCondition);
1108 }
1109 );
1110 return conditions;
1111 }
1112
1113 /**
1114 * Get form filter oprators
1115 */
1116 function wpsc_get_ticket_filter_operators(el, nonce) {
1117 var content = jQuery(el).closest(".content");
1118 var slug = jQuery(el).val().trim();
1119
1120 content.find(".conditional, .wpsc-inline-loader").remove();
1121 if (!slug) {
1122 return;
1123 }
1124
1125 content.append(supportcandy.inline_loader);
1126 var data = {
1127 action: "wpsc_get_ticket_filter_operators",
1128 slug: slug,
1129 _ajax_nonce: nonce,
1130 };
1131 jQuery.post(supportcandy.ajax_url, data, function (response) {
1132 content.find(".wpsc-inline-loader").remove();
1133 content.append(response);
1134 });
1135 }
1136
1137 /**
1138 * Get ticket filter value input(s)
1139 */
1140 function wpsc_get_ticket_filter_operands(el, id, nonce) {
1141 var content = jQuery(el).closest(".content");
1142 var operator = jQuery(el).val().trim();
1143
1144 content.find(".conditional.operand, .wpsc-inline-loader").remove();
1145 if (!operator) {
1146 return;
1147 }
1148
1149 content.append(supportcandy.inline_loader);
1150 var data = {
1151 action: "wpsc_get_ticket_filter_operands",
1152 operator,
1153 id,
1154 _ajax_nonce: nonce,
1155 };
1156 jQuery.post(supportcandy.ajax_url, data, function (response) {
1157 content.find(".wpsc-inline-loader").remove();
1158 content.append(response);
1159 });
1160 }
1161
1162 /**
1163 * Single file attachment upload handler
1164 */
1165 function wpsc_set_attach_single(el, uniqueId, slug) {
1166 var fileAttachment = el.files[0];
1167 wpsc_file_upload(fileAttachment, uniqueId, slug, true);
1168 jQuery(el).val("");
1169 }
1170
1171 /**
1172 * Myltiple file attachment upload handler
1173 */
1174 function wpsc_set_attach_multiple(el, uniqueId, slug) {
1175 jQuery.each(el.files, function (index, fileAttachment) {
1176 wpsc_file_upload(fileAttachment, uniqueId, slug);
1177 });
1178 jQuery(el).val("");
1179 }
1180
1181 /**
1182 * Trigger description attachments
1183 */
1184 function wpsc_trigger_desc_attachments(uniqueId) {
1185 jQuery("input." + uniqueId).trigger("click");
1186 }
1187
1188 /**
1189 * Remove an attachment
1190 */
1191 function wpsc_remove_attachment(el) {
1192 var single = jQuery(el).data("single");
1193 var uniqueId = jQuery(el).data("uniqueid");
1194 jQuery(el).closest(".wpsc-editor-attachment").remove();
1195 if (single) {
1196 jQuery("input." + uniqueId).show();
1197 }
1198 }
1199
1200 /**
1201 * Attachment upload progess update
1202 */
1203 function wpscAttachmentUploadProgress(e) {
1204 if (e.lengthComputable) {
1205 var max = e.total;
1206 var current = e.loaded;
1207 var progrss = current / max;
1208 e.currentTarget.attachment
1209 .find(".attachment-waiting")
1210 .circleProgress("value", progrss);
1211 }
1212 }
1213
1214 /**
1215 * Change raised by or create ticket as
1216 */
1217 function wpsc_get_change_create_as(nonce) {
1218 wpsc_show_modal();
1219 var data = { action: "wpsc_get_change_create_as", _ajax_nonce: nonce };
1220 jQuery.post(supportcandy.ajax_url, data, function (response) {
1221 // Set to modal.
1222 jQuery(".wpsc-modal-header").text(response.title);
1223 jQuery(".wpsc-modal-body").html(response.body);
1224 jQuery(".wpsc-modal-footer").html(response.footer);
1225 // Display modal.
1226 wpsc_show_modal_inner_container();
1227 });
1228 }
1229
1230 /**
1231 * Change create as
1232 */
1233 function wpsc_set_change_create_as(uniqueId, _ajax_nonce) {
1234 var name = jQuery("input.name." + uniqueId)
1235 .val()
1236 .trim();
1237 var email = jQuery("input.email." + uniqueId)
1238 .val()
1239 .trim();
1240 if (!name || !email) {
1241 alert(supportcandy.translations.req_fields_missing);
1242 return;
1243 }
1244
1245 if (!validateEmail(email)) {
1246 alert(supportcandy.translations.invalidEmail);
1247 return;
1248 }
1249
1250 var data = { action: "wpsc_add_new_create_as", name, email, _ajax_nonce };
1251 jQuery.post(supportcandy.ajax_url, data, function (response) {
1252 jQuery("input.name").val(response.name);
1253 jQuery("input.email").val(response.email);
1254
1255 // add option and select.
1256 if (jQuery("select.create-as").val() != response.id) {
1257 var newOption = new Option(response.label, response.id, false, false);
1258 jQuery("select.create-as").append(newOption);
1259 jQuery("select.create-as").val(response.id).trigger("change");
1260 }
1261
1262 wpsc_close_modal();
1263 wpsc_after_change_create_as();
1264 });
1265 }
1266
1267 /**
1268 * Check visibility conditions
1269 */
1270 function wpsc_check_tff_visibility() {
1271 var conditionalFields = jQuery(".wpsc-tff.conditional");
1272 if (conditionalFields.length === 0) {
1273 return;
1274 }
1275
1276 const callerId = Math.floor(Math.random() * 100000);
1277 supportcandy.checkTffVisibityCallers =
1278 supportcandy.checkTffVisibityCallers || [];
1279 supportcandy.checkTffVisibityCallers.push(callerId);
1280
1281 jQuery(".wpsc-ct-loader").html(supportcandy.inline_loader);
1282 jQuery("select.create-as").attr("disabled", true);
1283
1284 var form = jQuery("form.wpsc-create-ticket")[0];
1285 var dataform = new FormData(form);
1286
1287 var is_tinymce =
1288 typeof tinyMCE != "undefined" &&
1289 tinyMCE.activeEditor &&
1290 !tinyMCE.activeEditor.isHidden();
1291 if (is_tinymce && tinymce.get("description")) {
1292 var description = tinyMCE.get("description").getContent();
1293 } else {
1294 var description = jQuery("#description").val();
1295 }
1296
1297 dataform.append("description", description);
1298 dataform.append("action", "wpsc_check_tff_visibility");
1299 jQuery
1300 .ajax({
1301 url: supportcandy.ajax_url,
1302 type: "POST",
1303 data: dataform,
1304 processData: false,
1305 contentType: false,
1306 })
1307 .done(function (results) {
1308 jQuery.each(results, function (key, val) {
1309 if (parseInt(val) === 0) {
1310 jQuery(".wpsc-tff." + key).addClass("wpsc-hidden");
1311 jQuery(".wpsc-tff." + key).removeClass("wpsc-visible");
1312 } else {
1313 jQuery(".wpsc-tff." + key).removeClass("wpsc-hidden");
1314 jQuery(".wpsc-tff." + key).addClass("wpsc-visible");
1315 }
1316 });
1317 jQuery(".wpsc-ct-loader").html("");
1318 jQuery("select.create-as").removeAttr("disabled");
1319 })
1320 .fail(function (xhr, status, error) {
1321 console.error("AJAX request failed:", status, error);
1322 jQuery(".wpsc-ct-loader").html("");
1323 jQuery("select.create-as").removeAttr("disabled");
1324 })
1325 .always(function () {
1326 // remove callerId from supportcandy.checkTffVisibityCallers array
1327 supportcandy.checkTffVisibityCallers =
1328 supportcandy.checkTffVisibityCallers.filter(function (id) {
1329 return id !== callerId;
1330 });
1331 });
1332 }
1333
1334 /**
1335 * Validate input for email address
1336 */
1337 function validateEmail(email) {
1338 var re =
1339 /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
1340 return re.test(email);
1341 }
1342
1343 /**
1344 * Validate input for URL
1345 */
1346 function validateURL(url) {
1347 var re =
1348 /^(?:(?:https?|ftp):\/\/)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/\S*)?$/;
1349 return re.test(url);
1350 }
1351
1352 /**
1353 * Validate input for number
1354 */
1355 function validateNumber(number) {
1356 var re = /^[0-9]+$/;
1357 return re.test(number);
1358 }
1359
1360 /**
1361 * Change event for filter
1362 */
1363 function wpsc_tl_filter_change(el, type) {
1364 const filterSlug = jQuery(el).val()?.trim();
1365 const isTicketList = type === 'ticket_list';
1366 supportcandy.prevFilter = supportcandy.ticketList.filters.filterSlug;
1367
1368 if (filterSlug === 'custom') {
1369 wpsc_tl_get_custom_filter( type );
1370 return;
1371 }
1372
1373 supportcandy.ticketList.filters = { filterSlug };
1374 isTicketList ? wpsc_get_tickets() : wpsc_get_archive_tickets();
1375 }
1376
1377 /**
1378 * Change ticket list page
1379 */
1380 function wpsc_tl_set_page(page, type) {
1381 switch (page) {
1382 case "first":
1383 if (supportcandy.ticketList.pagination.current_page == 1) {
1384 return;
1385 }
1386 supportcandy.ticketList.filters.page_no = 1;
1387 break;
1388
1389 case "prev":
1390 if (supportcandy.ticketList.pagination.current_page < 2) {
1391 return;
1392 }
1393 supportcandy.ticketList.filters.page_no =
1394 supportcandy.ticketList.pagination.current_page - 1;
1395 break;
1396
1397 case "next":
1398 if (!supportcandy.ticketList.pagination.has_next_page) {
1399 return;
1400 }
1401 supportcandy.ticketList.filters.page_no =
1402 supportcandy.ticketList.pagination.current_page + 1;
1403 break;
1404
1405 case "last":
1406 if (
1407 supportcandy.ticketList.pagination.current_page ==
1408 supportcandy.ticketList.pagination.total_pages
1409 ) {
1410 return;
1411 }
1412 supportcandy.ticketList.filters.page_no =
1413 supportcandy.ticketList.pagination.total_pages;
1414 break;
1415 }
1416 type === 'ticket_list' ? wpsc_get_tickets() : wpsc_get_archive_tickets();
1417 }
1418
1419 /**
1420 * Reset ticket list filters
1421 */
1422 function wpsc_tl_reset_filter( type ) {
1423 var filters = { filterSlug: "" };
1424 supportcandy.ticketList.filters = filters;
1425 type === 'ticket_list' ? wpsc_get_tickets() : wpsc_get_archive_tickets();
1426 }
1427
1428 /**
1429 * Search input keyup
1430 */
1431 function wpsc_tl_search_keyup(e, el, type) {
1432 if (e.keyCode !== 13) {
1433 return;
1434 }
1435 var search = jQuery(el).val().trim();
1436 supportcandy.ticketList.filters.search = search;
1437 supportcandy.ticketList.filters.page_no = 1;
1438 type === 'ticket_list' ? wpsc_get_tickets() : wpsc_get_archive_tickets();
1439 }
1440
1441 /**
1442 * Apply filters
1443 */
1444 function wpsc_tl_apply_filter_btn_click( type ) {
1445 supportcandy.ticketList.filters.search = jQuery("input.wpsc-search-input")
1446 .val()
1447 .trim();
1448 supportcandy.ticketList.filters.orderby = jQuery(
1449 "select.wpsc-input-sort-by"
1450 ).val();
1451 supportcandy.ticketList.filters.order = jQuery(
1452 "select.wpsc-input-sort-order"
1453 ).val();
1454 supportcandy.ticketList.filters.page_no = 1;
1455 type === 'ticket_list' ? wpsc_get_tickets() : wpsc_get_archive_tickets();
1456 }
1457
1458 /**
1459 * Get custom filter UI
1460 */
1461 function wpsc_tl_get_custom_filter( type ) {
1462 wpsc_show_modal();
1463
1464 const isTicketList = type === 'ticket_list';
1465 const action = isTicketList ? 'wpsc_get_tl_custom_filter' : 'wpsc_get_atl_custom_filter';
1466
1467 const data = {
1468 action,
1469 _ajax_nonce: supportcandy.nonce,
1470 };
1471 if (supportcandy.ticketList.filters.filterSlug === 'custom') {
1472 data.filters = supportcandy.ticketList.filters;
1473 }
1474 jQuery.post(supportcandy.ajax_url, data, function (response) {
1475 jQuery('.wpsc-modal-header').text(response.title);
1476 jQuery('.wpsc-modal-body').html(response.body);
1477 jQuery('.wpsc-modal-footer').html(response.footer);
1478 wpsc_show_modal_inner_container();
1479 });
1480 }
1481
1482 /**
1483 * Apply custom filter
1484 */
1485 function wpsc_tl_apply_custom_filter(el, type) {
1486
1487 var filters = wpsc_get_condition_json("custom_filters");
1488 if (
1489 filters.length === 0 ||
1490 (filters.length === 1 && filters[0].length === 0)
1491 ) {
1492 alert(supportcandy.translations.req_fields_missing);
1493 return;
1494 }
1495
1496 const cust_filter = type === "ticket_list" ? "wpsc-tl-custom-filter" : "wpsc-atl-custom-filter";
1497 filters = {
1498 filterSlug: "custom",
1499 "parent-filter": jQuery(
1500 "." + cust_filter + " select[name=parent-filter]"
1501 ).val(),
1502 filters: JSON.stringify(filters),
1503 orderby: jQuery("." + cust_filter + " select[name=sort-by]").val(),
1504 order: jQuery("." + cust_filter + " select[name=sort-order]").val(),
1505 page_no: 1,
1506 };
1507 supportcandy.ticketList.filters = filters;
1508
1509 wpsc_close_modal();
1510 type === "ticket_list" ? wpsc_get_tickets() : wpsc_get_archive_tickets();
1511 }
1512
1513 /**
1514 * Edit ticket list filter
1515 */
1516 function wpsc_tl_edit_filter( type ) {
1517 if (supportcandy.ticketList.filters.filterSlug == "custom") {
1518 wpsc_tl_get_custom_filter( type );
1519 } else {
1520 type === "ticket_list" ? wpsc_tl_get_edit_saved_filter() : wpsc_atl_get_edit_saved_filter();
1521 }
1522 }
1523
1524 /**
1525 * Add saved filter
1526 */
1527 function wpsc_tl_add_saved_filter() {
1528 var filters = wpsc_get_condition_json("custom_filters");
1529 if (
1530 filters.length === 0 ||
1531 (filters.length === 1 && filters[0].length === 0)
1532 ) {
1533 alert(supportcandy.translations.req_fields_missing);
1534 return;
1535 }
1536
1537 filters = {
1538 "parent-filter": jQuery(
1539 ".wpsc-tl-custom-filter select[name=parent-filter]"
1540 ).val(),
1541 filters: JSON.stringify(filters),
1542 orderby: jQuery(".wpsc-tl-custom-filter select[name=sort-by]").val(),
1543 order: jQuery(".wpsc-tl-custom-filter select[name=sort-order]").val(),
1544 };
1545
1546 supportcandy.tempFilters = filters;
1547 wpsc_close_modal();
1548 setTimeout(function () {
1549 wpsc_tl_get_add_saved_filter();
1550 }, 500);
1551 }
1552
1553 /**
1554 * Get add saved filter ui
1555 */
1556 function wpsc_tl_get_add_saved_filter() {
1557 wpsc_show_modal();
1558 var data = { action: "wpsc_tl_get_add_saved_filter" };
1559 jQuery.post(supportcandy.ajax_url, data, function (response) {
1560 // Set to modal.
1561 jQuery(".wpsc-modal-header").text(response.title);
1562 jQuery(".wpsc-modal-body").html(response.body);
1563 jQuery(".wpsc-modal-footer").html(response.footer);
1564 // Display modal.
1565 wpsc_show_modal_inner_container();
1566 });
1567 }
1568
1569 /**
1570 * Save and apply custom filter
1571 */
1572 function wpsc_tl_set_add_saved_filter(el, nonce) {
1573 var label = jQuery("input.wpsc-cf-label").val().trim();
1574 if (!label) {
1575 alert(supportcandy.translations.req_fields_missing);
1576 return;
1577 }
1578
1579 jQuery(".wpsc-modal-footer button").attr("disabled", true);
1580 jQuery(el).text(supportcandy.translations.please_wait);
1581
1582 var data = {
1583 action: "wpsc_tl_set_add_saved_filter",
1584 filters: supportcandy.tempFilters,
1585 _ajax_nonce: nonce,
1586 };
1587 data.filters.label = label;
1588
1589 jQuery.post(supportcandy.ajax_url, data, function (response) {
1590 supportcandy.ticketList.filters = { filterSlug: response.slug };
1591 wpsc_close_modal();
1592 wpsc_get_ticket_list();
1593 });
1594 }
1595
1596 /**
1597 * Get edit saved filter modal
1598 */
1599 function wpsc_tl_get_edit_saved_filter() {
1600 wpsc_show_modal();
1601 var data = {
1602 action: "wpsc_tl_get_edit_saved_filter",
1603 filterSlug: supportcandy.ticketList.filters.filterSlug,
1604 _ajax_nonce: supportcandy.nonce,
1605 };
1606 jQuery.post(supportcandy.ajax_url, data, function (response) {
1607 // Set to modal.
1608 jQuery(".wpsc-modal-header").text(response.title);
1609 jQuery(".wpsc-modal-body").html(response.body);
1610 jQuery(".wpsc-modal-footer").html(response.footer);
1611 // Display modal.
1612 wpsc_show_modal_inner_container();
1613 });
1614 }
1615
1616 /**
1617 * Set edit saved filter
1618 */
1619 function wpsc_tl_set_edit_saved_filter(el) {
1620 var label = jQuery("input.wpsc-cf-label").val().trim();
1621 if (!label) {
1622 alert(supportcandy.translations.req_fields_missing);
1623 return;
1624 }
1625
1626 var filters = wpsc_get_condition_json("custom_filters");
1627 if (
1628 filters.length === 0 ||
1629 (filters.length === 1 && filters[0].length === 0)
1630 ) {
1631 alert(supportcandy.translations.req_fields_missing);
1632 return;
1633 }
1634
1635 jQuery(".wpsc-modal-footer button").attr("disabled", true);
1636 jQuery(el).text(supportcandy.translations.please_wait);
1637
1638 var form = jQuery(".wpsc-tl-custom-filter")[0];
1639 var dataform = new FormData(form);
1640 dataform.append("filters", JSON.stringify(filters));
1641
1642 jQuery
1643 .ajax({
1644 url: supportcandy.ajax_url,
1645 type: "POST",
1646 data: dataform,
1647 processData: false,
1648 contentType: false,
1649 })
1650 .done(function (res) {
1651 wpsc_close_modal();
1652 wpsc_get_ticket_list();
1653 });
1654 }
1655
1656 /**
1657 * Delete saved filter
1658 */
1659 function wpsc_tl_delete_saved_filter( type ) {
1660 var flag = confirm(supportcandy.translations.confirm);
1661 if (!flag) {
1662 return;
1663 }
1664
1665 var data = {
1666 action: "wpsc_tl_delete_saved_filter",
1667 slug: supportcandy.ticketList.filters.filterSlug,
1668 _ajax_nonce: supportcandy.nonce,
1669 };
1670 jQuery.post(supportcandy.ajax_url, data, function (res) {
1671 supportcandy.ticketList.filters = { filterSlug: "" };
1672 wpsc_get_ticket_list();
1673 });
1674 }
1675
1676 /**
1677 * Close custom filter modal
1678 */
1679 function wpsc_tl_close_custom_filter_modal() {
1680 jQuery("select.wpsc-input-filter").val(supportcandy.prevFilter);
1681 wpsc_close_modal();
1682 }
1683
1684 /**
1685 * Get form filter oprators
1686 */
1687 function wpsc_tc_get_operators(el, nonce) {
1688 var content = jQuery(el).closest(".content");
1689 var slug = jQuery(el).val().trim();
1690
1691 content.find(".conditional, .wpsc-inline-loader").remove();
1692 if (!slug) {
1693 return;
1694 }
1695
1696 content.append(supportcandy.inline_loader);
1697 var data = {
1698 action: "wpsc_tc_get_operators",
1699 slug: slug,
1700 _ajax_nonce: nonce,
1701 };
1702 jQuery.post(supportcandy.ajax_url, data, function (response) {
1703 content.find(".wpsc-inline-loader").remove();
1704 content.append(response);
1705 });
1706 }
1707
1708 /**
1709 * Get email notification condition operands
1710 */
1711 function wpsc_tc_get_operand(el, slug, nonce) {
1712 var content = jQuery(el).closest(".content");
1713 var operator = jQuery(el).val().trim();
1714
1715 content.find(".conditional.operand, .wpsc-inline-loader").remove();
1716 if (!operator) {
1717 return;
1718 }
1719
1720 content.append(supportcandy.inline_loader);
1721 var data = {
1722 action: "wpsc_tc_get_operand",
1723 operator,
1724 slug,
1725 _ajax_nonce: nonce,
1726 };
1727 jQuery.post(supportcandy.ajax_url, data, function (response) {
1728 content.find(".wpsc-inline-loader").remove();
1729 content.append(response);
1730 });
1731 }
1732
1733 /**
1734 * Add image to editor
1735 *
1736 * @param {*} editor
1737 */
1738 function wpsc_add_custom_image_tinymce(editor, nonce) {
1739 wpsc_show_modal();
1740 var data = {
1741 action: "wpsc_add_custom_image_tinymce",
1742 editor_id: editor.id,
1743 _ajax_nonce: nonce,
1744 };
1745 jQuery.post(supportcandy.ajax_url, data, function (response) {
1746 // Set to modal.
1747 jQuery(".wpsc-modal-header").text(response.title);
1748 jQuery(".wpsc-modal-body").html(response.body);
1749 jQuery(".wpsc-modal-footer").html(response.footer);
1750 // Display modal.
1751 wpsc_show_modal_inner_container();
1752 });
1753 }
1754
1755 /**
1756 * Check whether given url has image or not
1757 *
1758 * @param {*} url
1759 */
1760 function isValidImageURL(url) {
1761 if (typeof url !== "string") {
1762 return false;
1763 }
1764 return !!url.match(/\w+\.(jpg|jpeg|gif|png|tiff|bmp)$/gi);
1765 }
1766
1767 /**
1768 * Upload image to tinymce editor
1769 *
1770 * @param {*} el
1771 */
1772 function wpsc_insert_editor_img(ed) {
1773 if (
1774 jQuery("#wpsc-tinymce-image-url").val().trim() === "" &&
1775 validateURL(jQuery("#wpsc-tinymce-image-url").val().trim()) &&
1776 isValidImageURL(jQuery("#wpsc-tinymce-image-url").val().trim())
1777 ) {
1778 alert(supportcandy.translations.req_fields_error);
1779 return;
1780 }
1781
1782 if (jQuery("#wpsc-tinymce-image-width").val().trim() === "") {
1783 alert(supportcandy.translations.req_fields_error);
1784 return;
1785 }
1786
1787 if (jQuery("#wpsc-tinymce-image-height").val().trim() === "") {
1788 alert(supportcandy.translations.req_fields_error);
1789 return;
1790 }
1791
1792 var is_tinymce =
1793 typeof tinyMCE != "undefined" &&
1794 tinyMCE.activeEditor &&
1795 !tinyMCE.activeEditor.isHidden();
1796 if (!is_tinymce) {
1797 return;
1798 }
1799
1800 wpsc_close_modal();
1801
1802 var source = jQuery("#wpsc-tinymce-image-url").val().trim();
1803 var height = jQuery("#wpsc-tinymce-image-height").val().trim();
1804 var width = jQuery("#wpsc-tinymce-image-width").val().trim();
1805 tinymce
1806 .get(ed)
1807 .execCommand(
1808 "mceInsertContent",
1809 false,
1810 '<img height="' +
1811 height +
1812 '" width="' +
1813 width +
1814 '" src="' +
1815 source +
1816 '"/>'
1817 );
1818 }
1819
1820 /**
1821 * Add image to editor
1822 *
1823 * @param {*} editor
1824 */
1825 function wpsc_edit_custom_image_tinymce(editor, image_obj, nonce) {
1826 if (!(editor && image_obj)) {
1827 alert(supportcandy.translations.req_fields_error);
1828 return;
1829 }
1830
1831 wpsc_show_modal();
1832 var data = {
1833 action: "wpsc_edit_custom_image_tinymce",
1834 editor_id: editor.id,
1835 height: image_obj.height,
1836 width: image_obj.width,
1837 src: image_obj.src,
1838 _ajax_nonce: nonce,
1839 };
1840 jQuery.post(supportcandy.ajax_url, data, function (response) {
1841 // Set to modal.
1842 jQuery(".wpsc-modal-header").text(response.title);
1843 jQuery(".wpsc-modal-body").html(response.body);
1844 jQuery(".wpsc-modal-footer").html(response.footer);
1845 // Display modal.
1846 wpsc_show_modal_inner_container();
1847 });
1848 }
1849
1850 /**
1851 * Get create ticket from thread
1852 */
1853 function wpsc_it_thread_new_ticket(el, ticket_id, thread_id, nonce) {
1854 wpsc_show_modal();
1855 var data = {
1856 action: "wpsc_it_thread_new_ticket",
1857 ticket_id,
1858 thread_id,
1859 _ajax_nonce: nonce,
1860 };
1861 jQuery.post(supportcandy.ajax_url, data, function (response) {
1862 jQuery(".wpsc-modal-header").text(response.title);
1863 jQuery(".wpsc-modal-body").html(response.body);
1864 jQuery(".wpsc-modal-footer").html(response.footer);
1865 wpsc_show_modal_inner_container();
1866 });
1867 }
1868
1869 /**
1870 * Get thread info
1871 *
1872 * @param {*} thread_id
1873 */
1874 function wpsc_it_thread_info(el, ticket_id, thread_id, nonce, type = 'ticket_list') {
1875 wpsc_show_modal();
1876 var data = {
1877 action: type === 'ticket_list' ? "wpsc_it_thread_info" : "wpsc_it_archive_thread_info",
1878 thread_id,
1879 ticket_id,
1880 _ajax_nonce: nonce,
1881 };
1882 jQuery.post(supportcandy.ajax_url, data, function (response) {
1883 jQuery(".wpsc-modal-header").text(response.title);
1884 jQuery(".wpsc-modal-body").html(response.body);
1885 jQuery(".wpsc-modal-footer").html(response.footer);
1886 wpsc_show_modal_inner_container();
1887 });
1888 }
1889
1890 /**
1891 * Set thread new ticket
1892 */
1893 function wpsc_it_set_thread_new_ticket(el, thread_id, nonce) {
1894 var form = jQuery("form.new_ticket_from_thread")[0];
1895 var dataform = new FormData(form);
1896 jQuery(".wpsc-modal-footer button").attr("disabled", true);
1897 jQuery(el).text(supportcandy.translations.please_wait);
1898 jQuery
1899 .ajax({
1900 url: supportcandy.ajax_url,
1901 type: "POST",
1902 data: dataform,
1903 processData: false,
1904 contentType: false,
1905 })
1906 .done(function (response) {
1907 wpsc_close_modal();
1908 wpsc_get_individual_ticket(response.ticket_id);
1909 });
1910 }
1911
1912 /**
1913 * Get customer other tickets
1914 */
1915 function wpsc_get_rb_other_tickets(el, ticket_id, nonce) {
1916 wpsc_show_modal();
1917 var data = {
1918 action: "wpsc_get_rb_other_tickets",
1919 ticket_id,
1920 _ajax_nonce: nonce,
1921 };
1922 jQuery.post(supportcandy.ajax_url, data, function (response) {
1923 jQuery(".wpsc-modal-header").text(response.title);
1924 jQuery(".wpsc-modal-body").html(response.body);
1925 jQuery(".wpsc-modal-footer").html(response.footer);
1926 wpsc_show_modal_inner_container();
1927 });
1928 }
1929
1930 /**
1931 * Get raised by info
1932 */
1933 function wpsc_get_rb_info(el, ticket_id, nonce) {
1934 wpsc_show_modal();
1935 var data = { action: "wpsc_get_rb_info", ticket_id, _ajax_nonce: nonce };
1936 jQuery.post(supportcandy.ajax_url, data, function (response) {
1937 jQuery(".wpsc-modal-header").text(response.title);
1938 jQuery(".wpsc-modal-body").html(response.body);
1939 jQuery(".wpsc-modal-footer").html(response.footer);
1940 wpsc_show_modal_inner_container();
1941 });
1942 }
1943
1944 /**
1945 * Thread expander
1946 */
1947 function wpsc_ticket_thread_expander_toggle(el) {
1948 var height = parseInt(jQuery(el).parent().find(".thread-text").height());
1949 if (height === 100) {
1950 jQuery(el).parent().find(".thread-text").height("auto");
1951 jQuery(el).text(supportcandy.translations.view_less);
1952 } else {
1953 jQuery(el).parent().find(".thread-text").height(100);
1954 jQuery(el).text(supportcandy.translations.view_more);
1955 }
1956 }
1957
1958 /**
1959 * Get change bulk status
1960 */
1961 function wpsc_bulk_change_status(nonce) {
1962 var items = jQuery(".wpsc-bulk-select:checked");
1963 var checked = items.length === 0 ? false : true;
1964 jQuery(".wpsc-bulk-selector").prop("checked", checked);
1965 if (items.length != 0) {
1966 var ticket_ids = jQuery(".wpsc-bulk-select:checked")
1967 .map(function () {
1968 return this.value;
1969 })
1970 .get();
1971 wpsc_show_modal();
1972 var data = {
1973 action: "wpsc_bulk_change_status",
1974 ticket_ids,
1975 _ajax_nonce: nonce,
1976 };
1977 jQuery.post(supportcandy.ajax_url, data, function (response) {
1978 // Set to modal.
1979 jQuery(".wpsc-modal-header").text(response.title);
1980 jQuery(".wpsc-modal-body").html(response.body);
1981 jQuery(".wpsc-modal-footer").html(response.footer);
1982 // Display modal.
1983 wpsc_show_modal_inner_container();
1984 });
1985 }
1986 }
1987
1988 /**
1989 * Load older threads
1990 */
1991 function wpsc_load_older_threads(el, ticket_id) {
1992 if (supportcandy.reply_form_position === "top") {
1993 jQuery(".wpsc-it-thread-section-container").append(
1994 supportcandy.loader_html
1995 );
1996 } else {
1997 jQuery(".wpsc-it-thread-section-container").prepend(
1998 supportcandy.loader_html
1999 );
2000 }
2001
2002 var oldBtnContainer = jQuery(el).parent();
2003 oldBtnContainer.hide();
2004
2005 var data = {
2006 action: "wpsc_load_older_threads",
2007 ticket_id,
2008 last_thread: supportcandy.threads.last_thread,
2009 };
2010 jQuery.post(supportcandy.ajax_url, data, function (response) {
2011 jQuery(".wpsc-it-thread-section-container").find(".wpsc-loader").remove();
2012 // add threads.
2013 if (supportcandy.reply_form_position === "top") {
2014 jQuery(".wpsc-it-thread-section-container").append(response.threads);
2015 } else {
2016 var scrollHeightBody = jQuery("body").prop("scrollHeight");
2017 var windowScrollTop = jQuery(window).scrollTop();
2018 jQuery(".wpsc-it-thread-section-container").prepend(response.threads);
2019 var scrollDiff = jQuery("body").prop("scrollHeight") - scrollHeightBody;
2020 jQuery(window).scrollTop(windowScrollTop + scrollDiff);
2021 }
2022 // show btn if there are more threads available.
2023 if (response.has_next_page) {
2024 supportcandy.threads.last_thread = response.last_thread;
2025 oldBtnContainer.show();
2026 }
2027 });
2028 }
2029
2030 /**
2031 * Set change bulk status
2032 */
2033 function wpsc_set_bulk_change_status(el) {
2034 var form = jQuery("form.frm-edit-bulk-status")[0];
2035 var dataform = new FormData(form);
2036 jQuery(".wpsc-modal-footer button").attr("disabled", true);
2037 jQuery(el).text(supportcandy.translations.please_wait);
2038 jQuery
2039 .ajax({
2040 url: supportcandy.ajax_url,
2041 type: "POST",
2042 data: dataform,
2043 processData: false,
2044 contentType: false,
2045 })
2046 .done(function (res) {
2047 wpsc_close_modal();
2048 wpsc_get_ticket_list();
2049 wpsc_run_ajax_background_process();
2050 });
2051 }
2052
2053 /**
2054 * Get bulk assign agent
2055 */
2056 function wpsc_bulk_assign_agents(nonce) {
2057 var items = jQuery(".wpsc-bulk-select:checked");
2058 var checked = items.length === 0 ? false : true;
2059 jQuery(".wpsc-bulk-selector").prop("checked", checked);
2060 if (items.length != 0) {
2061 var ticket_ids = jQuery(".wpsc-bulk-select:checked")
2062 .map(function () {
2063 return this.value;
2064 })
2065 .get();
2066 wpsc_show_modal();
2067 var data = {
2068 action: "wpsc_bulk_assign_agents",
2069 ticket_ids,
2070 _ajax_nonce: nonce,
2071 };
2072 jQuery.post(supportcandy.ajax_url, data, function (response) {
2073 // Set to modal.
2074 jQuery(".wpsc-modal-header").text(response.title);
2075 jQuery(".wpsc-modal-body").html(response.body);
2076 jQuery(".wpsc-modal-footer").html(response.footer);
2077 // Display modal.
2078 wpsc_show_modal_inner_container();
2079 });
2080 }
2081 }
2082
2083 /**
2084 * Set change assign agent
2085 */
2086 function wpsc_set_bulk_assign_agent(el) {
2087 var form = jQuery("form.frm-bulk-assign-agent")[0];
2088 var dataform = new FormData(form);
2089 jQuery(".wpsc-modal-footer button").attr("disabled", true);
2090 jQuery(el).text(supportcandy.translations.please_wait);
2091 jQuery
2092 .ajax({
2093 url: supportcandy.ajax_url,
2094 type: "POST",
2095 data: dataform,
2096 processData: false,
2097 contentType: false,
2098 })
2099 .done(function (res) {
2100 wpsc_close_modal();
2101 wpsc_get_ticket_list();
2102 wpsc_run_ajax_background_process();
2103 });
2104 }
2105
2106 /**
2107 * Get bulk assign tags
2108 */
2109 function wpsc_bulk_assign_tags(nonce) {
2110 var items = jQuery(".wpsc-bulk-select:checked");
2111 var checked = items.length === 0 ? false : true;
2112 jQuery(".wpsc-bulk-selector").prop("checked", checked);
2113 if (items.length != 0) {
2114 var ticket_ids = jQuery(".wpsc-bulk-select:checked")
2115 .map(function () {
2116 return this.value;
2117 })
2118 .get();
2119 wpsc_show_modal();
2120 var data = {
2121 action: "wpsc_bulk_assign_tags",
2122 ticket_ids,
2123 _ajax_nonce: nonce,
2124 };
2125 jQuery.post(supportcandy.ajax_url, data, function (response) {
2126 // Set to modal.
2127 jQuery(".wpsc-modal-header").text(response.title);
2128 jQuery(".wpsc-modal-body").html(response.body);
2129 jQuery(".wpsc-modal-footer").html(response.footer);
2130 // Display modal.
2131 wpsc_show_modal_inner_container();
2132 });
2133 }
2134 }
2135
2136 /**
2137 * Set change assign tags
2138 */
2139 function wpsc_set_bulk_assign_tag(el) {
2140 var form = jQuery("form.frm-bulk-assign-tags")[0];
2141 var dataform = new FormData(form);
2142
2143 var tags = dataform.getAll("tags[]");
2144 if (!tags.length) {
2145 return;
2146 }
2147
2148 jQuery(".wpsc-modal-footer button").attr("disabled", true);
2149 jQuery(el).text(supportcandy.translations.please_wait);
2150 jQuery
2151 .ajax({
2152 url: supportcandy.ajax_url,
2153 type: "POST",
2154 data: dataform,
2155 processData: false,
2156 contentType: false,
2157 })
2158 .done(function (res) {
2159 wpsc_close_modal();
2160 wpsc_get_ticket_list();
2161 wpsc_run_ajax_background_process();
2162 });
2163 }
2164
2165 /**
2166 * Get bulk archive ticket
2167 */
2168 function wpsc_bulk_archive_tickets(nonce) {
2169 var items = jQuery(".wpsc-bulk-select:checked");
2170 var checked = items.length === 0 ? false : true;
2171 jQuery(".wpsc-bulk-selector").prop("checked", checked);
2172 if (items.length != 0) {
2173 var ticket_ids = jQuery(".wpsc-bulk-select:checked")
2174 .map(function () {
2175 return this.value;
2176 })
2177 .get();
2178 var flag = confirm(supportcandy.translations.confirm);
2179 if (!flag) {
2180 return;
2181 }
2182
2183 var data = {
2184 action: "wpsc_bulk_archive_tickets",
2185 ticket_ids,
2186 _ajax_nonce: nonce,
2187 };
2188 jQuery.post(supportcandy.ajax_url, data, function (response) {
2189 wpsc_get_ticket_list();
2190 wpsc_run_ajax_background_process();
2191 });
2192 }
2193 }
2194
2195 /**
2196 * Get bulk permanently delete ticket
2197 */
2198 function wpsc_bulk_permanently_delete_tickets(nonce) {
2199 var items = jQuery(".wpsc-bulk-select:checked");
2200 var checked = items.length === 0 ? false : true;
2201 jQuery(".wpsc-bulk-selector").prop("checked", checked);
2202 if (items.length != 0) {
2203 var ticket_ids = jQuery(".wpsc-bulk-select:checked")
2204 .map(function () {
2205 return this.value;
2206 })
2207 .get();
2208 var flag = confirm(supportcandy.translations.delete_permanently);
2209 if (!flag) {
2210 return;
2211 }
2212
2213 var data = {
2214 action: "wpsc_bulk_permanently_delete_tickets",
2215 ticket_ids,
2216 _ajax_nonce: nonce,
2217 };
2218 jQuery.post(supportcandy.ajax_url, data, function (response) {
2219 wpsc_get_ticket_list();
2220 wpsc_run_ajax_background_process();
2221 });
2222 }
2223 }
2224
2225 /**
2226 * Set agent working hrs
2227 */
2228 function wpsc_set_agent_wh_hrs(el) {
2229 const form = jQuery(".wpsc-frm-agent-wh")[0];
2230 const dataform = new FormData(form);
2231 jQuery(el).text(supportcandy.translations.please_wait);
2232 jQuery(".wpsc-section-container").html(supportcandy.loader_html);
2233 jQuery
2234 .ajax({
2235 url: supportcandy.ajax_url,
2236 type: "POST",
2237 data: dataform,
2238 processData: false,
2239 contentType: false,
2240 })
2241 .done(function (res) {
2242 window.location.reload();
2243 });
2244 }
2245
2246 /**
2247 * Set add agent exception
2248 */
2249 function wpsc_set_add_agent_wh_exception(el) {
2250 var title = jQuery("input[name=title]").val();
2251 var from_date = jQuery("input[name=exception_date]").val();
2252 if (!title || !from_date) {
2253 alert(supportcandy.translations.req_fields_missing);
2254 return;
2255 }
2256 const form = jQuery(".wpsc-frm-add-agent-exception")[0];
2257 const dataform = new FormData(form);
2258 jQuery(el).text(supportcandy.translations.please_wait);
2259 jQuery
2260 .ajax({
2261 url: supportcandy.ajax_url,
2262 type: "POST",
2263 data: dataform,
2264 processData: false,
2265 contentType: false,
2266 })
2267 .done(function (res) {
2268 window.location.reload();
2269 });
2270 }
2271
2272 /**
2273 * Update agent working hour exception
2274 */
2275 function wpsc_set_edit_agent_wh_exception(el) {
2276 var title = jQuery("input[name=title]").val();
2277 var from_date = jQuery("input[name=exception_date]").val();
2278 if (!title || !from_date) {
2279 alert(supportcandy.translations.req_fields_missing);
2280 return;
2281 }
2282
2283 const form = jQuery(".wpsc-frm-edit-agent-exception")[0];
2284 const dataform = new FormData(form);
2285 jQuery(el).text(supportcandy.translations.please_wait);
2286 jQuery
2287 .ajax({
2288 url: supportcandy.ajax_url,
2289 type: "POST",
2290 data: dataform,
2291 processData: false,
2292 contentType: false,
2293 })
2294 .done(function (res) {
2295 window.location.reload();
2296 });
2297 }
2298
2299 /**
2300 * Get agent profile holiday actions
2301 */
2302 function wpsc_get_ap_leaves_actions(dateSelected, nonce) {
2303 supportcandy.temp.dateSelected = dateSelected;
2304 wpsc_show_modal();
2305 var data = {
2306 action: "wpsc_get_ap_leaves_actions",
2307 dateSelected,
2308 _ajax_nonce: nonce,
2309 };
2310 jQuery.post(supportcandy.ajax_url, data, function (response) {
2311 // Set to modal.
2312 jQuery(".wpsc-modal-header").text(response.title);
2313 jQuery(".wpsc-modal-body").html(response.body);
2314 jQuery(".wpsc-modal-footer").html(response.footer);
2315 // Display modal.
2316 wpsc_show_modal_inner_container();
2317 });
2318 }
2319
2320 /**
2321 * Set agent profile holiday actions
2322 */
2323 function wpsc_set_ap_leaves_actions(el) {
2324 const form = jQuery(".wpsc-frm-ap-holiday-actions")[0];
2325 const dataform = new FormData(form);
2326 dataform.append("dateSelected", supportcandy.temp.dateSelected);
2327 jQuery(el).text(supportcandy.translations.please_wait);
2328 jQuery
2329 .ajax({
2330 url: supportcandy.ajax_url,
2331 type: "POST",
2332 data: dataform,
2333 processData: false,
2334 contentType: false,
2335 })
2336 .done(function (response) {
2337 jQuery.each(supportcandy.temp.dateSelected, function (index, value) {
2338 if (response.action == "add" && response.is_recurring == 1) {
2339 jQuery("td")
2340 .find("[data-date=" + value + "]")
2341 .css({ "background-color": "#eb4d4b" });
2342 } else if (response.action == "add" && response.is_recurring == 0) {
2343 jQuery("td")
2344 .find("[data-date=" + value + "]")
2345 .css({ "background-color": "#f0932b" });
2346 } else {
2347 jQuery("td")
2348 .find("[data-date=" + value + "]")
2349 .css("background-color", "unset");
2350 }
2351 });
2352 supportcandy.temp.holidayList = response.holidayList;
2353 wpsc_close_modal();
2354 });
2355 }
2356
2357 /**
2358 * Clear dates
2359 */
2360 function wpsc_clear_date(el) {
2361 jQuery(el).prev().val("");
2362
2363 var conditionalFields = jQuery(".wpsc-tff.conditional");
2364 if (conditionalFields.length !== 0) {
2365 wpsc_check_tff_visibility();
2366 }
2367 }
2368
2369 /**
2370 * Check whether there is text in reply box
2371 */
2372 function wpsc_is_description_text() {
2373 var is_tinymce =
2374 typeof tinyMCE != "undefined" &&
2375 tinyMCE.activeEditor &&
2376 !tinyMCE.activeEditor.isHidden();
2377 if (is_tinymce && tinymce.get("description")) {
2378 var description = tinyMCE.get("description").getContent();
2379 } else {
2380 var description = jQuery("#description").val();
2381 }
2382
2383 var flag = false;
2384 if (description && description.length != 0) {
2385 flag = true;
2386 }
2387
2388 return flag;
2389 }
2390
2391 /**
2392 * Get Edit customer info
2393 *
2394 * @param {Int} ticket_id
2395 */
2396 function wpsc_get_edit_rb_info(el, ticket_id, nonce) {
2397 wpsc_show_modal();
2398 var data = { action: "wpsc_get_edit_rb_info", ticket_id, _ajax_nonce: nonce };
2399 jQuery.post(supportcandy.ajax_url, data, function (response) {
2400 jQuery(".wpsc-modal-header").text(response.title);
2401 jQuery(".wpsc-modal-body").html(response.body);
2402 jQuery(".wpsc-modal-footer").html(response.footer);
2403 wpsc_show_modal_inner_container();
2404 });
2405 }
2406
2407 /**
2408 * Set edit customer info
2409 *
2410 * @param {object} el
2411 * @param {Int} ticket_id
2412 */
2413 function wpsc_set_edit_rb_info(el, ticket_id, nonce) {
2414 var form = jQuery("form.frm-edit-rb-info")[0];
2415 var dataform = new FormData(form);
2416 jQuery(".wpsc-modal-footer button").attr("disabled", true);
2417 jQuery(el).text(supportcandy.translations.please_wait);
2418 jQuery
2419 .ajax({
2420 url: supportcandy.ajax_url,
2421 type: "POST",
2422 data: dataform,
2423 processData: false,
2424 contentType: false,
2425 })
2426 .done(function (res) {
2427 wpsc_close_modal();
2428 setTimeout(function () {
2429 wpsc_get_rb_info(el, ticket_id, nonce);
2430 }, 500);
2431 });
2432 }
2433
2434 /**
2435 * Get tickets based on filter set
2436 */
2437 function wpsc_get_archive_tickets() {
2438 jQuery(".wpsc-bulk-selector").prop("checked", false);
2439 jQuery(".wpsc-ticket-list").html(supportcandy.loader_html);
2440
2441 var data = {
2442 action: "wpsc_get_archive_tickets",
2443 _ajax_nonce: supportcandy.nonce,
2444 is_frontend : supportcandy.is_frontend
2445 };
2446 if (
2447 typeof supportcandy.ticketList != "undefined" &&
2448 typeof supportcandy.ticketList.filters != "undefined"
2449 ) {
2450 data.filters = supportcandy.ticketList.filters;
2451 }
2452
2453 jQuery.post(supportcandy.ajax_url, data, function (response) {
2454 jQuery(".wpsc-filter-actions").html(response.filter_actions);
2455 jQuery(".wpsc-ticket-bulk-actions").html(response.bulk_actions);
2456 jQuery(".wpsc-ticket-list").html(response.tickets);
2457 jQuery(".wpsc-pagination-txt").text(response.pagination_str);
2458 jQuery("select.wpsc-input-filter").val(response.filters.filterSlug);
2459 jQuery("select.wpsc-input-sort-by").val(response.filters.orderby);
2460 jQuery("select.wpsc-input-sort-order").val(response.filters.order);
2461 jQuery("input.wpsc-search-input").val(response.filters.search);
2462 if (response.pagination.total_pages > 1) {
2463 jQuery(".wpsc-pagination-btn").show();
2464 } else {
2465 jQuery(".wpsc-pagination-btn").hide();
2466 }
2467 supportcandy.ticketList = {
2468 filters: response.filters,
2469 pagination: response.pagination,
2470 };
2471 });
2472 }
2473
2474 /**
2475 * Get tickets based on filter set
2476 */
2477 function wpsc_get_tickets() {
2478 jQuery(".wpsc-bulk-selector").prop("checked", false);
2479 jQuery(".wpsc-ticket-list").html(supportcandy.loader_html);
2480
2481 var data = {
2482 action: "wpsc_get_tickets",
2483 _ajax_nonce: supportcandy.nonce,
2484 is_frontend: supportcandy.is_frontend,
2485 };
2486 if (
2487 typeof supportcandy.ticketList != "undefined" &&
2488 typeof supportcandy.ticketList.filters != "undefined"
2489 ) {
2490 data.filters = supportcandy.ticketList.filters;
2491 }
2492
2493 jQuery.post(supportcandy.ajax_url, data, function (response) {
2494 jQuery(".wpsc-filter-actions").html(response.filter_actions);
2495 jQuery(".wpsc-ticket-bulk-actions").html(response.bulk_actions);
2496 jQuery(".wpsc-ticket-list").html(response.tickets);
2497 jQuery(".wpsc-pagination-txt").text(response.pagination_str);
2498 jQuery("select.wpsc-input-filter").val(response.filters.filterSlug);
2499 jQuery("select.wpsc-input-sort-by").val(response.filters.orderby);
2500 jQuery("select.wpsc-input-sort-order").val(response.filters.order);
2501 jQuery("input.wpsc-search-input").val(response.filters.search);
2502 if (response.pagination.total_pages > 1) {
2503 jQuery(".wpsc-pagination-btn").show();
2504 } else {
2505 jQuery(".wpsc-pagination-btn").hide();
2506 }
2507 supportcandy.ticketList = {
2508 filters: response.filters,
2509 pagination: response.pagination,
2510 };
2511 });
2512 }
2513
2514 /**
2515 * Copy ticket url
2516 *
2517 * @param {int} ticket_id
2518 */
2519 function wpsc_it_copy_url(ticket_id) {
2520 var temp = jQuery("<input>");
2521 jQuery("body").append(temp);
2522 temp.val(jQuery("#wpsc-ticket-url").text()).select();
2523 document.execCommand("copy");
2524 temp.remove();
2525 alert(supportcandy.translations.copy_url);
2526 }
2527
2528 /**
2529 * Open auto-refresh toggle
2530 */
2531 function wpsc_get_tl_auto_refresh() {
2532 wpsc_show_modal();
2533
2534 // Get modal parts from static modals.
2535 var headerText = jQuery(
2536 ".wpsc-tl_snippets .auto-refresh .modal-header"
2537 ).text();
2538 var bodyHTML = jQuery(".wpsc-tl_snippets .auto-refresh .modal-body").html();
2539 var footerHTML = jQuery(
2540 ".wpsc-tl_snippets .auto-refresh .modal-footer"
2541 ).html();
2542
2543 // Set them to modal.
2544 jQuery(".wpsc-modal-header").text(headerText);
2545 jQuery(".wpsc-modal-body").html(bodyHTML);
2546 jQuery(".wpsc-modal-footer").html(footerHTML);
2547
2548 // Set current status.
2549 var status = supportcandy.tl_auto_refresh ? supportcandy.tl_auto_refresh : 0;
2550 var el = status
2551 ? jQuery(".wpsc-modal .toggle-on")
2552 : jQuery(".wpsc-modal .toggle-off");
2553 jQuery(".wpsc-modal-body input").val(status);
2554 status ? wpsc_toggle_on(el) : wpsc_toggle_off(el);
2555
2556 // Display modal.
2557 wpsc_show_modal_inner_container();
2558 }
2559
2560 /**
2561 * Set auto-refresh settings
2562 */
2563 function wpsc_set_tl_auto_refresh(el) {
2564 jQuery(".wpsc-modal-footer button").attr("disabled", true);
2565 jQuery(el).text(supportcandy.translations.please_wait);
2566 var status = parseInt(jQuery(".wpsc-modal-body input").val());
2567 wpsc_close_modal();
2568 if (supportcandy.tl_auto_refresh !== status) {
2569 supportcandy.tl_auto_refresh = status;
2570 if (status === 1) {
2571 wpsc_tl_auto_refresh();
2572 }
2573 }
2574 }
2575
2576 /**
2577 * Auto refresh
2578 */
2579 function wpsc_tl_auto_refresh() {
2580 if (
2581 supportcandy.current_section === "ticket-list" &&
2582 !supportcandy.ticketListIsIndividual &&
2583 supportcandy.tl_auto_refresh === 1 &&
2584 !(
2585 jQuery(".wpsc-bulk-select:checked").length ||
2586 jQuery(".wpsc-search-input").is(":focus")
2587 )
2588 ) {
2589 wpsc_get_tickets();
2590 if (
2591 typeof supportcandy.tl_auto_refresh_schedule !== "undefined" &&
2592 supportcandy.tl_auto_refresh_schedule == true
2593 ) {
2594 return;
2595 }
2596 supportcandy.tl_auto_refresh_schedule = true;
2597 setTimeout(function () {
2598 supportcandy.tl_auto_refresh_schedule = false;
2599 wpsc_tl_auto_refresh();
2600 }, 60000);
2601 }
2602 }
2603
2604 /**
2605 * User logout
2606 */
2607 function wpsc_user_logout(el, nonce) {
2608 jQuery(el).text(supportcandy.translations.please_wait);
2609 var data = { action: "wpsc_user_logout", _ajax_nonce: nonce };
2610 jQuery.post(supportcandy.ajax_url, data, function (res) {
2611 window.location.reload();
2612 });
2613 }
2614
2615 /**
2616 * Close model popup on escape key.
2617 */
2618 jQuery(document).keydown(function (e) {
2619 if (e.keyCode == 27) {
2620 wpsc_close_modal();
2621 }
2622 });
2623
2624 /**
2625 * Get customer info
2626 */
2627 function wpsc_view_customer_info(customer_id, _ajax_nonce) {
2628 wpsc_show_modal();
2629 var data = { action: "wpsc_view_customer_info", customer_id, _ajax_nonce };
2630 jQuery.post(supportcandy.ajax_url, data, function (response) {
2631 jQuery(".wpsc-modal-header").text(response.title);
2632 jQuery(".wpsc-modal-body").html(response.body);
2633 jQuery(".wpsc-modal-footer").html(response.footer);
2634 wpsc_show_modal_inner_container();
2635 });
2636 }
2637
2638 /**
2639 * Get edit customer info
2640 */
2641 function wpsc_get_edit_customer_info(customer_id, _ajax_nonce) {
2642 wpsc_show_modal();
2643 var data = {
2644 action: "wpsc_get_edit_customer_info",
2645 customer_id,
2646 _ajax_nonce,
2647 };
2648 jQuery.post(supportcandy.ajax_url, data, function (response) {
2649 jQuery(".wpsc-modal-header").text(response.title);
2650 jQuery(".wpsc-modal-body").html(response.body);
2651 jQuery(".wpsc-modal-footer").html(response.footer);
2652 wpsc_show_modal_inner_container();
2653 });
2654 }
2655
2656 /**
2657 * Set edit customer info
2658 */
2659 function wpsc_set_edit_customer_info(el, id, nonce) {
2660 var form = jQuery("form.frm-edit-customer-info")[0];
2661 var dataform = new FormData(form);
2662 jQuery(".wpsc-modal-footer button").attr("disabled", true);
2663 jQuery(el).text(supportcandy.translations.please_wait);
2664
2665 jQuery
2666 .ajax({
2667 url: supportcandy.ajax_url,
2668 type: "POST",
2669 data: dataform,
2670 processData: false,
2671 contentType: false,
2672 })
2673 .done(function (res) {
2674 wpsc_close_modal();
2675 wpsc_scroll_top();
2676 wpsc_view_customer_detailed_info(res.customer_id, res.nonce);
2677 });
2678 }
2679
2680 /**
2681 * Get customer logs
2682 */
2683 function wpsc_view_customer_logs(customer_id, _ajax_nonce) {
2684 wpsc_show_modal();
2685 var data = { action: "wpsc_view_customer_logs", customer_id, _ajax_nonce };
2686 jQuery.post(supportcandy.ajax_url, data, function (response) {
2687 jQuery(".wpsc-modal-header").text(response.title);
2688 jQuery(".wpsc-modal-body").html(response.body);
2689 jQuery(".wpsc-modal-footer").html(response.footer);
2690 wpsc_show_modal_inner_container();
2691 });
2692 }
2693
2694 /**
2695 *
2696 * @param {*} ticket
2697 */
2698 function wpsc_clear_saved_draft_reply(ticket_id) {
2699 var draft_replies = JSON.parse(localStorage.getItem("wpsc_auto_draft")) || {};
2700 var is_tinymce =
2701 typeof tinyMCE != "undefined" &&
2702 tinyMCE.activeEditor &&
2703 !tinyMCE.activeEditor.isHidden();
2704 if (is_tinymce && tinymce.get("description")) {
2705 tinyMCE.get("description").setContent("");
2706 }
2707 jQuery("#description").val("");
2708 if (!ticket_id) return;
2709 delete draft_replies[ticket_id];
2710 localStorage.setItem("wpsc_auto_draft", JSON.stringify(draft_replies));
2711 }
2712
2713 /**
2714 * Run background processes.
2715 */
2716 function wpsc_run_ajax_background_process() {
2717 var data = { action: "wpsc_run_ajax_background_process" };
2718 jQuery.post(supportcandy.ajax_url, data, function (response) {});
2719 }
2720
2721 /**
2722 * Assign ticket to self
2723 */
2724 function wpsc_self_assign_ticket(ticket_id, nonce) {
2725 var data = {
2726 action: "wpsc_self_assign_ticket",
2727 ticket_id,
2728 _ajax_nonce: nonce,
2729 };
2730
2731 jQuery.post(supportcandy.ajax_url, data, function (response) {
2732 wpsc_get_individual_ticket(ticket_id);
2733 });
2734 }
2735
2736 /**
2737 * Delete wpsc_auto_draft localStorage after 24 hr
2738 */
2739 function wpsc_delete_auto_draft() {
2740 var draft_cron_time = localStorage.getItem("wpsc_draft_cron_time");
2741 if (!draft_cron_time) {
2742 localStorage.setItem("wpsc_draft_cron_time", new Date().getTime());
2743 }
2744 var diff = new Date().getTime() - draft_cron_time;
2745 if (diff < 3600000) {
2746 return;
2747 }
2748 var draft_replies = JSON.parse(localStorage.getItem("wpsc_auto_draft")) || {};
2749 Object.keys(draft_replies).forEach((ticket_id) => {
2750 var timeDifference =
2751 new Date() - new Date(draft_replies[ticket_id].date_updated);
2752 if (timeDifference > 86400000) {
2753 delete draft_replies[ticket_id];
2754 }
2755 });
2756 localStorage.setItem("wpsc_auto_draft", JSON.stringify(draft_replies));
2757 localStorage.setItem("wpsc_draft_cron_time", new Date().getTime());
2758 }
2759
2760 /**
2761 * Delete customer
2762 */
2763 function wpsc_delete_customer(customer_id, nonce) {
2764 if (!confirm(supportcandy.translations.customer_delete_warn)) {
2765 return;
2766 }
2767
2768 var data = {
2769 action: "wpsc_delete_customer",
2770 customer_id,
2771 _ajax_nonce: nonce,
2772 };
2773 jQuery.post(supportcandy.ajax_url, data, function (response) {
2774 window.location.reload();
2775 });
2776 }
2777
2778 /**
2779 * Dashboard - Get filter from and to dates based on duration slug
2780 */
2781 function wpsc_db_set_filter_duration_dates(duration) {
2782 let dateStr = "";
2783 let date = new Date();
2784 let from_date, to_date;
2785
2786 switch (duration) {
2787 case "today":
2788 dateStr = date.toISOString().split("T")[0];
2789 date_from_to = {
2790 from: dateStr,
2791 to: dateStr,
2792 };
2793 break;
2794
2795 case "yesterday":
2796 date.setDate(date.getDate() - 1);
2797 dateStr = date.toISOString().split("T")[0];
2798 date_from_to = {
2799 from: dateStr,
2800 to: dateStr,
2801 };
2802 break;
2803
2804 case "this-week":
2805 let firstDayOfWeek = date.getDay() === 0 ? 6 : date.getDay() - 1;
2806 date.setDate(date.getDate() - firstDayOfWeek);
2807 from_date = date.toISOString().split("T")[0];
2808 date.setDate(date.getDate() + 6);
2809 to_date = date.toISOString().split("T")[0];
2810 date_from_to = {
2811 from: from_date,
2812 to: to_date,
2813 };
2814 break;
2815
2816 case "last-week":
2817 let firstDayOfLastWeek = date.getDay() === 0 ? 7 : date.getDay();
2818 date.setDate(date.getDate() - firstDayOfLastWeek - 6);
2819 from_date = date.toISOString().split("T")[0];
2820 date.setDate(date.getDate() + 6);
2821 to_date = date.toISOString().split("T")[0];
2822 date_from_to = {
2823 from: from_date,
2824 to: to_date,
2825 };
2826 break;
2827
2828 case "last-7":
2829 date.setDate(date.getDate() - 1); // Exclude today
2830 to_date = date.toISOString().split("T")[0];
2831 date.setDate(date.getDate() - 6); // Last 7 days from yesterday
2832 from_date = date.toISOString().split("T")[0];
2833 date_from_to = {
2834 from: from_date,
2835 to: to_date,
2836 };
2837 break;
2838
2839 case "last-30-days":
2840 date.setDate(date.getDate() - 1); // Exclude today
2841 to_date = date.toISOString().split("T")[0];
2842 date.setDate(date.getDate() - 29); // Last 30 days from yesterday
2843 from_date = date.toISOString().split("T")[0];
2844 date_from_to = {
2845 from: from_date,
2846 to: to_date,
2847 };
2848 break;
2849
2850 case "this-month":
2851 let this_month_starts = new Date(date.getFullYear(), date.getMonth(), 1);
2852 let this_month_ends = new Date(
2853 date.getFullYear(),
2854 date.getMonth() + 1,
2855 0
2856 );
2857 date_from_to = {
2858 from: `${this_month_starts.getFullYear()}-${(
2859 this_month_starts.getMonth() + 1
2860 )
2861 .toString()
2862 .padStart(2, "0")}-${this_month_starts
2863 .getDate()
2864 .toString()
2865 .padStart(2, "0")}`,
2866 to: `${this_month_ends.getFullYear()}-${(this_month_ends.getMonth() + 1)
2867 .toString()
2868 .padStart(2, "0")}-${this_month_ends
2869 .getDate()
2870 .toString()
2871 .padStart(2, "0")}`,
2872 };
2873 break;
2874
2875 case "last-month":
2876 date.setMonth(date.getMonth() - 1);
2877 let last_month_starts = new Date(date.getFullYear(), date.getMonth(), 1);
2878 let last_month_ends = new Date(
2879 date.getFullYear(),
2880 date.getMonth() + 1,
2881 0
2882 );
2883 date_from_to = {
2884 from: `${last_month_starts.getFullYear()}-${(
2885 last_month_starts.getMonth() + 1
2886 )
2887 .toString()
2888 .padStart(2, "0")}-${last_month_starts
2889 .getDate()
2890 .toString()
2891 .padStart(2, "0")}`,
2892 to: `${last_month_ends.getFullYear()}-${(last_month_ends.getMonth() + 1)
2893 .toString()
2894 .padStart(2, "0")}-${last_month_ends
2895 .getDate()
2896 .toString()
2897 .padStart(2, "0")}`,
2898 };
2899 break;
2900
2901 case "this-quarter":
2902 let this_quarter_month_starts = Math.floor(date.getMonth() / 3) * 3;
2903 let quarter_start = new Date(
2904 date.getFullYear(),
2905 this_quarter_month_starts,
2906 1
2907 );
2908 let quarter_end = new Date(
2909 date.getFullYear(),
2910 this_quarter_month_starts + 3,
2911 0
2912 );
2913 date_from_to = {
2914 from: `${quarter_start.getFullYear()}-${(quarter_start.getMonth() + 1)
2915 .toString()
2916 .padStart(2, "0")}-${quarter_start
2917 .getDate()
2918 .toString()
2919 .padStart(2, "0")}`,
2920 to: `${quarter_end.getFullYear()}-${(quarter_end.getMonth() + 1)
2921 .toString()
2922 .padStart(2, "0")}-${quarter_end
2923 .getDate()
2924 .toString()
2925 .padStart(2, "0")}`,
2926 };
2927 break;
2928
2929 case "last-quarter":
2930 let last_quarter_month_ends = Math.floor(date.getMonth() / 3) * 3 - 1; // End month of last quarter
2931 let last_quarter_month_starts = last_quarter_month_ends - 2; // Start month of last quarter
2932 if (last_quarter_month_ends < 0) {
2933 last_quarter_month_ends += 12;
2934 last_quarter_month_starts += 12;
2935 }
2936 let last_quarter_start = new Date(
2937 date.getFullYear(),
2938 last_quarter_month_starts,
2939 1
2940 );
2941 let last_quarter_end = new Date(
2942 date.getFullYear(),
2943 last_quarter_month_ends + 1,
2944 0
2945 );
2946 date_from_to = {
2947 from: `${last_quarter_start.getFullYear()}-${(
2948 last_quarter_start.getMonth() + 1
2949 )
2950 .toString()
2951 .padStart(2, "0")}-${last_quarter_start
2952 .getDate()
2953 .toString()
2954 .padStart(2, "0")}`,
2955 to: `${last_quarter_end.getFullYear()}-${(
2956 last_quarter_end.getMonth() + 1
2957 )
2958 .toString()
2959 .padStart(2, "0")}-${last_quarter_end
2960 .getDate()
2961 .toString()
2962 .padStart(2, "0")}`,
2963 };
2964 break;
2965
2966 case "this-year":
2967 date_from_to = {
2968 from: `${date.getFullYear()}-01-01`,
2969 to: `${date.getFullYear()}-12-31`,
2970 };
2971 break;
2972
2973 case "last-year":
2974 date_from_to = {
2975 from: `${date.getFullYear() - 1}-01-01`,
2976 to: `${date.getFullYear() - 1}-12-31`,
2977 };
2978 break;
2979 }
2980 return date_from_to;
2981 }
2982
2983 /**
2984 * Get batches for async report loading
2985 */
2986 function wpsc_rp_get_baches(fromDate, toDate) {
2987 let diffTime = Math.abs(toDate - fromDate);
2988 let diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)) + 1;
2989
2990 let batches = [];
2991 let batchData = [];
2992
2993 if (diffDays == 1) {
2994 // one day.
2995
2996 for (var i = 0; i <= 23; i++) {
2997 var hr = String(i);
2998 hr = hr.length == 1 ? "0" + hr : hr;
2999 batchData.push({
3000 fromDate: fromDate.toISOString().split("T")[0] + " " + hr + ":00:00",
3001 toDate: fromDate.toISOString().split("T")[0] + " " + hr + ":59:59",
3002 durationType: "day",
3003 });
3004 }
3005 } else if (diffDays <= 31) {
3006 // days.
3007
3008 do {
3009 batchData.push({
3010 fromDate: fromDate.toISOString().split("T")[0] + " 00:00:00",
3011 toDate: fromDate.toISOString().split("T")[0] + " 23:59:59",
3012 durationType: "days",
3013 });
3014 fromDate.setDate(fromDate.getDate() + 1);
3015 } while (fromDate <= toDate);
3016 } else if (diffDays <= 180) {
3017 // weeks.
3018
3019 var from = fromDate.toISOString().split("T")[0];
3020 fromDate.setDate(fromDate.getDate() + (7 - fromDate.getDay()));
3021 batchData.push({
3022 fromDate: from + " 00:00:00",
3023 toDate: fromDate.toISOString().split("T")[0] + " 23:59:59",
3024 durationType: "weeks",
3025 });
3026 do {
3027 fromDate.setDate(fromDate.getDate() + 1);
3028 var from = new Date(fromDate.toLocaleDateString("en-CA"));
3029 fromDate.setDate(fromDate.getDate() + 6);
3030 var to = new Date(fromDate.toLocaleDateString("en-CA"));
3031 to = toDate < to ? new Date(toDate.toLocaleDateString("en-CA")) : to;
3032 batchData.push({
3033 fromDate: from.toISOString().split("T")[0] + " 00:00:00",
3034 toDate: to.toISOString().split("T")[0] + " 23:59:59",
3035 durationType: "weeks",
3036 });
3037 } while (fromDate < toDate);
3038 } else if (diffDays <= 720) {
3039 // months.
3040
3041 // convert toDate from local to UTC.
3042 toDate = new Date(
3043 toDate.getFullYear(),
3044 toDate.getMonth(),
3045 toDate.getDate()
3046 );
3047
3048 var from = fromDate.toISOString().split("T")[0];
3049 if (fromDate.getMonth() == 11) {
3050 fromDate = new Date(fromDate.getFullYear(), 11, 31);
3051 } else {
3052 fromDate = new Date(fromDate.getFullYear(), fromDate.getMonth() + 1, 0);
3053 }
3054 batchData.push({
3055 fromDate: from + " 00:00:00",
3056 toDate: fromDate.toISOString().split("T")[0] + " 23:59:59",
3057 durationType: "months",
3058 });
3059 do {
3060 fromDate.setDate(fromDate.getDate() + 1);
3061 var from = new Date(fromDate.toLocaleDateString("en-CA"));
3062 if (fromDate.getMonth() == 11) {
3063 fromDate = new Date(fromDate.getFullYear(), 11, 31);
3064 } else {
3065 fromDate = new Date(fromDate.getFullYear(), fromDate.getMonth() + 1, 0);
3066 }
3067 var to = new Date(fromDate.toLocaleDateString("en-CA"));
3068 to = toDate < to ? new Date(toDate.toLocaleDateString("en-CA")) : to;
3069 batchData.push({
3070 fromDate: from.toISOString().split("T")[0] + " 00:00:00",
3071 toDate: to.toISOString().split("T")[0] + " 23:59:59",
3072 durationType: "months",
3073 });
3074 } while (fromDate < toDate);
3075 } else {
3076 // years.
3077
3078 // convert toDate from local to UTC.
3079 toDate = new Date(
3080 toDate.getFullYear(),
3081 toDate.getMonth(),
3082 toDate.getDate()
3083 );
3084
3085 var from = fromDate.toISOString().split("T")[0];
3086 fromDate = new Date(fromDate.getFullYear(), 11, 31);
3087 batchData.push({
3088 fromDate: from + " 00:00:00",
3089 toDate: fromDate.toISOString().split("T")[0] + " 23:59:59",
3090 durationType: "years",
3091 });
3092 do {
3093 fromDate.setDate(fromDate.getDate() + 1);
3094 var from = new Date(fromDate.toLocaleDateString("en-CA"));
3095 fromDate = new Date(fromDate.getFullYear(), 11, 31);
3096 var to = new Date(fromDate.toLocaleDateString("en-CA"));
3097 to = toDate < to ? new Date(toDate.toLocaleDateString("en-CA")) : to;
3098 batchData.push({
3099 fromDate: from.toISOString().split("T")[0] + " 00:00:00",
3100 toDate: to.toISOString().split("T")[0] + " 23:59:59",
3101 durationType: "years",
3102 });
3103 } while (fromDate < toDate);
3104 }
3105
3106 // devide into batches of 5.
3107 var batchLength = batchData.length;
3108 for (var i = 0; i < batchLength; i = i + 5) {
3109 var batch = [];
3110 var counter = 0;
3111 do {
3112 var index = i + counter;
3113 if (index == batchLength) {
3114 break;
3115 }
3116 batch.push(batchData[index]);
3117 counter++;
3118 } while (counter < 5);
3119 batches.push(batch);
3120 }
3121
3122 return batches;
3123 }
3124
3125 function wpsc_generate_random_color() {
3126 var minLuminance = 0.7;
3127
3128 do {
3129 var color = Math.floor(Math.random() * 16777216); // 0x000000 to 0xFFFFFF
3130 var red = (color >> 16) & 0xff;
3131 var green = (color >> 8) & 0xff;
3132 var blue = color & 0xff;
3133
3134 // Calculate luminance (brightness).
3135 var luminance = (0.299 * red + 0.587 * green + 0.114 * blue) / 255;
3136 } while (luminance < minLuminance);
3137
3138 return "#" + color.toString(16).toUpperCase();
3139 }
3140
3141 // onclick load respective card ticket list
3142 function wpsc_get_dbc_ticket_list(element, card) {
3143 var count = parseInt(jQuery(element).find(".wpsc-dbc-count").text(), 10);
3144 if (count === 0) {
3145 return;
3146 }
3147 var data = {
3148 action: "wpsc_dash_card_count_filter",
3149 card: card,
3150 view: supportcandy.is_frontend,
3151 _ajax_nonce: supportcandy.nonce,
3152 };
3153 jQuery.post(supportcandy.ajax_url, data, function (response) {
3154 if (response.url) {
3155 window.location = response.url;
3156 }
3157 });
3158 }
3159
3160 // onclick load respective card ticket list
3161 function wpsc_get_agent_status_ticket_list(agent_id, status_id, nonce) {
3162 var data = {
3163 action: "wpsc_get_agent_status_ticket_list",
3164 agent_id: agent_id,
3165 status_id: status_id,
3166 view: supportcandy.is_frontend,
3167 _ajax_nonce: nonce,
3168 };
3169 jQuery.post(supportcandy.ajax_url, data, function (response) {
3170 if (response.url) {
3171 window.location = response.url;
3172 }
3173 });
3174 }
3175
3176 /**
3177 * Refresh tags list
3178 */
3179 function wpsc_it_refresh_tags(ticket_id) {
3180 jQuery(".wpsc-it-tag-body").html(supportcandy.loader_html);
3181
3182 var data = { action: "wpsc_it_refresh_tags", ticket_id };
3183 jQuery.post(supportcandy.ajax_url, data, function (res) {
3184 jQuery(".wpsc-it-tag-body").html(res);
3185 });
3186 }
3187
3188 /**
3189 * View user wordpress profile
3190 */
3191 function wpsc_wp_user_profile(el, ticket_id, nonce) {
3192 var data = {
3193 action: "wpsc_wp_user_profile",
3194 ticket_id,
3195 _ajax_nonce: nonce,
3196 };
3197 jQuery.post(supportcandy.ajax_url, data, function (response) {
3198 if (response.url) {
3199 window.open(response.url, "_blank");
3200 }
3201 });
3202 }
3203
3204 /**
3205 * Restore archived ticket
3206 */
3207 function wpsc_it_restore_archived(ticket_id, nonce) {
3208
3209 if (!confirm(supportcandy.translations.confirm)){
3210 return;
3211 }
3212
3213 var data = {
3214 action: "wpsc_it_restore_archived",
3215 ticket_id,
3216 _ajax_nonce: nonce
3217 };
3218
3219 jQuery.post(
3220 supportcandy.ajax_url,
3221 data,
3222 function (response) {
3223 wpsc_get_archive_ticket_list();
3224 }
3225 );
3226 }
3227
3228 /**
3229 * Get refresh current ticket
3230 */
3231 function wpsc_it_archive_ab_refresh(ticket_id) {
3232 if (wpsc_is_description_text()) {
3233 if (confirm(supportcandy.translations.confirm)) {
3234 wpsc_clear_saved_draft_reply(ticket_id);
3235 } else {
3236 return;
3237 }
3238 }
3239
3240 wpsc_get_individual_archive_ticket(ticket_id);
3241 }
3242
3243 /**
3244 * Load older threads
3245 */
3246 function wpsc_load_older_archive_threads(el, ticket_id) {
3247 if (supportcandy.reply_form_position === "top") {
3248 jQuery(".wpsc-it-thread-section-container").append(
3249 supportcandy.loader_html
3250 );
3251 } else {
3252 jQuery(".wpsc-it-thread-section-container").prepend(
3253 supportcandy.loader_html
3254 );
3255 }
3256
3257 var oldBtnContainer = jQuery(el).parent();
3258 oldBtnContainer.hide();
3259
3260 var data = {
3261 action: "wpsc_load_older_archive_threads",
3262 ticket_id,
3263 last_thread: supportcandy.threads.last_thread,
3264 };
3265 jQuery.post(supportcandy.ajax_url, data, function (response) {
3266 jQuery(".wpsc-it-thread-section-container").find(".wpsc-loader").remove();
3267 // add threads.
3268 if (supportcandy.reply_form_position === "top") {
3269 jQuery(".wpsc-it-thread-section-container").append(response.threads);
3270 } else {
3271 var scrollHeightBody = jQuery("body").prop("scrollHeight");
3272 var windowScrollTop = jQuery(window).scrollTop();
3273 jQuery(".wpsc-it-thread-section-container").prepend(response.threads);
3274 var scrollDiff = jQuery("body").prop("scrollHeight") - scrollHeightBody;
3275 jQuery(window).scrollTop(windowScrollTop + scrollDiff);
3276 }
3277 // show btn if there are more threads available.
3278 if (response.has_next_page) {
3279 supportcandy.threads.last_thread = response.last_thread;
3280 oldBtnContainer.show();
3281 }
3282 });
3283 }
3284
3285
3286 /**
3287 * Delete permanently
3288 */
3289 function wpsc_iat_delete_permanently(ticket_id, nonce) {
3290 var flag = confirm(supportcandy.translations.delete_permanently);
3291 if (!flag) {
3292 return;
3293 }
3294
3295 var data = {
3296 action: "wpsc_iat_delete_permanently",
3297 ticket_id,
3298 _ajax_nonce: nonce,
3299 };
3300 jQuery.post(supportcandy.ajax_url, data, function (response) {
3301 wpsc_get_archive_ticket_list();
3302 });
3303 }
3304
3305 /**
3306 * Get bulk delete archive ticket
3307 */
3308 function wpsc_bulk_delete_archive_tickets(nonce) {
3309 var items = jQuery(".wpsc-bulk-select:checked");
3310 var checked = items.length === 0 ? false : true;
3311 jQuery(".wpsc-bulk-selector").prop("checked", checked);
3312 if (items.length != 0) {
3313 var ticket_ids = jQuery(".wpsc-bulk-select:checked")
3314 .map(function () {
3315 return this.value;
3316 })
3317 .get();
3318 var flag = confirm(supportcandy.translations.delete_permanently);
3319 if (!flag) {
3320 return;
3321 }
3322
3323 var data = {
3324 action: "wpsc_bulk_delete_archive_tickets",
3325 ticket_ids,
3326 _ajax_nonce: nonce,
3327 };
3328 jQuery.post(supportcandy.ajax_url, data, function (response) {
3329 wpsc_get_archive_ticket_list();
3330 wpsc_run_ajax_background_process();
3331 });
3332 }
3333 }
3334
3335 /**
3336 * Restore bulk archive tickets
3337 */
3338 function wpsc_bulk_restore_archive_tickets(nonce) {
3339 var items = jQuery(".wpsc-bulk-select:checked");
3340 var checked = items.length === 0 ? false : true;
3341 jQuery(".wpsc-bulk-selector").prop("checked", checked);
3342 if (items.length != 0) {
3343 var ticket_ids = jQuery(".wpsc-bulk-select:checked")
3344 .map(function () {
3345 return this.value;
3346 })
3347 .get();
3348 var flag = confirm(supportcandy.translations.confirm);
3349 if (!flag) {
3350 return;
3351 }
3352 var data = {
3353 action: "wpsc_bulk_restore_archive_tickets",
3354 ticket_ids,
3355 _ajax_nonce: nonce,
3356 };
3357 jQuery.post(supportcandy.ajax_url, data, function (response) {
3358 wpsc_get_archive_ticket_list();
3359 });
3360 }
3361 }
3362
3363 /**
3364 * Get bulk archive ticket
3365 */
3366 function wpsc_bulk_delete_tickets(nonce) {
3367 var items = jQuery(".wpsc-bulk-select:checked");
3368 var checked = items.length === 0 ? false : true;
3369 jQuery(".wpsc-bulk-selector").prop("checked", checked);
3370 if (items.length != 0) {
3371 var ticket_ids = jQuery(".wpsc-bulk-select:checked")
3372 .map(function () {
3373 return this.value;
3374 })
3375 .get();
3376 var flag = confirm(supportcandy.translations.confirm);
3377 if (!flag) {
3378 return;
3379 }
3380
3381 var data = {
3382 action: "wpsc_bulk_delete_tickets",
3383 ticket_ids,
3384 _ajax_nonce: nonce,
3385 };
3386 jQuery.post(supportcandy.ajax_url, data, function (response) {
3387 wpsc_get_ticket_list();
3388 wpsc_run_ajax_background_process();
3389 });
3390 }
3391 }
3392
3393 /**
3394 * Restore bulk tickets
3395 */
3396 function wpsc_bulk_restore_tickets(nonce) {
3397 var items = jQuery(".wpsc-bulk-select:checked");
3398 var checked = items.length === 0 ? false : true;
3399 jQuery(".wpsc-bulk-selector").prop("checked", checked);
3400 if (items.length != 0) {
3401 var ticket_ids = jQuery(".wpsc-bulk-select:checked")
3402 .map(function () {
3403 return this.value;
3404 })
3405 .get();
3406 var flag = confirm(supportcandy.translations.confirm);
3407 if (!flag) {
3408 return;
3409 }
3410 var data = {
3411 action: "wpsc_bulk_restore_tickets",
3412 ticket_ids,
3413 _ajax_nonce: nonce,
3414 };
3415 jQuery.post(supportcandy.ajax_url, data, function (response) {
3416 wpsc_get_ticket_list();
3417 });
3418 }
3419 }
3420
3421 /**
3422 * Deletes bulk ticket permanentaly
3423 */
3424 function wpsc_bulk_delete_tickets_permanently(nonce) {
3425 var items = jQuery(".wpsc-bulk-select:checked");
3426 var checked = items.length === 0 ? false : true;
3427 jQuery(".wpsc-bulk-selector").prop("checked", checked);
3428 if (items.length != 0) {
3429 var ticket_ids = jQuery(".wpsc-bulk-select:checked")
3430 .map(function () {
3431 return this.value;
3432 })
3433 .get();
3434 var flag = confirm(supportcandy.translations.confirm);
3435 if (!flag) {
3436 return;
3437 }
3438
3439 var data = {
3440 action: "wpsc_bulk_delete_tickets_permanently",
3441 ticket_ids,
3442 _ajax_nonce: nonce,
3443 };
3444 jQuery.post(supportcandy.ajax_url, data, function (response) {
3445 wpsc_get_ticket_list();
3446 });
3447 }
3448 }