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

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