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 / DonationForms / DataTransferObjects / DonationFormGoalData.php

DonationFormGoalData.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/DonationForms/DataTransferObjects/DonationFormGoalData.php

289 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\DonationForms\DataTransferObjects;
4
5 use Give\Campaigns\CampaignDonationQuery;
6 use Give\Campaigns\Models\Campaign;
7 use Give\Campaigns\ValueObjects\CampaignGoalType;
8 use Give\DonationForms\DonationQuery;
9 use Give\DonationForms\Properties\FormSettings;
10 use Give\DonationForms\SubscriptionQuery;
11 use Give\DonationForms\ValueObjects\GoalProgressType;
12 use Give\DonationForms\ValueObjects\GoalSource;
13 use Give\DonationForms\ValueObjects\GoalType;
14 use Give\Framework\Support\Contracts\Arrayable;
15
16 /**
17 * @since 4.1.0 added goalSource
18 * @since 3.0.0
19 */
20 class DonationFormGoalData implements Arrayable
21 {
22 /**
23 * @var int
24 */
25 public $formId;
26 /**
27 * @var FormSettings
28 */
29 public $formSettings;
30 /**
31 * @var false
32 */
33 public $isEnabled;
34 /**
35 * @var GoalType
36 */
37 public $goalType;
38 /**
39 * @var int
40 */
41 public $targetAmount;
42 /**
43 * @var GoalProgressType
44 */
45 public $goalProgressType;
46 /**
47 * @var string|null
48 */
49 public $goalStartDate;
50 /**
51 * @var string|null
52 */
53 public $goalEndDate;
54 /**
55 * @since 4.1.0
56 *
57 * @var Campaign|null
58 */
59 public $campaign;
60 /**
61 * @var GoalSource $goalSource
62 */
63 public $goalSource;
64
65 /**
66 * @since 3.0.0
67 */
68 public function __construct(int $formId, FormSettings $formSettings)
69 {
70 $this->formId = $formId;
71 $this->formSettings = $formSettings;
72 $this->isEnabled = $formSettings->enableDonationGoal ?? false;
73 $this->goalType = $formSettings->goalType ?? GoalType::AMOUNT();
74 $this->goalSource = $formSettings->goalSource ?? GoalSource::CAMPAIGN();
75 $this->targetAmount = $this->formSettings->goalAmount ?? 0;
76 $this->goalProgressType = $this->formSettings->goalProgressType ?? GoalProgressType::ALL_TIME();
77 $this->goalStartDate = $this->formSettings->goalStartDate ?? null;
78 $this->goalEndDate = $this->formSettings->goalEndDate ?? null;
79 $this->campaign = Campaign::findByFormId($this->formId);
80 }
81
82 /**
83 * @since 4.1.0 switch between Campaign goal and Form goal
84 * @since 3.0.0
85 *
86 * @return int|float
87 */
88 public function getCurrentAmount()
89 {
90 switch ($this->getGoalType()->getValue()):
91 case 'donors':
92 case 'donorsFromSubscriptions':
93 return $this->getQuery()->countDonors();
94 case 'donations':
95 return $this->goalSource->isCampaign()
96 ? $this->getQuery()->countDonations()
97 : $this->getQuery()->count();
98 case 'subscriptions':
99 return $this->getQuery()->count();
100 case 'amountFromSubscriptions':
101 return $this->getQuery()->sumInitialAmount();
102 default:
103 return $this->getQuery()->sumIntendedAmount();
104 endswitch;
105 }
106
107 /**
108 * Check if goal type is subscription
109 *
110 * @since 4.1.0
111 */
112 public function isSubscription(): bool
113 {
114 return in_array(
115 $this->getGoalType()->getValue(), [
116 'subscriptions',
117 'amountFromSubscriptions',
118 'donorsFromSubscriptions',
119 ], true);
120 }
121
122 /**
123 * @since 4.1.0
124 */
125 public function getQuery()
126 {
127 return $this->goalSource->isCampaign()
128 ? $this->getCampaignQuery()
129 : $this->getFormQuery();
130 }
131
132 /**
133 * Get Campaign query
134 *
135 * @since 4.1.0
136 *
137 * @return CampaignDonationQuery|SubscriptionQuery
138 */
139 private function getCampaignQuery()
140 {
141 if ($this->isSubscription()) {
142 $query = new SubscriptionQuery();
143
144 $ids = array_map(function ($form) {
145 return $form->id;
146 }, $this->campaign->forms()->getAll());
147
148 $query->forms($ids);
149
150 return $query;
151 }
152
153 return new CampaignDonationQuery($this->campaign);
154 }
155
156 /**
157 * Get Form query
158 *
159 * @since 4.7.0 add support for date range
160 * @since 4.1.0
161 *
162 * @return DonationQuery|SubscriptionQuery
163 */
164 private function getFormQuery()
165 {
166 $query = $this->isSubscription()
167 ? new SubscriptionQuery()
168 : new DonationQuery();
169
170 if ($this->goalStartDate && $this->goalEndDate) {
171 $query->between($this->goalStartDate, $this->goalEndDate);
172 }
173
174 if ($this->goalStartDate && !$this->goalEndDate) {
175 $query->between($this->goalStartDate, current_datetime()->format('Y-m-d'));
176 }
177
178 $query->form($this->formId);
179
180 return $query;
181 }
182
183 /**
184 * Get goal type
185 *
186 * @return CampaignGoalType|GoalType
187 */
188 public function getGoalType()
189 {
190 return $this->goalSource->isCampaign()
191 ? $this->campaign->goalType
192 : $this->goalType;
193 }
194
195 /**
196 * Get target amount
197 *
198 * @since 4.1.0
199 *
200 * @return float|int
201 */
202 public function getTargetAmount()
203 {
204 return $this->goalSource->isCampaign()
205 ? $this->campaign->goal ?? 0
206 : $this->targetAmount;
207 }
208
209 /**
210 * Check if goal type is an amount
211 *
212 * @since 4.1.0
213 */
214 public function isAmount(): bool
215 {
216 return $this->getGoalType()->isOneOf(
217 $this->getGoalType()::AMOUNT(),
218 $this->getGoalType()::AMOUNT_FROM_SUBSCRIPTIONS()
219 );
220 }
221
222 /**
223 * @since 3.0.0
224 */
225 public function getLabel(): string
226 {
227 if ($this->getGoalType()->isDonors() || $this->getGoalType()->isDonorsFromSubscriptions()) {
228 return __('donors', 'give');
229 }
230
231 if ($this->getGoalType()->isDonations()) {
232 return __('donations', 'give');
233 }
234
235 if ($this->getGoalType()->isSubscriptions()) {
236 return __('recurring donations', 'give');
237 }
238
239 return __('amount', 'give');
240 }
241
242 /**
243 * @since 4.2.0 add percentage value
244 * @since 3.0.0
245 */
246 public function toArray(): array
247 {
248 $currentAmount = $this->getCurrentAmount();
249 $targetAmount = $this->getTargetAmount();
250 $goalTypeIsAmount = $this->isAmount();
251
252 $progressPercentage = ! $currentAmount || ! $targetAmount ? 0 : ($currentAmount / $targetAmount) * 100;
253
254 return [
255 'type' => $this->getGoalType()->getValue(),
256 'typeIsCount' => ! $goalTypeIsAmount,
257 'typeIsMoney' => $goalTypeIsAmount,
258 'enabled' => $this->isEnabled,
259 'show' => $this->isEnabled,
260 'currentAmount' => $currentAmount,
261 'targetAmount' => $targetAmount,
262 'label' => $this->getLabel(),
263 'percentage' => $progressPercentage,
264 'isAchieved' => $this->isEnabled && $this->formSettings->enableAutoClose && $progressPercentage >= 100,
265 ];
266 }
267
268 /**
269 * Get total donation revenue, the exception is for subscription amount goal, it will return the sum of initial amount
270 *
271 * @since 4.3.0
272 */
273 public function getTotalDonationRevenue()
274 {
275 if ($this->getGoalType()->getValue() === 'amountFromSubscriptions') {
276 $query = $this->getQuery();
277
278 return $query->sumInitialAmount();
279 }
280
281
282 $query = $this->goalSource->isCampaign()
283 ? new CampaignDonationQuery($this->campaign)
284 : (new DonationQuery())->form($this->formId);
285
286 return $query->sumIntendedAmount();
287 }
288 }
289