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