PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.7.1
VikBooking Hotel Booking Engine & PMS v1.7.1
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
vikbooking / admin / helpers / critical_dates.php

critical_dates.php in VikBooking Hotel Booking Engine & PMS 1.7.1, at admin/helpers/critical_dates.php

257 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikBooking
4 * @subpackage com_vikbooking
5 * @author Alessio Gaggii - e4j - Extensionsforjoomla.com
6 * @copyright Copyright (C) 2020 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 /**
14 * Critical Dates handler class for Vik Booking.
15 *
16 * @since 1.13.5
17 */
18 class VikBookingCriticalDates
19 {
20 /**
21 * The singleton instance of the class.
22 *
23 * @var VikBookingCriticalDates
24 */
25 protected static $instance = null;
26
27 /**
28 * Class constructor is protected.
29 *
30 * @see getInstance()
31 */
32 protected function __construct()
33 {
34
35 }
36
37 /**
38 * Returns the global Critical Dates object, either
39 * a new instance or the existing instance
40 * if the class was already instantiated.
41 *
42 * @return self a new instance of the class.
43 */
44 public static function getInstance()
45 {
46 if (is_null(static::$instance)) {
47 static::$instance = new static();
48 }
49
50 return static::$instance;
51 }
52
53 /**
54 * Checks whether a precise note exists on the given date.
55 *
56 * @param string $ymd the date string in Y-m-d format.
57 * @param int $idroom the ID of the room involved.
58 * @param int $subunit the index of the sub-unit (0 by default).
59 * @param string $key the key name of the note.
60 * @param boolean $get whether to get the found record.
61 *
62 * @return mixed True/array if festivity exists, false otherwise.
63 */
64 public function dayNoteExists($ymd, $idroom, $subunit = 0, $key = '', $get = false)
65 {
66 // vars validation
67 $idroom = intval($idroom);
68 $subunit = intval($subunit);
69
70 $dbo = JFactory::getDbo();
71 $q = "SELECT * FROM `#__vikbooking_critical_dates` WHERE `dt`=" . $dbo->quote($ymd) . " AND `idroom`={$idroom} AND `subunit`={$subunit};";
72 $dbo->setQuery($q);
73 $dbo->execute();
74 if (!$dbo->getNumRows()) {
75 return false;
76 }
77 $note = $dbo->loadAssoc();
78
79 $notes_info = json_decode($note['info']);
80 if (!$notes_info) {
81 return false;
82 }
83
84 // update this information in the array in case it needs to be returned
85 $note['info'] = $notes_info;
86
87 // seek for the requested note
88 foreach ($notes_info as $n) {
89 if (empty($key) || (!empty($n->type) && $n->type == $key)) {
90 return $get ? $note : true;
91 }
92 }
93 return false;
94 }
95
96 /**
97 * Stores/updates a note for the given date, idroom and subunit. If a record for the given data exists, then the
98 * note is added to or updated in the current record. Otherwise a new record is created.
99 *
100 * @param array $note the array note to store.
101 * @param string $ymd the date string in Y-m-d format.
102 * @param int $idroom the ID of the room involved.
103 * @param int $subunit the index of the sub-unit (0 by default).
104 *
105 * @return boolean True if the new record is stored or updated, false otherwise.
106 */
107 public function storeDayNote($note, $ymd, $idroom = 0, $subunit = 0)
108 {
109 // vars validation
110 $idroom = intval($idroom);
111 $subunit = intval($subunit);
112 if (!is_array($note) || !count($note) || (empty($note['name']) && empty($note['descr']))) {
113 return false;
114 }
115 if (!isset($note['type'])) {
116 $note['type'] = 'custom';
117 }
118 if (!isset($note['ts'])) {
119 $note['ts'] = time();
120 }
121 if (!empty($note['name'])) {
122 // make sure the "name" does not contain commas, which may be used by JS as separator in the readable notes data attribute
123 $note['name'] = trim(str_replace(',', '', $note['name']));
124 }
125
126 $dbo = JFactory::getDbo();
127 $q = "SELECT * FROM `#__vikbooking_critical_dates` WHERE `dt`=" .$dbo->quote($ymd) . " AND `idroom`={$idroom} AND `subunit`={$subunit}";
128 $dbo->setQuery($q);
129 $dbo->execute();
130 if (!$dbo->getNumRows()) {
131 // create a new record
132 $q = "INSERT INTO `#__vikbooking_critical_dates` (`dt`, `idroom`, `subunit`, `info`) VALUES (".$dbo->quote($ymd).", ".$dbo->quote($idroom).", ".$dbo->quote($subunit).", ".$dbo->quote(json_encode(array($note))).");";
133 $dbo->setQuery($q);
134 $dbo->execute();
135
136 return ((int)$dbo->insertid() > 0);
137 }
138
139 // update current record by pushing the note into the existing info array of objects
140 $notes = $dbo->loadAssoc();
141 $notes_arr = json_decode($notes['info']);
142 array_push($notes_arr, (object)$note);
143 $q = "UPDATE `#__vikbooking_critical_dates` SET `info`=".$dbo->quote(json_encode($notes_arr))." WHERE `id`={$notes['id']};";
144 $dbo->setQuery($q);
145 $dbo->execute();
146
147 return ((int)$dbo->getAffectedRows() > 0);
148 }
149
150 /**
151 * Deletes a specific note for the given date, room ID and subunit.
152 * If the day will not contain anymore notes, then
153 * the whole record will be deleted.
154 *
155 * @param int $index the array index of the note.
156 * @param string $ymd the date string in Y-m-d format.
157 * @param int $idroom the room ID involved.
158 * @param int $subunit the subunit of the room ID.
159 * @param string $type the key name of the note ('custom' for manual entries).
160 *
161 * @return boolean True if the fest is removed, false otherwise.
162 */
163 public function deleteDayNote($index, $ymd, $idroom = 0, $subunit = 0, $type = 'custom')
164 {
165 $record = $this->dayNoteExists($ymd, $idroom, $subunit, $type, true);
166 if (!$record) {
167 return false;
168 }
169
170 $drop_record = false;
171 if (isset($record['info'][$index])) {
172 // note was found by array-index
173 array_splice($record['info'], $index, 1);
174 if (!count($record['info'])) {
175 // this day has got no more notes
176 $drop_record = true;
177 }
178 } else {
179 // seek for the requested note by type
180 foreach ($record['info'] as $k => $note) {
181 if (empty($type) || (!empty($note->type) && $note->type == $type)) {
182 // note found
183 array_splice($record['info'], $k, 1);
184 if (!count($record['info'])) {
185 // this day has got no more notes
186 $drop_record = true;
187 }
188 break;
189 }
190 }
191 }
192
193 $dbo = JFactory::getDbo();
194 if ($drop_record) {
195 $q = "DELETE FROM `#__vikbooking_critical_dates` WHERE `dt`=" . $dbo->quote($ymd) . " AND `idroom`={$idroom} AND `subunit`={$subunit}";
196 } else {
197 $q = "UPDATE `#__vikbooking_critical_dates` SET `info`=".$dbo->quote(json_encode($record['info']))." WHERE `dt`=" . $dbo->quote($ymd) . " AND `idroom`={$idroom} AND `subunit`={$subunit}";
198 }
199 $dbo->setQuery($q);
200 $dbo->execute();
201
202 return true;
203 }
204
205 /**
206 * Loads the room day notes of any type and returns and associative
207 * array indexed by date with decoded notes information.
208 *
209 * @param string $from_ymd the optional minimum date in Y-m-d (defaults to today).
210 * @param string $to_ymd the optional maximum date in Y-m-d (defaults to none).
211 * @param int $idroom the optional room id filter
212 * @param int $subunit the optional room index filter
213 *
214 * @return array the list of festivities found in Vik Booking.
215 */
216 public function loadRoomDayNotes($from_ymd = null, $to_ymd = null, $idroom = null, $subunit = null)
217 {
218 $dbo = JFactory::getDbo();
219
220 $rdnotes = array();
221 if (empty($from_ymd)) {
222 $from_ymd = date('Y-m-d');
223 }
224
225 $clauses = array();
226 array_push($clauses, "`dt`>=" . $dbo->quote($from_ymd));
227
228 if (!empty($to_ymd)) {
229 array_push($clauses, "`dt`<=" . $dbo->quote($to_ymd));
230 }
231 if ($idroom !== null && is_int($idroom)) {
232 array_push($clauses, "`idroom`=" . (int)$idroom);
233 }
234 if ($subunit !== null && is_int($subunit)) {
235 array_push($clauses, "`subunit`=" . (int)$subunit);
236 }
237
238 $q = "SELECT * FROM `#__vikbooking_critical_dates` WHERE " . implode(' AND ', $clauses) . " ORDER BY `dt` ASC;";
239 $dbo->setQuery($q);
240 $dbo->execute();
241 if ($dbo->getNumRows()) {
242 $all_notes = $dbo->loadAssocList();
243 // make sure to decode all notes infos
244 foreach ($all_notes as $k => $v) {
245 $v['info'] = json_decode($v['info']);
246
247 $keyid = $v['dt'] . '_' . (int)$v['idroom'] . '_' . (int)$v['subunit'];
248
249 $rdnotes[$keyid] = $v;
250 }
251 }
252
253 return $rdnotes;
254 }
255
256 }
257