/** * Saves all *new* comments to the db. * $mysqli : manual db connection (for transaction handling) * $bookingId : booking id for this allocation */ function save($mysqli, $bookingId) { foreach ($this->comments as $comment) { if ($comment->id == 0) { // ignore $commentId for now, it needs to remain set to 0 in case we rollback on exception $comment->bookingId = $bookingId; $commentId = BookingDBO::insertBookingComment($mysqli, $comment); } } }
/** * Updates the booking dates for the given allocation. * Only those dates that appear in *both* the old and new arrays *and* are different will be updated. * $mysqli : database link (to enforce manual transaction handling) * $allocationId : id of parent allocation record * $oldBookingDates : current array() of BookingDate indexed by date (d.m.Y) * $newBookingDates : new updated array() of BookingDate indexed by date (d.m.Y) */ static function updateBookingDates($mysqli, $allocationId, $oldBookingDates, $newBookingDates) { global $wpdb; error_log("updateBookingDates {$allocationId} "); // these are the dates that exist in both old and new $bookingDates = array_intersect_key($oldBookingDates, $newBookingDates); error_log("updateBookingDates intersection " . var_export($bookingDates, true)); // fetch allocation details $allocationRs = self::fetchAllocationForId($mysqli, $allocationId); $stmt = $mysqli->prepare("UPDATE " . $wpdb->prefix . "bookingdates \r\n SET status = ?,\r\n checked_out = ?,\r\n last_updated_by = ?,\r\n last_updated_date = NOW()\r\n WHERE allocation_id = ?\r\n AND booking_date = STR_TO_DATE(?, '%d.%m.%Y')"); $userLogin = wp_get_current_user()->user_login; $auditMsg = ""; foreach ($bookingDates as $bd => $bdObj) { // only apply where the status has changed $hasChanged = false; if ($oldBookingDates[$bd]->status !== $newBookingDates[$bd]->status) { $auditMsg .= "{$bd} => " . $newBookingDates[$bd]->status . ", "; $hasChanged = true; } if ($oldBookingDates[$bd]->checkedOut !== $newBookingDates[$bd]->checkedOut) { $hasChanged = true; } // do db update using the same statement if ($hasChanged) { $checkedOut = $newBookingDates[$bd]->checkedOut ? 'Y' : 'N'; $stmt->bind_param('sssis', $newBookingDates[$bd]->status, $checkedOut, $userLogin, $allocationId, $bd); if (false === $stmt->execute()) { throw new DatabaseException("Error during UPDATE: " . $mysqli->error); } } } $auditMsg = rtrim($auditMsg, ', '); $stmt->close(); // if blank, we didn't actually do anything if ($auditMsg !== '') { // keep an audit trail... $auditMsg = "Updating dates for allocation {$allocationId} ({$allocationRs->guest_name}) and " . $allocationRs->resource_name . ": " . $auditMsg; BookingDBO::insertBookingComment($mysqli, new BookingComment($allocationRs->booking_id, $auditMsg, BookingComment::COMMENT_TYPE_AUDIT)); } }