class-export-handler.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. $date_from = isset($_POST['date_from']) ? sanitize_text_field($_POST['date_from']) : '';
  22. $date_to = isset($_POST['date_to']) ? sanitize_text_field($_POST['date_to']) : '';
  23. $statuses = isset($_POST['statuses']) ? array_map('sanitize_text_field', $_POST['statuses']) : array();
  24. // Get all data without pagination for export
  25. $report_data = $this->db_manager->get_product_categories_summary($date_from, $date_to, $statuses, 999999, 1, 'category', 'ASC');
  26. $settings = $this->settings_manager->get_settings();
  27. $default_property = $settings['default_property'] ?? '';
  28. $property_values = array();
  29. if (!empty($default_property)) {
  30. $property_values = $this->db_manager->get_property_values($default_property);
  31. }
  32. $this->generate_csv_output($report_data, $property_values);
  33. }
  34. private function generate_csv_output($report_data, $property_values) {
  35. // Set headers for CSV download
  36. header('Content-Type: text/csv');
  37. header('Content-Disposition: attachment; filename="custom-reports-' . date('Y-m-d') . '.csv"');
  38. header('Pragma: no-cache');
  39. header('Expires: 0');
  40. $output = fopen('php://output', 'w');
  41. // CSV headers
  42. $headers = $this->get_csv_headers($property_values);
  43. fputcsv($output, $headers);
  44. // CSV data
  45. foreach ($report_data as $row) {
  46. $csv_row = $this->format_csv_row($row, $property_values);
  47. fputcsv($output, $csv_row);
  48. }
  49. fclose($output);
  50. exit;
  51. }
  52. private function get_csv_headers($property_values) {
  53. $headers = array(
  54. __('Product Category', StudiouWC_Main::get_text_domain()),
  55. __('Orders Count', StudiouWC_Main::get_text_domain())
  56. );
  57. foreach ($property_values as $slug => $name) {
  58. $headers[] = $name . ' Count';
  59. }
  60. $headers[] = __('Total Price', StudiouWC_Main::get_text_domain());
  61. return $headers;
  62. }
  63. private function format_csv_row($row, $property_values) {
  64. $csv_row = array(
  65. $row['category'],
  66. $row['orders_count']
  67. );
  68. foreach ($property_values as $slug => $name) {
  69. $csv_row[] = $row['property_counts'][$slug] ?? 0;
  70. }
  71. $csv_row[] = $row['total_price'];
  72. return $csv_row;
  73. }
  74. }