PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.4.2
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.4.2
3.5.4 3.5.3 3.5.2 3.5.1 3.4.9 3.5.0 3.4.8 3.4.7 trunk 2.3.1 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6
supportcandy / framework / scripts.js

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

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