PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 4.3.2
GiveWP – Donation Plugin and Fundraising Platform v4.3.2
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 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / src / DonationForms / V2 / DataTransferObjects / DonationFormQueryData.php
give / src / DonationForms / V2 / DataTransferObjects Last commit date
DonationFormQueryData.php 1 year ago
DonationFormQueryData.php
227 lines
1 <?php
2
3 namespace Give\DonationForms\V2\DataTransferObjects;
4
5 use DateTime;
6 use Give\Campaigns\Models\Campaign;
7 use Give\DonationForms\Properties\FormSettings;
8 use Give\DonationForms\Properties\GoalSettings;
9 use Give\DonationForms\V2\Models\DonationForm;
10 use Give\DonationForms\V2\Properties\DonationFormLevel;
11 use Give\DonationForms\V2\ValueObjects\DonationFormMetaKeys as LegacyDonationFormMetaKeys;
12 use Give\DonationForms\V2\ValueObjects\DonationFormStatus;
13 use Give\DonationForms\ValueObjects\DonationFormMetaKeys;
14 use Give\DonationForms\ValueObjects\GoalType;
15 use Give\Framework\Support\Facades\DateTime\Temporal;
16 use Give\Framework\Support\ValueObjects\Money;
17
18 /**
19 * Class DonationFormQueryData
20 *
21 * @since 4.3.0 add GoalSettings
22 * @since 2.24.0
23 */
24 final class DonationFormQueryData
25 {
26
27 /**
28 * @var int
29 */
30 public $id;
31 /**
32 * @var string
33 */
34 public $title;
35 /**
36 * @var DonationFormLevel[]
37 */
38 public $levels;
39 /**
40 * @var boolean
41 */
42 public $goalOption;
43 /**
44 * @var int
45 */
46 public $totalNumberOfDonations;
47 /**
48 * @var Money
49 */
50 public $totalAmountDonated;
51 /**
52 * @var DateTime
53 */
54 public $createdAt;
55 /**
56 * @var DateTime
57 */
58 public $updatedAt;
59 /**
60 * @var string
61 */
62 public $status;
63
64 /**
65 * @since 4.3.0
66 */
67 public GoalSettings $goalSettings;
68
69 /**
70 * @since 4.3.0
71 */
72 public bool $usesFormBuilder;
73
74 /**
75 * @since 4.3.0
76 */
77 public int $campaignId;
78
79 /**
80 * Convert data from donation form object to DonationForm Model
81 *
82 * @since 2.24.0
83 *
84 * @param $object
85 *
86 * @return DonationFormQueryData
87 */
88 public static function fromObject($object): DonationFormQueryData
89 {
90 $self = new DonationFormQueryData();
91
92 $self->campaignId = 0;
93 $self->id = (int)$object->id;
94 $self->title = $object->title;
95 $self->levels = $self->getDonationFormLevels($object);
96 $self->goalOption = ($object->{LegacyDonationFormMetaKeys::GOAL_OPTION()->getKeyAsCamelCase()} === 'enabled');
97 $self->createdAt = Temporal::toDateTime($object->createdAt);
98 $self->updatedAt = Temporal::toDateTime($object->updatedAt);
99 $self->totalAmountDonated = Money::fromDecimal($object->{LegacyDonationFormMetaKeys::FORM_EARNINGS()->getKeyAsCamelCase()},
100 give_get_currency());
101 $self->totalNumberOfDonations = (int)$object->{LegacyDonationFormMetaKeys::FORM_SALES()->getKeyAsCamelCase()};
102 $self->status = new DonationFormStatus($object->status);
103 $self->goalSettings = $self->getGoalSettings($object);
104 $self->usesFormBuilder = (bool)$object->settings;
105
106 return $self;
107 }
108
109 /**
110 * Convert DTO to DonationForm
111 *
112 * @return DonationForm
113 */
114 public function toDonationForm(): DonationForm
115 {
116 $attributes = get_object_vars($this);
117
118 return new DonationForm($attributes);
119 }
120
121 /**
122 * @since 2.24.0
123 *
124 * @param $object
125 *
126 * @return DonationFormLevel[]
127 */
128 public function getDonationFormLevels($object): array
129 {
130 switch ($object->{LegacyDonationFormMetaKeys::PRICE_OPTION()->getKeyAsCamelCase()}) {
131 case 'multi':
132 $levels = maybe_unserialize($object->{LegacyDonationFormMetaKeys::DONATION_LEVELS()->getKeyAsCamelCase()});
133
134 if (empty($levels)) {
135 return [];
136 }
137
138 return array_map(static function ($level) {
139 return DonationFormLevel::fromArray($level);
140 }, $levels);
141 case 'set':
142 $amount = $object->{LegacyDonationFormMetaKeys::SET_PRICE()->getKeyAsCamelCase()};
143
144 if (empty($amount)) {
145 return [];
146 }
147
148 return [
149 DonationFormLevel::fromPrice($amount),
150 ];
151 default:
152 return [];
153 }
154 }
155
156
157 /**
158 * @since 4.3.0
159 */
160 private function getGoalSettings(object $queryObject): GoalSettings
161 {
162 $formSettings = $queryObject->{DonationFormMetaKeys::SETTINGS()->getKeyAsCamelCase()};
163
164 // v3 form
165 if ($formSettings) {
166 $settings = FormSettings::fromjson($formSettings);
167 // uses campaign goal settings
168 if ($settings->goalSource->isCampaign()) {
169 $campaign = Campaign::findByFormId($queryObject->id);
170 $this->campaignId = $campaign->id;
171
172 return GoalSettings::fromArray([
173 'goalSource' => $settings->goalSource->getValue(),
174 'enableDonationGoal' => $settings->enableDonationGoal,
175 'goalType' => $this->convertGoalType($campaign->goalType->getValue()),
176 'goalAmount' => $campaign->goal,
177 ]);
178 }
179
180 return GoalSettings::fromArray([
181 'goalSource' => $settings->goalSource->getValue(),
182 'enableDonationGoal' => $settings->enableDonationGoal,
183 'goalType' => $settings->goalType,
184 'goalAmount' => $settings->goalAmount,
185 ]);
186 }
187
188
189 // v2 form
190 return GoalSettings::fromArray([
191 'goalSource' => 'form',
192 'enableDonationGoal' => $queryObject->goalOption === 'enabled',
193 'goalType' => $this->convertGoalType($queryObject->goalFormat, (bool)$queryObject->recurringGoalFormat),
194 'goalAmount' => $queryObject->goalAmount,
195 ]);
196 }
197
198
199 /**
200 * @since 4.3.0
201 */
202 public function convertGoalType(string $type, bool $isRecurring = false): GoalType
203 {
204 switch ($type) {
205 case 'donation':
206 case 'donations':
207 return $isRecurring
208 ? GoalType::SUBSCRIPTIONS()
209 : GoalType::DONATIONS();
210 case 'donors':
211 return $isRecurring
212 ? GoalType::DONORS_FROM_SUBSCRIPTIONS()
213 : GoalType::DONORS();
214 case 'subscriptions':
215 return GoalType::SUBSCRIPTIONS();
216 case 'donorsFromSubscriptions':
217 return GoalType::DONORS_FROM_SUBSCRIPTIONS();
218 case 'amountFromSubscriptions':
219 return GoalType::AMOUNT_FROM_SUBSCRIPTIONS();
220 default:
221 return $isRecurring
222 ? GoalType::AMOUNT_FROM_SUBSCRIPTIONS()
223 : GoalType::AMOUNT();
224 }
225 }
226 }
227