PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.5
VikBooking Hotel Booking Engine & PMS v1.6.5
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
vikbooking / admin / helpers / src / state / helper.php

helper.php in VikBooking Hotel Booking Engine & PMS 1.6.5, at admin/helpers/src/state/helper.php

123 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 = "SELECT * FROM `#__vikbooking_states` WHERE `id_country`=" . $id_country . " AND `state_2_code`=" . $dbo->quote($state_2_code);
47 $dbo->setQuery($q, 0, 1);
48 $dbo->execute();
49 if (!$dbo->getNumRows()) {
50 // no records found
51 return $state_2_code;
52 }
53
54 $state_info = $dbo->loadAssoc();
55
56 return $whole ? $state_info : $state_info['state_name'];
57 }
58
59 /**
60 * Attempts to find the country ID from the given identifier.
61 *
62 * @param string $country country identifier (name or 2/3-char code).
63 *
64 * @return int country ID found or null.
65 */
66 public static function getCountryId($country)
67 {
68 $dbo = JFactory::getDbo();
69
70 if (is_numeric($country)) {
71 $q = "SELECT `id` FROM `#__vikbooking_countries` WHERE `id`=" . (int)$country;
72 $dbo->setQuery($q, 0, 1);
73 $dbo->execute();
74 if (!$dbo->getNumRows()) {
75 // no records found for this country
76 return null;
77 }
78 return $dbo->loadResult();
79 }
80
81 // find country ID by name or code
82 $field_value = $country;
83 $field_name = $dbo->qn('country_name');
84 if (strlen($country) == 3) {
85 $field_name = $dbo->qn('country_3_code');
86 } elseif (strlen($country) == 2) {
87 $field_name = $dbo->qn('country_2_code');
88 }
89
90 $q = "SELECT `id` FROM `#__vikbooking_countries` WHERE {$field_name}=" . $dbo->quote($field_value);
91 $dbo->setQuery($q, 0, 1);
92 $dbo->execute();
93 if (!$dbo->getNumRows()) {
94 // country not found
95 return null;
96 }
97
98 return $dbo->loadResult();
99 }
100
101 /**
102 * Returns all states for the given country ID.
103 *
104 * @param int $id_country
105 *
106 * @return array list of states found, if any.
107 */
108 public static function getCountryStates($id_country)
109 {
110 $dbo = JFactory::getDbo();
111
112 $q = "SELECT * FROM `#__vikbooking_states` WHERE `id_country`=" . (int)$id_country;
113 $dbo->setQuery($q);
114 $dbo->execute();
115 if (!$dbo->getNumRows()) {
116 // no records found for this country
117 return [];
118 }
119
120 return $dbo->loadAssocList();
121 }
122 }
123