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 |