Commit 480102c5 authored by droces's avatar droces

Fixes related to template codes and prearrival emails

parent caf2fc2f
......@@ -326,13 +326,6 @@ function abookings_node_form_submit($form, FormStateInterface &$form_state) {
$status_email_send = $form_state->getValue('status_email_send');
// kint($status_email_send, '$status_email_send');
// if ( $previous_booking_status != 'confirmed'
// && $submitted_booking_status == 'confirmed'
// && $status_email_send === 'send'
// && $bookable) {
// send_confirmation_email($booking, $bookable);
// }
if ( $previous_booking_status == 'expired'
&& $submitted_booking_status != 'expired') {
$dateTime = new DrupalDateTime();
......@@ -403,6 +396,7 @@ function abookings_mail($key, &$message, $params) {
* Triggers checks on provisional and confirmed bookings.
*/
function abookings_cron() {
kint('abookings_cron()');
check_provisional_bookings();
check_confirmed_bookings();
}
......@@ -497,6 +491,7 @@ function expire_booking($booking, $bookable) {
* Checks if confirmed bookings have passed their check-out date and time, and if so, completes them.
*/
function check_confirmed_bookings() {
// kint('check_confirmed_bookings()');
// Fetch all bookings that have a status of 'confirmed'
$results = get_bookings('confirmed');
......@@ -529,8 +524,10 @@ function check_confirmed_bookings() {
$prearrival_timestamp = intval($checkin_timestamp) - ($prearrival_hours * 60 * 60);
// kint($prearrival_timestamp, '$prearrival_timestamp');
$prearrival_sent = search_history($booking, 'Pre-arrival info');
// If current time exceeds (prearrival hours before check-in time)
if (time() > $prearrival_timestamp) {
if (time() > $prearrival_timestamp && ! $prearrival_sent) {
// kint('Sending prearrival email');
send_prearrival_email($booking, $bookable);
}
......
......@@ -42,18 +42,19 @@ function log_event_to_history(Node $booking, $type, $description, $date) {
$date = time();
}
$history = [];
$field_history_val = $booking->get('field_history')->getValue();
if (! in_array(0, $field_history_val)) {
return NULL;
}
if (! in_array('value', $field_history_val[0])) {
return NULL;
}
$history_json = $field_history_val[0]['value'];
$history = json_decode($history_json);
if (gettype($history) === 'object') {
$history = [$history];
// kint($field_history_val, '$field_history_val');
if ($field_history_val) {
if (array_key_exists('value', $field_history_val[0])) {
$history_json = $field_history_val[0]['value'];
$history = json_decode($history_json);
// kint($history, '$history');
if (gettype($history) === 'object') {
$history = [$history];
}
}
}
$history[] = [
......@@ -89,13 +90,15 @@ function send_booking_email($subject, $body, $to, $key) {
// kint($result, '$result');
if ($result['result'] != true) {
$message = t('There was a problem sending your email notification to @email.', array('@email' => $to));
$message = t('There was a problem sending your email notification to @email, email key: @key.',
array('@email' => $to, '@key' => $key));
drupal_set_message($message, 'error');
\Drupal::logger('mail-log')->error($message);
return;
}
$message = t('An email notification has been sent to @email ', array('@email' => $to));
$message = t('An email notification has been sent to @email, email key: @key.',
array('@email' => $to, '@key' => $key));
drupal_set_message($message);
\Drupal::logger('mail-log')->notice($message);
}
......
......@@ -81,59 +81,70 @@ function get_codes_fm_booking($booking) {
'{deposit_rate}' => $deposit_rate . '%',
'{base_cost}' => $base_cost,
];
$field_country_value = $booking->get('field_country')->getValue();
if (array_key_exists(0, $field_country_value)) {
if (array_key_exists('value', $field_country_value[0])) {
$codes['{country}'] = $booking->get('field_country')->getValue()[0]['value'];
}
}
if (array_key_exists('value', $booking->get('field_notes')->getValue()[0])) {
$codes['{notes}'] = $booking->get('field_notes')->getValue()[0]['value'];
}
if (array_key_exists('value', $booking->get('field_phone_number')->getValue()[0])) {
$codes['{phone_number}'] = $booking->get('field_phone_number')->getValue()[0]['value'];
// foreach ($codes as $code_text => $field_name) {
// set_code_if_exists($codes, $code_text, $booking, $field_name, 'value');
// }
set_code_if_exists($codes, 'country', $booking, 'field_country', 'value');
set_code_if_exists($codes, 'notes', $booking, 'field_notes', 'value');
set_code_if_exists($codes, 'phone_number', $booking, 'field_phone_number', 'value');
set_code_if_exists($codes, 'phone_number_alt', $booking, 'field_phone_number_alt', 'value');
return $codes;
}
/**
* For a given field, if the field has a value, set it's template code to it.
*
* @param $value_key
* 'value' for most fields, otherwise 'target_id' if a reference field.
*/
function set_code_if_exists(&$codes, $code_text, $entity, $field_name, $value_key) {
$field_value = $entity->get($field_name)->getValue();
if (! array_key_exists(0, $field_value)) {
return NULL;
}
if (array_key_exists('value', $booking->get('field_phone_number_alt')->getValue()[0])) {
$codes['{phone_number_alt}'] = $booking->get('field_phone_number_alt')->getValue()[0]['value'];
if (array_key_exists($value_key, $field_value[0])) {
return NULL;
}
return $codes;
// return $field_value[0][$value_key];
$codes['{' . $code_text . '}'] = $field_value[0][$value_key];
}
function get_codes() {
$codes = [
'settings' => [
'{deposit_hours}' => '',
'{deposit_rate}' => '',
'{deposit_hours}' => '',
'{deposit_rate}' => '',
],
'bookable' => [
'{bookable_id}' => '',
'{bookable_name}' => '',
'{checkin_time}' => '',
'{checkout_time}' => '',
'{max_guests}' => '',
'{min_guests}' => '',
'{bookable_id}' => '',
'{bookable_name}' => '',
'{checkin_time}' => '',
'{checkout_time}' => '',
'{max_guests}' => '',
'{min_guests}' => '',
],
'booking' => [
'{booking_id}' => '',
'{checkin_date}' => '',
'{checkout_date}' => '',
'{booking_status}' => '',
'{first_name}' => '',
'{last_name}' => '',
'{booking_status}' => '',
'{country}' => '',
'{email_address}' => '',
'{first_name}' => '',
'{notes}' => '',
'{num_guests}' => '',
'{num_nights}' => '',
'{phone_number}' => '',
'{phone_number_alt}' => '',
'{booking_id}' => '',
'{checkin_date}' => '',
'{checkout_date}' => '',
'{booking_status}' => '',
'{first_name}' => '',
'{last_name}' => '',
'{booking_status}' => '',
'{country}' => '',
'{email_address}' => '',
'{first_name}' => '',
'{notes}' => '',
'{num_guests}' => '',
'{num_nights}' => '',
'{phone_number}' => '',
'{phone_number_alt}' => '',
'{promo_code_provided}' => '',
'{deposit_amount}' => '',
'{deposit_rate}' => '',
'{total_cost}' => '',
'{deposit_amount}' => '',
'{deposit_rate}' => '',
'{total_cost}' => '',
],
];
return $codes;
......
......@@ -200,8 +200,6 @@ function all_booking_form_alter(&$form, FormStateInterface $form_state, $form_id
$bookable = node_load($bookable_nid);
// kint($bookable, '$bookable');
// send_confirmation_email($booking, $bookable);
if (! in_array("/book", $build_info_args)) {
be_booking_form_alter($form, $form_state, $form_id);
}
......@@ -544,25 +542,11 @@ function build_costs_markup(Node $booking) {
function get_history_as_markup($booking) {
$history = $booking->get('field_history')->getValue();
if (! $history || ! in_array('value', $history[0])) {
// kint('get_history_as_markup()');
$history = get_history($booking);
if (! $history) {
return NULL;
}
$history_json = $history[0]['value'];
// kint($history_json, '$history_json');
// Wrap the date in a quotation marks, to be converted to a string in json_decode().
$history_json = preg_replace('/\d{2,4}-\d{2}-\d{2}/', '"$0"', $history_json);
$history = json_decode($history_json);
// kint($history, '$history');
if ((! $history) || ($history === NULL)) {
return null;
}
// kint($history, '$history');
if (getType($history) === 'object') {
$history = [$history];
}
$markup =
'<h3 class="entity-meta__title">History</h3>'
......@@ -595,6 +579,50 @@ function get_history_as_markup($booking) {
return $markup;
}
function get_history($booking) {
$history = $booking->get('field_history')->getValue();
if (! $history) {
return NULL;
}
if (! array_key_exists('value', $history[0])) {
return NULL;
}
$history_json = $history[0]['value'];
$history = json_decode($history_json);
// kint($history, '$history');
if ((! $history) || ($history === NULL)) {
return null;
}
// kint($history, '$history');
if (getType($history) === 'object') {
$history = [$history];
}
return $history;
}
function search_history($booking, $key) {
// kint($key, '$key');
$history_raw = $booking->get('field_history')->getValue();
if (! $history_raw) {
return NULL;
}
if (! array_key_exists('value', $history_raw[0])) {
return NULL;
}
$history = $history_raw[0]['value'];
// kint($history, '$history');
return strpos($history, $key) > 0;
}
/**
*
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment