| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage mod_vikbooking_horizontalsearch |
| 5 |
* @author Alessio Gaggii - E4J s.r.l |
| 6 |
* @copyright Copyright (C) 2018 E4J s.r.l. All rights reserved. |
| 7 |
* @license GNU General Public License version 2 or later; see LICENSE |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
final class VikBookingWidgetHorizontalSearch |
| 15 |
{ |
| 16 |
/** |
| 17 |
* Returns a JS string to define the array-variables containing |
| 18 |
* the months and the week days. |
| 19 |
* |
| 20 |
* @param string $format either long or 3char. |
| 21 |
* @param mixed $module_id the ID of the module to write unique variables |
| 22 |
* |
| 23 |
* @return string |
| 24 |
* |
| 25 |
* @since 1.1.0 |
| 26 |
*/ |
| 27 |
public static function getMonWdayScript($format = 'long', $module_id = 0) |
| 28 |
{ |
| 29 |
$module_id = (string)$module_id; |
| 30 |
|
| 31 |
return 'var vboMapWdays'.$module_id.' = ["'.self::applySubstr(JText::translate('VBJQCALSUN'), $format).'", "'.self::applySubstr(JText::translate('VBJQCALMON'), $format).'", "'.self::applySubstr(JText::translate('VBJQCALTUE'), $format).'", "'.self::applySubstr(JText::translate('VBJQCALWED'), $format).'", "'.self::applySubstr(JText::translate('VBJQCALTHU'), $format).'", "'.self::applySubstr(JText::translate('VBJQCALFRI'), $format).'", "'.self::applySubstr(JText::translate('VBJQCALSAT'), $format).'"]; |
| 32 |
var vboMapMons'.$module_id.' = ["'.self::applySubstr(JText::translate('VBMONTHONE'), $format).'","'.self::applySubstr(JText::translate('VBMONTHTWO'), $format).'","'.self::applySubstr(JText::translate('VBMONTHTHREE'), $format).'","'.self::applySubstr(JText::translate('VBMONTHFOUR'), $format).'","'.self::applySubstr(JText::translate('VBMONTHFIVE'), $format).'","'.self::applySubstr(JText::translate('VBMONTHSIX'), $format).'","'.self::applySubstr(JText::translate('VBMONTHSEVEN'), $format).'","'.self::applySubstr(JText::translate('VBMONTHEIGHT'), $format).'","'.self::applySubstr(JText::translate('VBMONTHNINE'), $format).'","'.self::applySubstr(JText::translate('VBMONTHTEN'), $format).'","'.self::applySubstr(JText::translate('VBMONTHELEVEN'), $format).'","'.self::applySubstr(JText::translate('VBMONTHTWELVE'), $format).'"];'; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Counts the maximum adults, children or guests as a sum from all rooms. |
| 37 |
* |
| 38 |
* @param string $gtype the type of guest "adults", "children", "guests". |
| 39 |
* |
| 40 |
* @return int the total number of guests for all rooms. |
| 41 |
* |
| 42 |
* @since 1.5.0 |
| 43 |
*/ |
| 44 |
public static function getMaxestGuests($gtype = 'adults') |
| 45 |
{ |
| 46 |
$dbo = JFactory::getDbo(); |
| 47 |
|
| 48 |
$sum_col = 'toadult'; |
| 49 |
if (!strcasecmp($gtype, 'children')) { |
| 50 |
$sum_col = 'tochild'; |
| 51 |
} elseif (!strcasecmp($gtype, 'guests')) { |
| 52 |
$sum_col = 'totpeople'; |
| 53 |
} |
| 54 |
|
| 55 |
$q = "SELECT SUM(`{$sum_col}`) FROM `#__vikbooking_rooms` WHERE `avail`=1"; |
| 56 |
$dbo->setQuery($q, 0, 1); |
| 57 |
$dbo->execute(); |
| 58 |
if ($dbo->getNumRows()) { |
| 59 |
return (int)$dbo->loadResult(); |
| 60 |
} |
| 61 |
|
| 62 |
return 1; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Fetches from the custom fields a list of basic fields |
| 67 |
* for the request information (inquiry) form. |
| 68 |
* |
| 69 |
* @return array the list of fieldset objects. |
| 70 |
* |
| 71 |
* @since 1.5.0 |
| 72 |
*/ |
| 73 |
public static function grabInquiryFields() |
| 74 |
{ |
| 75 |
$vbo_tn = VikBooking::getTranslator(); |
| 76 |
|
| 77 |
$dbo = JFactory::getDbo(); |
| 78 |
|
| 79 |
// build the list of inquiry fields |
| 80 |
$inquiry_fields = array(); |
| 81 |
|
| 82 |
// grab the first two nominative fields |
| 83 |
$q = "SELECT * FROM `#__vikbooking_custfields` WHERE `isnominative`=1 ORDER BY `ordering` ASC"; |
| 84 |
$dbo->setQuery($q, 0, 2); |
| 85 |
$dbo->execute(); |
| 86 |
if ($dbo->getNumRows()) { |
| 87 |
// translate records |
| 88 |
$records = $dbo->loadAssocList(); |
| 89 |
$vbo_tn->translateContents($records, '#__vikbooking_custfields'); |
| 90 |
// prepare the fieldset object of type nominative to push |
| 91 |
$fieldset = new stdClass; |
| 92 |
$fieldset->type = 'nominative'; |
| 93 |
$fieldset->required = 1; |
| 94 |
$fieldset->fields = json_decode(json_encode($records)); |
| 95 |
// push fieldset |
| 96 |
array_push($inquiry_fields, $fieldset); |
| 97 |
} |
| 98 |
|
| 99 |
// grab the first email field |
| 100 |
$q = "SELECT * FROM `#__vikbooking_custfields` WHERE `isemail`=1 ORDER BY `ordering` ASC"; |
| 101 |
$dbo->setQuery($q, 0, 1); |
| 102 |
$dbo->execute(); |
| 103 |
if ($dbo->getNumRows()) { |
| 104 |
// translate records |
| 105 |
$records = $dbo->loadAssocList(); |
| 106 |
$vbo_tn->translateContents($records, '#__vikbooking_custfields'); |
| 107 |
// prepare the fieldset object of type email to push |
| 108 |
$fieldset = new stdClass; |
| 109 |
$fieldset->type = 'email'; |
| 110 |
$fieldset->required = 1; |
| 111 |
$fieldset->fields = json_decode(json_encode($records)); |
| 112 |
// push fieldset |
| 113 |
array_push($inquiry_fields, $fieldset); |
| 114 |
} |
| 115 |
|
| 116 |
// grab the first phone field |
| 117 |
$q = "SELECT * FROM `#__vikbooking_custfields` WHERE `isphone`=1 ORDER BY `ordering` ASC"; |
| 118 |
$dbo->setQuery($q, 0, 1); |
| 119 |
$dbo->execute(); |
| 120 |
if ($dbo->getNumRows()) { |
| 121 |
// translate records |
| 122 |
$records = $dbo->loadAssocList(); |
| 123 |
$vbo_tn->translateContents($records, '#__vikbooking_custfields'); |
| 124 |
// prepare the fieldset object of type phone to push |
| 125 |
$fieldset = new stdClass; |
| 126 |
$fieldset->type = 'phone'; |
| 127 |
$fieldset->fields = json_decode(json_encode($records)); |
| 128 |
// push fieldset |
| 129 |
array_push($inquiry_fields, $fieldset); |
| 130 |
} |
| 131 |
|
| 132 |
// grab the first country field |
| 133 |
$q = "SELECT * FROM `#__vikbooking_custfields` WHERE `type`='country' ORDER BY `ordering` ASC"; |
| 134 |
$dbo->setQuery($q, 0, 1); |
| 135 |
$dbo->execute(); |
| 136 |
if ($dbo->getNumRows()) { |
| 137 |
// translate records |
| 138 |
$records = $dbo->loadAssocList(); |
| 139 |
$vbo_tn->translateContents($records, '#__vikbooking_custfields'); |
| 140 |
// prepare the fieldset object of type country to push |
| 141 |
$fieldset = new stdClass; |
| 142 |
$fieldset->type = 'country'; |
| 143 |
$fieldset->fields = json_decode(json_encode($records)); |
| 144 |
// push fieldset |
| 145 |
array_push($inquiry_fields, $fieldset); |
| 146 |
} |
| 147 |
|
| 148 |
// grab the first city field |
| 149 |
$q = "SELECT * FROM `#__vikbooking_custfields` WHERE `flag`='city' ORDER BY `ordering` ASC"; |
| 150 |
$dbo->setQuery($q, 0, 1); |
| 151 |
$dbo->execute(); |
| 152 |
if ($dbo->getNumRows()) { |
| 153 |
// translate records |
| 154 |
$records = $dbo->loadAssocList(); |
| 155 |
$vbo_tn->translateContents($records, '#__vikbooking_custfields'); |
| 156 |
// prepare the fieldset object of type city to push |
| 157 |
$fieldset = new stdClass; |
| 158 |
$fieldset->type = 'city'; |
| 159 |
$fieldset->fields = json_decode(json_encode($records)); |
| 160 |
// push fieldset |
| 161 |
array_push($inquiry_fields, $fieldset); |
| 162 |
} |
| 163 |
|
| 164 |
// always push the message/special request field |
| 165 |
$fieldset = new stdClass; |
| 166 |
$fieldset->type = 'special_requests'; |
| 167 |
$fieldset->required = 1; |
| 168 |
$fieldset->fields = array('textarea'); |
| 169 |
// push fieldset |
| 170 |
array_push($inquiry_fields, $fieldset); |
| 171 |
|
| 172 |
// grab the first two mandatory checkbox fields |
| 173 |
$q = "SELECT * FROM `#__vikbooking_custfields` WHERE `type`='checkbox' AND `required`=1 ORDER BY `ordering` ASC"; |
| 174 |
$dbo->setQuery($q, 0, 2); |
| 175 |
$dbo->execute(); |
| 176 |
if ($dbo->getNumRows()) { |
| 177 |
// translate records |
| 178 |
$records = $dbo->loadAssocList(); |
| 179 |
$vbo_tn->translateContents($records, '#__vikbooking_custfields'); |
| 180 |
// prepare the fieldset object of type checkbox to push |
| 181 |
$fieldset = new stdClass; |
| 182 |
$fieldset->type = 'checkbox'; |
| 183 |
$fieldset->required = 1; |
| 184 |
$fieldset->fields = json_decode(json_encode($records)); |
| 185 |
// push fieldset |
| 186 |
array_push($inquiry_fields, $fieldset); |
| 187 |
} |
| 188 |
|
| 189 |
// return the list of fieldsets |
| 190 |
return $inquiry_fields; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Evaluates the fieldset type and field data to return the type of input. |
| 195 |
* |
| 196 |
* @param string $type the type of the fieldset to evaluate. |
| 197 |
* @param mixed $field the current inquiry field, either an object or a string. |
| 198 |
* |
| 199 |
* @return string the type identifier of the field to display. |
| 200 |
* |
| 201 |
* @since 1.5.0 |
| 202 |
*/ |
| 203 |
public static function parseFieldType($type, $field) |
| 204 |
{ |
| 205 |
// accepted list of fields to render |
| 206 |
$valid_fields = array( |
| 207 |
'text', |
| 208 |
'textarea', |
| 209 |
'number', |
| 210 |
'email', |
| 211 |
'phone', |
| 212 |
'checkbox', |
| 213 |
); |
| 214 |
|
| 215 |
if (is_object($field)) { |
| 216 |
// we expect this to be a record from the custom fields |
| 217 |
if ($type == 'nominative') { |
| 218 |
return 'text'; |
| 219 |
} |
| 220 |
if ($type == 'email') { |
| 221 |
return 'email'; |
| 222 |
} |
| 223 |
if ($type == 'phone') { |
| 224 |
return 'phone'; |
| 225 |
} |
| 226 |
if ($type == 'country') { |
| 227 |
return 'country'; |
| 228 |
} |
| 229 |
if ($type == 'city') { |
| 230 |
return 'text'; |
| 231 |
} |
| 232 |
if ($type == 'checkbox') { |
| 233 |
return 'checkbox'; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
// this could be a string for building a textarea or fallback to text |
| 238 |
return in_array($field, $valid_fields) ? $field : 'text'; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Evaluates the fieldset type and field data to return the label for the input. |
| 243 |
* |
| 244 |
* @param string $type the type of the fieldset to evaluate. |
| 245 |
* @param mixed $field the current inquiry field, either an object or a string. |
| 246 |
* |
| 247 |
* @return string the label (title) for the field to display. |
| 248 |
* |
| 249 |
* @since 1.5.0 |
| 250 |
*/ |
| 251 |
public static function parseFieldLabel($type, $field) |
| 252 |
{ |
| 253 |
if (is_object($field) && isset($field->name)) { |
| 254 |
$label = JText::translate($field->name); |
| 255 |
|
| 256 |
if (!empty($field->poplink)) { |
| 257 |
// try not to make the whole translation string a link |
| 258 |
$label_parts = explode(' ', $label); |
| 259 |
if (count($label_parts) > 4) { |
| 260 |
$label_out = implode(' ', array_slice($label_parts, 0, (count($label_parts) - 4))) . ' '; |
| 261 |
$label_in = implode(' ', array_slice($label_parts, -4)); |
| 262 |
} else { |
| 263 |
// unable to make the linked part shorter |
| 264 |
$label_out = ''; |
| 265 |
$label_in = $label; |
| 266 |
} |
| 267 |
// build final link tag |
| 268 |
$label = $label_out . '<a href="' . $field->poplink . '" target="_blank">' . $label_in . '</a>'; |
| 269 |
} |
| 270 |
|
| 271 |
return $label; |
| 272 |
} |
| 273 |
|
| 274 |
if ($type == 'special_requests') { |
| 275 |
return JText::translate('INQ_NOTES_SPREQUESTS'); |
| 276 |
} |
| 277 |
|
| 278 |
return ucwords(str_replace(array('_', '-'), ' ', $type)); |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Returns a string with the requested length. |
| 283 |
* |
| 284 |
* @param string $text the text to apply the substr onto. |
| 285 |
* @param string $format either long or 3char. |
| 286 |
* |
| 287 |
* @return string |
| 288 |
* |
| 289 |
* @since 1.1.0 |
| 290 |
*/ |
| 291 |
private static function applySubstr($text, $format) |
| 292 |
{ |
| 293 |
$mb_supported = function_exists('mb_substr'); |
| 294 |
|
| 295 |
if ($format == 'long') { |
| 296 |
return $text; |
| 297 |
} |
| 298 |
|
| 299 |
return $mb_supported ? mb_substr($text, 0, 3, 'UTF-8') : substr($text, 0, 3); |
| 300 |
} |
| 301 |
} |
| 302 |
|