PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / import / classes / locations.php
vikappointments / site / helpers / libraries / import / classes Last commit date
cities.php 3 days ago coupons.php 3 days ago customers.php 3 days ago empgroups.php 3 days ago employees.php 3 days ago groups.php 3 days ago index.html 3 days ago locations.php 3 days ago optiongroups.php 3 days ago options.php 3 days ago reviews.php 3 days ago services.php 3 days ago states.php 3 days ago
locations.php
255 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 VAPLoader::import('libraries.import.object');
15
16 /**
17 * Class used to handle an import event for the LOCATIONS.
18 *
19 * @see ImportObject
20 * @since 1.7.3
21 */
22 class ImportObjectLocations extends ImportObject
23 {
24 /**
25 * Overloaded bind function.
26 *
27 * @param object &$data The object of the record to import.
28 * @param array $args Associative list of additional parameters.
29 *
30 * @return boolean True if the record should be imported, otherwise false.
31 */
32 protected function bind(&$data, array $args = array())
33 {
34 // check whether the employee is not specified and the import
35 // request rather include it
36 if (empty($data->id_employee) && !empty($args['id_employee']))
37 {
38 // force employee ID
39 $data->id_employee = $args['id_employee'];
40 }
41
42 // check if we are dealing with a country 2 code
43 if (!empty($data->id_country) && preg_match("/^[A-Z][A-Z0-9]$/i", $data->id_country))
44 {
45 // overwrite country 2 code with ID
46 $data->id_country = $this->searchCountry($data->id_country);
47 }
48
49 // check if we are dealing with a state 2 code
50 if (!empty($data->id_state) && preg_match("/^[A-Z][A-Z0-9]$/i", $data->id_state) && !empty($data->id_country))
51 {
52 // overwrite state 2 code with ID
53 $data->id_state = $this->searchState($data->id_country, $data->id_state);
54 }
55
56 // check if we are dealing with a city 2 code
57 if (!empty($data->id_city) && preg_match("/^[A-Z][A-Z0-9]$/i", $data->id_city) && !empty($data->id_state))
58 {
59 // overwrite city 2 code with ID
60 $data->id_city = $this->searchCity($data->id_state, $data->id_city);
61 }
62
63 // call parent method to check the data integrity
64 return parent::bind($data);
65 }
66
67 /**
68 * Helper method used to search the matching ID for the given country 2 code.
69 *
70 * @param string $code The country 2 code.
71 *
72 * @return integer The matching ID.
73 */
74 private function searchCountry($code)
75 {
76 static $countries = [];
77
78 // check whether the same code has been already fetched
79 if (!isset($countries[$code]))
80 {
81 // nope, cache initial result
82 $countries[$code] = 0;
83
84 // fetch country details
85 $item = JModelVAP::getInstance('country')->getItem([
86 'country_2_code' => $code,
87 ]);
88
89 if ($item)
90 {
91 // register the ID of the country 2 code
92 $countries[$code] = $item->id;
93 }
94 }
95
96 return $countries[$code];
97 }
98
99 /**
100 * Helper method used to search the matching ID for the given state 2 code.
101 *
102 * @param integer $country The ID of the country to which the state
103 * should belong.
104 * @param string $code The state 2 code.
105 *
106 * @return integer The matching ID.
107 */
108 private function searchState($country, $code)
109 {
110 static $states = [];
111
112 // check whether the same code has been already fetched
113 if (!isset($states[$code]))
114 {
115 // nope, cache initial result
116 $states[$code] = 0;
117
118 // fetch state details
119 $item = JModelVAP::getInstance('state')->getItem([
120 'state_2_code' => $code,
121 'id_country' => (int) $country,
122 ]);
123
124 if ($item)
125 {
126 // register the ID of the state 2 code
127 $states[$code] = $item->id;
128 }
129 }
130
131 return $states[$code];
132 }
133
134 /**
135 * Helper method used to search the matching ID for the given city 2 code.
136 *
137 * @param integer $state The ID of the state to which the city
138 * should belong.
139 * @param string $code The city 2 code.
140 *
141 * @return integer The matching ID.
142 */
143 private function searchCity($state, $code)
144 {
145 static $cities = [];
146
147 // check whether the same code has been already fetched
148 if (!isset($cities[$code]))
149 {
150 // nope, cache initial result
151 $cities[$code] = 0;
152
153 // fetch city details
154 $item = JModelVAP::getInstance('city')->getItem([
155 'city_2_code' => $code,
156 'id_state' => (int) $state,
157 ]);
158
159 if ($item)
160 {
161 // register the ID of the city 2 code
162 $cities[$code] = $item->id;
163 }
164 }
165
166 return $cities[$code];
167 }
168
169 /**
170 * Builds the base query to export all the records.
171 * @since 1.7 $app and $dbo are now included within the $options argument.
172 *
173 * @param JObject $options A registry of export options.
174 * @param string $alias The table alias.
175 *
176 * @return mixed The query builder object.
177 *
178 * @uses getColumns()
179 * @uses getTable()
180 * @uses getPrimaryKey()
181 */
182 protected function buildExportQuery($options, $alias = 'l')
183 {
184 $app = $options->get('app');
185 $dbo = $options->get('dbo');
186
187 $filters = array();
188 $filters['keys'] = $app->getUserStateFromRequest('vaplocations.keys', 'keys', '', 'string');
189 $filters['id_country'] = $app->getUserStateFromRequest('vaplocations.country', 'id_country', 0, 'uint');
190 $filters['id_state'] = $app->getUserStateFromRequest('vaplocations.state[' . $filters['id_country'] . ']', 'id_state', 0, 'uint');
191 $filters['id_city'] = $app->getUserStateFromRequest('vaplocations.city[' . $filters['id_state'] . ']', 'id_city', 0, 'uint');
192 $filters['id_employee'] = $options->get('id_employee', 0);
193
194 $q = parent::buildExportQuery($options, $alias);
195
196 if ($filters['id_employee'])
197 {
198 // filter the location by employee
199 $q->where($dbo->qn('l.id_employee') . ' = ' . $filters['id_employee']);
200 }
201 else
202 {
203 // retrieve also the location owner (employee)
204 $q->leftjoin($dbo->qn('#__vikappointments_employee', 'e') . ' ON ' . $dbo->qn('e.id') . ' = ' . $dbo->qn('l.id_employee'));
205 }
206
207 if (strlen($filters['keys']))
208 {
209 $key = $dbo->q("%{$filters['keys']}%");
210
211 $where = array();
212 // search by location name
213 $where[] = $dbo->qn('l.name') . ' LIKE ' . $key;
214 // search by location address
215 $where[] = $dbo->qn('l.address') . ' LIKE ' . $key;
216 // search by ZIP code
217 $where[] = $dbo->qn('l.zip') . ' LIKE ' . $key;
218 // search by country
219 $where[] = $dbo->qn('c.country_name') . ' LIKE ' . $key;
220 // search by state
221 $where[] = $dbo->qn('s.state_name') . ' LIKE ' . $key;
222 // search by city
223 $where[] = $dbo->qn('c.city_name') . ' LIKE ' . $key;
224
225 if (!$filters['id_employee'])
226 {
227 $sprintf = 'CONCAT_WS(\' \', %s, %s) LIKE %s';
228
229 // search also by employee full name (first + last and last + first)
230 $where[] = sprintf($sprintf, $dbo->qn('e.firstname'), $dbo->qn('e.lastname'), $key);
231 $where[] = sprintf($sprintf, $dbo->qn('e.lastname'), $dbo->qn('e.firstname'), $key);
232
233 $q->andWhere($where, 'OR');
234 }
235 }
236
237 if ($filters['id_country'])
238 {
239 $q->where($dbo->qn('l.id_country') . ' = ' . $filters['id_country']);
240
241 if ($filters['id_state'])
242 {
243 $q->where($dbo->qn('l.id_state') . ' = ' . $filters['id_state']);
244
245 if ($filters['id_city'])
246 {
247 $q->where($dbo->qn('l.id_city') . ' = ' . $filters['id_city']);
248 }
249 }
250 }
251
252 return $q;
253 }
254 }
255