function createRecurringBooking($conn, $username) { try { echo "<p id='log'>"; echo "Creating recurring booking...<br>"; // Sets all variables from the form POST // set table, throw error if empty // The value provided is now of format // 'type,table', so we need to explode this to find table name $tablestring = $_POST["selectresource"]; if (!$tablestring) { throw new Exception("Table cannot be empty<br>"); } // if // explodes string using comma as delimiter $str_explode = explode(",", $tablestring); // table is second element in array $table = $str_explode[1]; // table type is first element $type = $str_explode[0]; // set and check day $inputday = $_POST["selectday"]; if (!$inputday) { throw new Exception("Day cannot be empty<br>"); } // if // format day into 3 characters, and capitalise first char // so 'monday' -> 'Mon', for comparisons later $day = ucfirst(substr($inputday, 0, 3)); // Set and check periods // Since multiple periods may be selected, an array of available options is created $periodoptions = array("period1", "period2", "period3", "period4", "period5", "period6", "break", "lunch", "t9to10", "t10to11", "t11to12", "t12to13", "t13to14", "t14to15", "t15to16", "t16to17"); // Foreach element in array // We check if the checkbox associated with it has been checked // If so, we push it to our new empty array called periodsselected $periodsselected = []; foreach ($periodoptions as $p) { if (isset($_POST[$p])) { array_push($periodsselected, $p); } // if } // foreach // Now we know what checkboxes have been selected // Set description, does not check because description is allowed to be blank $description = $_POST["description"]; // If not booking whole day // type is set as either period/time/vehicle // each has their method of getting the numerical value of the period // for period based resources // Another array which will be used to store numerical values of // periods selected, e.g. [1, 3, 7] $periodsnumerical = []; foreach ($periodsselected as $p) { if ($type == "period") { // casts last character as an int, to get numerical value of 'n' $p = (int) substr($p, -1); } elseif ($type == "vehicle") { // use a switch to translate string to numerical value switch ($p) { case "period1": $p = 1; break; case "period2": $p = 2; break; case "break": $p = 3; break; case "period3": $p = 4; break; case "period4": $p = 5; break; case "lunch": $p = 6; break; case "period5": $p = 7; break; case "period6": $p = 8; break; default: $p = 1; break; } // switch } elseif ($type == "time") { // use a switch to translate switch ($p) { case "t9to10": $p = 1; break; case "t10to11": $p = 2; break; case "t11to12": $p = 3; break; case "t12to13": $p = 4; break; case "t13to14": $p = 5; break; case "t14to15": $p = 6; break; case "t15to16": $p = 7; break; case "t16to17": $p = 8; break; default: $p = 1; break; } // switch } else { throw new Exception("Problem with the resource type<br>"); } // else // Pushes period to numerical array array_push($periodsnumerical, $p); } // foreach if (empty($periodsnumerical)) { throw new Exception("Period cannot be empty<br>"); } // if //echo "Period(s):" . implode(", ",$periodsnumerical) . "<br>"; // set frequency and check if (!isset($_POST["frequency"])) { throw new Exception("Frequency cannot be empty<br>"); } $frequency = $_POST["frequency"]; if (!$frequency) { throw new Exception("Frequency cannot be empty<br>"); } // if // set start date $startdate = $_POST["startdate"]; if (!$startdate) { throw new Exception("Start date cannot be empty<br>"); } // if // checks if the start date given is in the past if (checkIfInThePast($startdate)) { throw new Exception("Start date cannot be in the past<br>"); } // if // set end date $enddate = $_POST["enddate"]; if (!$enddate) { throw new Exception("End date cannot be empty<br>"); } // if // checks if end date is in the past if (checkIfInThePast($enddate)) { throw new Exception("End date cannot be in the past<br>"); } // if // checks that end date is after start date if (strtotime($enddate) < strtotime($startdate)) { throw new Exception("End date should be after start date<br>"); } // if // Simple input validation is now complete // Now loop through each day, and try to book // each day/period for the table given // If already booked, report a warning // initialise currentdate to use // formats to YYYYMMDD if ($frequency == "biweekly") { // sets a boolean to say if booking is biweekly or not $isbiweekly = true; $isweekly = false; } else { $isbiweekly = false; $isweekly = true; } // reformat start and end dates $startdate = date("Ymd", strtotime($startdate)); $enddate = date("Ymd", strtotime($enddate)); $weekcount = 0; $currentdate = $startdate; $fullname = getFullname($conn, $username); $id = getID($conn, $username); // while current date is less than end date while (strtotime($currentdate) <= strtotime($enddate)) { // if current date is equal to date required if (findDayFromDate($currentdate) == $day) { // increment weekcount every time a booking is made $weekcount++; // if not biweekly, or is biweekly and this is an odd week // so that biweekly bookings occur 1st week, 3rd week, ... // loops through each period in the day and books it // Foreach period in numerical list foreach ($periodsnumerical as $pd) { if (!$isbiweekly || $isbiweekly && $weekcount % 2 != 0) { $sqlCheck = "SELECT * FROM {$table}\n WHERE BookingDate={$currentdate} AND BookingPeriod={$pd}"; $resultCheck = mysqli_query($conn, $sqlCheck); if (mysqli_num_rows($resultCheck) > 0) { echo "Warning: Record already exists for period {$pd} on" . styleDate($currentdate) . "<br>"; echo "Did not add record<br>"; } else { $id = $_SESSION["id"]; $fullname = $_SESSION["fullname"]; $sqlInsert = "INSERT INTO {$table}\n (BookingDate,BookedByID,BookedByName,BookingPeriod,BookingDesc)\n VALUES\n ({$currentdate},{$id},'{$fullname}',{$pd},'{$description}')"; $resultInsert = mysqli_query($conn, $sqlInsert); if ($resultInsert) { } else { echo "Error adding record: " . mysqli_error($conn) . "<br>"; } // else } // else } // if } // foreach } // if // increment current date $currentdate = strtotime("+1 day", strtotime($currentdate)); $currentdate = date("Ymd", $currentdate); } // while if ($isweekly) { $isweeklyval = 1; $isbiweeklyval = 0; } else { $isweeklyval = 0; $isbiweeklyval = 1; } // else // sets is multipleperiods if (count($periodsnumerical) > 1) { $ismultipleperiods = 1; } else { $ismultipleperiods = 0; } // else // If multiple periods, changes the recurperiod variable so that it is used // to store which days are recurred // So sets recur period to 135 if recurring occurs on periods 1,3, and 5 if ($ismultipleperiods) { $period = implode("", $periodsnumerical); } else { $period = $periodsnumerical[0]; } // else $sqlInsertRecur = "INSERT INTO RecurringBookings\n (Resource,StartDate,EndDate,BookedByID,BookedByName,RecurDay,\n RecurPeriod,isWeekly,isBiWeekly,isMultiplePeriods)\n VALUES\n ('{$table}',{$startdate},{$enddate},{$id},'{$fullname}','{$day}',{$period},\n {$isweeklyval},{$isbiweeklyval},{$ismultipleperiods})"; $resultInsertRecur = mysqli_query($conn, $sqlInsertRecur); if (!$resultInsertRecur) { echo "Error adding record to recurring bookings table:" . mysqli_error($conn) . "<br>"; } else { echo "<b>Successfully added a recurring booking</b><br>"; echo "Resource: " . styleResource($table, $type) . "<br>"; echo "Day: " . getFullDay($day) . "<br>"; // Account for multiple periods if (strlen($period) > 1) { echo "Periods: "; } else { echo "Period:"; } // else echo stylePeriod($period, $type) . "<br>"; if ($isweekly) { echo "Every week<br>"; } else { echo "Every fortnight<br>"; } // else echo "Between " . styleDateWithYears($startdate) . " and " . styleDateWithYears($enddate) . "<br>"; echo "Description: {$description} <br>"; } } catch (Exception $e) { echo "<b><br>An error occured:</b><br>"; echo $e->getMessage(); } finally { echo "<br><button id='dismiss'>Dismiss</button>"; echo "</p>"; } // finally }
function writeTable($conn, $showall) { $id = $_SESSION["id"]; $sqlGetBookings = "SELECT * FROM RecurringBookings\n WHERE BookedByID={$id}"; $resultGetBookings = mysqli_query($conn, $sqlGetBookings); $today = date('Ymd'); while ($row = mysqli_fetch_assoc($resultGetBookings)) { // if we want to show all bookings // or today's date is less than the end date of the booking if ($showall == true || strtotime($today) <= strtotime($row["EndDate"])) { echo "<tr>"; echo "<td>" . styleResource($row["Resource"]) . "</td>"; echo "<td>" . styleDay($row["RecurDay"]) . "</td>"; echo "<td>" . stylePeriod($row["RecurPeriod"], getResourceType($row["Resource"])) . "</td>"; echo "<td>"; if ($row["isWeekly"] == 1) { echo "Week"; } else { echo "Fortnight"; } echo "</td>"; echo "<td>" . styleDate($row["StartDate"]) . "</td>"; echo "<td>" . styleDate($row["EndDate"]) . "</td>"; ?> <td> <form method="POST" action="managerecurring.php"> <!--Has a confirm box to make sure user can cancel action--> <button type="submit" id="deletebooking" name="deletebooking" onclick="return confirm('Are you sure you want to delete this recurring record? \nThat includes ALL future bookings related to this recurrence!');" value=<?php echo "'" . $row["BookingID"] . "," . $row["Resource"] . "," . $row["BookedByID"] . "'"; ?> >Delete</button> </form> </td> <?php } // if } // while }