admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('camas_nonce')
));
wp_add_inline_script('jquery', $this->get_dashboard_js());
}
public function get_dashboard_js() {
$js = <<<'JS'
jQuery(document).ready(function($) {
loadStatistics();
$('#sync-button').on('click', function(e) {
e.preventDefault();
loadMembers();
});
$('#send-reminder-button').on('click', function(e) {
e.preventDefault();
sendReminders();
});
function loadMembers() {
var status = $('#sync-status');
status.html('
Loading...
');
$.ajax({
url: camasAjax.ajaxurl,
method: 'POST',
data: {
action: 'camas_get_members_list',
nonce: camasAjax.nonce
},
success: function(response) {
if (response.success) {
var members = response.data;
var html = '';
if (members.length === 0) {
html = 'No members found ';
} else {
members.forEach(function(m) {
html += '' + (m.first_name || '') + ' ';
html += '' + (m.last_name || '') + ' ';
html += '' + (m.email || '') + ' ';
html += '' + (m.membership_type || '') + ' ';
html += '' + (m.created_at || '') + ' ';
});
}
$('#members-list').html(html);
status.html('Members loaded!
');
} else {
status.html('Error: ' + response.data + '
');
}
},
error: function() {
status.html('AJAX Error
');
}
});
}
function loadStatistics() {
$.ajax({
url: camasAjax.ajaxurl,
method: 'POST',
data: {
action: 'camas_get_statistics',
nonce: camasAjax.nonce
},
success: function(response) {
if (response.success) {
$('#stat-total').text(response.data.total);
$('#stat-active').text(response.data.active);
$('#stat-collected').text(response.data.collected);
$('#stat-pending').text(response.data.pending);
}
}
});
}
function sendReminders() {
var type = $('#reminder-type').val();
var msg = $('#reminder-message').val();
var status = $('#reminder-status');
if (!msg.trim()) {
status.html('Please enter a message
');
return;
}
status.html('Sending...
');
$.ajax({
url: camasAjax.ajaxurl,
method: 'POST',
data: {
action: 'camas_send_reminder_email',
nonce: camasAjax.nonce,
reminder_type: type,
message: msg
},
success: function(response) {
if (response.success) {
status.html('Sent to ' + response.data.count + ' members!
');
$('#reminder-message').val('');
} else {
status.html('Error: ' + response.data + '
');
}
},
error: function() {
status.html('AJAX Error
');
}
});
}
});
JS;
return $js;
}
public function render_dashboard_page() {
echo '';
echo '
CamAS Treasurer Dashboard ';
echo '
';
echo '
';
echo '
';
echo '
';
echo '
';
echo '
';
echo '
';
echo '
Members List ';
echo '
Click below to sync data from Fluent Forms:
';
echo '
Refresh Members List ';
echo '
';
echo '
';
echo 'First Name Last Name Email Membership Type Join Date ';
echo 'Click Refresh to load members... ';
echo '
';
echo '
';
echo '
Send Email Reminders ';
echo '
Send to: All Members Pending Payment Members
';
echo '
Message:
';
echo '
Send Reminders ';
echo '
';
echo '
';
}
public function ajax_get_members_list() {
check_ajax_referer('camas_nonce', 'nonce');
global $wpdb;
$entries = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}fluentform_submissions WHERE form_id = 3 ORDER BY created_at DESC");
$members = array();
foreach ($entries as $entry) {
$data = json_decode($entry->response, true);
$members[] = array(
'first_name' => isset($data['first_name']) ? $data['first_name'] : '',
'last_name' => isset($data['last_name']) ? $data['last_name'] : '',
'email' => isset($data['email']) ? $data['email'] : '',
'membership_type' => isset($data['membership_type']) ? $data['membership_type'] : '',
'created_at' => $entry->created_at
);
}
wp_send_json_success($members);
}
public function ajax_get_statistics() {
check_ajax_referer('camas_nonce', 'nonce');
global $wpdb;
$total = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->prefix}fluentform_submissions WHERE form_id = 3");
wp_send_json_success(array(
'total' => intval($total),
'active' => 0,
'collected' => 0,
'pending' => 0
));
}
public function ajax_send_reminder_email() {
check_ajax_referer('camas_nonce', 'nonce');
global $wpdb;
$reminder_type = sanitize_text_field($_POST['reminder_type']);
$message = sanitize_textarea_field($_POST['message']);
$entries = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}fluentform_submissions WHERE form_id = 3");
$count = 0;
foreach ($entries as $entry) {
$data = json_decode($entry->response, true);
$email = isset($data['email']) ? $data['email'] : '';
if ($email && is_email($email)) {
wp_mail($email, 'CamAS e.V. - Reminder', $message, array('Content-Type: text/plain; charset=UTF-8'));
$count++;
}
}
wp_send_json_success(array('count' => $count));
}
}
new CAMAS_Treasurer_Dashboard();
}
}
}
}
}
}
}
}
}
}
}
}
})
}
}
}
}
}
})
}
}
}
})
}
}
}
}
}
})
}
})
})
})
}
}
}
}
}
}
}