1-config_panel_activity.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace cx_newsletter;
  3. class config_panel_activity
  4. extends activity {
  5. public function show_after_button() : null {
  6. return null;
  7. }
  8. public function inside_buttons() : array {
  9. return [ 'save' ];
  10. }
  11. public function inside_inputs() : array {
  12. return [
  13. 'source_address' => 'email',
  14. 'reply_to_address' => 'email',
  15. 'email_count' => 'int',
  16. 'sms_count' => 'int',
  17. 'apikey' => 'string',
  18. 'custom_smtp' => '?bool',
  19. 'smtp_address' => '?domain',
  20. 'smtp_port' => '?int',
  21. 'smtp_user' => '?string',
  22. 'smtp_password' => '?string'
  23. ];
  24. }
  25. public function get_site_template_name() : string {
  26. return 'config_panel';
  27. }
  28. private function validate_result() : ?string {
  29. if (!$this->is_validated('source_address')) {
  30. return 'Source E-Mail address is not valid.';
  31. }
  32. if (!$this->is_validated('reply_to_address')) {
  33. return 'Reply to E-Mail address it not valid.';
  34. }
  35. if (!$this->is_validated('email_count')) {
  36. return 'Count of the E-Mails is not valid.';
  37. }
  38. if (!$this->is_validated('sms_count')) {
  39. return 'Count of the SMS-es is not valid.';
  40. }
  41. if (!$this->is_validated('apikey')) {
  42. return 'Api Key is not valid.';
  43. }
  44. if (strlen($this->get_validated('apikey')) < 16) {
  45. return 'Api Key must has minimum 16 characters.';
  46. }
  47. if ($this->use_custom_smtp('true', null) !== null) {
  48. return $this->smtp_section_validate();
  49. }
  50. return null;
  51. }
  52. private function smtp_section_validate() : ?string {
  53. if (!$this->is_validated('smtp_address')) {
  54. return 'SMTP address is not property set.';
  55. }
  56. if (empty($this->get_validated('smtp_address'))) {
  57. return 'SMTP address can not be blank.';
  58. }
  59. if (!$this->is_validated('smtp_port')) {
  60. return 'SMTP port is not property set.';
  61. }
  62. if (empty($this->get_validated('smtp_port'))) {
  63. return 'SMTP port is not set.';
  64. }
  65. if (empty($this->get_validated('smtp_user'))) {
  66. return 'SMTP user can not be blank.';
  67. }
  68. if (!$this->is_validated('smtp_password')) {
  69. return 'SMTP password is not property set.';
  70. }
  71. if (empty($this->get_validated('smtp_password'))) {
  72. return 'SMTP password can not be blank';
  73. }
  74. return null;
  75. }
  76. private function use_custom_smtp(
  77. string $true = 'true',
  78. ?string $false = 'false'
  79. ) : ?string {
  80. if (!empty($this->get_received('custom_smtp'))) {
  81. return $true;
  82. }
  83. return $false;
  84. }
  85. private function save() {
  86. $validate = $this->validate_result();
  87. if ($validate !== null) {
  88. $this->error_toast($validate);
  89. return;
  90. }
  91. $settings = $this->get_settings();
  92. $settings
  93. ->save('source_address', $this->get_validated('source_address'))
  94. ->save('reply_to_address', $this->get_validated('reply_to_address'))
  95. ->save('email_count', $this->get_validated('email_count'))
  96. ->save('sms_count', $this->get_validated('sms_count'))
  97. ->save('apikey', $this->get_validated('apikey'))
  98. ->save('custom_smtp', $this->use_custom_smtp())
  99. ->save('smtp_address', $this->get_validated('smtp_address'))
  100. ->save('smtp_port', strval($this->get_validated('smtp_port')))
  101. ->save('smtp_user', $this->get_validated('smtp_user'))
  102. ->save('smtp_password', $this->get_validated('smtp_password'));
  103. $this->success_toast('Settings had been saved.');
  104. }
  105. public function process() : self {
  106. if ($this->is_received('save')) {
  107. $this->save();
  108. }
  109. $this->set('source_address', $this->get_settings()->get('source_address'));
  110. $this->set('reply_to_address', $this->get_settings()->get('reply_to_address'));
  111. $this->set('email_count', $this->get_settings()->get('email_count'));
  112. $this->set('sms_count', $this->get_settings()->get('sms_count'));
  113. $this->set('apikey', $this->get_settings()->get('apikey'));
  114. $this->set('smtp_address', $this->get_settings()->get('smtp_address'));
  115. $this->set('smtp_port', $this->get_settings()->get('smtp_port'));
  116. $this->set('smtp_user', $this->get_settings()->get('smtp_user'));
  117. $this->set('smtp_password', $this->get_settings()->get('smtp_password'));
  118. if ($this->get_settings()->use_custom_smtp()) {
  119. $this->set('custom_smtp_checked', 'checked');
  120. } else {
  121. $this->set('custom_smtp_checked', '');
  122. }
  123. return $this;
  124. }
  125. }