| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- /**
- * Export Handler Class - Handles CSV export functionality
- *
- * @package StudiouWCCustomReports
- */
- if (!defined('ABSPATH')) {
- exit;
- }
- class StudiouWC_Export_Handler {
-
- private $db_manager;
- private $settings_manager;
-
- public function __construct($db_manager, $settings_manager) {
- $this->db_manager = $db_manager;
- $this->settings_manager = $settings_manager;
- }
-
- public function export_csv() {
- if (!current_user_can('manage_woocommerce')) {
- wp_die(__('You do not have sufficient permissions to access this page.', StudiouWC_Main::get_text_domain()));
- }
-
- $date_from = isset($_POST['date_from']) ? sanitize_text_field($_POST['date_from']) : '';
- $date_to = isset($_POST['date_to']) ? sanitize_text_field($_POST['date_to']) : '';
- $statuses = isset($_POST['statuses']) ? array_map('sanitize_text_field', $_POST['statuses']) : array();
-
- // Get all data without pagination for export
- $report_data = $this->db_manager->get_product_categories_summary($date_from, $date_to, $statuses, 999999, 1, 'category', 'ASC');
-
- $settings = $this->settings_manager->get_settings();
- $default_property = $settings['default_property'] ?? '';
- $property_values = array();
-
- if (!empty($default_property)) {
- $property_values = $this->db_manager->get_property_values($default_property);
- }
-
- $this->generate_csv_output($report_data, $property_values);
- }
-
- private function generate_csv_output($report_data, $property_values) {
- // Set headers for CSV download
- header('Content-Type: text/csv');
- header('Content-Disposition: attachment; filename="custom-reports-' . date('Y-m-d') . '.csv"');
- header('Pragma: no-cache');
- header('Expires: 0');
-
- $output = fopen('php://output', 'w');
-
- // CSV headers
- $headers = $this->get_csv_headers($property_values);
- fputcsv($output, $headers);
-
- // CSV data
- foreach ($report_data as $row) {
- $csv_row = $this->format_csv_row($row, $property_values);
- fputcsv($output, $csv_row);
- }
-
- fclose($output);
- exit;
- }
-
- private function get_csv_headers($property_values) {
- $headers = array(
- __('Product Category', StudiouWC_Main::get_text_domain()),
- __('Orders Count', StudiouWC_Main::get_text_domain())
- );
-
- foreach ($property_values as $slug => $name) {
- $headers[] = $name . ' Count';
- }
-
- $headers[] = __('Total Price', StudiouWC_Main::get_text_domain());
-
- return $headers;
- }
-
- private function format_csv_row($row, $property_values) {
- $csv_row = array(
- $row['category'],
- $row['orders_count']
- );
-
- foreach ($property_values as $slug => $name) {
- $csv_row[] = $row['property_counts'][$slug] ?? 0;
- }
-
- $csv_row[] = $row['total_price'];
-
- return $csv_row;
- }
- }
|