From e05d07c1a1a5792c6c90d2947d7c9aedb1b4eae6 Mon Sep 17 00:00:00 2001
From: Andrea Manoni <a.manoni@imprimis.it>
Date: Wed, 19 Feb 2025 11:49:31 +0100
Subject: [PATCH] 1927# esportazione csv albero categorie

---
 admin/wp-connect-io6-admin-page.php | 45 ++++++++++++++++++++++++-----
 assets/css/main.css                 |  4 +--
 2 files changed, 39 insertions(+), 10 deletions(-)

diff --git a/admin/wp-connect-io6-admin-page.php b/admin/wp-connect-io6-admin-page.php
index 3b7c1d9..a533e8f 100755
--- a/admin/wp-connect-io6-admin-page.php
+++ b/admin/wp-connect-io6-admin-page.php
@@ -108,6 +108,9 @@ add_action('wp_ajax_io6-test-api', 'io6_test_api');
 add_action('wp_ajax_nopriv_io6-single-sync', 'io6_single_sync');
 add_action('wp_ajax_io6-single-sync', 'io6_single_sync');
 
+add_action('wp_ajax_nopriv_io6-export-categories', 'io6_export_categories');
+add_action('wp_ajax_io6-export-categories', 'io6_export_categories');
+
 add_action('wp_ajax_nopriv_importerone-statistic', 'io6_show_statistic');
 add_action('wp_ajax_importerone-statistic', 'io6_show_statistic');
 
@@ -152,6 +155,21 @@ function io6_save_post($post_id) {
 	}
 };
 
+function io6_export_categories(){
+	global $wpdb;
+
+	$sql = "SELECT tt.term_id as id_category, tt.parent as id_parent, t.name
+	FROM $wpdb->term_taxonomy tt
+	INNER JOIN $wpdb->terms t ON t.term_id = tt.term_id
+	WHERE taxonomy = 'product_cat'
+	ORDER BY tt.parent ASC";
+	$categories_tree = $wpdb->get_results($wpdb->prepare($sql));
+
+	if(!empty($categories_tree)){
+		prepareCSV($categories_tree, 'prestashop_categories_tree', true);
+	}
+}
+
 function io6_show_statistic(){
 	global $wpdb;
 
@@ -346,7 +364,7 @@ function io6_get_statistic(){
 
 			if($download){
 				if(!empty($result)){
-					prepareIo6StatisticCSV($result, 'io6_images_to_download');
+					prepareCSV($result, 'io6_images_to_download');
 				}
 			} else {
 				echo json_encode($rows, JSON_UNESCAPED_SLASHES);
@@ -371,7 +389,7 @@ function io6_get_statistic(){
 
 			if($download){
 				if(!empty($result)){
-					prepareIo6StatisticCSV($result, 'io6_products_not_synced');
+					prepareCSV($result, 'io6_products_not_synced');
 				}
 			} else {
 			echo json_encode($rows, JSON_UNESCAPED_SLASHES);
@@ -396,7 +414,7 @@ function io6_get_statistic(){
 
 			if($download){
 				if(!empty($result)){
-					prepareIo6StatisticCSV($result, 'io6_products_synced');
+					prepareCSV($result, 'io6_products_synced');
 				}
 			} else {
 			echo json_encode($rows, JSON_UNESCAPED_SLASHES);
@@ -424,7 +442,7 @@ function io6_get_statistic(){
 	
 			if($download){
 				if(!empty($result)){
-					prepareIo6StatisticCSV($result, 'io6_products_with_images');
+					prepareCSV($result, 'io6_products_with_images');
 				}
 			} else {
 				echo json_encode($rows, JSON_UNESCAPED_SLASHES);
@@ -452,7 +470,7 @@ function io6_get_statistic(){
 	
 			if($download){
 				if(!empty($result)){
-					prepareIo6StatisticCSV($result, 'io6_products_without_images');
+					prepareCSV($result, 'io6_products_without_images');
 				}
 			} else {
 			echo json_encode($rows, JSON_UNESCAPED_SLASHES);
@@ -484,7 +502,7 @@ function io6_get_statistic(){
 	
 			if($download){
 				if(!empty($result)){
-					prepareIo6StatisticCSV($result, 'io6_products_same_mpn');
+					prepareCSV($result, 'io6_products_same_mpn');
 				}
 			} else {
 			echo json_encode($rows, JSON_UNESCAPED_SLASHES);
@@ -516,7 +534,7 @@ function io6_get_statistic(){
 	
 			if($download){
 				if(!empty($result)){
-					prepareIo6StatisticCSV($result, 'io6_products_same_ean');
+					prepareCSV($result, 'io6_products_same_ean');
 				}
 			} else {
 			echo json_encode($rows, JSON_UNESCAPED_SLASHES);
@@ -528,7 +546,7 @@ function io6_get_statistic(){
 	  }
 }
 
-function prepareIo6StatisticCSV($result, $filename, $download = true){
+function prepareCSV($result, $filename, $download = true){
 	if($download){
 	  header('Content-Description: File Transfer');
 	  header('Content-Type: application/csv');
@@ -687,6 +705,17 @@ function admin_io6_settings()
 			'io6_section_general',
 			__('Impostazioni generali', IO6_DOMAIN),
 			function () {
+			?> 
+				<div class="panel export-categories-section">
+					<div>
+						<h4>Esporta categorie per mapping</h4>
+						<span>
+							Scarica l'albero categorie in formato CSV per popolare le categorie di un catalogo personale in ImporterONE.
+						</span>
+					</div>
+					<a name="io6-export-categories" id="io6-export-categories" class="button button-large" href="<?php site_url() ?>/wp-admin/admin-ajax.php?action=io6-export-categories">Esporta categorie</a>
+				</div>
+			<?php
 			},
 			'io6_section_general'
 		);
diff --git a/assets/css/main.css b/assets/css/main.css
index 6548f64..aeddb12 100755
--- a/assets/css/main.css
+++ b/assets/css/main.css
@@ -91,6 +91,6 @@ a[id^=btnShow_].button.button-small {
 	margin: 0px 5px;
 }
 
-.icecool-stats-container div{
-	margin-bottom: 20px;
+.export-categories-section h4{
+	margin: 1rem 0 0.5rem 0;
 }
\ No newline at end of file
-- 
GitLab