AddEventTicketsToDonationConfirmationPageDonationTotal.php
2 years ago
AddEventTicketsToDonationConfirmationPageEventTicketDetails.php
2 years ago
AttachAttendeeDataToTicketData.php
2 years ago
ConvertEventTicketsBlockToFieldsApi.php
2 years ago
EnqueueDonationFormScripts.php
2 years ago
EnqueueEventDetailsScripts.php
2 years ago
EnqueueFormBuilderScripts.php
2 years ago
EnqueueListTableScripts.php
2 years ago
GenerateTicketsFromPurchaseData.php
2 years ago
RegisterEventsMenuItem.php
2 years ago
RenderDonationFormBlock.php
2 years ago
UpdateDonationConfirmationPageReceiptDonationAmount.php
2 years ago
AttachAttendeeDataToTicketData.php
77 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\EventTickets\Actions; |
| 4 | |
| 5 | use Give\EventTickets\Models\EventTicket; |
| 6 | use Give\Framework\QueryBuilder\QueryBuilder; |
| 7 | |
| 8 | /** |
| 9 | * @since 3.6.0 |
| 10 | */ |
| 11 | class AttachAttendeeDataToTicketData |
| 12 | { |
| 13 | /** |
| 14 | * @since 3.6.0 |
| 15 | * @var array |
| 16 | */ |
| 17 | protected $attendeeDataLookup; |
| 18 | |
| 19 | /** |
| 20 | * @since 3.6.0 |
| 21 | * @param EventTicket[] $tickets |
| 22 | */ |
| 23 | public function __construct(array $tickets) |
| 24 | { |
| 25 | $this->attendeeDataLookup = array_reduce($this->getAttendeeDataForTickets($tickets), function ($lookup, $data) { |
| 26 | $lookup[$data->donationId] = ['name' => $data->attendeeName, 'email' => $data->attendeeEmail]; |
| 27 | return $lookup; |
| 28 | }, []); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @since 3.6.0 |
| 33 | */ |
| 34 | public function __invoke(EventTicket $ticket): array |
| 35 | { |
| 36 | return array_merge($ticket->toArray(), [ |
| 37 | 'attendee' => $this->attendeeDataLookup[$ticket->donationId] ?? null, |
| 38 | ]); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * This query relates donors names to tickets through donations. |
| 43 | * |
| 44 | * @since 3.6.0 |
| 45 | * |
| 46 | * @param EventTicket[] $tickets |
| 47 | */ |
| 48 | protected function getAttendeeDataForTickets(array $tickets): array |
| 49 | { |
| 50 | if (empty($tickets)) { |
| 51 | return []; |
| 52 | } |
| 53 | |
| 54 | return (new QueryBuilder) |
| 55 | ->from('posts', 'Donation') |
| 56 | ->select( |
| 57 | ['Donation.ID', 'donationId'], |
| 58 | ['Donor.name', 'attendeeName'], |
| 59 | ['Donor.email', 'attendeeEmail'] |
| 60 | ) |
| 61 | ->join(function($builder) { |
| 62 | $builder |
| 63 | ->leftJoin('give_donationmeta', $tableAlias = 'DonationDonorId' ) |
| 64 | ->on('Donation.ID', "DonationDonorId.donation_id") |
| 65 | ->andOn("DonationDonorId.meta_key", '_give_payment_donor_id', true); |
| 66 | }) |
| 67 | ->join(function($builder) { |
| 68 | $builder |
| 69 | ->leftJoin('give_donors', $tableAlias = 'Donor' ) |
| 70 | ->on('DonationDonorId.meta_value', "Donor.id"); |
| 71 | }) |
| 72 | ->where('post_type', 'give_payment') |
| 73 | ->whereIn('Donation.ID', array_column($tickets, 'donationId')) |
| 74 | ->getAll(); |
| 75 | } |
| 76 | } |
| 77 |