4-edit_message_activity.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace cx_newsletter;
  3. class edit_message_activity
  4. extends activity {
  5. public function show_after_button() : string {
  6. return 'create';
  7. }
  8. public function inside_buttons() : array {
  9. return [ 'edit', 'save' ];
  10. }
  11. public function inside_inputs() : array {
  12. return [
  13. 'id' => 'int',
  14. 'title' => 'string',
  15. 'content' => 'string'
  16. ];
  17. }
  18. public function get_site_template_name() : string {
  19. return 'edit_message';
  20. }
  21. public function prepare_edit() : void {
  22. if (!$this->is_validated('id')) {
  23. $this->error_toast('ID of the message to edit is wrong.');
  24. return;
  25. }
  26. $mapper = new messages_mapper(
  27. $this->get_database(),
  28. $this->get_tables()
  29. );
  30. $message = $mapper->complete(new message($this->get_validated('id')));
  31. $this->set('id', $message->get_id());
  32. $this->set('title', $message->title);
  33. $this->set('content', $message->content);
  34. }
  35. public function create() : void {
  36. $message = new message();
  37. $message->title = $this->get_validated('title');
  38. $message->content = stripslashes($this->get_validated('content'));
  39. $mapper = new messages_mapper(
  40. $this->get_database(),
  41. $this->get_tables()
  42. );
  43. try {
  44. $message = $mapper->create($message);
  45. $this->success_toast('Created successfull.');
  46. $this->set('id', $message->get_id());
  47. $this->set('title', $message->title);
  48. $this->set('content', $message->content);
  49. } catch (\exception $exception) {
  50. $this->error_toast($exception->getMessage());
  51. }
  52. }
  53. public function own_validation() : ?string {
  54. if (!$this->is_validated('title')) {
  55. return 'Title is not sended.';
  56. }
  57. if (!$this->is_validated('content')) {
  58. return 'Content is not sended.';
  59. }
  60. $title = $this->get_validated('title');
  61. $content = $this->get_validated('content');
  62. if ($this->is_validated('id')) {
  63. $this->set('id', $this->get_validated('id'));
  64. }
  65. if (strlen($title) < 8) {
  66. return 'Title must be longer than 8 characters.';
  67. }
  68. if (\esc_html($title) !== $title) {
  69. return 'Title can not has HTML characters.';
  70. }
  71. return null;
  72. }
  73. public function save() : void {
  74. $result = $this->own_validation();
  75. if ($result !== null) {
  76. $this->warning_toast($result);
  77. return;
  78. }
  79. if (!$this->is_validated('id')) {
  80. $this->create();
  81. return;
  82. }
  83. $mapper = new messages_mapper(
  84. $this->get_database(),
  85. $this->get_tables()
  86. );
  87. $message = new message($this->get_validated('id'));
  88. $message->title = $this->get_validated('title');
  89. $message->content = stripslashes($this->get_validated('content'));
  90. try {
  91. $mapper->save($message);
  92. $this->success_toast('Message saved.');
  93. $this->set('id', $message->get_id());
  94. $this->set('title', $message->title);
  95. $this->set('content', $message->content);
  96. } catch (\exception $exception) {
  97. $this->error_toast($exception->getMessage());
  98. }
  99. }
  100. public function process() : self {
  101. if ($this->is_validated('title')) {
  102. $this->set('title', $this->get_validated('title'));
  103. }
  104. if ($this->is_validated('content')) {
  105. $this->set(
  106. 'content',
  107. stripslashes($this->get_validated('content'))
  108. );
  109. }
  110. if ($this->is_validated('id')) {
  111. $this->set('id', $this->get_validated('id'));
  112. }
  113. if ($this->is_received('save')) {
  114. $this->save();
  115. }
  116. if ($this->is_received('edit')) {
  117. $this->prepare_edit();
  118. }
  119. $content = '';
  120. if ($this->get('content') !== null) {
  121. $content = $this->get('content');
  122. }
  123. \ob_start();
  124. \wp_editor($content, 'content', [
  125. 'textarea_rows' => '',
  126. 'quicktags' => false,
  127. 'wpautoup' => false,
  128. 'media_buttons' => true,
  129. 'textarea_name' => 'content',
  130. 'teeny' => true,
  131. 'tinymce' => [
  132. 'content_css' => false,
  133. 'inline' => false
  134. ]
  135. ]);
  136. $result = \ob_get_clean();
  137. $this->set('editor', $result);
  138. return $this;
  139. }
  140. }