1-config_panel_activity.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. ];
  19. }
  20. public function get_site_template_name() : string {
  21. return 'config_panel';
  22. }
  23. private function validate_result() : ?string {
  24. if (!$this->is_validated('source_address')) {
  25. return 'Source E-Mail address is not valid.';
  26. }
  27. if (!$this->is_validated('reply_to_address')) {
  28. return 'Reply to E-Mail address it not valid.';
  29. }
  30. if (!$this->is_validated('email_count')) {
  31. return 'Count of the E-Mails is not valid.';
  32. }
  33. if (!$this->is_validated('sms_count')) {
  34. return 'Count of the SMS-es is not valid.';
  35. }
  36. if (!$this->is_validated('apikey')) {
  37. return 'Api Key is not valid.';
  38. }
  39. if (strlen($this->get_validated('apikey')) < 16) {
  40. return 'Api Key must has minimum 16 characters.';
  41. }
  42. return null;
  43. }
  44. private function save() {
  45. $validate = $this->validate_result();
  46. if ($validate !== null) {
  47. $this->error_toast($validate);
  48. return;
  49. }
  50. $settings = $this->get_settings();
  51. $settings
  52. ->save('source_address', $this->get_validated('source_address'))
  53. ->save('reply_to_address', $this->get_validated('reply_to_address'))
  54. ->save('email_count', $this->get_validated('email_count'))
  55. ->save('sms_count', $this->get_validated('sms_count'))
  56. ->save('apikey', $this->get_validated('apikey'));
  57. $this->success_toast('Settings had been saved.');
  58. }
  59. public function process() : self {
  60. if ($this->is_received('save')) {
  61. $this->save();
  62. }
  63. $this->set('source_address', $this->get_settings()->get('source_address'));
  64. $this->set('reply_to_address', $this->get_settings()->get('reply_to_address'));
  65. $this->set('email_count', $this->get_settings()->get('email_count'));
  66. $this->set('sms_count', $this->get_settings()->get('sms_count'));
  67. $this->set('apikey', $this->get_settings()->get('apikey'));
  68. return $this;
  69. }
  70. }