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 / ConvertEventTicketsBlockToFieldsApi.php

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

62 lines 2.5 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\Donations\Models\Donation;
6 use Give\EventTickets\DataTransferObjects\EventTicketTypeData;
7 use Give\EventTickets\DataTransferObjects\TicketPurchaseData;
8 use Give\EventTickets\Fields\EventTickets;
9 use Give\EventTickets\Repositories\EventRepository;
10 use Give\EventTickets\Repositories\EventTicketRepository;
11 use Give\Framework\Blocks\BlockModel;
12 use Give\Framework\FieldsAPI\Exceptions\EmptyNameException;
13 use Give\Framework\Support\ValueObjects\Money;
14
15 class ConvertEventTicketsBlockToFieldsApi
16 {
17 /**
18 * @since 4.16.8.1 Resolve a purchased ticket type only among the ticket types of this block's own event, and reject it if that event has already ended.
19 * @since 4.6.0 Add support for currency conversion
20 * @since 3.20.0 Set event end date and time.
21 * @since 3.12.2 Remove event ID from field name
22 * @since 3.6.0
23 *
24 * @throws EmptyNameException
25 */
26 public function __invoke(BlockModel $block, int $formId)
27 {
28 return EventTickets::make($block->getShortName())
29 ->tap(function (EventTickets $eventTicketsField) use ($block, $formId) {
30 $eventId = $block->getAttribute('eventId');
31 $event = give(EventRepository::class)->getById($eventId);
32 $ticketTypes = array_map(function ($ticketType) {
33 return EventTicketTypeData::make($ticketType)->toArray();
34 }, $event->ticketTypes()->getAll() ?? []);
35
36 $eventTicketsField
37 ->title($event->title)
38 ->startDateTime($event->startDateTime->format('Y-m-d H:i:s'))
39 ->endDateTime($event->endDateTime->format('Y-m-d H:i:s'))
40 ->description($event->description)
41 ->ticketTypes($ticketTypes);
42
43 $eventTicketsField->scope(function (EventTickets $field, $value, Donation $donation) use ($event) {
44
45 $ticketPurchaseData = array_map(function ($data) use ($event) {
46 return TicketPurchaseData::fromFieldValueObject($data, $event);
47 }, json_decode($value));
48
49 array_walk($ticketPurchaseData, new GenerateTicketsFromPurchaseData($donation));
50
51 $totalTicketsAmount = give(EventTicketRepository::class)->getTotalByDonation($donation);
52
53 $donation->amount = $donation->amount->add($totalTicketsAmount);
54
55 $donation->save();
56 });
57
58 return $eventTicketsField;
59 });
60 }
61 }
62