AuthenticationData.php
2 years ago
DonateControllerData.php
2 years ago
DonateFormRouteData.php
1 year ago
DonateRouteData.php
2 years ago
DonationConfirmationReceiptViewRouteData.php
2 years ago
DonationFormGoalData.php
2 years ago
DonationFormPreviewRouteData.php
2 years ago
DonationFormQueryData.php
2 years ago
DonationFormViewRouteData.php
2 years ago
LegacyPurchaseFormData.php
2 years ago
UserData.php
2 years ago
ValidationRouteData.php
2 years ago
DonationFormGoalData.php
143 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\DataTransferObjects; |
| 4 | |
| 5 | use Give\DonationForms\DonationQuery; |
| 6 | use Give\DonationForms\Models\DonationForm; |
| 7 | use Give\DonationForms\Properties\FormSettings; |
| 8 | use Give\DonationForms\Repositories\DonationFormRepository; |
| 9 | use Give\DonationForms\SubscriptionQuery; |
| 10 | use Give\DonationForms\ValueObjects\GoalProgressType; |
| 11 | use Give\DonationForms\ValueObjects\GoalType; |
| 12 | use Give\Framework\Support\Contracts\Arrayable; |
| 13 | |
| 14 | /** |
| 15 | * @since 3.0.0 |
| 16 | */ |
| 17 | class DonationFormGoalData implements Arrayable |
| 18 | { |
| 19 | /** |
| 20 | * @var int |
| 21 | */ |
| 22 | public $formId; |
| 23 | /** |
| 24 | * @var array |
| 25 | */ |
| 26 | public $formSettings; |
| 27 | /** |
| 28 | * @var false |
| 29 | */ |
| 30 | public $isEnabled; |
| 31 | /** |
| 32 | * @var GoalType |
| 33 | */ |
| 34 | public $goalType; |
| 35 | /** |
| 36 | * @var int |
| 37 | */ |
| 38 | public $targetAmount; |
| 39 | /** |
| 40 | * @var GoalProgressType |
| 41 | */ |
| 42 | public $goalProgressType; |
| 43 | /** |
| 44 | * @var string|null |
| 45 | */ |
| 46 | public $goalStartDate; |
| 47 | /** |
| 48 | * @var string|null |
| 49 | */ |
| 50 | public $goalEndDate; |
| 51 | |
| 52 | /** |
| 53 | * @since 3.0.0 |
| 54 | */ |
| 55 | public function __construct(int $formId, FormSettings $formSettings) |
| 56 | { |
| 57 | $this->formId = $formId; |
| 58 | $this->formSettings = $formSettings; |
| 59 | $this->isEnabled = $formSettings->enableDonationGoal ?? false; |
| 60 | $this->goalType = $formSettings->goalType ?? GoalType::AMOUNT(); |
| 61 | $this->targetAmount = $this->formSettings->goalAmount ?? 0; |
| 62 | $this->goalProgressType = $this->formSettings->goalProgressType ?? GoalProgressType::ALL_TIME(); |
| 63 | $this->goalStartDate = $this->formSettings->goalStartDate ?? null; |
| 64 | $this->goalEndDate = $this->formSettings->goalEndDate ?? null; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @since 3.0.0 |
| 69 | * |
| 70 | * @return int|float |
| 71 | */ |
| 72 | public function getCurrentAmount() |
| 73 | { |
| 74 | $query = $this->goalType->isOneOf(GoalType::SUBSCRIPTIONS(), GoalType::AMOUNT_FROM_SUBSCRIPTIONS(), GoalType::DONORS_FROM_SUBSCRIPTIONS()) |
| 75 | ? new SubscriptionQuery() |
| 76 | : new DonationQuery(); |
| 77 | |
| 78 | $query->form($this->formId); |
| 79 | |
| 80 | if($this->goalProgressType->isCustom()) { |
| 81 | $query->between($this->goalStartDate, $this->goalEndDate); |
| 82 | } |
| 83 | |
| 84 | switch ($this->goalType): |
| 85 | case GoalType::DONORS(): |
| 86 | return $query->countDonors(); |
| 87 | case GoalType::DONATIONS(): |
| 88 | return $query->count(); |
| 89 | case GoalType::SUBSCRIPTIONS(): |
| 90 | return $query->count(); |
| 91 | case GoalType::AMOUNT_FROM_SUBSCRIPTIONS(): |
| 92 | return $query->sumInitialAmount(); |
| 93 | case GoalType::DONORS_FROM_SUBSCRIPTIONS(): |
| 94 | return $query->countDonors(); |
| 95 | case GoalType::AMOUNT(): |
| 96 | default: |
| 97 | return $query->sumIntendedAmount(); |
| 98 | endswitch; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @since 3.0.0 |
| 103 | */ |
| 104 | public function getLabel(): string |
| 105 | { |
| 106 | if ($this->goalType->isDonors() || $this->goalType->isDonorsFromSubscriptions()) { |
| 107 | return __('donors', 'give'); |
| 108 | } |
| 109 | |
| 110 | if ($this->goalType->isDonations()) { |
| 111 | return __('donations', 'give'); |
| 112 | } |
| 113 | |
| 114 | if ($this->goalType->isSubscriptions()) { |
| 115 | return __('recurring donations', 'give'); |
| 116 | } |
| 117 | |
| 118 | return __('amount', 'give'); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @since 3.0.0 |
| 123 | */ |
| 124 | public function toArray(): array |
| 125 | { |
| 126 | $currentAmount = $this->getCurrentAmount(); |
| 127 | $progressPercentage = !$currentAmount || !$this->targetAmount ? 0 : ($currentAmount / $this->targetAmount) * 100; |
| 128 | $goalTypeIsAmount = $this->goalType->isOneOf(GoalType::AMOUNT(), GoalType::AMOUNT_FROM_SUBSCRIPTIONS()); |
| 129 | |
| 130 | return [ |
| 131 | 'type' => $this->goalType->getValue(), |
| 132 | 'typeIsCount' => !$goalTypeIsAmount, |
| 133 | 'typeIsMoney' => $goalTypeIsAmount, |
| 134 | 'enabled' => $this->isEnabled, |
| 135 | 'show' => $this->isEnabled, |
| 136 | 'currentAmount' => $currentAmount, |
| 137 | 'targetAmount' => $this->targetAmount, |
| 138 | 'label' => $this->getLabel(), |
| 139 | 'isAchieved' => $this->isEnabled && $this->formSettings->enableAutoClose && $progressPercentage >= 100 |
| 140 | ]; |
| 141 | } |
| 142 | } |
| 143 |