Files
SIGE-WEB-snapshot/wp-content/plugins/sige-events/includes/data_sharing.php

256 lines
8.4 KiB
PHP

<?php
function dataSharing()
{
// Disable cache for all users
nocache_headers();
// Append nocache query parameter to bust cache
if (!isset($_GET['nocache'])) {
$new_url = add_query_arg('nocache', time(), get_permalink());
wp_redirect($new_url);
exit();
}
// Prevent back/forward navigation cache
$output = '<script>
if (performance.navigation.type === performance.navigation.TYPE_BACK_FORWARD) {
window.location.reload();
}
</script>';
global $wpdb;
$current_user_id = get_current_user_id();
$current_year = date('Y');
$confidentiality_option = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM " . $wpdb->prefix . "sige_confidentiality_user_option WHERE user_id = %d AND year = %d",
$current_user_id,
$current_year
)
);
$output = "";
if (!$confidentiality_option) {
$confidentiality_message = $wpdb->get_var($wpdb->prepare("
SELECT message FROM {$wpdb->prefix}sige_confidentiality_message
WHERE information_type = %s
", 'Sharing Information'));
$output .= '
<style>
.modal {
display: block;
position: fixed;
z-index: 9999;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.4);
}
.modal-content {
background-color: #fefefe;
margin: 10% auto;
padding: 20px;
border: 1px solid #888;
width: 90%;
max-width: 400px;
text-align: center;
border-radius: 10px;
}
.modal-content button {
padding: 10px 20px;
margin: 10px;
border: none;
cursor: pointer;
width: 120px;
border-radius: 5px;
font-size: 16px;
}
.modal-content .agree {
background-color: #4CAF50;
color: white;
}
.modal-content .disagree {
background-color: #f44336;
color: white;
}
</style>
<div id="confidentialityModal" class="modal">
<div class="modal-content">
<h2>' . esc_html__('Do you authorize us sharing your e-mail and name data with our sponsors?', 'sige') . '</h2>
<p>' . wp_kses_post($confidentiality_message) . '</p>
<button class="agree" onclick="handleConfidentialityResponse(\'yes\')">' . esc_html__('Agree', 'sige') . '</button>
<button class="disagree" onclick="handleConfidentialityResponse(\'no\')">' . esc_html__('Disagree', 'sige') . '</button>
</div>
</div>
<script>
function handleConfidentialityResponse(response) {
const xhr = new XMLHttpRequest();
xhr.open("POST", "' . admin_url('admin-ajax.php') . '");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onload = function() {
if (xhr.status === 200) {
document.getElementById("confidentialityModal").style.display = "none";
location.reload();
}
};
xhr.send("action=save_confidentiality_response&user_id=' . $current_user_id . '&year=' . $current_year . '&response=" + encodeURIComponent(response));
}
</script>
';
}
return $output;
}
add_shortcode('data_sharing', 'dataSharing');
function retrieve_data_sponsors()
{
// Disable cache for all users
nocache_headers();
// Append nocache query parameter to bust cache
if (!isset($_GET['nocache'])) {
$new_url = add_query_arg('nocache', time(), get_permalink());
wp_redirect($new_url);
exit();
}
// Prevent back/forward navigation cache
$output = '<script>
if (performance.navigation.type === performance.navigation.TYPE_BACK_FORWARD) {
window.location.reload();
}
</script>';
global $wpdb;
$current_year = date('Y');
$query = $wpdb->prepare(
"
SELECT DISTINCT
u.user_login,
um.meta_value as full_name
FROM {$wpdb->prefix}sige_confidentiality_user_option co
JOIN {$wpdb->prefix}users u ON co.user_id = u.ID
JOIN {$wpdb->prefix}usermeta um ON u.ID = um.user_id
AND um.meta_key = 'full_name'
WHERE co.sharing_information = %s
AND co.year = %d ORDER BY full_name",
'yes ',
$current_year
);
$results = $wpdb->get_results($query);
// Add CSS styles
echo '<style>
.sponsor-data-container {
margin: 20px;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.sponsor-table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
.sponsor-table th, .sponsor-table td {
padding: 12px;
border: 1px solid #ddd;
text-align: left;
}
.sponsor-table th {
background-color: #f5f5f5;
font-weight: bold;
}
.sponsor-table tr:nth-child(even) {
background-color: #f9f9f9;
}
.sponsor-table tr:hover {
background-color: #f0f0f0;
}
.export-btn {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
margin-bottom: 20px;
}
.export-btn:hover {
background-color: #45a049;
}
</style>';
// Display the data table
echo '<div class="sponsor-data-container">';
echo '<h2>Dados de Participantes que optaram por oferecer informações de email para patrocinadores (' . $current_year . ')</h2>';
if (!empty($results)) {
// Export button
echo '<button class="export-btn" onclick="exportTableToCSV(\'sponsor_data_' . $current_year . '.csv\')">
Export to CSV
</button>';
// Table
echo '<table class="sponsor-table" id="sponsorTable">
<thead>
<tr>
<th>Full Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>';
foreach ($results as $row) {
echo '<tr>
<td>' . esc_html($row->full_name) . '</td>
<td>' . esc_html($row->user_login) . '</td>
</tr>';
}
echo '</tbody></table>';
// Add JavaScript for CSV export
echo '<script>
function exportTableToCSV(filename) {
var csv = [];
var rows = document.querySelectorAll("#sponsorTable tr");
for (var i = 0; i < rows.length; i++) {
var row = [], cols = rows[i].querySelectorAll("td, th");
for (var j = 0; j < cols.length; j++) {
var text = cols[j].innerText.replace(/"/g, \'"""\');
row.push(\'"\' + text + \'"\');
}
csv.push(row.join(","));
}
// Download CSV file
var csvFile = new Blob([csv.join("\\n")], {type: "text/csv"});
var downloadLink = document.createElement("a");
downloadLink.download = filename;
downloadLink.href = window.URL.createObjectURL(csvFile);
downloadLink.style.display = "none";
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
</script>';
} else {
echo '<p>No data available for ' . $current_year . '.</p>';
}
echo '</div>';
return ob_get_clean();
}
add_shortcode('sponsor_data', 'retrieve_data_sponsors');