| @@ -1,0 +1,150 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikBooking | |
| 4 | + * @subpackage core | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2022 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 | + * State/Province helper class. | |
| 16 | + * | |
| 17 | + * @since 1.16.0 (J) - 1.6.0 (WP) | |
| 18 | + */ | |
| 19 | +class VBOStateHelper | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * Finds the state/province full name from the given state 2-char code and country. | |
| 23 | + * | |
| 24 | + * @param string $state_2_code the selected state/province. | |
| 25 | + * @param string $country the selected country (probably 3-char code). | |
| 26 | + * @param boolean $whole true to get the whole state record found. | |
| 27 | + * | |
| 28 | + * @return string|array the state/province full name found or whole record. | |
| 29 | + */ | |
| 30 | + public static function getFullName($state_2_code, $country = '', $whole = false) | |
| 31 | + { | |
| 32 | + $dbo = JFactory::getDbo(); | |
| 33 | + | |
| 34 | + if (empty($state_2_code) || empty($country)) { | |
| 35 | + return $state_2_code; | |
| 36 | + } | |
| 37 | + | |
| 38 | + // find the ID of the given country | |
| 39 | + $id_country = static::getCountryId($country); | |
| 40 | + | |
| 41 | + if (empty($id_country)) { | |
| 42 | + return $state_2_code; | |
| 43 | + } | |
| 44 | + | |
| 45 | + // make sure this province exists | |
| 46 | + $q = $dbo->getQuery(true) | |
| 47 | + ->select('*') | |
| 48 | + ->from($dbo->qn('#__vikbooking_states')) | |
| 49 | + ->where($dbo->qn('id_country') . ' = ' . (int) $id_country) | |
| 50 | + ->where($dbo->qn('state_2_code') . ' = ' . $dbo->q($state_2_code)); | |
| 51 | + $dbo->setQuery($q, 0, 1); | |
| 52 | + $dbo->execute(); | |
| 53 | + if (!$dbo->getNumRows()) { | |
| 54 | + // no records found | |
| 55 | + return $state_2_code; | |
| 56 | + } | |
| 57 | + | |
| 58 | + $state_info = $dbo->loadAssoc(); | |
| 59 | + | |
| 60 | + return $whole ? $state_info : $state_info['state_name']; | |
| 61 | + } | |
| 62 | + | |
| 63 | + /** | |
| 64 | + * Attempts to find the country ID from the given identifier. | |
| 65 | + * | |
| 66 | + * @param string $country country identifier (id, name or 2/3-char code). | |
| 67 | + * | |
| 68 | + * @return int country ID found or null. | |
| 69 | + */ | |
| 70 | + public static function getCountryId($country) | |
| 71 | + { | |
| 72 | + $dbo = JFactory::getDbo(); | |
| 73 | + | |
| 74 | + if (is_numeric($country)) { | |
| 75 | + $q = $dbo->getQuery(true) | |
| 76 | + ->select($dbo->qn('id')) | |
| 77 | + ->from($dbo->qn('#__vikbooking_countries')) | |
| 78 | + ->where($dbo->qn('id') . ' = ' . (int) $country); | |
| 79 | + | |
| 80 | + $dbo->setQuery($q, 0, 1); | |
| 81 | + | |
| 82 | + return $dbo->loadResult(); | |
| 83 | + } | |
| 84 | + | |
| 85 | + // no null values accepted | |
| 86 | + $country = (string) $country; | |
| 87 | + | |
| 88 | + // find country ID by name or code | |
| 89 | + $field_value = $country; | |
| 90 | + $field_name = $dbo->qn('country_name'); | |
| 91 | + if (strlen($country) == 3) { | |
| 92 | + $field_name = $dbo->qn('country_3_code'); | |
| 93 | + } elseif (strlen($country) == 2) { | |
| 94 | + $field_name = $dbo->qn('country_2_code'); | |
| 95 | + } | |
| 96 | + | |
| 97 | + $q = $dbo->getQuery(true) | |
| 98 | + ->select($dbo->qn('id')) | |
| 99 | + ->from($dbo->qn('#__vikbooking_countries')) | |
| 100 | + ->where($field_name . ' = ' . $dbo->q($field_value)); | |
| 101 | + | |
| 102 | + $dbo->setQuery($q, 0, 1); | |
| 103 | + | |
| 104 | + return $dbo->loadResult(); | |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * Returns all states for the given country ID. | |
| 109 | + * | |
| 110 | + * @param int $id_country | |
| 111 | + * | |
| 112 | + * @return array list of states found, if any. | |
| 113 | + */ | |
| 114 | + public static function getCountryStates($id_country) | |
| 115 | + { | |
| 116 | + $dbo = JFactory::getDbo(); | |
| 117 | + | |
| 118 | + $q = $dbo->getQuery(true) | |
| 119 | + ->select('*') | |
| 120 | + ->from($dbo->qn('#__vikbooking_states')) | |
| 121 | + ->where($dbo->qn('id_country') . ' = ' . (int) $id_country); | |
| 122 | + | |
| 123 | + $dbo->setQuery($q); | |
| 124 | + | |
| 125 | + return $dbo->loadAssocList(); | |
| 126 | + } | |
| 127 | + | |
| 128 | + /** | |
| 129 | + * Returns the country record from the given ID. | |
| 130 | + * | |
| 131 | + * @param int $id_country The country record ID. | |
| 132 | + * | |
| 133 | + * @return ?array Associative record or null. | |
| 134 | + * | |
| 135 | + * @since 1.17.2 (J) - 1.7.2 (WP) | |
| 136 | + */ | |
| 137 | + public static function getCountryData($id_country) | |
| 138 | + { | |
| 139 | + $dbo = JFactory::getDbo(); | |
| 140 | + | |
| 141 | + $q = $dbo->getQuery(true) | |
| 142 | + ->select('*') | |
| 143 | + ->from($dbo->qn('#__vikbooking_countries')) | |
| 144 | + ->where($dbo->qn('id') . ' = ' . (int) $id_country); | |
| 145 | + | |
| 146 | + $dbo->setQuery($q); | |
| 147 | + | |
| 148 | + return $dbo->loadAssoc(); | |
| 149 | + } | |
| 150 | +} | |