class-export-handler.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Export Handler Class - Handles CSV export functionality
  4. *
  5. * @package StudiouWCCustomReports
  6. */
  7. if (!defined('ABSPATH')) {
  8. exit;
  9. }
  10. class StudiouWC_Export_Handler {
  11. private $db_manager;
  12. private $settings_manager;
  13. public function __construct($db_manager, $settings_manager) {
  14. $this->db_manager = $db_manager;
  15. $this->settings_manager = $settings_manager;
  16. }
  17. public function export_csv() {
  18. if (!current_user_can('manage_woocommerce')) {
  19. wp_die(__('You do not have sufficient permissions to access this page.', StudiouWC_Main::get_text_domain()));
  20. }
  21. $view = isset($_POST['view']) ? sanitize_text_field($_POST['view']) : 'product_categories_summary';
  22. $date_from = isset($_POST['date_from']) ? sanitize_text_field($_POST['date_from']) : '';
  23. $date_to = isset($_POST['date_to']) ? sanitize_text_field($_POST['date_to']) : '';
  24. $statuses = isset($_POST['statuses']) ? array_map('sanitize_text_field', $_POST['statuses']) : array();
  25. if ($view === 'order_customers_list') {
  26. $report_data = $this->db_manager->get_order_customers_list($date_from, $date_to, $statuses, 999999, 1, 'customer_name', 'ASC');
  27. $this->generate_customers_csv_output($report_data);
  28. } else {
  29. // Get all data without pagination for export
  30. $report_data = $this->db_manager->get_product_categories_summary($date_from, $date_to, $statuses, 999999, 1, 'category', 'ASC');
  31. $settings = $this->settings_manager->get_settings();
  32. $default_property = $settings['default_property'] ?? '';
  33. $property_values = array();
  34. if (!empty($default_property)) {
  35. $property_values = $this->db_manager->get_property_values($default_property);
  36. }
  37. $this->generate_csv_output($report_data, $property_values);
  38. }
  39. }
  40. private function generate_csv_output($report_data, $property_values) {
  41. // Set headers for CSV download
  42. header('Content-Type: text/csv; charset=utf-8');
  43. $filename = sprintf(
  44. /* translators: %s: current date */
  45. __('custom-reports-%s.csv', StudiouWC_Main::get_text_domain()),
  46. date('Y-m-d')
  47. );
  48. header('Content-Disposition: attachment; filename="' . $filename . '"');
  49. header('Pragma: no-cache');
  50. header('Expires: 0');
  51. $output = fopen('php://output', 'w');
  52. // Add UTF-8 BOM for Excel compatibility
  53. fprintf($output, chr(0xEF).chr(0xBB).chr(0xBF));
  54. // CSV headers
  55. $headers = $this->get_csv_headers($property_values);
  56. fputcsv($output, $headers);
  57. // CSV data
  58. foreach ($report_data as $row) {
  59. $csv_row = $this->format_csv_row($row, $property_values);
  60. fputcsv($output, $csv_row);
  61. }
  62. fclose($output);
  63. exit;
  64. }
  65. private function generate_customers_csv_output($report_data) {
  66. header('Content-Type: text/csv; charset=utf-8');
  67. $filename = sprintf(
  68. /* translators: %s: current date */
  69. __('order-customers-%s.csv', StudiouWC_Main::get_text_domain()),
  70. date('Y-m-d')
  71. );
  72. header('Content-Disposition: attachment; filename="' . $filename . '"');
  73. header('Pragma: no-cache');
  74. header('Expires: 0');
  75. $output = fopen('php://output', 'w');
  76. // Add UTF-8 BOM for Excel compatibility
  77. fprintf($output, chr(0xEF).chr(0xBB).chr(0xBF));
  78. // CSV headers
  79. fputcsv($output, array(
  80. __('Customer Name', StudiouWC_Main::get_text_domain()),
  81. __('Email', StudiouWC_Main::get_text_domain()),
  82. __('Phone', StudiouWC_Main::get_text_domain()),
  83. __('Orders Count', StudiouWC_Main::get_text_domain()),
  84. __('Total Orders Price', StudiouWC_Main::get_text_domain()),
  85. ));
  86. // CSV data
  87. foreach ($report_data as $row) {
  88. fputcsv($output, array(
  89. $row['customer_name'],
  90. $row['customer_email'],
  91. $row['customer_phone'],
  92. $row['orders_count'],
  93. strip_tags(wc_price($row['total_price'])),
  94. ));
  95. }
  96. fclose($output);
  97. exit;
  98. }
  99. private function get_csv_headers($property_values) {
  100. $headers = array(
  101. __('Product Category', StudiouWC_Main::get_text_domain()),
  102. __('Orders Count', StudiouWC_Main::get_text_domain())
  103. );
  104. foreach ($property_values as $slug => $name) {
  105. /* translators: %s: property name */
  106. $headers[] = sprintf(__('%s Count', StudiouWC_Main::get_text_domain()), $name);
  107. }
  108. $headers[] = __('Total Price', StudiouWC_Main::get_text_domain());
  109. return $headers;
  110. }
  111. private function format_csv_row($row, $property_values) {
  112. $csv_row = array(
  113. $row['category'],
  114. $row['orders_count']
  115. );
  116. foreach ($property_values as $slug => $name) {
  117. $csv_row[] = $row['property_counts'][$slug] ?? 0;
  118. }
  119. // Format price without HTML for CSV
  120. $csv_row[] = strip_tags(wc_price($row['total_price']));
  121. return $csv_row;
  122. }
  123. }