admin.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. // Batch description form handling
  29. if ($('#studiou-wcpcm-batch-description-form').length) {
  30. console.log('STUDIOU WC: Batch description form found');
  31. initBatchDescriptionForm();
  32. }
  33. // Sample CSV download handler
  34. if ($('#studiou-wcpcm-download-sample').length) {
  35. initSampleDownload();
  36. }
  37. });
  38. /**
  39. * Initialize import form
  40. */
  41. function initImportForm() {
  42. $('#studiou-wcpcm-import-form').on('submit', function(e) {
  43. e.preventDefault();
  44. // Show spinner
  45. var $submitBtn = $(this).find('#studiou-wcpcm-import-button');
  46. var $spinner = $(this).find('.spinner');
  47. $submitBtn.prop('disabled', true);
  48. $spinner.css('visibility', 'visible');
  49. // Clear previous notices
  50. $('.studiou-wcpcm-notice-area').empty();
  51. // Create form data
  52. var formData = new FormData(this);
  53. formData.append('action', 'studiou_wcpcm_import');
  54. formData.append('nonce', studiouWcpcm.nonce);
  55. // Send AJAX request
  56. $.ajax({
  57. url: studiouWcpcm.ajaxUrl,
  58. type: 'POST',
  59. data: formData,
  60. processData: false,
  61. contentType: false,
  62. success: function(response) {
  63. if (response.success) {
  64. showNotice('success', studiouWcpcm.i18n.importSuccess);
  65. // Show results
  66. $('#studiou-wcpcm-import-results').text(response.data.message);
  67. $('.studiou-wcpcm-import-results').show();
  68. } else {
  69. showNotice('error', response.data.message);
  70. }
  71. },
  72. error: function() {
  73. showNotice('error', studiouWcpcm.i18n.importError);
  74. },
  75. complete: function() {
  76. // Hide spinner
  77. $submitBtn.prop('disabled', false);
  78. $spinner.css('visibility', 'hidden');
  79. }
  80. });
  81. });
  82. }
  83. /**
  84. * Initialize export form
  85. */
  86. function initExportForm() {
  87. $('#studiou-wcpcm-export-form').on('submit', function(e) {
  88. e.preventDefault();
  89. // Show spinner
  90. var $submitBtn = $(this).find('#studiou-wcpcm-export-button');
  91. var $spinner = $(this).find('.spinner');
  92. $submitBtn.prop('disabled', true);
  93. $spinner.css('visibility', 'visible');
  94. // Clear previous notices
  95. $('.studiou-wcpcm-notice-area').empty();
  96. // Send AJAX request
  97. $.ajax({
  98. url: studiouWcpcm.ajaxUrl,
  99. type: 'POST',
  100. data: {
  101. action: 'studiou_wcpcm_export',
  102. nonce: studiouWcpcm.nonce
  103. },
  104. success: function(response) {
  105. if (response.success) {
  106. showNotice('success', studiouWcpcm.i18n.exportSuccess);
  107. // Show download link
  108. $('#studiou-wcpcm-export-message').text(response.data.message);
  109. $('#studiou-wcpcm-download-export').attr('href', response.data.download_url);
  110. $('.studiou-wcpcm-export-results').show();
  111. } else {
  112. showNotice('error', response.data.message);
  113. }
  114. },
  115. error: function() {
  116. showNotice('error', studiouWcpcm.i18n.exportError);
  117. },
  118. complete: function() {
  119. // Hide spinner
  120. $submitBtn.prop('disabled', false);
  121. $spinner.css('visibility', 'hidden');
  122. }
  123. });
  124. });
  125. }
  126. /**
  127. * Initialize move products form
  128. */
  129. function initMoveForm() {
  130. console.log('STUDIOU WC: Initializing move form handlers');
  131. // Add debug button click handler first
  132. $('#studiou-wcpcm-move-button').on('click', function(e) {
  133. console.log('STUDIOU WC: Move button clicked directly');
  134. });
  135. $('#studiou-wcpcm-move-form').on('submit', function(e) {
  136. e.preventDefault();
  137. // Debug: Log form submission
  138. console.log('STUDIOU WC: Move form submitted');
  139. // Validate that different categories are selected
  140. var sourceCategory = $('#source_category').val();
  141. var targetCategory = $('#target_category').val();
  142. console.log('STUDIOU WC: Source category:', sourceCategory, 'Target category:', targetCategory);
  143. if (!sourceCategory || !targetCategory) {
  144. console.log('STUDIOU WC: Missing category selection');
  145. showNotice('error', 'Please select both source and target categories');
  146. return;
  147. }
  148. if (sourceCategory === targetCategory) {
  149. console.log('STUDIOU WC: Same category selected');
  150. showNotice('error', studiouWcpcm.i18n.selectDifferentCategories);
  151. return;
  152. }
  153. // Show spinner
  154. var $submitBtn = $(this).find('#studiou-wcpcm-move-button');
  155. var $spinner = $(this).find('.spinner');
  156. $submitBtn.prop('disabled', true);
  157. $spinner.css('visibility', 'visible');
  158. // Clear previous notices
  159. $('.studiou-wcpcm-notice-area').empty();
  160. // Debug: Log AJAX request details
  161. console.log('STUDIOU WC: Sending AJAX request to:', studiouWcpcm.ajaxUrl);
  162. console.log('STUDIOU WC: Nonce:', studiouWcpcm.nonce);
  163. // Send AJAX request
  164. $.ajax({
  165. url: studiouWcpcm.ajaxUrl,
  166. type: 'POST',
  167. data: {
  168. action: 'studiou_wcpcm_move_products',
  169. source_category: sourceCategory,
  170. target_category: targetCategory,
  171. nonce: studiouWcpcm.nonce
  172. },
  173. success: function(response) {
  174. console.log('STUDIOU WC: AJAX response:', response);
  175. if (response.success) {
  176. showNotice('success', studiouWcpcm.i18n.moveSuccess);
  177. // Show results
  178. $('#studiou-wcpcm-move-message').html('<p>' + response.data.message + '</p>');
  179. $('.studiou-wcpcm-move-results').show();
  180. // Refresh the page to update category counts
  181. setTimeout(function() {
  182. location.reload();
  183. }, 3000);
  184. } else {
  185. showNotice('error', response.data.message);
  186. }
  187. },
  188. error: function(xhr, status, error) {
  189. console.error('STUDIOU WC: AJAX error:', status, error);
  190. console.error('STUDIOU WC: Response text:', xhr.responseText);
  191. showNotice('error', studiouWcpcm.i18n.moveError + ': ' + error);
  192. },
  193. complete: function() {
  194. // Hide spinner
  195. $submitBtn.prop('disabled', false);
  196. $spinner.css('visibility', 'hidden');
  197. }
  198. });
  199. });
  200. }
  201. /**
  202. * Initialize batch description form
  203. */
  204. function initBatchDescriptionForm() {
  205. console.log('STUDIOU WC: Initializing batch description form handlers');
  206. // Check all button handler
  207. $('#studiou-wcpcm-check-all').on('click', function(e) {
  208. e.preventDefault();
  209. console.log('STUDIOU WC: Check all button clicked');
  210. $('#categories_list input[type="checkbox"]').prop('checked', true);
  211. });
  212. // Uncheck all button handler
  213. $('#studiou-wcpcm-uncheck-all').on('click', function(e) {
  214. e.preventDefault();
  215. console.log('STUDIOU WC: Uncheck all button clicked');
  216. $('#categories_list input[type="checkbox"]').prop('checked', false);
  217. });
  218. // Form submit handler
  219. $('#studiou-wcpcm-batch-description-form').on('submit', function(e) {
  220. e.preventDefault();
  221. console.log('STUDIOU WC: Batch description form submitted');
  222. // Get selected categories
  223. var selectedCategories = [];
  224. $('#categories_list input[type="checkbox"]:checked').each(function() {
  225. selectedCategories.push($(this).val());
  226. });
  227. var description = $('#category_description').val();
  228. console.log('STUDIOU WC: Selected categories:', selectedCategories);
  229. console.log('STUDIOU WC: Description:', description);
  230. // Validate input
  231. if (selectedCategories.length === 0) {
  232. console.log('STUDIOU WC: No categories selected');
  233. showNotice('error', 'Please select at least one category');
  234. return;
  235. }
  236. // Show spinner
  237. var $submitBtn = $(this).find('#studiou-wcpcm-batch-description-button');
  238. var $spinner = $(this).find('.spinner');
  239. $submitBtn.prop('disabled', true);
  240. $spinner.css('visibility', 'visible');
  241. // Clear previous notices
  242. $('.studiou-wcpcm-notice-area').empty();
  243. // Debug: Log AJAX request details
  244. console.log('STUDIOU WC: Sending batch description AJAX request to:', studiouWcpcm.ajaxUrl);
  245. console.log('STUDIOU WC: Nonce:', studiouWcpcm.nonce);
  246. // Send AJAX request
  247. $.ajax({
  248. url: studiouWcpcm.ajaxUrl,
  249. type: 'POST',
  250. data: {
  251. action: 'studiou_wcpcm_batch_description',
  252. selected_categories: selectedCategories,
  253. description: description,
  254. nonce: studiouWcpcm.nonce
  255. },
  256. success: function(response) {
  257. console.log('STUDIOU WC: Batch description AJAX response:', response);
  258. if (response.success) {
  259. showNotice('success', studiouWcpcm.i18n.batchDescriptionSuccess || 'Batch description operation successful');
  260. // Show results
  261. $('#studiou-wcpcm-batch-description-message').html('<p>' + response.data.message + '</p>');
  262. $('.studiou-wcpcm-batch-description-results').show();
  263. } else {
  264. showNotice('error', response.data.message);
  265. }
  266. },
  267. error: function(xhr, status, error) {
  268. console.error('STUDIOU WC: Batch description AJAX error:', status, error);
  269. console.error('STUDIOU WC: Response text:', xhr.responseText);
  270. showNotice('error', (studiouWcpcm.i18n.batchDescriptionError || 'Batch description operation error') + ': ' + error);
  271. },
  272. complete: function() {
  273. // Hide spinner
  274. $submitBtn.prop('disabled', false);
  275. $spinner.css('visibility', 'hidden');
  276. }
  277. });
  278. });
  279. }
  280. /**
  281. * Initialize sample CSV download
  282. */
  283. function initSampleDownload() {
  284. $('#studiou-wcpcm-download-sample').on('click', function(e) {
  285. e.preventDefault();
  286. // Sample CSV content
  287. var csvContent = 'name,url-name,parent-name,description,display-type,visibility,protected-passwords,operation\n' +
  288. '"Clothing","clothing","","Top quality clothing","default","public","","A"\n' +
  289. '"Men","men","Clothing","Men\'s clothing","products","public","","A"\n' +
  290. '"Women","women","Clothing","Women\'s clothing","products","protected","password123","A"\n' +
  291. '"Kids","kids","Clothing","Kids clothing","products","public","","U"\n' +
  292. '"Accessories","accessories","","All accessories","default","public","","D"';
  293. // Create download link
  294. var blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
  295. var url = URL.createObjectURL(blob);
  296. var link = document.createElement('a');
  297. link.setAttribute('href', url);
  298. link.setAttribute('download', 'sample-product-categories.csv');
  299. link.style.visibility = 'hidden';
  300. document.body.appendChild(link);
  301. link.click();
  302. document.body.removeChild(link);
  303. });
  304. }
  305. /**
  306. * Show notice message
  307. *
  308. * @param {string} type Notice type (success, error)
  309. * @param {string} message Notice message
  310. */
  311. function showNotice(type, message) {
  312. var $notice = $('<div class="studiou-wcpcm-notice studiou-wcpcm-notice-' + type + '"></div>').text(message);
  313. $('.studiou-wcpcm-notice-area').append($notice);
  314. // Scroll to notice
  315. $('html, body').animate({
  316. scrollTop: $('.studiou-wcpcm-notice-area').offset().top - 50
  317. }, 500);
  318. }
  319. // Additional debug: Check if elements exist after DOM load
  320. $(window).on('load', function() {
  321. console.log('STUDIOU WC: Window loaded');
  322. console.log('STUDIOU WC: Move form exists:', $('#studiou-wcpcm-move-form').length > 0);
  323. console.log('STUDIOU WC: Move button exists:', $('#studiou-wcpcm-move-button').length > 0);
  324. console.log('STUDIOU WC: Source select exists:', $('#source_category').length > 0);
  325. console.log('STUDIOU WC: Target select exists:', $('#target_category').length > 0);
  326. console.log('STUDIOU WC: Batch description form exists:', $('#studiou-wcpcm-batch-description-form').length > 0);
  327. console.log('STUDIOU WC: Batch description button exists:', $('#studiou-wcpcm-batch-description-button').length > 0);
  328. });
  329. })(jQuery);