| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @copyright © Melograno Venture Studio. All rights reserved. |
| 5 |
* @licence See LICENCE.md for license details. |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace IvyForms\Repository\Amelia; |
| 9 |
|
| 10 |
/** |
| 11 |
* Repository for Amelia booking data linked to IvyForms entries. |
| 12 |
*/ |
| 13 |
class AmeliaRepository |
| 14 |
{ |
| 15 |
/** @var \wpdb */ |
| 16 |
private $wpdb; |
| 17 |
|
| 18 |
public function __construct() |
| 19 |
{ |
| 20 |
global $wpdb; |
| 21 |
$this->wpdb = $wpdb; |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Fetch Amelia bookings linked to an IvyForms entry. |
| 26 |
* |
| 27 |
* @return array<int, array{ |
| 28 |
* appointmentId: int|string|null, |
| 29 |
* id: int|string|null, |
| 30 |
* purchaseId: int|string|null |
| 31 |
* }> |
| 32 |
*/ |
| 33 |
public function getBookingsByEntryId(int $entryId): array |
| 34 |
{ |
| 35 |
$prefix = $this->wpdb->prefix; |
| 36 |
$customerBookingsTable = $prefix . 'amelia_customer_bookings'; |
| 37 |
$packagesToCustomersTable = $prefix . 'amelia_packages_to_customers'; |
| 38 |
|
| 39 |
if ( |
| 40 |
!$this->tableExists($customerBookingsTable) |
| 41 |
|| !$this->tableExists($packagesToCustomersTable) |
| 42 |
) { |
| 43 |
return []; |
| 44 |
} |
| 45 |
|
| 46 |
$query = $this->wpdb->prepare( |
| 47 |
"SELECT appointmentId, id, NULL AS purchaseId |
| 48 |
FROM {$customerBookingsTable} |
| 49 |
WHERE ivyEntryId = %d |
| 50 |
UNION ALL |
| 51 |
SELECT NULL AS appointmentId, NULL AS id, id AS purchaseId |
| 52 |
FROM {$packagesToCustomersTable} |
| 53 |
WHERE ivyEntryId = %d", |
| 54 |
$entryId, |
| 55 |
$entryId |
| 56 |
); |
| 57 |
|
| 58 |
$rows = $this->wpdb->get_results($query, ARRAY_A); |
| 59 |
|
| 60 |
return is_array($rows) ? $rows : []; |
| 61 |
} |
| 62 |
|
| 63 |
private function tableExists(string $tableName): bool |
| 64 |
{ |
| 65 |
$like = $this->wpdb->esc_like($tableName); |
| 66 |
$result = $this->wpdb->get_var( |
| 67 |
$this->wpdb->prepare('SHOW TABLES LIKE %s', $like) |
| 68 |
); |
| 69 |
|
| 70 |
return $result === $tableName; |
| 71 |
} |
| 72 |
} |
| 73 |
|