| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage com_vikbooking |
| 5 |
* @author Alessio Gaggii - e4j - Extensionsforjoomla.com |
| 6 |
* @copyright Copyright (C) 2018 e4j - Extensionsforjoomla.com. All rights reserved. |
| 7 |
* @license GNU General Public License version 2 or later; see LICENSE |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 12 |
|
| 13 |
jimport('joomla.application.component.view'); |
| 14 |
|
| 15 |
class VikbookingViewPromotions extends JViewVikBooking { |
| 16 |
function display($tpl = null) { |
| 17 |
VikBooking::prepareViewContent(); |
| 18 |
$dbo = JFactory::getDBO(); |
| 19 |
$vbo_tn = VikBooking::getTranslator(); |
| 20 |
$pshowrooms = VikRequest::getInt('showrooms', 1, 'request'); |
| 21 |
$pshowrooms = $pshowrooms == 1 ? 1 : 0; |
| 22 |
$pmaxdate = VikRequest::getInt('maxdate', 6, 'request'); |
| 23 |
$plim = VikRequest::getInt('lim', 10, 'request'); |
| 24 |
$promotions = array(); |
| 25 |
$rooms = array(); |
| 26 |
$ind = 0; |
| 27 |
$q = "SELECT * FROM `#__vikbooking_seasons` WHERE `promo`=1;"; |
| 28 |
$dbo->setQuery($q); |
| 29 |
$dbo->execute(); |
| 30 |
if ($dbo->getNumRows() > 0) { |
| 31 |
$all_promotions = $dbo->loadAssocList(); |
| 32 |
$vbo_tn->translateContents($all_promotions, '#__vikbooking_seasons'); |
| 33 |
$base_year = (int)date('Y'); |
| 34 |
$base_ts = time(); |
| 35 |
$base_month = (int)date('n'); |
| 36 |
foreach ($all_promotions as $k => $promo) { |
| 37 |
$promo_year = !empty($promo['year']) && $promo['year'] > 0 ? $promo['year'] : $base_year; |
| 38 |
$promo_from_ts = ((int)mktime(0, 0, 0, 1, 1, $promo_year)) + $promo['from']; |
| 39 |
$promo_to_ts = ((int)mktime(0, 0, 0, 1, 1, $promo_year)) + $promo['to']; |
| 40 |
if ($promo_year % 4 == 0 && ($promo_year % 100 != 0 || $promo_year % 400 == 0)) { |
| 41 |
//leap year |
| 42 |
$leapts = mktime(0, 0, 0, 2, 29, $promo_year); |
| 43 |
if ($promo_from_ts >= $leapts) { |
| 44 |
$promo_from_ts += 86400; |
| 45 |
} |
| 46 |
} |
| 47 |
if ($base_ts > $promo_from_ts && $base_ts > $promo_to_ts) { |
| 48 |
if (empty($promo['year'])) { |
| 49 |
$promo_year++; |
| 50 |
$promo_from_ts = ((int)mktime(0, 0, 0, 1, 1, $promo_year)) + $promo['from']; |
| 51 |
if ($promo_year % 4 == 0 && ($promo_year % 100 != 0 || $promo_year % 400 == 0)) { |
| 52 |
//leap year |
| 53 |
$leapts = mktime(0, 0, 0, 2, 29, $promo_year); |
| 54 |
if ($promo_from_ts >= $leapts) { |
| 55 |
$promo_from_ts += 86400; |
| 56 |
} |
| 57 |
} |
| 58 |
} else { |
| 59 |
//Start ts is in the past, not tied to the year, check if season end ts is in the future |
| 60 |
$check_promo_to_ts = ((int)mktime(0, 0, 0, 1, 1, ($promo['from'] > $promo['to'] ? ($promo_year + 1) : $promo_year))) + $promo['to']; |
| 61 |
if ($base_ts > $check_promo_to_ts) { |
| 62 |
continue; |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
if ($promo['from'] > $promo['to']) { |
| 67 |
$promo_year++; |
| 68 |
} |
| 69 |
$promo_to_ts = ((int)mktime(0, 0, 0, 1, 1, $promo_year)) + $promo['to']; |
| 70 |
if ($promo_year % 4 == 0 && ($promo_year % 100 != 0 || $promo_year % 400 == 0)) { |
| 71 |
//leap year |
| 72 |
$leapts = mktime(0, 0, 0, 2, 29, $promo_year); |
| 73 |
if ($promo_to_ts >= $leapts) { |
| 74 |
$promo_to_ts += 86400; |
| 75 |
} |
| 76 |
} |
| 77 |
if ($promo_from_ts < $promo_to_ts) { |
| 78 |
//Begin: Check Max Date in the Future (Months) |
| 79 |
$promo_from_month = (int)date('n', $promo_from_ts); |
| 80 |
$promo_from_year = (int)date('Y', $promo_from_ts); |
| 81 |
if ($base_year == $promo_from_year) { |
| 82 |
//Same Year |
| 83 |
$months_diff = $promo_from_month - $base_month; |
| 84 |
if ($months_diff > $pmaxdate) { |
| 85 |
continue; |
| 86 |
} |
| 87 |
} else { |
| 88 |
//Different Year |
| 89 |
$promo_from_month += 12 * ($promo_from_year - $base_year); |
| 90 |
$months_diff = $promo_from_month - $base_month; |
| 91 |
if ($months_diff > $pmaxdate) { |
| 92 |
continue; |
| 93 |
} |
| 94 |
} |
| 95 |
//End: Check Max Date in the Future (Months) |
| 96 |
$promotions[$ind] = $all_promotions[$k]; |
| 97 |
$promotions[$ind]['promo_from_ts'] = $promo_from_ts; |
| 98 |
$promotions[$ind]['promo_to_ts'] = $promo_to_ts; |
| 99 |
$promotions[$ind]['promo_valid_ts'] = $promo_from_ts; |
| 100 |
//set valid until to end ts in case of 0 days in advance |
| 101 |
if (empty($promo['promodaysadv']) || !($promo['promodaysadv'] > 0)) { |
| 102 |
$promotions[$ind]['promo_valid_ts'] = $promo_to_ts; |
| 103 |
} elseif (!empty($promo['promodaysadv']) && $promo['promodaysadv'] > 0) { |
| 104 |
$dst_from_ts = date('I', $promo_from_ts); |
| 105 |
$valid_ts = $promo_from_ts - (86400 * $promo['promodaysadv']); |
| 106 |
$dst_valid_ts = date('I', $valid_ts); |
| 107 |
if ($dst_from_ts != $dst_valid_ts) { |
| 108 |
if ($dst_valid_ts) { |
| 109 |
$valid_ts -= 3600; |
| 110 |
} else { |
| 111 |
$valid_ts += 3600; |
| 112 |
} |
| 113 |
} |
| 114 |
$promotions[$ind]['promo_valid_ts'] = $valid_ts; |
| 115 |
} |
| 116 |
$ind++; |
| 117 |
} |
| 118 |
} |
| 119 |
if (count($promotions) > 0) { |
| 120 |
$promo_map = array(); |
| 121 |
$sorted = array(); |
| 122 |
$promos_rooms = array(); |
| 123 |
foreach ($promotions as $k => $v) { |
| 124 |
$promo_map[$k] = $v['promo_from_ts']; |
| 125 |
$allrooms = explode(",", $v['idrooms']); |
| 126 |
foreach ($allrooms as $idroom) { |
| 127 |
$idroom = intval(str_replace("-", "", trim($idroom))); |
| 128 |
if ($idroom > 0) { |
| 129 |
$promos_rooms[$idroom] = $idroom; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
asort($promo_map); |
| 134 |
foreach ($promo_map as $k => $v) { |
| 135 |
$sorted[$k] = $promotions[$k]; |
| 136 |
} |
| 137 |
$promotions = $sorted; |
| 138 |
if (count($promos_rooms) > 0) { |
| 139 |
$q = "SELECT * FROM `#__vikbooking_rooms` WHERE `id` IN(".implode(",", $promos_rooms).") ORDER BY `#__vikbooking_rooms`.`name` ASC;"; |
| 140 |
$dbo->setQuery($q); |
| 141 |
$dbo->execute(); |
| 142 |
if ($dbo->getNumRows() > 0) { |
| 143 |
$fetch_rooms = $dbo->loadAssocList(); |
| 144 |
$vbo_tn->translateContents($fetch_rooms, '#__vikbooking_rooms'); |
| 145 |
foreach ($fetch_rooms as $v) { |
| 146 |
$rooms[$v['id']] = $v; |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
$this->promotions = $promotions; |
| 154 |
$this->rooms = $rooms; |
| 155 |
$this->showrooms = $pshowrooms; |
| 156 |
$this->vbo_tn = $vbo_tn; |
| 157 |
//theme |
| 158 |
$theme = VikBooking::getTheme(); |
| 159 |
if ($theme != 'default') { |
| 160 |
$thdir = VBO_SITE_PATH.DS.'themes'.DS.$theme.DS.'promotions'; |
| 161 |
if (is_dir($thdir)) { |
| 162 |
$this->_setPath('template', $thdir.DS); |
| 163 |
} |
| 164 |
} |
| 165 |
// |
| 166 |
parent :: display($tpl); |
| 167 |
} |
| 168 |
} |
| 169 |
|