| 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\Services\Amelia; |
| 9 |
|
| 10 |
use IvyForms\Repository\Amelia\AmeliaRepository; |
| 11 |
use IvyForms\Services\Integrations\PluginInstaller; |
| 12 |
|
| 13 |
/** |
| 14 |
* Service for Amelia booking URLs linked to IvyForms entries. |
| 15 |
*/ |
| 16 |
class AmeliaService |
| 17 |
{ |
| 18 |
private const AMELIA_PLUGIN_SLUG = 'ameliabooking'; |
| 19 |
|
| 20 |
private AmeliaRepository $ameliaRepository; |
| 21 |
private PluginInstaller $pluginInstaller; |
| 22 |
|
| 23 |
public function __construct(AmeliaRepository $ameliaRepository, PluginInstaller $pluginInstaller) |
| 24 |
{ |
| 25 |
$this->ameliaRepository = $ameliaRepository; |
| 26 |
$this->pluginInstaller = $pluginInstaller; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Build Amelia admin booking URLs for the given entry. |
| 31 |
* |
| 32 |
* @return array<int, array{bookingId: int|null, packageCustomerId: int|null, url: string}> |
| 33 |
*/ |
| 34 |
public function getBookingUrlsByEntryId(int $entryId): array |
| 35 |
{ |
| 36 |
if (!$this->isAmeliaIntegrationEnabled()) { |
| 37 |
return []; |
| 38 |
} |
| 39 |
|
| 40 |
$urls = []; |
| 41 |
|
| 42 |
foreach ($this->ameliaRepository->getBookingsByEntryId($entryId) as $booking) { |
| 43 |
$bookingId = !empty($booking['id']) ? (int) $booking['id'] : null; |
| 44 |
|
| 45 |
$packageCustomerId = !empty($booking['purchaseId']) ? (int) $booking['purchaseId'] : null; |
| 46 |
|
| 47 |
$urls[] = [ |
| 48 |
'bookingId' => $bookingId, |
| 49 |
'packageCustomerId' => $packageCustomerId, |
| 50 |
'url' => $this->buildBookingUrl($booking, $bookingId, $packageCustomerId), |
| 51 |
]; |
| 52 |
} |
| 53 |
|
| 54 |
return $urls; |
| 55 |
} |
| 56 |
|
| 57 |
private function isAmeliaIntegrationEnabled(): bool |
| 58 |
{ |
| 59 |
$pluginFile = $this->pluginInstaller->getPluginFile(self::AMELIA_PLUGIN_SLUG); |
| 60 |
|
| 61 |
if ( |
| 62 |
$pluginFile === null |
| 63 |
|| !$this->pluginInstaller->isPluginInstalled($pluginFile) |
| 64 |
|| !$this->pluginInstaller->isPluginActive(self::AMELIA_PLUGIN_SLUG) |
| 65 |
) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
$ameliaSettings = json_decode(get_option('amelia_settings'), true); |
| 70 |
|
| 71 |
return !empty($ameliaSettings['featuresIntegrations']['ivy']['enabled']); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* @param array{ |
| 76 |
* appointmentId: int|string|null, |
| 77 |
* id: int|string|null, |
| 78 |
* purchaseId: int|string|null |
| 79 |
* } $booking |
| 80 |
*/ |
| 81 |
private function buildBookingUrl(array $booking, ?int $bookingId, ?int $packageCustomerId): string |
| 82 |
{ |
| 83 |
$appointmentId = $booking['appointmentId'] ?? null; |
| 84 |
|
| 85 |
if (empty($appointmentId) && !empty($bookingId)) { |
| 86 |
return admin_url( |
| 87 |
'admin.php?page=wpamelia-bookings&booking=' . $bookingId |
| 88 |
) . '#/events'; |
| 89 |
} |
| 90 |
|
| 91 |
if (!empty($packageCustomerId)) { |
| 92 |
return admin_url( |
| 93 |
'admin.php?page=wpamelia-bookings&id=' . $packageCustomerId |
| 94 |
) . '#/packages'; |
| 95 |
} |
| 96 |
|
| 97 |
return admin_url( |
| 98 |
'admin.php?page=wpamelia-bookings&appointment=' . (int) $appointmentId |
| 99 |
. '&booking=' . (int) $bookingId |
| 100 |
) . '#/appointments'; |
| 101 |
} |
| 102 |
} |
| 103 |
|