| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\FormBuilder\Actions; |
| 4 |
|
| 5 |
use Give\FormBuilder\DataTransferObjects\EmailNotificationData; |
| 6 |
|
| 7 |
/** |
| 8 |
* Convert data from legacy configuration into DTO. |
| 9 |
* |
| 10 |
* @since 3.0.0 |
| 11 |
*/ |
| 12 |
class ConvertLegacyNotificationToEmailNotificationData |
| 13 |
{ |
| 14 |
/** @var string */ |
| 15 |
protected $id; |
| 16 |
|
| 17 |
/** @var string */ |
| 18 |
protected $title; |
| 19 |
|
| 20 |
/** @var array */ |
| 21 |
protected $fields; |
| 22 |
|
| 23 |
public function __construct(array $notification) |
| 24 |
{ |
| 25 |
$this->id = $notification['id']; |
| 26 |
$this->title = $notification['title']; |
| 27 |
$this->fields = $notification['fields']; |
| 28 |
} |
| 29 |
|
| 30 |
public function __invoke(): EmailNotificationData |
| 31 |
{ |
| 32 |
$dto = new EmailNotificationData; |
| 33 |
|
| 34 |
$dto->id = $this->id; |
| 35 |
$dto->title = $this->title; |
| 36 |
$dto->statusOptions = $this->getStatusOptions(); |
| 37 |
$dto->supportsRecipients = $this->hasRecipientField(); |
| 38 |
$dto->defaultValues = $this->getDefaultValues(); |
| 39 |
|
| 40 |
return $dto; |
| 41 |
} |
| 42 |
|
| 43 |
protected function getStatusOptions() |
| 44 |
{ |
| 45 |
try { |
| 46 |
$field = $this->findFieldBy('id', "_give_{$this->id}_notification"); |
| 47 |
$formattedOptions = []; |
| 48 |
foreach($field['options'] as $value => $label) { |
| 49 |
$formattedOptions[] = [ |
| 50 |
'value' => $value, |
| 51 |
'label' => $label, |
| 52 |
]; |
| 53 |
} |
| 54 |
return $formattedOptions; |
| 55 |
} catch(\Exception $e) { |
| 56 |
return []; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
protected function hasRecipientField(): bool |
| 61 |
{ |
| 62 |
try { |
| 63 |
$this->findFieldBy('id', "_give_{$this->id}_recipient"); |
| 64 |
return true; |
| 65 |
} catch(\Exception $e) { |
| 66 |
return false; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
protected function findFieldBy($key, $value) |
| 71 |
{ |
| 72 |
foreach( $this->fields as $field ) { |
| 73 |
if( isset($field[$key]) && $field[$key] === $value ) { |
| 74 |
return $field; |
| 75 |
} |
| 76 |
} |
| 77 |
throw new \Exception("Field not found with $key of '$value'"); |
| 78 |
} |
| 79 |
|
| 80 |
protected function getDefaultValues(): array |
| 81 |
{ |
| 82 |
$defaultValues = []; |
| 83 |
foreach($this->fields as $field){ |
| 84 |
$key = str_replace("_give_{$this->id}_", '', $field['id']); |
| 85 |
$defaultValues[$key] = $field['default'] ?? ''; |
| 86 |
} |
| 87 |
|
| 88 |
if (!isset($defaultValues['email_message'])) { |
| 89 |
foreach ($this->fields as $field) { |
| 90 |
if (strpos($field['id'], '_message') !== false) { |
| 91 |
$defaultValues['email_message'] = $field['default'] ?? ''; |
| 92 |
break; |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if (!isset($defaultValues['email_subject'])) { |
| 98 |
foreach ($this->fields as $field) { |
| 99 |
if (strpos($field['id'], '_subject') !== false) { |
| 100 |
$defaultValues['email_subject'] = $field['default'] ?? ''; |
| 101 |
break; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
return $defaultValues; |
| 107 |
} |
| 108 |
} |
| 109 |
|