Refactor conditional logic in db_object_t and implement booking notifications in order_bookings_o
- Optimized conditional checks and improved value handling (null, boolean, numeric, strings) in `db_object_t`. - Added `notifyNewBooking` method in `order_bookings_o` to handle department notifications for new bookings. - Updated Slack message template for booking notifications.
This commit is contained in:
@@ -62,7 +62,7 @@ class order_bookings_o extends db
|
||||
public function onAfterNewBooking(): void
|
||||
{
|
||||
self::requireSelected();
|
||||
// TODO: Send department notification (Slack, SMS)
|
||||
$this->notifyNewBooking(); // Notify the department about the new booking
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,6 +73,9 @@ class order_bookings_o extends db
|
||||
public function notifyNewBooking(): void
|
||||
{
|
||||
self::requireSelected();
|
||||
/**
|
||||
* Department booking notification
|
||||
*/
|
||||
// Get the department from the booking
|
||||
$department = (new departments_o())->select((int)$this->department->value());
|
||||
if (!$department->exists()) {
|
||||
@@ -94,7 +97,7 @@ class order_bookings_o extends db
|
||||
if ($deliverSlack) {
|
||||
// Construct email
|
||||
$message = "*" . $branding->name->value() . "*\n";
|
||||
$message .= "_New Booking Created_\n\n";
|
||||
$message .= "_New Booking Created (TEST)_\n\n";
|
||||
$message .= "*Customer:* " . $customer->getCustomerName($customer_array['customer_number']) . " (`" . $customer_array['customer_number'] . "`)\n";
|
||||
// Send a notification to the department
|
||||
$slack = new slack();
|
||||
|
||||
@@ -1155,38 +1155,35 @@ trait db_object_t
|
||||
if ($value === false) {
|
||||
throw new Exception('Failed to encode value for key: ' . $key . ' - ' . json_last_error_msg());
|
||||
}
|
||||
$set[] = "$key = '$value'";
|
||||
continue;
|
||||
}
|
||||
// If the value is null, set it to null
|
||||
if ($value === null || (is_string($value) && strtolower($value) === 'null')) {
|
||||
elseif ($value === null || (is_string($value) && strtolower($value) === 'null')) {
|
||||
$value = null;
|
||||
$set[] = "$key = NULL";
|
||||
continue;
|
||||
}
|
||||
// If the value is numeric, don't escape it
|
||||
if (is_numeric($value) && strlen((int)$value) == strlen($value)) {
|
||||
elseif (is_numeric($value) && strlen((int)$value) == strlen($value)) {
|
||||
$value = (int)$value;
|
||||
$set[] = "$key = $value";
|
||||
continue;
|
||||
}
|
||||
|
||||
// If the value is boolean, convert it to 1 or 0
|
||||
if (is_bool($value)) {
|
||||
elseif (is_bool($value)) {
|
||||
$value = $value ? 1 : 0;
|
||||
$set[] = "$key = $value";
|
||||
continue;
|
||||
} else {
|
||||
// If the value is a string, escape it
|
||||
$value = $db->escape_string($value);
|
||||
$set[] = "$key = '$value'";
|
||||
}
|
||||
|
||||
// If the value is a string, escape it
|
||||
$value = $db->escape_string($value);
|
||||
$set[] = "$key = '$value'";
|
||||
}
|
||||
$set = implode(', ', $set);
|
||||
$sql = "INSERT INTO $this->table SET $set";
|
||||
$db->query($sql);
|
||||
// Trigger the object changed event
|
||||
self::objectChanged();
|
||||
// Prepare the SQL query to insert the data
|
||||
$columns = implode(', ', array_keys($data));
|
||||
$values = implode("', '", array_values($data));
|
||||
$sql = "INSERT INTO $this->table ($columns) VALUES ('$values')";
|
||||
//echo "SQL: $sql\n"; // Debugging line, can be removed in production
|
||||
$db->query($sql);
|
||||
return $db->insert_id();
|
||||
|
||||
Reference in New Issue
Block a user