| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2025 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 for hot events. |
| 16 |
* |
| 17 |
* @since 1.18.6 (J) - 1.8.6 (WP) |
| 18 |
*/ |
| 19 |
final class VBODateHotevents |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Loads the festivities for the given range of date timestamps. |
| 23 |
* |
| 24 |
* @param int $tsFrom Date range start timestamp. |
| 25 |
* @param int $tsTo Date range end timestamp. |
| 26 |
* |
| 27 |
* @return array |
| 28 |
*/ |
| 29 |
public static function loadPeriod(int $tsFrom, int $tsTo) |
| 30 |
{ |
| 31 |
$dbo = JFactory::getDbo(); |
| 32 |
|
| 33 |
// build the hot events list |
| 34 |
$hotEvents = []; |
| 35 |
|
| 36 |
// load festivities |
| 37 |
$q = $dbo->getQuery(true) |
| 38 |
->select('*') |
| 39 |
->from($dbo->qn('#__vikbooking_fests_dates')) |
| 40 |
->where($dbo->qn('dt') . ' >= ' . $dbo->q(date('Y-m-d', $tsFrom))) |
| 41 |
->where($dbo->qn('dt') . ' <= ' . $dbo->q(date('Y-m-d', $tsTo))) |
| 42 |
->order($dbo->qn('dt') . ' ASC'); |
| 43 |
|
| 44 |
$dbo->setQuery($q); |
| 45 |
foreach ($dbo->loadAssocList() as $festRecord) { |
| 46 |
$festData = (array) json_decode($festRecord['festinfo'], true); |
| 47 |
if (!$festData) { |
| 48 |
continue; |
| 49 |
} |
| 50 |
|
| 51 |
// prepare events for the current date |
| 52 |
$hotEvents[$festRecord['dt']] = $hotEvents[$festRecord['dt']] ?? []; |
| 53 |
|
| 54 |
foreach ($festData as $fest) { |
| 55 |
$regions = (array) (($fest['regions'] ?? []) ?: ['global']); |
| 56 |
$festName = ($fest['trans_name'] ?? '') ?: ($fest['descr'] ?? ''); |
| 57 |
if (empty($festName)) { |
| 58 |
// malformed festivity |
| 59 |
continue; |
| 60 |
} |
| 61 |
|
| 62 |
// push festivity details |
| 63 |
$hotEvents[$festRecord['dt']][] = [ |
| 64 |
'name' => $festName, |
| 65 |
'regions' => $regions, |
| 66 |
]; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// sort list |
| 71 |
foreach ($hotEvents as &$dayEvents) { |
| 72 |
// sort global events first |
| 73 |
usort($dayEvents, function($a, $b) { |
| 74 |
if (in_array('global', $a['regions']) || in_array('global', $b['regions'])) { |
| 75 |
return 1; |
| 76 |
} |
| 77 |
|
| 78 |
return 0; |
| 79 |
}); |
| 80 |
} |
| 81 |
|
| 82 |
unset($dayEvents); |
| 83 |
|
| 84 |
// return the list of hot events |
| 85 |
return $hotEvents; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Given a period date and interval type, matches the belonging events. |
| 90 |
* |
| 91 |
* @param DateTimeInterface $period The data period under evaluation. |
| 92 |
* @param string $intervalType The data evaluation interval type. |
| 93 |
* @param array $hotEvents Associative list of hot events. |
| 94 |
* |
| 95 |
* @return ?array |
| 96 |
*/ |
| 97 |
public static function matchPeriodEvents(DateTimeInterface $period, string $intervalType, array $hotEvents) |
| 98 |
{ |
| 99 |
if (!$hotEvents) { |
| 100 |
// nothing to match |
| 101 |
return null; |
| 102 |
} |
| 103 |
|
| 104 |
// build the list of events matching the period |
| 105 |
$periodEvents = []; |
| 106 |
|
| 107 |
// get the timestamps at midnight for the evaluation date |
| 108 |
$dt = clone $period; |
| 109 |
$dt->modify('00:00:00'); |
| 110 |
$tsFrom = $dt->format('U'); |
| 111 |
$tsTo = $tsFrom; |
| 112 |
|
| 113 |
if ($intervalType === 'MONTH') { |
| 114 |
$tsTo = strtotime(date('Y-m-t', $tsFrom)); |
| 115 |
} |
| 116 |
|
| 117 |
// iterate all hot events |
| 118 |
foreach ($hotEvents as $day => $events) { |
| 119 |
$dayTs = strtotime($day); |
| 120 |
if ($tsFrom <= $dayTs && $tsTo >= $dayTs) { |
| 121 |
// merge events matching the period |
| 122 |
$periodEvents = array_merge($periodEvents, $events); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return $periodEvents ?: null; |
| 127 |
} |
| 128 |
} |
| 129 |
|