FormSettings.php
351 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\Properties; |
| 4 | |
| 5 | use Give\DonationForms\ValueObjects\DesignSettingsImageStyle; |
| 6 | use Give\DonationForms\ValueObjects\DesignSettingsLogoPosition; |
| 7 | use Give\DonationForms\ValueObjects\DesignSettingsSectionStyle; |
| 8 | use Give\DonationForms\ValueObjects\DesignSettingsTextFieldStyle; |
| 9 | use Give\DonationForms\ValueObjects\DonationFormStatus; |
| 10 | use Give\DonationForms\ValueObjects\GoalType; |
| 11 | use Give\Framework\Support\Contracts\Arrayable; |
| 12 | use Give\Framework\Support\Contracts\Jsonable; |
| 13 | |
| 14 | /** |
| 15 | * @since 3.2.0 Remove addSlashesRecursive method |
| 16 | * @since 3.0.0 |
| 17 | */ |
| 18 | class FormSettings implements Arrayable, Jsonable |
| 19 | { |
| 20 | /** |
| 21 | * @var boolean |
| 22 | */ |
| 23 | public $showHeader; |
| 24 | /** |
| 25 | * @var boolean |
| 26 | */ |
| 27 | public $showHeading; |
| 28 | /** |
| 29 | * @var boolean |
| 30 | */ |
| 31 | public $showDescription; |
| 32 | /** |
| 33 | * @var string |
| 34 | */ |
| 35 | public $formTitle; |
| 36 | /** |
| 37 | * @var boolean |
| 38 | */ |
| 39 | public $enableDonationGoal; |
| 40 | /** |
| 41 | * @var boolean |
| 42 | */ |
| 43 | public $enableAutoClose; |
| 44 | /** |
| 45 | * @var GoalType |
| 46 | */ |
| 47 | public $goalType; |
| 48 | /** |
| 49 | * @var string |
| 50 | */ |
| 51 | public $designId; |
| 52 | /** |
| 53 | * @var string |
| 54 | */ |
| 55 | public $heading; |
| 56 | /** |
| 57 | * @var string |
| 58 | */ |
| 59 | public $description; |
| 60 | /** |
| 61 | * @var string |
| 62 | */ |
| 63 | public $primaryColor; |
| 64 | /** |
| 65 | * @var string |
| 66 | */ |
| 67 | public $secondaryColor; |
| 68 | /** |
| 69 | * @var float |
| 70 | */ |
| 71 | public $goalAmount; |
| 72 | /** |
| 73 | * @since 3.2.0 Added registrationNotification property. |
| 74 | * @var string |
| 75 | */ |
| 76 | public $registrationNotification; |
| 77 | /** |
| 78 | * @var string |
| 79 | */ |
| 80 | public $customCss; |
| 81 | /** |
| 82 | * @var string |
| 83 | */ |
| 84 | public $goalAchievedMessage; |
| 85 | |
| 86 | /** |
| 87 | * @var string |
| 88 | */ |
| 89 | public $pageSlug; |
| 90 | |
| 91 | /** |
| 92 | * @var string |
| 93 | */ |
| 94 | public $receiptHeading; |
| 95 | |
| 96 | /** |
| 97 | * @var string |
| 98 | */ |
| 99 | public $receiptDescription; |
| 100 | |
| 101 | /** |
| 102 | * @var DonationFormStatus |
| 103 | */ |
| 104 | public $formStatus; |
| 105 | |
| 106 | /** |
| 107 | * @var array |
| 108 | */ |
| 109 | public $emailTemplateOptions; |
| 110 | |
| 111 | /** |
| 112 | * @var string |
| 113 | * @todo Extract to a value object. |
| 114 | */ |
| 115 | public $emailOptionsStatus; |
| 116 | |
| 117 | /** |
| 118 | * @var string |
| 119 | */ |
| 120 | public $emailTemplate; |
| 121 | |
| 122 | /** |
| 123 | * @var string |
| 124 | */ |
| 125 | public $emailLogo; |
| 126 | |
| 127 | /** |
| 128 | * @var string |
| 129 | */ |
| 130 | public $emailFromName; |
| 131 | |
| 132 | /** |
| 133 | * @var string |
| 134 | */ |
| 135 | public $emailFromEmail; |
| 136 | |
| 137 | /** |
| 138 | * @var boolean |
| 139 | */ |
| 140 | public $formGridCustomize; |
| 141 | |
| 142 | /** |
| 143 | * @var string |
| 144 | */ |
| 145 | public $formGridRedirectUrl; |
| 146 | |
| 147 | /** |
| 148 | * @var string |
| 149 | */ |
| 150 | public $formGridDonateButtonText; |
| 151 | |
| 152 | /** |
| 153 | * @var boolean |
| 154 | */ |
| 155 | public $formGridHideDocumentationLink; |
| 156 | |
| 157 | /** |
| 158 | * @var boolean |
| 159 | */ |
| 160 | public $offlineDonationsCustomize; |
| 161 | |
| 162 | /** |
| 163 | * @var string |
| 164 | */ |
| 165 | public $offlineDonationsInstructions; |
| 166 | |
| 167 | /** |
| 168 | * @var string |
| 169 | */ |
| 170 | public $donateButtonCaption; |
| 171 | /** |
| 172 | * @var string |
| 173 | */ |
| 174 | public $multiStepFirstButtonText; |
| 175 | /** |
| 176 | * @var string |
| 177 | */ |
| 178 | public $multiStepNextButtonText; |
| 179 | |
| 180 | /** |
| 181 | * @var array |
| 182 | */ |
| 183 | public $pdfSettings; |
| 184 | |
| 185 | /** |
| 186 | * @since 3.4.0 |
| 187 | * @var string |
| 188 | */ |
| 189 | public $designSettingsImageUrl; |
| 190 | |
| 191 | /** |
| 192 | * @since 3.4.0 |
| 193 | * @var string |
| 194 | */ |
| 195 | public $designSettingsImageStyle; |
| 196 | |
| 197 | /** |
| 198 | * @since 3.4.0 |
| 199 | * @var string |
| 200 | */ |
| 201 | public $designSettingsLogoUrl; |
| 202 | |
| 203 | /** |
| 204 | * @since 3.4.0 |
| 205 | * @var string |
| 206 | */ |
| 207 | public $designSettingsLogoPosition; |
| 208 | |
| 209 | /** |
| 210 | * @since 3.4.0 |
| 211 | * @var string |
| 212 | */ |
| 213 | public $designSettingsSectionStyle; |
| 214 | |
| 215 | /** |
| 216 | * @since 3.4.0 |
| 217 | * @var string |
| 218 | */ |
| 219 | public $designSettingsTextFieldStyle; |
| 220 | |
| 221 | /** |
| 222 | * @since 3.2.0 Added registrationNotification |
| 223 | * @since 3.0.0 |
| 224 | */ |
| 225 | public static function fromArray(array $array): self |
| 226 | { |
| 227 | $self = new self(); |
| 228 | |
| 229 | $self->showHeader = $array['showHeader'] ?? true; |
| 230 | $self->showHeading = $array['showHeading'] ?? true; |
| 231 | $self->heading = $array['heading'] ?? __('Support Our Cause', 'give'); |
| 232 | $self->showDescription = $array['showDescription'] ?? true; |
| 233 | $self->description = $array['description'] ?? __( |
| 234 | 'Help our organization by donating today! Donations go to making a difference for our cause.', |
| 235 | 'give' |
| 236 | ); |
| 237 | $self->formTitle = $array['formTitle'] ?? __('Donation Form', 'give'); |
| 238 | $self->donateButtonCaption = $array['donateButtonCaption'] ?? __('Donate now', 'give'); |
| 239 | $self->enableDonationGoal = $array['enableDonationGoal'] ?? false; |
| 240 | $self->enableAutoClose = $array['enableAutoClose'] ?? false; |
| 241 | $self->goalType = ! empty($array['goalType']) && GoalType::isValid($array['goalType']) ? new GoalType( |
| 242 | $array['goalType'] |
| 243 | ) : GoalType::AMOUNT(); |
| 244 | $self->designId = $array['designId'] ?? null; |
| 245 | $self->primaryColor = $array['primaryColor'] ?? '#69b86b'; |
| 246 | $self->secondaryColor = $array['secondaryColor'] ?? '#f49420'; |
| 247 | $self->goalAmount = $array['goalAmount'] ?? 0; |
| 248 | $self->registrationNotification = $array['registrationNotification'] ?? false; |
| 249 | $self->customCss = $array['customCss'] ?? ''; |
| 250 | $self->pageSlug = $array['pageSlug'] ?? ''; |
| 251 | $self->goalAchievedMessage = $array['goalAchievedMessage'] ?? __( |
| 252 | 'Thank you to all our donors, we have met our fundraising goal.', |
| 253 | 'give' |
| 254 | ); |
| 255 | $self->receiptHeading = $array['receiptHeading'] ?? __( |
| 256 | 'Hey {first_name}, thanks for your donation!', |
| 257 | 'give' |
| 258 | ); |
| 259 | $self->receiptDescription = $array['receiptDescription'] ?? __( |
| 260 | '{first_name}, your contribution means a lot and will be put to good use in making a difference. We’ve sent your donation receipt to {email}.', |
| 261 | 'give' |
| 262 | ); |
| 263 | $self->formStatus = ! empty($array['formStatus']) ? new DonationFormStatus( |
| 264 | $array['formStatus'] |
| 265 | ) : DonationFormStatus::DRAFT(); |
| 266 | |
| 267 | $self->formGridCustomize = $array['formGridCustomize'] ?? false; |
| 268 | $self->formGridRedirectUrl = $array['formGridRedirectUrl'] ?? ''; |
| 269 | $self->formGridDonateButtonText = $array['formGridDonateButtonText'] ?? ''; |
| 270 | $self->formGridHideDocumentationLink = $array['formGridHideDocumentationLink'] ?? false; |
| 271 | |
| 272 | $self->emailTemplateOptions = $array['emailTemplateOptions'] ?? []; |
| 273 | |
| 274 | $self->emailOptionsStatus = $array['emailOptionsStatus'] ?? 'global'; |
| 275 | |
| 276 | $self->emailTemplate = $array['emailTemplate'] ?? 'default'; |
| 277 | |
| 278 | $self->emailFromName = $array['emailFromName'] ?? ''; |
| 279 | |
| 280 | $self->emailFromEmail = $array['emailFromEmail'] ?? ''; |
| 281 | |
| 282 | $self->emailLogo = $array['emailLogo'] ?? ''; |
| 283 | |
| 284 | $self->offlineDonationsCustomize = $array['offlineDonationsCustomize'] ?? false; |
| 285 | |
| 286 | $self->offlineDonationsInstructions = $array['offlineDonationsInstructions'] ?? ''; |
| 287 | |
| 288 | $self->multiStepFirstButtonText = $array['multiStepFirstButtonText'] ?? __('Donate now', 'give'); |
| 289 | |
| 290 | $self->multiStepNextButtonText = $array['multiStepNextButtonText'] ?? __('Continue', 'give'); |
| 291 | |
| 292 | $self->pdfSettings = isset($array['pdfSettings']) && is_array( |
| 293 | $array['pdfSettings'] |
| 294 | ) ? $array['pdfSettings'] : []; |
| 295 | |
| 296 | $self->designSettingsImageUrl = $array['designSettingsImageUrl'] ?? ''; |
| 297 | $self->designSettingsImageStyle = ! empty($array['designSettingsImageStyle']) ? new DesignSettingsImageStyle( |
| 298 | $array['designSettingsImageStyle'] |
| 299 | ) : DesignSettingsImageStyle::BACKGROUND(); |
| 300 | |
| 301 | $self->designSettingsLogoUrl = $array['designSettingsLogoUrl'] ?? ''; |
| 302 | $self->designSettingsLogoPosition = ! empty($array['designSettingsLogoPosition']) ? new DesignSettingsLogoPosition( |
| 303 | $array['designSettingsLogoPosition'] |
| 304 | ) : DesignSettingsLogoPosition::LEFT(); |
| 305 | |
| 306 | $self->designSettingsSectionStyle = ! empty($array['designSettingsSectionStyle']) ? new DesignSettingsSectionStyle( |
| 307 | $array['designSettingsSectionStyle'] |
| 308 | ) : DesignSettingsSectionStyle::DEFAULT(); |
| 309 | |
| 310 | $self->designSettingsTextFieldStyle = ! empty($array['designSettingsTextFieldStyle']) ? new DesignSettingsTextFieldStyle( |
| 311 | $array['designSettingsTextFieldStyle'] |
| 312 | ) : DesignSettingsTextFieldStyle::DEFAULT(); |
| 313 | |
| 314 | return $self; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * @since 3.0.0 |
| 319 | */ |
| 320 | public static function fromJson(string $json): self |
| 321 | { |
| 322 | return self::fromArray( |
| 323 | json_decode($json, true) |
| 324 | ); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * @since 3.0.0 |
| 329 | */ |
| 330 | public function toArray(): array |
| 331 | { |
| 332 | return get_object_vars($this); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * @since 3.2.0 Remove call to addSlashesRecursive method for emailTemplateOptions in favor of SanitizeDonationFormPreviewRequest class |
| 337 | * @since 3.0.0 |
| 338 | */ |
| 339 | public function toJson($options = 0): string |
| 340 | { |
| 341 | return json_encode( |
| 342 | array_merge( |
| 343 | $this->toArray(), |
| 344 | [ |
| 345 | 'goalType' => $this->goalType ? $this->goalType->getValue() : null, |
| 346 | ] |
| 347 | ) |
| 348 | ); |
| 349 | } |
| 350 | } |
| 351 |