| @@ -124,8 +124,129 @@ | ||
| 124 | 124 | return $subunitMatrix->relocateRoomRecords(); |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | /** |
| 128 | + * Applies (or undoes) the relocation moveset to complete the room re-assignment operation. | |
| 129 | + * | |
| 130 | + * @param VBOBookingSubunitMoveset $moveset The moveset to process and apply. | |
| 131 | + * @param bool $undo True to undo the applied moveset. | |
| 132 | + * | |
| 133 | + * @return true | |
| 134 | + * | |
| 135 | + * @throws Exception | |
| 136 | + * | |
| 137 | + * @since 1.18.11 (J) - 1.8.11 (WP) | |
| 138 | + */ | |
| 139 | + public function applyMoveset(VBOBookingSubunitMoveset $moveset, bool $undo = false) | |
| 140 | + { | |
| 141 | + // obtain moveset signature | |
| 142 | + $signature = $moveset->getSignature(); | |
| 143 | + | |
| 144 | + return $this->execMovesetSignature($signature, $undo); | |
| 145 | + } | |
| 146 | + | |
| 147 | + /** | |
| 148 | + * Executes the relocation moveset signature to complete the room re-assignment operation. | |
| 149 | + * | |
| 150 | + * @param string $signature The moveset signature to execute. | |
| 151 | + * @param bool $undo True to undo the applied moveset. | |
| 152 | + * | |
| 153 | + * @return true | |
| 154 | + * | |
| 155 | + * @throws Exception | |
| 156 | + * | |
| 157 | + * @since 1.18.11 (J) - 1.8.11 (WP) | |
| 158 | + */ | |
| 159 | + public function execMovesetSignature(string $signature, bool $undo = false) | |
| 160 | + { | |
| 161 | + $dbo = JFactory::getDbo(); | |
| 162 | + | |
| 163 | + // room details empty container | |
| 164 | + $roomDetails = []; | |
| 165 | + | |
| 166 | + // confirm the integrity of the operations to execute | |
| 167 | + $confirmedOperations = []; | |
| 168 | + | |
| 169 | + // scan operation steps | |
| 170 | + $operationSteps = explode('-', $signature); | |
| 171 | + foreach ($operationSteps as $operationStep) { | |
| 172 | + // obtain move details | |
| 173 | + $moveData = explode('.', $operationStep); | |
| 174 | + if (count($moveData) < 4) { | |
| 175 | + throw new Exception('Invalid moveset operation.', 400); | |
| 176 | + } | |
| 177 | + | |
| 178 | + // fetch requested room record | |
| 179 | + $dbo->setQuery( | |
| 180 | + $dbo->getQuery(true) | |
| 181 | + ->select('*') | |
| 182 | + ->from($dbo->qn('#__vikbooking_ordersrooms')) | |
| 183 | + ->where($dbo->qn('id') . ' = ' . (int) ($moveData[1] ?? 0)) | |
| 184 | + ->where($dbo->qn('idorder') . ' = ' . (int) ($moveData[0] ?? 0)) | |
| 185 | + ); | |
| 186 | + $roomRecord = $dbo->loadAssoc(); | |
| 187 | + | |
| 188 | + if (!$roomRecord) { | |
| 189 | + throw new Exception('Could not find moveset operation.', 404); | |
| 190 | + } | |
| 191 | + | |
| 192 | + // confirm operation | |
| 193 | + $confirmedOperations[] = $moveData; | |
| 194 | + | |
| 195 | + if (!$roomDetails) { | |
| 196 | + // load the first and only room details | |
| 197 | + $roomDetails = VikBooking::getRoomInfo((int) $roomRecord['idroom'], ['id', 'name'], true); | |
| 198 | + } | |
| 199 | + } | |
| 200 | + | |
| 201 | + if (!$confirmedOperations) { | |
| 202 | + throw new Exception('No valid moveset operations.', 400); | |
| 203 | + } | |
| 204 | + | |
| 205 | + // get current CMS user and name | |
| 206 | + $user = JFactory::getUser(); | |
| 207 | + $userName = $user->name; | |
| 208 | + | |
| 209 | + // determine action name | |
| 210 | + $actionName = $undo ? JText::translate('VBO_UNDO_CHANGES') : JText::translate('VBO_RESOLVE_ROOM_ASSIGNMENT'); | |
| 211 | + | |
| 212 | + // apply all moves | |
| 213 | + foreach ($confirmedOperations as $moveData) { | |
| 214 | + // determine the room index to apply | |
| 215 | + $prev_room_index = $undo ? (int) ($moveData[3] ?? 0) : (int) ($moveData[2] ?? 0); | |
| 216 | + $new_room_index = $undo ? (int) ($moveData[2] ?? 0) : (int) ($moveData[3] ?? 0); | |
| 217 | + | |
| 218 | + // update current room record | |
| 219 | + $dbo->setQuery( | |
| 220 | + $dbo->getQuery(true) | |
| 221 | + ->update($dbo->qn('#__vikbooking_ordersrooms')) | |
| 222 | + ->set($dbo->qn('roomindex') . ' = ' . ($new_room_index ?: 'NULL')) | |
| 223 | + ->where($dbo->qn('id') . ' = ' . (int) ($moveData[1] ?? 0)) | |
| 224 | + ->where($dbo->qn('idorder') . ' = ' . (int) ($moveData[0] ?? 0)) | |
| 225 | + ); | |
| 226 | + $dbo->execute(); | |
| 227 | + | |
| 228 | + // Booking History | |
| 229 | + VikBooking::getBookingHistoryInstance((int) ($moveData[0] ?? 0)) | |
| 230 | + ->setExtraData([ | |
| 231 | + 'action' => 'resolve_room_assignment', | |
| 232 | + 'type' => $undo ? 'undo' : 'apply', | |
| 233 | + ]) | |
| 234 | + ->store( | |
| 235 | + 'MB', | |
| 236 | + sprintf( | |
| 237 | + '(%s) %s: %s', | |
| 238 | + (string) $userName, | |
| 239 | + $actionName, | |
| 240 | + JText::sprintf('VBOROOMSUBUNITCHANGEFT', $roomDetails['name'], $prev_room_index, $new_room_index) | |
| 241 | + ) | |
| 242 | + ); | |
| 243 | + } | |
| 244 | + | |
| 245 | + return true; | |
| 246 | + } | |
| 247 | + | |
| 248 | + /** | |
| 128 | 249 | * Builds and returns the room booking records matrix. |
| 129 | 250 | * |
| 130 | 251 | * @param ?array $relocateRoomRecord Optional room booking record to relocate. |
| 131 | 252 | * |