| @@ -1,0 +1,256 @@ | ||
| 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 "rooms". | |
| 15 | + * | |
| 16 | + * @since 1.4.0 | |
| 17 | + */ | |
| 18 | +class VikBookingConditionalRuleRooms 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('VBPVIEWORDERSTHREE'); | |
| 29 | + $this->ruleDescr = JText::translate('VBO_CONDTEXT_RULE_ROOMS_DESCR'); | |
| 30 | + $this->ruleId = basename(__FILE__); | |
| 31 | + } | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * Displays the rule parameters. | |
| 35 | + * | |
| 36 | + * @return void | |
| 37 | + * | |
| 38 | + * @since 1.15.0 (J) - 1.5.0 (WP) added support for sub-units. | |
| 39 | + */ | |
| 40 | + public function renderParams() | |
| 41 | + { | |
| 42 | + $this->vbo_app->loadSelect2(); | |
| 43 | + $rooms = $this->loadRooms(); | |
| 44 | + $current_rooms = $this->getParam('rooms', array()); | |
| 45 | + $current_rooms = !is_array($current_rooms) ? array() : $current_rooms; | |
| 46 | + | |
| 47 | + // check if we've got rooms with sub-units defined | |
| 48 | + $sub_units = []; | |
| 49 | + foreach ($rooms as $rdata) { | |
| 50 | + if ($rdata['units'] < 2) { | |
| 51 | + continue; | |
| 52 | + } | |
| 53 | + $room_features = VikBooking::getRoomParam('features', $rdata['params']); | |
| 54 | + if (is_array($room_features) && count($room_features)) { | |
| 55 | + $sub_units[$rdata['id']] = [ | |
| 56 | + 'name' => $rdata['name'], | |
| 57 | + 'units' => $rdata['units'], | |
| 58 | + 'features' => $room_features, | |
| 59 | + ]; | |
| 60 | + } | |
| 61 | + } | |
| 62 | + | |
| 63 | + ?> | |
| 64 | + <div class="vbo-param-container"> | |
| 65 | + <div class="vbo-param-label"><?php echo JText::translate('VBOROOMSASSIGNED'); ?></div> | |
| 66 | + <div class="vbo-param-setting"> | |
| 67 | + <select name="<?php echo $this->inputName('rooms', true); ?>" id="<?php echo $this->inputID('rooms'); ?>" multiple="multiple" onchange="vboChangeSubUnits();"> | |
| 68 | + <?php | |
| 69 | + foreach ($rooms as $rdata) { | |
| 70 | + ?> | |
| 71 | + <option value="<?php echo $rdata['id']; ?>"<?php echo in_array($rdata['id'], $current_rooms) ? ' selected="selected"' : ''; ?>><?php echo $rdata['name']; ?></option> | |
| 72 | + <?php | |
| 73 | + } | |
| 74 | + ?> | |
| 75 | + </select> | |
| 76 | + </div> | |
| 77 | + </div> | |
| 78 | + | |
| 79 | + <?php | |
| 80 | + if (count($sub_units)) { | |
| 81 | + ?> | |
| 82 | + <div class="vbo-param-container"> | |
| 83 | + <div class="vbo-param-label"><?php echo JText::translate('VBNEWROOMNINE'); ?></div> | |
| 84 | + <div class="vbo-param-setting"> | |
| 85 | + <?php echo $this->vbo_app->printYesNoButtons($this->inputName('use_sub_units'), JText::translate('VBYES'), JText::translate('VBNO'), (int)$this->getParam('use_sub_units', 0), 1, 0, 'vboToggleUseSubUnits();'); ?> | |
| 86 | + </div> | |
| 87 | + </div> | |
| 88 | + <?php | |
| 89 | + $init_display = (int)$this->getParam('use_sub_units', 0); | |
| 90 | + foreach ($sub_units as $rid => $rdata) { | |
| 91 | + $display_runits = ($init_display && in_array($rid, $current_rooms)); | |
| 92 | + ?> | |
| 93 | + <div class="vbo-param-container vbo-rule-rooms-rsubunits" data-rid="<?php echo $rid; ?>" style="<?php echo !$display_runits ? 'display: none;' : ''; ?>"> | |
| 94 | + <div class="vbo-param-label"><?php echo $rdata['name']; ?></div> | |
| 95 | + <div class="vbo-param-setting"> | |
| 96 | + <select name="<?php echo $this->inputName("sub_unit_$rid"); ?>"> | |
| 97 | + <option value=""></option> | |
| 98 | + <?php | |
| 99 | + $cur_val = (int)$this->getParam("sub_unit_$rid", 0); | |
| 100 | + for ($i = 1; $i <= $rdata['units']; $i++) { | |
| 101 | + ?> | |
| 102 | + <option value="<?php echo $i; ?>"<?php echo $cur_val == $i ? ' selected="selected"' : ''; ?>><?php echo $this->getFirstFeature($i, $rdata['features']); ?></option> | |
| 103 | + <?php | |
| 104 | + } | |
| 105 | + ?> | |
| 106 | + </select> | |
| 107 | + </div> | |
| 108 | + </div> | |
| 109 | + <?php | |
| 110 | + } | |
| 111 | + } | |
| 112 | + ?> | |
| 113 | + | |
| 114 | + <script type="text/javascript"> | |
| 115 | + jQuery(function() { | |
| 116 | + jQuery('#<?php echo $this->inputID('rooms'); ?>').select2(); | |
| 117 | + }); | |
| 118 | + | |
| 119 | + function vboToggleUseSubUnits() { | |
| 120 | + jQuery('.vbo-rule-rooms-rsubunits').hide(); | |
| 121 | + var use_sub_units = jQuery('input[name="<?php echo $this->inputName('use_sub_units'); ?>"]').prop('checked'); | |
| 122 | + if (use_sub_units) { | |
| 123 | + var rooms_selected = jQuery('#<?php echo $this->inputID('rooms'); ?>').val(); | |
| 124 | + jQuery('.vbo-rule-rooms-rsubunits').each(function() { | |
| 125 | + var rid = jQuery(this).attr('data-rid'); | |
| 126 | + if (rooms_selected && rooms_selected.length && rooms_selected.indexOf(rid) >= 0) { | |
| 127 | + jQuery(this).show(); | |
| 128 | + } else { | |
| 129 | + jQuery(this).hide().find('select').val(''); | |
| 130 | + } | |
| 131 | + }); | |
| 132 | + } else { | |
| 133 | + // hide all sub-units and make them empty | |
| 134 | + jQuery('.vbo-rule-rooms-rsubunits').hide().find('select').val(''); | |
| 135 | + } | |
| 136 | + } | |
| 137 | + | |
| 138 | + function vboChangeSubUnits() { | |
| 139 | + var rooms_selected = jQuery('#<?php echo $this->inputID('rooms'); ?>').val(); | |
| 140 | + if (!rooms_selected || !rooms_selected.length) { | |
| 141 | + // hide all sub-units and make them empty | |
| 142 | + jQuery('.vbo-rule-rooms-rsubunits').hide().find('select').val(''); | |
| 143 | + } else { | |
| 144 | + // hide all sub-units, but don't touch their values | |
| 145 | + jQuery('.vbo-rule-rooms-rsubunits').hide(); | |
| 146 | + // check if the use of sub-units is enabled | |
| 147 | + var use_sub_units = jQuery('input[name="<?php echo $this->inputName('use_sub_units'); ?>"]').prop('checked'); | |
| 148 | + // display only the sub-units for the selected rooms | |
| 149 | + if (use_sub_units) { | |
| 150 | + for (var i = 0; i < rooms_selected.length; i++) { | |
| 151 | + var sub_units_cont = jQuery('.vbo-rule-rooms-rsubunits[data-rid="' + rooms_selected[i] + '"]'); | |
| 152 | + if (sub_units_cont && sub_units_cont.length) { | |
| 153 | + // show the sub-units for this room | |
| 154 | + sub_units_cont.show(); | |
| 155 | + } | |
| 156 | + } | |
| 157 | + } | |
| 158 | + } | |
| 159 | + } | |
| 160 | + </script> | |
| 161 | + <?php | |
| 162 | + } | |
| 163 | + | |
| 164 | + /** | |
| 165 | + * Tells whether the rule is compliant. | |
| 166 | + * | |
| 167 | + * @return bool True on success, false otherwise. | |
| 168 | + */ | |
| 169 | + public function isCompliant() | |
| 170 | + { | |
| 171 | + $rooms_booked = $this->getProperty('rooms', array()); | |
| 172 | + if (!is_array($rooms_booked) || !count($rooms_booked)) { | |
| 173 | + return false; | |
| 174 | + } | |
| 175 | + | |
| 176 | + $allowed_rooms = $this->getParam('rooms', array()); | |
| 177 | + | |
| 178 | + $one_found = false; | |
| 179 | + foreach ($rooms_booked as $rb) { | |
| 180 | + if (!isset($rb['idroom'])) { | |
| 181 | + continue; | |
| 182 | + } | |
| 183 | + if (in_array($rb['idroom'], $allowed_rooms)) { | |
| 184 | + $one_found = true; | |
| 185 | + break; | |
| 186 | + } | |
| 187 | + } | |
| 188 | + | |
| 189 | + // check sub-units | |
| 190 | + $use_sub_units = (int)$this->getParam('use_sub_units', 0); | |
| 191 | + if ($use_sub_units) { | |
| 192 | + $sub_unit_matched = false; | |
| 193 | + // parse again the rooms booked | |
| 194 | + foreach ($rooms_booked as $rb) { | |
| 195 | + // grab the sub-unit index for this room id | |
| 196 | + $room_sub_unit_filt = (int)$this->getParam('sub_unit_' . $rb['idroom'], 0); | |
| 197 | + if (empty($room_sub_unit_filt)) { | |
| 198 | + // no sub-unit defined for this room ID | |
| 199 | + continue; | |
| 200 | + } | |
| 201 | + if ($rb['roomindex'] == $room_sub_unit_filt) { | |
| 202 | + // the index of the room booked matched the one in the params | |
| 203 | + $sub_unit_matched = true; | |
| 204 | + } | |
| 205 | + } | |
| 206 | + | |
| 207 | + return $one_found && $sub_unit_matched; | |
| 208 | + } | |
| 209 | + | |
| 210 | + // return true if at least one room booked is in the parameters | |
| 211 | + return $one_found; | |
| 212 | + } | |
| 213 | + | |
| 214 | + /** | |
| 215 | + * Internal function for this rule only. | |
| 216 | + * | |
| 217 | + * @return array | |
| 218 | + */ | |
| 219 | + protected function loadRooms() | |
| 220 | + { | |
| 221 | + $rooms = array(); | |
| 222 | + | |
| 223 | + $dbo = JFactory::getDbo(); | |
| 224 | + $q = "SELECT `id`, `name`, `units`, `params` FROM `#__vikbooking_rooms` ORDER BY `name` ASC;"; | |
| 225 | + $dbo->setQuery($q); | |
| 226 | + $dbo->execute(); | |
| 227 | + if ($dbo->getNumRows()) { | |
| 228 | + $rooms = $dbo->loadAssocList(); | |
| 229 | + } | |
| 230 | + | |
| 231 | + return $rooms; | |
| 232 | + } | |
| 233 | + | |
| 234 | + /** | |
| 235 | + * Internal function for this rule only. | |
| 236 | + * | |
| 237 | + * @param int $i the room unit index to get. | |
| 238 | + * @param array $features the list of room features. | |
| 239 | + * | |
| 240 | + * @return array | |
| 241 | + */ | |
| 242 | + protected function getFirstFeature($i, $features = []) | |
| 243 | + { | |
| 244 | + if (!is_array($features) || !isset($features[$i])) { | |
| 245 | + return $i; | |
| 246 | + } | |
| 247 | + | |
| 248 | + foreach ($features[$i] as $fkey => $fval) { | |
| 249 | + if (!empty($fkey) && !empty($fval)) { | |
| 250 | + return "#$i - " . JText::translate($fkey) . ': ' . $fval; | |
| 251 | + } | |
| 252 | + } | |
| 253 | + | |
| 254 | + return "#$i"; | |
| 255 | + } | |
| 256 | +} | |