Sindbad~EG File Manager
<?php
class EventManager {
private $db;
public function __construct() {
$this->db = Database::getInstance()->getConnection();
}
/**
* Get all events with optional filters
*/
public function getEvents($filters = []) {
$sql = "SELECT e.*,
(SELECT COUNT(*) FROM event_registrations er WHERE er.event_id = e.id) as registration_count,
(SELECT COUNT(*) FROM event_attendance ea WHERE ea.event_id = e.id) as attendance_count
FROM events e WHERE 1=1";
$params = [];
if (isset($filters['active']) && $filters['active']) {
$sql .= " AND e.is_active = 1";
}
if (isset($filters['featured']) && $filters['featured']) {
$sql .= " AND e.featured = 1";
}
if (isset($filters['public']) && $filters['public']) {
$sql .= " AND e.public_registration = 1";
}
if (isset($filters['upcoming']) && $filters['upcoming']) {
$sql .= " AND e.start_date >= NOW()";
}
if (isset($filters['event_type'])) {
$sql .= " AND e.event_type = :event_type";
$params['event_type'] = $filters['event_type'];
}
$sql .= " ORDER BY e.start_date ASC";
if (isset($filters['limit'])) {
$sql .= " LIMIT " . intval($filters['limit']);
}
$stmt = $this->db->prepare($sql);
$stmt->execute($params);
return $stmt->fetchAll();
}
/**
* Get event by ID
*/
public function getEventById($eventId) {
$stmt = $this->db->prepare("
SELECT e.*,
(SELECT COUNT(*) FROM event_registrations er WHERE er.event_id = e.id) as registration_count,
(SELECT COUNT(*) FROM event_attendance ea WHERE ea.event_id = e.id) as attendance_count
FROM events e
WHERE e.id = :id
");
$stmt->execute(['id' => $eventId]);
return $stmt->fetch();
}
/**
* Register for an event
*/
public function registerForEvent($eventId, $registrationData) {
try {
$this->db->beginTransaction();
// Generate tracking code
$trackingCode = $this->generateTrackingCode();
// Check if member exists
$memberId = null;
$userId = null;
$registrationType = 'guest';
if (!empty($registrationData['email'])) {
// Check if email belongs to a member
$stmt = $this->db->prepare("SELECT id FROM members WHERE email = :email");
$stmt->execute(['email' => $registrationData['email']]);
$member = $stmt->fetch();
if ($member) {
$memberId = $member['id'];
$registrationType = 'member';
}
}
// Insert registration
$stmt = $this->db->prepare("
INSERT INTO event_registrations (
event_id, member_id, user_id, first_name, last_name, email, phone,
registration_type, tracking_code, registration_data, status
) VALUES (
:event_id, :member_id, :user_id, :first_name, :last_name, :email, :phone,
:registration_type, :tracking_code, :registration_data, 'confirmed'
)
");
$result = $stmt->execute([
'event_id' => $eventId,
'member_id' => $memberId,
'user_id' => $userId,
'first_name' => $registrationData['first_name'],
'last_name' => $registrationData['last_name'],
'email' => $registrationData['email'],
'phone' => $registrationData['phone'] ?? null,
'registration_type' => $registrationType,
'tracking_code' => $trackingCode,
'registration_data' => json_encode($registrationData)
]);
if ($result) {
$registrationId = $this->db->lastInsertId();
// Create tracking code entry
$this->createTrackingCodeEntry($eventId, $memberId, $userId, $trackingCode);
$this->db->commit();
// Send confirmation email
$event = $this->getEventById($eventId);
$registrationData['tracking_code'] = $trackingCode;
$registrationData['registration_type'] = $registrationType;
$this->sendRegistrationEmail($registrationData, $event);
return [
'success' => true,
'registration_id' => $registrationId,
'tracking_code' => $trackingCode,
'message' => 'Registration successful!'
];
}
$this->db->rollback();
return ['success' => false, 'message' => 'Registration failed'];
} catch (Exception $e) {
$this->db->rollback();
return ['success' => false, 'message' => $e->getMessage()];
}
}
/**
* Get registration by tracking code
*/
public function getRegistrationByTrackingCode($trackingCode) {
$stmt = $this->db->prepare("
SELECT er.*, e.name as event_name, e.description as event_description,
e.start_date, e.end_date, e.location_type,
m.first_name as member_first_name, m.last_name as member_last_name
FROM event_registrations er
LEFT JOIN events e ON er.event_id = e.id
LEFT JOIN members m ON er.member_id = m.id
WHERE er.tracking_code = :tracking_code
");
$stmt->execute(['tracking_code' => $trackingCode]);
return $stmt->fetch();
}
/**
* Record attendance
*/
public function recordAttendance($eventId, $registrationId = null, $attendanceData = []) {
try {
$stmt = $this->db->prepare("
INSERT INTO event_attendance (
event_id, registration_id, member_id, user_id, first_name, last_name,
attendance_type, notes, recorded_by
) VALUES (
:event_id, :registration_id, :member_id, :user_id, :first_name, :last_name,
:attendance_type, :notes, :recorded_by
)
");
$result = $stmt->execute([
'event_id' => $eventId,
'registration_id' => $registrationId,
'member_id' => $attendanceData['member_id'] ?? null,
'user_id' => $attendanceData['user_id'] ?? null,
'first_name' => $attendanceData['first_name'],
'last_name' => $attendanceData['last_name'],
'attendance_type' => $attendanceData['attendance_type'] ?? 'registered',
'notes' => $attendanceData['notes'] ?? null,
'recorded_by' => $_SESSION['user_id'] ?? null
]);
if ($result && $registrationId) {
// Update registration status to attended
$stmt = $this->db->prepare("
UPDATE event_registrations
SET status = 'attended', attended_at = NOW()
WHERE id = :id
");
$stmt->execute(['id' => $registrationId]);
}
return ['success' => $result, 'message' => $result ? 'Attendance recorded' : 'Failed to record attendance'];
} catch (Exception $e) {
return ['success' => false, 'message' => $e->getMessage()];
}
}
/**
* Get event registrations
*/
public function getEventRegistrations($eventId, $status = null) {
$sql = "SELECT er.*, m.first_name as member_first_name, m.last_name as member_last_name
FROM event_registrations er
LEFT JOIN members m ON er.member_id = m.id
WHERE er.event_id = :event_id";
$params = ['event_id' => $eventId];
if ($status) {
$sql .= " AND er.status = :status";
$params['status'] = $status;
}
$sql .= " ORDER BY er.registered_at DESC";
$stmt = $this->db->prepare($sql);
$stmt->execute($params);
return $stmt->fetchAll();
}
/**
* Get event attendance
*/
public function getEventAttendance($eventId) {
$stmt = $this->db->prepare("
SELECT ea.*, er.tracking_code,
m.first_name as member_first_name,
m.last_name as member_last_name,
m.membershipcard_id
FROM event_attendance ea
LEFT JOIN event_registrations er ON ea.registration_id = er.id
LEFT JOIN members m ON ea.member_id = m.id
WHERE ea.event_id = :event_id
ORDER BY ea.check_in_time DESC
");
$stmt->execute(['event_id' => $eventId]);
return $stmt->fetchAll();
}
/**
* Generate unique tracking code
*/
private function generateTrackingCode() {
do {
$code = 'EVT' . date('Y') . strtoupper(substr(uniqid(), -6));
$stmt = $this->db->prepare("SELECT id FROM event_registrations WHERE tracking_code = :code");
$stmt->execute(['code' => $code]);
} while ($stmt->fetch());
return $code;
}
/**
* Create tracking code entry in memberuser_codes table
*/
private function createTrackingCodeEntry($eventId, $memberId, $userId, $trackingCode) {
try {
$stmt = $this->db->prepare("
INSERT INTO memberuser_codes (
code, description, member_id, user_id, event_id, code_type, tracking_code, created_by
) VALUES (
:code, :description, :member_id, :user_id, :event_id, :code_type, :tracking_code, :created_by
)
");
$stmt->execute([
'code' => $trackingCode,
'description' => 'Event registration tracking code',
'member_id' => $memberId,
'user_id' => $userId,
'event_id' => $eventId,
'code_type' => $memberId ? 'member' : 'user',
'tracking_code' => $trackingCode,
'created_by' => $_SESSION['user_id'] ?? 1
]);
} catch (Exception $e) {
// Log error but don't fail the registration
error_log("Failed to create tracking code entry: " . $e->getMessage());
}
}
/**
* Send registration confirmation email
*/
public function sendRegistrationEmail($registrationData, $eventData) {
try {
require_once 'EmailService.php';
$emailService = new EmailService();
// Prepare email data
$emailData = [
'recipient_email' => $registrationData['email'],
'recipient_name' => $registrationData['first_name'] . ' ' . $registrationData['last_name'],
'subject' => 'Event Registration Confirmation - ' . $eventData['name'],
'body' => $this->generateRegistrationEmailBody($registrationData, $eventData),
'email_type' => 'general'
];
return $emailService->queueEmail($emailData);
} catch (Exception $e) {
error_log("Failed to send registration email: " . $e->getMessage());
return false;
}
}
/**
* Generate registration email body
*/
private function generateRegistrationEmailBody($registrationData, $eventData) {
$html = '<h2>Event Registration Confirmation</h2>';
$html .= '<p>Dear ' . htmlspecialchars($registrationData['first_name']) . ',</p>';
$html .= '<p>Thank you for registering for <strong>' . htmlspecialchars($eventData['name']) . '</strong>.</p>';
$html .= '<h3>Event Details:</h3>';
$html .= '<ul>';
$html .= '<li><strong>Event:</strong> ' . htmlspecialchars($eventData['name']) . '</li>';
$html .= '<li><strong>Date:</strong> ' . date('F j, Y', strtotime($eventData['start_date'])) . '</li>';
$html .= '<li><strong>Time:</strong> ' . date('g:i A', strtotime($eventData['start_date'])) . '</li>';
if ($eventData['location_type']) {
$html .= '<li><strong>Location:</strong> ' . ucfirst($eventData['location_type']) . '</li>';
}
$html .= '</ul>';
$html .= '<h3>Registration Details:</h3>';
$html .= '<ul>';
$html .= '<li><strong>Tracking Code:</strong> ' . htmlspecialchars($registrationData['tracking_code']) . '</li>';
$html .= '<li><strong>Registration Type:</strong> ' . ucfirst($registrationData['registration_type']) . '</li>';
$html .= '</ul>';
$html .= '<p>Please keep your tracking code safe as you will need it to check your registration status and for event check-in.</p>';
$html .= '<p>If you have any questions, please contact us.</p>';
$html .= '<p>We look forward to seeing you at the event!</p>';
$html .= '<p>Best regards,<br>Event Management Team</p>';
return $html;
}
}
?>
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists