| 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 |
/** |
| 14 |
* Helper class to invoke the Channel Manager update requests. |
| 15 |
*/ |
| 16 |
class VboVcmInvoker |
| 17 |
{ |
| 18 |
/** |
| 19 |
* @var array |
| 20 |
*/ |
| 21 |
public $oids = []; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
public $orig_statuses = []; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
public $sync_type = 'new'; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var array |
| 35 |
*/ |
| 36 |
public $orig_booking; |
| 37 |
|
| 38 |
/** |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
private $error = ''; |
| 42 |
|
| 43 |
/** |
| 44 |
* @var bool |
| 45 |
*/ |
| 46 |
private $result = false; |
| 47 |
|
| 48 |
/** |
| 49 |
* Class constructor will attempt to require a class from VCM. |
| 50 |
*/ |
| 51 |
public function __construct() |
| 52 |
{ |
| 53 |
if (!class_exists('synchVikBooking')) { |
| 54 |
require_once(VCM_SITE_PATH . DIRECTORY_SEPARATOR . "helpers" . DIRECTORY_SEPARATOR . "synch.vikbooking.php"); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Sets the booking IDs for the sync |
| 60 |
* |
| 61 |
* @param array $oids |
| 62 |
* |
| 63 |
* @return self |
| 64 |
*/ |
| 65 |
public function setOids($oids) |
| 66 |
{ |
| 67 |
$this->oids = $oids; |
| 68 |
|
| 69 |
return $this; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Sets the type of synchronization for VCM |
| 74 |
* |
| 75 |
* @param string $set_sync_type |
| 76 |
* |
| 77 |
* @return self |
| 78 |
*/ |
| 79 |
public function setSyncType($set_sync_type) |
| 80 |
{ |
| 81 |
$this->sync_type = !in_array($set_sync_type, array('new', 'modify', 'cancel')) ? 'new' : $set_sync_type; |
| 82 |
|
| 83 |
return $this; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Method needed to inject the previous/original status of each booking |
| 88 |
* before making any sort of update. Useful to inform VCM that the booking was |
| 89 |
* pending (stand-by) to support Request to Book reservations with some channels. |
| 90 |
* |
| 91 |
* @param array $orig_statuses array of original booking statuses. |
| 92 |
* |
| 93 |
* @return self |
| 94 |
* |
| 95 |
* @since 1.14 (J) - 1.4.0 (WP) |
| 96 |
* @requires VCM >= 1.8.0 |
| 97 |
*/ |
| 98 |
public function setOriginalStatuses($orig_statuses) |
| 99 |
{ |
| 100 |
if (empty($orig_statuses)) { |
| 101 |
return $this; |
| 102 |
} |
| 103 |
|
| 104 |
if (is_scalar($orig_statuses)) { |
| 105 |
$orig_statuses = [$orig_statuses]; |
| 106 |
} |
| 107 |
|
| 108 |
if (!is_array($orig_statuses)) { |
| 109 |
return $this; |
| 110 |
} |
| 111 |
|
| 112 |
$this->orig_statuses = $orig_statuses; |
| 113 |
|
| 114 |
return $this; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Sets the original booking array |
| 119 |
* |
| 120 |
* @param mixed $obooking JSON+URL encoded string or array |
| 121 |
* @param boolean $decode |
| 122 |
*/ |
| 123 |
public function setOriginalBooking($obooking, $decode = false) |
| 124 |
{ |
| 125 |
if (!empty($obooking)) { |
| 126 |
$original_booking = $decode === true ? json_decode(urldecode($obooking), true) : $obooking; |
| 127 |
if (is_array($original_booking) && $original_booking) { |
| 128 |
$this->orig_booking = $original_booking; |
| 129 |
} |
| 130 |
} |
| 131 |
return $this; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Launch the synchronization with VCM |
| 136 |
*/ |
| 137 |
public function doSync() |
| 138 |
{ |
| 139 |
if (!is_array($this->oids) || !$this->oids) { |
| 140 |
$this->setError('oids is empty.'); |
| 141 |
return $this->result; |
| 142 |
} |
| 143 |
|
| 144 |
if ($this->sync_type == 'new') { |
| 145 |
foreach ($this->oids as $okey => $oid) { |
| 146 |
if (empty($oid)) { |
| 147 |
continue; |
| 148 |
} |
| 149 |
$vcm = new SynchVikBooking($oid); |
| 150 |
$vcm->setSkipCheckAutoSync(); |
| 151 |
|
| 152 |
/** |
| 153 |
* We attempt to inject the previous status of the booking, if set. |
| 154 |
* For example, bookings that become confirmed could have been in a |
| 155 |
* pending (stand-by) status, and VCM should be aware of this change |
| 156 |
* because some channels may need extra actions. |
| 157 |
* |
| 158 |
* @since 1.14 (J) - 1.4.0 (WP) |
| 159 |
* @requires VCM >= 1.8.0 |
| 160 |
*/ |
| 161 |
if (!empty($this->orig_statuses[$okey]) && method_exists($vcm, 'setBookingPreviousStatus')) { |
| 162 |
$vcm->setBookingPreviousStatus($this->orig_statuses[$okey]); |
| 163 |
} |
| 164 |
|
| 165 |
$rq_rs = $vcm->sendRequest(); |
| 166 |
$this->result = $this->result || $rq_rs ? true : $this->result; |
| 167 |
} |
| 168 |
} elseif ($this->sync_type == 'modify') { |
| 169 |
// only one Booking ID per request as the original booking is transmitted in JSON format or as an array if called via PHP execution. |
| 170 |
if (is_array($this->orig_booking) && $this->orig_booking) { |
| 171 |
foreach ($this->oids as $okey => $oid) { |
| 172 |
if (empty($oid)) { |
| 173 |
continue; |
| 174 |
} |
| 175 |
$vcm = new SynchVikBooking($oid); |
| 176 |
$vcm->setSkipCheckAutoSync(); |
| 177 |
$vcm->setFromModification($this->orig_booking); |
| 178 |
$this->result = $vcm->sendRequest(); |
| 179 |
break; |
| 180 |
} |
| 181 |
} else { |
| 182 |
$this->setError('orig_booking is empty.'); |
| 183 |
} |
| 184 |
} elseif ($this->sync_type == 'cancel') { |
| 185 |
foreach ($this->oids as $okey => $oid) { |
| 186 |
if (empty($oid)) { |
| 187 |
continue; |
| 188 |
} |
| 189 |
$vcm = new SynchVikBooking($oid); |
| 190 |
$vcm->setSkipCheckAutoSync(); |
| 191 |
$vcm->setFromCancellation(['id' => $oid]); |
| 192 |
$rq_rs = $vcm->sendRequest(); |
| 193 |
$this->result = $this->result || $rq_rs ? true : $this->result; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
if ($this->result !== true && !$this->getError()) { |
| 198 |
$this->setError('VCM returned errors'); |
| 199 |
} |
| 200 |
|
| 201 |
return $this->result; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Sets the class error variable |
| 206 |
* |
| 207 |
* @param string $err_str |
| 208 |
*/ |
| 209 |
private function setError($err_str) |
| 210 |
{ |
| 211 |
$this->error .= (string)$err_str; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Returns the class error variable |
| 216 |
*/ |
| 217 |
public function getError() |
| 218 |
{ |
| 219 |
return $this->error; |
| 220 |
} |
| 221 |
} |
| 222 |
|