| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* Wizard main country (and state) help instruction. |
| 16 |
* |
| 17 |
* @since 1.18.2 (J) - 1.8.2 (WP) |
| 18 |
*/ |
| 19 |
class VBOHelpWizardDriverGeneralBaseCountry extends VBOHelpWizardInstructionaware |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @inheritDoc |
| 23 |
*/ |
| 24 |
public function getID() |
| 25 |
{ |
| 26 |
return 'general.base_country'; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* @inheritDoc |
| 31 |
*/ |
| 32 |
public function getIcon() |
| 33 |
{ |
| 34 |
return VikBookingIcons::i('map-signs'); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @inheritDoc |
| 39 |
*/ |
| 40 |
public function getPriority() |
| 41 |
{ |
| 42 |
return PHP_INT_MAX; // highest priority |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* @inheritDoc |
| 47 |
*/ |
| 48 |
public function isConfigured() |
| 49 |
{ |
| 50 |
return VBOFactory::getConfig()->getBool('maincountry'); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* @inheritDoc |
| 55 |
*/ |
| 56 |
public function isDismissible() |
| 57 |
{ |
| 58 |
// users cannot skip this instruction |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @inheritDoc |
| 64 |
*/ |
| 65 |
public function isProcessable(?string &$btnText = null) |
| 66 |
{ |
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @inheritDoc |
| 72 |
*/ |
| 73 |
public function process(array $args = []) |
| 74 |
{ |
| 75 |
$countryCode = $args['country'] ?? null; |
| 76 |
|
| 77 |
if (empty($countryCode)) { |
| 78 |
// country not selected |
| 79 |
throw new InvalidArgumentException(JText::translate('VBOREPORTERRNODATA'), 400); |
| 80 |
} |
| 81 |
|
| 82 |
// make sure the country exists |
| 83 |
$countryId = VBOStateHelper::getCountryId($countryCode); |
| 84 |
|
| 85 |
if (!$countryId) { |
| 86 |
throw new UnexpectedValueException('The selected country (' . $countryCode . ') does not exist!', 404); |
| 87 |
} |
| 88 |
|
| 89 |
// get country states |
| 90 |
$states = VBOStateHelper::getCountryStates($countryId); |
| 91 |
|
| 92 |
if (!strcasecmp($args['scope'] ?? '', 'states')) { |
| 93 |
// return the available states to the caller |
| 94 |
return [ |
| 95 |
'states' => $states, |
| 96 |
]; |
| 97 |
} |
| 98 |
|
| 99 |
$stateCode = $args['state'] ?? null; |
| 100 |
|
| 101 |
if ($states) { |
| 102 |
// take only the state matching the selected code |
| 103 |
$state = array_filter($states, function($state) use ($stateCode) { |
| 104 |
return $state['state_2_code'] == $stateCode; |
| 105 |
}); |
| 106 |
|
| 107 |
if (!$state) { |
| 108 |
// state not selected |
| 109 |
throw new InvalidArgumentException(JText::translate('VBOREPORTERRNODATA'), 400); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// save main country and state on VBO configuration |
| 114 |
$config = VBOFactory::getConfig(); |
| 115 |
$config->set('maincountry', $countryCode); |
| 116 |
$config->set('mainstate', (string) $stateCode); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Returns the most probable country where the business is based. |
| 121 |
* |
| 122 |
* @return string Country 2 code. |
| 123 |
*/ |
| 124 |
public function guessCountry() |
| 125 |
{ |
| 126 |
$host = JUri::getInstance()->getHost(); |
| 127 |
|
| 128 |
// extract TLD from domain (only if 2 chars) |
| 129 |
if (preg_match("/\.([a-z]{2,2})\/?$/i", $host, $match)) { |
| 130 |
$countryCode = strtoupper(end($match)); |
| 131 |
|
| 132 |
$lookup = [ |
| 133 |
'UK' => 'GB', |
| 134 |
]; |
| 135 |
|
| 136 |
$countryCode = $lookup[$countryCode] ?? $countryCode; |
| 137 |
|
| 138 |
// check whether the TLD matches a country code (IT, DE, FR and so on) |
| 139 |
if (VBOStateHelper::getCountryId($countryCode)) { |
| 140 |
return $countryCode; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
$timezone = JFactory::getApplication()->get('offset'); |
| 145 |
|
| 146 |
switch ($timezone) { |
| 147 |
case 'Europe/Rome': |
| 148 |
return 'IT'; |
| 149 |
|
| 150 |
case 'Europe/Athens': |
| 151 |
return 'GR'; |
| 152 |
|
| 153 |
case 'Europe/Madrid': |
| 154 |
return 'ES'; |
| 155 |
|
| 156 |
case 'Europe/London': |
| 157 |
return 'GB'; |
| 158 |
|
| 159 |
case 'Europe/Paris': |
| 160 |
return 'FR'; |
| 161 |
|
| 162 |
case 'Europe/Berlin': |
| 163 |
return 'DE'; |
| 164 |
} |
| 165 |
|
| 166 |
return ''; |
| 167 |
} |
| 168 |
} |
| 169 |
|