PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.9
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.9
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.9, at framework/scripts.js

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