| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage com_vikbooking |
| 5 |
* @author Alessio Gaggii - e4j - Extensionsforjoomla.com |
| 6 |
* @copyright Copyright (C) 2018 e4j - Extensionsforjoomla.com. All rights reserved. |
| 7 |
* @license GNU General Public License version 2 or later; see LICENSE |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 12 |
|
| 13 |
/** |
| 14 |
* Class handler for conditional rule "rate plans". |
| 15 |
* |
| 16 |
* @since 1.4.0 |
| 17 |
*/ |
| 18 |
class VikBookingConditionalRuleRatePlans extends VikBookingConditionalRule |
| 19 |
{ |
| 20 |
/** |
| 21 |
* Class constructor will define the rule name, description and identifier. |
| 22 |
*/ |
| 23 |
public function __construct() |
| 24 |
{ |
| 25 |
// call parent constructor |
| 26 |
parent::__construct(); |
| 27 |
|
| 28 |
$this->ruleName = JText::translate('VBMENURATEPLANS'); |
| 29 |
$this->ruleDescr = JText::translate('VBO_CONDTEXT_RULE_RPL_DESCR'); |
| 30 |
$this->ruleId = basename(__FILE__); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Displays the rule parameters. |
| 35 |
* |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function renderParams() |
| 39 |
{ |
| 40 |
$this->vbo_app->loadSelect2(); |
| 41 |
$rplans = $this->loadRatePlans(); |
| 42 |
$otarplans = $this->loadOTARatePlans(); |
| 43 |
$current_rplans = $this->getParam('rplans', array()); |
| 44 |
$cur_ota_rplans = $this->getParam('otarplans', array()); |
| 45 |
|
| 46 |
?> |
| 47 |
<div class="vbo-param-container"> |
| 48 |
<div class="vbo-param-label"><?php echo JText::translate('VBOSPTYPESPRICE'); ?></div> |
| 49 |
<div class="vbo-param-setting"> |
| 50 |
<select name="<?php echo $this->inputName('rplans', true); ?>" id="<?php echo $this->inputID('rplans'); ?>" multiple="multiple"> |
| 51 |
<?php |
| 52 |
foreach ($rplans as $rdata) { |
| 53 |
?> |
| 54 |
<option value="<?php echo $rdata['id']; ?>"<?php echo is_array($current_rplans) && in_array($rdata['id'], $current_rplans) ? ' selected="selected"' : ''; ?>><?php echo $rdata['name']; ?></option> |
| 55 |
<?php |
| 56 |
} |
| 57 |
?> |
| 58 |
</select> |
| 59 |
</div> |
| 60 |
</div> |
| 61 |
|
| 62 |
<?php |
| 63 |
if (count($otarplans)) { |
| 64 |
// this is an associative array with key=channel_name, value=channel_rate_plans |
| 65 |
?> |
| 66 |
<div class="vbo-param-container"> |
| 67 |
<div class="vbo-param-label"><?php echo JText::translate('VBMENUCHANNELMANAGER'); ?></div> |
| 68 |
<div class="vbo-param-setting"> |
| 69 |
<select name="<?php echo $this->inputName('otarplans', true); ?>" id="<?php echo $this->inputID('otarplans'); ?>" multiple="multiple"> |
| 70 |
<?php |
| 71 |
foreach ($otarplans as $ch_name => $ch_rplans) { |
| 72 |
?> |
| 73 |
<optgroup label="<?php echo addslashes($ch_name); ?>"> |
| 74 |
<?php |
| 75 |
foreach ($ch_rplans as $ch_rplan) { |
| 76 |
?> |
| 77 |
<option value="<?php echo $ch_rplan; ?>"<?php echo is_array($cur_ota_rplans) && in_array($ch_rplan, $cur_ota_rplans) ? ' selected="selected"' : ''; ?>><?php echo $ch_rplan; ?></option> |
| 78 |
<?php |
| 79 |
} |
| 80 |
?> |
| 81 |
</optgroup> |
| 82 |
<?php |
| 83 |
} |
| 84 |
?> |
| 85 |
</select> |
| 86 |
</div> |
| 87 |
</div> |
| 88 |
<?php |
| 89 |
} |
| 90 |
?> |
| 91 |
|
| 92 |
<script type="text/javascript"> |
| 93 |
jQuery(function() { |
| 94 |
jQuery('#<?php echo $this->inputID('rplans'); ?>').select2(); |
| 95 |
jQuery('#<?php echo $this->inputID('otarplans'); ?>').select2(); |
| 96 |
}); |
| 97 |
</script> |
| 98 |
<?php |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Tells whether the rule is compliant. |
| 103 |
* |
| 104 |
* @return bool True on success, false otherwise. |
| 105 |
*/ |
| 106 |
public function isCompliant() |
| 107 |
{ |
| 108 |
$rplans_booked = $this->getProperty('rooms', array()); |
| 109 |
if (!is_array($rplans_booked) || !count($rplans_booked)) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
// get filters |
| 114 |
$allowed_rplans = $this->getParam('rplans', array()); |
| 115 |
$allowed_ota_rplans = $this->getParam('otarplans', array()); |
| 116 |
|
| 117 |
$all_tariff_ids = array(); |
| 118 |
$ota_rplans_available = false; |
| 119 |
foreach ($rplans_booked as $rplan_book) { |
| 120 |
if (array_key_exists('otarplan', $rplan_book)) { |
| 121 |
// when accessing the "rooms" property, only a few columns may be available |
| 122 |
$ota_rplans_available = true; |
| 123 |
} |
| 124 |
if (!isset($rplan_book['idtar']) || in_array((int)$rplan_book['idtar'], $all_tariff_ids)) { |
| 125 |
continue; |
| 126 |
} |
| 127 |
array_push($all_tariff_ids, (int)$rplan_book['idtar']); |
| 128 |
} |
| 129 |
|
| 130 |
if (!count($all_tariff_ids) && !count($allowed_ota_rplans)) { |
| 131 |
// useless to proceed |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
// whether we have found a match |
| 136 |
$one_found = false; |
| 137 |
|
| 138 |
// get all rate plan IDs from tariffs |
| 139 |
$dbo = JFactory::getDbo(); |
| 140 |
|
| 141 |
if (count($all_tariff_ids)) { |
| 142 |
$records = array(); |
| 143 |
$q = "SELECT `idprice` FROM `#__vikbooking_dispcost` WHERE `id` IN (" . implode(', ', $all_tariff_ids) . ")"; |
| 144 |
$dbo->setQuery($q); |
| 145 |
$dbo->execute(); |
| 146 |
if ($dbo->getNumRows()) { |
| 147 |
$records = $dbo->loadAssocList(); |
| 148 |
} |
| 149 |
$all_price_ids = array(); |
| 150 |
foreach ($records as $record) { |
| 151 |
array_push($all_price_ids, $record['idprice']); |
| 152 |
} |
| 153 |
|
| 154 |
// check if website rate plans are matching |
| 155 |
foreach ($all_price_ids as $idprice) { |
| 156 |
if (in_array($idprice, $allowed_rplans)) { |
| 157 |
$one_found = true; |
| 158 |
break; |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if (!$one_found && count($allowed_ota_rplans)) { |
| 164 |
// check if the OTA rate plan matches with the ones allowed |
| 165 |
if (!$ota_rplans_available) { |
| 166 |
// load full reservation rooms data, including ota rate plans |
| 167 |
$full_rooms_data = VikBooking::loadOrdersRoomsData($this->getPropVal('booking', 'id')); |
| 168 |
} else { |
| 169 |
// it looks like the property accessed has got enough information |
| 170 |
$full_rooms_data = $rplans_booked; |
| 171 |
} |
| 172 |
foreach ($full_rooms_data as $rdata) { |
| 173 |
if (empty($rdata['otarplan'])) { |
| 174 |
continue; |
| 175 |
} |
| 176 |
foreach ($allowed_ota_rplans as $ota_rplan) { |
| 177 |
if (stripos($ota_rplan, $rdata['otarplan']) !== false) { |
| 178 |
$one_found = true; |
| 179 |
break 2; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
// return true if at least one rate plan booked is in the parameters |
| 186 |
return $one_found; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Internal function for this rule only. |
| 191 |
* |
| 192 |
* @return array |
| 193 |
*/ |
| 194 |
protected function loadRatePlans() |
| 195 |
{ |
| 196 |
$rplans = array(); |
| 197 |
|
| 198 |
$dbo = JFactory::getDbo(); |
| 199 |
$q = "SELECT `id`, `name` FROM `#__vikbooking_prices` ORDER BY `name` ASC;"; |
| 200 |
$dbo->setQuery($q); |
| 201 |
$dbo->execute(); |
| 202 |
if ($dbo->getNumRows()) { |
| 203 |
$rplans = $dbo->loadAssocList(); |
| 204 |
} |
| 205 |
|
| 206 |
return $rplans; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Internal function for this rule only. |
| 211 |
* |
| 212 |
* @return array |
| 213 |
* |
| 214 |
* @since 1.15.0 (J) - 1.5.0 (WP) |
| 215 |
*/ |
| 216 |
protected function loadOTARatePlans() |
| 217 |
{ |
| 218 |
$otarplans = array(); |
| 219 |
|
| 220 |
if (!$this->isChannelManagerAvailable()) { |
| 221 |
return $otarplans; |
| 222 |
} |
| 223 |
|
| 224 |
$dbo = JFactory::getDbo(); |
| 225 |
|
| 226 |
try { |
| 227 |
$q = "SELECT `channel`,`otapricing` FROM `#__vikchannelmanager_roomsxref` ORDER BY `channel` ASC;"; |
| 228 |
$dbo->setQuery($q); |
| 229 |
$dbo->execute(); |
| 230 |
if ($dbo->getNumRows()) { |
| 231 |
$records = $dbo->loadAssocList(); |
| 232 |
foreach ($records as $ch) { |
| 233 |
// make sure the channel name is readable |
| 234 |
$ch['channel'] = $ch['channel'] == 'airbnbapi' ? 'airbnb' : $ch['channel']; |
| 235 |
$ch_name = ucfirst($ch['channel']); |
| 236 |
// decode mapped pricing information |
| 237 |
$ch_pricing = json_decode($ch['otapricing'], true); |
| 238 |
if (!is_array($ch_pricing) || !isset($ch_pricing['RatePlan']) ) { |
| 239 |
continue; |
| 240 |
} |
| 241 |
if (!isset($otarplans[$ch_name])) { |
| 242 |
$otarplans[$ch_name] = array(); |
| 243 |
} |
| 244 |
foreach ($ch_pricing['RatePlan'] as $rpid => $rpdata) { |
| 245 |
if (empty($rpdata['name'])) { |
| 246 |
continue; |
| 247 |
} |
| 248 |
$rplan_name = ucwords($rpdata['name']); |
| 249 |
if (in_array($rplan_name, $otarplans[$ch_name])) { |
| 250 |
continue; |
| 251 |
} |
| 252 |
array_push($otarplans[$ch_name], $rplan_name); |
| 253 |
} |
| 254 |
if (!count($otarplans[$ch_name])) { |
| 255 |
unset($otarplans[$ch_name]); |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
} catch (Exception $e) { |
| 260 |
// do nothing |
| 261 |
} |
| 262 |
|
| 263 |
return $otarplans; |
| 264 |
} |
| 265 |
|
| 266 |
} |
| 267 |
|