PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.2.0
Fluent Support – Helpdesk & Customer Support Ticket System v2.2.0
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / app / Hooks / Handlers / DataExporter.php

DataExporter.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.2.0, at app/Hooks/Handlers/DataExporter.php

104 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $agents = is_array($agents) ? array_map('intval', $agents) : [];
23 if (empty($columns)) {
24 $columns = Helper::getExportOptions();
25 }
26 $columns = $this->defineColumns($columns);
27
28 $reporting = new Reporting();
29 $data = $reporting->agentSummary($from_date, $to_date, $agents);
30
31 $csvWriter->insertOne(array_keys($columns));
32
33 $rows = [];
34 foreach ($data as $summary) {
35 $row = [];
36 foreach ($columns as $key => $column) {
37 if (isset($summary[$column])) {
38 $row[$key] = $summary[$column];
39 } else if (isset($summary['stats'][$column])) {
40 $row[$key] = $summary['stats'][$column];
41 } else if (isset($summary['active_stat'][$column])) {
42 $row[$key] = $summary['active_stat'][$column];
43 }
44 }
45 $rows[] = $row;
46 }
47
48 $csvWriter->insertAll($rows);
49
50 $currentTime = current_time('timestamp');
51 $csvWriter->output('agent_report-' . gmdate('Y-m-d_H-i-s', $currentTime) . '.csv');
52 }
53
54 private function defineColumns($columns)
55 {
56 $arr = [
57 'Agent First Name' => 'first_name',
58 'Agent Last Name' => 'last_name',
59 'Agent Full Name' => 'full_name',
60 'Responses' => 'responses',
61 'Interactions' => 'interactions',
62 'Open Tickets' => 'opens',
63 'Closed' => 'closed',
64 'Waiting Tickets' => 'waiting_tickets',
65 'Average Waiting' => 'average_waiting',
66 'Max Waiting' => 'max_waiting',
67 ];
68
69 if (Helper::isAgentFeedbackEnabled()) {
70 $arr['Likes'] = 'likes';
71 $arr['Dislikes'] = 'dislikes';
72 }
73
74 return array_filter($arr, function ($column, $key) use ($columns) {
75 return in_array($key, $columns);
76 }, ARRAY_FILTER_USE_BOTH);
77 }
78
79 private function getCsvWriter()
80 {
81 if (!class_exists('\League\Csv\Writer')) {
82 include FLUENT_SUPPORT_PLUGIN_PATH . 'app/Services/Libs/csv/autoload.php';
83 }
84
85 return \League\Csv\Writer::createFromFileObject(new \SplTempFileObject());
86 }
87
88 private function verifyRequest()
89 {
90 $permission = 'fst_view_all_reports';
91
92 if ( ! isset($_REQUEST['_wpnonce']) || ! wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])), 'fluent-support') ) {
93 wp_die('Security check failed');
94 }
95
96 if ( PermissionManager::currentUserCan($permission) ) {
97 return true;
98 }
99
100 wp_die('You do not have permission');
101 }
102
103 }
104