PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / EventTickets / Actions / AttachAttendeeDataToTicketData.php

AttachAttendeeDataToTicketData.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/EventTickets/Actions/AttachAttendeeDataToTicketData.php

77 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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