-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretrieve-submission-elementor-form.php
193 lines (157 loc) · 5.65 KB
/
retrieve-submission-elementor-form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
function submission_values_shortcode() {
global $wpdb;
// Enqueue the JavaScript library
wp_enqueue_script('xlsx-library', 'https://unpkg.com/xlsx/dist/xlsx.full.min.js', array(), '1.0', true);
// Query the wp_e_submissions_values table
$results = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}e_submissions_values");
// Organize the data into an associative array
$data = array();
foreach ($results as $result) {
$submission_id = $result->submission_id;
$key = $result->key;
$value = $result->value;
if (!isset($data[$submission_id])) {
$data[$submission_id] = array();
}
$data[$submission_id][$key] = $value;
}
// Filter data by $affiliate_username
$affiliate_username = do_shortcode('[affiliate_username]');
$filtered_data = array_filter($data, function($submission_data) use ($affiliate_username) {
$referrer = isset($submission_data['referrer']) ? $submission_data['referrer'] : '';
return $referrer == $affiliate_username;
});
// Pagination setup
$total_items = count($filtered_data);
$items_per_page = 2;
$total_pages = ceil($total_items / $items_per_page);
$current_page = max(1, get_query_var('cpage'));
$offset = ($current_page - 1) * $items_per_page;
$paged_data = array_slice($filtered_data, $offset, $items_per_page, true);
// Generate the HTML table with CSS styles
$output = '<style>
.submission-table {
width: 100%;
border-collapse: collapse;
}
.submission-table th, .submission-table td {
padding: 10px;
border: 1px solid #ccc;
}
.submission-table th {
background-color: #f2f2f2;
font-weight: bold;
}
.submission-table tr:nth-child(even) {
background-color: #f9f9f9;
}
@media (max-width: 768px) {
.submission-table th, .submission-table td {
padding: 5px;
}
.submission-table th {
font-size: 12px;
}
.submission-table td {
font-size: 12px;
}
}
.submission-pagination {
text-align: right;
margin-top: 10px;
margin-bottom:30px;
}
.submission-pagination span {
padding-left: 10px;
padding-right: 10px;
}
.submission-pagination a {
display: inline-block;
padding: 5px 10px;
background-color: #f2f2f2;
border: 1px solid #ccc;
text-decoration: none;
color: #333;
border-width: 1px;
border-style: solid;
-webkit-appearance: none;
border-radius: 3px;
white-space: nowrap;
box-sizing: border-box;
text-decoration: none!important;
}
.submission-pagination a:hover {
background-color: #e0e0e0;
}
.disable{
color: #a7aaad!important;
border-color: #dcdcde!important;
background: #f6f7f7!important;
box-shadow: none!important;
cursor: default;
transform: none!important;
padding: 10px;
border-width: 1px;
border-style: solid;
-webkit-appearance: none;
border-radius: 3px;
white-space: nowrap;
box-sizing: border-box;
}
.export{
padding: 4px;
padding-left:10px;
padding-right:10px;
margin-left: 10px;
background: #00b4ff !important;
color: #fff!important;
border-width: 1px;
border-style: solid;
border-color: #dcdcde!important;
}
</style>';
$output .= '<div class="submission-pagination">';
if ($current_page > 1) {
$output .= '<a href="'.add_query_arg('cpage', ($current_page - 1)).'">« Previous</a>';
} else {
$output .= '<span class="disable">« Previous</span>';
}
$output .= '<span>Page '.$current_page.' of '.$total_pages.'</span>';
if ($current_page < $total_pages) {
$output .= '<a href="'.add_query_arg('cpage', ($current_page + 1)).'">Next »</a>';
} else {
$output .= '<span class="disable" >Next »</span>';
}
// Add export button
$output .= '<button class="export" onclick="exportToExcel()">Export to Excel</button>';
$output .= '</div>';
$output .= '<table class="submission-table">';
$output .= '<tr><th>Name</th><th>Email</th><th>Phone</th><th>Referrer By</th></tr>';
foreach ($paged_data as $submission_data) {
$name = isset($submission_data['name']) ? $submission_data['name'] : '';
$email = isset($submission_data['email']) ? $submission_data['email'] : '';
$phone = isset($submission_data['phone']) ? $submission_data['phone'] : '';
$referrer = isset($submission_data['referrer']) ? $submission_data['referrer'] : '';
$output .= "<tr><td style='width: 300px;'>$name</td><td style='width: 300px;'>$email</td><td>$phone</td><td>$referrer</td></tr>";
}
$output .= '</table>';
// JavaScript function to handle export to Excel
$output .= '<script>
function exportToExcel() {
// Create a new Excel workbook
let wb = XLSX.utils.book_new();
// Convert the filtered data to an array of arrays
let data = Object.entries(' . json_encode($filtered_data) . ');
let sheetData = data.map(([_, rowData]) => Object.values(rowData));
// Create a worksheet from the data
let ws = XLSX.utils.aoa_to_sheet(sheetData);
// Add the worksheet to the workbook
XLSX.utils.book_append_sheet(wb, ws, "Submissions");
// Save the workbook as a file
XLSX.writeFile(wb, "submissions.xlsx");
}
</script>';
return $output;
}
add_shortcode('submission_values', 'submission_values_shortcode');