| Current Path : /var/www/cesa.co.za/php/includes/ |
| Current File : /var/www/cesa.co.za/php/includes/EventSchedules.php |
<?php
include_once('BaseTableClass.php');
class EventSchedules extends BaseTableClass {
public function checkCreateTable() {
$checkCreateSQL = "DROP TABLE IF EXISTS `EventSchedules`;
CREATE TABLE IF NOT EXISTS `EventSchedules` (
`EventScheduleID` int(11) NOT NULL AUTO_INCREMENT,
`EventID` int(11) DEFAULT 0,
`ScheduleDate` date DEFAULT NULL,
`StartTime` time DEFAULT NULL,
`EndTime` time DEFAULT NULL,
PRIMARY KEY (`EventScheduleID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_general_ci;";
}
/**
* Create a schedule entry
* @param int $eventID
* @param string $scheduleDate
* @param string $startTime
* @param string $endTime
* @return int|false EventScheduleID on success, false on failure
*/
public function create($eventID, $scheduleDate, $startTime, $endTime) {
global $conni;
$eventID = (int)$eventID;
// Escape values properly
if (isset($conni) && ($conni instanceof mysqli)) {
$scheduleDate = mysqli_real_escape_string($conni, $scheduleDate);
$startTime = mysqli_real_escape_string($conni, $startTime);
$endTime = mysqli_real_escape_string($conni, $endTime);
} else {
$scheduleDate = addslashes($scheduleDate);
$startTime = addslashes($startTime);
$endTime = addslashes($endTime);
}
// Check if schedule already exists for this event and date
$existing = $this->sqlf->getRow("SELECT EventScheduleID FROM EventSchedules WHERE EventID = $eventID AND ScheduleDate = '$scheduleDate' LIMIT 1");
if ($existing) {
// Schedule already exists, return its ID
return $existing['EventScheduleID'];
}
$sql = "INSERT INTO EventSchedules (EventID, ScheduleDate, StartTime, EndTime)
VALUES ($eventID, '$scheduleDate', '$startTime', '$endTime')";
if ($this->sqlf->execute($sql)) {
if (isset($conni) && ($conni instanceof mysqli)) {
return mysqli_insert_id($conni);
} else {
// Try to get insert ID from sqlf if available
if (method_exists($this->sqlf, 'Insert_ID')) {
return $this->sqlf->Insert_ID();
}
return true; // Return true if we can't get ID but query succeeded
}
}
return false;
}
/**
* Create multiple schedule entries for date range
* @param int $eventID
* @param string $startDate
* @param string $endDate
* @param string $startTime
* @param string $endTime
* @param bool $isWeekly If true, only create entries for the same day of week
* @return array Array of created EventScheduleIDs
*/
public function createSchedulesForDateRange($eventID, $startDate, $endDate, $startTime = '08:30:00', $endTime = '16:30:00', $isWeekly = false) {
$scheduleIDs = [];
echo 'Creating event eschedules<br />';
// exit;
// Validate inputs
if (empty($startDate) || empty($endDate)) {
return $scheduleIDs;
}
echo 'Got start and end dates<br />';
try {
$start = new DateTime($startDate);
$end = new DateTime($endDate);
} catch (Exception $e) {
// Invalid date format, return empty array
echo 'Invalid date format: ' . $startDate . ' - ' . $endDate . '<br />';
return $scheduleIDs;
}
if ($isWeekly) {
// Same day of week - create one entry per week until end date
$dayOfWeek = $start->format('w'); // 0 = Sunday, 6 = Saturday
$current = clone $start;
while ($current <= $end) {
if ($current->format('w') == $dayOfWeek) {
$scheduleID = $this->create($eventID, $current->format('Y-m-d'), $startTime, $endTime);
if ($scheduleID) {
$scheduleIDs[] = $scheduleID;
}
$current->modify('+1 week');
} else {
$current->modify('+1 day');
}
}
} else {
// Daily - create one entry per day
$current = clone $start;
while ($current <= $end) {
$scheduleID = $this->create($eventID, $current->format('Y-m-d'), $startTime, $endTime);
if ($scheduleID) {
$scheduleIDs[] = $scheduleID;
}
$current->modify('+1 day');
}
}
return $scheduleIDs;
}
/**
* Check if schedules exist for an event
* @param int $eventID
* @return bool True if schedules exist, false otherwise
*/
public function schedulesExistForEvent($eventID) {
$eventID = (int)$eventID;
$result = $this->sqlf->getRow("SELECT COUNT(*) as count FROM EventSchedules WHERE EventID = $eventID");
return isset($result['count']) && $result['count'] > 0;
}
/**
* Get all schedules for an event
* @param int $eventID
* @return array Array of schedule records
*/
public function getByEventID($eventID) {
$eventID = (int)$eventID;
return $this->sqlf->getAll("SELECT * FROM EventSchedules WHERE EventID = $eventID ORDER BY ScheduleDate ASC");
}
}