bat-8.x-1.x-dev/modules/bat_options/bat_options.module
modules/bat_options/bat_options.module
<?php
/**
* @file
* This file misses a description.
*/
define('BAT_OPTIONS_ADD', 'add');
define('BAT_OPTIONS_ADD_DAILY', 'add-daily');
define('BAT_OPTIONS_SUB', 'sub');
define('BAT_OPTIONS_SUB_DAILY', 'sub-daily');
define('BAT_OPTIONS_REPLACE', 'replace');
define('BAT_OPTIONS_INCREASE', 'increase');
define('BAT_OPTIONS_DECREASE', 'decrease');
define('BAT_OPTIONS_NOCHARGE', 'no_charge');
define('BAT_OPTIONS_OPTIONAL', 'optional');
define('BAT_OPTIONS_MANDATORY', 'mandatory');
define('BAT_OPTIONS_ONREQUEST', 'on_request');
use Drupal\bat_unit\Entity\UnitType;
/**
* Returns the available price options for booking_unit options field.
*/
function bat_options_price_options() {
$options = [
BAT_OPTIONS_ADD => t('Add to price'),
BAT_OPTIONS_ADD_DAILY => t('Add to price per night'),
BAT_OPTIONS_SUB => t('Subtract from price'),
BAT_OPTIONS_SUB_DAILY => t('Subtract from price per night'),
BAT_OPTIONS_REPLACE => t('Replace price'),
BAT_OPTIONS_INCREASE => t('Increase price by % amount'),
BAT_OPTIONS_DECREASE => t('Decrease price by % amount'),
BAT_OPTIONS_NOCHARGE => t('No charge'),
];
return $options;
}
/**
* Returns available options given a Bat type.
*
* @param \Drupal\bat_unit\Entity\UnitType $type
* The type from which to retrieve options.
*
* @return array
* The available options for the given type.
*/
function bat_options_get_type_options(UnitType $type) {
$options = &drupal_static(__FUNCTION__);
if (isset($options['types'][$type->id()])) {
return $options['types'][$type->id()];
}
$type_options = is_array($type->getTranslation('und')->get('field_addons')) ? $type->getTranslation('und')->get('field_addons') : [];
$options['types'][$type->id()] = $type_options;
return $options['types'][$type->id()];
}
/**
* Converts option human name to its machine name.
*
* @param string $option_name
* The human option name.
* @param string $pattern
* The pattern used to convert. By default "/[^a-z0-9_]+/".
* @param string $replacement
* The replacement string. By default "_".
*
* @return string
* The option machine name.
*/
function bat_options_get_machine_name($option_name, $pattern = '/[^a-z0-9_]+/', $replacement = '_') {
return preg_replace($pattern, $replacement, mb_strtolower($option_name));
}
/**
* Given an option, return a string that explains the operation.
*/
function bat_options_get_operation_label($option) {
$label = '';
$currency_symbol = '$';
switch ($option['operation']) {
case 'add':
$label = t('+@amount@currency_symbol to price', [
'@amount' => $option['value'],
'@currency_symbol' => $currency_symbol,
]);
break;
case 'add-daily':
$label = t('+@amount@currency_symbol per night to price', [
'@amount' => $option['value'],
'@currency_symbol' => $currency_symbol,
]);
break;
case 'sub-daily':
$label = t('-@amount@currency_symbol per night from price', [
'@amount' => $option['value'],
'@currency_symbol' => $currency_symbol,
]);
break;
case 'replace':
$label = t('Replace price with @amount@currency_symbol', [
'@amount' => $option['value'],
'@currency_symbol' => $currency_symbol,
]);
break;
case 'increase':
$label = t('Increase price by @amount%', [
'@amount' => $option['value'],
]);
break;
case 'decrease':
$label = t('Decrease price by @amount%', [
'@amount' => $option['value'],
]);
break;
case 'sub':
$label = t('-@amount@currency_symbol from price', [
'@amount' => $option['value'],
'@currency_symbol' => $currency_symbol,
]);
break;
}
return $label;
}
/**
* Calculate the price for an option.
*/
function bat_options_get_option_price($booking_price, $option, $quantity, $nights) {
$price = 0;
switch ($option['operation']) {
case BAT_OPTIONS_ADD:
$price += $option['value'];
break;
case BAT_OPTIONS_ADD_DAILY:
$price += $option['value'] * $nights;
break;
case BAT_OPTIONS_SUB:
$price -= $option['value'];
break;
case BAT_OPTIONS_SUB_DAILY:
$price -= $option['value'] * $nights;
break;
case BAT_OPTIONS_REPLACE:
$price = $option['value'] - $booking_price;
break;
case BAT_OPTIONS_INCREASE:
$price += $booking_price * $option['value'] / 100;
break;
case BAT_OPTIONS_DECREASE:
$price -= $booking_price * $option['value'] / 100;
break;
}
return $price * 100;
}
