| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentSupport\App\Modules\PermissionManager; |
| 6 |
use FluentSupport\App\Services\Helper; |
| 7 |
use FluentSupport\App\Modules\Reporting\Reporting; |
| 8 |
use FluentSupport\App\Services\Csv\CsvWriter; |
| 9 |
|
| 10 |
class DataExporter |
| 11 |
{ |
| 12 |
public function exportReport() |
| 13 |
{ |
| 14 |
$this->verifyRequest(); |
| 15 |
$csvWriter = new CsvWriter(); |
| 16 |
$request = Helper::FluentSupport('request'); |
| 17 |
$from_date = $request->getSafe('from_date', 'sanitize_text_field'); |
| 18 |
$to_date = $request->getSafe('to_date', 'sanitize_text_field'); |
| 19 |
$columns = $request->get('columns', []); |
| 20 |
$columns = is_array($columns) ? array_map('sanitize_text_field', $columns) : []; |
| 21 |
$agents = $request->get('agents', []); |
| 22 |
// Normalize both caller shapes (array or comma-joined string) before agentSummary()'s explode(',', $agent). |
| 23 |
$agentIds = is_array($agents) ? $agents : explode(',', (string) $agents); |
| 24 |
$agents = implode(',', array_filter(array_map('intval', $agentIds))); |
| 25 |
if (empty($columns)) { |
| 26 |
$columns = Helper::getExportOptions(); |
| 27 |
} |
| 28 |
$columns = $this->defineColumns($columns); |
| 29 |
|
| 30 |
$reporting = new Reporting(); |
| 31 |
$data = $reporting->agentSummary($from_date, $to_date, $agents); |
| 32 |
|
| 33 |
$csvWriter->insertOne(array_keys($columns)); |
| 34 |
|
| 35 |
$rows = []; |
| 36 |
foreach ($data as $summary) { |
| 37 |
$row = []; |
| 38 |
foreach ($columns as $key => $column) { |
| 39 |
if (isset($summary[$column])) { |
| 40 |
$row[$key] = $summary[$column]; |
| 41 |
} else if (isset($summary['stats'][$column])) { |
| 42 |
$row[$key] = $summary['stats'][$column]; |
| 43 |
} else if (isset($summary['active_stat'][$column])) { |
| 44 |
$row[$key] = $summary['active_stat'][$column]; |
| 45 |
} |
| 46 |
} |
| 47 |
$rows[] = $row; |
| 48 |
} |
| 49 |
|
| 50 |
$csvWriter->insertAll($rows); |
| 51 |
|
| 52 |
$currentTime = current_time('timestamp'); |
| 53 |
$csvWriter->output('agent_report-' . gmdate('Y-m-d_H-i-s', $currentTime) . '.csv'); |
| 54 |
} |
| 55 |
|
| 56 |
private function defineColumns($columns) |
| 57 |
{ |
| 58 |
$arr = [ |
| 59 |
'Agent First Name' => 'first_name', |
| 60 |
'Agent Last Name' => 'last_name', |
| 61 |
'Agent Full Name' => 'full_name', |
| 62 |
'Responses' => 'responses', |
| 63 |
'Interactions' => 'interactions', |
| 64 |
'Open Tickets' => 'opens', |
| 65 |
'Closed' => 'closed', |
| 66 |
'Waiting Tickets' => 'waiting_tickets', |
| 67 |
'Average Waiting' => 'average_waiting', |
| 68 |
'Max Waiting' => 'max_waiting', |
| 69 |
]; |
| 70 |
|
| 71 |
if (Helper::isAgentFeedbackEnabled()) { |
| 72 |
$arr['Likes'] = 'likes'; |
| 73 |
$arr['Dislikes'] = 'dislikes'; |
| 74 |
} |
| 75 |
|
| 76 |
return array_filter($arr, function ($column, $key) use ($columns) { |
| 77 |
return in_array($key, $columns); |
| 78 |
}, ARRAY_FILTER_USE_BOTH); |
| 79 |
} |
| 80 |
|
| 81 |
private function getCsvWriter() |
| 82 |
{ |
| 83 |
if (!class_exists('\League\Csv\Writer')) { |
| 84 |
include FLUENT_SUPPORT_PLUGIN_PATH . 'app/Services/Libs/csv/autoload.php'; |
| 85 |
} |
| 86 |
|
| 87 |
return \League\Csv\Writer::createFromFileObject(new \SplTempFileObject()); |
| 88 |
} |
| 89 |
|
| 90 |
private function verifyRequest() |
| 91 |
{ |
| 92 |
$permission = 'fst_view_all_reports'; |
| 93 |
|
| 94 |
if ( ! isset($_REQUEST['_wpnonce']) || ! wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])), 'fluent-support') ) { |
| 95 |
wp_die('Security check failed'); |
| 96 |
} |
| 97 |
|
| 98 |
if ( PermissionManager::currentUserCan($permission) ) { |
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
wp_die('You do not have permission'); |
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|