| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author Alessio Gaggii - 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 |
* Helper class to handle reminders. |
| 16 |
* |
| 17 |
* @since 1.15.0 (J) - 1.5.0 (WP) |
| 18 |
*/ |
| 19 |
final class VBORemindersHelper extends JObject |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Proxy to construct the object. |
| 23 |
* |
| 24 |
* @param array|object $data optional data to bind. |
| 25 |
* |
| 26 |
* @return self |
| 27 |
*/ |
| 28 |
public static function getInstance($data = array()) |
| 29 |
{ |
| 30 |
return new static($data); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Loads records from the db table for the reminders. |
| 35 |
* |
| 36 |
* @param array $fetch optional query fetch options. |
| 37 |
* @param int $offset optional query limit start. |
| 38 |
* @param int $length optional query limit length. |
| 39 |
* |
| 40 |
* @return array list of record objects, if any. |
| 41 |
*/ |
| 42 |
public function loadReminders($fetch = [], $offset = 0, $length = 0) |
| 43 |
{ |
| 44 |
$dbo = JFactory::getDbo(); |
| 45 |
|
| 46 |
// build default fetch params |
| 47 |
$params = [ |
| 48 |
'after' => 'NOW', |
| 49 |
'before' => null, |
| 50 |
'idorder' => 0, |
| 51 |
'completed' => 0, |
| 52 |
'expired' => 0, |
| 53 |
]; |
| 54 |
if (is_array($fetch) && count($fetch)) { |
| 55 |
// merge fetch params |
| 56 |
$params = array_merge($params, $fetch); |
| 57 |
} |
| 58 |
|
| 59 |
// build query |
| 60 |
$clauses = []; |
| 61 |
$ordering = ["`duedate` ASC"]; |
| 62 |
if (empty($params['expired']) && !empty($params['after']) && is_string($params['after'])) { |
| 63 |
if (!strcasecmp($params['after'], 'NOW')) { |
| 64 |
$clauses[] = "`duedate` >= NOW()"; |
| 65 |
} else { |
| 66 |
// datetime string is expected |
| 67 |
$clauses[] = "`duedate` >= " . $dbo->quote($params['after']); |
| 68 |
} |
| 69 |
} |
| 70 |
if (!empty($params['before'])) { |
| 71 |
// set clause for max future date |
| 72 |
$clauses[] = "`duedate` <= " . $dbo->quote($params['before']); |
| 73 |
} |
| 74 |
if (empty($params['completed'])) { |
| 75 |
$clauses[] = "`completed` = 0"; |
| 76 |
} |
| 77 |
if (!empty($params['idorder'])) { |
| 78 |
// exclude all reminders not for this booking |
| 79 |
$params['idorder'] = (int)$params['idorder']; |
| 80 |
$clauses[] = "`idorder` IN (0, {$params['idorder']})"; |
| 81 |
// set ordering to display reminders for this booking on top |
| 82 |
array_unshift($ordering, "`idorder` DESC"); |
| 83 |
} |
| 84 |
|
| 85 |
$q = "SELECT * FROM `#__vikbooking_reminders`" . (count($clauses) ? ' WHERE ' . implode(' AND ', $clauses) : '') . " ORDER BY " . implode(', ', $ordering); |
| 86 |
$dbo->setQuery($q, $offset, $length); |
| 87 |
$reminders = $dbo->loadObjectList(); |
| 88 |
if (!$reminders) { |
| 89 |
return []; |
| 90 |
} |
| 91 |
|
| 92 |
// decode payload on all records, if needed |
| 93 |
foreach ($reminders as $k => $reminder) { |
| 94 |
if (!empty($reminder->payload)) { |
| 95 |
$reminders[$k]->payload = json_decode($reminder->payload); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return $reminders; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Returns a list of imminent reminders. |
| 104 |
* |
| 105 |
* @param int $length the maximum records to fetch. |
| 106 |
* |
| 107 |
* @return array list of object records, if any. |
| 108 |
*/ |
| 109 |
public function getImminents($length = 10) |
| 110 |
{ |
| 111 |
// imminents reminders are meant to not expire in more than 1 day |
| 112 |
$now_info = getdate(); |
| 113 |
$lim_max_date = date('Y-m-d H:i:s', mktime(23, 59, 59, $now_info['mon'], ($now_info['mday'] + 1), $now_info['year'])); |
| 114 |
|
| 115 |
$fetch = [ |
| 116 |
'after' => 'NOW', |
| 117 |
'before' => $lim_max_date, |
| 118 |
'idorder' => 0, |
| 119 |
'completed' => 0, |
| 120 |
'expired' => 0, |
| 121 |
]; |
| 122 |
|
| 123 |
return $this->loadReminders($fetch, 0, $length); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Gets a specific reminder by ID. |
| 128 |
* |
| 129 |
* @param int $rid the record ID. |
| 130 |
* |
| 131 |
* @return null|object |
| 132 |
*/ |
| 133 |
public function getReminder($rid) |
| 134 |
{ |
| 135 |
if (empty($rid)) { |
| 136 |
return null; |
| 137 |
} |
| 138 |
|
| 139 |
$dbo = JFactory::getDbo(); |
| 140 |
|
| 141 |
$q = "SELECT * FROM `#__vikbooking_reminders` WHERE `id`=" . (int)$rid; |
| 142 |
$dbo->setQuery($q, 0, 1); |
| 143 |
$reminder = $dbo->loadObject(); |
| 144 |
if (!$reminder) { |
| 145 |
return null; |
| 146 |
} |
| 147 |
|
| 148 |
if (!empty($reminder->payload)) { |
| 149 |
$reminder->payload = json_decode($reminder->payload); |
| 150 |
} |
| 151 |
|
| 152 |
return $reminder; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Inserts a new reminder record object. |
| 157 |
* |
| 158 |
* @param object $reminder the record object to insert. |
| 159 |
* |
| 160 |
* @return bool |
| 161 |
*/ |
| 162 |
public function saveReminder($reminder) |
| 163 |
{ |
| 164 |
if (!is_object($reminder) || !count(get_object_vars($reminder))) { |
| 165 |
$this->setError('Empty or invalid argument'); |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
if (!empty($reminder->payload) && !is_scalar($reminder->payload)) { |
| 170 |
// make sure to JSON encode the payload property |
| 171 |
$reminder->payload = json_encode($reminder->payload); |
| 172 |
} |
| 173 |
|
| 174 |
$dbo = JFactory::getDbo(); |
| 175 |
|
| 176 |
try { |
| 177 |
$dbo->insertObject('#__vikbooking_reminders', $reminder, 'id'); |
| 178 |
} catch (Exception $e) { |
| 179 |
// do nothing |
| 180 |
$this->setError('The query to insert the record failed'); |
| 181 |
} |
| 182 |
|
| 183 |
return (!empty($reminder->id)); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Inserts a new reminder record object. |
| 188 |
* |
| 189 |
* @param object $reminder the record object to insert. |
| 190 |
* |
| 191 |
* @return bool |
| 192 |
*/ |
| 193 |
public function updateReminder($reminder) |
| 194 |
{ |
| 195 |
if (!is_object($reminder) || !count(get_object_vars($reminder))) { |
| 196 |
$this->setError('Empty or invalid argument'); |
| 197 |
return false; |
| 198 |
} |
| 199 |
|
| 200 |
if (empty($reminder->id)) { |
| 201 |
$this->setError('Empty reminder id'); |
| 202 |
return false; |
| 203 |
} |
| 204 |
|
| 205 |
$dbo = JFactory::getDbo(); |
| 206 |
|
| 207 |
$res = false; |
| 208 |
|
| 209 |
try { |
| 210 |
$res = $dbo->updateObject('#__vikbooking_reminders', $reminder, 'id'); |
| 211 |
} catch (Exception $e) { |
| 212 |
// do nothing |
| 213 |
$this->setError('The query to insert the record failed'); |
| 214 |
} |
| 215 |
|
| 216 |
return $res; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Deletes an existing reminder record. |
| 221 |
* |
| 222 |
* @param int|object $reminder the record to remove. |
| 223 |
* |
| 224 |
* @return bool |
| 225 |
*/ |
| 226 |
public function deleteReminder($reminder) |
| 227 |
{ |
| 228 |
if (!is_numeric($reminder) && !is_object($reminder)) { |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
$reminder_id = null; |
| 233 |
|
| 234 |
if (is_object($reminder) && !empty($reminder->id)) { |
| 235 |
$reminder_id = (int)$reminder->id; |
| 236 |
} elseif (is_numeric($reminder)) { |
| 237 |
$reminder_id = (int)$reminder; |
| 238 |
} |
| 239 |
|
| 240 |
if (empty($reminder_id)) { |
| 241 |
return false; |
| 242 |
} |
| 243 |
|
| 244 |
$dbo = JFactory::getDbo(); |
| 245 |
|
| 246 |
$q = "DELETE FROM `#__vikbooking_reminders` WHERE `id`=" . $reminder_id; |
| 247 |
$dbo->setQuery($q); |
| 248 |
$dbo->execute(); |
| 249 |
|
| 250 |
return ($dbo->getAffectedRows() > 0); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Removes the reminders with a due date in the past. |
| 255 |
* |
| 256 |
* @return void |
| 257 |
*/ |
| 258 |
public function removeExpired() |
| 259 |
{ |
| 260 |
$dbo = JFactory::getDbo(); |
| 261 |
|
| 262 |
$q = "DELETE FROM `#__vikbooking_reminders` WHERE `duedate` < NOW();"; |
| 263 |
$dbo->setQuery($q); |
| 264 |
$dbo->execute(); |
| 265 |
|
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Given two dates, compares the relative differences and returns the information. |
| 271 |
* The language definitions are supposed to be loaded from the admin section. |
| 272 |
* |
| 273 |
* @param string|DateTime $date_a the date to compare from. |
| 274 |
* @param string|DateTime $date_b the date to compare against. |
| 275 |
* |
| 276 |
* @return array false on failure, array with diff otherwise. |
| 277 |
*/ |
| 278 |
public function relativeDatesDiff($date_a, $date_b = null) |
| 279 |
{ |
| 280 |
if (is_string($date_a)) { |
| 281 |
$date_a = new DateTime($date_a); |
| 282 |
} |
| 283 |
|
| 284 |
if (!($date_a instanceof DateTime)) { |
| 285 |
$date_a = new DateTime(); |
| 286 |
} |
| 287 |
|
| 288 |
if (is_string($date_b) || empty($date_b)) { |
| 289 |
// by default we compare against now |
| 290 |
if (empty($date_b)) { |
| 291 |
$date_b = new DateTime(); |
| 292 |
} else { |
| 293 |
$date_b = new DateTime($date_b); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
if (!($date_b instanceof DateTime)) { |
| 298 |
$date_b = new DateTime(); |
| 299 |
} |
| 300 |
|
| 301 |
// calculate close y-m-d dates |
| 302 |
$fromd_ymd = $date_a->format('Y-m-d'); |
| 303 |
$today = date('Y-m-d'); |
| 304 |
$yesterday = date('Y-m-d', strtotime('-1 day')); |
| 305 |
$tomorrow = date('Y-m-d', strtotime('+1 day')); |
| 306 |
|
| 307 |
// get the date interval object of differences |
| 308 |
$dt_interval = $date_a->diff($date_b); |
| 309 |
|
| 310 |
// compose the associative data to be returned |
| 311 |
$diff_data = [ |
| 312 |
'past' => ($date_a < $date_b), |
| 313 |
'sameday' => ($fromd_ymd == $date_b->format('Y-m-d')), |
| 314 |
'today' => ($fromd_ymd == $today), |
| 315 |
'yesterday' => ($fromd_ymd == $yesterday), |
| 316 |
'tomorrow' => ($fromd_ymd == $tomorrow), |
| 317 |
'seconds' => $dt_interval->s, |
| 318 |
'minutes' => $dt_interval->i, |
| 319 |
'hours' => $dt_interval->h, |
| 320 |
'days' => $dt_interval->d, |
| 321 |
/** |
| 322 |
* Rely on weeks only if less than a month for better precision. |
| 323 |
* Weeks is the only value to not be calculated natively in DateInterval. |
| 324 |
*/ |
| 325 |
'weeks' => ($dt_interval->m > 0 ? 0 : floor($dt_interval->d / 7)), |
| 326 |
// |
| 327 |
'months' => $dt_interval->m, |
| 328 |
'years' => $dt_interval->y, |
| 329 |
// set the DateTime objects parsed |
| 330 |
'date_a' => $date_a, |
| 331 |
'date_b' => $date_b, |
| 332 |
// prepare the formatted relative difference string |
| 333 |
'relative' => $fromd_ymd, |
| 334 |
]; |
| 335 |
|
| 336 |
// build the relative difference string |
| 337 |
if ($diff_data['today']) { |
| 338 |
$diff_data['relative'] = JText::translate('VBTODAY'); |
| 339 |
} elseif ($diff_data['yesterday']) { |
| 340 |
$diff_data['relative'] = JText::translate('VBOYESTERDAY'); |
| 341 |
} elseif ($diff_data['tomorrow']) { |
| 342 |
$diff_data['relative'] = JText::translate('VBOTOMORROW'); |
| 343 |
} elseif ($diff_data['years'] > 0) { |
| 344 |
// no translations available at the moment for the singular version of "year" |
| 345 |
$diff_num = $diff_data['years'] . ' ' . JText::translate('VBCONFIGSEARCHPMAXDATEYEARS'); |
| 346 |
$diff_data['relative'] = JText::sprintf(($diff_data['past'] ? 'VBO_REL_EXP_PAST' : 'VBO_REL_EXP_FUTURE'), strtolower($diff_num)); |
| 347 |
} elseif ($diff_data['months'] > 0) { |
| 348 |
$diff_num = $diff_data['months'] . ' ' . JText::translate(($diff_data['months'] > 1 ? 'VBCONFIGSEARCHPMAXDATEMONTHS' : 'VBPVIEWRESTRICTIONSTWO')); |
| 349 |
$diff_data['relative'] = JText::sprintf(($diff_data['past'] ? 'VBO_REL_EXP_PAST' : 'VBO_REL_EXP_FUTURE'), strtolower($diff_num)); |
| 350 |
} elseif ($diff_data['weeks'] > 1) { |
| 351 |
// use weeks only if more than one for a better precision |
| 352 |
$diff_num = $diff_data['weeks'] . ' ' . JText::translate(($diff_data['weeks'] > 1 ? 'VBCONFIGSEARCHPMAXDATEWEEKS' : 'VBOWEEK')); |
| 353 |
$diff_data['relative'] = JText::sprintf(($diff_data['past'] ? 'VBO_REL_EXP_PAST' : 'VBO_REL_EXP_FUTURE'), strtolower($diff_num)); |
| 354 |
} elseif ($diff_data['days'] > 0) { |
| 355 |
$diff_num = $diff_data['days'] . ' ' . JText::translate(($diff_data['days'] > 1 ? 'VBCONFIGSEARCHPMAXDATEDAYS' : 'VBODAY')); |
| 356 |
$diff_data['relative'] = JText::sprintf(($diff_data['past'] ? 'VBO_REL_EXP_PAST' : 'VBO_REL_EXP_FUTURE'), strtolower($diff_num)); |
| 357 |
} |
| 358 |
|
| 359 |
return $diff_data; |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* Tells whether a specific booking ID has got a reminder assigned. |
| 364 |
* |
| 365 |
* @param int $booking_id the reservation ID to check. |
| 366 |
* |
| 367 |
* @return bool |
| 368 |
* |
| 369 |
* @since 1.16.3 (J) - 1.6.3 (WP) |
| 370 |
*/ |
| 371 |
public function bookingHasReminder($booking_id) |
| 372 |
{ |
| 373 |
$dbo = JFactory::getDbo(); |
| 374 |
|
| 375 |
$q = $dbo->getQuery(true); |
| 376 |
|
| 377 |
$q->select('COUNT(1)') |
| 378 |
->from($dbo->qn('#__vikbooking_reminders')) |
| 379 |
->where($dbo->qn('idorder') . ' = ' . (int)$booking_id); |
| 380 |
|
| 381 |
$dbo->setQuery($q); |
| 382 |
|
| 383 |
return (bool)$dbo->loadResult(); |
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
* Gathers a list of Airbnb reservations that may require a host-to-guest review. |
| 388 |
* |
| 389 |
* @param int $lim_start_ts the checkout timestamp to use as limit. |
| 390 |
* |
| 391 |
* @return array |
| 392 |
* |
| 393 |
* @since 1.16.3 (J) - 1.6.3 (WP) |
| 394 |
*/ |
| 395 |
public function gatherAirbnbReservationsCheckedOut($lim_start_ts = 0) |
| 396 |
{ |
| 397 |
if (!$lim_start_ts) { |
| 398 |
// default to checkout two weeks ago |
| 399 |
$lim_start_ts = strtotime("-14 days", strtotime(date('Y-m-d'))); |
| 400 |
} |
| 401 |
|
| 402 |
// maximum checkout date must be 14 days ahead from checkout |
| 403 |
$lim_end_ts = strtotime("+14 days", $lim_start_ts); |
| 404 |
|
| 405 |
$dbo = JFactory::getDbo(); |
| 406 |
|
| 407 |
$q = $dbo->getQuery(true) |
| 408 |
->select($dbo->qn([ |
| 409 |
'o.id', |
| 410 |
'o.status', |
| 411 |
'o.checkin', |
| 412 |
'o.checkout', |
| 413 |
'o.idorderota', |
| 414 |
'o.channel', |
| 415 |
])) |
| 416 |
->from($dbo->qn('#__vikbooking_orders', 'o')) |
| 417 |
->where($dbo->qn('o.status') . ' = ' . $dbo->q('confirmed')) |
| 418 |
->where($dbo->qn('o.checkout') . ' >= ' . $lim_start_ts) |
| 419 |
->where($dbo->qn('o.checkout') . ' <= ' . $lim_end_ts) |
| 420 |
->where($dbo->qn('o.channel') . ' LIKE ' . $dbo->q('airbnbapi%')); |
| 421 |
|
| 422 |
$dbo->setQuery($q); |
| 423 |
|
| 424 |
return $dbo->loadAssocList(); |
| 425 |
} |
| 426 |
} |
| 427 |
|