CustomerBookingEventPeriodRepository.php
8 months ago
CustomerBookingEventTicketRepository.php
8 months ago
EventPeriodsRepository.php
8 months ago
EventProvidersRepository.php
1 year ago
EventRepository.php
8 months ago
EventTagsRepository.php
8 months ago
EventTicketRepository.php
8 months ago
EventRepository.php
1991 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaBooking\Infrastructure\Repository\Booking\Event; |
| 4 | |
| 5 | use AmeliaBooking\Domain\Collection\Collection; |
| 6 | use AmeliaBooking\Domain\Common\Exceptions\InvalidArgumentException; |
| 7 | use AmeliaBooking\Domain\Entity\Booking\Event\Event; |
| 8 | use AmeliaBooking\Domain\Factory\Booking\Appointment\CustomerBookingFactory; |
| 9 | use AmeliaBooking\Domain\Factory\Booking\Event\EventFactory; |
| 10 | use AmeliaBooking\Domain\Repository\Booking\Event\EventRepositoryInterface; |
| 11 | use AmeliaBooking\Domain\Services\DateTime\DateTimeService; |
| 12 | use AmeliaBooking\Infrastructure\Common\Exceptions\QueryExecutionException; |
| 13 | use AmeliaBooking\Infrastructure\Licence; |
| 14 | use AmeliaBooking\Infrastructure\Repository\AbstractRepository; |
| 15 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\CustomerBookingsTable; |
| 16 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\CustomerBookingsToEventsPeriodsTable; |
| 17 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\CustomerBookingToEventsTicketsTable; |
| 18 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\EventsPeriodsTable; |
| 19 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\EventsProvidersTable; |
| 20 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\EventsTagsTable; |
| 21 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Booking\EventsTicketsTable; |
| 22 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Coupon\CouponsTable; |
| 23 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Gallery\GalleriesTable; |
| 24 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Location\LocationsTable; |
| 25 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\Payment\PaymentsTable; |
| 26 | use AmeliaBooking\Infrastructure\WP\InstallActions\DB\User\UsersTable; |
| 27 | |
| 28 | /** |
| 29 | * Class EventRepository |
| 30 | * |
| 31 | * @package AmeliaBooking\Infrastructure\Repository\Booking\Event |
| 32 | */ |
| 33 | class EventRepository extends AbstractRepository implements EventRepositoryInterface |
| 34 | { |
| 35 | public const FACTORY = EventFactory::class; |
| 36 | |
| 37 | /** |
| 38 | * @param Event $entity |
| 39 | * |
| 40 | * @return int |
| 41 | * @throws QueryExecutionException |
| 42 | */ |
| 43 | public function add($entity) |
| 44 | { |
| 45 | $data = $entity->toArray(); |
| 46 | |
| 47 | $params = [ |
| 48 | ':bookingOpens' => $data['bookingOpens'] ? DateTimeService::getCustomDateTimeInUtc($data['bookingOpens']) : null, |
| 49 | ':bookingCloses' => $data['bookingCloses'] ? DateTimeService::getCustomDateTimeInUtc($data['bookingCloses']) : null, |
| 50 | ':bookingOpensRec' => $data['bookingOpensRec'], |
| 51 | ':bookingClosesRec' => $data['bookingClosesRec'], |
| 52 | ':status' => $data['status'], |
| 53 | ':name' => $data['name'], |
| 54 | ':description' => $data['description'], |
| 55 | ':color' => $data['color'], |
| 56 | ':price' => $data['price'], |
| 57 | ':bringingAnyone' => $data['bringingAnyone'] ? 1 : 0, |
| 58 | ':bookMultipleTimes' => $data['bookMultipleTimes'] ? 1 : 0, |
| 59 | ':maxCapacity' => $data['maxCapacity'], |
| 60 | ':maxCustomCapacity' => $data['maxCustomCapacity'], |
| 61 | ':maxExtraPeople' => $data['maxExtraPeople'], |
| 62 | ':show' => $data['show'] ? 1 : 0, |
| 63 | ':notifyParticipants' => $data['notifyParticipants'], |
| 64 | ':customLocation' => $data['customLocation'], |
| 65 | ':parentId' => $data['parentId'], |
| 66 | ':created' => $data['created'], |
| 67 | ':closeAfterMin' => $data['closeAfterMin'], |
| 68 | ':closeAfterMinBookings' => $data['closeAfterMinBookings'] ? 1 : 0, |
| 69 | ':aggregatedPrice' => $data['aggregatedPrice'] ? 1 : 0, |
| 70 | ':pictureFullPath' => $data['pictureFullPath'], |
| 71 | ':pictureThumbPath' => $data['pictureThumbPath'], |
| 72 | ':error' => '', |
| 73 | ]; |
| 74 | |
| 75 | $additionalData = Licence\DataModifier::getEventRepositoryData($data); |
| 76 | |
| 77 | $params = array_merge($params, $additionalData['values'], $additionalData['addValues']); |
| 78 | |
| 79 | try { |
| 80 | $statement = $this->connection->prepare( |
| 81 | "INSERT INTO {$this->table} |
| 82 | ( |
| 83 | {$additionalData['columns']} |
| 84 | `bookingOpens`, |
| 85 | `bookingCloses`, |
| 86 | `bookingOpensRec`, |
| 87 | `bookingClosesRec`, |
| 88 | `status`, |
| 89 | `name`, |
| 90 | `description`, |
| 91 | `color`, |
| 92 | `price`, |
| 93 | `bringingAnyone`, |
| 94 | `bookMultipleTimes`, |
| 95 | `maxCapacity`, |
| 96 | `maxCustomCapacity`, |
| 97 | `maxExtraPeople`, |
| 98 | `show`, |
| 99 | `notifyParticipants`, |
| 100 | `customLocation`, |
| 101 | `parentId`, |
| 102 | `created`, |
| 103 | `closeAfterMin`, |
| 104 | `closeAfterMinBookings`, |
| 105 | `aggregatedPrice`, |
| 106 | `pictureFullPath`, |
| 107 | `pictureThumbPath`, |
| 108 | `error` |
| 109 | ) |
| 110 | VALUES ( |
| 111 | {$additionalData['placeholders']} |
| 112 | :bookingOpens, |
| 113 | :bookingCloses, |
| 114 | :bookingOpensRec, |
| 115 | :bookingClosesRec, |
| 116 | :status, |
| 117 | :name, |
| 118 | :description, |
| 119 | :color, |
| 120 | :price, |
| 121 | :bringingAnyone, |
| 122 | :bookMultipleTimes, |
| 123 | :maxCapacity, |
| 124 | :maxCustomCapacity, |
| 125 | :maxExtraPeople, |
| 126 | :show, |
| 127 | :notifyParticipants, |
| 128 | :customLocation, |
| 129 | :parentId, |
| 130 | :created, |
| 131 | :closeAfterMin, |
| 132 | :closeAfterMinBookings, |
| 133 | :aggregatedPrice, |
| 134 | :pictureFullPath, |
| 135 | :pictureThumbPath, |
| 136 | :error |
| 137 | )" |
| 138 | ); |
| 139 | |
| 140 | $res = $statement->execute($params); |
| 141 | |
| 142 | if (!$res) { |
| 143 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__); |
| 144 | } |
| 145 | |
| 146 | return $this->connection->lastInsertId(); |
| 147 | } catch (\Exception $e) { |
| 148 | throw new QueryExecutionException('Unable to add data in ' . __CLASS__, $e->getCode(), $e); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @param int $id |
| 154 | * @param Event $entity |
| 155 | * |
| 156 | * @return mixed |
| 157 | * @throws QueryExecutionException |
| 158 | */ |
| 159 | public function update($id, $entity) |
| 160 | { |
| 161 | $data = $entity->toArray(); |
| 162 | |
| 163 | $params = [ |
| 164 | ':id' => $id, |
| 165 | ':bookingOpens' => $data['bookingOpens'] ? DateTimeService::getCustomDateTimeInUtc($data['bookingOpens']) : null, |
| 166 | ':bookingCloses' => $data['bookingCloses'] ? DateTimeService::getCustomDateTimeInUtc($data['bookingCloses']) : null, |
| 167 | ':bookingOpensRec' => $data['bookingOpensRec'], |
| 168 | ':bookingClosesRec' => $data['bookingClosesRec'], |
| 169 | ':status' => $data['status'], |
| 170 | ':name' => $data['name'], |
| 171 | ':description' => $data['description'], |
| 172 | ':color' => $data['color'], |
| 173 | ':price' => $data['price'], |
| 174 | ':bringingAnyone' => $data['bringingAnyone'] ? 1 : 0, |
| 175 | ':bookMultipleTimes' => $data['bookMultipleTimes'] ? 1 : 0, |
| 176 | ':maxCapacity' => $data['maxCapacity'], |
| 177 | ':maxCustomCapacity' => $data['maxCustomCapacity'], |
| 178 | ':maxExtraPeople' => $data['maxExtraPeople'], |
| 179 | ':show' => $data['show'] ? 1 : 0, |
| 180 | ':notifyParticipants' => $data['notifyParticipants'] ? 1 : 0, |
| 181 | ':customLocation' => $data['customLocation'], |
| 182 | ':parentId' => $data['parentId'], |
| 183 | ':closeAfterMin' => $data['closeAfterMin'], |
| 184 | ':closeAfterMinBookings' => $data['closeAfterMinBookings'] ? 1 : 0, |
| 185 | ':aggregatedPrice' => $data['aggregatedPrice'] ? 1 : 0, |
| 186 | ':pictureFullPath' => $data['pictureFullPath'], |
| 187 | ':pictureThumbPath' => $data['pictureThumbPath'], |
| 188 | ]; |
| 189 | |
| 190 | $additionalData = Licence\DataModifier::getEventRepositoryData($data); |
| 191 | |
| 192 | $params = array_merge($params, $additionalData['values']); |
| 193 | |
| 194 | try { |
| 195 | $statement = $this->connection->prepare( |
| 196 | "UPDATE {$this->table} |
| 197 | SET |
| 198 | {$additionalData['columnsPlaceholders']} |
| 199 | `bookingOpens` = :bookingOpens, |
| 200 | `bookingCloses` = :bookingCloses, |
| 201 | `bookingOpensRec` = :bookingOpensRec, |
| 202 | `bookingClosesRec` = :bookingClosesRec, |
| 203 | `status` = :status, |
| 204 | `name` = :name, |
| 205 | `description` = :description, |
| 206 | `color` = :color, |
| 207 | `price` = :price, |
| 208 | `bringingAnyone` = :bringingAnyone, |
| 209 | `bookMultipleTimes` = :bookMultipleTimes, |
| 210 | `maxCapacity` = :maxCapacity, |
| 211 | `maxCustomCapacity` = :maxCustomCapacity, |
| 212 | `maxExtraPeople` = :maxExtraPeople, |
| 213 | `show` = :show, |
| 214 | `notifyParticipants` = :notifyParticipants, |
| 215 | `customLocation` = :customLocation, |
| 216 | `parentId` = :parentId, |
| 217 | `closeAfterMin` = :closeAfterMin, |
| 218 | `closeAfterMinBookings` = :closeAfterMinBookings, |
| 219 | `aggregatedPrice` = :aggregatedPrice, |
| 220 | `pictureFullPath` = :pictureFullPath, |
| 221 | `pictureThumbPath` = :pictureThumbPath |
| 222 | WHERE id = :id" |
| 223 | ); |
| 224 | |
| 225 | $res = $statement->execute($params); |
| 226 | |
| 227 | if (!$res) { |
| 228 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 229 | } |
| 230 | |
| 231 | return $res; |
| 232 | } catch (\Exception $e) { |
| 233 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * @param int $id |
| 239 | * @param int|null $parentId |
| 240 | * |
| 241 | * @return mixed |
| 242 | * @throws QueryExecutionException |
| 243 | */ |
| 244 | public function updateParentId($id, $parentId) |
| 245 | { |
| 246 | $params = [ |
| 247 | ':id' => $id, |
| 248 | ':parentId' => $parentId, |
| 249 | ]; |
| 250 | |
| 251 | try { |
| 252 | $statement = $this->connection->prepare( |
| 253 | "UPDATE {$this->table} |
| 254 | SET |
| 255 | `parentId` = :parentId |
| 256 | WHERE id = :id" |
| 257 | ); |
| 258 | |
| 259 | $res = $statement->execute($params); |
| 260 | |
| 261 | if (!$res) { |
| 262 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__); |
| 263 | } |
| 264 | |
| 265 | return $res; |
| 266 | } catch (\Exception $e) { |
| 267 | throw new QueryExecutionException('Unable to save data in ' . __CLASS__, $e->getCode(), $e); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * @param array $criteria |
| 273 | * |
| 274 | * @return Collection |
| 275 | * @throws QueryExecutionException |
| 276 | * @throws InvalidArgumentException |
| 277 | */ |
| 278 | public function getProvidersEvents($criteria) |
| 279 | { |
| 280 | $eventsPeriodsTable = EventsPeriodsTable::getTableName(); |
| 281 | $eventsProvidersTable = EventsProvidersTable::getTableName(); |
| 282 | $usersTable = UsersTable::getTableName(); |
| 283 | |
| 284 | $params = []; |
| 285 | $where = []; |
| 286 | |
| 287 | if (!empty($criteria['dates'])) { |
| 288 | if (isset($criteria['dates'][0], $criteria['dates'][1])) { |
| 289 | $whereStart = "(ep.periodStart BETWEEN :eventFrom AND :eventTo)"; |
| 290 | $params[':eventFrom'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 291 | $params[':eventTo'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 292 | |
| 293 | $whereEnd = "(ep.periodEnd BETWEEN :bookingFrom2 AND :bookingTo2)"; |
| 294 | $params[':bookingFrom2'] = $params[':eventFrom']; |
| 295 | $params[':bookingTo2'] = $params[':eventTo']; |
| 296 | |
| 297 | $where[] = "({$whereStart} OR {$whereEnd})"; |
| 298 | } elseif (isset($criteria['dates'][0])) { |
| 299 | $where[] = "(ep.periodStart >= :eventFrom)"; |
| 300 | $params[':eventFrom'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 301 | } elseif (isset($criteria['dates'][1])) { |
| 302 | $where[] = "(ep.periodStart <= :eventTo)"; |
| 303 | $params[':eventTo'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 304 | } else { |
| 305 | $where[] = "(ep.periodStart > :eventFrom)"; |
| 306 | $params[':eventFrom'] = DateTimeService::getNowDateTimeInUtc(); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | if (!empty($criteria['providers'])) { |
| 311 | $queryProviders = []; |
| 312 | |
| 313 | foreach ((array)$criteria['providers'] as $index => $value) { |
| 314 | $param = ':provider' . $index; |
| 315 | $queryProviders[] = $param; |
| 316 | $params[$param] = $value; |
| 317 | } |
| 318 | |
| 319 | $where[] = 'epr.userId IN (' . implode(', ', $queryProviders) . ')'; |
| 320 | } |
| 321 | |
| 322 | if (!empty($criteria['status'])) { |
| 323 | $params[':status'] = $criteria['status']; |
| 324 | |
| 325 | $where[] = 'e.status = :status'; |
| 326 | } |
| 327 | |
| 328 | $where = $where ? 'WHERE ' . implode(' AND ', $where) : ''; |
| 329 | |
| 330 | try { |
| 331 | $statement = $this->connection->prepare( |
| 332 | "SELECT |
| 333 | e.id AS event_id, |
| 334 | e.name AS event_name, |
| 335 | e.status AS event_status, |
| 336 | e.bookingOpens AS event_bookingOpens, |
| 337 | e.bookingCloses AS event_bookingCloses, |
| 338 | e.recurringCycle AS event_recurringCycle, |
| 339 | e.recurringOrder AS event_recurringOrder, |
| 340 | e.recurringInterval AS event_recurringInterval, |
| 341 | e.recurringUntil AS event_recurringUntil, |
| 342 | e.recurringMonthly AS event_recurringMonthly, |
| 343 | e.monthlyDate AS event_monthlyDate, |
| 344 | e.monthlyOnRepeat AS event_monthlyOnRepeat, |
| 345 | e.monthlyOnDay AS event_monthlyOnDay, |
| 346 | e.bringingAnyone AS event_bringingAnyone, |
| 347 | e.bookMultipleTimes AS event_bookMultipleTimes, |
| 348 | e.maxCapacity AS event_maxCapacity, |
| 349 | e.maxCustomCapacity AS event_maxCustomCapacity, |
| 350 | e.maxExtraPeople AS event_maxExtraPeople, |
| 351 | e.price AS event_price, |
| 352 | e.description AS event_description, |
| 353 | e.color AS event_color, |
| 354 | e.show AS event_show, |
| 355 | e.locationId AS event_locationId, |
| 356 | e.customLocation AS event_customLocation, |
| 357 | e.parentId AS event_parentId, |
| 358 | e.created AS event_created, |
| 359 | e.notifyParticipants AS event_notifyParticipants, |
| 360 | e.translations AS event_translations, |
| 361 | e.deposit AS event_deposit, |
| 362 | e.depositPayment AS event_depositPayment, |
| 363 | e.depositPerPerson AS event_depositPerPerson, |
| 364 | e.fullPayment AS event_fullPayment, |
| 365 | e.customPricing AS event_customPricing, |
| 366 | e.aggregatedPrice AS event_aggregatedPrice, |
| 367 | |
| 368 | ep.id AS event_periodId, |
| 369 | ep.periodStart AS event_periodStart, |
| 370 | ep.periodEnd AS event_periodEnd, |
| 371 | |
| 372 | pu.id AS provider_id, |
| 373 | pu.firstName AS provider_firstName, |
| 374 | pu.lastName AS provider_lastName, |
| 375 | pu.email AS provider_email, |
| 376 | pu.note AS provider_note, |
| 377 | pu.description AS provider_description, |
| 378 | pu.phone AS provider_phone, |
| 379 | pu.gender AS provider_gender, |
| 380 | pu.pictureFullPath AS provider_pictureFullPath, |
| 381 | pu.pictureThumbPath AS provider_pictureThumbPath, |
| 382 | pu.translations AS provider_translations |
| 383 | FROM {$this->table} e |
| 384 | INNER JOIN {$eventsPeriodsTable} ep ON ep.eventId = e.id |
| 385 | INNER JOIN {$eventsProvidersTable} epr ON epr.eventId = e.id |
| 386 | INNER JOIN {$usersTable} pu ON pu.id = epr.userId |
| 387 | {$where} |
| 388 | ORDER BY ep.periodStart" |
| 389 | ); |
| 390 | |
| 391 | $statement->execute($params); |
| 392 | |
| 393 | $rows = $statement->fetchAll(); |
| 394 | } catch (\Exception $e) { |
| 395 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 396 | } |
| 397 | |
| 398 | return call_user_func([static::FACTORY, 'createCollection'], $rows); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * @param array $criteria |
| 403 | * @param int|null $itemsPerPage |
| 404 | * |
| 405 | * @return array |
| 406 | * @throws QueryExecutionException |
| 407 | * @throws InvalidArgumentException |
| 408 | */ |
| 409 | public function getFilteredIds($criteria, $itemsPerPage) |
| 410 | { |
| 411 | $eventsPeriodsTable = EventsPeriodsTable::getTableName(); |
| 412 | $eventsTagsTable = EventsTagsTable::getTableName(); |
| 413 | $customerBookingsTable = CustomerBookingsTable::getTableName(); |
| 414 | $customerBookingsEventsPeriods = CustomerBookingsToEventsPeriodsTable::getTableName(); |
| 415 | $eventsProvidersTable = EventsProvidersTable::getTableName(); |
| 416 | $usersTable = UsersTable::getTableName(); |
| 417 | |
| 418 | $params = []; |
| 419 | |
| 420 | $where = []; |
| 421 | |
| 422 | $joins = ''; |
| 423 | |
| 424 | $groupBy = 'GROUP BY e.id'; |
| 425 | |
| 426 | if (isset($criteria['parentId'])) { |
| 427 | $params[':parentId'] = $criteria['parentId']; |
| 428 | |
| 429 | $params[':originParentId'] = $criteria['parentId']; |
| 430 | |
| 431 | $where[] = '(e.parentId = :parentId OR e.id = :originParentId)'; |
| 432 | } |
| 433 | |
| 434 | if (!empty($criteria['events'])) { |
| 435 | $queryExcludeIds = []; |
| 436 | |
| 437 | foreach ($criteria['events'] as $index => $value) { |
| 438 | $param = ':id' . $index; |
| 439 | |
| 440 | $queryExcludeIds[] = $param; |
| 441 | |
| 442 | $params[$param] = $value; |
| 443 | } |
| 444 | |
| 445 | $where[] = 'e.id IN (' . implode(', ', $queryExcludeIds) . ')'; |
| 446 | } |
| 447 | |
| 448 | if (!empty($criteria['excludeIds'])) { |
| 449 | $queryExcludeIds = []; |
| 450 | |
| 451 | foreach ($criteria['excludeIds'] as $index => $value) { |
| 452 | $param = ':excludeId' . $index; |
| 453 | |
| 454 | $queryExcludeIds[] = $param; |
| 455 | |
| 456 | $params[$param] = $value; |
| 457 | } |
| 458 | |
| 459 | $where[] = 'e.id NOT IN (' . implode(', ', $queryExcludeIds) . ')'; |
| 460 | } |
| 461 | |
| 462 | if (!empty($criteria['search'])) { |
| 463 | $params[':search1'] = "%{$criteria['search']}%"; |
| 464 | $params[':search2'] = "{\"name\":{%{$criteria['search']}%\"description\":{%"; |
| 465 | $params[':search3'] = "{\"description\":{%\"name\":{%{$criteria['search']}%"; |
| 466 | $params[':search4'] = "{\"name\":{%{$criteria['search']}%"; |
| 467 | |
| 468 | $where[] = "(e.name LIKE :search1 |
| 469 | OR e.translations LIKE :search2 |
| 470 | OR e.translations LIKE :search3 |
| 471 | OR (e.translations LIKE :search4 AND e.translations NOT LIKE '%\"description\":{%') |
| 472 | OR e.id LIKE '%" . $criteria['search'] . "%')"; |
| 473 | } |
| 474 | |
| 475 | if (!empty($criteria['status'])) { |
| 476 | $params[':status'] = $criteria['status']; |
| 477 | $where[] = "e.status = :status"; |
| 478 | } |
| 479 | |
| 480 | if (isset($criteria['show'])) { |
| 481 | $where[] = 'e.show = 1'; |
| 482 | } |
| 483 | |
| 484 | if (!empty($criteria['dates'])) { |
| 485 | if (isset($criteria['dates'][0], $criteria['dates'][1])) { |
| 486 | $where[] = "((ep.periodStart BETWEEN :eventFrom1 AND :eventTo1) |
| 487 | OR (ep.periodEnd BETWEEN :eventFrom2 AND :eventTo2) |
| 488 | OR (:eventFrom3 BETWEEN ep.periodStart AND ep.periodEnd) |
| 489 | OR (:eventTo3 BETWEEN ep.periodStart AND ep.periodEnd))"; |
| 490 | |
| 491 | $params[':eventFrom1'] = $params[':eventFrom2'] = $params[':eventFrom3'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 492 | $params[':eventTo1'] = $params[':eventTo2'] = $params[':eventTo3'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 493 | } elseif (isset($criteria['dates'][0])) { |
| 494 | $where[] = "(ep.periodStart >= :eventFrom OR (ep.periodEnd >= :eventTo))"; |
| 495 | $params[':eventFrom'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 496 | $params[':eventTo'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 497 | } elseif (isset($criteria['dates'][1])) { |
| 498 | $where[] = "(ep.periodStart <= :eventTo)"; |
| 499 | |
| 500 | $params[':eventTo'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 501 | } else { |
| 502 | $where[] = "(ep.periodStart > :eventFrom)"; |
| 503 | $params[':eventFrom'] = DateTimeService::getNowDateTimeInUtc(); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | if (!empty($criteria['tag'])) { |
| 508 | $queryTags = []; |
| 509 | |
| 510 | $tags = $criteria['tag']; |
| 511 | foreach ((array)$tags as $index => $value) { |
| 512 | $param = ':tag' . $index; |
| 513 | |
| 514 | $queryTags[] = $param; |
| 515 | |
| 516 | $params[$param] = $value; |
| 517 | } |
| 518 | |
| 519 | $where[] = 'et.name IN (' . implode(', ', $queryTags) . ')'; |
| 520 | |
| 521 | $joins .= " |
| 522 | INNER JOIN {$eventsTagsTable} et ON et.eventId = e.id |
| 523 | "; |
| 524 | } |
| 525 | |
| 526 | if (!empty($criteria['id'])) { |
| 527 | if (!empty($criteria['recurring'])) { |
| 528 | $whereOr = []; |
| 529 | foreach ((array)$criteria['id'] as $index => $value) { |
| 530 | $param = 'id' . $index; |
| 531 | |
| 532 | $params[':rec1' . $param] = (int)$value; |
| 533 | $params[':rec2' . $param] = (int)$value; |
| 534 | $params[':rec3' . $param] = (int)$value; |
| 535 | $params[':rec4' . $param] = (int)$value; |
| 536 | |
| 537 | $whereOr[] = "((e.id = :rec1id" . $index . " AND e.parentId IS NULL) OR |
| 538 | (e.parentId IN (SELECT parentId FROM {$this->table} WHERE parentId IS NOT NULL AND parentId = :rec2id" . $index . ")) OR |
| 539 | (e.id >= :rec3id" . $index . " AND e.parentId IN (SELECT parentId FROM {$this->table} WHERE id = :rec4id" . $index . ")))"; |
| 540 | } |
| 541 | $where[] = '(' . implode(' OR ', $whereOr) . ')'; |
| 542 | } else { |
| 543 | $queryIds = []; |
| 544 | |
| 545 | foreach ((array)$criteria['id'] as $index => $value) { |
| 546 | $param = ':id' . $index; |
| 547 | |
| 548 | $queryIds[] = $param; |
| 549 | |
| 550 | $params[$param] = (int)$value; |
| 551 | } |
| 552 | |
| 553 | $where[] = 'e.id IN (' . implode(', ', $queryIds) . ')'; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | if (!empty($criteria['customers']) || !empty($criteria['customerId']) || !empty($criteria['customerBookingsIds'])) { |
| 558 | $joins .= " |
| 559 | LEFT JOIN {$customerBookingsEventsPeriods} cbe ON cbe.eventPeriodId = ep.id |
| 560 | LEFT JOIN {$customerBookingsTable} cb ON cb.id = cbe.customerBookingId |
| 561 | "; |
| 562 | |
| 563 | if (!empty($criteria['customerId'])) { |
| 564 | $params[':customerId'] = $criteria['customerId']; |
| 565 | |
| 566 | $where[] = 'cb.customerId = :customerId'; |
| 567 | } |
| 568 | |
| 569 | if (!empty($criteria['customers'])) { |
| 570 | $queryCustomerIds = []; |
| 571 | |
| 572 | foreach ($criteria['customers'] as $index => $value) { |
| 573 | $param = ':customerId' . $index; |
| 574 | |
| 575 | $queryCustomerIds[] = $param; |
| 576 | |
| 577 | $params[$param] = $value; |
| 578 | } |
| 579 | |
| 580 | $where[] = 'cb.customerId IN (' . implode(', ', $queryCustomerIds) . ')'; |
| 581 | } |
| 582 | |
| 583 | if (!empty($criteria['customerBookingsIds'])) { |
| 584 | $queryBookingsIds = []; |
| 585 | |
| 586 | foreach ($criteria['customerBookingsIds'] as $index => $value) { |
| 587 | $param = ':customerBookingId' . $index; |
| 588 | |
| 589 | $queryBookingsIds[] = $param; |
| 590 | |
| 591 | $params[$param] = $value; |
| 592 | } |
| 593 | |
| 594 | $where[] = 'cb.id IN (' . implode(', ', $queryBookingsIds) . ')'; |
| 595 | } |
| 596 | |
| 597 | if (!empty($criteria['customerBookingStatus'])) { |
| 598 | $params[':customerBookingStatus'] = $criteria['customerBookingStatus']; |
| 599 | |
| 600 | $where[] = 'cb.status = :customerBookingStatus'; |
| 601 | } |
| 602 | |
| 603 | if (!empty($criteria['customerBookingCouponId'])) { |
| 604 | $params[':customerBookingCouponId'] = $criteria['customerBookingCouponId']; |
| 605 | |
| 606 | $where[] = 'cb.couponId = :customerBookingCouponId'; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | if (!empty($criteria['locationId'])) { |
| 611 | $params[':locationId'] = $criteria['locationId']; |
| 612 | |
| 613 | $where[] = 'e.locationId = :locationId'; |
| 614 | } |
| 615 | |
| 616 | if (!empty($criteria['locations'])) { |
| 617 | $queryLocations = []; |
| 618 | |
| 619 | foreach ((array)$criteria['locations'] as $index => $value) { |
| 620 | $param = ':location' . $index; |
| 621 | $queryLocations[] = $param; |
| 622 | $params[$param] = $value; |
| 623 | } |
| 624 | |
| 625 | $where3 = 'e.locationId IN (' . implode(', ', $queryLocations) . ')'; |
| 626 | |
| 627 | $where[] = '(' . $where3 . ')'; |
| 628 | } |
| 629 | |
| 630 | if (!empty($criteria['providers'])) { |
| 631 | $joins .= " |
| 632 | LEFT JOIN {$eventsProvidersTable} epr ON epr.eventId = e.id |
| 633 | INNER JOIN {$usersTable} pu ON pu.id = epr.userId OR pu.id = e.organizerId |
| 634 | "; |
| 635 | |
| 636 | $queryProviders = []; |
| 637 | |
| 638 | foreach ((array)$criteria['providers'] as $index => $value) { |
| 639 | $param = ':provider' . $index; |
| 640 | $queryProviders[] = $param; |
| 641 | $params[$param] = $value; |
| 642 | } |
| 643 | |
| 644 | $where1 = 'epr.userId IN (' . implode(', ', $queryProviders) . ')'; |
| 645 | |
| 646 | $queryProviders = []; |
| 647 | foreach ((array)$criteria['providers'] as $index => $value) { |
| 648 | $param = ':organizer' . $index; |
| 649 | $queryProviders[] = $param; |
| 650 | $params[$param] = $value; |
| 651 | } |
| 652 | |
| 653 | $where2 = 'e.organizerId IN (' . implode(', ', $queryProviders) . ')'; |
| 654 | |
| 655 | $where[] = '(' . $where1 . ' OR ' . $where2 . ')'; |
| 656 | } |
| 657 | |
| 658 | $where = $where ? 'WHERE ' . implode(' AND ', $where) : ''; |
| 659 | |
| 660 | $limit = $this->getLimit( |
| 661 | !empty($criteria['page']) ? (int)$criteria['page'] : 0, |
| 662 | (int)$itemsPerPage |
| 663 | ); |
| 664 | |
| 665 | try { |
| 666 | $statement = $this->connection->prepare( |
| 667 | "SELECT |
| 668 | e.id |
| 669 | FROM {$this->table} e |
| 670 | INNER JOIN {$eventsPeriodsTable} ep ON ep.eventId = e.id |
| 671 | {$joins} |
| 672 | {$where} |
| 673 | {$groupBy} |
| 674 | {$this->getOrderBy(!empty($criteria['sort']) ? $criteria['sort'] : null)} |
| 675 | {$limit}" |
| 676 | ); |
| 677 | |
| 678 | $statement->execute($params); |
| 679 | |
| 680 | $rows = $statement->fetchAll(); |
| 681 | } catch (\Exception $e) { |
| 682 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 683 | } |
| 684 | |
| 685 | return array_column($rows, 'id'); |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * @param array $criteria |
| 690 | * |
| 691 | * @return int |
| 692 | * @throws QueryExecutionException |
| 693 | * @throws InvalidArgumentException |
| 694 | */ |
| 695 | public function getFilteredIdsCount($criteria) |
| 696 | { |
| 697 | $eventsPeriodsTable = EventsPeriodsTable::getTableName(); |
| 698 | $eventsTagsTable = EventsTagsTable::getTableName(); |
| 699 | $eventsProvidersTable = EventsProvidersTable::getTableName(); |
| 700 | $usersTable = UsersTable::getTableName(); |
| 701 | |
| 702 | |
| 703 | $params = []; |
| 704 | $where = []; |
| 705 | |
| 706 | if (isset($criteria['parentId'])) { |
| 707 | $params[':parentId'] = $criteria['parentId']; |
| 708 | |
| 709 | $params[':originParentId'] = $criteria['parentId']; |
| 710 | |
| 711 | $where[] = 'e.parentId = :parentId OR e.id = :originParentId'; |
| 712 | } |
| 713 | |
| 714 | if (!empty($criteria['search'])) { |
| 715 | $params[':search1'] = "%{$criteria['search']}%"; |
| 716 | $params[':search2'] = "{\"name\":{%{$criteria['search']}%\"description\":{%"; |
| 717 | $params[':search3'] = "{\"description\":{%\"name\":{%{$criteria['search']}%"; |
| 718 | $params[':search4'] = "{\"name\":{%{$criteria['search']}%"; |
| 719 | |
| 720 | $where[] = "(e.name LIKE :search1 |
| 721 | OR e.translations LIKE :search2 |
| 722 | OR e.translations LIKE :search3 |
| 723 | OR (e.translations LIKE :search4 AND e.translations NOT LIKE '%\"description\":{%') |
| 724 | OR e.id LIKE '%" . $criteria['search'] . "%')"; |
| 725 | } |
| 726 | |
| 727 | if (isset($criteria['show'])) { |
| 728 | $where[] = 'e.show = 1'; |
| 729 | } |
| 730 | |
| 731 | if (!empty($criteria['dates'])) { |
| 732 | if (isset($criteria['dates'][0], $criteria['dates'][1])) { |
| 733 | $where[] = "((ep.periodStart BETWEEN :eventFrom1 AND :eventTo1) |
| 734 | OR (ep.periodEnd BETWEEN :eventFrom2 AND :eventTo2) |
| 735 | OR (:eventFrom3 BETWEEN ep.periodStart AND ep.periodEnd) |
| 736 | OR (:eventTo3 BETWEEN ep.periodStart AND ep.periodEnd))"; |
| 737 | |
| 738 | $params[':eventFrom1'] = $params[':eventFrom2'] = $params[':eventFrom3'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 739 | $params[':eventTo1'] = $params[':eventTo2'] = $params[':eventTo3'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 740 | } elseif (isset($criteria['dates'][0])) { |
| 741 | $where[] = "(ep.periodStart >= :eventFrom OR (ep.periodEnd >= :eventTo))"; |
| 742 | |
| 743 | $params[':eventFrom'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 744 | |
| 745 | $params[':eventTo'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][0]); |
| 746 | } elseif (isset($criteria['dates'][1])) { |
| 747 | $where[] = "(ep.periodStart <= :eventTo)"; |
| 748 | |
| 749 | $params[':eventTo'] = DateTimeService::getCustomDateTimeInUtc($criteria['dates'][1]); |
| 750 | } else { |
| 751 | $where[] = "(ep.periodStart > :eventFrom)"; |
| 752 | |
| 753 | $params[':eventFrom'] = DateTimeService::getNowDateTimeInUtc(); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | if (!empty($criteria['locationId'])) { |
| 758 | $params[':locationId'] = $criteria['locationId']; |
| 759 | |
| 760 | $where[] = 'e.locationId = :locationId'; |
| 761 | } |
| 762 | |
| 763 | if (!empty($criteria['locations'])) { |
| 764 | $queryLocations = []; |
| 765 | |
| 766 | foreach ((array)$criteria['locations'] as $index => $value) { |
| 767 | $param = ':location' . $index; |
| 768 | $queryLocations[] = $param; |
| 769 | $params[$param] = $value; |
| 770 | } |
| 771 | |
| 772 | $where3 = 'e.locationId IN (' . implode(', ', $queryLocations) . ')'; |
| 773 | |
| 774 | $where[] = '(' . $where3 . ')'; |
| 775 | } |
| 776 | |
| 777 | |
| 778 | $tagJoin = ''; |
| 779 | |
| 780 | if (isset($criteria['tag'])) { |
| 781 | $queryTags = []; |
| 782 | |
| 783 | $tags = $criteria['tag'];//explode(',', $criteria['tag']); |
| 784 | foreach ((array)$tags as $index => $value) { |
| 785 | $param = ':tag' . $index; |
| 786 | |
| 787 | $queryTags[] = $param; |
| 788 | |
| 789 | $params[$param] = $value;//trim($value, '{}'); |
| 790 | } |
| 791 | |
| 792 | $where[] = 'et.name IN (' . implode(', ', $queryTags) . ')'; |
| 793 | |
| 794 | $tagJoin = "INNER JOIN {$eventsTagsTable} et ON et.eventId = e.id"; |
| 795 | } |
| 796 | |
| 797 | if (!empty($criteria['id'])) { |
| 798 | if (!empty($criteria['recurring'])) { |
| 799 | $whereOr = []; |
| 800 | foreach ((array)$criteria['id'] as $index => $value) { |
| 801 | $param = 'id' . $index; |
| 802 | |
| 803 | $params[':rec1' . $param] = (int)$value; |
| 804 | $params[':rec2' . $param] = (int)$value; |
| 805 | $params[':rec3' . $param] = (int)$value; |
| 806 | $params[':rec4' . $param] = (int)$value; |
| 807 | |
| 808 | $whereOr[] = "((e.id = :rec1id" . $index . " AND e.parentId IS NULL) OR |
| 809 | (e.parentId IN (SELECT parentId FROM {$this->table} WHERE parentId IS NOT NULL AND parentId = :rec2id" . $index . ")) OR |
| 810 | (e.id >= :rec3id" . $index . " AND e.parentId IN (SELECT parentId FROM {$this->table} WHERE id = :rec4id" . $index . ")))"; |
| 811 | } |
| 812 | $where[] = '(' . implode(' OR ', $whereOr) . ')'; |
| 813 | } else { |
| 814 | $queryIds = []; |
| 815 | |
| 816 | foreach ((array)$criteria['id'] as $index => $value) { |
| 817 | $param = ':id' . $index; |
| 818 | |
| 819 | $queryIds[] = $param; |
| 820 | |
| 821 | $params[$param] = (int)$value; |
| 822 | } |
| 823 | |
| 824 | $where[] = 'e.id IN (' . implode(', ', $queryIds) . ')'; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | $providerJoin = ''; |
| 829 | |
| 830 | if (!empty($criteria['providers'])) { |
| 831 | $providerJoin = " |
| 832 | LEFT JOIN {$eventsProvidersTable} epr ON epr.eventId = e.id |
| 833 | INNER JOIN {$usersTable} pu ON pu.id = epr.userId OR pu.id = e.organizerId"; |
| 834 | |
| 835 | $queryProviders = []; |
| 836 | |
| 837 | foreach ((array)$criteria['providers'] as $index => $value) { |
| 838 | $param = ':provider' . $index; |
| 839 | $queryProviders[] = $param; |
| 840 | $params[$param] = $value; |
| 841 | } |
| 842 | $where1 = 'epr.userId IN (' . implode(', ', $queryProviders) . ')'; |
| 843 | |
| 844 | $queryProviders = []; |
| 845 | foreach ((array)$criteria['providers'] as $index => $value) { |
| 846 | $param = ':organizer' . $index; |
| 847 | $queryProviders[] = $param; |
| 848 | $params[$param] = $value; |
| 849 | } |
| 850 | $where2 = 'e.organizerId IN (' . implode(', ', $queryProviders) . ')'; |
| 851 | |
| 852 | $where[] = '(' . $where1 . ' OR ' . $where2 . ')'; |
| 853 | } |
| 854 | |
| 855 | $customerJoin = ''; |
| 856 | |
| 857 | $customerBookingsTable = CustomerBookingsTable::getTableName(); |
| 858 | $customerBookingsEventsPeriods = CustomerBookingsToEventsPeriodsTable::getTableName(); |
| 859 | |
| 860 | if (!empty($criteria['customerId']) || !empty($criteria['customerBookingsIds'])) { |
| 861 | $customerJoin = " |
| 862 | LEFT JOIN {$customerBookingsEventsPeriods} cbe ON cbe.eventPeriodId = ep.id |
| 863 | LEFT JOIN {$customerBookingsTable} cb ON cb.id = cbe.customerBookingId"; |
| 864 | |
| 865 | if (!empty($criteria['customerId'])) { |
| 866 | $params[':customerId'] = $criteria['customerId']; |
| 867 | |
| 868 | $where[] = 'cb.customerId = :customerId'; |
| 869 | } |
| 870 | |
| 871 | if (!empty($criteria['customerBookingsIds'])) { |
| 872 | $queryBookingsIds = []; |
| 873 | |
| 874 | foreach ($criteria['customerBookingsIds'] as $index => $value) { |
| 875 | $param = ':customerBookingId' . $index; |
| 876 | |
| 877 | $queryBookingsIds[] = $param; |
| 878 | |
| 879 | $params[$param] = $value; |
| 880 | } |
| 881 | |
| 882 | $where[] = 'cb.id IN (' . implode(', ', $queryBookingsIds) . ')'; |
| 883 | } |
| 884 | |
| 885 | if (!empty($criteria['customerBookingStatus'])) { |
| 886 | $params[':customerBookingStatus'] = $criteria['customerBookingStatus']; |
| 887 | |
| 888 | $where[] = 'cb.status = :customerBookingStatus'; |
| 889 | } |
| 890 | |
| 891 | if (!empty($criteria['customerBookingCouponId'])) { |
| 892 | $params[':customerBookingCouponId'] = $criteria['customerBookingCouponId']; |
| 893 | |
| 894 | $where[] = 'cb.couponId = :customerBookingCouponId'; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | $where = $where ? 'WHERE ' . implode(' AND ', $where) : ''; |
| 899 | |
| 900 | try { |
| 901 | $statement = $this->connection->prepare( |
| 902 | "SELECT e.id |
| 903 | FROM {$this->table} e |
| 904 | INNER JOIN {$eventsPeriodsTable} ep ON ep.eventId = e.id |
| 905 | {$tagJoin} |
| 906 | {$providerJoin} |
| 907 | {$customerJoin} |
| 908 | {$where} |
| 909 | GROUP BY e.id |
| 910 | ORDER BY ep.periodStart" |
| 911 | ); |
| 912 | |
| 913 | $statement->execute($params); |
| 914 | |
| 915 | $rows = $statement->fetchAll(); |
| 916 | } catch (\Exception $e) { |
| 917 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 918 | } |
| 919 | |
| 920 | return sizeOf($rows); |
| 921 | } |
| 922 | |
| 923 | /** |
| 924 | * @param int $id |
| 925 | * |
| 926 | * @return Event |
| 927 | * @throws QueryExecutionException |
| 928 | * @throws InvalidArgumentException |
| 929 | */ |
| 930 | public function getById($id, $criteria = []) |
| 931 | { |
| 932 | $eventsPeriodsTable = EventsPeriodsTable::getTableName(); |
| 933 | $eventsTagsTable = EventsTagsTable::getTableName(); |
| 934 | $eventsTicketTable = EventsTicketsTable::getTableName(); |
| 935 | |
| 936 | $customerBookingsTable = CustomerBookingsTable::getTableName(); |
| 937 | $paymentsTable = PaymentsTable::getTableName(); |
| 938 | $usersTable = UsersTable::getTableName(); |
| 939 | $customerBookingsEventsPeriods = CustomerBookingsToEventsPeriodsTable::getTableName(); |
| 940 | $galleriesTable = GalleriesTable::getTableName(); |
| 941 | $eventsProvidersTable = EventsProvidersTable::getTableName(); |
| 942 | $couponsTable = CouponsTable::getTableName(); |
| 943 | |
| 944 | $fields = ''; |
| 945 | |
| 946 | $joins = "INNER JOIN {$eventsPeriodsTable} ep ON ep.eventId = e.id |
| 947 | LEFT JOIN {$eventsTagsTable} et ON et.eventId = e.id |
| 948 | LEFT JOIN {$customerBookingsEventsPeriods} cbe ON cbe.eventPeriodId = ep.id |
| 949 | LEFT JOIN {$customerBookingsTable} cb ON cb.id = cbe.customerBookingId |
| 950 | LEFT JOIN {$usersTable} cu ON cu.id = cb.customerId |
| 951 | LEFT JOIN {$eventsProvidersTable} epr ON epr.eventId = e.id |
| 952 | LEFT JOIN {$usersTable} pu ON pu.id = epr.userId |
| 953 | LEFT JOIN {$paymentsTable} p ON p.customerBookingId = cb.id |
| 954 | LEFT JOIN {$galleriesTable} g ON g.entityId = e.id AND g.entityType = 'event' |
| 955 | LEFT JOIN {$couponsTable} c ON c.id = cb.couponId |
| 956 | LEFT JOIN {$eventsTicketTable} t ON t.eventId = e.id"; |
| 957 | |
| 958 | if (!empty($criteria['fetchBookingsTickets'])) { |
| 959 | $bookingsTicketsTable = CustomerBookingToEventsTicketsTable::getTableName(); |
| 960 | |
| 961 | $fields .= ' |
| 962 | cbt.id AS booking_ticket_id, |
| 963 | cbt.eventTicketId AS booking_ticket_eventTicketId, |
| 964 | cbt.price AS booking_ticket_price, |
| 965 | cbt.persons AS booking_ticket_persons, |
| 966 | '; |
| 967 | |
| 968 | $joins .= " |
| 969 | LEFT JOIN {$bookingsTicketsTable} cbt ON cbt.customerBookingId = cb.id |
| 970 | "; |
| 971 | } |
| 972 | |
| 973 | $fields .= 'e.id AS event_id, |
| 974 | e.name AS event_name, |
| 975 | e.status AS event_status, |
| 976 | e.bookingOpens AS event_bookingOpens, |
| 977 | e.bookingCloses AS event_bookingCloses, |
| 978 | e.bookingOpensRec AS event_bookingOpensRec, |
| 979 | e.bookingClosesRec AS event_bookingClosesRec, |
| 980 | e.ticketRangeRec AS event_ticketRangeRec, |
| 981 | e.recurringCycle AS event_recurringCycle, |
| 982 | e.recurringOrder AS event_recurringOrder, |
| 983 | e.recurringInterval AS event_recurringInterval, |
| 984 | e.recurringMonthly AS event_recurringMonthly, |
| 985 | e.monthlyDate AS event_monthlyDate, |
| 986 | e.monthlyOnRepeat AS event_monthlyOnRepeat, |
| 987 | e.monthlyOnDay AS event_monthlyOnDay, |
| 988 | e.recurringUntil AS event_recurringUntil, |
| 989 | e.bringingAnyone AS event_bringingAnyone, |
| 990 | e.bookMultipleTimes AS event_bookMultipleTimes, |
| 991 | e.maxCapacity AS event_maxCapacity, |
| 992 | e.maxCustomCapacity AS event_maxCustomCapacity, |
| 993 | e.maxExtraPeople AS event_maxExtraPeople, |
| 994 | e.price AS event_price, |
| 995 | e.description AS event_description, |
| 996 | e.color AS event_color, |
| 997 | e.show AS event_show, |
| 998 | e.notifyParticipants AS event_notifyParticipants, |
| 999 | e.locationId AS event_locationId, |
| 1000 | e.customLocation AS event_customLocation, |
| 1001 | e.parentId AS event_parentId, |
| 1002 | e.created AS event_created, |
| 1003 | e.settings AS event_settings, |
| 1004 | e.zoomUserId AS event_zoomUserId, |
| 1005 | e.organizerId AS event_organizerId, |
| 1006 | e.translations AS event_translations, |
| 1007 | e.deposit AS event_deposit, |
| 1008 | e.depositPayment AS event_depositPayment, |
| 1009 | e.depositPerPerson AS event_depositPerPerson, |
| 1010 | e.fullPayment AS event_fullPayment, |
| 1011 | e.customPricing AS event_customPricing, |
| 1012 | e.aggregatedPrice AS event_aggregatedPrice, |
| 1013 | |
| 1014 | ep.id AS event_periodId, |
| 1015 | ep.periodStart AS event_periodStart, |
| 1016 | ep.periodEnd AS event_periodEnd, |
| 1017 | ep.zoomMeeting AS event_periodZoomMeeting, |
| 1018 | ep.lessonSpace AS event_periodLessonSpace, |
| 1019 | ep.googleCalendarEventId AS event_googleCalendarEventId, |
| 1020 | ep.googleMeetUrl AS event_googleMeetUrl, |
| 1021 | ep.outlookCalendarEventId AS event_outlookCalendarEventId, |
| 1022 | ep.microsoftTeamsUrl AS event_microsoftTeamsUrl, |
| 1023 | ep.appleCalendarEventId AS event_appleCalendarEventId, |
| 1024 | |
| 1025 | et.id AS event_tagId, |
| 1026 | et.name AS event_tagName, |
| 1027 | |
| 1028 | cb.id AS booking_id, |
| 1029 | cb.customerId AS booking_customerId, |
| 1030 | cb.status AS booking_status, |
| 1031 | cb.price AS booking_price, |
| 1032 | cb.persons AS booking_persons, |
| 1033 | cb.customFields AS booking_customFields, |
| 1034 | cb.info AS booking_info, |
| 1035 | cb.aggregatedPrice AS booking_aggregatedPrice, |
| 1036 | cb.token AS booking_token, |
| 1037 | cb.utcOffset AS booking_utcOffset, |
| 1038 | cb.couponId AS booking_couponId, |
| 1039 | |
| 1040 | cu.id AS customer_id, |
| 1041 | cu.firstName AS customer_firstName, |
| 1042 | cu.lastName AS customer_lastName, |
| 1043 | cu.email AS customer_email, |
| 1044 | cu.note AS customer_note, |
| 1045 | cu.phone AS customer_phone, |
| 1046 | cu.gender AS customer_gender, |
| 1047 | cu.birthday AS customer_birthday, |
| 1048 | |
| 1049 | p.id AS payment_id, |
| 1050 | p.amount AS payment_amount, |
| 1051 | p.dateTime AS payment_dateTime, |
| 1052 | p.status AS payment_status, |
| 1053 | p.gateway AS payment_gateway, |
| 1054 | p.gatewayTitle AS payment_gatewayTitle, |
| 1055 | p.transactionId AS payment_transactionId, |
| 1056 | p.data AS payment_data, |
| 1057 | p.wcOrderId AS payment_wcOrderId, |
| 1058 | p.wcOrderItemId AS payment_wcOrderItemId, |
| 1059 | p.invoiceNumber AS payment_invoiceNumber, |
| 1060 | |
| 1061 | pu.id AS provider_id, |
| 1062 | pu.firstName AS provider_firstName, |
| 1063 | pu.lastName AS provider_lastName, |
| 1064 | pu.email AS provider_email, |
| 1065 | pu.note AS provider_note, |
| 1066 | pu.description AS provider_description, |
| 1067 | pu.phone AS provider_phone, |
| 1068 | pu.gender AS provider_gender, |
| 1069 | pu.translations AS provider_translations, |
| 1070 | pu.timeZone AS provider_timeZone, |
| 1071 | |
| 1072 | g.id AS gallery_id, |
| 1073 | g.pictureFullPath AS gallery_picture_full, |
| 1074 | g.pictureThumbPath AS gallery_picture_thumb, |
| 1075 | g.position AS gallery_position, |
| 1076 | |
| 1077 | c.id AS coupon_id, |
| 1078 | c.code AS coupon_code, |
| 1079 | c.discount AS coupon_discount, |
| 1080 | c.deduction AS coupon_deduction, |
| 1081 | c.limit AS coupon_limit, |
| 1082 | c.customerLimit AS coupon_customerLimit, |
| 1083 | c.status AS coupon_status, |
| 1084 | |
| 1085 | t.id AS ticket_id, |
| 1086 | t.name AS ticket_name, |
| 1087 | t.enabled AS ticket_enabled, |
| 1088 | t.price AS ticket_price, |
| 1089 | t.spots AS ticket_spots, |
| 1090 | t.waitingListSpots AS ticket_waiting_list_spots, |
| 1091 | t.dateRanges AS ticket_dateRanges, |
| 1092 | t.translations AS ticket_translations'; |
| 1093 | |
| 1094 | try { |
| 1095 | $statement = $this->connection->prepare( |
| 1096 | "SELECT |
| 1097 | {$fields} |
| 1098 | FROM {$this->table} e |
| 1099 | {$joins} |
| 1100 | |
| 1101 | WHERE e.id = :eventId" |
| 1102 | ); |
| 1103 | |
| 1104 | $statement->bindParam(':eventId', $id); |
| 1105 | |
| 1106 | $statement->execute(); |
| 1107 | |
| 1108 | $rows = $statement->fetchAll(); |
| 1109 | } catch (\Exception $e) { |
| 1110 | throw new QueryExecutionException('Unable to find event by id in ' . __CLASS__, $e->getCode(), $e); |
| 1111 | } |
| 1112 | |
| 1113 | return call_user_func([static::FACTORY, 'createCollection'], $rows)->getItem($id); |
| 1114 | } |
| 1115 | |
| 1116 | |
| 1117 | /** |
| 1118 | * @param int $id |
| 1119 | * |
| 1120 | * @return mixed |
| 1121 | * @throws QueryExecutionException |
| 1122 | */ |
| 1123 | public function isRecurring($id) |
| 1124 | { |
| 1125 | try { |
| 1126 | $statement = $this->connection->prepare( |
| 1127 | "SELECT |
| 1128 | e.recurringOrder AS event_recurringOrder, |
| 1129 | e.parentId AS event_parentId |
| 1130 | FROM {$this->table} e |
| 1131 | WHERE e.id = :eventId" |
| 1132 | ); |
| 1133 | |
| 1134 | $statement->bindParam(':eventId', $id); |
| 1135 | |
| 1136 | $statement->execute(); |
| 1137 | |
| 1138 | return $statement->fetch(); |
| 1139 | } catch (\Exception $e) { |
| 1140 | throw new QueryExecutionException('Unable to find event by id in ' . __CLASS__, $e->getCode(), $e); |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | |
| 1145 | /** |
| 1146 | * @param int $id |
| 1147 | * @param int $parentId |
| 1148 | * |
| 1149 | * @return mixed |
| 1150 | * @throws QueryExecutionException |
| 1151 | */ |
| 1152 | public function getRecurringIds($id, $parentId) |
| 1153 | { |
| 1154 | $whereParent = empty($parentId) ? '' : ' OR e.parentId = :parentId'; |
| 1155 | try { |
| 1156 | $statement = $this->connection->prepare( |
| 1157 | "SELECT |
| 1158 | e.id AS eventId |
| 1159 | FROM {$this->table} e |
| 1160 | WHERE e.parentId = :eventId" . $whereParent |
| 1161 | ); |
| 1162 | |
| 1163 | $statement->bindParam(':eventId', $id); |
| 1164 | if ($parentId) { |
| 1165 | $statement->bindParam(':parentId', $parentId); |
| 1166 | } |
| 1167 | |
| 1168 | $statement->execute(); |
| 1169 | |
| 1170 | $events = $statement->fetchAll(); |
| 1171 | |
| 1172 | return array_column($events, 'eventId'); |
| 1173 | } catch (\Exception $e) { |
| 1174 | throw new QueryExecutionException('Unable to find event by id in ' . __CLASS__, $e->getCode(), $e); |
| 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | /** |
| 1179 | * @param int $bookingId |
| 1180 | * @param array $criteria |
| 1181 | * |
| 1182 | * @return Event |
| 1183 | * @throws QueryExecutionException |
| 1184 | * @throws InvalidArgumentException |
| 1185 | */ |
| 1186 | public function getByBookingId($bookingId, $criteria = []) |
| 1187 | { |
| 1188 | $eventsPeriodsTable = EventsPeriodsTable::getTableName(); |
| 1189 | |
| 1190 | $customerBookingsEventsPeriods = CustomerBookingsToEventsPeriodsTable::getTableName(); |
| 1191 | |
| 1192 | $fields = ''; |
| 1193 | |
| 1194 | $joins = ''; |
| 1195 | |
| 1196 | if (!empty($criteria['fetchEventsCoupons'])) { |
| 1197 | $couponsTable = CouponsTable::getTableName(); |
| 1198 | |
| 1199 | $fields .= ' |
| 1200 | ec.id AS coupon_id, |
| 1201 | ec.code AS coupon_code, |
| 1202 | ec.discount AS coupon_discount, |
| 1203 | ec.deduction AS coupon_deduction, |
| 1204 | ec.limit AS coupon_limit, |
| 1205 | ec.customerLimit AS coupon_customerLimit, |
| 1206 | ec.status AS coupon_status, |
| 1207 | '; |
| 1208 | |
| 1209 | $joins .= " |
| 1210 | LEFT JOIN {$couponsTable} ec ON ec.id = cb.couponId |
| 1211 | "; |
| 1212 | } |
| 1213 | |
| 1214 | if (!empty($criteria['fetchEventsTickets'])) { |
| 1215 | $ticketsTable = EventsTicketsTable::getTableName(); |
| 1216 | |
| 1217 | $fields .= ' |
| 1218 | eti.id AS ticket_id, |
| 1219 | eti.name AS ticket_name, |
| 1220 | eti.enabled AS ticket_enabled, |
| 1221 | eti.price AS ticket_price, |
| 1222 | eti.spots AS ticket_spots, |
| 1223 | eti.waitingListSpots AS ticket_waiting_list_spots, |
| 1224 | eti.dateRanges AS ticket_dateRanges, |
| 1225 | eti.translations AS ticket_translations, |
| 1226 | '; |
| 1227 | |
| 1228 | $joins .= " |
| 1229 | LEFT JOIN {$ticketsTable} eti ON eti.eventId = e.id |
| 1230 | "; |
| 1231 | } |
| 1232 | |
| 1233 | if (!empty($criteria['fetchEventsTags'])) { |
| 1234 | $tagsTable = EventsTagsTable::getTableName(); |
| 1235 | |
| 1236 | $fields .= ' |
| 1237 | eta.id AS event_tagId, |
| 1238 | eta.name AS event_tagName, |
| 1239 | '; |
| 1240 | |
| 1241 | $joins .= " |
| 1242 | LEFT JOIN {$tagsTable} eta ON eta.eventId = e.id |
| 1243 | "; |
| 1244 | } |
| 1245 | |
| 1246 | if (!empty($criteria['fetchEventsImages'])) { |
| 1247 | $galleriesTable = GalleriesTable::getTableName(); |
| 1248 | |
| 1249 | $fields .= ' |
| 1250 | eg.id AS gallery_id, |
| 1251 | eg.pictureFullPath AS gallery_picture_full, |
| 1252 | eg.pictureThumbPath AS gallery_picture_thumb, |
| 1253 | eg.position AS gallery_position, |
| 1254 | '; |
| 1255 | |
| 1256 | $joins .= " |
| 1257 | LEFT JOIN {$galleriesTable} eg ON eg.entityId = e.id AND eg.entityType = 'event' |
| 1258 | "; |
| 1259 | } |
| 1260 | |
| 1261 | if (!empty($criteria['fetchEventsProviders'])) { |
| 1262 | $eventsProvidersTable = EventsProvidersTable::getTableName(); |
| 1263 | |
| 1264 | $usersTable = UsersTable::getTableName(); |
| 1265 | |
| 1266 | $joins .= " |
| 1267 | LEFT JOIN {$eventsProvidersTable} epr ON epr.eventId = e.id |
| 1268 | LEFT JOIN {$usersTable} pu ON pu.id = epr.userId |
| 1269 | "; |
| 1270 | |
| 1271 | $fields .= ' |
| 1272 | pu.id AS provider_id, |
| 1273 | pu.firstName AS provider_firstName, |
| 1274 | pu.lastName AS provider_lastName, |
| 1275 | pu.email AS provider_email, |
| 1276 | pu.note AS provider_note, |
| 1277 | pu.description AS provider_description, |
| 1278 | pu.phone AS provider_phone, |
| 1279 | pu.gender AS provider_gender, |
| 1280 | pu.pictureFullPath AS provider_pictureFullPath, |
| 1281 | pu.pictureThumbPath AS provider_pictureThumbPath, |
| 1282 | pu.translations AS provider_translations, |
| 1283 | pu.timeZone AS provider_timeZone, |
| 1284 | '; |
| 1285 | } |
| 1286 | |
| 1287 | $fields .= " |
| 1288 | e.id AS event_id, |
| 1289 | e.name AS event_name, |
| 1290 | e.status AS event_status, |
| 1291 | e.bookingOpens AS event_bookingOpens, |
| 1292 | e.bookingCloses AS event_bookingCloses, |
| 1293 | e.recurringCycle AS event_recurringCycle, |
| 1294 | e.recurringOrder AS event_recurringOrder, |
| 1295 | e.recurringInterval AS event_recurringInterval, |
| 1296 | e.recurringUntil AS event_recurringUntil, |
| 1297 | e.bringingAnyone AS event_bringingAnyone, |
| 1298 | e.bookMultipleTimes AS event_bookMultipleTimes, |
| 1299 | e.maxCapacity AS event_maxCapacity, |
| 1300 | e.maxCustomCapacity AS event_maxCustomCapacity, |
| 1301 | e.maxExtraPeople AS event_maxExtraPeople, |
| 1302 | e.price AS event_price, |
| 1303 | e.description AS event_description, |
| 1304 | e.color AS event_color, |
| 1305 | e.show AS event_show, |
| 1306 | e.notifyParticipants AS event_notifyParticipants, |
| 1307 | e.locationId AS event_locationId, |
| 1308 | e.customLocation AS event_customLocation, |
| 1309 | e.customPricing AS event_customPricing, |
| 1310 | e.parentId AS event_parentId, |
| 1311 | e.created AS event_created, |
| 1312 | e.settings AS event_settings, |
| 1313 | e.zoomUserId AS event_zoomUserId, |
| 1314 | e.translations AS event_translations, |
| 1315 | e.deposit AS event_deposit, |
| 1316 | e.depositPayment AS event_depositPayment, |
| 1317 | e.depositPerPerson AS event_depositPerPerson, |
| 1318 | e.fullPayment AS event_fullPayment, |
| 1319 | e.organizerId AS event_organizerId, |
| 1320 | e.aggregatedPrice AS event_aggregatedPrice, |
| 1321 | |
| 1322 | ep.id AS event_periodId, |
| 1323 | ep.periodStart AS event_periodStart, |
| 1324 | ep.periodEnd AS event_periodEnd, |
| 1325 | ep.zoomMeeting AS event_periodZoomMeeting, |
| 1326 | ep.lessonSpace AS event_periodLessonSpace, |
| 1327 | ep.googleCalendarEventId AS event_googleCalendarEventId, |
| 1328 | ep.googleMeetUrl AS event_googleMeetUrl, |
| 1329 | ep.outlookCalendarEventId AS event_outlookCalendarEventId, |
| 1330 | ep.microsoftTeamsUrl AS event_microsoftTeamsUrl, |
| 1331 | ep.appleCalendarEventId AS event_appleCalendarEventId |
| 1332 | "; |
| 1333 | |
| 1334 | $params = [ |
| 1335 | ':customerBookingId' => $bookingId, |
| 1336 | ]; |
| 1337 | |
| 1338 | try { |
| 1339 | $statement = $this->connection->prepare( |
| 1340 | "SELECT |
| 1341 | {$fields} |
| 1342 | FROM {$customerBookingsEventsPeriods} cbe |
| 1343 | INNER JOIN {$eventsPeriodsTable} ep ON ep.id = cbe.eventPeriodId |
| 1344 | INNER JOIN {$this->table} e ON e.id = ep.eventId |
| 1345 | {$joins} |
| 1346 | WHERE cbe.customerBookingId = :customerBookingId" |
| 1347 | ); |
| 1348 | |
| 1349 | $statement->execute($params); |
| 1350 | |
| 1351 | $rows = $statement->fetchAll(); |
| 1352 | } catch (\Exception $e) { |
| 1353 | throw new QueryExecutionException('Unable to find event by booking id in ' . __CLASS__, $e->getCode(), $e); |
| 1354 | } |
| 1355 | |
| 1356 | /** @var Collection $events */ |
| 1357 | $events = call_user_func([static::FACTORY, 'createCollection'], $rows); |
| 1358 | |
| 1359 | return $events->length() ? $events->getItem($events->keys()[0]) : null; |
| 1360 | } |
| 1361 | |
| 1362 | /** |
| 1363 | * @param array $ids |
| 1364 | * @param array $criteria |
| 1365 | * |
| 1366 | * @return Collection |
| 1367 | * @throws QueryExecutionException |
| 1368 | * @throws InvalidArgumentException |
| 1369 | */ |
| 1370 | public function getByIdsWithEntities($ids, $criteria = [], $sort = null) |
| 1371 | { |
| 1372 | $params = []; |
| 1373 | |
| 1374 | $where = []; |
| 1375 | |
| 1376 | $fields = ''; |
| 1377 | |
| 1378 | $joins = ''; |
| 1379 | |
| 1380 | $orderBy = ''; |
| 1381 | |
| 1382 | if (!empty($criteria['fetchEventsPeriods'])) { |
| 1383 | $eventsPeriodsTable = EventsPeriodsTable::getTableName(); |
| 1384 | |
| 1385 | $fields .= ' |
| 1386 | ep.id AS event_periodId, |
| 1387 | ep.periodStart AS event_periodStart, |
| 1388 | ep.periodEnd AS event_periodEnd, |
| 1389 | ep.zoomMeeting AS event_periodZoomMeeting, |
| 1390 | ep.lessonSpace AS event_periodLessonSpace, |
| 1391 | ep.googleCalendarEventId AS event_googleCalendarEventId, |
| 1392 | ep.googleMeetUrl AS event_googleMeetUrl, |
| 1393 | ep.outlookCalendarEventId AS event_outlookCalendarEventId, |
| 1394 | ep.microsoftTeamsUrl AS event_microsoftTeamsUrl, |
| 1395 | ep.appleCalendarEventId AS event_appleCalendarEventId, |
| 1396 | '; |
| 1397 | |
| 1398 | $joins .= " |
| 1399 | INNER JOIN {$eventsPeriodsTable} ep ON ep.eventId = e.id |
| 1400 | "; |
| 1401 | |
| 1402 | $orderBy = $this->getOrderBy($sort); |
| 1403 | } |
| 1404 | |
| 1405 | if (!empty($criteria['fetchEventsLocation'])) { |
| 1406 | $locationsTable = LocationsTable::getTableName(); |
| 1407 | |
| 1408 | $fields .= ' |
| 1409 | l.id AS location_id, |
| 1410 | l.name AS location_name, |
| 1411 | '; |
| 1412 | |
| 1413 | $joins .= " |
| 1414 | LEFT JOIN {$locationsTable} l ON l.id = e.locationId |
| 1415 | "; |
| 1416 | } |
| 1417 | |
| 1418 | if (!empty($criteria['fetchEventsCoupons'])) { |
| 1419 | $couponsTable = CouponsTable::getTableName(); |
| 1420 | |
| 1421 | $fields .= ' |
| 1422 | ec.id AS coupon_id, |
| 1423 | ec.code AS coupon_code, |
| 1424 | ec.discount AS coupon_discount, |
| 1425 | ec.deduction AS coupon_deduction, |
| 1426 | ec.limit AS coupon_limit, |
| 1427 | ec.customerLimit AS coupon_customerLimit, |
| 1428 | ec.status AS coupon_status, |
| 1429 | '; |
| 1430 | |
| 1431 | $joins .= " |
| 1432 | LEFT JOIN {$couponsTable} ec ON ec.id = cb.couponId |
| 1433 | "; |
| 1434 | } |
| 1435 | |
| 1436 | if (!empty($criteria['fetchEventsTickets'])) { |
| 1437 | $ticketsTable = EventsTicketsTable::getTableName(); |
| 1438 | |
| 1439 | $fields .= ' |
| 1440 | eti.id AS ticket_id, |
| 1441 | eti.name AS ticket_name, |
| 1442 | eti.enabled AS ticket_enabled, |
| 1443 | eti.price AS ticket_price, |
| 1444 | eti.spots AS ticket_spots, |
| 1445 | eti.waitingListSpots AS ticket_waiting_list_spots, |
| 1446 | eti.dateRanges AS ticket_dateRanges, |
| 1447 | eti.translations AS ticket_translations, |
| 1448 | '; |
| 1449 | |
| 1450 | $joins .= " |
| 1451 | LEFT JOIN {$ticketsTable} eti ON eti.eventId = e.id |
| 1452 | "; |
| 1453 | |
| 1454 | $orderBy .= (!empty($orderBy) ? ',' : 'ORDER BY') . ' eti.id ASC'; |
| 1455 | } |
| 1456 | |
| 1457 | if (!empty($criteria['fetchEventsTags'])) { |
| 1458 | $tagsTable = EventsTagsTable::getTableName(); |
| 1459 | |
| 1460 | $fields .= ' |
| 1461 | eta.id AS event_tagId, |
| 1462 | eta.name AS event_tagName, |
| 1463 | '; |
| 1464 | |
| 1465 | $joins .= " |
| 1466 | LEFT JOIN {$tagsTable} eta ON eta.eventId = e.id |
| 1467 | "; |
| 1468 | } |
| 1469 | |
| 1470 | if (!empty($criteria['fetchEventsImages'])) { |
| 1471 | $galleriesTable = GalleriesTable::getTableName(); |
| 1472 | |
| 1473 | $fields .= ' |
| 1474 | eg.id AS gallery_id, |
| 1475 | eg.pictureFullPath AS gallery_picture_full, |
| 1476 | eg.pictureThumbPath AS gallery_picture_thumb, |
| 1477 | eg.position AS gallery_position, |
| 1478 | '; |
| 1479 | |
| 1480 | $joins .= " |
| 1481 | LEFT JOIN {$galleriesTable} eg ON eg.entityId = e.id AND eg.entityType = 'event' |
| 1482 | "; |
| 1483 | } |
| 1484 | |
| 1485 | if (!empty($criteria['fetchEventsOrganizer'])) { |
| 1486 | $usersTable = UsersTable::getTableName(); |
| 1487 | |
| 1488 | $joins .= " |
| 1489 | LEFT JOIN {$usersTable} ou ON ou.id = e.organizerId |
| 1490 | "; |
| 1491 | |
| 1492 | $fields .= ' |
| 1493 | ou.id AS organizer_id, |
| 1494 | ou.firstName AS organizer_firstName, |
| 1495 | ou.lastName AS organizer_lastName, |
| 1496 | ou.email AS organizer_email, |
| 1497 | ou.badgeId AS organizer_badgeId, |
| 1498 | ou.pictureThumbPath AS organizer_pictureThumbPath, |
| 1499 | ou.pictureFullPath AS organizer_pictureFullPath, |
| 1500 | '; |
| 1501 | } |
| 1502 | |
| 1503 | if (!empty($criteria['fetchEventsProviders'])) { |
| 1504 | $eventsProvidersTable = EventsProvidersTable::getTableName(); |
| 1505 | |
| 1506 | $usersTable = UsersTable::getTableName(); |
| 1507 | |
| 1508 | $joins .= " |
| 1509 | LEFT JOIN {$eventsProvidersTable} epr ON epr.eventId = e.id |
| 1510 | LEFT JOIN {$usersTable} pu ON pu.id = epr.userId |
| 1511 | "; |
| 1512 | |
| 1513 | $fields .= ' |
| 1514 | pu.id AS provider_id, |
| 1515 | pu.firstName AS provider_firstName, |
| 1516 | pu.lastName AS provider_lastName, |
| 1517 | pu.email AS provider_email, |
| 1518 | pu.note AS provider_note, |
| 1519 | pu.description AS provider_description, |
| 1520 | pu.phone AS provider_phone, |
| 1521 | pu.gender AS provider_gender, |
| 1522 | pu.pictureFullPath AS provider_pictureFullPath, |
| 1523 | pu.pictureThumbPath AS provider_pictureThumbPath, |
| 1524 | pu.translations AS provider_translations, |
| 1525 | pu.timeZone AS provider_timeZone, |
| 1526 | pu.badgeId AS provider_badgeId, |
| 1527 | '; |
| 1528 | } |
| 1529 | |
| 1530 | $fields .= " |
| 1531 | e.id AS event_id, |
| 1532 | e.name AS event_name, |
| 1533 | e.status AS event_status, |
| 1534 | e.bookingOpens AS event_bookingOpens, |
| 1535 | e.bookingCloses AS event_bookingCloses, |
| 1536 | e.bookingOpensRec AS event_bookingOpensRec, |
| 1537 | e.bookingClosesRec AS event_bookingClosesRec, |
| 1538 | e.ticketRangeRec AS event_ticketRangeRec, |
| 1539 | e.recurringCycle AS event_recurringCycle, |
| 1540 | e.recurringOrder AS event_recurringOrder, |
| 1541 | e.recurringInterval AS event_recurringInterval, |
| 1542 | e.recurringMonthly AS event_recurringMonthly, |
| 1543 | e.monthlyDate AS event_monthlyDate, |
| 1544 | e.monthlyOnRepeat AS event_monthlyOnRepeat, |
| 1545 | e.monthlyOnDay AS event_monthlyOnDay, |
| 1546 | e.recurringUntil AS event_recurringUntil, |
| 1547 | e.bringingAnyone AS event_bringingAnyone, |
| 1548 | e.bookMultipleTimes AS event_bookMultipleTimes, |
| 1549 | e.maxCapacity AS event_maxCapacity, |
| 1550 | e.maxCustomCapacity AS event_maxCustomCapacity, |
| 1551 | e.maxExtraPeople AS event_maxExtraPeople, |
| 1552 | e.price AS event_price, |
| 1553 | e.description AS event_description, |
| 1554 | e.color AS event_color, |
| 1555 | e.show AS event_show, |
| 1556 | e.notifyParticipants AS event_notifyParticipants, |
| 1557 | e.locationId AS event_locationId, |
| 1558 | e.customLocation AS event_customLocation, |
| 1559 | e.parentId AS event_parentId, |
| 1560 | e.created AS event_created, |
| 1561 | e.settings AS event_settings, |
| 1562 | e.zoomUserId AS event_zoomUserId, |
| 1563 | e.organizerId AS event_organizerId, |
| 1564 | e.translations AS event_translations, |
| 1565 | e.deposit AS event_deposit, |
| 1566 | e.depositPayment AS event_depositPayment, |
| 1567 | e.depositPerPerson AS event_depositPerPerson, |
| 1568 | e.fullPayment AS event_fullPayment, |
| 1569 | e.customPricing AS event_customPricing, |
| 1570 | e.closeAfterMin AS event_closeAfterMin, |
| 1571 | e.closeAfterMinBookings AS event_closeAfterMinBookings, |
| 1572 | e.aggregatedPrice AS event_aggregatedPrice, |
| 1573 | e.pictureFullPath AS event_pictureFullPath, |
| 1574 | e.pictureThumbPath AS event_pictureThumbPath |
| 1575 | "; |
| 1576 | |
| 1577 | if (!empty($ids)) { |
| 1578 | $queryIds = []; |
| 1579 | |
| 1580 | foreach ($ids as $index => $value) { |
| 1581 | $param = ':id' . $index; |
| 1582 | |
| 1583 | $queryIds[] = $param; |
| 1584 | |
| 1585 | $params[$param] = $value; |
| 1586 | } |
| 1587 | |
| 1588 | $where[] = 'e.id IN (' . implode(', ', $queryIds) . ')'; |
| 1589 | } |
| 1590 | |
| 1591 | $where = $where ? 'WHERE ' . implode(' AND ', $where) : ''; |
| 1592 | |
| 1593 | try { |
| 1594 | $statement = $this->connection->prepare( |
| 1595 | "SELECT |
| 1596 | {$fields} |
| 1597 | FROM {$this->table} e |
| 1598 | {$joins} |
| 1599 | {$where} |
| 1600 | {$orderBy}" |
| 1601 | ); |
| 1602 | |
| 1603 | $statement->execute($params); |
| 1604 | |
| 1605 | $rows = $statement->fetchAll(); |
| 1606 | } catch (\Exception $e) { |
| 1607 | throw new QueryExecutionException('Unable to find event by id in ' . __CLASS__, $e->getCode(), $e); |
| 1608 | } |
| 1609 | |
| 1610 | return call_user_func([static::FACTORY, 'createCollection'], $rows); |
| 1611 | } |
| 1612 | |
| 1613 | /** |
| 1614 | * @param array $criteria |
| 1615 | * |
| 1616 | * @return Collection |
| 1617 | * @throws QueryExecutionException |
| 1618 | * @throws InvalidArgumentException |
| 1619 | */ |
| 1620 | public function getBookingsByCriteria($criteria = []) |
| 1621 | { |
| 1622 | $params = []; |
| 1623 | |
| 1624 | $where = []; |
| 1625 | |
| 1626 | $fields = ''; |
| 1627 | |
| 1628 | $joins = ''; |
| 1629 | |
| 1630 | $eventsPeriodsTable = EventsPeriodsTable::getTableName(); |
| 1631 | |
| 1632 | $customerBookingsEventsPeriods = CustomerBookingsToEventsPeriodsTable::getTableName(); |
| 1633 | |
| 1634 | $customerBookingsTable = CustomerBookingsTable::getTableName(); |
| 1635 | |
| 1636 | if (!empty($criteria['fetchApprovedBookings'])) { |
| 1637 | $where[] = "cb.status = 'approved'"; |
| 1638 | } |
| 1639 | |
| 1640 | if (!empty($criteria['customerId'])) { |
| 1641 | $params[':customerId'] = $criteria['customerId']; |
| 1642 | |
| 1643 | $where[] = 'cb.customerId = :customerId'; |
| 1644 | } |
| 1645 | |
| 1646 | if (!empty($criteria['customerBookingStatus'])) { |
| 1647 | $params[':customerBookingStatus'] = $criteria['customerBookingStatus']; |
| 1648 | |
| 1649 | $where[] = 'cb.status = :customerBookingStatus'; |
| 1650 | } |
| 1651 | |
| 1652 | if (!empty($criteria['customerBookingId'])) { |
| 1653 | $params[':customerBookingId'] = $criteria['customerBookingId']; |
| 1654 | |
| 1655 | $where[] = 'cb.id = :customerBookingId'; |
| 1656 | } |
| 1657 | |
| 1658 | if (!empty($criteria['fetchBookingsPayments'])) { |
| 1659 | $paymentsTable = PaymentsTable::getTableName(); |
| 1660 | |
| 1661 | $fields .= ' |
| 1662 | p.id AS payment_id, |
| 1663 | p.amount AS payment_amount, |
| 1664 | p.dateTime AS payment_dateTime, |
| 1665 | p.created AS payment_created, |
| 1666 | p.status AS payment_status, |
| 1667 | p.gateway AS payment_gateway, |
| 1668 | p.gatewayTitle AS payment_gatewayTitle, |
| 1669 | p.transactionId AS payment_transactionId, |
| 1670 | p.data AS payment_data, |
| 1671 | p.wcOrderId AS payment_wcOrderId, |
| 1672 | p.wcOrderItemId AS payment_wcOrderItemId, |
| 1673 | p.invoiceNumber AS payment_invoiceNumber, |
| 1674 | '; |
| 1675 | |
| 1676 | $joins .= " |
| 1677 | LEFT JOIN {$paymentsTable} p ON p.customerBookingId = cb.id |
| 1678 | "; |
| 1679 | } |
| 1680 | |
| 1681 | if (!empty($criteria['fetchBookingsCoupons'])) { |
| 1682 | $couponsTable = CouponsTable::getTableName(); |
| 1683 | |
| 1684 | $fields .= ' |
| 1685 | c.id AS coupon_id, |
| 1686 | c.code AS coupon_code, |
| 1687 | c.discount AS coupon_discount, |
| 1688 | c.deduction AS coupon_deduction, |
| 1689 | c.limit AS coupon_limit, |
| 1690 | c.customerLimit AS coupon_customerLimit, |
| 1691 | c.status AS coupon_status, |
| 1692 | '; |
| 1693 | |
| 1694 | $joins .= " |
| 1695 | LEFT JOIN {$couponsTable} c ON c.id = cb.couponId |
| 1696 | "; |
| 1697 | } |
| 1698 | |
| 1699 | if (!empty($criteria['fetchBookingsUsers'])) { |
| 1700 | $usersTable = UsersTable::getTableName(); |
| 1701 | |
| 1702 | $fields .= ' |
| 1703 | cu.id AS customer_id, |
| 1704 | cu.type AS customer_type, |
| 1705 | cu.firstName AS customer_firstName, |
| 1706 | cu.lastName AS customer_lastName, |
| 1707 | cu.email AS customer_email, |
| 1708 | cu.note AS customer_note, |
| 1709 | cu.phone AS customer_phone, |
| 1710 | cu.gender AS customer_gender, |
| 1711 | cu.birthday AS customer_birthday, |
| 1712 | cu.customFields AS customer_customFields, |
| 1713 | '; |
| 1714 | |
| 1715 | $joins .= " |
| 1716 | INNER JOIN {$usersTable} cu ON cu.id = cb.customerId |
| 1717 | "; |
| 1718 | } |
| 1719 | |
| 1720 | if (!empty($criteria['fetchBookingsTickets'])) { |
| 1721 | $bookingsTicketsTable = CustomerBookingToEventsTicketsTable::getTableName(); |
| 1722 | |
| 1723 | $fields .= ' |
| 1724 | cbt.id AS booking_ticket_id, |
| 1725 | cbt.eventTicketId AS booking_ticket_eventTicketId, |
| 1726 | cbt.price AS booking_ticket_price, |
| 1727 | cbt.persons AS booking_ticket_persons, |
| 1728 | '; |
| 1729 | |
| 1730 | $joins .= " |
| 1731 | LEFT JOIN {$bookingsTicketsTable} cbt ON cbt.customerBookingId = cb.id |
| 1732 | "; |
| 1733 | } |
| 1734 | |
| 1735 | $fields .= ' |
| 1736 | ep.eventId AS eventId, |
| 1737 | cb.id AS booking_id, |
| 1738 | cb.appointmentId AS booking_appointmentId, |
| 1739 | cb.customerId AS booking_customerId, |
| 1740 | cb.status AS booking_status, |
| 1741 | cb.price AS booking_price, |
| 1742 | cb.tax AS booking_tax, |
| 1743 | cb.persons AS booking_persons, |
| 1744 | cb.couponId AS booking_couponId, |
| 1745 | cb.customFields AS booking_customFields, |
| 1746 | cb.info AS booking_info, |
| 1747 | cb.utcOffset AS booking_utcOffset, |
| 1748 | cb.token AS booking_token, |
| 1749 | cb.aggregatedPrice AS booking_aggregatedPrice, |
| 1750 | cb.tax AS booking_tax, |
| 1751 | cb.qrCodes AS booking_qrCodes |
| 1752 | '; |
| 1753 | |
| 1754 | if (!empty($criteria['ids'])) { |
| 1755 | $queryIds = []; |
| 1756 | |
| 1757 | foreach ($criteria['ids'] as $index => $value) { |
| 1758 | $param = ':id' . $index; |
| 1759 | |
| 1760 | $queryIds[] = $param; |
| 1761 | |
| 1762 | $params[$param] = $value; |
| 1763 | } |
| 1764 | |
| 1765 | $where[] = 'ep.eventId IN (' . implode(', ', $queryIds) . ')'; |
| 1766 | } |
| 1767 | |
| 1768 | $where = $where ? 'WHERE ' . implode(' AND ', $where) : ''; |
| 1769 | |
| 1770 | try { |
| 1771 | $statement = $this->connection->prepare( |
| 1772 | "SELECT |
| 1773 | {$fields} |
| 1774 | FROM {$eventsPeriodsTable} ep |
| 1775 | INNER JOIN {$customerBookingsEventsPeriods} cbe ON cbe.eventPeriodId = ep.id |
| 1776 | INNER JOIN {$customerBookingsTable} cb ON cb.id = cbe.customerBookingId |
| 1777 | {$joins} |
| 1778 | {$where} |
| 1779 | ORDER BY cb.id" |
| 1780 | ); |
| 1781 | |
| 1782 | $statement->execute($params); |
| 1783 | |
| 1784 | $rows = $statement->fetchAll(); |
| 1785 | } catch (\Exception $e) { |
| 1786 | throw new QueryExecutionException('Unable to find event by id in ' . __CLASS__, $e->getCode(), $e); |
| 1787 | } |
| 1788 | |
| 1789 | $reformattedData = []; |
| 1790 | |
| 1791 | foreach ($rows as $row) { |
| 1792 | if (empty($reformattedData[$row['eventId']])) { |
| 1793 | $reformattedData[$row['eventId']] = []; |
| 1794 | } |
| 1795 | |
| 1796 | $reformattedData[$row['eventId']][] = $row; |
| 1797 | } |
| 1798 | |
| 1799 | $result = new Collection(); |
| 1800 | |
| 1801 | foreach ($reformattedData as $eventId => $bookingsData) { |
| 1802 | $reformattedBookingsData = CustomerBookingFactory::reformat($bookingsData); |
| 1803 | |
| 1804 | $eventBookings = new Collection(); |
| 1805 | |
| 1806 | foreach ($reformattedBookingsData as $bookingId => $data) { |
| 1807 | $eventBookings->addItem(CustomerBookingFactory::create($data), $bookingId); |
| 1808 | } |
| 1809 | |
| 1810 | $result->addItem($eventBookings, $eventId); |
| 1811 | } |
| 1812 | |
| 1813 | return $result; |
| 1814 | } |
| 1815 | |
| 1816 | |
| 1817 | /** |
| 1818 | * @param Event $event |
| 1819 | * @param array $booking |
| 1820 | * @param array $limitPerCustomer |
| 1821 | * @return int |
| 1822 | * @throws QueryExecutionException |
| 1823 | * @throws InvalidArgumentException |
| 1824 | */ |
| 1825 | public function getRelevantBookingsCount($event, $booking, $limitPerCustomer) |
| 1826 | { |
| 1827 | $eventsPeriodsTable = EventsPeriodsTable::getTableName(); |
| 1828 | |
| 1829 | $customerBookingsEventsPeriods = CustomerBookingsToEventsPeriodsTable::getTableName(); |
| 1830 | |
| 1831 | $customerBookingsTable = CustomerBookingsTable::getTableName(); |
| 1832 | |
| 1833 | $params = [ |
| 1834 | ':customerId' => $booking['customerId'] |
| 1835 | ]; |
| 1836 | |
| 1837 | $paymentTableJoin = ''; |
| 1838 | $compareToDate = 'ep.periodStart'; |
| 1839 | |
| 1840 | if ($limitPerCustomer['from'] === 'bookingDate') { |
| 1841 | $eventStartDate = |
| 1842 | (clone $event->getPeriods()->getItems()[0]->getPeriodStart()->getValue())->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i'); |
| 1843 | } else { |
| 1844 | $paymentTableJoin = 'INNER JOIN ' . PaymentsTable::getTableName() . ' p ON p.customerBookingId = cb.id'; |
| 1845 | $eventStartDate = DateTimeService::getNowDateTimeObject()->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i'); |
| 1846 | $compareToDate = 'p.created'; |
| 1847 | } |
| 1848 | |
| 1849 | $intervalString = "interval " . $limitPerCustomer['period'] . " " . $limitPerCustomer['timeFrame']; |
| 1850 | |
| 1851 | $where = "(STR_TO_DATE('" . $eventStartDate . "', '%Y-%m-%d %H:%i:%s') BETWEEN " . |
| 1852 | "(" . $compareToDate . " - " . $intervalString . " + interval 1 second)" . |
| 1853 | " AND (" . |
| 1854 | $compareToDate . " + " . $intervalString . " - interval 1 second))"; |
| 1855 | |
| 1856 | try { |
| 1857 | $statement = $this->connection->prepare( |
| 1858 | "SELECT COUNT(DISTINCT cb.id) AS count FROM |
| 1859 | {$this->table} e |
| 1860 | INNER JOIN {$eventsPeriodsTable} ep ON ep.eventId = e.id |
| 1861 | INNER JOIN {$customerBookingsEventsPeriods} cbep ON cbep.eventPeriodId = ep.id |
| 1862 | INNER JOIN {$customerBookingsTable} cb ON cb.id = cbep.customerBookingId |
| 1863 | {$paymentTableJoin} |
| 1864 | WHERE cb.customerId = :customerId AND {$where} AND e.status = 'approved' AND cb.status = 'approved' |
| 1865 | " |
| 1866 | ); |
| 1867 | |
| 1868 | $statement->execute($params); |
| 1869 | |
| 1870 | $rows = $statement->fetch()['count']; |
| 1871 | } catch (\Exception $e) { |
| 1872 | throw new QueryExecutionException('Unable to find by id in ' . __CLASS__, $e->getCode(), $e); |
| 1873 | } |
| 1874 | |
| 1875 | return $rows; |
| 1876 | } |
| 1877 | |
| 1878 | |
| 1879 | /** |
| 1880 | * @param string $sort |
| 1881 | * @return string |
| 1882 | */ |
| 1883 | private function getOrderBy($sort) |
| 1884 | { |
| 1885 | $order = "ORDER BY ep.periodStart"; |
| 1886 | if ($sort) { |
| 1887 | $column = $sort[0] === '-' ? substr($sort, 1) : $sort; |
| 1888 | $orderColumn = 'ep.periodStart'; |
| 1889 | switch ($column) { |
| 1890 | case 'name': |
| 1891 | $orderColumn = 'e.name'; |
| 1892 | break; |
| 1893 | case 'id': |
| 1894 | $orderColumn = 'e.id'; |
| 1895 | break; |
| 1896 | case 'bookingOpens': |
| 1897 | $orderColumn = 'COALESCE(e.bookingOpens, e.created)'; |
| 1898 | break; |
| 1899 | case 'bookingCloses': |
| 1900 | $orderColumn = 'COALESCE(e.bookingCloses, ep.periodStart)'; |
| 1901 | break; |
| 1902 | } |
| 1903 | |
| 1904 | $orderDirection = $sort[0] === '-' ? 'DESC' : 'ASC'; |
| 1905 | |
| 1906 | $order = "ORDER BY {$orderColumn} {$orderDirection}"; |
| 1907 | } |
| 1908 | |
| 1909 | return $order; |
| 1910 | } |
| 1911 | |
| 1912 | |
| 1913 | /** |
| 1914 | * @param Event $event |
| 1915 | * @param int $visible |
| 1916 | * @param boolean $applyGlobally |
| 1917 | * @throws QueryExecutionException |
| 1918 | * @throws InvalidArgumentException |
| 1919 | */ |
| 1920 | public function toggleEventVisibility($event, $visible, $applyGlobally) |
| 1921 | { |
| 1922 | $params = [ |
| 1923 | ':show' => $visible |
| 1924 | ]; |
| 1925 | |
| 1926 | if ($applyGlobally) { |
| 1927 | $params[':id1'] = $event->getId()->getValue(); |
| 1928 | $params[':id2'] = $event->getId()->getValue(); |
| 1929 | |
| 1930 | $where = "WHERE id = :id1 OR parentId = :id2"; |
| 1931 | if ($event->getParentId()) { |
| 1932 | $params[':id3'] = $event->getId()->getValue(); |
| 1933 | $params[':parentId'] = $event->getParentId()->getValue(); |
| 1934 | $where .= ' OR (id > :id3 AND parentId = :parentId)'; |
| 1935 | } |
| 1936 | } else { |
| 1937 | $params[':id'] = $event->getId()->getValue(); |
| 1938 | $where = 'WHERE id = :id'; |
| 1939 | } |
| 1940 | |
| 1941 | try { |
| 1942 | $statement = $this->connection->prepare( |
| 1943 | "UPDATE {$this->table} |
| 1944 | SET |
| 1945 | `show` = :show |
| 1946 | $where" |
| 1947 | ); |
| 1948 | |
| 1949 | $res = $statement->execute($params); |
| 1950 | |
| 1951 | if (!$res) { |
| 1952 | throw new QueryExecutionException('Unable to update visibility in ' . __CLASS__); |
| 1953 | } |
| 1954 | |
| 1955 | return $res; |
| 1956 | } catch (\Exception $e) { |
| 1957 | throw new QueryExecutionException('Unable to update visibility in ' . __CLASS__, $e->getCode(), $e); |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | /** |
| 1962 | * @param int $organizerId |
| 1963 | */ |
| 1964 | public function removeOrganizerFromEvents($organizerId) |
| 1965 | { |
| 1966 | $params = [ |
| 1967 | ':organizerId' => $organizerId |
| 1968 | ]; |
| 1969 | |
| 1970 | |
| 1971 | try { |
| 1972 | $statement = $this->connection->prepare( |
| 1973 | "UPDATE {$this->table} |
| 1974 | SET |
| 1975 | organizerId = NULL |
| 1976 | WHERE organizerId = :organizerId" |
| 1977 | ); |
| 1978 | |
| 1979 | $res = $statement->execute($params); |
| 1980 | |
| 1981 | if (!$res) { |
| 1982 | throw new QueryExecutionException('Unable to remove organizer in ' . __CLASS__); |
| 1983 | } |
| 1984 | |
| 1985 | return $res; |
| 1986 | } catch (\Exception $e) { |
| 1987 | throw new QueryExecutionException('Unable to remove organizer in ' . __CLASS__, $e->getCode(), $e); |
| 1988 | } |
| 1989 | } |
| 1990 | } |
| 1991 |