Commit b67de5a6 authored by droces's avatar droces

Reorganisation of module PHP files

parent a2d5fc31
......@@ -9,9 +9,6 @@
*
* abookings_help()
* abookings_theme()
* get_bookables()
* get_bookings()
* get_promo()
* abookings_node_form_validate()
* validate_promo()
* save_promo_line_item()
......@@ -42,8 +39,9 @@ use Drupal\Core\Datetime\DrupalDateTime;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
module_load_include('inc', 'abookings', 'abookings_emails');
module_load_include('inc', 'abookings', 'abookings_template_codes');
module_load_include('inc', 'abookings', 'includes/emails');
module_load_include('inc', 'abookings', 'includes/template_codes');
module_load_include('inc', 'abookings', 'includes/queries');
......@@ -79,117 +77,6 @@ function abookings_theme() {
/**
* Runs a query to fetch all bookable units.
*
* @return array
* An array in the format: ['nid' => node].
*/
function get_bookables() {
$bookables = [];
$query = \Drupal::entityQuery('node');
$results = $query
->condition('status', 1)
->condition('type', 'bookable_unit')
// ->range(0, 1) // Just for development
->execute();
// kint($results, '$results');
// Save the array in the format: ['nid' => node].
foreach ($results as $rid => $nid) {
$bookables[$nid] = node_load($nid);
}
// kint($bookables, '$bookables');
return $bookables;
}
/**
* Runs a query to fetch all bookings that have a specific booking status.
*
* @param string $status
* The value of the booking status the bookings must have.
*
* @return array
* An array of results in the format: ['rid' => nid], where rid is the booking's revision id.
*/
function get_bookings($status) {
// $langcode = \Drupal::currentUser()->getPreferredLangcode();
$query = \Drupal::entityQuery('node');
$results = $query
->condition('status', 1)
->condition('type', 'booking')
// ->range(0, 1) // Just for development
->condition('field_booking_status.value', $status, '=')
->execute();
// kint($results, '$results');
return $results;
}
/**
* Runs a query to fetch a promotion with a specific promo code. Returns the first matched promotion.
*
* @param string $code_provided
* A string of the value of the promo code that the promotions must have.
*
* @return \Drupal\node\Entity\Node
* The first promotion node found matching the promo code provided.
*/
function get_promo($code_provided, $checkin_date, $nights) {
// kint($code_provided, '$code_provided');
// Fetch all promos
$promo;
$query = \Drupal::entityQuery('node');
$results = $query
->condition('status', 1)
->condition('type', 'promo')
// ->range(0, 1) // Just for development
->execute();
// kint($results, '$results');
// Loop through them
foreach ($results as $rid => $nid) {
$promo = node_load($nid);
// kint($promo, '$promo');
// Check if code provided matches this promo's code
$code = $promo->get('field_promo_code')->getValue()[0]['value'];
// kint($code, '$code');
if ($code_provided !== $code) {
continue;
}
// Check that booking is within matched promo's valid dates.
$start_date = $promo->get('field_start_date')->getValue()[0]['value'];
$end_date = $promo->get('field_end_date')->getValue()[0]['value'];
if (! ($checkin_date >= $start_date && $checkin_date <= $end_date)) {
throw new BadRequestHttpException("Matched code, but not valid checkin date");
}
// Check that booking is for at least 'min nights'.
$min_nights = $promo->get('field_min_nights')->getValue()[0]['value'];
if (! (intval($nights) >= intval($min_nights))) {
throw new BadRequestHttpException("Matched code, but too few nights.");
}
return $promo;
}
if (! $promo) {
throw new BadRequestHttpException("Promo code doesn't exist.");
}
return NULL;
}
/**
* Form validation handler for node forms. Only used for booking nodes.
*/
......
<?php
/**
* @file
* Contains functions that query site data such as fetching bookings.
**/
/**
* CONTENTS
*
* get_bookables()
* get_bookings()
* get_promo()
**/
/**
* Runs a query to fetch all bookable units.
*
* @return array
* An array in the format: ['nid' => node].
*/
function get_bookables() {
$bookables = [];
$query = \Drupal::entityQuery('node');
$results = $query
->condition('status', 1)
->condition('type', 'bookable_unit')
// ->range(0, 1) // Just for development
->execute();
// kint($results, '$results');
// Save the array in the format: ['nid' => node].
foreach ($results as $rid => $nid) {
$bookables[$nid] = node_load($nid);
}
// kint($bookables, '$bookables');
return $bookables;
}
/**
* Runs a query to fetch all bookings that have a specific booking status.
*
* @param string $status
* The value of the booking status the bookings must have.
*
* @return array
* An array of results in the format: ['rid' => nid], where rid is the booking's revision id.
*/
function get_bookings($status) {
// $langcode = \Drupal::currentUser()->getPreferredLangcode();
$query = \Drupal::entityQuery('node');
$results = $query
->condition('status', 1)
->condition('type', 'booking')
// ->range(0, 1) // Just for development
->condition('field_booking_status.value', $status, '=')
->execute();
// kint($results, '$results');
return $results;
}
/**
* Runs a query to fetch a promotion with a specific promo code. Returns the first matched promotion.
*
* @param string $code_provided
* A string of the value of the promo code that the promotions must have.
*
* @return \Drupal\node\Entity\Node
* The first promotion node found matching the promo code provided.
*/
function get_promo($code_provided, $checkin_date, $nights) {
// kint($code_provided, '$code_provided');
// Fetch all promos
$promo;
$query = \Drupal::entityQuery('node');
$results = $query
->condition('status', 1)
->condition('type', 'promo')
// ->range(0, 1) // Just for development
->execute();
// kint($results, '$results');
// Loop through them
foreach ($results as $rid => $nid) {
$promo = node_load($nid);
// kint($promo, '$promo');
// Check if code provided matches this promo's code
$code = $promo->get('field_promo_code')->getValue()[0]['value'];
// kint($code, '$code');
if ($code_provided !== $code) {
continue;
}
// Check that booking is within matched promo's valid dates.
$start_date = $promo->get('field_start_date')->getValue()[0]['value'];
$end_date = $promo->get('field_end_date')->getValue()[0]['value'];
if (! ($checkin_date >= $start_date && $checkin_date <= $end_date)) {
throw new BadRequestHttpException("Matched code, but not valid checkin date");
}
// Check that booking is for at least 'min nights'.
$min_nights = $promo->get('field_min_nights')->getValue()[0]['value'];
if (! (intval($nights) >= intval($min_nights))) {
throw new BadRequestHttpException("Matched code, but too few nights.");
}
return $promo;
}
if (! $promo) {
throw new BadRequestHttpException("Promo code doesn't exist.");
}
return NULL;
}
......@@ -8,7 +8,7 @@ use Drupal\node\Entity\Node;
/**
* CONTENTS
*
*
* get_codes_fm_settings()
* get_codes_fm_bookable()
* get_codes_fm_booking()
......@@ -40,11 +40,24 @@ function get_codes_fm_bookable($bookable) {
$codes = [
'{bookable_id}' => $bookable->id(),
'{bookable_name}' => $bookable->label(),
'{checkin_time}' => $bookable->get('field_checkin_time')->getValue()[0]['value'],
'{checkout_time}' => $bookable->get('field_checkout_time')->getValue()[0]['value'],
'{max_guests}' => $bookable->get('field_max_guests')->getValue()[0]['value'],
'{min_guests}' => $bookable->get('field_min_guests')->getValue()[0]['value'],
];
$checkin_time = $bookable->get('field_checkin_time')->getValue();
if ($checkin_time)
$codes['{checkin_time}'] = $checkin_time[0]['value'];
$checkout_time = $bookable->get('field_checkout_time')->getValue();
if ($checkout_time)
$codes['{checkout_time}'] = $checkout_time[0]['value'];
$max_guests = $bookable->get('field_max_guests')->getValue();
if ($max_guests)
$codes['{max_guests}'] = $max_guests[0]['value'];
$min_guests = $bookable->get('field_min_guests')->getValue();
if ($min_guests)
$codes['{min_guests}'] = $max_guests[0]['value'];
return $codes;
}
......@@ -58,22 +71,9 @@ function get_codes_fm_booking(Node $booking) {
$deposit_rate = \Drupal::config('abookings.bookingsettings')->get('deposit_rate');
$deposit = intval($base_cost) / (100 / $deposit_rate);
$codes = [
// field_addons - How to show this?
'{booking_id}' => $booking->id(),
'{checkin_date}' => $booking->get('field_checkin_date')->getValue()[0]['value'],
'{checkout_date}' => $booking->get('field_checkout_date')->getValue()[0]['value'],
'{booking_status}' => $booking->get('field_booking_status')->getValue()[0]['value'],
'{first_name}' => $booking->get('field_first_name')->getValue()[0]['value'],
'{last_name}' => $booking->get('field_last_name')->getValue()[0]['value'],
// '{country}' => $booking->get('field_country')->getValue()[0]['value'],
'{email_address}' => $booking->get('field_email_address')->getValue()[0]['value'],
// '{notes}' => $booking->get('field_notes')->getValue()[0]['value'],
'{num_guests}' => $booking->get('field_num_guests')->getValue()[0]['value'],
'{num_nights}' => $booking->get('field_num_nights')->getValue()[0]['value'],
// '{phone_number}' => $booking->get('field_phone_number')->getValue()[0]['value'],
// '{phone_number_alt}' => $booking->get('field_phone_number_alt')->getValue()[0]['value'],
'{promo_code_provided}' => $promo_code_provided,
'{deposit_amount}' => $deposit,
'{deposit_rate}' => $deposit_rate . '%',
......@@ -86,6 +86,15 @@ function get_codes_fm_booking(Node $booking) {
// foreach ($codes as $code_text => $field_name) {
// set_code_if_exists($codes, $code_text, $booking, $field_name, 'value');
// }
set_code_if_exists($codes, 'checkin_date', $booking, 'field_checkin_date', 'value');
set_code_if_exists($codes, 'checkout_date', $booking, 'field_checkout_date', 'value');
set_code_if_exists($codes, 'booking_status', $booking, 'field_booking_status', 'value');
set_code_if_exists($codes, 'first_name', $booking, 'field_first_name', 'value');
set_code_if_exists($codes, 'last_name', $booking, 'field_last_name', 'value');
set_code_if_exists($codes, 'email_address', $booking, 'field_email_address', 'value');
set_code_if_exists($codes, 'num_nights', $booking, 'field_num_nights', 'value');
set_code_if_exists($codes, 'num_guests', $booking, 'field_num_guests', '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');
......@@ -95,7 +104,7 @@ function get_codes_fm_booking(Node $booking) {
/**
* 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.
*/
......@@ -168,4 +177,4 @@ function template_replace_codes($template, $codes) {
$template = str_replace($search, $replace, $template);
}
return $template;
}
\ No newline at end of file
}
......@@ -77,7 +77,7 @@ function booking_preprocess_page(&$variables) {
/**
* Implements hook_form_alter().
*
*
* Simplifies the booking form for display on the website's Book page, and adds validation and submission hanlders.
*/
function booking_form_alter(&$form, FormStateInterface $form_state, $form_id) {
......@@ -105,7 +105,7 @@ function booking_form_alter(&$form, FormStateInterface $form_state, $form_id) {
case 'node_booking_form':
$form['actions']['publish']['#value'] = t('Book');
$form['actions']['submit']['#value'] = t('Book');
if (in_array("/book", $build_info_args)) {
// kint('is front-end booking form');
fe_booking_form_alter($form, $form_state, $form_id);
......@@ -164,7 +164,9 @@ function all_booking_form_alter(&$form, FormStateInterface $form_state, $form_id
$build_info_args = $form_state->getBuildInfo()['args'];
$bookable_nid = \Drupal::config('abookings.bookingsettings')->get('bookable');
$form['field_history']['#access'] = FALSE;
// If this site represents just a single bookable
if ($bookable_nid) {
$form['field_bookable_unit']['widget']['#options'] = [$bookable_nid => 'Bookable unit'];
......@@ -283,14 +285,14 @@ function be_booking_form_alter(&$form, FormStateInterface $form_state, $form_id)
*/
function fe_booking_form_alter(&$form, FormStateInterface $form_state, $form_id) {
// kint($form, '$form');
$build_info_args = $form_state->getBuildInfo()['args'];
// $form['title']['#access'] = FALSE;
if (in_array('/book', $build_info_args)) {
// kint($form, '$form');
// $form['field_checkin_date']['#attributes']['class'][] = 'hidden';
// $form['field_checkout_date']['#attributes']['class'][] = 'hidden';
$form['field_base_cost']['#attributes']['class'][] = 'hidden';
......@@ -314,7 +316,7 @@ function fe_booking_form_alter(&$form, FormStateInterface $form_state, $form_id)
'field_booking_status',
'field_paid_status',
'field_reminder_sent_deposit',
'field_history',
// 'field_history', // Hidden from form above
'path',
'author',
'advanced',
......@@ -499,7 +501,7 @@ function build_costs_markup(Node $booking) {
if (count($addons) == 0) {
$markup .= ' <tr><td><em>No addons</em></td><td></td></tr>';
}
// Total cost
$markup .=
' <tr class="total"><td>= Total cost</td><td>' . $booking_costs['total_cost'] . '</td></tr>';
......@@ -549,7 +551,7 @@ function get_history_as_markup($booking) {
return NULL;
}
$markup =
$markup =
'<h3 class="entity-meta__title">History</h3>'
. '<table>'
. ' <tr><th>Date</th>'
......@@ -625,7 +627,7 @@ function search_history($booking, $key) {
/**
*
*
*/
function get_line_items($booking_nid, $type) {
$l_items = [];
......@@ -640,7 +642,7 @@ function get_line_items($booking_nid, $type) {
if (isset($type)) {
$query->condition('field_type.value', $type, '=');
}
$results = $query->execute();
// kint($results, '$results');
......@@ -680,4 +682,4 @@ function get_manual_emails_form(Node $node) {
// kint($booking_node_form, '$booking_node_form');
return $booking_node_form;
// return '';
}
\ No newline at end of file
}
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