3-manage_messages_activity.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace cx_newsletter;
  3. class manage_messages_activity
  4. extends activity {
  5. public function show_after_button() : null {
  6. return null;
  7. }
  8. public function inside_buttons() : array {
  9. return [ 'remove' ];
  10. }
  11. public function inside_inputs() : array {
  12. return [
  13. 'id' => 'int'
  14. ];
  15. }
  16. public function get_site_template_name() : string {
  17. return 'manage_messages';
  18. }
  19. public function remove(messages_mapper $mapper) : void {
  20. if (!$this->is_validated('id')) {
  21. $this->error_toast('ID of the message is wrong.');
  22. }
  23. $message = new message($this->get_validated('id'));
  24. try {
  25. $mapper->remove($message);
  26. $this->success_toast('Removed success.');
  27. } catch (\exception $exception) {
  28. $this->error_toast($exception->getMessage());
  29. }
  30. }
  31. private function load_groups() : \cx_appengine\string_builder {
  32. $mapper = new groups_mapper(
  33. $this->get_database(),
  34. $this->get_tables()
  35. );
  36. $default = new group();
  37. $default->name = 'ALL';
  38. $groups = $mapper->load_all();
  39. $list = new \cx_appengine\string_builder();
  40. array_push($groups, $default);
  41. foreach ($groups as $group) {
  42. $options = [];
  43. $options['group_name'] = $group->name;
  44. if ($group->has_id()) {
  45. $options['group_id'] = $group->get_id();
  46. } else {
  47. $options['group_id'] = 'NULL';
  48. $options['selected'] = 'selected';
  49. }
  50. $list->push(
  51. $this
  52. ->get_templates()
  53. ->prepare('single_group_option')
  54. ->render($options)
  55. );
  56. }
  57. return $list;
  58. }
  59. public function process() : self {
  60. $converter = new messages_converter();
  61. $messages = new \cx_appengine\string_builder();
  62. $mapper = new messages_mapper(
  63. $this->get_database(),
  64. $this->get_tables()
  65. );
  66. if ($this->is_received('remove')) {
  67. $this->remove($mapper);
  68. }
  69. $groups = $this->load_groups();
  70. foreach ($mapper->load_all() as $message) {
  71. $options = $converter->load_object($message)->get_array();
  72. $options['groups'] = $groups;
  73. $messages->push(
  74. $this
  75. ->get_templates()
  76. ->prepare('manage_messages_single_message')
  77. ->render($options)
  78. );
  79. }
  80. $this->set('messages_place', $messages);
  81. return $this;
  82. }
  83. }