PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.20
VikAppointments Services Booking Calendar v1.2.20
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / html / countries.php
vikappointments / site / helpers / libraries / html Last commit date
assets.php 1 month ago color.php 1 month ago countries.php 1 month ago index.html 1 month ago media.php 1 month ago site.php 1 month ago sitescripts.php 1 month ago status.php 1 month ago vikappointments.php 1 month ago
countries.php
220 lines
1 <?php
2 /**
3 * @package VikAppointments
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 * VikAppointments countries class handler.
16 *
17 * @since 1.7
18 */
19 abstract class VAPHtmlCountries
20 {
21 /**
22 * Countries cache/lookup.
23 *
24 * @var array
25 */
26 protected static $countries = array();
27
28 /**
29 * Returns the list of supported countries.
30 *
31 * @param mixed $orderby The ordering column or an array with column and direction.
32 *
33 * @return array The countries list.
34 */
35 public static function getlist($orderby = 'id')
36 {
37 if (is_scalar($orderby))
38 {
39 $orderby = array($orderby, 'ASC');
40 }
41
42 // route ordering column
43 $orderby[0] = static::ordcol($orderby[0]);
44
45 static $all = false;
46
47 // check if we already retrieved the countries from the database
48 if (!$all)
49 {
50 // do not execute again
51 $all = true;
52
53 // clear countries list
54 static::$countries = array();
55
56 $dbo = JFactory::getDbo();
57
58 $q = $dbo->getQuery(true)
59 ->select('*')
60 ->from($dbo->qn('#__vikappointments_countries'))
61 ->where($dbo->qn('published') . ' = 1')
62 ->order($dbo->qn('id') . ' ASC');
63
64 $dbo->setQuery($q);
65
66 // iterate countries and cache them
67 foreach ($dbo->loadObjectList() as $row)
68 {
69 static::$countries[$row->country_2_code] = static::db2country($row);
70 }
71 }
72
73 // keep countries
74 $list = array_values(static::$countries);
75
76 // do not sort in case of default ordering
77 if ($orderby[0] != 'id' || $orderby[1] != 'ASC')
78 {
79 // sort the countries with the given ordering
80 usort($list, function($a, $b) use ($orderby)
81 {
82 // fetch ordering direction factor
83 $factor = strcasecmp($orderby[1], 'DESC') ? 1 : -1;
84
85 if ($a->{$orderby[0]} > $b->{$orderby[0]})
86 {
87 return 1 * $factor;
88 }
89
90 if ($a->{$orderby[0]} < $b->{$orderby[0]})
91 {
92 return -1 * $factor;
93 }
94
95 return 0;
96 });
97 }
98
99 return $list;
100 }
101
102 /**
103 * Returns the published country that match the given 2 letters code.
104 *
105 * @param string $country_2_code The 2 letters code (ISO 3166).
106 *
107 * @return mixed The country details on success, false otherwise.
108 */
109 public static function withcode($country_2_code)
110 {
111 $country_2_code = strtoupper($country_2_code);
112
113 // recover country from database if not set
114 if (!isset(static::$countries[$country_2_code]))
115 {
116 $dbo = JFactory::getDbo();
117
118 $q = $dbo->getQuery(true)
119 ->select('*')
120 ->from($dbo->qn('#__vikappointments_countries'))
121 ->where($dbo->qn('published') . ' = 1')
122 ->where($dbo->qn('country_2_code') . ' = ' . $dbo->q($country_2_code));
123
124 $dbo->setQuery($q, 0, 1);
125 $country = $dbo->loadObject();
126
127 if ($country)
128 {
129 // cache country
130 static::$countries[$country_2_code] = static::db2country($country);
131 }
132 else
133 {
134 // mark the country as missing
135 static::$countries[$country_2_code] = false;
136 }
137 }
138
139 return static::$countries[$country_2_code];
140 }
141
142 /**
143 * Creates a country object by using the details
144 * retrieved from the database.
145 *
146 * @param mixed $record The database record.
147 *
148 * @return object The country object.
149 */
150 public static function db2country($record)
151 {
152 $record = (object) $record;
153
154 $country = new stdClass;
155
156 if (isset($record->id))
157 {
158 $country->id = $record->id;
159 }
160
161 if (isset($record->country_name))
162 {
163 $country->name = $record->country_name;
164 }
165
166 if (isset($record->country_2_code))
167 {
168 $country->code2 = $record->country_2_code;
169 }
170
171 if (isset($record->country_3_code))
172 {
173 $country->code3 = $record->country_3_code;
174 }
175
176 if (isset($record->phone_prefix))
177 {
178 $country->dial = $record->phone_prefix;
179 }
180
181 return $country;
182 }
183
184 /**
185 * Fetched the countries property that should be
186 * used to sort the list.
187 *
188 * @param string $column Either a database column or an object
189 * property for sorting the records.
190 *
191 * @return string The resulting column.
192 */
193 protected static function ordcol($column)
194 {
195 // reverse lookup to check whether a db column was specified
196 $lookup = array(
197 'id' => 'id',
198 'country_name' => 'name',
199 'country_2_code' => 'code2',
200 'country_3_code' => 'code3',
201 'phone_prefix' => 'dial',
202 );
203
204 if (isset($lookup[$column]))
205 {
206 // a db column was specified, route it
207 return $lookup[$column];
208 }
209
210 if (in_array($column, array_values($lookup)))
211 {
212 // the specified column is supported, return it
213 return $column;
214 }
215
216 // unsupported column, fallback to ID
217 return 'id';
218 }
219 }
220