admin.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. * Admin JavaScript for Studiou WC Product Category Manager
  3. */
  4. (function($) {
  5. 'use strict';
  6. // Initialize admin scripts
  7. $(document).ready(function() {
  8. // Debug: Log that script is loaded
  9. console.log('STUDIOU WC: Admin script loaded');
  10. console.log('STUDIOU WC: studiouWcpcm object:', studiouWcpcm);
  11. // Import form handling
  12. if ($('#studiou-wcpcm-import-form').length) {
  13. console.log('STUDIOU WC: Import form found');
  14. initImportForm();
  15. }
  16. // Export form handling
  17. if ($('#studiou-wcpcm-export-form').length) {
  18. console.log('STUDIOU WC: Export form found');
  19. initExportForm();
  20. }
  21. // Move products form handling
  22. if ($('#studiou-wcpcm-move-form').length) {
  23. console.log('STUDIOU WC: Move form found');
  24. initMoveForm();
  25. } else {
  26. console.log('STUDIOU WC: Move form NOT found');
  27. }
  28. // Sample CSV download handler
  29. if ($('#studiou-wcpcm-download-sample').length) {
  30. initSampleDownload();
  31. }
  32. });
  33. /**
  34. * Initialize import form
  35. */
  36. function initImportForm() {
  37. $('#studiou-wcpcm-import-form').on('submit', function(e) {
  38. e.preventDefault();
  39. // Show spinner
  40. var $submitBtn = $(this).find('#studiou-wcpcm-import-button');
  41. var $spinner = $(this).find('.spinner');
  42. $submitBtn.prop('disabled', true);
  43. $spinner.css('visibility', 'visible');
  44. // Clear previous notices
  45. $('.studiou-wcpcm-notice-area').empty();
  46. // Create form data
  47. var formData = new FormData(this);
  48. formData.append('action', 'studiou_wcpcm_import');
  49. formData.append('nonce', studiouWcpcm.nonce);
  50. // Send AJAX request
  51. $.ajax({
  52. url: studiouWcpcm.ajaxUrl,
  53. type: 'POST',
  54. data: formData,
  55. processData: false,
  56. contentType: false,
  57. success: function(response) {
  58. if (response.success) {
  59. showNotice('success', studiouWcpcm.i18n.importSuccess);
  60. // Show results
  61. $('#studiou-wcpcm-import-results').text(response.data.message);
  62. $('.studiou-wcpcm-import-results').show();
  63. } else {
  64. showNotice('error', response.data.message);
  65. }
  66. },
  67. error: function() {
  68. showNotice('error', studiouWcpcm.i18n.importError);
  69. },
  70. complete: function() {
  71. // Hide spinner
  72. $submitBtn.prop('disabled', false);
  73. $spinner.css('visibility', 'hidden');
  74. }
  75. });
  76. });
  77. }
  78. /**
  79. * Initialize export form
  80. */
  81. function initExportForm() {
  82. $('#studiou-wcpcm-export-form').on('submit', function(e) {
  83. e.preventDefault();
  84. // Show spinner
  85. var $submitBtn = $(this).find('#studiou-wcpcm-export-button');
  86. var $spinner = $(this).find('.spinner');
  87. $submitBtn.prop('disabled', true);
  88. $spinner.css('visibility', 'visible');
  89. // Clear previous notices
  90. $('.studiou-wcpcm-notice-area').empty();
  91. // Send AJAX request
  92. $.ajax({
  93. url: studiouWcpcm.ajaxUrl,
  94. type: 'POST',
  95. data: {
  96. action: 'studiou_wcpcm_export',
  97. nonce: studiouWcpcm.nonce
  98. },
  99. success: function(response) {
  100. if (response.success) {
  101. showNotice('success', studiouWcpcm.i18n.exportSuccess);
  102. // Show download link
  103. $('#studiou-wcpcm-export-message').text(response.data.message);
  104. $('#studiou-wcpcm-download-export').attr('href', response.data.download_url);
  105. $('.studiou-wcpcm-export-results').show();
  106. } else {
  107. showNotice('error', response.data.message);
  108. }
  109. },
  110. error: function() {
  111. showNotice('error', studiouWcpcm.i18n.exportError);
  112. },
  113. complete: function() {
  114. // Hide spinner
  115. $submitBtn.prop('disabled', false);
  116. $spinner.css('visibility', 'hidden');
  117. }
  118. });
  119. });
  120. }
  121. /**
  122. * Initialize move products form
  123. */
  124. function initMoveForm() {
  125. console.log('STUDIOU WC: Initializing move form handlers');
  126. // Add debug button click handler first
  127. $('#studiou-wcpcm-move-button').on('click', function(e) {
  128. console.log('STUDIOU WC: Move button clicked directly');
  129. });
  130. $('#studiou-wcpcm-move-form').on('submit', function(e) {
  131. e.preventDefault();
  132. // Debug: Log form submission
  133. console.log('STUDIOU WC: Move form submitted');
  134. // Validate that different categories are selected
  135. var sourceCategory = $('#source_category').val();
  136. var targetCategory = $('#target_category').val();
  137. console.log('STUDIOU WC: Source category:', sourceCategory, 'Target category:', targetCategory);
  138. if (!sourceCategory || !targetCategory) {
  139. console.log('STUDIOU WC: Missing category selection');
  140. showNotice('error', 'Please select both source and target categories');
  141. return;
  142. }
  143. if (sourceCategory === targetCategory) {
  144. console.log('STUDIOU WC: Same category selected');
  145. showNotice('error', studiouWcpcm.i18n.selectDifferentCategories);
  146. return;
  147. }
  148. // Show spinner
  149. var $submitBtn = $(this).find('#studiou-wcpcm-move-button');
  150. var $spinner = $(this).find('.spinner');
  151. $submitBtn.prop('disabled', true);
  152. $spinner.css('visibility', 'visible');
  153. // Clear previous notices
  154. $('.studiou-wcpcm-notice-area').empty();
  155. // Debug: Log AJAX request details
  156. console.log('STUDIOU WC: Sending AJAX request to:', studiouWcpcm.ajaxUrl);
  157. console.log('STUDIOU WC: Nonce:', studiouWcpcm.nonce);
  158. // Send AJAX request
  159. $.ajax({
  160. url: studiouWcpcm.ajaxUrl,
  161. type: 'POST',
  162. data: {
  163. action: 'studiou_wcpcm_move_products',
  164. source_category: sourceCategory,
  165. target_category: targetCategory,
  166. nonce: studiouWcpcm.nonce
  167. },
  168. success: function(response) {
  169. console.log('STUDIOU WC: AJAX response:', response);
  170. if (response.success) {
  171. showNotice('success', studiouWcpcm.i18n.moveSuccess);
  172. // Show results
  173. $('#studiou-wcpcm-move-message').html('<p>' + response.data.message + '</p>');
  174. $('.studiou-wcpcm-move-results').show();
  175. // Refresh the page to update category counts
  176. setTimeout(function() {
  177. location.reload();
  178. }, 3000);
  179. } else {
  180. showNotice('error', response.data.message);
  181. }
  182. },
  183. error: function(xhr, status, error) {
  184. console.error('STUDIOU WC: AJAX error:', status, error);
  185. console.error('STUDIOU WC: Response text:', xhr.responseText);
  186. showNotice('error', studiouWcpcm.i18n.moveError + ': ' + error);
  187. },
  188. complete: function() {
  189. // Hide spinner
  190. $submitBtn.prop('disabled', false);
  191. $spinner.css('visibility', 'hidden');
  192. }
  193. });
  194. });
  195. }
  196. /**
  197. * Initialize sample CSV download
  198. */
  199. function initSampleDownload() {
  200. $('#studiou-wcpcm-download-sample').on('click', function(e) {
  201. e.preventDefault();
  202. // Sample CSV content
  203. var csvContent = 'name,url-name,parent-name,description,display-type,visibility,protected-passwords,operation\n' +
  204. '"Clothing","clothing","","Top quality clothing","default","public","","A"\n' +
  205. '"Men","men","Clothing","Men\'s clothing","products","public","","A"\n' +
  206. '"Women","women","Clothing","Women\'s clothing","products","protected","password123","A"\n' +
  207. '"Kids","kids","Clothing","Kids clothing","products","public","","U"\n' +
  208. '"Accessories","accessories","","All accessories","default","public","","D"';
  209. // Create download link
  210. var blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
  211. var url = URL.createObjectURL(blob);
  212. var link = document.createElement('a');
  213. link.setAttribute('href', url);
  214. link.setAttribute('download', 'sample-product-categories.csv');
  215. link.style.visibility = 'hidden';
  216. document.body.appendChild(link);
  217. link.click();
  218. document.body.removeChild(link);
  219. });
  220. }
  221. /**
  222. * Show notice message
  223. *
  224. * @param {string} type Notice type (success, error)
  225. * @param {string} message Notice message
  226. */
  227. function showNotice(type, message) {
  228. var $notice = $('<div class="studiou-wcpcm-notice studiou-wcpcm-notice-' + type + '"></div>').text(message);
  229. $('.studiou-wcpcm-notice-area').append($notice);
  230. // Scroll to notice
  231. $('html, body').animate({
  232. scrollTop: $('.studiou-wcpcm-notice-area').offset().top - 50
  233. }, 500);
  234. }
  235. // Additional debug: Check if elements exist after DOM load
  236. $(window).on('load', function() {
  237. console.log('STUDIOU WC: Window loaded');
  238. console.log('STUDIOU WC: Move form exists:', $('#studiou-wcpcm-move-form').length > 0);
  239. console.log('STUDIOU WC: Move button exists:', $('#studiou-wcpcm-move-button').length > 0);
  240. console.log('STUDIOU WC: Source select exists:', $('#source_category').length > 0);
  241. console.log('STUDIOU WC: Target select exists:', $('#target_category').length > 0);
  242. });
  243. })(jQuery);